Skip to content

Commit 8c2132e

Browse files
committed
port/esp32: CAN_v2. WIP
Signed-off-by: Ihor Nehrutsa <[email protected]>
1 parent 5e7e2db commit 8c2132e

File tree

7 files changed

+775
-207
lines changed

7 files changed

+775
-207
lines changed

docs/library/machine.CAN.rst

+408
Large diffs are not rendered by default.

ports/esp32/esp32_common.cmake

+1
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

+288-186
Large diffs are not rendered by default.

ports/esp32/machine_can.h

+74-20
Original file line numberDiff line numberDiff line change
@@ -23,36 +23,95 @@
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
36+
37+
//#define DEVICE_NAME "CAN"
3338

34-
#if MICROPY_HW_ENABLE_CAN
39+
#define CAN_MODE_SILENT_LOOPBACK (0x10)
3540

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

38-
typedef enum _filter_mode_t {
39-
FILTER_RAW_SINGLE = 0,
50+
typedef enum {
51+
FILTER_RAW_SINGLE = 1,
4052
FILTER_RAW_DUAL,
4153
FILTER_ADDRESS
4254
} filter_mode_t;
4355

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

52-
typedef struct _esp32_can_obj_t {
110+
typedef struct {
53111
mp_obj_base_t base;
54112
esp32_can_config_t *config;
55-
mp_obj_t rxcallback;
113+
mp_obj_t rx_callback;
114+
mp_obj_t tx_callback;
56115
TaskHandle_t irq_handler;
57116
byte rx_state;
58117
bool extframe : 1;
@@ -62,17 +121,12 @@ typedef struct _esp32_can_obj_t {
62121
uint16_t num_error_warning; // FIXME: populate this value somewhere
63122
uint16_t num_error_passive;
64123
uint16_t num_bus_off;
124+
twai_handle_t handle;
125+
twai_status_info_t status;
65126
} esp32_can_obj_t;
66127

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-
74128
extern const mp_obj_type_t machine_can_type;
75129

76-
#endif // MICROPY_HW_ENABLE_CAN
130+
#endif // MICROPY_PY_MACHINE_CAN
77131

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

ports/esp32/modmachine.c

+1
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

+1
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

+2-1
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)