STM32F10x_StdPeriph_Examples/USART/DMA_Interrupt/main.c

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