Skip to content

Commit

Permalink
port/esp32: CAN_v2. WIP
Browse files Browse the repository at this point in the history
Signed-off-by: Ihor Nehrutsa <[email protected]>
  • Loading branch information
IhorNehrutsa committed Mar 3, 2025
1 parent 5e7e2db commit 8c2132e
Show file tree
Hide file tree
Showing 7 changed files with 775 additions and 207 deletions.
408 changes: 408 additions & 0 deletions docs/library/machine.CAN.rst

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions ports/esp32/esp32_common.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ list(APPEND MICROPY_SOURCE_PORT
fatfs_port.c
help.c
machine_bitstream.c
machine_can.c
machine_timer.c
machine_pin.c
machine_touchpad.c
Expand Down
474 changes: 288 additions & 186 deletions ports/esp32/machine_can.c

Large diffs are not rendered by default.

94 changes: 74 additions & 20 deletions ports/esp32/machine_can.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,36 +23,95 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef MICROPY_INCLUDED_ESP32_CAN_H
#define MICROPY_INCLUDED_ESP32_CAN_H

#ifndef MICROPY_INCLUDED_ESP32_MACHINE_CAN_H
#define MICROPY_INCLUDED_ESP32_MACHINE_CAN_H
/*
#include "modmachine.h"
#include "freertos/task.h"
#include "mpconfigport.h"
#include "py/obj.h"
*/
#if MICROPY_PY_MACHINE_CAN

//#define DEVICE_NAME "CAN"

#if MICROPY_HW_ENABLE_CAN
#define CAN_MODE_SILENT_LOOPBACK (0x10)

#define DEVICE_NAME "CAN"
typedef enum {
MODE_NORMAL = TWAI_MODE_NORMAL,
MODE_SLEEP = -1,
MODE_LOOPBACK = -2, // TWAI_MODE_NORMAL | CAN_MODE_SILENT_LOOPBACK,
MODE_SILENT = TWAI_MODE_NO_ACK,
MODE_SILENT_LOOPBACK = -3,
MODE_LISTEN_ONLY = TWAI_MODE_LISTEN_ONLY, // esp32 specific
} can_mode_t;

typedef enum _filter_mode_t {
FILTER_RAW_SINGLE = 0,
typedef enum {
FILTER_RAW_SINGLE = 1,
FILTER_RAW_DUAL,
FILTER_ADDRESS
} filter_mode_t;

typedef struct _esp32_can_config_t {
typedef enum {
RX_STATE_FIFO_EMPTY = 1,
RX_STATE_MESSAGE_PENDING,
RX_STATE_FIFO_FULL,
RX_STATE_FIFO_OVERFLOW,
} rx_state_t;

typedef enum {
NOT_INITIATED = -1,
STOPPED = TWAI_STATE_STOPPED,
RUNNING = TWAI_STATE_RUNNING,
BUS_OFF = TWAI_STATE_BUS_OFF,
RECOVERING = TWAI_STATE_RECOVERING,
} state_t;

typedef enum {
ERROR = -1,
/*
ERROR_ACTIVE = TWAI_ERROR_ACTIVE,
ERROR_WARNING = TWAI_ERROR_WARNING,
ERROR_PASSIVE = TWAI_ERROR_PASSIVE,
ERROR_BUS_OFF = TWAI_ERROR_BUS_OFF,
*/
} error_state_t;


typedef enum {
RTR = 1,
EXTENDED_ID,
FD_F,
BRS,
} message_flags_t;

typedef enum {
CRC = 1,
FORM,
OVERRUN,
ESI,
} recv_errors_t;

typedef enum {
ARB = 1,
NACK,
ERR,
} send_errors_t;

typedef struct {
twai_timing_config_t timing;
twai_filter_config_t filter;
twai_general_config_t general;
uint32_t baudrate; // bit/s
uint32_t bitrate; // bit/s
bool initialized;
} esp32_can_config_t;

typedef struct _esp32_can_obj_t {
typedef struct {
mp_obj_base_t base;
esp32_can_config_t *config;
mp_obj_t rxcallback;
mp_obj_t rx_callback;
mp_obj_t tx_callback;
TaskHandle_t irq_handler;
byte rx_state;
bool extframe : 1;
Expand All @@ -62,17 +121,12 @@ typedef struct _esp32_can_obj_t {
uint16_t num_error_warning; // FIXME: populate this value somewhere
uint16_t num_error_passive;
uint16_t num_bus_off;
twai_handle_t handle;
twai_status_info_t status;
} esp32_can_obj_t;

typedef enum _rx_state_t {
RX_STATE_FIFO_EMPTY = 0,
RX_STATE_MESSAGE_PENDING,
RX_STATE_FIFO_FULL,
RX_STATE_FIFO_OVERFLOW,
} rx_state_t;

extern const mp_obj_type_t machine_can_type;

#endif // MICROPY_HW_ENABLE_CAN
#endif // MICROPY_PY_MACHINE_CAN

#endif // MICROPY_INCLUDED_ESP32_CAN_H
#endif // MICROPY_INCLUDED_ESP32_MACHINE_CAN_H
1 change: 1 addition & 0 deletions ports/esp32/modmachine.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
#define MICROPY_PY_MACHINE_EXTRA_GLOBALS \
{ MP_ROM_QSTR(MP_QSTR_sleep), MP_ROM_PTR(&machine_lightsleep_obj) }, \
\
{ MP_ROM_QSTR(MP_QSTR_CAN), MP_ROM_PTR(&machine_can_type) }, \
{ MP_ROM_QSTR(MP_QSTR_Timer), MP_ROM_PTR(&machine_timer_type) }, \
MICROPY_PY_MACHINE_SDCARD_ENTRY \
{ MP_ROM_QSTR(MP_QSTR_Pin), MP_ROM_PTR(&machine_pin_type) }, \
Expand Down
1 change: 1 addition & 0 deletions ports/esp32/modmachine.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ typedef enum {
extern const mp_obj_type_t machine_touchpad_type;
extern const mp_obj_type_t machine_dac_type;
extern const mp_obj_type_t machine_sdcard_type;
extern const mp_obj_type_t machine_can_type;

void machine_init(void);
void machine_deinit(void);
Expand Down
3 changes: 2 additions & 1 deletion ports/esp32/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
#define MICROPY_STACK_CHECK_MARGIN (1024)
#define MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF (1)
#define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_MPZ)
#define MICROPY_ERROR_REPORTING (MICROPY_ERROR_REPORTING_NORMAL)
#define MICROPY_ERROR_REPORTING (MICROPY_ERROR_REPORTING_NORMAL+1)
#define MICROPY_WARNINGS (1)
#define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_FLOAT)
#define MICROPY_STREAMS_POSIX_API (1)
Expand Down Expand Up @@ -153,6 +153,7 @@
#define MICROPY_PY_MACHINE_UART_INCLUDEFILE "ports/esp32/machine_uart.c"
#define MICROPY_PY_MACHINE_UART_SENDBREAK (1)
#define MICROPY_PY_MACHINE_UART_IRQ (1)
#define MICROPY_PY_MACHINE_CAN (1)
#define MICROPY_PY_MACHINE_WDT (1)
#define MICROPY_PY_MACHINE_WDT_INCLUDEFILE "ports/esp32/machine_wdt.c"
#define MICROPY_PY_NETWORK (1)
Expand Down

0 comments on commit 8c2132e

Please sign in to comment.