1 | //CAN1
|
2 | #define GPIO_CAN1 GPIOD
|
3 | #define GPIO_Pin_CAN1_RX GPIO_Pin_0
|
4 | #define GPIO_Pin_CAN1_TX GPIO_Pin_1
|
5 |
|
6 | //CAN2
|
7 | #define GPIO_CAN2 GPIOB
|
8 | #define GPIO_Pin_CAN2_RX GPIO_Pin_12
|
9 | #define GPIO_Pin_CAN2_TX GPIO_Pin_6
|
10 |
|
11 |
|
12 | void CAN_init(void)
|
13 | {
|
14 | //Periph-Clock an
|
15 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1 | RCC_APB1Periph_CAN2, ENABLE);
|
16 | RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOD, ENABLE);
|
17 |
|
18 | //CAN1
|
19 | GPIO_StructInit(&GPIO_InitStructure);
|
20 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_CAN1_RX;
|
21 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
|
22 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
23 | GPIO_Init(GPIO_CAN1, &GPIO_InitStructure);
|
24 |
|
25 | GPIO_StructInit(&GPIO_InitStructure);
|
26 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_CAN1_TX;
|
27 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
|
28 | GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
|
29 | GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
|
30 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
31 | GPIO_Init(GPIO_CAN1, &GPIO_InitStructure);
|
32 |
|
33 | GPIO_PinAFConfig(GPIO_CAN1, GPIO_PinSource0, GPIO_AF_CAN1);
|
34 | GPIO_PinAFConfig(GPIO_CAN1, GPIO_PinSource1, GPIO_AF_CAN1);
|
35 |
|
36 | //CAN2
|
37 | GPIO_StructInit(&GPIO_InitStructure);
|
38 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_CAN2_RX;
|
39 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
|
40 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
41 | GPIO_Init(GPIO_CAN2, &GPIO_InitStructure);
|
42 |
|
43 | GPIO_StructInit(&GPIO_InitStructure);
|
44 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_CAN2_TX;
|
45 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
|
46 | GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
|
47 | GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
|
48 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
49 | GPIO_Init(GPIO_CAN2, &GPIO_InitStructure);
|
50 |
|
51 | GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_CAN2);
|
52 | GPIO_PinAFConfig(GPIOB, GPIO_PinSource12, GPIO_AF_CAN2);
|
53 | //
|
54 | //GPIOB->AFR[0]|=(0x09<<24);
|
55 | //GPIOB->AFR[1]|=(0x09<<16);
|
56 |
|
57 | CAN_DeInit(CAN1);
|
58 | CAN_DeInit(CAN2);
|
59 |
|
60 |
|
61 | //Merde: überprüfen für 42Mhz Clock APB1
|
62 | CAN_StructInit(&CAN_InitStructure);
|
63 | CAN_InitStructure.CAN_Prescaler = 4; //!< Specifies the length of a time quantum. It ranges from 1 to 1024.
|
64 | CAN_InitStructure.CAN_SJW = CAN_SJW_2tq; //!< Specifies the maximum number of time quanta the CAN hardware is allowed to lengthen or shorten a bit to perform resynchronization.
|
65 | CAN_InitStructure.CAN_BS1 = CAN_BS1_13tq; //!< Specifies the number of time quanta in Bit Segment 1.
|
66 | CAN_InitStructure.CAN_BS2 = CAN_BS2_7tq; //!< Specifies the number of time quanta in Bit Segment 2.
|
67 | // tq=4/42Mhz 1Bit=SJW+BS1+BS2 bei 500kbaud= 2µSek
|
68 |
|
69 | CAN_InitStructure.CAN_Mode = CAN_Mode_Normal; //!< Specifies the CAN operating mode.
|
70 | CAN_InitStructure.CAN_TTCM = DISABLE; //!< Enable or disable the time triggered communication mode.
|
71 | CAN_InitStructure.CAN_ABOM = DISABLE; //!< Enable or disable the automatic bus-off management.
|
72 | CAN_InitStructure.CAN_AWUM = DISABLE; //!< Enable or disable the automatic wake-up mode.
|
73 | CAN_InitStructure.CAN_NART = ENABLE; //!< Enable or disable the non-automatic retransmission mode.
|
74 | CAN_InitStructure.CAN_RFLM = DISABLE; //!< Enable or disable the Receive FIFO Locked mode.
|
75 | CAN_InitStructure.CAN_TXFP = DISABLE; //!< Enable or disable the transmit FIFO priority.
|
76 |
|
77 | CAN_Init(CAN1, &CAN_InitStructure);
|
78 | CAN_Init(CAN2, &CAN_InitStructure);
|
79 |
|
80 |
|
81 | // Can filter Init
|
82 | CAN_FilterInit(&CAN_FilterInitStructure);
|
83 | CAN_FilterInitStructure.CAN_FilterNumber = 0;
|
84 | CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask;
|
85 | CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit;
|
86 | CAN_FilterInitStructure.CAN_FilterIdHigh = 0x0112<<5;
|
87 | CAN_FilterInitStructure.CAN_FilterIdLow = 0x0000;
|
88 | CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0xFFFF;
|
89 | CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0xFFFF;
|
90 | CAN_FilterInitStructure.CAN_FilterFIFOAssignment = 0;
|
91 | CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;
|
92 | CAN_FilterInit(&CAN_FilterInitStructure);
|
93 | }
|