Skip to content

Commit 9e85099

Browse files
committed
canio.CAN: switch rx/tx, make both mandatory, move declarations around
1 parent 4f736ca commit 9e85099

File tree

4 files changed

+43
-47
lines changed

4 files changed

+43
-47
lines changed

ports/atmel-samd/common-hal/_canio/CAN.c

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,12 @@ STATIC canio_can_obj_t *can_objs[MP_ARRAY_SIZE(can_insts)];
4747
// This must be placed in the first 64kB of RAM
4848
STATIC COMPILER_SECTION(".canram") canio_can_state_t can_state[MP_ARRAY_SIZE(can_insts)];
4949

50-
__attribute__((optimize("O0"), noinline))
51-
void common_hal_canio_can_construct(canio_can_obj_t *self, mcu_pin_obj_t *rx, mcu_pin_obj_t *tx, int baudrate, bool loopback, bool silent)
50+
void common_hal_canio_can_construct(canio_can_obj_t *self, mcu_pin_obj_t *tx, mcu_pin_obj_t *rx, int baudrate, bool loopback, bool silent)
5251
{
5352
mcu_pin_function_t *tx_function = mcu_find_pin_function(can_tx, tx, -1, MP_QSTR_tx);
54-
int instance = tx_function ? tx_function->instance : -1;
53+
int instance = tx_function->instance;
5554

5655
mcu_pin_function_t *rx_function = mcu_find_pin_function(can_rx, rx, instance, MP_QSTR_rx);
57-
instance = rx_function->instance;
5856

5957
const uint32_t can_frequency = CONF_CAN0_FREQUENCY;
6058

@@ -69,17 +67,13 @@ void common_hal_canio_can_construct(canio_can_obj_t *self, mcu_pin_obj_t *rx, mc
6967
mp_raise_OSError(MP_EINVAL); // baudrate cannot be attained (16kHz or something is lower bound, should never happen)
7068
}
7169

72-
if (tx_function) {
73-
gpio_set_pin_direction(tx_function->pin, GPIO_DIRECTION_OUT);
74-
gpio_set_pin_function(tx_function->pin, tx_function->function);
75-
common_hal_never_reset_pin(tx_function->obj);
76-
}
70+
gpio_set_pin_direction(tx_function->pin, GPIO_DIRECTION_OUT);
71+
gpio_set_pin_function(tx_function->pin, tx_function->function);
72+
common_hal_never_reset_pin(tx_function->obj);
7773

78-
if (rx_function) {
79-
gpio_set_pin_direction(rx_function->pin, GPIO_DIRECTION_IN);
80-
gpio_set_pin_function(rx_function->pin, rx_function->function);
81-
common_hal_never_reset_pin(rx_function->obj);
82-
}
74+
gpio_set_pin_direction(rx_function->pin, GPIO_DIRECTION_IN);
75+
gpio_set_pin_function(rx_function->pin, rx_function->function);
76+
common_hal_never_reset_pin(rx_function->obj);
8377

8478
self->tx_pin_number = tx ? common_hal_mcu_pin_number(tx) : COMMON_HAL_MCU_NO_PIN;
8579
self->rx_pin_number = rx ? common_hal_mcu_pin_number(rx) : COMMON_HAL_MCU_NO_PIN;

ports/atmel-samd/common-hal/_canio/CAN.h

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
#include "py/obj.h"
3030
#include "shared-bindings/_canio/__init__.h"
31+
#include "shared-bindings/_canio/CAN.h"
3132
#include "component/can.h"
3233
#include "common-hal/microcontroller/Pin.h"
3334
#include "common-hal/_canio/__init__.h"
@@ -36,7 +37,7 @@
3637
#define COMMON_HAL_CAN_RX_FIFO_LEN (2)
3738
#define COMMON_HAL_CAN_TX_FIFO_LEN (2)
3839

39-
typedef struct {
40+
typedef struct canio_can_obj {
4041
mp_obj_base_t base;
4142
Can *hw;
4243
canio_can_state_t *state;
@@ -52,22 +53,3 @@ typedef struct {
5253
bool fifo0_in_use:1;
5354
bool fifo1_in_use:1;
5455
} canio_can_obj_t;
55-
56-
void common_hal_canio_can_construct(canio_can_obj_t *self, mcu_pin_obj_t *rx, mcu_pin_obj_t *tx, int baudrate, bool loopback, bool silent);
57-
bool common_hal_canio_can_auto_restart_get(canio_can_obj_t *self);
58-
bool common_hal_canio_can_deinited(canio_can_obj_t *self);
59-
int common_hal_canio_can_baudrate_get(canio_can_obj_t *self);
60-
int common_hal_canio_can_bus_off_state_count_get(canio_can_obj_t *self);
61-
int common_hal_canio_can_error_passive_state_count_get(canio_can_obj_t *self);
62-
int common_hal_canio_can_error_warning_state_count_get(canio_can_obj_t *self);
63-
bool common_hal_canio_can_loopback_get(canio_can_obj_t *self);
64-
int common_hal_canio_can_receive_error_count_get(canio_can_obj_t *self);
65-
canio_bus_state_t common_hal_canio_can_state_get(canio_can_obj_t *self);
66-
bool common_hal_canio_can_silent_get(canio_can_obj_t *self);
67-
int common_hal_canio_can_transmit_error_count_get(canio_can_obj_t *self);
68-
void common_hal_canio_can_auto_restart_set(canio_can_obj_t *self, bool auto_restart);
69-
void common_hal_canio_can_check_for_deinit(canio_can_obj_t *self);
70-
void common_hal_canio_can_deinit(canio_can_obj_t *self);
71-
void common_hal_canio_can_restart(canio_can_obj_t *self);
72-
void common_hal_canio_can_send(canio_can_obj_t *self, canio_message_obj_t *message);
73-
void common_hal_canio_reset(void);

shared-bindings/_canio/CAN.c

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,35 +42,32 @@
4242
//| """CAN bus protocol"""
4343
//|
4444
//| def __init__(self,
45-
//| rx: Optional[microcontroller.Pin]=None,
46-
//| tx: Optional[microcontroller.Pin]=None,
45+
//| tx: microcontroller.Pin,
46+
//| rx: microcontroller.Pin,
4747
//| *,
4848
//| baudrate: int = 250000,
4949
//| loopback: bool = False,
50+
//| silent: bool = False,
5051
//| auto_restart: bool = False,
5152
//| ):
5253
//| """A common shared-bus protocol. The rx and tx pins are generally
5354
//| connected to a transceiver which controls the H and L pins on a
5455
//| shared bus.
5556
//|
56-
//| Normally, both ``tx`` and ``rx`` pins will be specified. However,
57-
//| in silent and loopback modes, the other pin may not be required and
58-
//| can be used for other purposes.
59-
//|
60-
//| :param ~microcontroller.Pin rx: the pin to receive with, or None.
61-
//| :param ~microcontroller.Pin tx: the pin to transmit with, or None.
57+
//| :param ~microcontroller.Pin rx: the pin to receive with
58+
//| :param ~microcontroller.Pin tx: the pin to transmit with
6259
//| :param int baudrate: The bit rate of the bus in Hz. All devices on the bus must agree on this value.
63-
//| :param bool loopback: True if the peripheral will be operated in loopback mode. In loopback mode, the ``rx`` pin's value is ignored, and the device receives the packets it sends.
64-
//| :param bool silent: True if the peripheral will be operated in silent mode. In silent mode, the ``tx`` pin is always driven to the high logic level. This mode can be used to "sniff" a CAN bus without interfering.
60+
//| :param bool loopback: When True the ``rx`` pin's value is ignored, and the device receives the packets it sends.
61+
//| :param bool silent: When True the ``tx`` pin is always driven to the high logic level. This mode can be used to "sniff" a CAN bus without interfering.
6562
//| :param bool auto_restart: If True, will restart communications after entering bus-off state
6663
//| """
6764
//| ...
6865
//|
6966
STATIC mp_obj_t canio_can_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
7067
enum { ARG_rx, ARG_tx, ARG_baudrate, ARG_loopback, ARG_silent, ARG_auto_restart, NUM_ARGS };
7168
static const mp_arg_t allowed_args[] = {
72-
{ MP_QSTR_rx, MP_ARG_OBJ, {.u_obj = mp_const_none} },
73-
{ MP_QSTR_tx, MP_ARG_OBJ, {.u_obj = mp_const_none} },
69+
{ MP_QSTR_tx, MP_ARG_OBJ | MP_ARG_REQUIRED },
70+
{ MP_QSTR_rx, MP_ARG_OBJ | MP_ARG_REQUIRED },
7471
{ MP_QSTR_baudrate, MP_ARG_INT, {.u_int = 250000} },
7572
{ MP_QSTR_loopback, MP_ARG_BOOL, {.u_bool = false} },
7673
{ MP_QSTR_silent, MP_ARG_BOOL, {.u_bool = false} },
@@ -89,7 +86,7 @@ STATIC mp_obj_t canio_can_make_new(const mp_obj_type_t *type, size_t n_args, con
8986

9087
canio_can_obj_t *self = m_new_obj(canio_can_obj_t);
9188
self->base.type = &canio_can_type;
92-
common_hal_canio_can_construct(self, rx_pin, tx_pin, args[ARG_baudrate].u_int, args[ARG_loopback].u_bool, args[ARG_silent].u_bool);
89+
common_hal_canio_can_construct(self, tx_pin, rx_pin, args[ARG_baudrate].u_int, args[ARG_loopback].u_bool, args[ARG_silent].u_bool);
9390

9491
common_hal_canio_can_auto_restart_set(self, args[ARG_auto_restart].u_bool);
9592

shared-bindings/_canio/CAN.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,28 @@
2727
#pragma once
2828

2929
#include "py/obj.h"
30+
#include "shared-bindings/microcontroller/Pin.h"
31+
#include "shared-bindings/_canio/Message.h"
3032

3133
extern const mp_obj_type_t canio_can_type;
34+
35+
typedef struct canio_can_obj canio_can_obj_t;
36+
37+
void common_hal_canio_can_construct(canio_can_obj_t *self, mcu_pin_obj_t *tx, mcu_pin_obj_t *rx, int baudrate, bool loopback, bool silent);
38+
bool common_hal_canio_can_auto_restart_get(canio_can_obj_t *self);
39+
bool common_hal_canio_can_deinited(canio_can_obj_t *self);
40+
int common_hal_canio_can_baudrate_get(canio_can_obj_t *self);
41+
int common_hal_canio_can_bus_off_state_count_get(canio_can_obj_t *self);
42+
int common_hal_canio_can_error_passive_state_count_get(canio_can_obj_t *self);
43+
int common_hal_canio_can_error_warning_state_count_get(canio_can_obj_t *self);
44+
bool common_hal_canio_can_loopback_get(canio_can_obj_t *self);
45+
int common_hal_canio_can_receive_error_count_get(canio_can_obj_t *self);
46+
canio_bus_state_t common_hal_canio_can_state_get(canio_can_obj_t *self);
47+
bool common_hal_canio_can_silent_get(canio_can_obj_t *self);
48+
int common_hal_canio_can_transmit_error_count_get(canio_can_obj_t *self);
49+
void common_hal_canio_can_auto_restart_set(canio_can_obj_t *self, bool auto_restart);
50+
void common_hal_canio_can_check_for_deinit(canio_can_obj_t *self);
51+
void common_hal_canio_can_deinit(canio_can_obj_t *self);
52+
void common_hal_canio_can_restart(canio_can_obj_t *self);
53+
void common_hal_canio_can_send(canio_can_obj_t *self, canio_message_obj_t *message);
54+
void common_hal_canio_reset(void);

0 commit comments

Comments
 (0)