Skip to content

Commit bd3d673

Browse files
committed
port/esp32: CAN_v2.
Co-Authored-By: Sveinung Kval Bakken <sveinung.bakken@gmail.com> Signed-off-by: Ihor Nehrutsa <Ihor.Nehrutsa@gmail.com>
1 parent 8c984f1 commit bd3d673

File tree

9 files changed

+800
-232
lines changed

9 files changed

+800
-232
lines changed

docs/library/machine.CAN.rst

Lines changed: 408 additions & 0 deletions
Large diffs are not rendered by default.

docs/library/machine.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ Classes
260260
machine.Signal.rst
261261
machine.ADC.rst
262262
machine.ADCBlock.rst
263+
machine.CAN.rst
263264
machine.PWM.rst
264265
machine.UART.rst
265266
machine.SPI.rst

examples/esp32_can.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ def send_and_check(can_bus, name, id, expected_result=True, extended=False):
5151
extframe=True,
5252
mode=CAN.SILENT_LOOPBACK,
5353
baudrate=CAN.BAUDRATE_500k,
54-
tx_io=18,
55-
rx_io=19,
54+
tx=18,
55+
rx=19,
5656
auto_restart=False,
5757
)
5858

ports/esp32/esp32_common.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ list(APPEND MICROPY_SOURCE_PORT
104104
fatfs_port.c
105105
help.c
106106
machine_bitstream.c
107+
machine_can.c
107108
machine_timer.c
108109
machine_pin.c
109110
machine_touchpad.c

ports/esp32/machine_can.c

Lines changed: 312 additions & 209 deletions
Large diffs are not rendered by default.

ports/esp32/machine_can.h

Lines changed: 72 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,36 +23,93 @@
2323
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2424
* THE SOFTWARE.
2525
*/
26-
#ifndef MICROPY_INCLUDED_ESP32_CAN_H
27-
#define MICROPY_INCLUDED_ESP32_CAN_H
28-
26+
#ifndef MICROPY_INCLUDED_ESP32_MACHINE_CAN_H
27+
#define MICROPY_INCLUDED_ESP32_MACHINE_CAN_H
28+
/*
2929
#include "modmachine.h"
3030
#include "freertos/task.h"
3131
32+
#include "mpconfigport.h"
3233
#include "py/obj.h"
34+
*/
35+
#if MICROPY_PY_MACHINE_CAN
3336

34-
#if MICROPY_HW_ENABLE_CAN
37+
#define CAN_MODE_SILENT_LOOPBACK (0x10)
3538

36-
#define DEVICE_NAME "CAN"
39+
typedef enum {
40+
MODE_NORMAL = TWAI_MODE_NORMAL,
41+
MODE_SLEEP = -1,
42+
MODE_LOOPBACK = -2, // TWAI_MODE_NORMAL | CAN_MODE_SILENT_LOOPBACK,
43+
MODE_SILENT = TWAI_MODE_NO_ACK,
44+
MODE_SILENT_LOOPBACK = -3,
45+
MODE_LISTEN_ONLY = TWAI_MODE_LISTEN_ONLY, // esp32 specific
46+
} can_mode_t;
3747

38-
typedef enum _filter_mode_t {
39-
FILTER_RAW_SINGLE = 0,
48+
typedef enum {
49+
FILTER_RAW_SINGLE = 1,
4050
FILTER_RAW_DUAL,
4151
FILTER_ADDRESS
4252
} filter_mode_t;
4353

44-
typedef struct _esp32_can_config_t {
54+
typedef enum {
55+
RX_STATE_FIFO_EMPTY = 1,
56+
RX_STATE_MESSAGE_PENDING,
57+
RX_STATE_FIFO_FULL,
58+
RX_STATE_FIFO_OVERFLOW,
59+
} rx_state_t;
60+
61+
typedef enum {
62+
NOT_INITIATED = TWAI_STATE_STOPPED - 1,
63+
STOPPED = TWAI_STATE_STOPPED,
64+
RUNNING = TWAI_STATE_RUNNING,
65+
BUS_OFF = TWAI_STATE_BUS_OFF,
66+
RECOVERING = TWAI_STATE_RECOVERING,
67+
} state_t;
68+
69+
typedef enum {
70+
ERROR = -1,
71+
/*
72+
ERROR_ACTIVE = TWAI_ERROR_ACTIVE,
73+
ERROR_WARNING = TWAI_ERROR_WARNING,
74+
ERROR_PASSIVE = TWAI_ERROR_PASSIVE,
75+
ERROR_BUS_OFF = TWAI_ERROR_BUS_OFF,
76+
*/
77+
} error_state_t;
78+
79+
80+
typedef enum {
81+
RTR = 1,
82+
EXTENDED_ID,
83+
FD_F,
84+
BRS,
85+
} message_flags_t;
86+
87+
typedef enum {
88+
CRC = 1,
89+
FORM,
90+
OVERRUN,
91+
ESI,
92+
} recv_errors_t;
93+
94+
typedef enum {
95+
ARB = 1,
96+
NACK,
97+
ERR,
98+
} send_errors_t;
99+
100+
typedef struct {
45101
twai_timing_config_t timing;
46102
twai_filter_config_t filter;
47103
twai_general_config_t general;
48-
uint32_t baudrate; // bit/s
104+
uint32_t bitrate; // bit/s
49105
bool initialized;
50106
} esp32_can_config_t;
51107

52-
typedef struct _esp32_can_obj_t {
108+
typedef struct {
53109
mp_obj_base_t base;
54110
esp32_can_config_t *config;
55-
mp_obj_t rxcallback;
111+
mp_obj_t rx_callback;
112+
mp_obj_t tx_callback;
56113
TaskHandle_t irq_handler;
57114
byte rx_state;
58115
bool extframe : 1;
@@ -62,17 +119,12 @@ typedef struct _esp32_can_obj_t {
62119
uint16_t num_error_warning; // FIXME: populate this value somewhere
63120
uint16_t num_error_passive;
64121
uint16_t num_bus_off;
122+
twai_handle_t handle;
123+
twai_status_info_t status;
65124
} esp32_can_obj_t;
66125

67-
typedef enum _rx_state_t {
68-
RX_STATE_FIFO_EMPTY = 0,
69-
RX_STATE_MESSAGE_PENDING,
70-
RX_STATE_FIFO_FULL,
71-
RX_STATE_FIFO_OVERFLOW,
72-
} rx_state_t;
73-
74126
extern const mp_obj_type_t machine_can_type;
75127

76-
#endif // MICROPY_HW_ENABLE_CAN
128+
#endif // MICROPY_PY_MACHINE_CAN
77129

78-
#endif // MICROPY_INCLUDED_ESP32_CAN_H
130+
#endif // MICROPY_INCLUDED_ESP32_MACHINE_CAN_H

ports/esp32/modmachine.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
#define MICROPY_PY_MACHINE_EXTRA_GLOBALS \
5555
{ MP_ROM_QSTR(MP_QSTR_sleep), MP_ROM_PTR(&machine_lightsleep_obj) }, \
5656
\
57+
{ MP_ROM_QSTR(MP_QSTR_CAN), MP_ROM_PTR(&machine_can_type) }, \
5758
{ MP_ROM_QSTR(MP_QSTR_Timer), MP_ROM_PTR(&machine_timer_type) }, \
5859
MICROPY_PY_MACHINE_SDCARD_ENTRY \
5960
{ MP_ROM_QSTR(MP_QSTR_Pin), MP_ROM_PTR(&machine_pin_type) }, \

ports/esp32/modmachine.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ typedef enum {
1212
extern const mp_obj_type_t machine_touchpad_type;
1313
extern const mp_obj_type_t machine_dac_type;
1414
extern const mp_obj_type_t machine_sdcard_type;
15+
extern const mp_obj_type_t machine_can_type;
1516

1617
void machine_init(void);
1718
void machine_deinit(void);

ports/esp32/mpconfigport.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
#define MICROPY_STACK_CHECK_MARGIN (1024)
6363
#define MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF (1)
6464
#define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_MPZ)
65-
#define MICROPY_ERROR_REPORTING (MICROPY_ERROR_REPORTING_NORMAL)
65+
#define MICROPY_ERROR_REPORTING (MICROPY_ERROR_REPORTING_NORMAL+1)
6666
#define MICROPY_WARNINGS (1)
6767
#define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_FLOAT)
6868
#define MICROPY_STREAMS_POSIX_API (1)
@@ -153,6 +153,7 @@
153153
#define MICROPY_PY_MACHINE_UART_INCLUDEFILE "ports/esp32/machine_uart.c"
154154
#define MICROPY_PY_MACHINE_UART_SENDBREAK (1)
155155
#define MICROPY_PY_MACHINE_UART_IRQ (1)
156+
#define MICROPY_PY_MACHINE_CAN (1)
156157
#define MICROPY_PY_MACHINE_WDT (1)
157158
#define MICROPY_PY_MACHINE_WDT_INCLUDEFILE "ports/esp32/machine_wdt.c"
158159
#define MICROPY_PY_NETWORK (1)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy