STM32F10x_StdPeriph_Examples/CAN/DualCAN/main.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    CAN/DualCAN/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 "stm32_eval.h"
00023 #include "stm3210c_eval_lcd.h"
00024 
00025 /** @addtogroup STM32F10x_StdPeriph_Examples
00026   * @{
00027   */
00028 
00029 /** @addtogroup CAN_DualCAN
00030   * @{
00031   */ 
00032 
00033 /* Private typedef -----------------------------------------------------------*/
00034 /* Private define ------------------------------------------------------------*/
00035 /* Private define ------------------------------------------------------------*/
00036 /* Private macro -------------------------------------------------------------*/
00037 /* Private variables ---------------------------------------------------------*/
00038 CAN_InitTypeDef        CAN_InitStructure;
00039 CAN_FilterInitTypeDef  CAN_FilterInitStructure;
00040 CanTxMsg TxMessage;
00041 
00042 /* Private function prototypes -----------------------------------------------*/
00043 void NVIC_Config(void);
00044 void CAN_Config(void);
00045 void LED_Display(uint8_t Ledstatus);
00046 void Init_RxMes(CanRxMsg *RxMessage);
00047 void Delay(void);
00048 
00049 /* Private functions ---------------------------------------------------------*/
00050 
00051 /**
00052   * @brief  Main program.
00053   * @param  None
00054   * @retval None
00055   */
00056 int main(void)
00057 {
00058   /*!< At this stage the microcontroller clock setting is already configured, 
00059        this is done through SystemInit() function which is called from startup
00060        file (startup_stm32f10x_xx.s) before to branch to application main.
00061        To reconfigure the default setting of SystemInit() function, refer to
00062        system_stm32f10x.c file
00063      */
00064         
00065   /* NVIC configuration */
00066   NVIC_Config();
00067 
00068   /* Configures LED 1..4 */
00069   STM_EVAL_LEDInit(LED1);
00070   STM_EVAL_LEDInit(LED2);
00071   STM_EVAL_LEDInit(LED3);
00072   STM_EVAL_LEDInit(LED4);
00073 
00074   /* LCD Initilalization */
00075   STM3210C_LCD_Init();
00076   LCD_Clear(LCD_COLOR_WHITE);
00077 
00078   /* Set the LCD Back Color */
00079   LCD_SetBackColor(LCD_COLOR_RED);
00080   /* Set the LCD Text Color */
00081   LCD_SetTextColor(LCD_COLOR_GREEN);
00082   
00083   LCD_DisplayStringLine(LCD_LINE_0, "   STM3210C-EVAL    ");
00084   LCD_DisplayStringLine(LCD_LINE_1, " STM32F10x Dual CAN ");
00085   LCD_DisplayStringLine(LCD_LINE_2, "To start Press on:  ");
00086   LCD_DisplayStringLine(LCD_LINE_3, "Key or Tamper Button");
00087 
00088   /* Set the LCD Back Color */
00089   LCD_SetBackColor(LCD_COLOR_BLUE);
00090   /* Set the LCD Text Color */
00091   LCD_SetTextColor(LCD_COLOR_WHITE);   
00092     
00093   /* Configure BUTTON_KEY */
00094   STM_EVAL_PBInit(BUTTON_KEY, BUTTON_MODE_GPIO); 
00095   
00096   /* Configure BUTTON_TAMPER */
00097   STM_EVAL_PBInit(BUTTON_TAMPER, BUTTON_MODE_GPIO); 
00098    
00099   /* CANs configuration */
00100   CAN_Config();
00101 
00102   /* IT Configuration for CAN1 */  
00103   CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE);
00104 
00105   /* IT Configuration for CAN2 */  
00106   CAN_ITConfig(CAN2, CAN_IT_FMP0, ENABLE);
00107 
00108   /* turn off all leds*/
00109   STM_EVAL_LEDOff(LED1);
00110   STM_EVAL_LEDOff(LED2);
00111   STM_EVAL_LEDOff(LED3);
00112   STM_EVAL_LEDOff(LED4);
00113  
00114   /* Infinite loop */
00115   while(1)
00116   {
00117     if(STM_EVAL_PBGetState(BUTTON_KEY)== RESET)
00118     {
00119       /* Turn On LED1 */
00120       LED_Display(0x01);
00121       TxMessage.Data[0] = 0x55;
00122       CAN_Transmit(CAN1, &TxMessage);
00123 
00124       /* Loop while KEY button is pressed */
00125       while(STM_EVAL_PBGetState(BUTTON_KEY)== RESET)
00126       {
00127       }
00128     }
00129     if(STM_EVAL_PBGetState(BUTTON_TAMPER)== RESET)
00130     {
00131       /* Turn On LED2 */
00132       LED_Display(0x2);
00133       TxMessage.Data[0] = 0xAA;
00134       CAN_Transmit(CAN2, &TxMessage);
00135 
00136       /* Loop while TAMPER button is pressed */
00137       while(STM_EVAL_PBGetState(BUTTON_TAMPER)== RESET)
00138       {
00139       }
00140     }
00141   }
00142 }
00143 
00144 /**
00145   * @brief  Configures CAN1 and CAN2.
00146   * @param  None
00147   * @retval None
00148   */
00149 void CAN_Config(void)
00150 {
00151   GPIO_InitTypeDef  GPIO_InitStructure;
00152 
00153   /* Configure CAN1 and CAN2 IOs **********************************************/
00154   /* GPIOB, GPIOD and AFIO clocks enable */
00155   RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOB, ENABLE);
00156      
00157   /* Configure CAN1 RX pin */
00158   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
00159   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
00160   GPIO_Init(GPIOD, &GPIO_InitStructure);
00161    
00162   /* Configure CAN2 RX pin */
00163   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
00164   GPIO_Init(GPIOB, &GPIO_InitStructure);
00165 
00166   /* Configure CAN1 TX pin */
00167   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
00168   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
00169   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
00170   GPIO_Init(GPIOD, &GPIO_InitStructure);
00171 
00172   /* Configure CAN2 TX pin */
00173   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
00174   GPIO_Init(GPIOB, &GPIO_InitStructure);
00175 
00176   /* Remap CAN1 and CAN2 GPIOs */
00177   GPIO_PinRemapConfig(GPIO_Remap2_CAN1 , ENABLE);
00178   GPIO_PinRemapConfig(GPIO_Remap_CAN2, ENABLE);
00179 
00180   /* Configure CAN1 and CAN2 **************************************************/  
00181   /* CAN1 and CAN2 Periph clocks enable */
00182   RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1 | RCC_APB1Periph_CAN2, ENABLE);  
00183   
00184    /* CAN1 and CAN2 register init */
00185   CAN_DeInit(CAN1);
00186   CAN_DeInit(CAN2);
00187 
00188   /* Struct init*/
00189   CAN_StructInit(&CAN_InitStructure);
00190 
00191   /* CAN1 and CAN2  cell init */
00192   CAN_InitStructure.CAN_TTCM = DISABLE;
00193   CAN_InitStructure.CAN_ABOM = DISABLE;
00194   CAN_InitStructure.CAN_AWUM = DISABLE;
00195   CAN_InitStructure.CAN_NART = DISABLE;
00196   CAN_InitStructure.CAN_RFLM = DISABLE;
00197   CAN_InitStructure.CAN_TXFP = ENABLE;
00198   CAN_InitStructure.CAN_Mode = CAN_Mode_Normal;
00199   CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
00200   CAN_InitStructure.CAN_BS1 = CAN_BS1_3tq;
00201   CAN_InitStructure.CAN_BS2 = CAN_BS2_2tq;
00202   CAN_InitStructure.CAN_Prescaler =5;
00203   
00204   /*Initializes the CAN1  and CAN2 */
00205   CAN_Init(CAN1, &CAN_InitStructure);
00206   CAN_Init(CAN2, &CAN_InitStructure);
00207 
00208   /* CAN1 filter init */
00209   CAN_FilterInitStructure.CAN_FilterNumber = 1;
00210   CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask;
00211   CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit;
00212   CAN_FilterInitStructure.CAN_FilterIdHigh = 0x6420;
00213   CAN_FilterInitStructure.CAN_FilterIdLow = 0x0000;
00214   CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x0000;
00215   CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0x0000;
00216   CAN_FilterInitStructure.CAN_FilterFIFOAssignment = 0;
00217   CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;
00218   CAN_FilterInit(&CAN_FilterInitStructure);
00219   
00220   /* CAN2 filter init */
00221   CAN_FilterInitStructure.CAN_FilterIdHigh =0x2460;
00222   CAN_FilterInitStructure.CAN_FilterNumber = 15;
00223   CAN_FilterInit(&CAN_FilterInitStructure);
00224   
00225   /* Transmit */
00226   TxMessage.StdId = 0x321;
00227   TxMessage.ExtId = 0x01;
00228   TxMessage.RTR = CAN_RTR_DATA;
00229   TxMessage.IDE = CAN_ID_STD;
00230   TxMessage.DLC = 1;  
00231 }
00232 
00233 /**
00234   * @brief  Initializes a Rx Message.
00235   * @param  CanRxMsg *RxMessage.
00236   * @retval None
00237   */
00238 void Init_RxMes(CanRxMsg *RxMessage)
00239 {
00240   uint8_t i = 0;
00241 
00242   RxMessage->StdId = 0x00;
00243   RxMessage->ExtId = 0x00;
00244   RxMessage->IDE = CAN_ID_STD;
00245   RxMessage->DLC = 0;
00246   RxMessage->FMI = 0;
00247   for (i = 0; i < 8; i++)
00248   {
00249     RxMessage->Data[i] = 0x00;
00250   }
00251 }
00252 
00253 /**
00254   * @brief  Configures the NVIC for CAN.
00255   * @param  None
00256   * @retval None
00257   */
00258 void NVIC_Config(void)
00259 {
00260   NVIC_InitTypeDef  NVIC_InitStructure;
00261 
00262   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
00263  
00264   NVIC_InitStructure.NVIC_IRQChannel = CAN1_RX0_IRQn;
00265   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0;
00266   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0;
00267   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
00268   NVIC_Init(&NVIC_InitStructure);
00269 
00270   NVIC_InitStructure.NVIC_IRQChannel = CAN2_RX0_IRQn;
00271   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x1;
00272   NVIC_Init(&NVIC_InitStructure);
00273 }
00274 
00275 /**
00276   * @brief  Turn ON/OFF the dedicate led.
00277   * @param  Ledstatus: Communication status. 
00278   * @retval None
00279   */
00280 void LED_Display(uint8_t Ledstatus)
00281 {
00282   switch(Ledstatus)
00283   {
00284     case(1): 
00285       STM_EVAL_LEDOn(LED1);
00286       LCD_DisplayStringLine(LCD_LINE_4, "CAN1 send Msg       ");
00287       LCD_DisplayStringLine(LCD_LINE_5, "                    ");
00288       break;
00289    
00290     case(2): 
00291       STM_EVAL_LEDOn(LED2);
00292       LCD_DisplayStringLine(LCD_LINE_4, "                    ");
00293       LCD_DisplayStringLine(LCD_LINE_5, "CAN2 send Msg       ");
00294       break;
00295             
00296     case(3): 
00297       STM_EVAL_LEDOff(LED1);
00298       STM_EVAL_LEDOn(LED3);
00299       LCD_DisplayStringLine(LCD_LINE_4, "CAN1 receive Passed ");
00300       break;
00301      
00302     case(4): 
00303       STM_EVAL_LEDOff(LED2);
00304       STM_EVAL_LEDOn(LED4);
00305       LCD_DisplayStringLine(LCD_LINE_5, "CAN2 receive Passed ");
00306       break;
00307 
00308     case(5): 
00309       STM_EVAL_LEDOff(LED1);
00310       STM_EVAL_LEDOff(LED3);
00311       LCD_DisplayStringLine(LCD_LINE_6, "Communication Failed ");
00312       break;
00313 
00314     case(6): 
00315       STM_EVAL_LEDOff(LED2);
00316       STM_EVAL_LEDOff(LED4);
00317       LCD_DisplayStringLine(LCD_LINE_6, "Communication Failed ");
00318       break;
00319       
00320     default:
00321       break;
00322   }
00323 }
00324 
00325 /**
00326   * @brief  Delay
00327   * @param  None
00328   * @retval None
00329   */
00330 void Delay(void)
00331 {
00332   uint16_t nTime = 0x0000;
00333 
00334   for(nTime = 0; nTime <0xFFF; nTime++)
00335   {
00336   }
00337 }
00338 
00339 #ifdef  USE_FULL_ASSERT
00340 
00341 /**
00342   * @brief  Reports the name of the source file and the source line number
00343   *         where the assert_param error has occurred.
00344   * @param  file: pointer to the source file name
00345   * @param  line: assert_param error line source number
00346   * @retval None
00347   */
00348 void assert_failed(uint8_t* file, uint32_t line)
00349 { 
00350   /* User can add his own implementation to report the file name and line number,
00351      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00352 
00353   /* Infinite loop */
00354   while (1)
00355   {
00356   }
00357 }
00358 #endif
00359 
00360 /**
00361   * @}
00362   */
00363 
00364 /**
00365   * @}
00366   */
00367 
00368 /******************* (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