Skip to content

Commit 823ca03

Browse files
committed
stm32/can: Add "auto_restart" option to constructor and init() method.
1 parent 1608c4f commit 823ca03

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

docs/library/pyb.CAN.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ Class Methods
4949
Methods
5050
-------
5151

52-
.. method:: CAN.init(mode, extframe=False, prescaler=100, \*, sjw=1, bs1=6, bs2=8)
52+
.. method:: CAN.init(mode, extframe=False, prescaler=100, \*, sjw=1, bs1=6, bs2=8, auto_restart=False)
5353

5454
Initialise the CAN bus with the given parameters:
5555

@@ -64,6 +64,8 @@ Methods
6464
it can be between 1 and 1024 inclusive
6565
- *bs2* defines the location of the transmit point in units of the time quanta;
6666
it can be between 1 and 16 inclusive
67+
- *auto_restart* sets whether the controller will automatically try and restart
68+
communications after entering the bus-off state
6769

6870
The time quanta tq is the basic unit of time for the CAN bus. tq is the CAN
6971
prescaler value divided by PCLK1 (the frequency of internal peripheral bus 1);

ports/stm32/can.c

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -307,34 +307,32 @@ STATIC void pyb_can_print(const mp_print_t *print, mp_obj_t self_in, mp_print_ki
307307
if (!self->is_enabled) {
308308
mp_printf(print, "CAN(%u)", self->can_id);
309309
} else {
310-
mp_printf(print, "CAN(%u, CAN.", self->can_id);
311310
qstr mode;
312311
switch (self->can.Init.Mode) {
313312
case CAN_MODE_NORMAL: mode = MP_QSTR_NORMAL; break;
314313
case CAN_MODE_LOOPBACK: mode = MP_QSTR_LOOPBACK; break;
315314
case CAN_MODE_SILENT: mode = MP_QSTR_SILENT; break;
316315
case CAN_MODE_SILENT_LOOPBACK: default: mode = MP_QSTR_SILENT_LOOPBACK; break;
317316
}
318-
mp_printf(print, "%q, extframe=", mode);
319-
if (self->extframe) {
320-
mode = MP_QSTR_True;
321-
} else {
322-
mode = MP_QSTR_False;
323-
}
324-
mp_printf(print, "%q)", mode);
317+
mp_printf(print, "CAN(%u, CAN.%q, extframe=%q, auto_restart=%q)",
318+
self->can_id,
319+
mode,
320+
self->extframe ? MP_QSTR_True : MP_QSTR_False,
321+
(self->can.Instance->MCR & CAN_MCR_ABOM) ? MP_QSTR_True : MP_QSTR_False);
325322
}
326323
}
327324

328325
// init(mode, extframe=False, prescaler=100, *, sjw=1, bs1=6, bs2=8)
329326
STATIC mp_obj_t pyb_can_init_helper(pyb_can_obj_t *self, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
330-
enum { ARG_mode, ARG_extframe, ARG_prescaler, ARG_sjw, ARG_bs1, ARG_bs2 };
327+
enum { ARG_mode, ARG_extframe, ARG_prescaler, ARG_sjw, ARG_bs1, ARG_bs2, ARG_auto_restart };
331328
static const mp_arg_t allowed_args[] = {
332329
{ MP_QSTR_mode, MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = CAN_MODE_NORMAL} },
333330
{ MP_QSTR_extframe, MP_ARG_BOOL, {.u_bool = false} },
334331
{ MP_QSTR_prescaler, MP_ARG_INT, {.u_int = 100} },
335332
{ MP_QSTR_sjw, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1} },
336333
{ MP_QSTR_bs1, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 6} },
337334
{ MP_QSTR_bs2, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 8} },
335+
{ MP_QSTR_auto_restart, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
338336
};
339337

340338
// parse args
@@ -352,7 +350,7 @@ STATIC mp_obj_t pyb_can_init_helper(pyb_can_obj_t *self, size_t n_args, const mp
352350
init->BS1 = ((args[ARG_bs1].u_int - 1) & 0xf) << 16;
353351
init->BS2 = ((args[ARG_bs2].u_int - 1) & 7) << 20;
354352
init->TTCM = DISABLE;
355-
init->ABOM = DISABLE;
353+
init->ABOM = args[ARG_auto_restart].u_bool ? ENABLE : DISABLE;
356354
init->AWUM = DISABLE;
357355
init->NART = DISABLE;
358356
init->RFLM = DISABLE;

0 commit comments

Comments
 (0)