STM32F0xx Standard Peripherals Firmware Library: main.c Source File

STM32F0xx Standard Peripherals Library

STM32F0xx_StdPeriph_Examples/CAN/CAN_DualFIFO/main.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    CAN/CAN_DualFIFO/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 STM32F0xx_StdPeriph_Examples
00033   * @{
00034   */
00035 
00036 /** @addtogroup CAN_DualFIFO
00037   * @{
00038   */
00039 
00040 /* Private typedef -----------------------------------------------------------*/
00041 /* Private define ------------------------------------------------------------*/
00042 #define MESSAGE1   "   STM32072B-EVAL   "
00043 #define MESSAGE2   "TAMPER:Send Frame to FIFO 0"
00044 #define MESSAGE3   "SEL: Send Frame to FIFO 1"
00045 
00046 /* Private macro -------------------------------------------------------------*/
00047 /* Private variables ---------------------------------------------------------*/
00048 CanTxMsg TxMessage = {0}, TxMessage1 = {0};
00049 uint8_t KeyNumber = 0x0;
00050 
00051 /* Private function prototypes -----------------------------------------------*/
00052 static void CAN_Config(void);
00053 
00054 /* Private functions ---------------------------------------------------------*/
00055 
00056 /**
00057   * @brief  Main program.
00058   * @param  None
00059   * @retval None
00060   */
00061 int main(void)
00062 {
00063   /*!< At this stage the microcontroller clock setting is already configured, 
00064        this is done through SystemInit() function which is called from startup
00065        file (startup_stm32f0xx.s) before to branch to application main.
00066        To reconfigure the default setting of SystemInit() function, refer to
00067        system_stm32f0xx.c file
00068      */
00069   
00070   /* Initialize the LCD */
00071   STM32072B_LCD_Init();      
00072   
00073   /* Clear the LCD */
00074   LCD_Clear(LCD_COLOR_WHITE);
00075   
00076   /* Set the LCD Back Color */
00077   LCD_SetBackColor(Blue);
00078   
00079   /* Set the LCD Text Color */
00080   LCD_SetTextColor(White);
00081   
00082   /* Displays MESSAGE1 on line 0 */
00083   LCD_DisplayStringLine(LINE(0), (uint8_t *)MESSAGE1);
00084   
00085   /* Set the LCD Text size */
00086   LCD_SetFont(&Font8x12);
00087   
00088   /* Display */
00089   LCD_DisplayStringLine(LINE(0x13), "CAN CAN_DualFIFO using FIFO 0 and FIFO 1 ");
00090   
00091   /* Set the LCD Back Color */
00092   LCD_SetBackColor(White);
00093   /* Set the LCD Text Color */
00094   LCD_SetTextColor(Blue);
00095   /* Set the LCD Text size */
00096   LCD_SetFont(&Font12x12);
00097   
00098   /* Display Messages on the the LCD */  
00099   LCD_DisplayStringLine(LINE(0x7), (uint8_t *)MESSAGE2);
00100   LCD_DisplayStringLine(LINE(0x8), (uint8_t *)MESSAGE3);
00101                         
00102   /* Set the LCD Text size */
00103   LCD_SetFont(&Font16x24);
00104   
00105   /* Configures LED 1..4 */
00106   STM_EVAL_LEDInit(LED1);
00107   STM_EVAL_LEDInit(LED2);
00108   STM_EVAL_LEDInit(LED3);
00109   STM_EVAL_LEDInit(LED4);
00110   
00111   /* Configure Push button key */
00112   STM_EVAL_PBInit(BUTTON_TAMPER, BUTTON_MODE_EXTI); 
00113   
00114   /* Configure Push button sel */
00115   STM_EVAL_PBInit(BUTTON_SEL, BUTTON_MODE_EXTI); 
00116    
00117   /* CAN configuration */
00118   CAN_Config();
00119   
00120   /* Infinite loop */
00121   while(1)
00122   {
00123   }
00124 }
00125 
00126 /**
00127   * @brief  Configures the CAN.
00128   * @param  None
00129   * @retval None
00130   */
00131 static void CAN_Config(void)
00132 {
00133   GPIO_InitTypeDef  GPIO_InitStructure;
00134   NVIC_InitTypeDef  NVIC_InitStructure;
00135   CAN_InitTypeDef        CAN_InitStructure;
00136   CAN_FilterInitTypeDef  CAN_FilterInitStructure;
00137     
00138   /* CAN GPIOs configuration **************************************************/
00139 
00140   /* Enable GPIO clock */
00141   RCC_AHBPeriphClockCmd(CAN_GPIO_CLK, ENABLE);
00142 
00143   /* Connect CAN pins to AF7 */
00144   GPIO_PinAFConfig(CAN_GPIO_PORT, CAN_RX_SOURCE, CAN_AF_PORT);
00145   GPIO_PinAFConfig(CAN_GPIO_PORT, CAN_TX_SOURCE, CAN_AF_PORT); 
00146   
00147   /* Configure CAN RX and TX pins */
00148   GPIO_InitStructure.GPIO_Pin = CAN_RX_PIN | CAN_TX_PIN;
00149   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
00150   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
00151   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
00152   GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;
00153   GPIO_Init(CAN_GPIO_PORT, &GPIO_InitStructure);
00154 
00155   /* NVIC configuration *******************************************************/
00156   NVIC_InitStructure.NVIC_IRQChannel = CEC_CAN_IRQn;
00157   NVIC_InitStructure.NVIC_IRQChannelPriority = 0x0;
00158   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
00159   NVIC_Init(&NVIC_InitStructure);
00160 
00161   /* CAN configuration ********************************************************/  
00162   /* Enable CAN clock */
00163   RCC_APB1PeriphClockCmd(CAN_CLK, ENABLE);
00164   
00165   /* CAN register init */
00166   CAN_DeInit(CANx);
00167   CAN_StructInit(&CAN_InitStructure);
00168 
00169   /* CAN cell init */
00170   CAN_InitStructure.CAN_TTCM = DISABLE;
00171   CAN_InitStructure.CAN_ABOM = DISABLE;
00172   CAN_InitStructure.CAN_AWUM = DISABLE;
00173   CAN_InitStructure.CAN_NART = DISABLE;
00174   CAN_InitStructure.CAN_RFLM = DISABLE;
00175   CAN_InitStructure.CAN_TXFP = DISABLE;
00176   CAN_InitStructure.CAN_Mode = CAN_Mode_Normal;
00177   CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
00178     
00179   /* CAN Baudrate = 1MBps (CAN clocked at 36 MHz) */
00180   CAN_InitStructure.CAN_BS1 = CAN_BS1_9tq;
00181   CAN_InitStructure.CAN_BS2 = CAN_BS2_8tq;
00182   CAN_InitStructure.CAN_Prescaler = 2;
00183   CAN_Init(CANx, &CAN_InitStructure);
00184 
00185   /* CAN filter init "FIFO0" */
00186   CAN_FilterInitStructure.CAN_FilterNumber = 0;
00187   CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdList;
00188   CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit;
00189   CAN_FilterInitStructure.CAN_FilterIdHigh = 0x6420;
00190   CAN_FilterInitStructure.CAN_FilterIdLow = 0x2461;
00191   CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x0000;
00192   CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0x0000;
00193   CAN_FilterInitStructure.CAN_FilterFIFOAssignment = 0;
00194   CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;
00195   CAN_FilterInit(&CAN_FilterInitStructure);
00196   
00197   /* CAN filter init "FIFO1" */
00198   CAN_FilterInitStructure.CAN_FilterNumber = 1;
00199   CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdList;
00200   CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit;
00201   CAN_FilterInitStructure.CAN_FilterIdHigh = 0x2460;
00202   CAN_FilterInitStructure.CAN_FilterIdLow = 0x0000;
00203   CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x0000;
00204   CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0x0000;
00205   CAN_FilterInitStructure.CAN_FilterFIFOAssignment = 1;
00206   CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;
00207   CAN_FilterInit(&CAN_FilterInitStructure);
00208   
00209   
00210   /* Transmit Structure preparation */
00211   TxMessage.StdId = 0x321;
00212   TxMessage.ExtId = 0x00;
00213   TxMessage.RTR = CAN_RTR_DATA;
00214   TxMessage.IDE = CAN_ID_STD;
00215   TxMessage.DLC = 1;
00216   
00217   /* Transmit Structure preparation 2*/
00218   TxMessage1.StdId = 0x123;
00219   TxMessage1.ExtId = 0x01;
00220   TxMessage1.RTR = CAN_RTR_DATA;
00221   TxMessage1.IDE = CAN_ID_STD;
00222   TxMessage1.DLC = 1;
00223   
00224   /* Enable FIFO 0 message pending Interrupt */
00225   CAN_ITConfig(CANx, CAN_IT_FMP0, ENABLE);
00226   
00227   /* Enable FIFO 1 message pending Interrupt */
00228   CAN_ITConfig(CANx, CAN_IT_FMP1, ENABLE);
00229 }
00230 
00231 /**
00232   * @brief  Turn ON/OFF the dedicated led
00233   * @param  Ledstatus: Led number from 1 to 4.
00234   * @retval None
00235   */
00236 void LED_Display(uint8_t Ledstatus)
00237 {
00238   /* Turn off all leds */
00239   STM_EVAL_LEDOff(LED1);
00240   STM_EVAL_LEDOff(LED2);
00241   STM_EVAL_LEDOff(LED3);
00242   STM_EVAL_LEDOff(LED4);
00243   
00244   switch(Ledstatus)
00245   {
00246     case(1): 
00247       STM_EVAL_LEDOn(LED1);
00248       break;
00249    
00250     case(2): 
00251       STM_EVAL_LEDOn(LED2);
00252       break;
00253  
00254     case(3): 
00255       STM_EVAL_LEDOn(LED3);
00256       break;
00257 
00258     case(4): 
00259       STM_EVAL_LEDOn(LED4);
00260       break;
00261     default:
00262       break;
00263   }
00264 }
00265 
00266 #ifdef  USE_FULL_ASSERT
00267 
00268 /**
00269   * @brief  Reports the name of the source file and the source line number
00270   *         where the assert_param error has occurred.
00271   * @param  file: pointer to the source file name
00272   * @param  line: assert_param error line source number
00273   * @retval None
00274   */
00275 void assert_failed(uint8_t* file, uint32_t line)
00276 { 
00277   /* User can add his own implementation to report the file name and line number,
00278      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00279 
00280   /* Infinite loop */
00281   while (1)
00282   {
00283   }
00284 }
00285 #endif
00286 
00287 /**
00288   * @}
00289   */
00290 
00291 /**
00292   * @}
00293   */
00294 
00295 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
STM32L1xx Standard Peripherals Library: Footer

 

 

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