comparison libs/STM32F10x_StdPeriph_Lib_V3.5.0/Project/STM32F10x_StdPeriph_Examples/CAN/Networking/main.c @ 0:c59513fd84fb

Initial commit of STM32 test code.
author Daniel O'Connor <darius@dons.net.au>
date Mon, 03 Oct 2011 21:19:15 +1030
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:c59513fd84fb
1 /**
2 ******************************************************************************
3 * @file CAN/Networking/main.c
4 * @author MCD Application Team
5 * @version V3.5.0
6 * @date 08-April-2011
7 * @brief Main program body
8 ******************************************************************************
9 * @attention
10 *
11 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
12 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
13 * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
14 * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
15 * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
16 * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
17 *
18 * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
19 ******************************************************************************
20 */
21
22 /* Includes ------------------------------------------------------------------*/
23 #include "stm32f10x.h"
24 #include "platform_config.h"
25
26 /** @addtogroup STM32F10x_StdPeriph_Examples
27 * @{
28 */
29
30 /** @addtogroup CAN_Networking
31 * @{
32 */
33
34
35 /* Private typedef -----------------------------------------------------------*/
36 /* Private define ------------------------------------------------------------*/
37 #define __CAN1_USED__
38 /* #define __CAN2_USED__*/
39
40 #ifdef __CAN1_USED__
41 #define CANx CAN1
42 #define GPIO_CAN GPIO_CAN1
43 #define GPIO_Remapping_CAN GPIO_Remapping_CAN1
44 #define GPIO_CAN GPIO_CAN1
45 #define GPIO_Pin_CAN_RX GPIO_Pin_CAN1_RX
46 #define GPIO_Pin_CAN_TX GPIO_Pin_CAN1_TX
47 #else /*__CAN2_USED__*/
48 #define CANx CAN2
49 #define GPIO_CAN GPIO_CAN2
50 #define GPIO_Remapping_CAN GPIO_Remap_CAN2
51 #define GPIO_CAN GPIO_CAN2
52 #define GPIO_Pin_CAN_RX GPIO_Pin_CAN2_RX
53 #define GPIO_Pin_CAN_TX GPIO_Pin_CAN2_TX
54 #endif /* __CAN1_USED__ */
55
56 #define KEY_PRESSED 0x01
57 #define KEY_NOT_PRESSED 0x00
58
59 /* Private macro -------------------------------------------------------------*/
60 /* Private variables ---------------------------------------------------------*/
61 CAN_InitTypeDef CAN_InitStructure;
62 CAN_FilterInitTypeDef CAN_FilterInitStructure;
63 CanTxMsg TxMessage;
64 uint8_t KeyNumber = 0x0;
65
66 /* Private function prototypes -----------------------------------------------*/
67 void NVIC_Config(void);
68 void CAN_Config(void);
69 void LED_Display(uint8_t Ledstatus);
70 void Init_RxMes(CanRxMsg *RxMessage);
71 void Delay(void);
72
73 /* Private functions ---------------------------------------------------------*/
74
75 /**
76 * @brief Main program.
77 * @param None
78 * @retval None
79 */
80 int main(void)
81 {
82 /*!< At this stage the microcontroller clock setting is already configured,
83 this is done through SystemInit() function which is called from startup
84 file (startup_stm32f10x_xx.s) before to branch to application main.
85 To reconfigure the default setting of SystemInit() function, refer to
86 system_stm32f10x.c file
87 */
88
89 /* NVIC configuration */
90 NVIC_Config();
91
92 /* Configures LED 1..4 */
93 STM_EVAL_LEDInit(LED1);
94 STM_EVAL_LEDInit(LED2);
95 STM_EVAL_LEDInit(LED3);
96 STM_EVAL_LEDInit(LED4);
97
98 /* Configure Push button key */
99 STM_EVAL_PBInit(BUTTON_KEY, BUTTON_MODE_GPIO);
100
101 /* CAN configuration */
102 CAN_Config();
103
104 CAN_ITConfig(CANx, CAN_IT_FMP0, ENABLE);
105
106 /* turn off all leds*/
107 STM_EVAL_LEDOff(LED1);
108 STM_EVAL_LEDOff(LED2);
109 STM_EVAL_LEDOff(LED3);
110 STM_EVAL_LEDOff(LED4);
111
112 /* Infinite loop */
113 while(1)
114 {
115 while(STM_EVAL_PBGetState(BUTTON_KEY) == KEY_PRESSED)
116 {
117 if(KeyNumber == 0x4)
118 {
119 KeyNumber = 0x00;
120 }
121 else
122 {
123 LED_Display(++KeyNumber);
124 TxMessage.Data[0] = KeyNumber;
125 CAN_Transmit(CANx, &TxMessage);
126 Delay();
127
128 while(STM_EVAL_PBGetState(BUTTON_KEY) != KEY_NOT_PRESSED)
129 {
130 }
131 }
132 }
133 }
134 }
135
136 /**
137 * @brief Configures the CAN.
138 * @param None
139 * @retval None
140 */
141 void CAN_Config(void)
142 {
143 GPIO_InitTypeDef GPIO_InitStructure;
144
145 /* GPIO clock enable */
146 RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
147 #ifdef __CAN1_USED__
148 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIO_CAN1, ENABLE);
149 #else /*__CAN2_USED__*/
150 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIO_CAN1, ENABLE);
151 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIO_CAN2, ENABLE);
152 #endif /* __CAN1_USED__ */
153 /* Configure CAN pin: RX */
154 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_CAN_RX;
155 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
156 GPIO_Init(GPIO_CAN, &GPIO_InitStructure);
157
158 /* Configure CAN pin: TX */
159 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_CAN_TX;
160 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
161 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
162 GPIO_Init(GPIO_CAN, &GPIO_InitStructure);
163
164 GPIO_PinRemapConfig(GPIO_Remapping_CAN , ENABLE);
165
166 /* CANx Periph clock enable */
167 #ifdef __CAN1_USED__
168 RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
169 #else /*__CAN2_USED__*/
170 RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
171 RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN2, ENABLE);
172 #endif /* __CAN1_USED__ */
173
174
175 /* CAN register init */
176 CAN_DeInit(CANx);
177 CAN_StructInit(&CAN_InitStructure);
178
179 /* CAN cell init */
180 CAN_InitStructure.CAN_TTCM = DISABLE;
181 CAN_InitStructure.CAN_ABOM = DISABLE;
182 CAN_InitStructure.CAN_AWUM = DISABLE;
183 CAN_InitStructure.CAN_NART = DISABLE;
184 CAN_InitStructure.CAN_RFLM = DISABLE;
185 CAN_InitStructure.CAN_TXFP = DISABLE;
186 CAN_InitStructure.CAN_Mode = CAN_Mode_Normal;
187
188 /* CAN Baudrate = 1MBps*/
189 CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
190 CAN_InitStructure.CAN_BS1 = CAN_BS1_3tq;
191 CAN_InitStructure.CAN_BS2 = CAN_BS2_5tq;
192 CAN_InitStructure.CAN_Prescaler = 4;
193 CAN_Init(CANx, &CAN_InitStructure);
194
195 /* CAN filter init */
196 #ifdef __CAN1_USED__
197 CAN_FilterInitStructure.CAN_FilterNumber = 0;
198 #else /*__CAN2_USED__*/
199 CAN_FilterInitStructure.CAN_FilterNumber = 14;
200 #endif /* __CAN1_USED__ */
201 CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask;
202 CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit;
203 CAN_FilterInitStructure.CAN_FilterIdHigh = 0x0000;
204 CAN_FilterInitStructure.CAN_FilterIdLow = 0x0000;
205 CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x0000;
206 CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0x0000;
207 CAN_FilterInitStructure.CAN_FilterFIFOAssignment = 0;
208 CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;
209 CAN_FilterInit(&CAN_FilterInitStructure);
210
211 /* Transmit */
212 TxMessage.StdId = 0x321;
213 TxMessage.ExtId = 0x01;
214 TxMessage.RTR = CAN_RTR_DATA;
215 TxMessage.IDE = CAN_ID_STD;
216 TxMessage.DLC = 1;
217 }
218
219 /**
220 * @brief Configures the NVIC for CAN.
221 * @param None
222 * @retval None
223 */
224 void NVIC_Config(void)
225 {
226 NVIC_InitTypeDef NVIC_InitStructure;
227
228 NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
229
230 #ifndef STM32F10X_CL
231 #ifdef __CAN1_USED__
232 NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;
233 #else /*__CAN2_USED__*/
234 /* CAN2 is not implemented in the device */
235 #error "CAN2 is implemented only in Connectivity line devices"
236 #endif /*__CAN1_USED__*/
237 #else
238 #ifdef __CAN1_USED__
239 NVIC_InitStructure.NVIC_IRQChannel = CAN1_RX0_IRQn;
240 #else /*__CAN2_USED__*/
241 NVIC_InitStructure.NVIC_IRQChannel = CAN2_RX0_IRQn;
242 #endif /*__CAN1_USED__*/
243
244 #endif /* STM32F10X_CL*/
245 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0;
246 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0;
247 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
248 NVIC_Init(&NVIC_InitStructure);
249 }
250
251 /**
252 * @brief Initializes a Rx Message.
253 * @param CanRxMsg *RxMessage
254 * @retval None
255 */
256 void Init_RxMes(CanRxMsg *RxMessage)
257 {
258 uint8_t i = 0;
259
260 RxMessage->StdId = 0x00;
261 RxMessage->ExtId = 0x00;
262 RxMessage->IDE = CAN_ID_STD;
263 RxMessage->DLC = 0;
264 RxMessage->FMI = 0;
265 for (i = 0;i < 8;i++)
266 {
267 RxMessage->Data[i] = 0x00;
268 }
269 }
270
271 /**
272 * @brief Turn ON/OFF the dedicate led
273 * @param Ledstatus: Led number from 0 to 3.
274 * @retval None
275 */
276 void LED_Display(uint8_t Ledstatus)
277 {
278 /* Turn off all leds */
279 STM_EVAL_LEDOff(LED1);
280 STM_EVAL_LEDOff(LED2);
281 STM_EVAL_LEDOff(LED3);
282 STM_EVAL_LEDOff(LED4);
283
284 switch(Ledstatus)
285 {
286 case(1):
287 STM_EVAL_LEDOn(LED1);
288 break;
289
290 case(2):
291 STM_EVAL_LEDOn(LED2);
292 break;
293
294 case(3):
295 STM_EVAL_LEDOn(LED3);
296 break;
297
298 case(4):
299 STM_EVAL_LEDOn(LED4);
300 break;
301 default:
302 break;
303 }
304 }
305
306 /**
307 * @brief Delay
308 * @param None
309 * @retval None
310 */
311 void Delay(void)
312 {
313 uint16_t nTime = 0x0000;
314
315 for(nTime = 0; nTime <0xFFF; nTime++)
316 {
317 }
318 }
319
320 #ifdef USE_FULL_ASSERT
321
322 /**
323 * @brief Reports the name of the source file and the source line number
324 * where the assert_param error has occurred.
325 * @param file: pointer to the source file name
326 * @param line: assert_param error line source number
327 * @retval None
328 */
329 void assert_failed(uint8_t* file, uint32_t line)
330 {
331 /* User can add his own implementation to report the file name and line number,
332 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
333
334 /* Infinite loop */
335 while (1)
336 {
337 }
338 }
339
340 #endif
341
342 /**
343 * @}
344 */
345
346 /**
347 * @}
348 */
349
350 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/