STM32F0xx Standard Peripherals Firmware Library: main.c Source File

STM32F0xx Standard Peripherals Library

STM32F0xx_StdPeriph_Examples/CAN/CAN_FIFOExtension/main.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    CAN/CAN_FIFOExtension/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_FIFOExtension
00037   * @{
00038   */
00039 
00040 /* Private typedef -----------------------------------------------------------*/
00041 /* Private define ------------------------------------------------------------*/
00042 #define TAMPER_PRESSED        0x00
00043 #define TAMPER_NOT_PRESSED    0x01
00044 
00045 #define MESSAGE1   "   STM32072B-EVAL   "
00046 #define MESSAGE2   "Press TAMP to Send message  "
00047 #define MESSAGE3   "  Send message to STM32     "
00048 #define MESSAGE4   "Receive message From STM32  "
00049 
00050 
00051 /* Private macro -------------------------------------------------------------*/
00052 /* Private variables ---------------------------------------------------------*/
00053 CanTxMsg TxMessage = {0};
00054 CanRxMsg RxMessage = {0};
00055 uint8_t KeyNumber = 0x0;
00056 extern __IO uint8_t MsgReceived;
00057 CAN_FilterInitTypeDef  CAN_FilterInitStructure;
00058 /* Private function prototypes -----------------------------------------------*/
00059 static void CAN_Config(void);
00060 static void Display_TransmittedMsg(uint8_t PushNumber);
00061 
00062 void Delay(void);
00063 /* Private functions ---------------------------------------------------------*/
00064 
00065 /**
00066   * @brief  Main program.
00067   * @param  None
00068   * @retval None
00069   */
00070 int main(void)
00071 {
00072   /*!< At this stage the microcontroller clock setting is already configured, 
00073        this is done through SystemInit() function which is called from startup
00074        file (startup_stm32f0xx.s) before to branch to application main.
00075        To reconfigure the default setting of SystemInit() function, refer to
00076        system_stm32f0xx.c file
00077      */
00078   
00079   /* Initialize the LCD */
00080   STM32072B_LCD_Init();      
00081   
00082   /* Clear the LCD */
00083   LCD_Clear(LCD_COLOR_WHITE);
00084   
00085   /* Set the LCD Back Color */
00086   LCD_SetBackColor(Blue);
00087   
00088   /* Set the LCD Text Color */
00089   LCD_SetTextColor(White);
00090   
00091   /* Displays MESSAGE1 on line 0 */
00092   LCD_DisplayStringLine(LINE(0), (uint8_t *)MESSAGE1);
00093     
00094   LCD_SetFont(&Font12x12);
00095   
00096   /* Display Messages on the the LCD */  
00097   LCD_DisplayStringLine(LINE(0x3), (uint8_t *)MESSAGE2);
00098   
00099   /* Set the LCD Text size */
00100   LCD_SetFont(&Font16x24);
00101   
00102   /* Configure Push button key */
00103   STM_EVAL_PBInit(BUTTON_TAMPER, BUTTON_MODE_GPIO); 
00104      
00105   /* CAN configuration */
00106   CAN_Config();
00107   
00108   /* Infinite loop */
00109   while(1)
00110   {
00111     while(STM_EVAL_PBGetState(BUTTON_TAMPER) == TAMPER_PRESSED)
00112     { 
00113       if(KeyNumber == 41) KeyNumber = 0;
00114       
00115       Display_TransmittedMsg(KeyNumber);
00116       
00117       KeyNumber++;
00118       
00119       Delay();
00120       
00121       while(STM_EVAL_PBGetState(BUTTON_TAMPER) != TAMPER_NOT_PRESSED)
00122       {
00123       }
00124     }
00125       if (MsgReceived != 0)
00126         {
00127             /* Display received the 6 messages on tghe LCD */
00128             Display_ReceivedMsg();
00129             MsgReceived = 0;
00130      
00131         }
00132   }
00133 }
00134 
00135 /**
00136   * @brief  Configures the CAN.
00137   * @param  None
00138   * @retval None
00139   */
00140 static void CAN_Config(void)
00141 {
00142   GPIO_InitTypeDef  GPIO_InitStructure;
00143   NVIC_InitTypeDef  NVIC_InitStructure;
00144   CAN_InitTypeDef   CAN_InitStructure;
00145   
00146   /* CAN GPIOs configuration **************************************************/
00147 
00148   /* Enable GPIO clock */
00149   RCC_AHBPeriphClockCmd(CAN_GPIO_CLK, ENABLE);
00150 
00151   /* Connect CAN pins to AF7 */
00152   GPIO_PinAFConfig(CAN_GPIO_PORT, CAN_RX_SOURCE, CAN_AF_PORT);
00153   GPIO_PinAFConfig(CAN_GPIO_PORT, CAN_TX_SOURCE, CAN_AF_PORT); 
00154   
00155   /* Configure CAN RX and TX pins */
00156   GPIO_InitStructure.GPIO_Pin = CAN_RX_PIN | CAN_TX_PIN;
00157   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
00158   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
00159   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
00160   GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;
00161   GPIO_Init(CAN_GPIO_PORT, &GPIO_InitStructure);
00162 
00163   /* CAN NVIC  configuration **************************************************/  
00164   NVIC_InitStructure.NVIC_IRQChannel = CEC_CAN_IRQn;
00165   NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
00166   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
00167   NVIC_Init(&NVIC_InitStructure);
00168   
00169   /* CAN configuration ********************************************************/  
00170   /* Enable CAN clock */
00171   RCC_APB1PeriphClockCmd(CAN_CLK, ENABLE);
00172   
00173   /* CAN register init */
00174   CAN_DeInit(CANx);
00175   CAN_StructInit(&CAN_InitStructure);
00176 
00177   /* CAN cell init */
00178   CAN_InitStructure.CAN_TTCM = DISABLE;
00179   CAN_InitStructure.CAN_ABOM = DISABLE;
00180   CAN_InitStructure.CAN_AWUM = DISABLE;
00181   CAN_InitStructure.CAN_NART = DISABLE;
00182   CAN_InitStructure.CAN_RFLM = ENABLE;
00183   CAN_InitStructure.CAN_TXFP = ENABLE;
00184   CAN_InitStructure.CAN_Mode = CAN_Mode_Normal;
00185   CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
00186     
00187   /* CAN Baudrate = 1MBps (CAN clocked at 36 MHz) */
00188   CAN_InitStructure.CAN_BS1 = CAN_BS1_9tq;
00189   CAN_InitStructure.CAN_BS2 = CAN_BS2_8tq;
00190   CAN_InitStructure.CAN_Prescaler = 2;
00191   CAN_Init(CANx, &CAN_InitStructure);
00192 
00193   /* CAN filter init "FIFO0" */
00194   CAN_FilterInitStructure.CAN_FilterNumber = 0;
00195   CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdList;
00196   CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit;
00197   CAN_FilterInitStructure.CAN_FilterIdHigh = 0x6420;
00198   CAN_FilterInitStructure.CAN_FilterIdLow = 0x2461;
00199   CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x0000;
00200   CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0x0000;
00201   CAN_FilterInitStructure.CAN_FilterFIFOAssignment = 0;
00202   CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;
00203   CAN_FilterInit(&CAN_FilterInitStructure);
00204   
00205   /* Transmit Structure preparation */
00206   TxMessage.StdId = 0x321;
00207   TxMessage.ExtId = 0x00;
00208   TxMessage.RTR = CAN_RTR_DATA;
00209   TxMessage.IDE = CAN_ID_STD;
00210   TxMessage.DLC = 1;
00211     
00212   /* Enable FIFO 0 full Interrupt */
00213   CAN_ITConfig(CANx, CAN_IT_FF0, ENABLE);
00214   
00215   /* Enable FIFO 1 full Interrupt */
00216   CAN_ITConfig(CANx, CAN_IT_FF1, ENABLE);
00217 }
00218 
00219 
00220 /**
00221   * @brief  Display transmitted msg on the LCD 
00222   * @param  None.
00223   * @retval None
00224   */
00225 static void Display_TransmittedMsg(uint8_t PushNumber)
00226 {
00227   uint8_t text[50] = {0}, dataindex =0;
00228   
00229   uint8_t TxMessages[6] = { 0, 1, 2, 3, 4, 5 };
00230   
00231   /* Set the LCD Back Color and Text Color*/
00232   LCD_SetBackColor(Blue);
00233   LCD_SetTextColor(White);
00234   
00235    /* Set the LCD Text size */
00236   LCD_SetFont(&Font12x12);
00237   
00238   /* Display Messages on the the LCD */  
00239   LCD_DisplayStringLine(LINE(5), (uint8_t *)MESSAGE3);
00240   
00241   
00242   for(dataindex = 0 ; dataindex < 6 ;dataindex++)
00243   {
00244     
00245     TxMessage.Data[0] = (uint8_t)(TxMessages[dataindex] + (6 * PushNumber));
00246     
00247     CAN_Transmit(CANx, &TxMessage);
00248     
00249     sprintf((char*)text,"Send Message Num%d    = %d            ",dataindex+1,TxMessage.Data[0] );
00250     
00251     LCD_SetBackColor(Cyan);
00252     LCD_SetTextColor(Black);
00253     
00254     LCD_DisplayStringLine(LINE(6+dataindex),text);
00255   }
00256   
00257 }
00258 
00259 /**
00260   * @brief  Display received msg on the LCD 
00261   * @param  None.
00262   * @retval None
00263   */
00264 void Display_ReceivedMsg(void)
00265 {
00266   uint8_t text[50] = {0}, dataindex =0;
00267   
00268   /* Set the LCD Back Color and Text Color*/
00269   LCD_SetBackColor(Blue);
00270   LCD_SetTextColor(White);
00271   
00272    /* Set the LCD Text size */
00273   LCD_SetFont(&Font12x12);
00274   
00275   
00276   /* Display Messages on the the LCD */  
00277   LCD_DisplayStringLine(LINE(13), (uint8_t *)MESSAGE4);
00278 
00279   
00280   for(dataindex = 0 ; dataindex < 6 ;dataindex++)
00281   {
00282     if (dataindex < 3)
00283     {  
00284       /* read data from CAN1 FIFO 0 */
00285       CAN_Receive(CANx, CAN_FIFO0, &RxMessage);
00286     }
00287     else
00288     {
00289       /* read data from CAN1 FIFO 1 */
00290       CAN_Receive(CANx, CAN_FIFO1, &RxMessage);
00291     }
00292     
00293     LCD_SetBackColor(Green);
00294     LCD_SetTextColor(Black);
00295     sprintf((char*)text,"Message received Num%d= %d   ",dataindex +1,RxMessage.Data[0]);
00296     
00297     /* Display message on the LCD*/
00298     LCD_DisplayStringLine(LINE(14+dataindex),text);
00299   }
00300 }
00301 
00302 /**
00303   * @brief  Delay
00304   * @param  None
00305   * @retval None
00306   */
00307 void Delay(void)
00308 {
00309   uint32_t nTime = 0x0000;
00310 
00311   for(nTime = 0; nTime <0xFFFFF; nTime++)
00312   {
00313   }
00314 }
00315 
00316 #ifdef  USE_FULL_ASSERT
00317 
00318 /**
00319   * @brief  Reports the name of the source file and the source line number
00320   *         where the assert_param error has occurred.
00321   * @param  file: pointer to the source file name
00322   * @param  line: assert_param error line source number
00323   * @retval None
00324   */
00325 void assert_failed(uint8_t* file, uint32_t line)
00326 { 
00327   /* User can add his own implementation to report the file name and line number,
00328      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00329 
00330   /* Infinite loop */
00331   while (1)
00332   {
00333   }
00334 }
00335 #endif
00336 
00337 /**
00338   * @}
00339   */
00340 
00341 /**
00342   * @}
00343   */
00344 
00345 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
STM32L1xx Standard Peripherals Library: Footer

 

 

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