STM32F0xx Standard Peripherals Firmware Library
|
STM32F0xx_StdPeriph_Examples/DMA/DMA_USARTTransfer/main.c
Go to the documentation of this file.
00001 /** 00002 ****************************************************************************** 00003 * @file DMA/DMA_USARTTransfer/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 DMA_USARTTransfer 00036 * @{ 00037 */ 00038 00039 /* Private typedef -----------------------------------------------------------*/ 00040 /* Private define ------------------------------------------------------------*/ 00041 /* Private macro -------------------------------------------------------------*/ 00042 /* Private variables ---------------------------------------------------------*/ 00043 uint8_t TxBuffer1[] = "Communication between USART1-USART2 using DMA2"; 00044 uint8_t TxBuffer2[] = "Communication between USART2-USART1 using DMA2"; 00045 uint8_t RxBuffer2 [RXBUFFERSIZE] = {0}; 00046 uint8_t RxBuffer1 [RXBUFFERSIZE] = {0}; 00047 __IO TestStatus TransferStatus1 = FAILED, TransferStatus2 = FAILED; 00048 USART_InitTypeDef USART_InitStructure; 00049 DMA_InitTypeDef DMA_InitStructure; 00050 00051 /* Private function prototypes -----------------------------------------------*/ 00052 static void RCC_Config(void); 00053 static void GPIO_Config(void); 00054 static TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength); 00055 00056 /* Private functions ---------------------------------------------------------*/ 00057 00058 /** 00059 * @brief Main program. 00060 * @param None 00061 * @retval None 00062 */ 00063 int main(void) 00064 { 00065 /*!< At this stage the microcontroller clock setting is already configured, 00066 this is done through SystemInit() function which is called from startup 00067 file (startup_stm32f0xx.s) before to branch to application main. 00068 To reconfigure the default setting of SystemInit() function, refer to 00069 system_stm32f0xx.c file 00070 */ 00071 /* Enable periph Clock */ 00072 RCC_Config(); 00073 00074 /* Configure Usarts Pins TX/RX */ 00075 GPIO_Config(); 00076 00077 /*------------------------------- USART-------------------------------------*/ 00078 /* USARTx configured as follow: 00079 - BaudRate = 9600 00080 - Word Length = USART_WordLength_7b 00081 - Stop Bit = USART_StopBits_1 00082 - Parity = USART_Parity_No 00083 - Hardware flow control disabled (RTS and CTS signals) 00084 - Receive and transmit enabled 00085 */ 00086 USART_InitStructure.USART_BaudRate = 9600; 00087 USART_InitStructure.USART_WordLength = USART_WordLength_7b; 00088 USART_InitStructure.USART_StopBits = USART_StopBits_1; 00089 USART_InitStructure.USART_Parity = USART_Parity_No; 00090 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; 00091 USART_InitStructure.USART_Mode = USART_Mode_Tx| USART_Mode_Rx; 00092 USART_Init(USART1, &USART_InitStructure); 00093 USART_Init(USART2, &USART_InitStructure); 00094 00095 /*------------------------------- DMA---------------------------------------*/ 00096 /* Common DMA configuration */ 00097 DMA_InitStructure.DMA_BufferSize = TXBUFFERSIZE; 00098 DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; 00099 DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; 00100 DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; 00101 DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; 00102 DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; 00103 DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; 00104 00105 /* DMA2 Channel1 configuration */ 00106 DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)TxBuffer1; 00107 DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; 00108 DMA_InitStructure.DMA_Priority = DMA_Priority_Low; 00109 DMA_InitStructure.DMA_PeripheralBaseAddr = USART1_TDR_Address; 00110 DMA_Init(DMA2_Channel1, &DMA_InitStructure); 00111 00112 /* DMA remap Channel1 to USART1 Tx */ 00113 DMA_RemapConfig(DMA2, DMA2_CH1_USART1_TX); 00114 00115 /* DMA2 Channel2 configuration */ 00116 DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)RxBuffer2; 00117 DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; 00118 DMA_InitStructure.DMA_Priority = DMA_Priority_Medium; 00119 DMA_InitStructure.DMA_PeripheralBaseAddr = USART2_RDR_Address; 00120 DMA_Init(DMA2_Channel2, &DMA_InitStructure); 00121 00122 /* DMA remap Channel2 to USART2 Rx */ 00123 DMA_RemapConfig(DMA2, DMA2_CH2_USART2_RX); 00124 00125 /* DMA2 Channel4 configuration */ 00126 DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)TxBuffer2; 00127 DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; 00128 DMA_InitStructure.DMA_Priority = DMA_Priority_Low; 00129 DMA_InitStructure.DMA_PeripheralBaseAddr = USART2_TDR_Address; 00130 DMA_Init(DMA2_Channel4, &DMA_InitStructure); 00131 00132 /* DMA remap Channel4 to USART2 Tx */ 00133 DMA_RemapConfig(DMA2, DMA2_CH4_USART2_TX); 00134 00135 /* DMA2 Channel3 configuration */ 00136 DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)RxBuffer1; 00137 DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; 00138 DMA_InitStructure.DMA_Priority = DMA_Priority_Medium; 00139 DMA_InitStructure.DMA_PeripheralBaseAddr = USART1_RDR_Address; 00140 DMA_Init(DMA2_Channel3, &DMA_InitStructure); 00141 00142 /* DMA remap Channel43to USART1 Rx */ 00143 DMA_RemapConfig(DMA2, DMA2_CH3_USART1_RX); 00144 00145 /* Enable the USART2 Rx and Tx DMA2 requests */ 00146 USART_DMACmd(USART2, USART_DMAReq_Rx|USART_DMAReq_Tx, ENABLE); 00147 00148 /* Enable the USART1 Rx and Tx DMA2 requests */ 00149 USART_DMACmd(USART1, USART_DMAReq_Tx|USART_DMAReq_Rx, ENABLE); 00150 00151 /* Enable the DMA2 channels */ 00152 DMA_Cmd(DMA2_Channel2, ENABLE); 00153 DMA_Cmd(DMA2_Channel1, ENABLE); 00154 DMA_Cmd(DMA2_Channel3, ENABLE); 00155 DMA_Cmd(DMA2_Channel4, ENABLE); 00156 00157 /* Enable Usart */ 00158 USART_Cmd(USART2, ENABLE); 00159 USART_Cmd(USART1, ENABLE); 00160 00161 /* Test on Channels DMA_FLAG_TC flag */ 00162 while(!DMA_GetFlagStatus(DMA2_FLAG_TC1)); 00163 while(!DMA_GetFlagStatus(DMA2_FLAG_TC2)); 00164 while(!DMA_GetFlagStatus(DMA2_FLAG_TC4)); 00165 while(!DMA_GetFlagStatus(DMA2_FLAG_TC3)); 00166 00167 /* Clear DMA2 TC flags */ 00168 DMA_ClearFlag(DMA2_FLAG_TC1); 00169 DMA_ClearFlag(DMA2_FLAG_TC2); 00170 DMA_ClearFlag(DMA2_FLAG_TC3); 00171 DMA_ClearFlag(DMA2_FLAG_TC4); 00172 00173 /* Disable DMA2 channels */ 00174 DMA_Cmd(DMA2_Channel1, DISABLE); 00175 DMA_Cmd(DMA2_Channel2, DISABLE); 00176 DMA_Cmd(DMA2_Channel3, DISABLE); 00177 DMA_Cmd(DMA2_Channel4, DISABLE); 00178 00179 /* Check the received data with the send ones */ 00180 /* TransferStatus1 = PASSED, if the data transmitted from USART2 and received 00181 by USART1 are the same */ 00182 /* TransferStatus1 = FAILED, if the data transmitted from USART2 and received 00183 by USART1 are different */ 00184 TransferStatus1 = Buffercmp((uint8_t*) RxBuffer1, (uint8_t*) TxBuffer2, RXBUFFERSIZE); 00185 00186 /* TransferStatus2 = PASSED, if the data transmitted from USART1 and received 00187 by USART2 are the same */ 00188 /* TransferStatus2 = FAILED, if the data transmitted from USART1 and received 00189 by USART2 are different */ 00190 TransferStatus2 = Buffercmp((uint8_t*) RxBuffer2, (uint8_t*) TxBuffer1, RXBUFFERSIZE); 00191 00192 /* Infinite Loop */ 00193 while(1); 00194 } 00195 00196 /** 00197 * @brief Configures the USART Peripheral. 00198 * @param None 00199 * @retval None 00200 */ 00201 static void RCC_Config(void) 00202 { 00203 /* Enable GPIO clock */ 00204 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA|RCC_AHBPeriph_GPIOD, ENABLE); 00205 00206 /* Enable USARTs Clock */ 00207 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); 00208 00209 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); 00210 00211 /* Enable the DMA periph */ 00212 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA2, ENABLE); 00213 00214 /* Enable Syscfg */ 00215 RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE); 00216 } 00217 00218 /** 00219 * @brief Configures the USART Peripheral. 00220 * @param None 00221 * @retval None 00222 */ 00223 static void GPIO_Config(void) 00224 { 00225 GPIO_InitTypeDef GPIO_InitStructure; 00226 00227 /* USART1 Pins configuration ************************************************/ 00228 /* Connect pin to Periph */ 00229 GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_1); 00230 GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_1); 00231 00232 /* Configure pins as AF pushpull */ 00233 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10; 00234 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; 00235 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 00236 GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; 00237 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; 00238 GPIO_Init(GPIOA, &GPIO_InitStructure); 00239 00240 /* USART2 Pins configuration ************************************************/ 00241 /* Connect pin to Periph */ 00242 GPIO_PinAFConfig(GPIOD, GPIO_PinSource6, GPIO_AF_0); 00243 GPIO_PinAFConfig(GPIOD, GPIO_PinSource5, GPIO_AF_0); 00244 00245 /* Configure pins as AF pushpull */ 00246 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6; 00247 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; 00248 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 00249 GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; 00250 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; 00251 GPIO_Init(GPIOD, &GPIO_InitStructure); 00252 } 00253 00254 /** 00255 * @brief Compares two buffers. 00256 * @param pBuffer1, pBuffer2: buffers to be compared. 00257 * @param BufferLength: buffer's length 00258 * @retval PASSED: pBuffer1 identical to pBuffer2 00259 * FAILED: pBuffer1 differs from pBuffer2 00260 */ 00261 static TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength) 00262 { 00263 while (BufferLength--) 00264 { 00265 if (*pBuffer1 != *pBuffer2) 00266 { 00267 return FAILED; 00268 } 00269 pBuffer1++; 00270 pBuffer2++; 00271 } 00272 00273 return PASSED; 00274 } 00275 00276 #ifdef USE_FULL_ASSERT 00277 00278 /** 00279 * @brief Reports the name of the source file and the source line number 00280 * where the assert_param error has occurred. 00281 * @param file: pointer to the source file name 00282 * @param line: assert_param error line source number 00283 * @retval None 00284 */ 00285 void assert_failed(uint8_t* file, uint32_t line) 00286 { 00287 /* User can add his own implementation to report the file name and line number, 00288 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ 00289 00290 /* Infinite loop */ 00291 while (1) 00292 { 00293 } 00294 } 00295 #endif 00296 00297 /** 00298 * @} 00299 */ 00300 00301 /** 00302 * @} 00303 */ 00304 00305 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/