Skip to content

Commit d1b42d7

Browse files
committed
stmhal: Improve CAN init so that it can take sjw, bs1, bs2 args.
Also update docs to explain how CAN baudrate is determined.
1 parent 224fee0 commit d1b42d7

3 files changed

Lines changed: 33 additions & 13 deletions

File tree

docs/library/pyb.CAN.rst

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,32 @@ Methods
4444
Initialise the CAN bus with the given parameters:
4545

4646
- ``mode`` is one of: NORMAL, LOOPBACK, SILENT, SILENT_LOOPBACK
47-
48-
If ``extframe`` is True then the bus uses extended identifiers in the frames (29 bits).
49-
Otherwise it uses standard 11 bit identifiers.
47+
- if ``extframe`` is True then the bus uses extended identifiers in the frames
48+
(29 bits); otherwise it uses standard 11 bit identifiers
49+
- ``prescaler`` is used to set the duration of 1 time quanta; the time quanta
50+
will be the input clock (PCLK1, see :meth:`pyb.freq()`) divided by the prescaler
51+
- ``sjw`` is the resynchronisation jump width in units of the time quanta;
52+
it can be 1, 2, 3, 4
53+
- ``bs1`` defines the location of the sample point in units of the time quanta;
54+
it can be between 1 and 1024 inclusive
55+
- ``bs2`` defines the location of the transmit point in units of the time quanta;
56+
it can be between 1 and 16 inclusive
57+
58+
The time quanta tq is the basic unit of time for the CAN bus. tq is the CAN
59+
prescaler value divided by PCLK1 (the frequency of internal peripheral bus 1);
60+
see :meth:`pyb.freq()` to determine PCLK1.
61+
62+
A single bit is made up of the synchronisation segment, which is always 1 tq.
63+
Then follows bit segment 1, then bit segment 2. The sample point is after bit
64+
segment 1 finishes. The transmit point is after bit segment 2 finishes.
65+
The baud rate will be 1/bittime, where the bittime is 1 + BS1 + BS2 multiplied
66+
by the time quanta tq.
67+
68+
For example, with PCLK1=42MHz, prescaler=100, sjw=1, bs1=6, bs2=8, the value of
69+
tq is 2.38 microseconds. The bittime is 35.7 microseconds, and the baudrate
70+
is 28kHz.
71+
72+
See page 680 of the STM32F405 datasheet for more details.
5073

5174
.. method:: can.deinit()
5275

stmhal/can.c

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -162,21 +162,15 @@ STATIC void pyb_can_print(void (*print)(void *env, const char *fmt, ...), void *
162162
}
163163
}
164164

165-
/// \method init(mode, extframe=False, prescaler=100, *, sjw=1, bs1=6, bs2=8)
166-
///
167-
/// Initialise the CAN bus with the given parameters:
168-
///
169-
/// - `mode` is one of: NORMAL, LOOPBACK, SILENT, SILENT_LOOPBACK
165+
// init(mode, extframe=False, prescaler=100, *, sjw=1, bs1=6, bs2=8)
170166
STATIC mp_obj_t pyb_can_init_helper(pyb_can_obj_t *self, mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
171167
static const mp_arg_t allowed_args[] = {
172168
{ MP_QSTR_mode, MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = CAN_MODE_NORMAL} },
173169
{ MP_QSTR_extframe, MP_ARG_BOOL, {.u_bool = false} },
174170
{ MP_QSTR_prescaler, MP_ARG_INT, {.u_int = 100} },
175-
/*
176171
{ MP_QSTR_sjw, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1} },
177172
{ MP_QSTR_bs1, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 6} },
178173
{ MP_QSTR_bs2, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 8} },
179-
*/
180174
};
181175

182176
// parse args
@@ -190,9 +184,9 @@ STATIC mp_obj_t pyb_can_init_helper(pyb_can_obj_t *self, mp_uint_t n_args, const
190184
CAN_InitTypeDef *init = &self->can.Init;
191185
init->Mode = args[0].u_int << 4; // shift-left so modes fit in a small-int
192186
init->Prescaler = args[2].u_int;
193-
init->SJW = CAN_SJW_1TQ; // TODO set from args
194-
init->BS1 = CAN_BS1_6TQ; // TODO set from args
195-
init->BS2 = CAN_BS2_8TQ; // TODO set from args
187+
init->SJW = ((args[3].u_int - 1) & 3) << 24;
188+
init->BS1 = ((args[4].u_int - 1) & 0xf) << 16;
189+
init->BS2 = ((args[5].u_int - 1) & 7) << 20;
196190
init->TTCM = DISABLE;
197191
init->ABOM = DISABLE;
198192
init->AWUM = DISABLE;

stmhal/qstrdefsport.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,9 @@ Q(addr)
171171
Q(fifo)
172172
Q(timeout)
173173
Q(extframe)
174+
Q(sjw)
175+
Q(bs1)
176+
Q(bs2)
174177
Q(NORMAL)
175178
Q(LOOPBACK)
176179
Q(SILENT)

0 commit comments

Comments
 (0)