Skip to content

Commit a79706f

Browse files
iabdalkaderdpgeorge
authored andcommitted
stm32/pyb_can: Define the maximum bit timing parameters.
Define the maximum parameters for CAN/FDCAN nominal bit timing, and for FDCAN data bit timing, for bit timing calculations.
1 parent 95104c9 commit a79706f

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

ports/stm32/pyb_can.c

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,16 @@
5959
#define CAN_DEFAULT_BS1 (8)
6060
#define CAN_DEFAULT_BS2 (3)
6161

62+
#define CAN_MAXIMUM_NBRP (512)
63+
#define CAN_MAXIMUM_NBS1 (256)
64+
#define CAN_MAXIMUM_NBS2 (128)
65+
// Minimum Nominal time segment for FDCAN is 2.
66+
#define CAN_MINIMUM_TSEG (2)
67+
68+
#define CAN_MAXIMUM_DBRP (32)
69+
#define CAN_MAXIMUM_DBS1 (32)
70+
#define CAN_MAXIMUM_DBS2 (16)
71+
6272
#define CAN_MODE_NORMAL FDCAN_MODE_NORMAL
6373
#define CAN_MODE_LOOPBACK FDCAN_MODE_EXTERNAL_LOOPBACK
6474
#define CAN_MODE_SILENT FDCAN_MODE_BUS_MONITORING
@@ -97,6 +107,11 @@ extern const uint8_t DLCtoBytes[16];
97107
#define CAN_DEFAULT_BS1 (6)
98108
#define CAN_DEFAULT_BS2 (8)
99109

110+
#define CAN_MAXIMUM_NBRP (1024)
111+
#define CAN_MAXIMUM_NBS1 (16)
112+
#define CAN_MAXIMUM_NBS2 (8)
113+
#define CAN_MINIMUM_TSEG (1)
114+
100115
#define CAN_IT_FIFO0_FULL CAN_IT_FF0
101116
#define CAN_IT_FIFO1_FULL CAN_IT_FF1
102117
#define CAN_IT_FIFO0_OVRF CAN_IT_FOV0
@@ -178,13 +193,13 @@ STATIC uint32_t pyb_can_get_source_freq() {
178193
}
179194

180195
STATIC void pyb_can_get_bit_timing(mp_uint_t baudrate, mp_uint_t sample_point,
196+
uint32_t max_brp, uint32_t max_bs1, uint32_t max_bs2, uint32_t min_tseg,
181197
mp_int_t *bs1_out, mp_int_t *bs2_out, mp_int_t *prescaler_out) {
182198
uint32_t can_kern_clk = pyb_can_get_source_freq();
183-
184-
// The following max values work on all MCUs for classical CAN.
185-
for (int brp = 1; brp < 512; brp++) {
186-
for (int bs1 = 1; bs1 < 16; bs1++) {
187-
for (int bs2 = 1; bs2 < 8; bs2++) {
199+
// Calculate CAN bit timing.
200+
for (uint32_t brp = 1; brp < max_brp; brp++) {
201+
for (uint32_t bs1 = min_tseg; bs1 < max_bs1; bs1++) {
202+
for (uint32_t bs2 = min_tseg; bs2 < max_bs2; bs2++) {
188203
if ((baudrate == (can_kern_clk / (brp * (1 + bs1 + bs2)))) &&
189204
((sample_point * 10) == (((1 + bs1) * 1000) / (1 + bs1 + bs2)))) {
190205
*bs1_out = bs1;
@@ -229,9 +244,10 @@ STATIC mp_obj_t pyb_can_init_helper(pyb_can_obj_t *self, size_t n_args, const mp
229244
// set the CAN configuration values
230245
memset(&self->can, 0, sizeof(self->can));
231246

232-
// Calculate CAN bit timing from baudrate if provided
247+
// Calculate CAN nominal bit timing from baudrate if provided
233248
if (args[ARG_baudrate].u_int != 0) {
234249
pyb_can_get_bit_timing(args[ARG_baudrate].u_int, args[ARG_sample_point].u_int,
250+
CAN_MAXIMUM_NBRP, CAN_MAXIMUM_NBS1, CAN_MAXIMUM_NBS2, CAN_MINIMUM_TSEG,
235251
&args[ARG_bs1].u_int, &args[ARG_bs2].u_int, &args[ARG_prescaler].u_int);
236252
}
237253

@@ -240,9 +256,10 @@ STATIC mp_obj_t pyb_can_init_helper(pyb_can_obj_t *self, size_t n_args, const mp
240256
if (args[ARG_brs_sample_point].u_int == 0) {
241257
args[ARG_brs_sample_point].u_int = args[ARG_sample_point].u_int;
242258
}
243-
// Calculate BRS CAN bit timing from baudrate if provided
259+
// Calculate CAN data bit timing from baudrate if provided
244260
if (args[ARG_brs_baudrate].u_int != 0) {
245261
pyb_can_get_bit_timing(args[ARG_brs_baudrate].u_int, args[ARG_brs_sample_point].u_int,
262+
CAN_MAXIMUM_DBRP, CAN_MAXIMUM_DBS1, CAN_MAXIMUM_DBS2, 1,
246263
&args[ARG_brs_bs1].u_int, &args[ARG_brs_bs2].u_int, &args[ARG_brs_prescaler].u_int);
247264
}
248265
// Set BRS bit timings.

0 commit comments

Comments
 (0)