1 | void CANInit(char bRefresh)
|
2 | {
|
3 | if (!bRefresh)
|
4 | {
|
5 | CAN_DeInit();
|
6 | }
|
7 |
|
8 | RCC_AHBPeriphClockCmd(RCC_APB1Periph_CAN, ENABLE);
|
9 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_AFIO, ENABLE);
|
10 |
|
11 | // GPIO_PinRemapConfig(GPIO_Remap_PD01, ENABLE);
|
12 | GPIO_PinRemapConfig(GPIO_Remap2_CAN, ENABLE);
|
13 |
|
14 | GPIO_InitTypeDef GPIO_InitStructure;
|
15 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
|
16 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
|
17 | GPIO_Init(GPIOD, &GPIO_InitStructure);
|
18 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
|
19 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
20 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
21 | GPIO_Init(GPIOD, &GPIO_InitStructure);
|
22 |
|
23 | CAN_InitTypeDef CAN_InitStructure;
|
24 | CAN_FilterInitTypeDef CAN_FilterInitStructure;
|
25 |
|
26 | /* CAN register init */
|
27 | CAN_StructInit(&CAN_InitStructure);
|
28 |
|
29 | /* CAN cell init */
|
30 | CAN_InitStructure.CAN_TTCM = DISABLE;
|
31 | CAN_InitStructure.CAN_ABOM = DISABLE;
|
32 | CAN_InitStructure.CAN_AWUM = DISABLE;
|
33 | CAN_InitStructure.CAN_NART = DISABLE;
|
34 | CAN_InitStructure.CAN_RFLM = DISABLE;
|
35 | CAN_InitStructure.CAN_TXFP = DISABLE;
|
36 | CAN_InitStructure.CAN_Mode = CAN_Mode_Normal; // CAN_Mode_LoopBack CAN_Mode_Normal
|
37 | CAN_InitStructure.CAN_SJW = CAN_SJW_1tq; // 10 Teileinheiten = 20KBaud @200KHz
|
38 | CAN_InitStructure.CAN_BS1 = CAN_BS1_5tq;
|
39 | CAN_InitStructure.CAN_BS2 = CAN_BS2_4tq;
|
40 | CAN_InitStructure.CAN_Prescaler = 40; // Einstellung 8MHz/40 >> 200KHz
|
41 | CAN_Init(&CAN_InitStructure);
|
42 |
|
43 | /* CAN filter init */
|
44 | CAN_FilterInitStructure.CAN_FilterNumber=1;
|
45 | CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask;
|
46 | CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit;
|
47 | CAN_FilterInitStructure.CAN_FilterIdHigh=0x0000;
|
48 | CAN_FilterInitStructure.CAN_FilterIdLow=0x0000;
|
49 | CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0x0000;
|
50 | CAN_FilterInitStructure.CAN_FilterMaskIdLow=0x0000;
|
51 | CAN_FilterInitStructure.CAN_FilterFIFOAssignment=CAN_FIFO0;
|
52 | CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;
|
53 | CAN_FilterInit(&CAN_FilterInitStructure);
|
54 |
|
55 | /* Enable CAN RX0 interrupt IRQ channel */
|
56 | NVIC_InitTypeDef NVIC_InitSt;
|
57 | NVIC_InitSt.NVIC_IRQChannel = USB_LP_CAN_RX0_IRQChannel;
|
58 | NVIC_InitSt.NVIC_IRQChannelPreemptionPriority = 0;
|
59 | NVIC_InitSt.NVIC_IRQChannelSubPriority = 0;
|
60 | NVIC_InitSt.NVIC_IRQChannelCmd = ENABLE;
|
61 | NVIC_Init(&NVIC_InitSt);
|
62 |
|
63 | /* CAN FIFO0 message pending interrupt enable */
|
64 | CAN_ITConfig(CAN_IT_FMP0, ENABLE);
|
65 |
|
66 | // Test
|
67 | CanTxMsg msg;
|
68 | msg.ExtId = 0x12345;
|
69 | msg.IDE = CAN_ID_EXT;
|
70 | msg.RTR = CAN_RTR_DATA;
|
71 | msg.DLC = 4;
|
72 | msg.Data[0] = 1;
|
73 | msg.Data[2] = 5;
|
74 | msg.Data[3] = 8;
|
75 | msg.Data[4] = 0;
|
76 | CAN_Transmit(&msg);
|
77 | }
|