STM32F0xx Standard Peripherals Firmware Library
|
STM32F0xx_StdPeriph_Examples/USART/USART_TwoBoards/DataExchangeDMA/main.c
Go to the documentation of this file.
00001 /** 00002 ****************************************************************************** 00003 * @file USART/USART_TwoBoards/DataExchangeDMA/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_DataExchangeDMA 00036 * @{ 00037 */ 00038 00039 /* Private typedef -----------------------------------------------------------*/ 00040 DMA_InitTypeDef DMA_InitStructure; 00041 00042 /* Private define ------------------------------------------------------------*/ 00043 /* Private macro -------------------------------------------------------------*/ 00044 /* Private variables ---------------------------------------------------------*/ 00045 uint8_t TxBuffer[] = "USART DMA Example: Communication between two USART using DMA"; 00046 uint8_t RxBuffer [RXBUFFERSIZE]; 00047 00048 uint8_t CmdBuffer [0x02] = {0x00, 0x00}; /* {Command, Number of byte to receive or to transmit} */ 00049 uint8_t AckBuffer [0x02] = {0x00, 0x00}; /* {Command, ACK} */ 00050 00051 __IO uint32_t TimeOut = 0x0; 00052 __IO JOYState_TypeDef PressedButton = JOY_NONE; 00053 00054 /* Private function prototypes -----------------------------------------------*/ 00055 static void TimeOut_UserCallback(void); 00056 static void USART_Config(void); 00057 static void SysTickConfig(void); 00058 static JOYState_TypeDef Read_Joystick(void); 00059 static TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength); 00060 static void Fill_Buffer(uint8_t *pBuffer, uint16_t BufferLength); 00061 00062 /* Private functions ---------------------------------------------------------*/ 00063 00064 /** 00065 * @brief Main program. 00066 * @param None 00067 * @retval None 00068 */ 00069 int main(void) 00070 { 00071 /*!< At this stage the microcontroller clock setting is already configured, 00072 this is done through SystemInit() function which is called from startup 00073 file (startup_stm32f0xx.s) before to branch to application main. 00074 To reconfigure the default setting of SystemInit() function, refer to 00075 system_stm32f0xx.c file 00076 */ 00077 /* USART configuration -----------------------------------------------------*/ 00078 USART_Config(); 00079 00080 /* SysTick configuration ---------------------------------------------------*/ 00081 SysTickConfig(); 00082 00083 /* Initialize LEDs mounted on EVAL board */ 00084 STM_EVAL_LEDInit(LED1); 00085 STM_EVAL_LEDInit(LED2); 00086 STM_EVAL_LEDInit(LED3); 00087 STM_EVAL_LEDInit(LED4); 00088 00089 /* Initialize push-buttons mounted on EVAL board */ 00090 STM_EVAL_PBInit(BUTTON_RIGHT, BUTTON_MODE_GPIO); 00091 STM_EVAL_PBInit(BUTTON_LEFT, BUTTON_MODE_GPIO); 00092 STM_EVAL_PBInit(BUTTON_UP, BUTTON_MODE_GPIO); 00093 STM_EVAL_PBInit(BUTTON_DOWN, BUTTON_MODE_GPIO); 00094 STM_EVAL_PBInit(BUTTON_SEL, BUTTON_MODE_GPIO); 00095 00096 while (1) 00097 { 00098 /* Clear Buffers */ 00099 Fill_Buffer(CmdBuffer, 0x02); 00100 Fill_Buffer(AckBuffer, 0x02); 00101 Fill_Buffer(RxBuffer, TXBUFFERSIZE); 00102 00103 /* Waiting transaction code in case of USART receiver */ 00104 /* DMA channel Rx of USART Configuration */ 00105 DMA_DeInit(USARTx_RX_DMA_CHANNEL); 00106 DMA_InitStructure.DMA_PeripheralBaseAddr = USARTx_RDR_ADDRESS; 00107 DMA_InitStructure.DMA_BufferSize = (uint16_t)2; 00108 DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)CmdBuffer; 00109 DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; 00110 DMA_InitStructure.DMA_Priority = DMA_Priority_Low; 00111 DMA_Init(USARTx_RX_DMA_CHANNEL, &DMA_InitStructure); 00112 00113 /* Enable the USART Rx DMA request */ 00114 USART_DMACmd(USARTx, USART_DMAReq_Rx, ENABLE); 00115 00116 /* Enable the DMA channel */ 00117 DMA_Cmd(USARTx_RX_DMA_CHANNEL, ENABLE); 00118 00119 PressedButton = Read_Joystick(); 00120 00121 /* Waiting Joystick pressed in case to transmit data or received Transaction command */ 00122 while((PressedButton == JOY_NONE) && (CmdBuffer[0x00] == 0x00)) 00123 { 00124 PressedButton = Read_Joystick(); 00125 } 00126 00127 /* USART in Mode Transmitter ---------------------------------------------*/ 00128 if ((PressedButton != JOY_NONE) && (CmdBuffer[0x00] == 0x00)) 00129 { 00130 /* Configure the USART to receive the ACK command Table */ 00131 /* DMA channel Rx of USART Configuration */ 00132 DMA_DeInit(USARTx_RX_DMA_CHANNEL); 00133 DMA_InitStructure.DMA_PeripheralBaseAddr = USARTx_RDR_ADDRESS; 00134 DMA_InitStructure.DMA_BufferSize = (uint16_t)2; 00135 DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)AckBuffer; 00136 DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; 00137 DMA_InitStructure.DMA_Priority = DMA_Priority_Low; 00138 DMA_Init(USARTx_RX_DMA_CHANNEL, &DMA_InitStructure); 00139 00140 /* Configure the USART to send the command table */ 00141 /* DMA channel Tx of USART Configuration */ 00142 DMA_DeInit(USARTx_TX_DMA_CHANNEL); 00143 DMA_InitStructure.DMA_PeripheralBaseAddr = USARTx_TDR_ADDRESS; 00144 DMA_InitStructure.DMA_BufferSize = (uint16_t)2; 00145 DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)CmdBuffer; 00146 DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; 00147 DMA_InitStructure.DMA_Priority = DMA_Priority_High; 00148 DMA_Init(USARTx_TX_DMA_CHANNEL, &DMA_InitStructure); 00149 00150 switch (PressedButton) 00151 { 00152 /* JOY_RIGHT button pressed */ 00153 case JOY_RIGHT: 00154 CmdBuffer[0x00] = CMD_RIGHT; 00155 CmdBuffer[0x01] = CMD_RIGHT_SIZE; 00156 break; 00157 /* JOY_LEFT button pressed */ 00158 case JOY_LEFT: 00159 CmdBuffer[0x00] = CMD_LEFT; 00160 CmdBuffer[0x01] = CMD_LEFT_SIZE; 00161 break; 00162 /* JOY_UP button pressed */ 00163 case JOY_UP: 00164 CmdBuffer[0x00] = CMD_UP; 00165 CmdBuffer[0x01] = CMD_UP_SIZE; 00166 break; 00167 /* JOY_DOWN button pressed */ 00168 case JOY_DOWN: 00169 CmdBuffer[0x00] = CMD_DOWN; 00170 CmdBuffer[0x01] = CMD_DOWN_SIZE; 00171 break; 00172 /* JOY_SEL button pressed */ 00173 case JOY_SEL: 00174 CmdBuffer[0x00] = CMD_SEL; 00175 CmdBuffer[0x01] = CMD_SEL_SIZE; 00176 break; 00177 default: 00178 break; 00179 } 00180 00181 /* Enable the USART DMA requests */ 00182 USART_DMACmd(USARTx, USART_DMAReq_Rx, ENABLE); 00183 USART_DMACmd(USARTx, USART_DMAReq_Tx, ENABLE); 00184 00185 /* Clear the TC bit in the SR register by writing 0 to it */ 00186 USART_ClearFlag(USARTx, USART_FLAG_TC); 00187 00188 /* Enable the DMA USART Tx Channel */ 00189 DMA_Cmd(USARTx_TX_DMA_CHANNEL, ENABLE); 00190 /* Enable the DMA USART Rx channel */ 00191 DMA_Cmd(USARTx_RX_DMA_CHANNEL, ENABLE); 00192 00193 /* Wait the USART DMA Tx transfer complete or time out */ 00194 TimeOut = USER_TIMEOUT; 00195 00196 while ((DMA_GetFlagStatus(USARTx_TX_DMA_FLAG_TC) == RESET)&&(TimeOut != 0x00)) 00197 { 00198 } 00199 00200 if(TimeOut == 0) 00201 { 00202 TimeOut_UserCallback(); 00203 } 00204 /* The software must wait until TC=1. The TC flag remains cleared during all data 00205 transfers and it is set by hardware at the last frame�s end of transmission*/ 00206 TimeOut = USER_TIMEOUT; 00207 while ((USART_GetFlagStatus(USARTx, USART_FLAG_TC) == RESET)&&(TimeOut != 0x00)) 00208 { 00209 } 00210 if(TimeOut == 0) 00211 { 00212 TimeOut_UserCallback(); 00213 } 00214 00215 /* Wait the USART DMA Rx transfer complete or time out */ 00216 TimeOut = USER_TIMEOUT; 00217 00218 while ((DMA_GetFlagStatus(USARTx_RX_DMA_FLAG_TC) == RESET)&&(TimeOut != 0x00)) 00219 {} 00220 00221 if(TimeOut == 0) 00222 { 00223 TimeOut_UserCallback(); 00224 } 00225 00226 /* Clear DMA1 global flags */ 00227 DMA_ClearFlag(USARTx_TX_DMA_FLAG_GL); 00228 DMA_ClearFlag(USARTx_RX_DMA_FLAG_GL); 00229 00230 /* Disable the DMA channels */ 00231 DMA_Cmd(USARTx_TX_DMA_CHANNEL, DISABLE); 00232 DMA_Cmd(USARTx_RX_DMA_CHANNEL, DISABLE); 00233 00234 /* Disable the USART Tx DMA request */ 00235 USART_DMACmd(USARTx, USART_DMAReq_Tx, DISABLE); 00236 /* Disable the USART Rx DMA requests */ 00237 USART_DMACmd(USARTx, USART_DMAReq_Rx, DISABLE); 00238 00239 /* DMA channel Tx of USART Configuration */ 00240 DMA_DeInit(USARTx_TX_DMA_CHANNEL); 00241 DMA_InitStructure.DMA_PeripheralBaseAddr = USARTx_TDR_ADDRESS; 00242 DMA_InitStructure.DMA_BufferSize = (uint16_t)CmdBuffer[0x01]; 00243 DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)TxBuffer; 00244 DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; 00245 DMA_InitStructure.DMA_Priority = DMA_Priority_High; 00246 DMA_Init(USARTx_TX_DMA_CHANNEL, &DMA_InitStructure); 00247 00248 USART_DMACmd(USARTx, USART_DMAReq_Tx, ENABLE); 00249 00250 /* Clear the TC bit in the SR register by writing 0 to it */ 00251 USART_ClearFlag(USARTx, USART_FLAG_TC); 00252 00253 /* Enable DMA1 USART Tx Channel */ 00254 DMA_Cmd(USARTx_TX_DMA_CHANNEL, ENABLE); 00255 00256 /* Wait the USART DMA Tx transfer complete or time out */ 00257 TimeOut = USER_TIMEOUT; 00258 while ((DMA_GetFlagStatus(USARTx_TX_DMA_FLAG_TC) == RESET)&&(TimeOut != 0x00)) 00259 { 00260 } 00261 if(TimeOut == 0) 00262 { 00263 TimeOut_UserCallback(); 00264 } 00265 00266 /* The software must wait until TC=1. The TC flag remains cleared during all data 00267 transfers and it is set by hardware at the last frame�s end of transmission*/ 00268 TimeOut = USER_TIMEOUT; 00269 while ((USART_GetFlagStatus(USARTx, USART_FLAG_TC) == RESET)&&(TimeOut != 0x00)) 00270 { 00271 } 00272 if(TimeOut == 0) 00273 { 00274 TimeOut_UserCallback(); 00275 } 00276 00277 /* Clear DMA global flags */ 00278 DMA_ClearFlag(USARTx_TX_DMA_FLAG_GL); 00279 DMA_ClearFlag(USARTx_RX_DMA_FLAG_GL); 00280 00281 DMA_Cmd(USARTx_TX_DMA_CHANNEL, DISABLE); 00282 00283 /* Disable the USART Tx DMA requests */ 00284 USART_DMACmd(USARTx, USART_DMAReq_Tx, DISABLE); 00285 00286 CmdBuffer[0x00] = 0x00; 00287 } 00288 00289 /* USART in Mode Receiver-------------------------------------------------*/ 00290 /* USART Receive Transaction command and the number of Bytes to receive */ 00291 if (CmdBuffer[0x00] != 0x00) 00292 { 00293 /* Wait the USART DMA Rx transfer complete or time out */ 00294 TimeOut = USER_TIMEOUT; 00295 while ((DMA_GetFlagStatus(USARTx_RX_DMA_FLAG_TC) == RESET)&&(TimeOut != 0x00)) 00296 {} 00297 if(TimeOut == 0) 00298 { 00299 TimeOut_UserCallback(); 00300 } 00301 00302 /* Clear DMA1 global flags */ 00303 DMA_ClearFlag(USARTx_TX_DMA_FLAG_GL); 00304 DMA_ClearFlag(USARTx_RX_DMA_FLAG_GL); 00305 00306 /* Disable the DMA channels */ 00307 DMA_Cmd(USARTx_RX_DMA_CHANNEL, DISABLE); 00308 00309 /* Disable the USART Rx DMA requests */ 00310 USART_DMACmd(USARTx, USART_DMAReq_Rx, DISABLE); 00311 00312 /* At this Steep the USART send the ACK command (after Receive the transaction 00313 command and the number of data to receive this parameter is mandatory 00314 to configure the DMA_BufferSize in the second reception phase */ 00315 00316 AckBuffer[0x00] = CmdBuffer[0x00]; 00317 AckBuffer[0x01] = CMD_ACK; 00318 00319 /* DMA channel Tx of USART Configuration */ 00320 DMA_DeInit(USARTx_TX_DMA_CHANNEL); 00321 DMA_InitStructure.DMA_PeripheralBaseAddr = USARTx_TDR_ADDRESS; 00322 DMA_InitStructure.DMA_BufferSize = (uint16_t)2; 00323 DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)AckBuffer; 00324 DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; 00325 DMA_InitStructure.DMA_Priority = DMA_Priority_High; 00326 DMA_Init(USARTx_TX_DMA_CHANNEL, &DMA_InitStructure); 00327 00328 USART_DMACmd(USARTx, USART_DMAReq_Tx, ENABLE); 00329 00330 /* Clear the TC bit in the SR register by writing 0 to it */ 00331 USART_ClearFlag(USARTx, USART_FLAG_TC); 00332 00333 /* Enable DMA1 USART Tx Channel */ 00334 DMA_Cmd(USARTx_TX_DMA_CHANNEL, ENABLE); 00335 00336 /* Wait the USART DMA Tx transfer complete or time out */ 00337 TimeOut = USER_TIMEOUT; 00338 00339 while ((DMA_GetFlagStatus(USARTx_TX_DMA_FLAG_TC) == RESET)&&(TimeOut != 0x00)) 00340 {} 00341 00342 if(TimeOut == 0) 00343 { 00344 TimeOut_UserCallback(); 00345 } 00346 00347 /* The software must wait until TC=1. The TC flag remains cleared during all data 00348 transfers and it is set by hardware at the last frame�s end of transmission*/ 00349 TimeOut = USER_TIMEOUT; 00350 while ((USART_GetFlagStatus(USARTx, USART_FLAG_TC) == RESET)&&(TimeOut != 0x00)) 00351 { 00352 } 00353 if(TimeOut == 0) 00354 { 00355 TimeOut_UserCallback(); 00356 } 00357 00358 /* Clear DMA1 global flags */ 00359 DMA_ClearFlag(USARTx_TX_DMA_FLAG_GL); 00360 DMA_ClearFlag(USARTx_RX_DMA_FLAG_GL); 00361 00362 DMA_Cmd(USARTx_TX_DMA_CHANNEL, DISABLE); 00363 00364 /* Disable the USART Tx DMA requests */ 00365 USART_DMACmd(USARTx, USART_DMAReq_Tx, DISABLE); 00366 00367 /* The transmitter After receive the ACK command it sends the defined data 00368 in his TxBuffer */ 00369 /* DMA channel Rx of USART Configuration */ 00370 DMA_DeInit(USARTx_RX_DMA_CHANNEL); 00371 DMA_InitStructure.DMA_PeripheralBaseAddr = USARTx_RDR_ADDRESS; 00372 DMA_InitStructure.DMA_BufferSize = (uint16_t)CmdBuffer[0x01]; 00373 DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t) RxBuffer; 00374 DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; 00375 DMA_InitStructure.DMA_Priority = DMA_Priority_Low; 00376 DMA_Init(USARTx_RX_DMA_CHANNEL, &DMA_InitStructure); 00377 00378 USART_DMACmd(USARTx, USART_DMAReq_Rx , ENABLE); 00379 00380 /* Enable the DMA channel */ 00381 DMA_Cmd(USARTx_RX_DMA_CHANNEL, ENABLE); 00382 00383 /* Wait the USART DMA Rx transfer complete or time out */ 00384 TimeOut = USER_TIMEOUT; 00385 00386 while ((DMA_GetFlagStatus(USARTx_RX_DMA_FLAG_TC) == RESET)&&(TimeOut != 0x00)) 00387 { 00388 } 00389 00390 if(TimeOut == 0) 00391 { 00392 TimeOut_UserCallback(); 00393 } 00394 00395 /* Clear DMA1 global flags */ 00396 DMA_ClearFlag(USARTx_TX_DMA_FLAG_GL); 00397 DMA_ClearFlag(USARTx_RX_DMA_FLAG_GL); 00398 00399 /* Disable the DMA channels */ 00400 DMA_Cmd(USARTx_RX_DMA_CHANNEL, DISABLE); 00401 00402 /* Disable the USART Rx DMA requests */ 00403 USART_DMACmd(USARTx, USART_DMAReq_Rx, DISABLE); 00404 00405 switch (CmdBuffer[0x01]) 00406 { 00407 /* CMD_RIGHT command received */ 00408 case CMD_RIGHT_SIZE: 00409 if (Buffercmp(TxBuffer, RxBuffer, CMD_RIGHT_SIZE) != FAILED) 00410 { 00411 /* Turn ON LED2 and LED3 */ 00412 STM_EVAL_LEDOn(LED2); 00413 STM_EVAL_LEDOn(LED3); 00414 /* Turn OFF LED4 */ 00415 STM_EVAL_LEDOff(LED4); 00416 } 00417 break; 00418 /* CMD_LEFT command received */ 00419 case CMD_LEFT_SIZE: 00420 if (Buffercmp(TxBuffer, RxBuffer, CMD_LEFT_SIZE) != FAILED) 00421 { 00422 /* Turn ON LED4 */ 00423 STM_EVAL_LEDOn(LED4); 00424 /* Turn OFF LED2 and LED3 */ 00425 STM_EVAL_LEDOff(LED2); 00426 STM_EVAL_LEDOff(LED3); 00427 } 00428 break; 00429 /* CMD_UP command received */ 00430 case CMD_UP_SIZE: 00431 if (Buffercmp(TxBuffer, RxBuffer, CMD_UP_SIZE) != FAILED) 00432 { 00433 /* Turn ON LED2 */ 00434 STM_EVAL_LEDOn(LED2); 00435 /* Turn OFF LED3 and LED4 */ 00436 STM_EVAL_LEDOff(LED3); 00437 STM_EVAL_LEDOff(LED4); 00438 } 00439 break; 00440 /* CMD_DOWN command received */ 00441 case CMD_DOWN_SIZE: 00442 if (Buffercmp(TxBuffer, RxBuffer, CMD_DOWN_SIZE) != FAILED) 00443 { 00444 /* Turn ON LED3 */ 00445 STM_EVAL_LEDOn(LED3); 00446 /* Turn OFF LED2 and LED4 */ 00447 STM_EVAL_LEDOff(LED2); 00448 STM_EVAL_LEDOff(LED4); 00449 } 00450 break; 00451 /* CMD_SEL command received */ 00452 case CMD_SEL_SIZE: 00453 if (Buffercmp(TxBuffer, RxBuffer, CMD_SEL_SIZE) != FAILED) 00454 { 00455 /* Turn ON all LED2, LED3 and LED4 */ 00456 STM_EVAL_LEDOn(LED2); 00457 STM_EVAL_LEDOn(LED3); 00458 STM_EVAL_LEDOn(LED4); 00459 } 00460 break; 00461 default: 00462 break; 00463 } 00464 CmdBuffer[0x00] = 0x00; 00465 } 00466 } 00467 } 00468 00469 /** 00470 * @brief Basic management of the timeout situation. 00471 * @param None. 00472 * @retval None. 00473 */ 00474 static void TimeOut_UserCallback(void) 00475 { 00476 /* User can add his own implementation to manage TimeOut Communication failure */ 00477 /* Block communication and all processes */ 00478 while (1) 00479 { 00480 } 00481 } 00482 00483 /** 00484 * @brief Configures the USART Peripheral. 00485 * @param None 00486 * @retval None 00487 */ 00488 static void USART_Config(void) 00489 { 00490 USART_InitTypeDef USART_InitStructure; 00491 GPIO_InitTypeDef GPIO_InitStructure; 00492 00493 /* Enable GPIO clock */ 00494 RCC_AHBPeriphClockCmd(USARTx_TX_GPIO_CLK | USARTx_RX_GPIO_CLK, ENABLE); 00495 00496 /* Enable USART clock */ 00497 USARTx_APBPERIPHCLOCK(USARTx_CLK, ENABLE); 00498 00499 /* Enable the DMA periph */ 00500 RCC_AHBPeriphClockCmd(DMAx_CLK, ENABLE); 00501 00502 /* Connect PXx to USARTx_Tx */ 00503 GPIO_PinAFConfig(USARTx_TX_GPIO_PORT, USARTx_TX_SOURCE, USARTx_TX_AF); 00504 00505 /* Connect PXx to USARTx_Rx */ 00506 GPIO_PinAFConfig(USARTx_RX_GPIO_PORT, USARTx_RX_SOURCE, USARTx_RX_AF); 00507 00508 /* Configure USART Tx and Rx as alternate function push-pull */ 00509 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; 00510 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_3; 00511 GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; 00512 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; 00513 00514 GPIO_InitStructure.GPIO_Pin = USARTx_TX_PIN; 00515 GPIO_Init(USARTx_TX_GPIO_PORT, &GPIO_InitStructure); 00516 00517 GPIO_InitStructure.GPIO_Pin = USARTx_RX_PIN; 00518 GPIO_Init(USARTx_RX_GPIO_PORT, &GPIO_InitStructure); 00519 00520 /* USARTx configuration ----------------------------------------------------*/ 00521 /* USARTx configured as follow: 00522 - BaudRate = 230400 baud 00523 - Word Length = 8 Bits 00524 - one Stop Bit 00525 - No parity 00526 - Hardware flow control disabled (RTS and CTS signals) 00527 - Receive and transmit enabled 00528 */ 00529 USART_InitStructure.USART_BaudRate = 230400; 00530 USART_InitStructure.USART_WordLength = USART_WordLength_8b; 00531 USART_InitStructure.USART_StopBits = USART_StopBits_1; 00532 /* When using Parity the word length must be configured to 9 bits */ 00533 USART_InitStructure.USART_Parity = USART_Parity_No; 00534 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; 00535 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; 00536 USART_Init(USARTx, &USART_InitStructure); 00537 00538 /* DMA Configuration -------------------------------------------------------*/ 00539 DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; 00540 DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; 00541 DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; 00542 DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; 00543 DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; 00544 DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; 00545 00546 /* Enable USART */ 00547 USART_Cmd(USARTx, ENABLE); 00548 } 00549 00550 /** 00551 * @brief Configure a SysTick Base time to 10 ms. 00552 * @param None 00553 * @retval None 00554 */ 00555 static void SysTickConfig(void) 00556 { 00557 /* Set SysTick Timer for 10ms interrupts */ 00558 if (SysTick_Config(SystemCoreClock / 100)) 00559 { 00560 /* Capture error */ 00561 while (1); 00562 } 00563 /* Configure the SysTick handler priority */ 00564 NVIC_SetPriority(SysTick_IRQn, 0x0); 00565 } 00566 00567 /** 00568 * @brief Reads key from evaluationboard. 00569 * @param None 00570 * @retval Return JOY_RIGHT, JOY_LEFT, JOY_SEL, JOY_UP, JOY_DOWN or JOY_NONE 00571 */ 00572 static JOYState_TypeDef Read_Joystick(void) 00573 { 00574 /* "JOY_RIGHT" key is pressed */ 00575 if (STM_EVAL_PBGetState(BUTTON_RIGHT)) 00576 { 00577 while (STM_EVAL_PBGetState(BUTTON_RIGHT) == RESET) 00578 {} 00579 return JOY_RIGHT; 00580 } 00581 /* "JOY_LEFT" key is pressed */ 00582 if (STM_EVAL_PBGetState(BUTTON_LEFT)) 00583 { 00584 while (STM_EVAL_PBGetState(BUTTON_LEFT) == RESET) 00585 {} 00586 return JOY_LEFT; 00587 } 00588 /* "JOY_UP" key is pressed */ 00589 if (STM_EVAL_PBGetState(BUTTON_UP)) 00590 { 00591 while (STM_EVAL_PBGetState(BUTTON_UP) == RESET) 00592 {} 00593 return JOY_UP; 00594 } 00595 /* "JOY_DOWN" key is pressed */ 00596 if (STM_EVAL_PBGetState(BUTTON_DOWN)) 00597 { 00598 while (STM_EVAL_PBGetState(BUTTON_DOWN) == RESET) 00599 {} 00600 return JOY_DOWN; 00601 } 00602 /* "JOY_SEL" key is pressed */ 00603 if (STM_EVAL_PBGetState(BUTTON_SEL)) 00604 { 00605 while (STM_EVAL_PBGetState(BUTTON_SEL) == RESET) 00606 {} 00607 return JOY_SEL; 00608 } 00609 /* No key is pressed */ 00610 else 00611 { 00612 return JOY_NONE; 00613 } 00614 } 00615 00616 /** 00617 * @brief Compares two buffers. 00618 * @param pBuffer1, pBuffer2: buffers to be compared. 00619 * @param BufferLength: buffer's length 00620 * @retval PASSED: pBuffer1 identical to pBuffer2 00621 * FAILED: pBuffer1 differs from pBuffer2 00622 */ 00623 static TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength) 00624 { 00625 while (BufferLength--) 00626 { 00627 if (*pBuffer1 != *pBuffer2) 00628 { 00629 return FAILED; 00630 } 00631 pBuffer1++; 00632 pBuffer2++; 00633 } 00634 00635 return PASSED; 00636 } 00637 00638 /** 00639 * @brief Fills buffer. 00640 * @param pBuffer: pointer on the Buffer to fill 00641 * @param BufferLength: size of the buffer to fill 00642 * @retval None 00643 */ 00644 static void Fill_Buffer(uint8_t *pBuffer, uint16_t BufferLength) 00645 { 00646 uint16_t index = 0; 00647 00648 /* Put in global buffer same values */ 00649 for (index = 0; index < BufferLength; index++ ) 00650 { 00651 pBuffer[index] = 0x00; 00652 } 00653 } 00654 00655 #ifdef USE_FULL_ASSERT 00656 00657 /** 00658 * @brief Reports the name of the source file and the source line number 00659 * where the assert_param error has occurred. 00660 * @param file: pointer to the source file name 00661 * @param line: assert_param error line source number 00662 * @retval None 00663 */ 00664 void assert_failed(uint8_t* file, uint32_t line) 00665 { 00666 /* User can add his own implementation to report the file name and line number, 00667 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ 00668 00669 /* Infinite loop */ 00670 while (1) 00671 { 00672 } 00673 } 00674 #endif 00675 00676 /** 00677 * @} 00678 */ 00679 00680 /** 00681 * @} 00682 */ 00683 00684 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/