STM32F10x_StdPeriph_Examples/CAN/LoopBack/main.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    CAN/LoopBack/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 "stm32_eval.h"
00024 
00025 /** @addtogroup STM32F10x_StdPeriph_Examples
00026   * @{
00027   */
00028 
00029 /** @addtogroup CAN_LoopBack
00030   * @{
00031   */ 
00032 
00033 /* Private typedef -----------------------------------------------------------*/
00034 typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
00035 
00036 /* Private define ------------------------------------------------------------*/
00037 /* Private macro -------------------------------------------------------------*/
00038 /* Private variables ---------------------------------------------------------*/
00039 __IO uint32_t ret = 0; /* for return of the interrupt handling */
00040 volatile TestStatus TestRx;
00041 
00042 /* Private function prototypes -----------------------------------------------*/
00043 void NVIC_Configuration(void);
00044 TestStatus CAN_Polling(void);
00045 TestStatus CAN_Interrupt(void);
00046 
00047 /* Private functions ---------------------------------------------------------*/
00048 
00049 /**
00050   * @brief  Main program.
00051   * @param  None
00052   * @retval None
00053   */
00054 int main(void)
00055 {
00056   /*!< At this stage the microcontroller clock setting is already configured, 
00057        this is done through SystemInit() function which is called from startup
00058        file (startup_stm32f10x_xx.s) before to branch to application main.
00059        To reconfigure the default setting of SystemInit() function, refer to
00060        system_stm32f10x.c file
00061      */     
00062        
00063   /* CAN1 Periph clock enable */
00064   RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
00065 
00066   /* NVIC Configuration */
00067   NVIC_Configuration();
00068   
00069   /* Configures LED 1..4 */
00070   STM_EVAL_LEDInit(LED1);
00071   STM_EVAL_LEDInit(LED2);
00072   STM_EVAL_LEDInit(LED3);
00073   STM_EVAL_LEDInit(LED4);
00074   
00075   /*Turns selected LED Off */  
00076   STM_EVAL_LEDOff(LED1);
00077   STM_EVAL_LEDOff(LED2);
00078   STM_EVAL_LEDOff(LED3);
00079   STM_EVAL_LEDOff(LED4);
00080   
00081   /* CAN transmit at 100Kb/s and receive by polling in loopback mode */
00082   TestRx = CAN_Polling();
00083 
00084   if (TestRx == FAILED)
00085   {
00086     /* Turn on led LD3 */
00087     STM_EVAL_LEDOn(LED3);
00088   }
00089   else
00090   {
00091     /* Turn on led LD1 */
00092     STM_EVAL_LEDOn(LED1);
00093   }
00094 
00095   /* CAN transmit at 500Kb/s and receive by interrupt in loopback mode */
00096   TestRx = CAN_Interrupt();
00097 
00098   if (TestRx == FAILED)
00099   {
00100     /* Turn on led LD4 */
00101     STM_EVAL_LEDOn(LED4);
00102   }
00103   else
00104   {
00105     /* Turn on led LD2 */
00106     STM_EVAL_LEDOn(LED2);
00107   }
00108 
00109   /* Infinite loop */
00110   while (1)
00111   {
00112   }
00113 }
00114 
00115 /**
00116   * @brief  Configures the CAN, transmit and receive by polling
00117   * @param  None
00118   * @retval PASSED if the reception is well done, FAILED in other case
00119   */
00120 TestStatus CAN_Polling(void)
00121 {
00122   CAN_InitTypeDef        CAN_InitStructure;
00123   CAN_FilterInitTypeDef  CAN_FilterInitStructure;
00124   CanTxMsg TxMessage;
00125   CanRxMsg RxMessage;
00126   uint32_t i = 0;
00127   uint8_t TransmitMailbox = 0;
00128 
00129   /* CAN register init */
00130   CAN_DeInit(CAN1);
00131   CAN_StructInit(&CAN_InitStructure);
00132 
00133   /* CAN cell init */
00134   CAN_InitStructure.CAN_TTCM=DISABLE;
00135   CAN_InitStructure.CAN_ABOM=DISABLE;
00136   CAN_InitStructure.CAN_AWUM=DISABLE;
00137   CAN_InitStructure.CAN_NART=DISABLE;
00138   CAN_InitStructure.CAN_RFLM=DISABLE;
00139   CAN_InitStructure.CAN_TXFP=DISABLE;
00140   CAN_InitStructure.CAN_Mode=CAN_Mode_LoopBack;
00141   CAN_InitStructure.CAN_SJW=CAN_SJW_1tq;
00142   CAN_InitStructure.CAN_BS1=CAN_BS1_8tq;
00143   CAN_InitStructure.CAN_BS2=CAN_BS2_7tq;
00144   CAN_InitStructure.CAN_Prescaler=5;
00145   CAN_Init(CAN1, &CAN_InitStructure);
00146 
00147   /* CAN filter init */
00148   CAN_FilterInitStructure.CAN_FilterNumber=0;
00149   CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask;
00150   CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit;
00151   CAN_FilterInitStructure.CAN_FilterIdHigh=0x0000;
00152   CAN_FilterInitStructure.CAN_FilterIdLow=0x0000;
00153   CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0x0000;
00154   CAN_FilterInitStructure.CAN_FilterMaskIdLow=0x0000;
00155   CAN_FilterInitStructure.CAN_FilterFIFOAssignment=0;
00156   CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;
00157   CAN_FilterInit(&CAN_FilterInitStructure);
00158 
00159   /* transmit */
00160   TxMessage.StdId=0x11;
00161   TxMessage.RTR=CAN_RTR_DATA;
00162   TxMessage.IDE=CAN_ID_STD;
00163   TxMessage.DLC=2;
00164   TxMessage.Data[0]=0xCA;
00165   TxMessage.Data[1]=0xFE;
00166 
00167   TransmitMailbox=CAN_Transmit(CAN1, &TxMessage);
00168   i = 0;
00169   while((CAN_TransmitStatus(CAN1, TransmitMailbox) != CANTXOK) && (i != 0xFF))
00170   {
00171     i++;
00172   }
00173 
00174   i = 0;
00175   while((CAN_MessagePending(CAN1, CAN_FIFO0) < 1) && (i != 0xFF))
00176   {
00177     i++;
00178   }
00179 
00180   /* receive */
00181   RxMessage.StdId=0x00;
00182   RxMessage.IDE=CAN_ID_STD;
00183   RxMessage.DLC=0;
00184   RxMessage.Data[0]=0x00;
00185   RxMessage.Data[1]=0x00;
00186   CAN_Receive(CAN1, CAN_FIFO0, &RxMessage);
00187 
00188   if (RxMessage.StdId!=0x11)
00189   {
00190     return FAILED;  
00191   }
00192 
00193   if (RxMessage.IDE!=CAN_ID_STD)
00194   {
00195     return FAILED;
00196   }
00197 
00198   if (RxMessage.DLC!=2)
00199   {
00200     return FAILED;  
00201   }
00202 
00203   if ((RxMessage.Data[0]<<8|RxMessage.Data[1])!=0xCAFE)
00204   {
00205     return FAILED;
00206   }
00207   
00208   return PASSED; /* Test Passed */
00209 }
00210 
00211 /**
00212   * @brief  Configures the CAN, transmit and receive using interrupt.
00213   * @param  None
00214   * @retval PASSED if the reception is well done, FAILED in other case
00215   */
00216 TestStatus CAN_Interrupt(void)
00217 {
00218   CAN_InitTypeDef        CAN_InitStructure;
00219   CAN_FilterInitTypeDef  CAN_FilterInitStructure;
00220   CanTxMsg TxMessage;
00221   uint32_t i = 0;
00222 
00223   /* CAN register init */
00224   CAN_DeInit(CAN1);
00225   CAN_StructInit(&CAN_InitStructure);
00226 
00227   /* CAN cell init */
00228   CAN_InitStructure.CAN_TTCM=DISABLE;
00229   CAN_InitStructure.CAN_ABOM=DISABLE;
00230   CAN_InitStructure.CAN_AWUM=DISABLE;
00231   CAN_InitStructure.CAN_NART=DISABLE;
00232   CAN_InitStructure.CAN_RFLM=DISABLE;
00233   CAN_InitStructure.CAN_TXFP=DISABLE;
00234   CAN_InitStructure.CAN_Mode=CAN_Mode_LoopBack;
00235   CAN_InitStructure.CAN_SJW=CAN_SJW_1tq;
00236   CAN_InitStructure.CAN_BS1=CAN_BS1_8tq;
00237   CAN_InitStructure.CAN_BS2=CAN_BS2_7tq;
00238   CAN_InitStructure.CAN_Prescaler=1;
00239   CAN_Init(CAN1, &CAN_InitStructure);
00240 
00241   /* CAN filter init */
00242   CAN_FilterInitStructure.CAN_FilterNumber=1;
00243   CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask;
00244   CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit;
00245   CAN_FilterInitStructure.CAN_FilterIdHigh=0x0000;
00246   CAN_FilterInitStructure.CAN_FilterIdLow=0x0000;
00247   CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0x0000;
00248   CAN_FilterInitStructure.CAN_FilterMaskIdLow=0x0000;
00249   CAN_FilterInitStructure.CAN_FilterFIFOAssignment=CAN_FIFO0;
00250   CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;
00251   CAN_FilterInit(&CAN_FilterInitStructure);
00252 
00253   /* CAN FIFO0 message pending interrupt enable */ 
00254   CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE);
00255 
00256   /* transmit 1 message */
00257   TxMessage.StdId=0x00;
00258   TxMessage.ExtId=0x1234;
00259   TxMessage.IDE=CAN_ID_EXT;
00260   TxMessage.RTR=CAN_RTR_DATA;
00261   TxMessage.DLC=2;
00262   TxMessage.Data[0]=0xDE;
00263   TxMessage.Data[1]=0xCA;
00264   CAN_Transmit(CAN1, &TxMessage);
00265 
00266   /* initialize the value that will be returned */
00267   ret = 0xFF;
00268        
00269   /* receive message with interrupt handling */
00270   i=0;
00271   while((ret == 0xFF) && (i < 0xFFF))
00272   {
00273     i++;
00274   }
00275   
00276   if (i == 0xFFF)
00277   {
00278     ret=0;  
00279   }
00280 
00281   /* disable interrupt handling */
00282   CAN_ITConfig(CAN1, CAN_IT_FMP0, DISABLE);
00283 
00284   return (TestStatus)ret;
00285 }
00286 
00287 /**
00288   * @brief  Configures the NVIC and Vector Table base address.
00289   * @param  None
00290   * @retval None
00291   */
00292 void NVIC_Configuration(void)
00293 {
00294   NVIC_InitTypeDef NVIC_InitStructure;
00295 
00296   /* Enable CAN1 RX0 interrupt IRQ channel */
00297 #ifndef STM32F10X_CL
00298   NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;
00299 #else
00300  NVIC_InitStructure.NVIC_IRQChannel = CAN1_RX0_IRQn;
00301 #endif /* STM32F10X_CL*/
00302   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
00303   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
00304   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
00305   NVIC_Init(&NVIC_InitStructure);
00306 }
00307 
00308 
00309 #ifdef  USE_FULL_ASSERT
00310 
00311 /**
00312   * @brief  Reports the name of the source file and the source line number
00313   *         where the assert_param error has occurred.
00314   * @param  file: pointer to the source file name
00315   * @param  line: assert_param error line source number
00316   * @retval None
00317   */
00318 void assert_failed(uint8_t* file, uint32_t line)
00319 { 
00320   /* User can add his own implementation to report the file name and line number,
00321      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00322 
00323   /* Infinite loop */
00324   while (1)
00325   {
00326   }
00327 }
00328 #endif
00329 
00330 /**
00331   * @}
00332   */
00333 
00334 /**
00335   * @}
00336   */ 
00337 
00338 /******************* (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