STM32F10x_StdPeriph_Examples/USART/IrDA/Transmit/main.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    USART/IrDA/Transmit/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_Transmit
00030   * @{
00031   */ 
00032 
00033 /* Private typedef -----------------------------------------------------------*/
00034 /* Private define ------------------------------------------------------------*/
00035 /* Private macro -------------------------------------------------------------*/
00036 /* Private variables ---------------------------------------------------------*/
00037 USART_InitTypeDef USART_InitStructure;
00038 JOYState_TypeDef MyKey = JOY_NONE;
00039 
00040 /* Private function prototypes -----------------------------------------------*/
00041 void RCC_Configuration(void);
00042 void GPIO_Configuration(void);
00043 JOYState_TypeDef ReadKey(void);
00044 
00045 /* Private functions ---------------------------------------------------------*/
00046 
00047 /**
00048   * @brief   Main program
00049   * @param  None
00050   * @retval None
00051   */
00052 int main(void)
00053 {
00054   /*!< At this stage the microcontroller clock setting is already configured, 
00055        this is done through SystemInit() function which is called from startup
00056        file (startup_stm32f10x_xx.s) before to branch to application main.
00057        To reconfigure the default setting of SystemInit() function, refer to
00058        system_stm32f10x.c file
00059      */     
00060        
00061   /* System Clocks Configuration */
00062   RCC_Configuration();
00063 
00064   /* Configure the GPIO ports */
00065   GPIO_Configuration();
00066 
00067 #ifndef USE_STM3210C_EVAL
00068   /* Initialize JoyStick Button mounted on STM3210X-EVAL board */       
00069   STM_EVAL_PBInit(BUTTON_UP, BUTTON_MODE_GPIO);
00070   STM_EVAL_PBInit(BUTTON_DOWN, BUTTON_MODE_GPIO);
00071   STM_EVAL_PBInit(BUTTON_LEFT, BUTTON_MODE_GPIO);
00072   STM_EVAL_PBInit(BUTTON_RIGHT, BUTTON_MODE_GPIO);
00073   STM_EVAL_PBInit(BUTTON_SEL, BUTTON_MODE_GPIO);
00074 #else
00075   /* Configure the IO Expander */
00076   if (IOE_Config())
00077   {
00078     /* IO Expander config error */
00079     while(1);
00080   }
00081 #endif  
00082      
00083 /* USARTy configuration ------------------------------------------------------*/
00084   /* USARTy configured as follow:
00085         - BaudRate = 115200 baud  
00086         - Word Length = 8 Bits
00087         - One Stop Bit
00088         - No parity
00089         - Hardware flow control disabled (RTS and CTS signals)
00090         - Receive and transmit enabled
00091   */
00092   USART_InitStructure.USART_BaudRate = 115200;
00093   USART_InitStructure.USART_WordLength = USART_WordLength_8b;
00094   USART_InitStructure.USART_StopBits = USART_StopBits_1;
00095   USART_InitStructure.USART_Parity = USART_Parity_No ;
00096   USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
00097   USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
00098 
00099   /* Configure the USARTy */
00100   USART_Init(USARTy, &USART_InitStructure);
00101   /* Enable the USARTy */
00102   USART_Cmd(USARTy, ENABLE);
00103   /* Set the USARTy prescaler */
00104   USART_SetPrescaler(USARTy, 0x1);
00105   /* Configure the USARTy IrDA mode */
00106   USART_IrDAConfig(USARTy, USART_IrDAMode_Normal);
00107 
00108   /* Enable the USARTy IrDA mode */
00109   USART_IrDACmd(USARTy, ENABLE);
00110 
00111 
00112   while (1)
00113   {
00114     /* Read Key */
00115     MyKey = ReadKey();
00116 
00117     switch(MyKey)
00118     {
00119       case JOY_UP:
00120         USART_SendData(USARTy, JOY_UP);
00121         while(USART_GetFlagStatus(USARTy, USART_FLAG_TXE) == RESET)
00122         {
00123         }
00124         break;
00125       case JOY_DOWN:
00126         USART_SendData(USARTy, JOY_DOWN);
00127         while(USART_GetFlagStatus(USARTy, USART_FLAG_TXE) == RESET)
00128         {
00129         }
00130         break;
00131       case JOY_LEFT:
00132         USART_SendData(USARTy, JOY_LEFT);
00133         while(USART_GetFlagStatus(USARTy, USART_FLAG_TXE) == RESET)
00134         {
00135         }
00136         break;
00137       case JOY_RIGHT:
00138         USART_SendData(USARTy, JOY_RIGHT);
00139         while(USART_GetFlagStatus(USARTy, USART_FLAG_TXE) == RESET)
00140         {
00141         }
00142         break;
00143       case JOY_SEL:
00144         USART_SendData(USARTy, JOY_SEL);
00145         while(USART_GetFlagStatus(USARTy, USART_FLAG_TXE) == RESET)
00146         {
00147         }
00148         break;
00149       case JOY_NONE:
00150         USART_SendData(USARTy, JOY_NONE);
00151         while(USART_GetFlagStatus(USARTy, USART_FLAG_TXE) == RESET)
00152         {
00153         } 
00154         break;
00155       default:
00156         break;
00157     }
00158   }
00159 }
00160 
00161 /**
00162   * @brief  Configures the different system clocks.
00163   * @param  None
00164   * @retval None
00165   */
00166 void RCC_Configuration(void)
00167 {  
00168   /* Enable GPIO clock */
00169   RCC_APB2PeriphClockCmd(USARTy_GPIO_CLK | RCC_APB2Periph_AFIO, ENABLE);
00170   
00171   /* Enable USARTy clocks */
00172   RCC_APB1PeriphClockCmd(USARTy_CLK, ENABLE);
00173 }
00174 
00175 /**
00176   * @brief  Configures the different GPIO ports.
00177   * @param  None
00178   * @retval None
00179   */
00180 void GPIO_Configuration(void)
00181 {
00182   GPIO_InitTypeDef GPIO_InitStructure;
00183 
00184 #ifndef USE_STM3210C_EVAL
00185   /* Enable the USART3 Pins Partial Software Remapping */
00186   GPIO_PinRemapConfig(GPIO_PartialRemap_USART3, ENABLE);
00187 #else
00188   /* Enable the USART2 Pins Software Remapping */
00189   GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);
00190 #endif  
00191 
00192   /* Configure USARTy Tx as alternate function push-pull */
00193   GPIO_InitStructure.GPIO_Pin = USARTy_TxPin;
00194   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
00195   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
00196   GPIO_Init(USARTy_GPIO, &GPIO_InitStructure);
00197   
00198   /* Configure USARTy Rx as input floating */
00199   GPIO_InitStructure.GPIO_Pin = USARTy_RxPin;
00200   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
00201   GPIO_Init(USARTy_GPIO, &GPIO_InitStructure);
00202 }
00203 
00204 /**
00205   * @brief  Reads key from demoboard.
00206   * @param  None
00207   * @retval Return JOY_RIGHT, JOY_LEFT, JOY_SEL, JOY_UP, JOY_DOWN or JOY_NONE
00208   */
00209 JOYState_TypeDef ReadKey(void)
00210 {
00211 #ifndef USE_STM3210C_EVAL
00212   /* "right" key is pressed */
00213   if(!STM_EVAL_PBGetState(BUTTON_RIGHT))
00214   {
00215     while(STM_EVAL_PBGetState(BUTTON_RIGHT) == Bit_RESET);
00216     return JOY_RIGHT; 
00217   }
00218   /* "left" key is pressed */
00219   if(!STM_EVAL_PBGetState(BUTTON_LEFT))
00220   {
00221     while(STM_EVAL_PBGetState(BUTTON_LEFT) == Bit_RESET);
00222     return JOY_LEFT; 
00223   }
00224   /* "up" key is pressed */
00225   if(!STM_EVAL_PBGetState(BUTTON_UP))
00226   {
00227     while(STM_EVAL_PBGetState(BUTTON_UP) == Bit_RESET);
00228     return JOY_UP; 
00229   }
00230   /* "down" key is pressed */
00231   if(!STM_EVAL_PBGetState(BUTTON_DOWN))
00232   {
00233     while(STM_EVAL_PBGetState(BUTTON_DOWN) == Bit_RESET);
00234     return JOY_DOWN; 
00235   }
00236   /* "sel" key is pressed */
00237   if(!STM_EVAL_PBGetState(BUTTON_SEL))
00238   {
00239     while(STM_EVAL_PBGetState(BUTTON_SEL) == Bit_RESET);
00240     return JOY_SEL; 
00241   }
00242   /* No key is pressed */
00243   else 
00244   {
00245     return JOY_NONE;
00246   }
00247 #else
00248   return IOE_JoyStickGetState();
00249 #endif  
00250 }
00251 
00252 #ifdef  USE_FULL_ASSERT
00253 
00254 /**
00255   * @brief  Reports the name of the source file and the source line number
00256   *         where the assert_param error has occurred.
00257   * @param  file: pointer to the source file name
00258   * @param  line: assert_param error line source number
00259   * @retval None
00260   */
00261 void assert_failed(uint8_t* file, uint32_t line)
00262 { 
00263   /* User can add his own implementation to report the file name and line number,
00264      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00265 
00266   /* Infinite loop */
00267   while (1)
00268   {
00269   }
00270 }
00271 #endif
00272 
00273 /**
00274   * @}
00275   */ 
00276 
00277 /**
00278   * @}
00279   */ 
00280 
00281 /******************* (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