-
Notifications
You must be signed in to change notification settings - Fork 6
/
CANbr.h
114 lines (104 loc) · 2.27 KB
/
CANbr.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <arduino.h>
/*
* http://www.bittiming.can-wiki.info/
*/
typedef struct btrSet
{
int canclock; // Mhz, clock speed of CAN peripheral
int kBPS; // kHz, CAN bus bits/Second
uint32_t BTR; // BTR register value
};
const btrSet meBTRset[] =
{
#ifdef STM32F0
// Clock = 48Mhz
{48,10,0x001c012b},
{48,20,0x001c0095},
{48,50,0x001c003b},
{48,100,0x001c001d},
{48,125,0x001c0017},
{48,250,0x001c000b},
{48,500,0x001c0005},
{48,1000,0x001c0002},
#endif
#ifdef STM32F1
// Clock = 36Mhz
{36,10,0x001e00c7},
{36,20,0x001e0063},
{36,50,0x001e0027},
{36,100,0x001e0013},
{36,125,0x001e000f},
{36,250,0x001e0007},
{36,500,0x001e0003},
{36,1000,0x001e0001},
#endif
#ifdef STM32F4
#ifdef STM32F413xx
// Clock = 50Mhz
{50,10,0x000701f3},
{50,20,0x000700f9},
{50,50,0x00070063},
{50,100,0x00070031},
{50,125,0x001c0018},
{50,250,0x00070013},
{50,500,0x00070009},
{50,1000,0x00070004},
#else
// Clock = 45Mhz for F446,
{45,10,0x001e00f9},
{45,20,0x001e007c},
{45,50,0x001e0031},
{45,100,0x001e0018},
{45,125,0x001e0013},
{45,250,0x001e0009},
{45,500,0x001e0004},
{45,750,0x001b0003},
{45,1000,0x001b0002},
#endif
#endif
#ifdef STM32L4
// Clock = 80Mhz
{80,10,0x001c01f3},
{80,20,0x001c00f9},
{80,50,0x001c0063},
{80,100,0x001c0031},
{80,125,0x001c0027},
{80,250,0x001c0013},
{80,500,0x001c0009},
{80,1000,0x001c0004},
#endif
{0,0,0}
};
static int meCanClock()
{
int canClock;
uint32_t pclk1_frequency = HAL_RCC_GetPCLK1Freq();
canClock = (int)pclk1_frequency/1000000;
return canClock;
}
uint32_t meCanBTR(int bps)
{
switch (bps)
{
case 10:
case 20:
case 50:
case 100:
case 125:
case 250:
case 500:
case 1000:
break;
default:
return 0;break;
}
int canClock=meCanClock();
for (int i =0;i< (sizeof(meBTRset)/sizeof(meBTRset[0]));i++)
{
btrSet b = meBTRset[i];
if (canClock != b.canclock) continue;
if (bps != b.kBPS) continue;
return b.BTR;
}
return 0;
}