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