1 | #define CAN1_TX GPIO_Pin_1
|
2 | #define CAN1_RX GPIO_Pin_0
|
3 | #define CAN_PORT GPIOD
|
4 | #define CAN_GPIO_CLK RCC_AHB1Periph_GPIOD
|
5 | #define CAN_TX_SOURCE GPIO_PinSource1
|
6 | #define CAN_RX_SOURCE GPIO_PinSource0
|
7 |
|
8 |
|
9 |
|
10 | void init_can(void)
|
11 | {
|
12 | CAN_InitTypeDef CAN_InitStruct;
|
13 | GPIO_InitTypeDef GPIO_InitStructure;
|
14 | uint8_t propbe;
|
15 |
|
16 | /* CAN GPIOs configuration **************************************************/
|
17 |
|
18 | /* Enable GPIO clock */
|
19 | RCC_AHB1PeriphClockCmd(CAN_GPIO_CLK, ENABLE);
|
20 |
|
21 | /* Connect CAN pins to AF9 */
|
22 | GPIO_PinAFConfig(CAN_PORT, CAN_RX_SOURCE, GPIO_AF_CAN1);
|
23 | GPIO_PinAFConfig(CAN_PORT, CAN_TX_SOURCE, GPIO_AF_CAN1);
|
24 |
|
25 | /* Configure CAN RX and TX pins */
|
26 | GPIO_InitStructure.GPIO_Pin = CAN1_TX | CAN1_RX;
|
27 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
|
28 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
29 | GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
|
30 | GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
|
31 | GPIO_Init(CAN_PORT, &GPIO_InitStructure);
|
32 |
|
33 | /* CAN configuration ********************************************************/
|
34 | /* Enable CAN clock */
|
35 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
|
36 |
|
37 | CAN_DeInit(CAN1);
|
38 | /* Initialize the time triggered communication mode */
|
39 | CAN_InitStruct.CAN_TTCM = DISABLE;
|
40 |
|
41 | /* Initialize the automatic bus-off management */
|
42 | CAN_InitStruct.CAN_ABOM = DISABLE;
|
43 |
|
44 | /* Initialize the automatic wake-up mode */
|
45 | CAN_InitStruct.CAN_AWUM = ENABLE;
|
46 |
|
47 | /* Initialize the no automatic retransmission */
|
48 | CAN_InitStruct.CAN_NART = DISABLE;
|
49 |
|
50 | /* Initialize the receive FIFO locked mode */
|
51 | CAN_InitStruct.CAN_RFLM = DISABLE;
|
52 |
|
53 | /* Initialize the transmit FIFO priority */
|
54 | CAN_InitStruct.CAN_TXFP = DISABLE;
|
55 |
|
56 | /* Initialize the CAN_Mode member */
|
57 | CAN_InitStruct.CAN_Mode = CAN_Mode_Normal;
|
58 |
|
59 | /* Initialize the CAN_SJW member */
|
60 | CAN_InitStruct.CAN_SJW = CAN_SJW_1tq;
|
61 |
|
62 | /* Initialize the CAN_BS1 member */
|
63 | CAN_InitStruct.CAN_BS1 = CAN_BS1_14tq;
|
64 |
|
65 | /* Initialize the CAN_BS2 member */
|
66 | CAN_InitStruct.CAN_BS2 = CAN_BS2_6tq;
|
67 |
|
68 | /* Initialize the CAN_Prescaler member */
|
69 | //CAN_InitStruct.CAN_Prescaler=1; // 2000 kbit/s
|
70 | CAN_InitStruct.CAN_Prescaler = 2; // 1000 kbit/s
|
71 | //CAN_InitStruct.CAN_Prescaler=4; // 500 kbit/s
|
72 | //CAN_InitStruct.CAN_Prescaler=5; // 400 kbit/s
|
73 | //CAN_InitStruct.CAN_Prescaler=8; // 250 kbit/s
|
74 | //CAN_InitStruct.CAN_Prescaler=10; // 200 kbit/s
|
75 | //CAN_InitStruct.CAN_Prescaler=16; // 125 kbit/s
|
76 | //CAN_InitStruct.CAN_Prescaler=20; // 100 kbit/s
|
77 | //CAN_InitStruct.CAN_Prescaler=40; // 50 kbit/s
|
78 | //CAN_InitStruct.CAN_Prescaler=80; // 40 kbit/s
|
79 | //CAN_InitStruct.CAN_Prescaler=200; // 10 kbit/s
|
80 | propbe = CAN_Init(CAN1, &CAN_InitStruct);
|
81 |
|
82 | can_filter_conf();
|
83 |
|
84 | }
|