canio.CAN: switch rx/tx, make both mandatory, move declarations around
This commit is contained in:
parent
4e4853dcb2
commit
9e8f1820c8
@ -47,14 +47,12 @@ STATIC canio_can_obj_t *can_objs[MP_ARRAY_SIZE(can_insts)];
|
|||||||
// This must be placed in the first 64kB of RAM
|
// This must be placed in the first 64kB of RAM
|
||||||
STATIC COMPILER_SECTION(".canram") canio_can_state_t can_state[MP_ARRAY_SIZE(can_insts)];
|
STATIC COMPILER_SECTION(".canram") canio_can_state_t can_state[MP_ARRAY_SIZE(can_insts)];
|
||||||
|
|
||||||
__attribute__((optimize("O0"), noinline))
|
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)
|
||||||
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)
|
|
||||||
{
|
{
|
||||||
mcu_pin_function_t *tx_function = mcu_find_pin_function(can_tx, tx, -1, MP_QSTR_tx);
|
mcu_pin_function_t *tx_function = mcu_find_pin_function(can_tx, tx, -1, MP_QSTR_tx);
|
||||||
int instance = tx_function ? tx_function->instance : -1;
|
int instance = tx_function->instance;
|
||||||
|
|
||||||
mcu_pin_function_t *rx_function = mcu_find_pin_function(can_rx, rx, instance, MP_QSTR_rx);
|
mcu_pin_function_t *rx_function = mcu_find_pin_function(can_rx, rx, instance, MP_QSTR_rx);
|
||||||
instance = rx_function->instance;
|
|
||||||
|
|
||||||
const uint32_t can_frequency = CONF_CAN0_FREQUENCY;
|
const uint32_t can_frequency = CONF_CAN0_FREQUENCY;
|
||||||
|
|
||||||
@ -69,17 +67,13 @@ void common_hal_canio_can_construct(canio_can_obj_t *self, mcu_pin_obj_t *rx, mc
|
|||||||
mp_raise_OSError(MP_EINVAL); // baudrate cannot be attained (16kHz or something is lower bound, should never happen)
|
mp_raise_OSError(MP_EINVAL); // baudrate cannot be attained (16kHz or something is lower bound, should never happen)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (tx_function) {
|
|
||||||
gpio_set_pin_direction(tx_function->pin, GPIO_DIRECTION_OUT);
|
gpio_set_pin_direction(tx_function->pin, GPIO_DIRECTION_OUT);
|
||||||
gpio_set_pin_function(tx_function->pin, tx_function->function);
|
gpio_set_pin_function(tx_function->pin, tx_function->function);
|
||||||
common_hal_never_reset_pin(tx_function->obj);
|
common_hal_never_reset_pin(tx_function->obj);
|
||||||
}
|
|
||||||
|
|
||||||
if (rx_function) {
|
|
||||||
gpio_set_pin_direction(rx_function->pin, GPIO_DIRECTION_IN);
|
gpio_set_pin_direction(rx_function->pin, GPIO_DIRECTION_IN);
|
||||||
gpio_set_pin_function(rx_function->pin, rx_function->function);
|
gpio_set_pin_function(rx_function->pin, rx_function->function);
|
||||||
common_hal_never_reset_pin(rx_function->obj);
|
common_hal_never_reset_pin(rx_function->obj);
|
||||||
}
|
|
||||||
|
|
||||||
self->tx_pin_number = tx ? common_hal_mcu_pin_number(tx) : COMMON_HAL_MCU_NO_PIN;
|
self->tx_pin_number = tx ? common_hal_mcu_pin_number(tx) : COMMON_HAL_MCU_NO_PIN;
|
||||||
self->rx_pin_number = rx ? common_hal_mcu_pin_number(rx) : COMMON_HAL_MCU_NO_PIN;
|
self->rx_pin_number = rx ? common_hal_mcu_pin_number(rx) : COMMON_HAL_MCU_NO_PIN;
|
||||||
|
@ -28,6 +28,7 @@
|
|||||||
|
|
||||||
#include "py/obj.h"
|
#include "py/obj.h"
|
||||||
#include "shared-bindings/_canio/__init__.h"
|
#include "shared-bindings/_canio/__init__.h"
|
||||||
|
#include "shared-bindings/_canio/CAN.h"
|
||||||
#include "component/can.h"
|
#include "component/can.h"
|
||||||
#include "common-hal/microcontroller/Pin.h"
|
#include "common-hal/microcontroller/Pin.h"
|
||||||
#include "common-hal/_canio/__init__.h"
|
#include "common-hal/_canio/__init__.h"
|
||||||
@ -36,7 +37,7 @@
|
|||||||
#define COMMON_HAL_CAN_RX_FIFO_LEN (2)
|
#define COMMON_HAL_CAN_RX_FIFO_LEN (2)
|
||||||
#define COMMON_HAL_CAN_TX_FIFO_LEN (2)
|
#define COMMON_HAL_CAN_TX_FIFO_LEN (2)
|
||||||
|
|
||||||
typedef struct {
|
typedef struct canio_can_obj {
|
||||||
mp_obj_base_t base;
|
mp_obj_base_t base;
|
||||||
Can *hw;
|
Can *hw;
|
||||||
canio_can_state_t *state;
|
canio_can_state_t *state;
|
||||||
@ -52,22 +53,3 @@ typedef struct {
|
|||||||
bool fifo0_in_use:1;
|
bool fifo0_in_use:1;
|
||||||
bool fifo1_in_use:1;
|
bool fifo1_in_use:1;
|
||||||
} canio_can_obj_t;
|
} canio_can_obj_t;
|
||||||
|
|
||||||
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);
|
|
||||||
bool common_hal_canio_can_auto_restart_get(canio_can_obj_t *self);
|
|
||||||
bool common_hal_canio_can_deinited(canio_can_obj_t *self);
|
|
||||||
int common_hal_canio_can_baudrate_get(canio_can_obj_t *self);
|
|
||||||
int common_hal_canio_can_bus_off_state_count_get(canio_can_obj_t *self);
|
|
||||||
int common_hal_canio_can_error_passive_state_count_get(canio_can_obj_t *self);
|
|
||||||
int common_hal_canio_can_error_warning_state_count_get(canio_can_obj_t *self);
|
|
||||||
bool common_hal_canio_can_loopback_get(canio_can_obj_t *self);
|
|
||||||
int common_hal_canio_can_receive_error_count_get(canio_can_obj_t *self);
|
|
||||||
canio_bus_state_t common_hal_canio_can_state_get(canio_can_obj_t *self);
|
|
||||||
bool common_hal_canio_can_silent_get(canio_can_obj_t *self);
|
|
||||||
int common_hal_canio_can_transmit_error_count_get(canio_can_obj_t *self);
|
|
||||||
void common_hal_canio_can_auto_restart_set(canio_can_obj_t *self, bool auto_restart);
|
|
||||||
void common_hal_canio_can_check_for_deinit(canio_can_obj_t *self);
|
|
||||||
void common_hal_canio_can_deinit(canio_can_obj_t *self);
|
|
||||||
void common_hal_canio_can_restart(canio_can_obj_t *self);
|
|
||||||
void common_hal_canio_can_send(canio_can_obj_t *self, canio_message_obj_t *message);
|
|
||||||
void common_hal_canio_reset(void);
|
|
||||||
|
@ -42,26 +42,23 @@
|
|||||||
//| """CAN bus protocol"""
|
//| """CAN bus protocol"""
|
||||||
//|
|
//|
|
||||||
//| def __init__(self,
|
//| def __init__(self,
|
||||||
//| rx: Optional[microcontroller.Pin]=None,
|
//| tx: microcontroller.Pin,
|
||||||
//| tx: Optional[microcontroller.Pin]=None,
|
//| rx: microcontroller.Pin,
|
||||||
//| *,
|
//| *,
|
||||||
//| baudrate: int = 250000,
|
//| baudrate: int = 250000,
|
||||||
//| loopback: bool = False,
|
//| loopback: bool = False,
|
||||||
|
//| silent: bool = False,
|
||||||
//| auto_restart: bool = False,
|
//| auto_restart: bool = False,
|
||||||
//| ):
|
//| ):
|
||||||
//| """A common shared-bus protocol. The rx and tx pins are generally
|
//| """A common shared-bus protocol. The rx and tx pins are generally
|
||||||
//| connected to a transceiver which controls the H and L pins on a
|
//| connected to a transceiver which controls the H and L pins on a
|
||||||
//| shared bus.
|
//| shared bus.
|
||||||
//|
|
//|
|
||||||
//| Normally, both ``tx`` and ``rx`` pins will be specified. However,
|
//| :param ~microcontroller.Pin rx: the pin to receive with
|
||||||
//| in silent and loopback modes, the other pin may not be required and
|
//| :param ~microcontroller.Pin tx: the pin to transmit with
|
||||||
//| can be used for other purposes.
|
|
||||||
//|
|
|
||||||
//| :param ~microcontroller.Pin rx: the pin to receive with, or None.
|
|
||||||
//| :param ~microcontroller.Pin tx: the pin to transmit with, or None.
|
|
||||||
//| :param int baudrate: The bit rate of the bus in Hz. All devices on the bus must agree on this value.
|
//| :param int baudrate: The bit rate of the bus in Hz. All devices on the bus must agree on this value.
|
||||||
//| :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.
|
//| :param bool loopback: When True the ``rx`` pin's value is ignored, and the device receives the packets it sends.
|
||||||
//| :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.
|
//| :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.
|
||||||
//| :param bool auto_restart: If True, will restart communications after entering bus-off state
|
//| :param bool auto_restart: If True, will restart communications after entering bus-off state
|
||||||
//| """
|
//| """
|
||||||
//| ...
|
//| ...
|
||||||
@ -69,8 +66,8 @@
|
|||||||
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) {
|
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) {
|
||||||
enum { ARG_rx, ARG_tx, ARG_baudrate, ARG_loopback, ARG_silent, ARG_auto_restart, NUM_ARGS };
|
enum { ARG_rx, ARG_tx, ARG_baudrate, ARG_loopback, ARG_silent, ARG_auto_restart, NUM_ARGS };
|
||||||
static const mp_arg_t allowed_args[] = {
|
static const mp_arg_t allowed_args[] = {
|
||||||
{ MP_QSTR_rx, MP_ARG_OBJ, {.u_obj = mp_const_none} },
|
{ MP_QSTR_tx, MP_ARG_OBJ | MP_ARG_REQUIRED },
|
||||||
{ MP_QSTR_tx, MP_ARG_OBJ, {.u_obj = mp_const_none} },
|
{ MP_QSTR_rx, MP_ARG_OBJ | MP_ARG_REQUIRED },
|
||||||
{ MP_QSTR_baudrate, MP_ARG_INT, {.u_int = 250000} },
|
{ MP_QSTR_baudrate, MP_ARG_INT, {.u_int = 250000} },
|
||||||
{ MP_QSTR_loopback, MP_ARG_BOOL, {.u_bool = false} },
|
{ MP_QSTR_loopback, MP_ARG_BOOL, {.u_bool = false} },
|
||||||
{ MP_QSTR_silent, MP_ARG_BOOL, {.u_bool = false} },
|
{ 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
|
|||||||
|
|
||||||
canio_can_obj_t *self = m_new_obj(canio_can_obj_t);
|
canio_can_obj_t *self = m_new_obj(canio_can_obj_t);
|
||||||
self->base.type = &canio_can_type;
|
self->base.type = &canio_can_type;
|
||||||
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);
|
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);
|
||||||
|
|
||||||
common_hal_canio_can_auto_restart_set(self, args[ARG_auto_restart].u_bool);
|
common_hal_canio_can_auto_restart_set(self, args[ARG_auto_restart].u_bool);
|
||||||
|
|
||||||
|
@ -27,5 +27,28 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "py/obj.h"
|
#include "py/obj.h"
|
||||||
|
#include "shared-bindings/microcontroller/Pin.h"
|
||||||
|
#include "shared-bindings/_canio/Message.h"
|
||||||
|
|
||||||
extern const mp_obj_type_t canio_can_type;
|
extern const mp_obj_type_t canio_can_type;
|
||||||
|
|
||||||
|
typedef struct canio_can_obj canio_can_obj_t;
|
||||||
|
|
||||||
|
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);
|
||||||
|
bool common_hal_canio_can_auto_restart_get(canio_can_obj_t *self);
|
||||||
|
bool common_hal_canio_can_deinited(canio_can_obj_t *self);
|
||||||
|
int common_hal_canio_can_baudrate_get(canio_can_obj_t *self);
|
||||||
|
int common_hal_canio_can_bus_off_state_count_get(canio_can_obj_t *self);
|
||||||
|
int common_hal_canio_can_error_passive_state_count_get(canio_can_obj_t *self);
|
||||||
|
int common_hal_canio_can_error_warning_state_count_get(canio_can_obj_t *self);
|
||||||
|
bool common_hal_canio_can_loopback_get(canio_can_obj_t *self);
|
||||||
|
int common_hal_canio_can_receive_error_count_get(canio_can_obj_t *self);
|
||||||
|
canio_bus_state_t common_hal_canio_can_state_get(canio_can_obj_t *self);
|
||||||
|
bool common_hal_canio_can_silent_get(canio_can_obj_t *self);
|
||||||
|
int common_hal_canio_can_transmit_error_count_get(canio_can_obj_t *self);
|
||||||
|
void common_hal_canio_can_auto_restart_set(canio_can_obj_t *self, bool auto_restart);
|
||||||
|
void common_hal_canio_can_check_for_deinit(canio_can_obj_t *self);
|
||||||
|
void common_hal_canio_can_deinit(canio_can_obj_t *self);
|
||||||
|
void common_hal_canio_can_restart(canio_can_obj_t *self);
|
||||||
|
void common_hal_canio_can_send(canio_can_obj_t *self, canio_message_obj_t *message);
|
||||||
|
void common_hal_canio_reset(void);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user