1 | /* Includes */
|
2 | #include "stm32f4xx.h"
|
3 | #include "stm32f4_discovery.h"
|
4 |
|
5 | /* Private macro */
|
6 | /* Private variables */
|
7 | CAN_InitTypeDef CAN_InitStructure;
|
8 | CAN_FilterInitTypeDef CAN_FilterInitStructure;
|
9 | CanTxMsg TxMessage;
|
10 | CanRxMsg RxMessage;
|
11 | /* Private function prototypes */
|
12 | void CAN_Config(void);
|
13 | void NVIC_Config(void);
|
14 | /* Private functions */
|
15 | //Pin definitions
|
16 |
|
17 | #define CAN_RX_PIN GPIO_Pin_0
|
18 | #define CAN_TX_PIN GPIO_Pin_1
|
19 |
|
20 | #define CAN_RX_SOURCE GPIO_PinSource0
|
21 | #define CAN_TX_SOURCE GPIO_PinSource1
|
22 |
|
23 | //Port definitions
|
24 | #define CAN_COM_PORT GPIOD
|
25 |
|
26 | //Clock definitions
|
27 | #define CAN_GPIO_CLK RCC_AHB1Periph_GPIOD
|
28 | #define CAN_CLK RCC_APB1Periph_CAN1
|
29 |
|
30 | void CAN_Config(void)
|
31 | {
|
32 | GPIO_InitTypeDef GPIO_InitStructure;
|
33 |
|
34 | /* CAN GPIOs configuration **************************************************/
|
35 |
|
36 | /* Enable GPIO clock */
|
37 | RCC_AHB1PeriphClockCmd(CAN_GPIO_CLK, ENABLE);
|
38 |
|
39 | /* Connect CAN pins to AF9 */
|
40 | GPIO_PinAFConfig(CAN_COM_PORT, CAN_RX_SOURCE, GPIO_AF_CAN1);
|
41 | GPIO_PinAFConfig(CAN_COM_PORT, CAN_TX_SOURCE, GPIO_AF_CAN1);
|
42 |
|
43 | /* Configure CAN RX and TX pins */
|
44 | GPIO_InitStructure.GPIO_Pin = CAN_RX_PIN | CAN_TX_PIN;
|
45 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
|
46 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
47 | GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
|
48 | GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
|
49 | GPIO_Init(CAN_COM_PORT, &GPIO_InitStructure);
|
50 |
|
51 | /* CAN configuration ********************************************************/
|
52 | /* Enable CAN clock */
|
53 | RCC_APB1PeriphClockCmd(CAN_CLK, ENABLE);
|
54 |
|
55 | //CAN1
|
56 | /* CAN register init */
|
57 | CAN_DeInit(CAN1);
|
58 |
|
59 | /* CAN cell init */
|
60 | CAN_DBGFreeze(CAN1, DISABLE);
|
61 | CAN_InitStructure.CAN_TTCM = DISABLE;
|
62 | CAN_InitStructure.CAN_ABOM = DISABLE;
|
63 | CAN_InitStructure.CAN_AWUM = DISABLE;
|
64 | CAN_InitStructure.CAN_NART = DISABLE;
|
65 | CAN_InitStructure.CAN_RFLM = DISABLE;
|
66 | CAN_InitStructure.CAN_TXFP = DISABLE;
|
67 | CAN_InitStructure.CAN_Mode = CAN_Mode_LoopBack;
|
68 | CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
|
69 |
|
70 | /* CAN Baudrate = 50 kBps (CAN clocked at 42MHz) */
|
71 | CAN_InitStructure.CAN_BS1 = CAN_BS1_14tq;
|
72 | CAN_InitStructure.CAN_BS2 = CAN_BS2_6tq;
|
73 | CAN_InitStructure.CAN_Prescaler = 40;
|
74 | CAN_Init(CAN1, &CAN_InitStructure);
|
75 |
|
76 | /* CAN filter init */
|
77 | CAN_FilterInitStructure.CAN_FilterNumber = 0;
|
78 | CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask;
|
79 | CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit;
|
80 | CAN_FilterInitStructure.CAN_FilterIdHigh = 0x0000;
|
81 | CAN_FilterInitStructure.CAN_FilterIdLow = 0x0000;
|
82 | CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x0000;
|
83 | CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0x0000;
|
84 | CAN_FilterInitStructure.CAN_FilterFIFOAssignment = 0;
|
85 | CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;
|
86 | CAN_FilterInit(&CAN_FilterInitStructure);
|
87 |
|
88 | /* Transmit Structure preparation */
|
89 | TxMessage.StdId = 0x321;
|
90 | TxMessage.ExtId = 0x01;
|
91 | TxMessage.RTR = CAN_RTR_DATA;
|
92 | TxMessage.IDE = CAN_ID_STD;
|
93 | TxMessage.DLC = 1;
|
94 |
|
95 | /* Enable FIFO 0 message pending Interrupt */
|
96 | CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE);
|
97 | }
|
98 |
|
99 | void NVIC_Config(void)
|
100 | {
|
101 | NVIC_InitTypeDef NVIC_InitStructure;
|
102 | NVIC_InitStructure.NVIC_IRQChannel = CAN1_RX0_IRQn;
|
103 | NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0;
|
104 | NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0;
|
105 | NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
106 | NVIC_Init(&NVIC_InitStructure);
|
107 | }
|
108 |
|
109 | void Init_RxMes(CanRxMsg *RxMessage)
|
110 | {
|
111 | uint8_t i = 0;
|
112 |
|
113 | RxMessage->StdId = 0x00;
|
114 | RxMessage->ExtId = 0x00;
|
115 | RxMessage->IDE = CAN_ID_STD;
|
116 | RxMessage->DLC = 0;
|
117 | RxMessage->FMI = 0;
|
118 | for (i = 0;i < 8;i++)
|
119 | {
|
120 | RxMessage->Data[i] = 0x00;
|
121 | }
|
122 | }
|
123 |
|
124 | /**
|
125 | **===========================================================================
|
126 | **
|
127 | ** Abstract: main program
|
128 | **
|
129 | **===========================================================================
|
130 | */
|
131 | int main(void)
|
132 | {
|
133 |
|
134 | /* Initialize LEDs */
|
135 | STM_EVAL_LEDInit(LED3);
|
136 | STM_EVAL_LEDInit(LED4);
|
137 | STM_EVAL_LEDInit(LED5);
|
138 | STM_EVAL_LEDInit(LED6);
|
139 |
|
140 | /* Turn on LEDs */
|
141 | STM_EVAL_LEDOn(LED3);
|
142 | STM_EVAL_LEDOn(LED4);
|
143 | STM_EVAL_LEDOn(LED5);
|
144 | STM_EVAL_LEDOn(LED6);
|
145 |
|
146 | NVIC_Config();
|
147 | CAN_Config();
|
148 | uint8_t test1 = 1;
|
149 | Init_RxMes(&RxMessage);
|
150 | /* Infinite loop */
|
151 | while (1)
|
152 | {
|
153 | TxMessage.Data[0] = test1;
|
154 | CAN_Transmit(CAN1, &TxMessage);
|
155 | }
|
156 | }
|
157 |
|
158 | void CAN1_RX0_IRQHandler(void)
|
159 | {
|
160 |
|
161 | CAN_Receive(CAN1, CAN_FIFO0, &RxMessage);
|
162 | STM_EVAL_LEDToggle(RxMessage.Data[0]);
|
163 |
|
164 |
|
165 | }
|