STM32F0xx Standard Peripherals Firmware Library: main.c Source File

STM32F0xx Standard Peripherals Library

STM32F0xx_StdPeriph_Examples/CRC/CRC_TwoBoards/main.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    CRC/CRC_TwoBoards/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 
00029 /* Includes ------------------------------------------------------------------*/
00030 #include "main.h"
00031 
00032 /** @addtogroup CRC_TwoBoards
00033   * @{
00034   */
00035 
00036 /* Private typedef -----------------------------------------------------------*/
00037 /* Private define ------------------------------------------------------------*/
00038 /* Private macro -------------------------------------------------------------*/
00039 /* Private variables ---------------------------------------------------------*/
00040 uint8_t TxBuffer[] = "USART Interrupt Example: Communication between two USART using Interrupt";
00041 uint8_t RxBuffer [RXBUFFERSIZE];
00042 
00043 __IO JOYState_TypeDef PressedButton  = JOY_NONE;
00044 __IO uint8_t RxIndex = 0x00;
00045 __IO uint8_t TxIndex = 0x00;
00046 
00047 /* Private function prototypes -----------------------------------------------*/
00048 static void USART_Config(void);
00049 static void SysTickConfig(void);
00050 static void CRC_Config(uint8_t poly);
00051 
00052 /* Private functions ---------------------------------------------------------*/
00053 
00054 /**
00055   * @brief  Main program.
00056   * @param  None
00057   * @retval None
00058   */
00059 int main(void)
00060 {
00061   /*!< At this stage the microcontrollers clock setting is already configured, 
00062        this is done through SystemInit() function which is called from startup
00063        file (startup_stm32f30x.s) before to branch to application main.
00064        To reconfigure the default setting of SystemInit() function, refer to
00065        system_stm32f30x.c file
00066      */ 
00067   
00068   /* USART configuration -----------------------------------------------------*/
00069   USART_Config();
00070   
00071   /* SysTick configuration ---------------------------------------------------*/
00072   SysTickConfig();
00073   
00074   /* Initialize LEDs mounted on STM32303C-EVAL board */
00075   STM_EVAL_LEDInit(LED1);
00076   STM_EVAL_LEDInit(LED2);
00077   STM_EVAL_LEDInit(LED3);
00078  
00079   /* Configure the CRC peripheral to use the polynomial x8 + x7 + x6 + x4 + x2 + 1 */
00080   CRC_Config(0xD5);
00081   
00082 #ifdef MODE_TRANSMITTER
00083   /* ------------------ USART in mode Tramitter ------------------------------*/
00084 
00085   /* Configure the external interrupt "Joystick SEL" button */
00086   STM_EVAL_PBInit(BUTTON_SEL, BUTTON_MODE_EXTI);
00087 
00088   /* Forever loop */
00089   while (1)
00090   {
00091     
00092     /*Wait "JOY_SEL" to start data transfer */
00093     if ((PressedButton != JOY_NONE))
00094     {
00095       /* Enable the USARTx transmit data register empty interrupt */
00096       USART_ITConfig(USARTx, USART_IT_TXE, ENABLE);
00097       /* Set PressedButton to default value */
00098       PressedButton = JOY_NONE;
00099     }
00100   }
00101 #else
00102   /* ------------------ USART in mode Receiver -------------------------------*/
00103   /* Enable the USARTx receive data register not empty interrupt */
00104   USART_ITConfig(USARTx, USART_IT_RXNE, ENABLE);
00105 
00106   /* Infinite loop */
00107   while(1)
00108   {
00109   }
00110   
00111 #endif /* MODE_TRANSMITTER */
00112 }
00113 
00114 /**
00115   * @brief  Configures the USART Peripheral.
00116   * @param  None
00117   * @retval None
00118   */
00119 static void USART_Config(void)
00120 {
00121   USART_InitTypeDef USART_InitStructure;
00122   NVIC_InitTypeDef NVIC_InitStructure;
00123   GPIO_InitTypeDef GPIO_InitStructure;
00124   
00125   /* Enable GPIO clock */
00126   RCC_AHBPeriphClockCmd(USARTx_TX_GPIO_CLK | USARTx_RX_GPIO_CLK, ENABLE);
00127   
00128   /* Enable USART clock */
00129   USARTx_APBPERIPHCLOCK(USARTx_CLK, ENABLE);
00130   
00131   /* Connect PXx to USARTx_Tx */
00132   GPIO_PinAFConfig(USARTx_TX_GPIO_PORT, USARTx_TX_SOURCE, USARTx_TX_AF);
00133   
00134   /* Connect PXx to USARTx_Rx */
00135   GPIO_PinAFConfig(USARTx_RX_GPIO_PORT, USARTx_RX_SOURCE, USARTx_RX_AF);
00136   
00137   /* Configure USART Tx and Rx as alternate function push-pull */
00138   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
00139   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
00140   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
00141   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
00142   GPIO_InitStructure.GPIO_Pin = USARTx_TX_PIN;
00143   GPIO_Init(USARTx_TX_GPIO_PORT, &GPIO_InitStructure);
00144   
00145   GPIO_InitStructure.GPIO_Pin = USARTx_RX_PIN;
00146   GPIO_Init(USARTx_RX_GPIO_PORT, &GPIO_InitStructure);
00147 
00148   /* USARTx configuration ----------------------------------------------------*/
00149   /* USARTx configured as follow:
00150   - BaudRate = 230400 baud  
00151   - Word Length = 8 Bits
00152   - One Stop Bit
00153   - No parity
00154   - Hardware flow control disabled (RTS and CTS signals)
00155   - Receive and transmit enabled
00156   */
00157   USART_InitStructure.USART_BaudRate = 230400;
00158   USART_InitStructure.USART_WordLength = USART_WordLength_8b;
00159   USART_InitStructure.USART_StopBits = USART_StopBits_1;
00160   USART_InitStructure.USART_Parity = USART_Parity_No;
00161   USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
00162   USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
00163   USART_Init(USARTx, &USART_InitStructure);
00164   
00165   /* NVIC configuration: Enable the USARTx Interrupt */
00166   NVIC_InitStructure.NVIC_IRQChannel = USARTx_IRQn;
00167   NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
00168   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
00169   NVIC_Init(&NVIC_InitStructure);
00170 
00171   /* Enable USART */
00172   USART_Cmd(USARTx, ENABLE);
00173 
00174 }
00175 
00176 /**
00177   * @brief  Configure a SysTick Base time to 10 ms.
00178   * @param  None
00179   * @retval None
00180   */
00181 static void SysTickConfig(void)
00182 {
00183   /* Setup SysTick Timer for 10ms interrupts  */
00184   if (SysTick_Config(SystemCoreClock / 100))
00185   {
00186     /* Capture error */
00187     while (1);
00188   }
00189   /* Configure the SysTick handler priority */
00190   NVIC_SetPriority(SysTick_IRQn, 0x0);
00191 }
00192 
00193 /**
00194   * @brief  Configure CRC peripheral to use 8-bit polynomials
00195   * @param  poly: the CRC polynomial
00196   * @retval None
00197   */
00198 static void CRC_Config(uint8_t poly)
00199 {
00200   /* Enable CRC AHB clock interface */
00201   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_CRC, ENABLE);
00202 
00203   /* DeInit CRC peripheral */
00204   CRC_DeInit();
00205   
00206   /* Init the INIT register */
00207   CRC_SetInitRegister(0);
00208   
00209   /* Select 8-bit polynomial size */
00210   CRC_PolynomialSizeSelect(CRC_PolSize_8);
00211   
00212   /* Set the polynomial coefficients */
00213   CRC_SetPolynomial(poly);
00214 }
00215 
00216 #ifdef  USE_FULL_ASSERT
00217 
00218 /**
00219   * @brief  Reports the name of the source file and the source line number
00220   *         where the assert_param error has occurred.
00221   * @param  file: pointer to the source file name
00222   * @param  line: assert_param error line source number
00223   * @retval None
00224   */
00225 void assert_failed(uint8_t* file, uint32_t line)
00226 { 
00227   /* User can add his own implementation to report the file name and line number,
00228      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00229 
00230   /* Infinite loop */
00231   while (1)
00232   {
00233   }
00234 }
00235 #endif
00236 
00237 /**
00238   * @}
00239   */
00240 
00241 
00242 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
STM32L1xx Standard Peripherals Library: Footer

 

 

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