STM32F0xx Standard Peripherals Firmware Library
|
STM32F0xx_StdPeriph_Examples/USART/USART_TwoBoards/DataExchangeInterrupt/main.c
Go to the documentation of this file.
00001 /** 00002 ****************************************************************************** 00003 * @file USART/USART_TwoBoards/DataExchangeInterrupt/main.c 00004 * @author MCD Application Team 00005 * @version V1.4.0 00006 * @date 24-July-2014 00007 * @brief Main program body 00008 ****************************************************************************** 00009 * @attention 00010 * 00011 * <h2><center>© COPYRIGHT 2014 STMicroelectronics</center></h2> 00012 * 00013 * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 00014 * You may not use this file except in compliance with the License. 00015 * You may obtain a copy of the License at: 00016 * 00017 * http://www.st.com/software_license_agreement_liberty_v2 00018 * 00019 * Unless required by applicable law or agreed to in writing, software 00020 * distributed under the License is distributed on an "AS IS" BASIS, 00021 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00022 * See the License for the specific language governing permissions and 00023 * limitations under the License. 00024 * 00025 ****************************************************************************** 00026 */ 00027 00028 /* Includes ------------------------------------------------------------------*/ 00029 #include "main.h" 00030 00031 /** @addtogroup STM32F0xx_StdPeriph_Examples 00032 * @{ 00033 */ 00034 00035 /** @addtogroup USART_DataExchangeInterrupt 00036 * @{ 00037 */ 00038 00039 /* Private typedef -----------------------------------------------------------*/ 00040 /* Private define ------------------------------------------------------------*/ 00041 /* Private macro -------------------------------------------------------------*/ 00042 /* Private variables ---------------------------------------------------------*/ 00043 uint8_t TxBuffer[] = "USART Interrupt Example: Communication between two USART using Interrupt"; 00044 uint8_t RxBuffer [RXBUFFERSIZE]; 00045 __IO uint8_t RxIndex = 0x00; 00046 __IO uint8_t TxIndex = 0x00; 00047 00048 __IO JOYState_TypeDef PressedButton = JOY_NONE; 00049 __IO uint8_t UsartMode = USART_MODE_TRANSMITTER; 00050 __IO uint8_t UsartTransactionType = USART_TRANSACTIONTYPE_CMD; 00051 00052 uint8_t CmdBuffer [0x02] = {0x00, 0x00}; /* {Transaction Command, 00053 Number of byte to receive or to transmit} */ 00054 uint8_t AckBuffer [0x02] = {0x00, 0x00}; /* {Transaction Command, ACK command} */ 00055 00056 __IO uint32_t TimeOut = 0x00; 00057 /* Private function prototypes -----------------------------------------------*/ 00058 static void USART_Config(void); 00059 static void SysTickConfig(void); 00060 static void TimeOut_UserCallback(void); 00061 static JOYState_TypeDef Read_Joystick(void); 00062 static TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength); 00063 static void Fill_Buffer(uint8_t *pBuffer, uint16_t BufferLength); 00064 /* Private functions ---------------------------------------------------------*/ 00065 00066 /** 00067 * @brief Main program. 00068 * @param None 00069 * @retval None 00070 */ 00071 int main(void) 00072 { 00073 /*!< At this stage the microcontroller clock setting is already configured, 00074 this is done through SystemInit() function which is called from startup 00075 file (startup_stm32f0xx.s) before to branch to application main. 00076 To reconfigure the default setting of SystemInit() function, refer to 00077 system_stm32f0xx.c file 00078 */ 00079 /* USART configuration -----------------------------------------------------*/ 00080 USART_Config(); 00081 00082 /* SysTick configuration ---------------------------------------------------*/ 00083 SysTickConfig(); 00084 00085 /* Initialize LEDs mounted on EVAL board */ 00086 STM_EVAL_LEDInit(LED1); 00087 STM_EVAL_LEDInit(LED2); 00088 STM_EVAL_LEDInit(LED3); 00089 STM_EVAL_LEDInit(LED4); 00090 00091 /* Initialize push-buttons mounted on EVAL board */ 00092 STM_EVAL_PBInit(BUTTON_RIGHT, BUTTON_MODE_GPIO); 00093 STM_EVAL_PBInit(BUTTON_LEFT, BUTTON_MODE_GPIO); 00094 STM_EVAL_PBInit(BUTTON_UP, BUTTON_MODE_GPIO); 00095 STM_EVAL_PBInit(BUTTON_DOWN, BUTTON_MODE_GPIO); 00096 STM_EVAL_PBInit(BUTTON_SEL, BUTTON_MODE_GPIO); 00097 00098 /* Enable the USARTx Receive interrupt: this interrupt is generated when the 00099 USARTx receive data register is not empty */ 00100 USART_ITConfig(USARTx, USART_IT_RXNE, ENABLE); 00101 00102 while (1) 00103 { 00104 TxIndex = 0x00; 00105 RxIndex = 0x00; 00106 UsartTransactionType = USART_TRANSACTIONTYPE_CMD; 00107 UsartMode = USART_MODE_RECEIVER; 00108 00109 Fill_Buffer(CmdBuffer, 0x02); 00110 Fill_Buffer(AckBuffer, 0x02); 00111 00112 /* Clear the RxBuffer */ 00113 Fill_Buffer(RxBuffer, TXBUFFERSIZE); 00114 00115 PressedButton = Read_Joystick(); 00116 00117 /* Waiting Joystick pressed in case to transmit data or received Transaction command */ 00118 while ((PressedButton == JOY_NONE) && (CmdBuffer[0x00] == 0x00)) 00119 { 00120 PressedButton = Read_Joystick(); 00121 } 00122 00123 /* USART in Mode Transmitter ---------------------------------------------*/ 00124 if ((PressedButton != JOY_NONE) && (CmdBuffer[0x00] == 0x00)) 00125 { 00126 UsartMode = USART_MODE_TRANSMITTER; 00127 switch (PressedButton) 00128 { 00129 /* JOY_RIGHT button pressed */ 00130 case JOY_RIGHT: 00131 CmdBuffer[0x00] = CMD_RIGHT; 00132 CmdBuffer[0x01] = CMD_RIGHT_SIZE; 00133 break; 00134 /* JOY_LEFT button pressed */ 00135 case JOY_LEFT: 00136 CmdBuffer[0x00] = CMD_LEFT; 00137 CmdBuffer[0x01] = CMD_LEFT_SIZE; 00138 break; 00139 /* JOY_UP button pressed */ 00140 case JOY_UP: 00141 CmdBuffer[0x00] = CMD_UP; 00142 CmdBuffer[0x01] = CMD_UP_SIZE; 00143 break; 00144 /* JOY_DOWN button pressed */ 00145 case JOY_DOWN: 00146 CmdBuffer[0x00] = CMD_DOWN; 00147 CmdBuffer[0x01] = CMD_DOWN_SIZE; 00148 break; 00149 /* JOY_SEL button pressed */ 00150 case JOY_SEL: 00151 CmdBuffer[0x00] = CMD_SEL; 00152 CmdBuffer[0x01] = CMD_SEL_SIZE; 00153 break; 00154 default: 00155 break; 00156 } 00157 00158 if (CmdBuffer[0x00]!= 0x00) 00159 { 00160 /* Enable the USARTx transmit data register empty interrupt */ 00161 USART_ITConfig(USARTx, USART_IT_TXE, ENABLE); 00162 00163 /* Wait until USART sends the command or time out */ 00164 TimeOut = USER_TIMEOUT; 00165 while ((TxIndex < 0x02)&&(TimeOut != 0x00)) 00166 {} 00167 if(TimeOut == 0) 00168 { 00169 TimeOut_UserCallback(); 00170 } 00171 00172 /* The software must wait until TC=1. The TC flag remains cleared during all data 00173 transfers and it is set by hardware at the last frame�s end of transmission*/ 00174 TimeOut = USER_TIMEOUT; 00175 while ((USART_GetFlagStatus(USARTx, USART_FLAG_TC) == RESET)&&(TimeOut != 0x00)) 00176 { 00177 } 00178 if(TimeOut == 0) 00179 { 00180 TimeOut_UserCallback(); 00181 } 00182 00183 /* Wait until USART receives the Ack command or time out*/ 00184 TimeOut = USER_TIMEOUT; 00185 while ((RxIndex < 0x02)&&(TimeOut != 0x00)) 00186 {} 00187 if(TimeOut == 0) 00188 { 00189 TimeOut_UserCallback(); 00190 } 00191 /* USART sends the data */ 00192 UsartTransactionType = USART_TRANSACTIONTYPE_DATA; 00193 TxIndex = 0x00; 00194 RxIndex = 0x00; 00195 00196 /* Enable the USARTx transmit data register empty interrupt */ 00197 USART_ITConfig(USARTx, USART_IT_TXE, ENABLE); 00198 00199 /* Wait until end of data transfer */ 00200 TimeOut = USER_TIMEOUT; 00201 while ((TxIndex < GetVar_NbrOfData())&&(TimeOut != 0x00)) 00202 {} 00203 if(TimeOut == 0) 00204 { 00205 TimeOut_UserCallback(); 00206 } 00207 00208 /* The software must wait until TC=1. The TC flag remains cleared during all data 00209 transfers and it is set by hardware at the last frame�s end of transmission*/ 00210 TimeOut = USER_TIMEOUT; 00211 while ((USART_GetFlagStatus(USARTx, USART_FLAG_TC) == RESET)&&(TimeOut != 0x00)) 00212 { 00213 } 00214 if(TimeOut == 0) 00215 { 00216 TimeOut_UserCallback(); 00217 } 00218 } 00219 CmdBuffer[0x00] = 0x00; 00220 } 00221 00222 /* USART in Mode Receiver-------------------------------------------------*/ 00223 if (CmdBuffer[0x00] != 0x00) 00224 { 00225 /* Wait until USART receives the command or time out */ 00226 TimeOut = USER_TIMEOUT; 00227 while ((RxIndex < 0x02)&&(TimeOut != 0x00)) 00228 {} 00229 if(TimeOut == 0) 00230 { 00231 TimeOut_UserCallback(); 00232 } 00233 UsartMode = USART_MODE_RECEIVER; 00234 00235 /* Enable the USARTx transmit data register empty interrupt */ 00236 USART_ITConfig(USARTx, USART_IT_TXE, ENABLE); 00237 00238 /* Wait until USART sends the ACK command or time out*/ 00239 TimeOut = USER_TIMEOUT; 00240 while ((TxIndex < 0x02)&&(TimeOut != 0x00)) 00241 {} 00242 if(TimeOut == 0) 00243 { 00244 TimeOut_UserCallback(); 00245 } 00246 00247 /* The software must wait until TC=1. The TC flag remains cleared during all data 00248 transfers and it is set by hardware at the last frame�s end of transmission*/ 00249 TimeOut = USER_TIMEOUT; 00250 while ((USART_GetFlagStatus(USARTx, USART_FLAG_TC) == RESET)&&(TimeOut != 0x00)) 00251 { 00252 } 00253 if(TimeOut == 0) 00254 { 00255 TimeOut_UserCallback(); 00256 } 00257 00258 /* USART receives the data */ 00259 UsartTransactionType = USART_TRANSACTIONTYPE_DATA; 00260 TxIndex = 0x00; 00261 RxIndex = 0x00; 00262 00263 /* Wait until end of data transfer or time out */ 00264 TimeOut = USER_TIMEOUT; 00265 while ((RxIndex < GetVar_NbrOfData())&&(TimeOut != 0x00)) 00266 {} 00267 if(TimeOut == 0) 00268 { 00269 TimeOut_UserCallback(); 00270 } 00271 switch (CmdBuffer[0x01]) 00272 { 00273 /* CMD_RIGHT command received */ 00274 case CMD_RIGHT_SIZE: 00275 if (Buffercmp(TxBuffer, RxBuffer, CMD_RIGHT_SIZE) != FAILED) 00276 { 00277 /* Turn ON LED2 and LED3 */ 00278 STM_EVAL_LEDOn(LED2); 00279 STM_EVAL_LEDOn(LED3); 00280 /* Turn OFF LED4 */ 00281 STM_EVAL_LEDOff(LED4); 00282 } 00283 break; 00284 /* CMD_LEFT command received */ 00285 case CMD_LEFT_SIZE: 00286 if (Buffercmp(TxBuffer, RxBuffer, CMD_LEFT_SIZE) != FAILED) 00287 { 00288 /* Turn ON LED4 */ 00289 STM_EVAL_LEDOn(LED4); 00290 /* Turn OFF LED2 and LED3 */ 00291 STM_EVAL_LEDOff(LED2); 00292 STM_EVAL_LEDOff(LED3); 00293 } 00294 break; 00295 /* CMD_UP command received */ 00296 case CMD_UP_SIZE: 00297 if (Buffercmp(TxBuffer, RxBuffer, CMD_UP_SIZE) != FAILED) 00298 { 00299 /* Turn ON LED2 */ 00300 STM_EVAL_LEDOn(LED2); 00301 /* Turn OFF LED3 and LED4 */ 00302 STM_EVAL_LEDOff(LED3); 00303 STM_EVAL_LEDOff(LED4); 00304 } 00305 break; 00306 /* CMD_DOWN command received */ 00307 case CMD_DOWN_SIZE: 00308 if (Buffercmp(TxBuffer, RxBuffer, CMD_DOWN_SIZE) != FAILED) 00309 { 00310 /* Turn ON LED3 */ 00311 STM_EVAL_LEDOn(LED3); 00312 /* Turn OFF LED2 and LED4 */ 00313 STM_EVAL_LEDOff(LED2); 00314 STM_EVAL_LEDOff(LED4); 00315 } 00316 break; 00317 /* CMD_SEL command received */ 00318 case CMD_SEL_SIZE: 00319 if (Buffercmp(TxBuffer, RxBuffer, CMD_SEL_SIZE) != FAILED) 00320 { 00321 /* Turn ON all LED2, LED3 and LED4 */ 00322 STM_EVAL_LEDOn(LED2); 00323 STM_EVAL_LEDOn(LED3); 00324 STM_EVAL_LEDOn(LED4); 00325 } 00326 break; 00327 default: 00328 break; 00329 } 00330 CmdBuffer[0x00] = 0x00; 00331 } 00332 } 00333 } 00334 00335 /** 00336 * @brief Basic management of the timeout situation. 00337 * @param None. 00338 * @retval None. 00339 */ 00340 static void TimeOut_UserCallback(void) 00341 { 00342 /* User can add his own implementation to manage TimeOut Communication failure */ 00343 /* Block communication and all processes */ 00344 while (1) 00345 { 00346 } 00347 } 00348 00349 /** 00350 * @brief Configures the USART Peripheral. 00351 * @param None 00352 * @retval None 00353 */ 00354 static void USART_Config(void) 00355 { 00356 USART_InitTypeDef USART_InitStructure; 00357 NVIC_InitTypeDef NVIC_InitStructure; 00358 GPIO_InitTypeDef GPIO_InitStructure; 00359 00360 /* Enable GPIO clock */ 00361 RCC_AHBPeriphClockCmd(USARTx_TX_GPIO_CLK | USARTx_RX_GPIO_CLK, ENABLE); 00362 00363 /* Enable USART clock */ 00364 USARTx_APBPERIPHCLOCK(USARTx_CLK, ENABLE); 00365 00366 /* Connect PXx to USARTx_Tx */ 00367 GPIO_PinAFConfig(USARTx_TX_GPIO_PORT, USARTx_TX_SOURCE, USARTx_TX_AF); 00368 00369 /* Connect PXx to USARTx_Rx */ 00370 GPIO_PinAFConfig(USARTx_RX_GPIO_PORT, USARTx_RX_SOURCE, USARTx_RX_AF); 00371 00372 /* Configure USART Tx and Rx as alternate function push-pull */ 00373 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; 00374 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_3; 00375 GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; 00376 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; 00377 GPIO_InitStructure.GPIO_Pin = USARTx_TX_PIN; 00378 GPIO_Init(USARTx_TX_GPIO_PORT, &GPIO_InitStructure); 00379 00380 GPIO_InitStructure.GPIO_Pin = USARTx_RX_PIN; 00381 GPIO_Init(USARTx_RX_GPIO_PORT, &GPIO_InitStructure); 00382 00383 /* USARTx configuration ----------------------------------------------------*/ 00384 /* USARTx configured as follow: 00385 - BaudRate = 230400 baud 00386 - Word Length = 8 Bits 00387 - One Stop Bit 00388 - No parity 00389 - Hardware flow control disabled (RTS and CTS signals) 00390 - Receive and transmit enabled 00391 */ 00392 USART_InitStructure.USART_BaudRate = 230400; 00393 USART_InitStructure.USART_WordLength = USART_WordLength_8b; 00394 USART_InitStructure.USART_StopBits = USART_StopBits_1; 00395 USART_InitStructure.USART_Parity = USART_Parity_No; 00396 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; 00397 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; 00398 USART_Init(USARTx, &USART_InitStructure); 00399 00400 /* NVIC configuration */ 00401 /* Enable the USARTx Interrupt */ 00402 NVIC_InitStructure.NVIC_IRQChannel = USARTx_IRQn; 00403 NVIC_InitStructure.NVIC_IRQChannelPriority = 0; 00404 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 00405 NVIC_Init(&NVIC_InitStructure); 00406 00407 /* Enable USART */ 00408 USART_Cmd(USARTx, ENABLE); 00409 } 00410 00411 /** 00412 * @brief Configure a SysTick Base time to 10 ms. 00413 * @param None 00414 * @retval None 00415 */ 00416 static void SysTickConfig(void) 00417 { 00418 /* Setup SysTick Timer for 10ms interrupts */ 00419 if (SysTick_Config(SystemCoreClock / 100)) 00420 { 00421 /* Capture error */ 00422 while (1); 00423 } 00424 /* Configure the SysTick handler priority */ 00425 NVIC_SetPriority(SysTick_IRQn, 0x0); 00426 } 00427 00428 /** 00429 * @brief Reads key from evaluation board. 00430 * @param None 00431 * @retval Return JOY_RIGHT, JOY_LEFT, JOY_SEL, JOY_UP, JOY_DOWN or JOY_NONE 00432 */ 00433 static JOYState_TypeDef Read_Joystick(void) 00434 { 00435 /* "JOY_RIGHT" key is pressed */ 00436 if (STM_EVAL_PBGetState(BUTTON_RIGHT)) 00437 { 00438 while (STM_EVAL_PBGetState(BUTTON_RIGHT) == RESET) 00439 {} 00440 return JOY_RIGHT; 00441 } 00442 /* "JOY_LEFT" key is pressed */ 00443 if (STM_EVAL_PBGetState(BUTTON_LEFT)) 00444 { 00445 while (STM_EVAL_PBGetState(BUTTON_LEFT) == RESET) 00446 {} 00447 return JOY_LEFT; 00448 } 00449 /* "JOY_UP" key is pressed */ 00450 if (STM_EVAL_PBGetState(BUTTON_UP)) 00451 { 00452 while (STM_EVAL_PBGetState(BUTTON_UP) == RESET) 00453 {} 00454 return JOY_UP; 00455 } 00456 /* "JOY_DOWN" key is pressed */ 00457 if (STM_EVAL_PBGetState(BUTTON_DOWN)) 00458 { 00459 while (STM_EVAL_PBGetState(BUTTON_DOWN) == RESET) 00460 {} 00461 return JOY_DOWN; 00462 } 00463 /* "JOY_SEL" key is pressed */ 00464 if (STM_EVAL_PBGetState(BUTTON_SEL)) 00465 { 00466 while (STM_EVAL_PBGetState(BUTTON_SEL) == RESET) 00467 {} 00468 return JOY_SEL; 00469 } 00470 /* No key is pressed */ 00471 else 00472 { 00473 return JOY_NONE; 00474 } 00475 } 00476 00477 /** 00478 * @brief Compares two buffers. 00479 * @param pBuffer1, pBuffer2: buffers to be compared. 00480 * @param BufferLength: buffer's length 00481 * @retval PASSED: pBuffer1 identical to pBuffer2 00482 * FAILED: pBuffer1 differs from pBuffer2 00483 */ 00484 static TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength) 00485 { 00486 while (BufferLength--) 00487 { 00488 if (*pBuffer1 != *pBuffer2) 00489 { 00490 return FAILED; 00491 } 00492 pBuffer1++; 00493 pBuffer2++; 00494 } 00495 00496 return PASSED; 00497 } 00498 /** 00499 * @brief Returns NbrOfData value. 00500 * @param None 00501 * @retval Tx Buffer Size (NbrOfDataToTransfer). 00502 */ 00503 uint8_t GetVar_NbrOfData(void) 00504 { 00505 return CmdBuffer[0x01]; 00506 } 00507 /** 00508 * @brief Fills buffer. 00509 * @param pBuffer: pointer on the Buffer to fill 00510 * @param BufferLength: size of the buffer to fill 00511 * @retval None 00512 */ 00513 static void Fill_Buffer(uint8_t *pBuffer, uint16_t BufferLength) 00514 { 00515 uint16_t index = 0; 00516 00517 /* Put in global buffer same values */ 00518 for (index = 0; index < BufferLength; index++ ) 00519 { 00520 pBuffer[index] = 0x00; 00521 } 00522 } 00523 00524 #ifdef USE_FULL_ASSERT 00525 00526 /** 00527 * @brief Reports the name of the source file and the source line number 00528 * where the assert_param error has occurred. 00529 * @param file: pointer to the source file name 00530 * @param line: assert_param error line source number 00531 * @retval None 00532 */ 00533 void assert_failed(uint8_t* file, uint32_t line) 00534 { 00535 /* User can add his own implementation to report the file name and line number, 00536 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ 00537 00538 /* Infinite loop */ 00539 while (1) 00540 { 00541 } 00542 } 00543 #endif 00544 00545 /** 00546 * @} 00547 */ 00548 00549 /** 00550 * @} 00551 */ 00552 00553 00554 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/