STM32F10x_StdPeriph_Examples/USART/DMA_Polling/main.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    USART/DMA_Polling/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_DMA_Polling
00030   * @{
00031   */ 
00032 
00033 /* Private typedef -----------------------------------------------------------*/
00034 typedef enum { FAILED = 0, PASSED = !FAILED} TestStatus;
00035 
00036 /* Private define ------------------------------------------------------------*/
00037 #define TxBufferSize1   (countof(TxBuffer1) - 1)
00038 #define TxBufferSize2   (countof(TxBuffer2) - 1)
00039 
00040 /* Private macro -------------------------------------------------------------*/
00041 #define countof(a)   (sizeof(a) / sizeof(*(a)))
00042 
00043 /* Private variables ---------------------------------------------------------*/
00044 USART_InitTypeDef USART_InitStructure;
00045 uint8_t TxBuffer1[] = "USART DMA Polling: USARTy -> USARTz using DMA";
00046 uint8_t TxBuffer2[] = "USART DMA Polling: USARTz -> USARTy using DMA";
00047 uint8_t RxBuffer1[TxBufferSize2];
00048 uint8_t RxBuffer2[TxBufferSize1];
00049 volatile TestStatus TransferStatus1 = FAILED;
00050 volatile TestStatus TransferStatus2 = FAILED; 
00051 
00052 /* Private function prototypes -----------------------------------------------*/
00053 void RCC_Configuration(void);
00054 void GPIO_Configuration(void);
00055 void DMA_Configuration(void);
00056 TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength);
00057 
00058 /* Private functions ---------------------------------------------------------*/
00059 
00060 /**
00061   * @brief   Main program
00062   * @param  None
00063   * @retval None
00064   */
00065 int main(void)
00066 {
00067   /*!< At this stage the microcontroller clock setting is already configured, 
00068        this is done through SystemInit() function which is called from startup
00069        file (startup_stm32f10x_xx.s) before to branch to application main.
00070        To reconfigure the default setting of SystemInit() function, refer to
00071        system_stm32f10x.c file
00072      */     
00073        
00074   /* System Clocks Configuration */
00075   RCC_Configuration();
00076 
00077   /* Configure the GPIO ports */
00078   GPIO_Configuration();
00079 
00080   /* Configure the DMA */
00081   DMA_Configuration();
00082 
00083 /* USARTy and USARTz configuration ------------------------------------------------------*/
00084   /* USARTy and USARTz configured as follow:
00085         - BaudRate = 230400 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 = 230400;
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 USARTy */
00100   USART_Init(USARTy, &USART_InitStructure);
00101   /* Configure USARTz */
00102   USART_Init(USARTz, &USART_InitStructure);
00103 
00104   /* Enable USARTy DMA Rx and TX request */
00105   USART_DMACmd(USARTy, USART_DMAReq_Rx | USART_DMAReq_Tx, ENABLE);
00106   /* Enable USARTz DMA Rx and TX request */
00107   USART_DMACmd(USARTz, USART_DMAReq_Rx | USART_DMAReq_Tx, ENABLE);
00108 
00109   /* Enable USARTy TX DMA1 Channel */
00110   DMA_Cmd(USARTy_Tx_DMA_Channel, ENABLE);
00111   /* Enable USARTy RX DMA1 Channel */
00112   DMA_Cmd(USARTy_Rx_DMA_Channel, ENABLE);
00113 
00114   /* Enable USARTz TX DMA1 Channel */
00115   DMA_Cmd(USARTz_Tx_DMA_Channel, ENABLE);
00116   /* Enable USARTz RX DMA1 Channel */
00117   DMA_Cmd(USARTz_Rx_DMA_Channel, ENABLE);
00118 
00119   /* Enable the USARTy */
00120   USART_Cmd(USARTy, ENABLE);
00121   /* Enable the USARTz */
00122   USART_Cmd(USARTz, ENABLE);
00123 
00124   /* Wait until USARTy TX DMA1 Channel Transfer Complete */
00125   while (DMA_GetFlagStatus(USARTy_Tx_DMA_FLAG) == RESET)
00126   {
00127   }
00128   /* Wait until USARTy RX DMA1 Channel Transfer Complete */
00129   while (DMA_GetFlagStatus(USARTy_Rx_DMA_FLAG) == RESET)
00130   {
00131   }
00132 
00133   /* Wait until USARTz TX DMA1 Channel Transfer Complete */
00134   while (DMA_GetFlagStatus(USARTz_Tx_DMA_FLAG) == RESET)
00135   {
00136   }
00137   /* Wait until USARTz RX DMA1 Channel Transfer Complete */
00138   while (DMA_GetFlagStatus(USARTz_Rx_DMA_FLAG) == RESET)
00139   {
00140   }
00141 
00142   /* Check the received data with the send ones */
00143   TransferStatus1 = Buffercmp(TxBuffer2, RxBuffer1, TxBufferSize2);
00144   /* TransferStatus1 = PASSED, if the data transmitted from USARTz and  
00145      received by USARTy are the same */
00146   /* TransferStatus1 = FAILED, if the data transmitted from USARTz and 
00147      received by USARTy are different */
00148   TransferStatus2 = Buffercmp(TxBuffer1, RxBuffer2, TxBufferSize1);
00149   /* TransferStatus2 = PASSED, if the data transmitted from USARTy and  
00150      received by USARTz are the same */
00151   /* TransferStatus2 = FAILED, if the data transmitted from USARTy and 
00152      received by USARTz are different */
00153 
00154   while (1)
00155   {
00156   }
00157 }
00158 
00159 /**
00160   * @brief  Configures the different system clocks.
00161   * @param  None
00162   * @retval None
00163   */
00164 void RCC_Configuration(void)
00165 {  
00166   /* DMA clock enable */
00167   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
00168 
00169   /* Enable GPIO clock */
00170   RCC_APB2PeriphClockCmd(USARTy_GPIO_CLK | USARTz_GPIO_CLK | RCC_APB2Periph_AFIO, ENABLE);
00171 
00172 #ifndef USE_STM3210C_EVAL
00173   /* Enable USARTy Clock */
00174   RCC_APB2PeriphClockCmd(USARTy_CLK, ENABLE); 
00175 #else
00176   /* Enable USARTy Clock */
00177   RCC_APB1PeriphClockCmd(USARTy_CLK, ENABLE); 
00178 #endif
00179   /* Enable USARTz Clock */
00180   RCC_APB1PeriphClockCmd(USARTz_CLK, ENABLE);  
00181 }
00182 
00183 /**
00184   * @brief  Configures the different GPIO ports.
00185   * @param  None
00186   * @retval None
00187   */
00188 void GPIO_Configuration(void)
00189 {
00190   GPIO_InitTypeDef GPIO_InitStructure;
00191 
00192 #ifdef USE_STM3210C_EVAL
00193   /* Enable the USART3 Pins Software Remapping */
00194   GPIO_PinRemapConfig(GPIO_PartialRemap_USART3, ENABLE);
00195   
00196   /* Enable the USART2 Pins Software Remapping */
00197   GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);  
00198 #elif defined(USE_STM3210B_EVAL) || defined(USE_STM32100B_EVAL)
00199   /* Enable the USART2 Pins Software Remapping */
00200   GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);
00201 #endif
00202 
00203   /* Configure USARTy Rx as input floating */
00204   GPIO_InitStructure.GPIO_Pin = USARTy_RxPin;
00205   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
00206   GPIO_Init(USARTy_GPIO, &GPIO_InitStructure);
00207   
00208   /* Configure USARTz Rx as input floating */
00209   GPIO_InitStructure.GPIO_Pin = USARTz_RxPin;
00210   GPIO_Init(USARTz_GPIO, &GPIO_InitStructure);  
00211   
00212   /* Configure USARTy Tx as alternate function push-pull */
00213   GPIO_InitStructure.GPIO_Pin = USARTy_TxPin;
00214   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
00215   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
00216   GPIO_Init(USARTy_GPIO, &GPIO_InitStructure);
00217 
00218   /* Configure USARTz Tx as alternate function push-pull */
00219   GPIO_InitStructure.GPIO_Pin = USARTz_TxPin;
00220   GPIO_Init(USARTz_GPIO, &GPIO_InitStructure); 
00221 }
00222 
00223 /**
00224   * @brief  Configures the DMA.
00225   * @param  None
00226   * @retval None
00227   */
00228 void DMA_Configuration(void)
00229 {
00230   DMA_InitTypeDef DMA_InitStructure;
00231 
00232   /* USARTy TX DMA1 Channel (triggered by USARTy Tx event) Config */
00233   DMA_DeInit(USARTy_Tx_DMA_Channel);  
00234   DMA_InitStructure.DMA_PeripheralBaseAddr = USARTy_DR_Base;
00235   DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)TxBuffer1;
00236   DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
00237   DMA_InitStructure.DMA_BufferSize = TxBufferSize1;
00238   DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
00239   DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
00240   DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
00241   DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
00242   DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
00243   DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
00244   DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
00245   DMA_Init(USARTy_Tx_DMA_Channel, &DMA_InitStructure);
00246 
00247   /* USARTy RX DMA1 Channel (triggered by USARTy Rx event) Config */
00248   DMA_DeInit(USARTy_Rx_DMA_Channel);  
00249   DMA_InitStructure.DMA_PeripheralBaseAddr = USARTy_DR_Base;
00250   DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)RxBuffer1;
00251   DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
00252   DMA_InitStructure.DMA_BufferSize = TxBufferSize2;
00253   DMA_Init(USARTy_Rx_DMA_Channel, &DMA_InitStructure);
00254   
00255   /* USARTz TX DMA1 Channel (triggered by USARTz Tx event) Config */
00256   DMA_DeInit(USARTz_Tx_DMA_Channel);  
00257   DMA_InitStructure.DMA_PeripheralBaseAddr = USARTz_DR_Base;
00258   DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)TxBuffer2;
00259   DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
00260   DMA_InitStructure.DMA_BufferSize = TxBufferSize2;  
00261   DMA_Init(USARTz_Tx_DMA_Channel, &DMA_InitStructure);
00262   
00263   /* USARTz RX DMA1 Channel (triggered by USARTz Rx event) Config */
00264   DMA_DeInit(USARTz_Rx_DMA_Channel);  
00265   DMA_InitStructure.DMA_PeripheralBaseAddr = USARTz_DR_Base;
00266   DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)RxBuffer2;
00267   DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
00268   DMA_InitStructure.DMA_BufferSize = TxBufferSize1;
00269   DMA_Init(USARTz_Rx_DMA_Channel, &DMA_InitStructure);  
00270 }
00271 
00272 /**
00273   * @brief  Compares two buffers.
00274   * @param  pBuffer1, pBuffer2: buffers to be compared.
00275   * @param  BufferLength: buffer's length
00276   * @retval PASSED: pBuffer1 identical to pBuffer2
00277   *   FAILED: pBuffer1 differs from pBuffer2
00278   */
00279 TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength)
00280 {
00281   while(BufferLength--)
00282   {
00283     if(*pBuffer1 != *pBuffer2)
00284     {
00285       return FAILED;
00286     }
00287 
00288     pBuffer1++;
00289     pBuffer2++;
00290   }
00291 
00292   return PASSED;
00293 }
00294 
00295 #ifdef  USE_FULL_ASSERT
00296 
00297 /**
00298   * @brief  Reports the name of the source file and the source line number
00299   *         where the assert_param error has occurred.
00300   * @param  file: pointer to the source file name
00301   * @param  line: assert_param error line source number
00302   * @retval None
00303   */
00304 void assert_failed(uint8_t* file, uint32_t line)
00305 { 
00306   /* User can add his own implementation to report the file name and line number,
00307      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00308 
00309   /* Infinite loop */
00310   while (1)
00311   {
00312   }
00313 }
00314 #endif
00315 
00316 /**
00317   * @}
00318   */ 
00319 
00320 /**
00321   * @}
00322   */ 
00323 
00324 /******************* (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