STM32F0xx Standard Peripherals Firmware Library: main.c Source File

STM32F0xx Standard Peripherals Library

STM32F0xx_StdPeriph_Examples/USART/USART_8xUsartsOneBoard/main.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    USART/USART_8xUsartsOneBoard/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>&copy; 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_8xUsartsOneBoard
00036   * @{
00037   */
00038 
00039 /* Private typedef -----------------------------------------------------------*/
00040 /* Private define ------------------------------------------------------------*/
00041 /* Private macro -------------------------------------------------------------*/
00042 /* Private variables ---------------------------------------------------------*/
00043 static __IO uint32_t TimingDelay;
00044 
00045 /* Buffer used for transmission */
00046 uint8_t aTxBuffer[BUFFER_SIZE] = "USART Example: 8xUsarts Tx/Rx Communication";
00047 uint8_t aRxBuffer[USART_MAX_INDEX][BUFFER_SIZE];
00048 /* Buffer used for reception */
00049 USART_InitTypeDef USART_InitStructure;
00050 __IO uint8_t TxCounter = 0;
00051 extern __IO uint8_t ReceiveState;
00052 USART_TypeDef* UsartInstance[USART_MAX_INDEX] = {USART1, USART2, USART3, USART4, USART5, USART6, USART7, USART8};
00053 uint8_t UsartIndex = 0;
00054 /* Private function prototypes -----------------------------------------------*/
00055 static uint16_t Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength);
00056 static void RCC_Configuration(void);
00057 static void GPIO_Configuration(void);
00058 static void NVIC_Configuration(void);
00059 static void Delay(__IO uint32_t nTime);
00060 
00061 /* Private functions ---------------------------------------------------------*/
00062 
00063 /**
00064   * @brief  Main program.
00065   * @param  None
00066   * @retval None
00067   */
00068 int main(void)
00069 {
00070   uint8_t *buffptr;
00071   
00072   /*!< At this stage the microcontroller clock setting is already configured, 
00073   this is done through SystemInit() function which is called from startup
00074   file (startup_stm32f0xx.s) before to branch to application main.
00075   To reconfigure the default setting of SystemInit() function, refer to
00076   system_stm32f0xx.c file
00077   */ 
00078   if (SysTick_Config(SystemCoreClock / 1000))
00079   { 
00080     /* error */ 
00081     while (1);
00082   }
00083   /* Configure clock GPIO, USARTs */
00084   RCC_Configuration();
00085   
00086   /* Configure GPIO Pin Tx/Rx for Usart communication */
00087   GPIO_Configuration();
00088   
00089   /* Configure NVIC */
00090   NVIC_Configuration();
00091   
00092   /* 8xUSARTs configuration --------------------------------------------------*/
00093   /* 8xUSARTs  configured as follow:
00094   - BaudRate = 9600 baud  
00095   - Word Length = 8 Bits
00096   - One Stop Bit
00097   - No parity
00098   - Hardware flow control disabled (RTS and CTS signals)
00099   - Receive and transmit enabled
00100   */
00101   USART_InitStructure.USART_BaudRate = 9600;
00102   USART_InitStructure.USART_WordLength = USART_WordLength_8b;
00103   USART_InitStructure.USART_StopBits = USART_StopBits_1;
00104   USART_InitStructure.USART_Parity = USART_Parity_No;
00105   USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
00106   USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
00107   
00108   /* Prepare all uart to receive a data packet */
00109   for(UsartIndex = 0; UsartIndex < USART_MAX_INDEX; UsartIndex++)
00110   {
00111     USART_Init(UsartInstance[UsartIndex], &USART_InitStructure);
00112     
00113     /* Enable 8xUSARTs Receive interrupts */
00114     USART_ITConfig(UsartInstance[UsartIndex], USART_IT_RXNE, ENABLE);
00115     
00116     /* Enable the 8xUSARTs */
00117     USART_Cmd(UsartInstance[UsartIndex], ENABLE);
00118   }
00119   
00120   /* Infinite Loop */
00121   while(1)
00122   {
00123     /* Set aRxBuffer to 0 */
00124     memset(aRxBuffer, 0x0, BUFFER_SIZE * USART_MAX_INDEX);
00125     
00126     /* When data has been transferred start the next transfer */
00127     for(UsartIndex = 0; UsartIndex < USART_MAX_INDEX; UsartIndex++)
00128     { 
00129       /* Initialize the global variable to handle the data transfer */
00130       TxCounter = 0;
00131       ReceiveState = 0;
00132       
00133       /* Delay to let time to see counter incrementation on the screen */
00134       Delay(2000);
00135       /* Set the data buffptr to use for the transfer */
00136       if (UsartIndex == 0)
00137       {
00138         buffptr = aTxBuffer;
00139       }
00140       else
00141       {
00142         buffptr = aRxBuffer[UsartIndex];
00143       }
00144       
00145       while(TxCounter < BUFFER_SIZE)
00146       {   
00147         /* Send one byte from USART1 to USARTx */
00148         USART_SendData(UsartInstance[UsartIndex], buffptr[TxCounter++]);
00149         
00150         /* Loop until USART1 DR register is empty */ 
00151         while(USART_GetFlagStatus(UsartInstance[UsartIndex], USART_FLAG_TXE) == RESET)
00152         {}
00153       }
00154       
00155       while(ReceiveState == 0);
00156       /* Compares two buffers */
00157       if(UsartIndex == (USART_MAX_INDEX - 1))
00158       {
00159         if( Buffercmp((uint8_t*) aRxBuffer[0], (uint8_t*) aTxBuffer, BUFFER_SIZE))
00160         {
00161           /* Error */
00162           while(1);
00163         }
00164       }
00165       else
00166       {
00167         if( Buffercmp((uint8_t*) aRxBuffer[UsartIndex+1], (uint8_t*) aTxBuffer, BUFFER_SIZE))
00168         {
00169           /* Error */
00170           while(1);
00171         }
00172       }
00173       ReceiveState = 0;
00174     }
00175     /* Insert delay */
00176     Delay(2000);
00177   }
00178 }
00179 
00180 /**
00181   * @brief  Decrements the TimingDelay variable.
00182   * @param  None
00183   * @retval None
00184   */
00185 void TimingDelay_Decrement(void)
00186 {
00187   if (TimingDelay != 0x00)
00188   { 
00189     TimingDelay--;
00190   }
00191 }
00192 
00193 /**
00194   * @brief  Configures the different system clocks.
00195   * @param  None
00196   * @retval None
00197   */
00198 static void RCC_Configuration(void)
00199 {   
00200   /* Enable GPIO clock */
00201   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA|RCC_AHBPeriph_GPIOB|RCC_AHBPeriph_GPIOC|RCC_AHBPeriph_GPIOD|RCC_AHBPeriph_GPIOF, ENABLE);
00202   
00203   /* Enable 8xUSARTs Clock */
00204   RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2|RCC_APB1Periph_USART3|RCC_APB1Periph_USART4|RCC_APB1Periph_USART5, ENABLE);  
00205   
00206   RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6|RCC_APB2Periph_USART7|RCC_APB2Periph_USART8|RCC_APB2Periph_USART1, ENABLE);
00207 }
00208 
00209 /**
00210   * @brief  Configures the different GPIO ports.
00211   * @param  None
00212   * @retval None
00213   */
00214 static void GPIO_Configuration(void)
00215 {
00216   GPIO_InitTypeDef GPIO_InitStructure;
00217   
00218   /* USART1 Pins configuration ************************************************/
00219   /* Connect pin to Periph */
00220   GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_1); 
00221   GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_1);    
00222   
00223   /* Configure pins as AF pushpull */
00224   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
00225   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
00226   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
00227   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
00228   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
00229   GPIO_Init(GPIOA, &GPIO_InitStructure);  
00230 
00231   /* USART2 Pins configuration ************************************************/  
00232   /* Connect pin to Periph */
00233   GPIO_PinAFConfig(GPIOD, GPIO_PinSource6, GPIO_AF_0);
00234   GPIO_PinAFConfig(GPIOD, GPIO_PinSource5, GPIO_AF_0);    
00235   
00236   /* Configure pins as AF pushpull */
00237   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6;
00238   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
00239   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
00240   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
00241   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
00242   GPIO_Init(GPIOD, &GPIO_InitStructure);   
00243   
00244   /* USART3 Pins configuration  ***********************************************/  
00245   /* Connect pin to Periph */
00246   GPIO_PinAFConfig(GPIOD, GPIO_PinSource8, GPIO_AF_0);
00247   GPIO_PinAFConfig(GPIOD, GPIO_PinSource9, GPIO_AF_0);    
00248   
00249   /* Configure pins as AF pushpull */
00250   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
00251   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
00252   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
00253   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
00254   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
00255   GPIO_Init(GPIOD, &GPIO_InitStructure); 
00256   
00257   /* USART4 Pins configuration  ***********************************************/  
00258   /* Connect pin to Periph */
00259   GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_0);
00260   GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_0);    
00261  
00262   /* Configure pins as AF pushpull */
00263   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
00264   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
00265   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
00266   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
00267   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
00268   GPIO_Init(GPIOC, &GPIO_InitStructure); 
00269   
00270   /* USART5 Pins configuration*************************************************/  
00271   /* Connect pin to Periph */
00272   GPIO_PinAFConfig(GPIOB, GPIO_PinSource3, GPIO_AF_4);
00273   GPIO_PinAFConfig(GPIOB, GPIO_PinSource4, GPIO_AF_4);    
00274   
00275   /* Configure pins as AF pushpull */
00276   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_4;
00277   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
00278   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
00279   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
00280   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
00281   GPIO_Init(GPIOB, &GPIO_InitStructure);
00282   
00283   /* USART6 Pins configuration*************************************************/  
00284   /* Connect pin to Periph */
00285   GPIO_PinAFConfig(GPIOF, GPIO_PinSource9, GPIO_AF_1);
00286   GPIO_PinAFConfig(GPIOF, GPIO_PinSource10, GPIO_AF_1);    
00287   
00288   /* Configure pins as AF pushpull */
00289   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_9;
00290   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
00291   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
00292   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
00293   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
00294   GPIO_Init(GPIOF, &GPIO_InitStructure); 
00295   
00296   /* USART7 Pins configuration  *************************************************/  
00297   /* Connect pin to Periph */
00298   GPIO_PinAFConfig(GPIOF, GPIO_PinSource2, GPIO_AF_1);
00299   GPIO_PinAFConfig(GPIOF, GPIO_PinSource3, GPIO_AF_1);    
00300   
00301   /* Configure pins as AF pushpull */
00302   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
00303   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
00304   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
00305   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
00306   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
00307   GPIO_Init(GPIOF, &GPIO_InitStructure); 
00308     
00309   /* USART8 Pins configuration  *************************************************/  
00310   /* Connect pin to Periph */
00311   GPIO_PinAFConfig(GPIOC, GPIO_PinSource8, GPIO_AF_1);
00312   GPIO_PinAFConfig(GPIOC, GPIO_PinSource9, GPIO_AF_1);    
00313   
00314   /* Configure pins as AF pushpull */
00315   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
00316   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
00317   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
00318   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
00319   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
00320   GPIO_Init(GPIOC, &GPIO_InitStructure); 
00321 }
00322 
00323 /**
00324   * @brief  Configures the nested vectored interrupt controller.
00325   * @param  None
00326   * @retval None
00327   */
00328 static void NVIC_Configuration(void)
00329 {
00330   NVIC_InitTypeDef NVIC_InitStructure;
00331   
00332   /* USART1 IRQ Channel configuration */
00333   NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
00334   NVIC_InitStructure.NVIC_IRQChannelPriority = 0x01;
00335   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
00336   NVIC_Init(&NVIC_InitStructure);
00337   
00338   /* USART2 IRQ Channel configuration */
00339   NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
00340   NVIC_InitStructure.NVIC_IRQChannelPriority = 0x01;
00341   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
00342   NVIC_Init(&NVIC_InitStructure);
00343   
00344   /* USART3_8 IRQ Channel configuration */
00345   NVIC_InitStructure.NVIC_IRQChannel = USART3_8_IRQn;
00346   NVIC_InitStructure.NVIC_IRQChannelPriority = 0x01;
00347   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
00348   NVIC_Init(&NVIC_InitStructure);
00349 }
00350 
00351 /**
00352   * @brief  Inserts a delay time.
00353   * @param  nTime: specifies the delay time length, in milliseconds.
00354   * @retval None
00355   */
00356 static void Delay(__IO uint32_t nTime)
00357 { 
00358   TimingDelay = nTime;
00359 
00360   while(TimingDelay != 0);
00361 }
00362 
00363 /**
00364   * @brief  Compares two buffers.
00365   * @param  pBuffer1, pBuffer2: buffers to be compared.
00366   * @param  BufferLength: buffer's length
00367   * @retval 0  : pBuffer1 identical to pBuffer2
00368   *        >0 : pBuffer1 differs from pBuffer2
00369   */
00370 static uint16_t Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength)
00371 {
00372   while (BufferLength--)
00373   {
00374     if ((*pBuffer1) != *pBuffer2)
00375     {
00376       return BufferLength;
00377     }
00378     pBuffer1++;
00379     pBuffer2++;
00380   }
00381 
00382   return 0;
00383 }
00384 
00385 #ifdef  USE_FULL_ASSERT
00386 
00387 /**
00388   * @brief  Reports the name of the source file and the source line number
00389   *         where the assert_param error has occurred.
00390   * @param  file: pointer to the source file name
00391   * @param  line: assert_param error line source number
00392   * @retval None
00393   */
00394 void assert_failed(uint8_t* file, uint32_t line)
00395 { 
00396   /* User can add his own implementation to report the file name and line number,
00397      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00398 
00399   /* Infinite loop */
00400   while (1)
00401   {
00402   }
00403 }
00404 #endif
00405 
00406 /**
00407   * @}
00408   */
00409 
00410 /**
00411   * @}
00412   */
00413 
00414 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
STM32L1xx Standard Peripherals Library: Footer

 

 

 For complete documentation on STM32 Microcontrollers visit www.st.com/STM32