From 0d02cbde46366409aa0822a4fabc6fa5786fbb64 Mon Sep 17 00:00:00 2001 From: EricB Date: Wed, 15 Oct 2025 16:50:18 -0400 Subject: [PATCH 1/7] Add board.c for Waveshare ESP32-C6 Touch LCD Implement display initialization for Waveshare ESP32-C6 Touch LCD. --- .../waveshare_esp32_c6_touch_lcd_1_47/board.c | 111 ++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 ports/espressif/boards/waveshare_esp32_c6_touch_lcd_1_47/board.c diff --git a/ports/espressif/boards/waveshare_esp32_c6_touch_lcd_1_47/board.c b/ports/espressif/boards/waveshare_esp32_c6_touch_lcd_1_47/board.c new file mode 100644 index 0000000000000..73ba16880b29e --- /dev/null +++ b/ports/espressif/boards/waveshare_esp32_c6_touch_lcd_1_47/board.c @@ -0,0 +1,111 @@ +// This file is part of the CircuitPython project: https://circuitpython.org +// +// SPDX-FileCopyrightText: Copyright (c) 2025 Benjamin Shockley +// +// SPDX-License-Identifier: MIT + +#include "supervisor/board.h" +#include "mpconfigboard.h" +#include "shared-bindings/microcontroller/Pin.h" +#include "shared-bindings/busio/SPI.h" +#include "shared-bindings/fourwire/FourWire.h" +#include "shared-module/displayio/__init__.h" +#include "shared-module/displayio/mipi_constants.h" +#include "shared-bindings/board/__init__.h" + +#define DELAY 0x80 +// Board used for this commit : https://www.waveshare.com/wiki/ESP32-C6-Touch-LCD-1.47 + +// Driver is ST7789V3 https://www.waveshare.com/w/upload/a/ad/ST7789VW.pdf +// Display Panel is LBS147TC-IF15 +// 172 X 320 Pixels RGB 18-bit + +// Init Sequence format +// Addr, seq length, data +uint8_t display_init_sequence[] = { + 0x01, 0 | DELAY, 120, // Software Reset - SWRESET + 0x11, 0 | DELAY, 120, // Sleep Out - SLPOUT + 0x13, 0, // Normal Display Mode ON - NORON + 0x36, 1, 0x88, // Memory Data Access Control - MADCTL + //0x3A, 1 | DELAY, 0x05, 10, // Interface Pixel Format - COLMOD 0x05= 16bits/pixel ,0x10 + 0x3A, 1 | DELAY, 0x55, 10, // Interface Pixel Format - COLMOD 0x05= 16bits/pixel ,0x10 + 0xB2, 5, 0x0C, 0x0C, 0x00, 0x33, 0x33, // Porch Setting - PORCTRL + 0xB7, 1, 0x35, // Gate Control - GCTRL + 0xBB, 1, 0x20, // VCOM Setting - VCOMS + 0xC0, 1, 0x2C, // LCM Control - LCMCTRL + 0xC2, 2, 0x01, 0xFF, // VDV and VRH Command Enable - VDVVRHEN + 0xC3, 1, 0x13, // VRH Set - VRHS + 0xC4, 1, 0x20, // VDV Set - VDVS + 0xC6, 1, 0x0F, // Frame Rate CTRL in Normal Mode - FRCTRL2 + 0xD0, 2, 0xA4, 0xA1, // Power Control 1 - PWCTRL1 + 0xE0, 14, 0xF0, 0x00, 0x04, 0x04, 0x04, 0x05, 0x29, 0x33, 0x3E, 0x38, 0x12, 0x12, 0x28, 0x30, // Positive Voltage Gamma Control + 0xE1, 14, 0xF0, 0x07, 0x0A, 0x0D, 0x0B, 0x07, 0x28, 0x33, 0x3E, 0x36, 0x14, 0x14, 0x29, 0x32, // Negative Voltage Gamma Control + 0x21, 0, // Display Inversion On - INVON + 0x29, 0 | DELAY, 255, // Display ON - DISPON +}; +// MADCTL D7->D2 = MY,MX,MV,ML,RGB,MH,0,0 +// MY = PAge Address ORder 0=Top to Bottom 1= Bottom to Top +// MX = Column Address order 0 = left to right 1 = right to left +// MV = Page column order 0 = Normal 1=Reverse +// ML = Line Address Order 0=Top-Bottom 1=Bottom-top +// RGB 0=RGB 1=BGR +// MH = Display Data Latch Data Order 0=Left2Right 1=Right2LEft +// 0x0C 1100 +// +static void display_init(void) { + busio_spi_obj_t *spi = common_hal_board_create_spi(0); + fourwire_fourwire_obj_t *bus = &allocate_display_bus()->fourwire_bus; + bus->base.type = &fourwire_fourwire_type; + + common_hal_fourwire_fourwire_construct( + bus, + spi, + &pin_GPIO15, // DC + &pin_GPIO14, // CS + &pin_GPIO22, // RST changed from 21 to 22 for TOUCH version + 80000000, // baudrate + 0, // polarity + 0 // phase + ); + + busdisplay_busdisplay_obj_t *display = &allocate_display()->display; + display->base.type = &busdisplay_busdisplay_type; + + common_hal_busdisplay_busdisplay_construct( + display, + bus, + 172, // width (after rotation) + 320, // height (after rotation) + 34, // column start + 0, // row start + 0, // rotation + 16, // color depth + false, // grayscale + false, // pixels in a byte share a row. Only valid for depths < 8 + 1, // bytes per cell. Only valid for depths < 8 + false, // reverse_pixels_in_byte. Only valid for depths < 8 + false, // reverse_pixels_in_word + MIPI_COMMAND_SET_COLUMN_ADDRESS, // set column command 0x2A + MIPI_COMMAND_SET_PAGE_ADDRESS, // set row command 0x2B + MIPI_COMMAND_WRITE_MEMORY_START, // write memory command 0x2C + display_init_sequence, + sizeof(display_init_sequence), + &pin_GPIO23, // backlight pin from 22 to 23 for TOUCH version + NO_BRIGHTNESS_COMMAND, + 1.0f, // brightness + false, // single_byte_bounds + false, // data_as_commands + true, // auto_refresh + 60, // native_frames_per_second + true, // backlight_on_high + false, // SH1107_addressing + 50000 // backlight pwm frequency + ); +} + +void board_init(void) { + // Display + display_init(); +} + +// Use the MP_WEAK supervisor/shared/board.c versions of routines not defined here. From 498999d5a9b492789a8d14e24a6c9a6879280362 Mon Sep 17 00:00:00 2001 From: EricB Date: Wed, 15 Oct 2025 16:51:30 -0400 Subject: [PATCH 2/7] Update mpconfigboard.h with setup comment Add comment for Micropython setup specific to Waveshare ESP32-C6 Touch LCD 1.47. --- .../mpconfigboard.h | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 ports/espressif/boards/waveshare_esp32_c6_touch_lcd_1_47/mpconfigboard.h diff --git a/ports/espressif/boards/waveshare_esp32_c6_touch_lcd_1_47/mpconfigboard.h b/ports/espressif/boards/waveshare_esp32_c6_touch_lcd_1_47/mpconfigboard.h new file mode 100644 index 0000000000000..5d7d2acffc809 --- /dev/null +++ b/ports/espressif/boards/waveshare_esp32_c6_touch_lcd_1_47/mpconfigboard.h @@ -0,0 +1,30 @@ +// This file is part of the CircuitPython project: https://circuitpython.org +// +// SPDX-FileCopyrightText: Copyright (c) 2025 Benjamin Shockley +// +// SPDX-License-Identifier: MIT + +#pragma once + +// Micropython setup for https://www.waveshare.com/wiki/ESP32-C6-Touch-LCD-1.47 + +#define MICROPY_HW_BOARD_NAME "Waveshare ESP32-C6 TOUCH LCD 1.47" +#define MICROPY_HW_MCU_NAME "ESP32-C6FH4" + +// I2C +#define CIRCUITPY_BOARD_I2C (1) +#define CIRCUITPY_BOARD_I2C_PIN {{.scl = &pin_GPIO19, .sda = &pin_GPIO18}} + +// SPI +#define CIRCUITPY_BOARD_SPI (1) +#define CIRCUITPY_BOARD_SPI_PIN {{.clock = &pin_GPIO1, .mosi = &pin_GPIO2, .miso = &pin_GPIO3}} + +// TXD0 and RXD0 +#define CIRCUITPY_BOARD_UART (1) +#define CIRCUITPY_BOARD_UART_PIN {{.tx = &pin_GPIO16, .rx = &pin_GPIO17}} + +// For entering safe mode, use BOOT button +#define CIRCUITPY_BOOT_BUTTON (&pin_GPIO9) + +// Explanation of how a user got into safe mode +#define BOARD_USER_SAFE_MODE_ACTION MP_ERROR_TEXT("You pressed the BOOT button at start up.") From 9e05ebddd2fc0305f8c895ce0dbd3fa19672becf Mon Sep 17 00:00:00 2001 From: EricB Date: Wed, 15 Oct 2025 16:52:11 -0400 Subject: [PATCH 3/7] Add configuration for Waveshare ESP32 C6 board --- .../mpconfigboard.mk | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 ports/espressif/boards/waveshare_esp32_c6_touch_lcd_1_47/mpconfigboard.mk diff --git a/ports/espressif/boards/waveshare_esp32_c6_touch_lcd_1_47/mpconfigboard.mk b/ports/espressif/boards/waveshare_esp32_c6_touch_lcd_1_47/mpconfigboard.mk new file mode 100644 index 0000000000000..e838f8ad2e5e8 --- /dev/null +++ b/ports/espressif/boards/waveshare_esp32_c6_touch_lcd_1_47/mpconfigboard.mk @@ -0,0 +1,11 @@ +CIRCUITPY_CREATOR_ID = 0x1BBB0000 +CIRCUITPY_CREATION_ID = 0x00C60002 + +IDF_TARGET = esp32c6 + +CIRCUITPY_ESP_FLASH_MODE = qio +CIRCUITPY_ESP_FLASH_FREQ = 80m +CIRCUITPY_ESP_FLASH_SIZE = 4MB + +# Board was originally defined with a 2MB firmware, almost 2MB user filesystem. Leave it that way. +CIRCUITPY_4MB_FLASH_LARGE_USER_FS_LAYOUT = 1 From 5ccec896de2b6d9d34f081381eb2d73c61f6ddc9 Mon Sep 17 00:00:00 2001 From: EricB Date: Wed, 15 Oct 2025 16:53:15 -0400 Subject: [PATCH 4/7] Add files via upload --- .../waveshare_esp32_c6_touch_lcd_1_47/pins.c | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 ports/espressif/boards/waveshare_esp32_c6_touch_lcd_1_47/pins.c diff --git a/ports/espressif/boards/waveshare_esp32_c6_touch_lcd_1_47/pins.c b/ports/espressif/boards/waveshare_esp32_c6_touch_lcd_1_47/pins.c new file mode 100644 index 0000000000000..a4f2fa6e0b604 --- /dev/null +++ b/ports/espressif/boards/waveshare_esp32_c6_touch_lcd_1_47/pins.c @@ -0,0 +1,80 @@ +// This file is part of the CircuitPython project: https://circuitpython.org +// +// SPDX-FileCopyrightText: Copyright (c) 2025 Benjamin Shockley +// +// SPDX-License-Identifier: MIT + +#include "shared-bindings/board/__init__.h" + +#include "shared-module/displayio/__init__.h" + +static const mp_rom_map_elem_t board_module_globals_table[] = { + CIRCUITPYTHON_BOARD_DICT_STANDARD_ITEMS + + { MP_ROM_QSTR(MP_QSTR_A0), MP_ROM_PTR(&pin_GPIO0) }, + { MP_ROM_QSTR(MP_QSTR_IO0), MP_ROM_PTR(&pin_GPIO0) }, + + { MP_ROM_QSTR(MP_QSTR_A1), MP_ROM_PTR(&pin_GPIO1) }, + { MP_ROM_QSTR(MP_QSTR_IO1), MP_ROM_PTR(&pin_GPIO1) }, + + { MP_ROM_QSTR(MP_QSTR_A2), MP_ROM_PTR(&pin_GPIO2) }, + { MP_ROM_QSTR(MP_QSTR_IO2), MP_ROM_PTR(&pin_GPIO2) }, + + { MP_ROM_QSTR(MP_QSTR_A3), MP_ROM_PTR(&pin_GPIO3) }, + { MP_ROM_QSTR(MP_QSTR_IO3), MP_ROM_PTR(&pin_GPIO3) }, + + { MP_ROM_QSTR(MP_QSTR_A4), MP_ROM_PTR(&pin_GPIO4) }, + { MP_ROM_QSTR(MP_QSTR_IO4), MP_ROM_PTR(&pin_GPIO4) }, + { MP_ROM_QSTR(MP_QSTR_SD_CS), MP_ROM_PTR(&pin_GPIO4) }, + + { MP_ROM_QSTR(MP_QSTR_A5), MP_ROM_PTR(&pin_GPIO5) }, + { MP_ROM_QSTR(MP_QSTR_IO5), MP_ROM_PTR(&pin_GPIO5) }, + { MP_ROM_QSTR(MP_QSTR_IMU_INT1), MP_ROM_PTR(&pin_GPIO5) }, + + + { MP_ROM_QSTR(MP_QSTR_MISO), MP_ROM_PTR(&pin_GPIO3) }, + + { MP_ROM_QSTR(MP_QSTR_IO6), MP_ROM_PTR(&pin_GPIO6) }, + { MP_ROM_QSTR(MP_QSTR_IMU_INT2), MP_ROM_PTR(&pin_GPIO6) }, + + + { MP_ROM_QSTR(MP_QSTR_IO7), MP_ROM_PTR(&pin_GPIO7) }, + + { MP_ROM_QSTR(MP_QSTR_IO8), MP_ROM_PTR(&pin_GPIO8) }, + + { MP_ROM_QSTR(MP_QSTR_BUTTON), MP_ROM_PTR(&pin_GPIO9) }, + { MP_ROM_QSTR(MP_QSTR_IO9), MP_ROM_PTR(&pin_GPIO9) }, + + { MP_ROM_QSTR(MP_QSTR_IO18), MP_ROM_PTR(&pin_GPIO18) }, + { MP_ROM_QSTR(MP_QSTR_SDA), MP_ROM_PTR(&pin_GPIO18) }, + + { MP_ROM_QSTR(MP_QSTR_IO19), MP_ROM_PTR(&pin_GPIO19) }, + { MP_ROM_QSTR(MP_QSTR_SCL), MP_ROM_PTR(&pin_GPIO19) }, + + { MP_ROM_QSTR(MP_QSTR_IO20), MP_ROM_PTR(&pin_GPIO20) }, + + { MP_ROM_QSTR(MP_QSTR_TOUCH_RST), MP_ROM_PTR(&pin_GPIO20) }, + { MP_ROM_QSTR(MP_QSTR_TOUCH_INT), MP_ROM_PTR(&pin_GPIO21) }, + + + { MP_ROM_QSTR(MP_QSTR_IO23), MP_ROM_PTR(&pin_GPIO23) }, + + { MP_ROM_QSTR(MP_QSTR_RX), MP_ROM_PTR(&pin_GPIO17) }, + { MP_ROM_QSTR(MP_QSTR_IO17), MP_ROM_PTR(&pin_GPIO17) }, + + { MP_ROM_QSTR(MP_QSTR_TX), MP_ROM_PTR(&pin_GPIO16) }, + { MP_ROM_QSTR(MP_QSTR_IO16), MP_ROM_PTR(&pin_GPIO16) }, + + { MP_ROM_QSTR(MP_QSTR_TFT_CS), MP_ROM_PTR(&pin_GPIO14) }, + { MP_ROM_QSTR(MP_QSTR_TFT_DC), MP_ROM_PTR(&pin_GPIO15) }, + { MP_ROM_QSTR(MP_QSTR_TFT_RESET), MP_ROM_PTR(&pin_GPIO22) }, + { MP_ROM_QSTR(MP_QSTR_TFT_BACKLIGHT), MP_ROM_PTR(&pin_GPIO21) }, + + //{ MP_ROM_QSTR(MP_QSTR_NEOPIXEL), MP_ROM_PTR(&pin_GPIO8) }, + { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) }, + { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&board_spi_obj) }, + { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&board_uart_obj) }, + + { MP_ROM_QSTR(MP_QSTR_DISPLAY), MP_ROM_PTR(&displays[0].display)} +}; +MP_DEFINE_CONST_DICT(board_module_globals, board_module_globals_table); From 4cfda4d44133c50569c88da3d990786d6e305785 Mon Sep 17 00:00:00 2001 From: EricB Date: Wed, 15 Oct 2025 16:54:15 -0400 Subject: [PATCH 5/7] Add files via upload --- .../waveshare_esp32_c6_touch_lcd_1_47/sdkconfig | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 ports/espressif/boards/waveshare_esp32_c6_touch_lcd_1_47/sdkconfig diff --git a/ports/espressif/boards/waveshare_esp32_c6_touch_lcd_1_47/sdkconfig b/ports/espressif/boards/waveshare_esp32_c6_touch_lcd_1_47/sdkconfig new file mode 100644 index 0000000000000..e962866216039 --- /dev/null +++ b/ports/espressif/boards/waveshare_esp32_c6_touch_lcd_1_47/sdkconfig @@ -0,0 +1,14 @@ +# +# Espressif IoT Development Framework Configuration +# +# +# Component config +# +# +# LWIP +# +# end of LWIP + +# end of Component config + +# end of Espressif IoT Development Framework Configuration From 1c1bb91d0aea636a4ac5c21e363a3c547b190065 Mon Sep 17 00:00:00 2001 From: EricB Date: Wed, 15 Oct 2025 17:09:48 -0400 Subject: [PATCH 6/7] Add twai_driver_handle and instance to CAN object Added twai_driver_handle and instance fields to canio_can_obj_t structure. --- ports/espressif/common-hal/canio/CAN.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ports/espressif/common-hal/canio/CAN.h b/ports/espressif/common-hal/canio/CAN.h index aa0bf662e906b..1589924d4895c 100644 --- a/ports/espressif/common-hal/canio/CAN.h +++ b/ports/espressif/common-hal/canio/CAN.h @@ -26,4 +26,6 @@ typedef struct canio_can_obj { bool silent : 1; bool auto_restart : 1; bool fifo_in_use : 1; + twai_handle_t *twai_driver_handle; + int instance; /* instance number to ease deinit ... could be taken from twai_driver_handle */ } canio_can_obj_t; From 0afdf9c59bc3e9d74a34fdc9f5b630068f37bf73 Mon Sep 17 00:00:00 2001 From: EricB Date: Wed, 15 Oct 2025 17:11:54 -0400 Subject: [PATCH 7/7] Refactor CAN driver to support multiple instances --- ports/espressif/common-hal/canio/CAN.c | 93 +++++++++++++++++++------- 1 file changed, 69 insertions(+), 24 deletions(-) diff --git a/ports/espressif/common-hal/canio/CAN.c b/ports/espressif/common-hal/canio/CAN.c index 941e454b124b5..8db93b3acfb23 100644 --- a/ports/espressif/common-hal/canio/CAN.c +++ b/ports/espressif/common-hal/canio/CAN.c @@ -16,7 +16,15 @@ #include "hal/twai_types.h" -static bool reserved_can; +static byte reserved_can_instance; /* Bit assigned instances 0x01 (CAN0) or 0x02 (CAN1) 0x00-NoneAssigned 0x03-Both Assigned*/ + +#define TWAI0 REG_TWAI_BASE(0) /**< \brief (CAN0) APB Base Address */ +#define TWAI1 REG_TWAI_BASE(1) /**< \brief (CAN1) APB Base Address */ +#define TWAI_INST_NUM 2 /**< \brief (CAN) Number of instances */ + +static twai_handle_t twai_bus_0; +static twai_handle_t twai_bus_1; +static twai_handle_t *const twai_insts[TWAI_INST_NUM] = { &twai_bus_0, &twai_bus_1 }; static twai_timing_config_t get_t_config(int baudrate) { switch (baudrate) { @@ -100,7 +108,7 @@ static twai_timing_config_t get_t_config(int baudrate) { void common_hal_canio_can_construct(canio_can_obj_t *self, const mcu_pin_obj_t *tx, const mcu_pin_obj_t *rx, int baudrate, bool loopback, bool silent) { #define DIV_ROUND(a, b) (((a) + (b) / 2) / (b)) #define DIV_ROUND_UP(a, b) (((a) + (b) - 1) / (b)) - if (reserved_can) { + if (reserved_can_instance==3) { mp_raise_ValueError(MP_ERROR_TEXT("All CAN peripherals are in use")); } @@ -108,10 +116,28 @@ void common_hal_canio_can_construct(canio_can_obj_t *self, const mcu_pin_obj_t * mp_raise_ValueError(MP_ERROR_TEXT("loopback + silent mode not supported by peripheral")); } + byte this_can_instance=0; + if (reserved_can_instance==0x00){ /* None assigned */ + reserved_can_instance=0x01; /* First instance is assigned*/ + this_can_instance=0; /* Idx of selected instance */ + } + else if (reserved_can_instance==0x01) /* First instance allready assigned */ + { + reserved_can_instance=0x03; /* Second instance also assigned */ + this_can_instance=1; /* Idx of selected instance */ + + }else if (reserved_can_instance==0x02) /* Second instance is assigned */ + { + reserved_can_instance=0x03; /* first instance also assigned*/ + this_can_instance=0; /* Idx of selected instance */ + } + twai_timing_config_t t_config = get_t_config(baudrate); - twai_general_config_t g_config = TWAI_GENERAL_CONFIG_DEFAULT(-1, -1, TWAI_MODE_NORMAL); + twai_general_config_t g_config = TWAI_GENERAL_CONFIG_DEFAULT_V2(this_can_instance,-1, -1, TWAI_MODE_NORMAL); g_config.tx_io = tx->number; g_config.rx_io = rx->number; + g_config.controller_id = this_can_instance; /* To ease later de-initing of proper instance */ + if (loopback) { g_config.mode = TWAI_MODE_NO_ACK; } @@ -120,8 +146,7 @@ void common_hal_canio_can_construct(canio_can_obj_t *self, const mcu_pin_obj_t * } twai_filter_config_t f_config = TWAI_FILTER_CONFIG_ACCEPT_ALL(); - - esp_err_t result = twai_driver_install(&g_config, &t_config, &f_config); + esp_err_t result = twai_driver_install_v2(&g_config, &t_config, &f_config , twai_insts[this_can_instance]); if (result == ESP_ERR_NO_MEM) { mp_raise_msg(&mp_type_MemoryError, MP_ERROR_TEXT("ESP-IDF memory allocation failed")); } else if (result == ESP_ERR_INVALID_ARG) { @@ -129,8 +154,8 @@ void common_hal_canio_can_construct(canio_can_obj_t *self, const mcu_pin_obj_t * } else if (result != ESP_OK) { mp_raise_OSError_msg_varg(MP_ERROR_TEXT("twai_driver_install returned esp-idf error #%d"), (int)result); } - - result = twai_start(); + + result = twai_start_v2(*twai_insts[this_can_instance]); if (result != ESP_OK) { mp_raise_OSError_msg_varg(MP_ERROR_TEXT("twai_start returned esp-idf error #%d"), (int)result); } @@ -144,9 +169,15 @@ void common_hal_canio_can_construct(canio_can_obj_t *self, const mcu_pin_obj_t * claim_pin(tx); claim_pin(rx); - reserved_can = true; + self->twai_driver_handle = twai_insts[this_can_instance]; + self->instance = this_can_instance; + + } + + + bool common_hal_canio_can_loopback_get(canio_can_obj_t *self) { return self->loopback; } @@ -157,19 +188,21 @@ int common_hal_canio_can_baudrate_get(canio_can_obj_t *self) { int common_hal_canio_can_transmit_error_count_get(canio_can_obj_t *self) { twai_status_info_t info; - twai_get_status_info(&info); + twai_get_status_info_v2( *self->twai_driver_handle,&info); return info.tx_error_counter; } int common_hal_canio_can_receive_error_count_get(canio_can_obj_t *self) { twai_status_info_t info; - twai_get_status_info(&info); + twai_get_status_info_v2( *self->twai_driver_handle,&info); return info.rx_error_counter; } + canio_bus_state_t common_hal_canio_can_state_get(canio_can_obj_t *self) { twai_status_info_t info; - twai_get_status_info(&info); + + twai_get_status_info_v2( *self->twai_driver_handle,&info); if (info.state == TWAI_STATE_BUS_OFF || info.state == TWAI_STATE_RECOVERING) { return BUS_STATE_OFF; } @@ -182,29 +215,30 @@ canio_bus_state_t common_hal_canio_can_state_get(canio_can_obj_t *self) { return BUS_STATE_ERROR_ACTIVE; } -static void can_restart(void) { + +static void can_restart(canio_can_obj_t *self) { twai_status_info_t info; - twai_get_status_info(&info); + twai_get_status_info_v2(*self->twai_driver_handle, &info); if (info.state != TWAI_STATE_BUS_OFF) { return; } - twai_initiate_recovery(); + twai_initiate_recovery_v2(*self->twai_driver_handle); // wait 100ms (hard coded for now) for bus to recover uint64_t deadline = port_get_raw_ticks(NULL) + 100; do { - twai_get_status_info(&info); + twai_get_status_info_v2(*self->twai_driver_handle,&info); } while (port_get_raw_ticks(NULL) < deadline && (info.state == TWAI_STATE_BUS_OFF || info.state == TWAI_STATE_RECOVERING)); } static void canio_maybe_auto_restart(canio_can_obj_t *self) { if (self->auto_restart) { - can_restart(); + can_restart(self); } } void common_hal_canio_can_restart(canio_can_obj_t *self) { if (!common_hal_canio_can_auto_restart_get(self)) { - can_restart(); + can_restart(self); } } @@ -217,6 +251,9 @@ void common_hal_canio_can_auto_restart_set(canio_can_obj_t *self, bool value) { canio_maybe_auto_restart(self); } + + + void common_hal_canio_can_send(canio_can_obj_t *self, mp_obj_t message_in) { canio_maybe_auto_restart(self); canio_message_obj_t *message = message_in; @@ -232,7 +269,7 @@ void common_hal_canio_can_send(canio_can_obj_t *self, mp_obj_t message_in) { memcpy(message_out.data, message->data, message->size); } // Allow transmission to occur in background - twai_transmit(&message_out, 0); + twai_transmit_v2(*self->twai_driver_handle,&message_out, 0); } bool common_hal_canio_can_silent_get(canio_can_obj_t *self) { @@ -251,18 +288,26 @@ void common_hal_canio_can_check_for_deinit(canio_can_obj_t *self) { void common_hal_canio_can_deinit(canio_can_obj_t *self) { if (self->tx_pin) { - (void)twai_stop(); - (void)twai_driver_uninstall(); + (void)twai_stop_v2(*self->twai_driver_handle); + (void)twai_driver_uninstall_v2(*self->twai_driver_handle); reset_pin_number(self->tx_pin->number); reset_pin_number(self->rx_pin->number); - reserved_can = false; + + if (self->instance==0){ + reserved_can_instance&=0x02; /* free first instance - clear 1st bit */ + }else if (self->instance==1){ + reserved_can_instance&=0x01; /* free second instance - clear 2nd bit */ + } + } self->tx_pin = NULL; self->rx_pin = NULL; } void common_hal_canio_reset(void) { - (void)twai_stop(); - (void)twai_driver_uninstall(); - reserved_can = false; + (void)twai_stop_v2(*twai_insts[0]); + (void)twai_driver_uninstall_v2(*twai_insts[0]); + (void)twai_stop_v2(*twai_insts[1]); + (void)twai_driver_uninstall_v2(*twai_insts[1]); + reserved_can_instance=0; /* unassign both bits */ }