STM32F10x_StdPeriph_Examples/USART/IrDA/Receive/main.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    USART/IrDA/Receive/main.c 
00004   * @author  MCD Application Team
00005   * @version V3.4.0
00006   * @date    10/15/2010
00007   * @brief   Main program body
00008   ******************************************************************************
00009   * @copy
00010   *
00011   * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
00012   * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
00013   * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
00014   * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
00015   * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
00016   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
00017   *
00018   * <h2><center>&copy; COPYRIGHT 2010 STMicroelectronics</center></h2>
00019   */ 
00020 
00021 /* Includes ------------------------------------------------------------------*/
00022 #include "stm32f10x.h"
00023 #include "platform_config.h"
00024 
00025 /** @addtogroup STM32F10x_StdPeriph_Examples
00026   * @{
00027   */
00028 
00029 /** @addtogroup USART_IrDA_Receive
00030   * @{
00031   */ 
00032 
00033 /* Private typedef -----------------------------------------------------------*/
00034 /* Private define ------------------------------------------------------------*/
00035 /* Private macro -------------------------------------------------------------*/
00036 /* Private variables ---------------------------------------------------------*/
00037 USART_InitTypeDef USART_InitStructure;
00038 JOYState_TypeDef ReceivedData = JOY_NONE;
00039 
00040 /* Private function prototypes -----------------------------------------------*/
00041 void RCC_Configuration(void);
00042 void GPIO_Configuration(void);
00043 
00044 /* Private functions ---------------------------------------------------------*/
00045 
00046 /**
00047   * @brief   Main program
00048   * @param  None
00049   * @retval None
00050   */
00051 int main(void)
00052 {
00053   /*!< At this stage the microcontroller clock setting is already configured, 
00054        this is done through SystemInit() function which is called from startup
00055        file (startup_stm32f10x_xx.s) before to branch to application main.
00056        To reconfigure the default setting of SystemInit() function, refer to
00057        system_stm32f10x.c file
00058      */     
00059        
00060   /* System Clocks Configuration */
00061   RCC_Configuration();
00062 
00063   /* Configure the GPIO ports */
00064   GPIO_Configuration();
00065 
00066   /* Initialize Leds mounted on STM3210X-EVAL board */
00067   STM_EVAL_LEDInit(LED1);
00068   STM_EVAL_LEDInit(LED2);
00069   STM_EVAL_LEDInit(LED3);
00070   STM_EVAL_LEDInit(LED4);
00071   
00072 /* USARTy configuration ------------------------------------------------------*/
00073   /* USARTy configured as follow:
00074         - BaudRate = 115200 baud  
00075         - Word Length = 8 Bits
00076         - One Stop Bit
00077         - No parity
00078         - Hardware flow control disabled (RTS and CTS signals)
00079         - Receive and transmit enabled
00080   */
00081   USART_InitStructure.USART_BaudRate = 115200;
00082   USART_InitStructure.USART_WordLength = USART_WordLength_8b;
00083   USART_InitStructure.USART_StopBits = USART_StopBits_1;
00084   USART_InitStructure.USART_Parity = USART_Parity_No ;
00085   USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
00086   USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
00087   
00088   /* Configure the USARTy */
00089   USART_Init(USARTy, &USART_InitStructure);
00090   /* Enable the USARTy */
00091   USART_Cmd(USARTy, ENABLE);
00092 
00093   /* Set the USARTy prescaler */
00094   USART_SetPrescaler(USARTy, 0x1);
00095   /* Configure the USARTy IrDA mode */
00096   USART_IrDAConfig(USARTy, USART_IrDAMode_Normal);
00097 
00098   /* Enable the USARTy IrDA mode */
00099   USART_IrDACmd(USARTy, ENABLE);
00100 
00101   while (1)
00102   {
00103     /* Wait until a byte is received */
00104     while(USART_GetFlagStatus(USARTy, USART_FLAG_RXNE) == RESET)
00105     {
00106     }
00107     /* Read the received byte */
00108     ReceivedData = (JOYState_TypeDef)USART_ReceiveData(USARTy);
00109 
00110     switch(ReceivedData)
00111     {
00112       case JOY_UP:
00113         STM_EVAL_LEDOn(LED1);
00114         STM_EVAL_LEDOff(LED2);
00115         STM_EVAL_LEDOff(LED3);
00116         STM_EVAL_LEDOff(LED4);     
00117         break;
00118       case JOY_DOWN:
00119         STM_EVAL_LEDOn(LED2);
00120         STM_EVAL_LEDOff(LED1);
00121         STM_EVAL_LEDOff(LED3);
00122         STM_EVAL_LEDOff(LED4); 
00123         break;
00124       case JOY_LEFT:
00125         STM_EVAL_LEDOn(LED3);
00126         STM_EVAL_LEDOff(LED1);
00127         STM_EVAL_LEDOff(LED2);
00128         STM_EVAL_LEDOff(LED4);
00129         break;
00130       case JOY_RIGHT:
00131         STM_EVAL_LEDOn(LED4);
00132         STM_EVAL_LEDOff(LED1);
00133         STM_EVAL_LEDOff(LED2);
00134         STM_EVAL_LEDOff(LED3);        
00135         break;
00136       case JOY_SEL:
00137         STM_EVAL_LEDOn(LED1);
00138         STM_EVAL_LEDOn(LED2);
00139         STM_EVAL_LEDOn(LED3);
00140         STM_EVAL_LEDOn(LED4);
00141         break;
00142       case JOY_NONE:
00143         break;
00144       default:
00145         break;
00146     }
00147   }
00148 }
00149 
00150 /**
00151   * @brief  Configures the different system clocks.
00152   * @param  None
00153   * @retval None
00154   */
00155 void RCC_Configuration(void)
00156 {   
00157   /* Enable GPIO clock */
00158   RCC_APB2PeriphClockCmd(USARTy_GPIO_CLK | RCC_APB2Periph_AFIO, ENABLE);
00159   
00160   /* Enable USARTy clocks */
00161   RCC_APB1PeriphClockCmd(USARTy_CLK, ENABLE);
00162 }
00163 
00164 /**
00165   * @brief  Configures the different GPIO ports.
00166   * @param  None
00167   * @retval None
00168   */
00169 void GPIO_Configuration(void)
00170 {
00171   GPIO_InitTypeDef GPIO_InitStructure;
00172 
00173 #ifndef USE_STM3210C_EVAL
00174   /* Enable the USART3 Pins Partial Software Remapping */
00175   GPIO_PinRemapConfig(GPIO_PartialRemap_USART3, ENABLE);
00176 #else
00177   /* Enable the USART2 Pins Software Remapping */
00178   GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);
00179 #endif  
00180 
00181   /* Configure USARTy Tx as alternate function push-pull */
00182   GPIO_InitStructure.GPIO_Pin = USARTy_TxPin;
00183   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
00184   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
00185   GPIO_Init(USARTy_GPIO, &GPIO_InitStructure);
00186   
00187   /* Configure USARTy Rx as input floating */
00188   GPIO_InitStructure.GPIO_Pin = USARTy_RxPin;
00189   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
00190   GPIO_Init(USARTy_GPIO, &GPIO_InitStructure);
00191 }
00192 
00193 #ifdef  USE_FULL_ASSERT
00194 
00195 /**
00196   * @brief  Reports the name of the source file and the source line number
00197   *         where the assert_param error has occurred.
00198   * @param  file: pointer to the source file name
00199   * @param  line: assert_param error line source number
00200   * @retval None
00201   */
00202 void assert_failed(uint8_t* file, uint32_t line)
00203 { 
00204   /* User can add his own implementation to report the file name and line number,
00205      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00206 
00207   /* Infinite loop */
00208   while (1)
00209   {
00210   }
00211 }
00212 #endif
00213 
00214 /**
00215   * @}
00216   */ 
00217 
00218 /**
00219   * @}
00220   */ 
00221 
00222 /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/
STM32F10x Standard Peripherals Library: Footer

 

 

 

      For complete documentation on STM32(CORTEX M3) 32-bit Microcontrollers platform visit  www.st.com/STM32