STM32F10x_StdPeriph_Examples/CAN/Networking/main.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    CAN/Networking/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 "platform_config.h"
00024 
00025 /** @addtogroup STM32F10x_StdPeriph_Examples
00026   * @{
00027   */
00028 
00029 /** @addtogroup CAN_Networking
00030   * @{
00031   */ 
00032 
00033 /* Private typedef -----------------------------------------------------------*/
00034 /* Private define ------------------------------------------------------------*/
00035 /* Private define ------------------------------------------------------------*/
00036 #define KEY_PRESSED     0x01
00037 #define KEY_NOT_PRESSED 0x00
00038 
00039 /* Private macro -------------------------------------------------------------*/
00040 /* Private variables ---------------------------------------------------------*/
00041 CAN_InitTypeDef        CAN_InitStructure;
00042 CAN_FilterInitTypeDef  CAN_FilterInitStructure;
00043 CanTxMsg TxMessage;
00044 uint8_t KeyNumber = 0x0;
00045 
00046 /* Private function prototypes -----------------------------------------------*/
00047 void NVIC_Config(void);
00048 void CAN_Config(void);
00049 void LED_Display(uint8_t Ledstatus);
00050 void Init_RxMes(CanRxMsg *RxMessage);
00051 void Delay(void);
00052 
00053 /* Private functions ---------------------------------------------------------*/
00054 
00055 /**
00056   * @brief  Main program.
00057   * @param  None
00058   * @retval None
00059   */
00060 int main(void)
00061 {
00062   /*!< At this stage the microcontroller clock setting is already configured, 
00063        this is done through SystemInit() function which is called from startup
00064        file (startup_stm32f10x_xx.s) before to branch to application main.
00065        To reconfigure the default setting of SystemInit() function, refer to
00066        system_stm32f10x.c file
00067      */     
00068        
00069   /* NVIC configuration */
00070   NVIC_Config();
00071 
00072   /* Configures LED 1..4 */
00073   STM_EVAL_LEDInit(LED1);
00074   STM_EVAL_LEDInit(LED2);
00075   STM_EVAL_LEDInit(LED3);
00076   STM_EVAL_LEDInit(LED4);
00077   
00078   /* Configure Push buttion key */
00079   STM_EVAL_PBInit(BUTTON_KEY, BUTTON_MODE_GPIO); 
00080    
00081   /* CAN configuration */
00082   CAN_Config();
00083   
00084   CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE);
00085 
00086   /* turn off all leds*/
00087   STM_EVAL_LEDOff(LED1);
00088   STM_EVAL_LEDOff(LED2);
00089   STM_EVAL_LEDOff(LED3);
00090   STM_EVAL_LEDOff(LED4);
00091  
00092   /* Infinite loop */
00093   while(1)
00094   {
00095     while(STM_EVAL_PBGetState(BUTTON_KEY) == KEY_PRESSED)
00096     {
00097       if(KeyNumber == 0x4) 
00098       {
00099         KeyNumber = 0x00;
00100       }
00101       else
00102       {
00103         LED_Display(++KeyNumber);
00104         TxMessage.Data[0] = KeyNumber;
00105         CAN_Transmit(CAN1, &TxMessage);
00106         Delay();
00107         
00108         while(STM_EVAL_PBGetState(BUTTON_KEY) != KEY_NOT_PRESSED)
00109         {
00110         }
00111       }
00112     }
00113   }
00114 }
00115 
00116 /**
00117   * @brief  Configures the CAN.
00118   * @param  None
00119   * @retval None
00120   */
00121 void CAN_Config(void)
00122 {
00123   GPIO_InitTypeDef  GPIO_InitStructure;
00124   
00125   /* GPIO clock enable */
00126   RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO |RCC_APB2Periph_GPIO_CAN, ENABLE);
00127   
00128   /* Configure CAN pin: RX */
00129   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_CAN_RX;
00130   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
00131   GPIO_Init(GPIO_CAN, &GPIO_InitStructure);
00132   
00133   /* Configure CAN pin: TX */
00134   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_CAN_TX;
00135   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
00136   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
00137   GPIO_Init(GPIO_CAN, &GPIO_InitStructure);
00138   
00139   GPIO_PinRemapConfig(GPIO_Remap_CAN , ENABLE);
00140   
00141   /* CAN1 Periph clock enable */
00142   RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
00143   
00144   
00145   /* CAN register init */
00146   CAN_DeInit(CAN1);
00147   CAN_StructInit(&CAN_InitStructure);
00148 
00149   /* CAN cell init */
00150   CAN_InitStructure.CAN_TTCM = DISABLE;
00151   CAN_InitStructure.CAN_ABOM = DISABLE;
00152   CAN_InitStructure.CAN_AWUM = DISABLE;
00153   CAN_InitStructure.CAN_NART = DISABLE;
00154   CAN_InitStructure.CAN_RFLM = DISABLE;
00155   CAN_InitStructure.CAN_TXFP = DISABLE;
00156   CAN_InitStructure.CAN_Mode = CAN_Mode_Normal;
00157   CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
00158   CAN_InitStructure.CAN_BS1 = CAN_BS1_3tq;
00159   CAN_InitStructure.CAN_BS2 = CAN_BS2_5tq;
00160   CAN_InitStructure.CAN_Prescaler = 4;
00161   CAN_Init(CAN1, &CAN_InitStructure);
00162 
00163   /* CAN filter init */
00164   CAN_FilterInitStructure.CAN_FilterNumber = 0;
00165   CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask;
00166   CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit;
00167   CAN_FilterInitStructure.CAN_FilterIdHigh = 0x0000;
00168   CAN_FilterInitStructure.CAN_FilterIdLow = 0x0000;
00169   CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x0000;
00170   CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0x0000;
00171   CAN_FilterInitStructure.CAN_FilterFIFOAssignment = 0;
00172   CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;
00173   CAN_FilterInit(&CAN_FilterInitStructure);
00174   
00175   /* Transmit */
00176   TxMessage.StdId = 0x321;
00177   TxMessage.ExtId = 0x01;
00178   TxMessage.RTR = CAN_RTR_DATA;
00179   TxMessage.IDE = CAN_ID_STD;
00180   TxMessage.DLC = 1;
00181 }
00182 
00183 /**
00184   * @brief  Configures the NVIC for CAN.
00185   * @param  None
00186   * @retval None
00187   */
00188 void NVIC_Config(void)
00189 {
00190   NVIC_InitTypeDef  NVIC_InitStructure;
00191 
00192   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
00193   
00194 #ifndef STM32F10X_CL
00195   NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;
00196 #else
00197  NVIC_InitStructure.NVIC_IRQChannel = CAN1_RX0_IRQn;
00198 #endif /* STM32F10X_CL*/
00199   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0;
00200   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0;
00201   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
00202   NVIC_Init(&NVIC_InitStructure);
00203 }
00204 
00205 /**
00206   * @brief  Initializes a Rx Message.
00207   * @param  CanRxMsg *RxMessage
00208   * @retval None
00209   */
00210 void Init_RxMes(CanRxMsg *RxMessage)
00211 {
00212   uint8_t i = 0;
00213 
00214   RxMessage->StdId = 0x00;
00215   RxMessage->ExtId = 0x00;
00216   RxMessage->IDE = CAN_ID_STD;
00217   RxMessage->DLC = 0;
00218   RxMessage->FMI = 0;
00219   for (i = 0;i < 8;i++)
00220     RxMessage->Data[i] = 0x00;
00221 }
00222 
00223 /**
00224   * @brief  Turn ON/OFF the dedicate led
00225   * @param  Ledstatus: Led number from 0 to 3.
00226   * @retval None
00227   */
00228 void LED_Display(uint8_t Ledstatus)
00229 {
00230   /* Turn off all leds */
00231   STM_EVAL_LEDOff(LED1);
00232   STM_EVAL_LEDOff(LED2);
00233   STM_EVAL_LEDOff(LED3);
00234   STM_EVAL_LEDOff(LED4);
00235   
00236   switch(Ledstatus)
00237   {
00238     case(1): 
00239       STM_EVAL_LEDOn(LED1);
00240       break;
00241    
00242     case(2): 
00243       STM_EVAL_LEDOn(LED2);
00244       break;
00245  
00246     case(3): 
00247       STM_EVAL_LEDOn(LED3);
00248       break;
00249 
00250     case(4): 
00251       STM_EVAL_LEDOn(LED4);
00252       break;
00253     default:
00254       break;
00255   }
00256 }
00257 
00258 /**
00259   * @brief  Delay
00260   * @param  None
00261   * @retval None
00262   */
00263 void Delay(void)
00264 {
00265   uint16_t nTime = 0x0000;
00266 
00267   for(nTime = 0; nTime <0xFFF; nTime++)
00268   {
00269   }
00270 }
00271 
00272 #ifdef  USE_FULL_ASSERT
00273 
00274 /**
00275   * @brief  Reports the name of the source file and the source line number
00276   *         where the assert_param error has occurred.
00277   * @param  file: pointer to the source file name
00278   * @param  line: assert_param error line source number
00279   * @retval None
00280   */
00281 void assert_failed(uint8_t* file, uint32_t line)
00282 { 
00283   /* User can add his own implementation to report the file name and line number,
00284      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00285 
00286   /* Infinite loop */
00287   while (1)
00288   {
00289   }
00290 }
00291 #endif
00292 
00293 /**
00294   * @}
00295   */
00296 
00297 /**
00298   * @}
00299   */
00300 
00301 /******************* (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