Skip to content

Commit f88e04f

Browse files
committed
Update machine_can.c
1 parent d547be4 commit f88e04f

File tree

3 files changed

+63
-51
lines changed

3 files changed

+63
-51
lines changed

ports/esp32/machine_can.c

+60-49
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
#include "esp_err.h"
4040
#include "esp_log.h"
4141
#include "soc/soc_caps.h"
42-
#include "soc/dport_reg.h"
42+
//#include "soc/dport_reg.h"
4343
#include "soc/clk_tree_defs.h"
4444
#include "soc/twai_periph.h"
4545
#include "hal/twai_types.h"
@@ -51,6 +51,8 @@
5151

5252
#if MICROPY_PY_MACHINE_CAN
5353

54+
#define debug_printf(...) mp_printf(&mp_plat_print, __VA_ARGS__); mp_printf(&mp_plat_print, " | %d at %s\n", __LINE__, __FILE__);
55+
5456
// Default bitrate: 500kb
5557
#define CAN_TASK_PRIORITY (ESP_TASK_PRIO_MIN + 1)
5658
#define CAN_TASK_STACK_SIZE (1024)
@@ -60,16 +62,6 @@
6062
#define CAN_DEFAULT_BS2 (4)
6163
#define CAN_MAX_DATA_FRAME (8)
6264

63-
// INTERNAL Deinitialize can
64-
void can_deinit(const esp32_can_obj_t *self) {
65-
check_esp_err(twai_stop_v2(self->handle));
66-
check_esp_err(twai_driver_uninstall_v2(self->handle));
67-
if (self->irq_handler != NULL) {
68-
vTaskDelete(self->irq_handler);
69-
}
70-
self->config->initialized = false;
71-
}
72-
7365
// #define TWAI_TIMING_CONFIG_20KBITS() {.clk_src = TWAI_CLK_SRC_DEFAULT, .quanta_resolution_hz = 400000, .brp = 0, .tseg_1 = 15, .tseg_2 = 4, .sjw = 3, .triple_sampling = false}
7466

7567
// static const twai_timing_config_t t_config = TWAI_TIMING_CONFIG_500KBITS();
@@ -85,9 +77,22 @@ esp32_can_config_t can_config = {
8577

8678
static esp32_can_obj_t esp32_can_obj = {
8779
{&machine_can_type},
88-
.config = &can_config
80+
.config = &can_config,
81+
.handle = NULL,
8982
};
9083

84+
// INTERNAL Deinitialize can
85+
void can_deinit(const esp32_can_obj_t *self) {
86+
if (self->handle) {
87+
check_esp_err(twai_stop_v2(self->handle));
88+
check_esp_err(twai_driver_uninstall_v2(self->handle));
89+
}
90+
if (self->irq_handler != NULL) {
91+
vTaskDelete(self->irq_handler);
92+
}
93+
self->config->initialized = false;
94+
}
95+
9196
static void esp32_can_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
9297
esp32_can_obj_t *self = MP_OBJ_TO_PTR(self_in);
9398

@@ -107,7 +112,7 @@ static void esp32_can_print(const mp_print_t *print, mp_obj_t self_in, mp_print_
107112
mode = MP_QSTR_UNKNOWN;
108113
break;
109114
}
110-
mp_printf(print, "CAN(tx=%u, rx=%u, bitrate=%ukb, mode=%q, loopback=%u, extframe=%u)",
115+
mp_printf(print, "CAN(tx=%u, rx=%u, bitrate=%u, mode=%q, loopback=%u, extframe=%u)",
111116
self->config->general.tx_io,
112117
self->config->general.rx_io,
113118
self->config->bitrate,
@@ -124,11 +129,11 @@ static void esp32_can_irq_task(void *self_in) {
124129
esp32_can_obj_t *self = MP_OBJ_TO_PTR(self_in);
125130
uint32_t alerts;
126131

127-
twai_reconfigure_alerts_v2(self->handle, TWAI_ALERT_ALL,
132+
check_esp_err(twai_reconfigure_alerts_v2(self->handle, TWAI_ALERT_ALL,
128133
// TWAI_ALERT_RX_DATA | TWAI_ALERT_RX_QUEUE_FULL | TWAI_ALERT_BUS_OFF | TWAI_ALERT_ERR_PASS |
129134
// TWAI_ALERT_ABOVE_ERR_WARN | TWAI_ALERT_TX_FAILED | TWAI_ALERT_TX_SUCCESS | TWAI_ALERT_BUS_RECOVERED,
130135
NULL
131-
);
136+
));
132137

133138
while (1) {
134139
check_esp_err(twai_read_alerts_v2(self->handle, &alerts, portMAX_DELAY));
@@ -151,22 +156,22 @@ static void esp32_can_irq_task(void *self_in) {
151156
self->bus_recovery_success = 1;
152157
}
153158

154-
if (self->rxcallback != mp_const_none) {
159+
if (self->rx_callback != mp_const_none) {
155160
if (alerts & TWAI_ALERT_RX_DATA) {
156161
check_esp_err(twai_get_status_info_v2(self->handle, &self->status));
157162
uint32_t msgs_to_rx = self->status.msgs_to_rx;
158163

159164
if (msgs_to_rx == 1) {
160165
// first message in queue
161-
mp_sched_schedule(self->rxcallback, MP_OBJ_NEW_SMALL_INT(0));
166+
mp_sched_schedule(self->rx_callback, MP_OBJ_NEW_SMALL_INT(0));
162167
} else if (msgs_to_rx >= self->config->general.rx_queue_len) {
163168
// queue is full
164-
mp_sched_schedule(self->rxcallback, MP_OBJ_NEW_SMALL_INT(1));
169+
mp_sched_schedule(self->rx_callback, MP_OBJ_NEW_SMALL_INT(1));
165170
}
166171
}
167172
if (alerts & TWAI_ALERT_RX_QUEUE_FULL) {
168173
// queue overflow
169-
mp_sched_schedule(self->rxcallback, MP_OBJ_NEW_SMALL_INT(2));
174+
mp_sched_schedule(self->rx_callback, MP_OBJ_NEW_SMALL_INT(2));
170175
}
171176
}
172177
}
@@ -235,72 +240,73 @@ static mp_obj_t esp32_can_init_helper(esp32_can_obj_t *self, size_t n_args, cons
235240
.triple_sampling = false
236241
};
237242
break;
238-
#if 1 // #ifdef TWAI_TIMING_CONFIG_1KBITS
243+
#ifdef TWAI_TIMING_CONFIG_1KBITS
239244
case 1000:
240245
self->config->timing = (twai_timing_config_t)TWAI_TIMING_CONFIG_1KBITS();
241246
break;
242247
#endif
243-
#if 1 // #ifdef TWAI_TIMING_CONFIG_5KBITS
248+
#ifdef TWAI_TIMING_CONFIG_5KBITS
244249
case 5000:
245250
self->config->timing = (twai_timing_config_t)TWAI_TIMING_CONFIG_5KBITS();
246251
break;
247252
#endif
248-
#if 1 // #ifdef TWAI_TIMING_CONFIG_10KBITS
253+
#ifdef TWAI_TIMING_CONFIG_10KBITS
249254
case 10000:
250255
self->config->timing = (twai_timing_config_t)TWAI_TIMING_CONFIG_10KBITS();
251256
break;
252257
#endif
253-
#if 1 // #ifdef TWAI_TIMING_CONFIG_12_5KBITS
258+
#ifdef TWAI_TIMING_CONFIG_12_5KBITS
254259
case 12500:
255260
self->config->timing = (twai_timing_config_t)TWAI_TIMING_CONFIG_12_5KBITS();
256261
break;
257262
#endif
258-
#if 1 // #ifdef TWAI_TIMING_CONFIG_16KBITS
263+
#ifdef TWAI_TIMING_CONFIG_16KBITS
259264
case 16000:
260265
self->config->timing = (twai_timing_config_t)TWAI_TIMING_CONFIG_16KBITS();
261266
break;
262267
#endif
263-
#if 1 // #ifdef TWAI_TIMING_CONFIG_20KBITS
268+
#ifdef TWAI_TIMING_CONFIG_20KBITS
264269
case 20000:
265270
self->config->timing = (twai_timing_config_t)TWAI_TIMING_CONFIG_20KBITS();
266271
break;
267272
#endif
268-
#if 1 // #ifdef TWAI_TIMING_CONFIG_25KBITS
273+
#ifdef TWAI_TIMING_CONFIG_25KBITS
269274
case 25000:
270275
self->config->timing = (twai_timing_config_t)TWAI_TIMING_CONFIG_25KBITS();
271276
break;
272277
#endif
273-
#if 1 // #ifdef TWAI_TIMING_CONFIG_50KBITS
278+
#ifdef TWAI_TIMING_CONFIG_50KBITS
274279
case 50000:
275280
self->config->timing = (twai_timing_config_t)TWAI_TIMING_CONFIG_50KBITS();
276281
break;
277282
#endif
278-
#if 1 // #ifdef TWAI_TIMING_CONFIG_100KBITS
283+
#ifdef TWAI_TIMING_CONFIG_100KBITS
279284
case 100000:
280285
self->config->timing = (twai_timing_config_t)TWAI_TIMING_CONFIG_100KBITS();
281286
break;
282287
#endif
283-
#if 1 // #ifdef TWAI_TIMING_CONFIG_125KBITS
288+
#ifdef TWAI_TIMING_CONFIG_125KBITS
284289
case 125000:
285290
self->config->timing = (twai_timing_config_t)TWAI_TIMING_CONFIG_125KBITS();
286291
break;
287292
#endif
288-
#if 1 // #ifdef TWAI_TIMING_CONFIG_250KBITS
293+
#ifdef TWAI_TIMING_CONFIG_250KBITS
289294
case 250000:
290295
self->config->timing = (twai_timing_config_t)TWAI_TIMING_CONFIG_250KBITS();
291296
break;
292297
#endif
293-
#if 1 // #ifdef TWAI_TIMING_CONFIG_500KBITS
298+
#ifdef TWAI_TIMING_CONFIG_500KBITS
294299
case 500000:
295300
self->config->timing = (twai_timing_config_t)TWAI_TIMING_CONFIG_500KBITS();
301+
debug_printf("A");
296302
break;
297303
#endif
298-
#if 1 // #ifdef TWAI_TIMING_CONFIG_800KBITS
304+
#ifdef TWAI_TIMING_CONFIG_800KBITS
299305
case 800000:
300306
self->config->timing = (twai_timing_config_t)TWAI_TIMING_CONFIG_800KBITS();
301307
break;
302308
#endif
303-
#if 1 // #ifdef TWAI_TIMING_CONFIG_1MBITS
309+
#ifdef TWAI_TIMING_CONFIG_1MBITS
304310
case 1000000:
305311
self->config->timing = (twai_timing_config_t)TWAI_TIMING_CONFIG_1MBITS();
306312
break;
@@ -317,7 +323,7 @@ static mp_obj_t esp32_can_init_helper(esp32_can_obj_t *self, size_t n_args, cons
317323
mp_raise_msg(&mp_type_RuntimeError, MP_ERROR_TEXT("failed to create can irq task handler"));
318324
}
319325
self->config->initialized = true;
320-
326+
debug_printf("B");
321327
return mp_const_none;
322328
}
323329

@@ -332,7 +338,7 @@ static mp_obj_t esp32_can_make_new(const mp_obj_type_t *type, size_t n_args, siz
332338

333339
// work out port
334340
mp_uint_t can_idx = mp_obj_get_int(args[0]);
335-
if (can_idx > SOC_TWAI_CONTROLLER_NUM) {
341+
if (can_idx > SOC_TWAI_CONTROLLER_NUM - 1) {
336342
mp_raise_msg_varg(&mp_type_ValueError, "out of CAN controllers:%d", SOC_TWAI_CONTROLLER_NUM);
337343
}
338344

@@ -344,7 +350,7 @@ static mp_obj_t esp32_can_make_new(const mp_obj_type_t *type, size_t n_args, siz
344350
// this can only be done if the hardware is in init mode
345351
can_deinit(self);
346352
}
347-
self->rxcallback = mp_const_none;
353+
self->rx_callback = mp_const_none;
348354
self->irq_handler = NULL;
349355
self->rx_state = RX_STATE_FIFO_EMPTY;
350356

@@ -365,7 +371,6 @@ static mp_obj_t esp32_can_init(size_t n_args, const mp_obj_t *pos_args, mp_map_t
365371
mp_raise_msg(&mp_type_RuntimeError, "Device is already initialized");
366372
return mp_const_none;
367373
}
368-
369374
return esp32_can_init_helper(self, n_args - 1, pos_args + 1, kw_args);
370375
}
371376
static MP_DEFINE_CONST_FUN_OBJ_KW(esp32_can_init_obj, 4, esp32_can_init);
@@ -611,7 +616,7 @@ static mp_obj_t esp32_can_recv(size_t n_args, const mp_obj_t *pos_args, mp_map_t
611616
// Return the result
612617
return ret_obj;
613618
}
614-
static MP_DEFINE_CONST_FUN_OBJ_KW(esp32_can_recv_obj, 0, esp32_can_recv);
619+
static MP_DEFINE_CONST_FUN_OBJ_KW(esp32_can_recv_obj, 1, esp32_can_recv);
615620

616621
// Clear filters setting
617622
static mp_obj_t esp32_can_clearfilter(mp_obj_t self_in) {
@@ -712,38 +717,44 @@ static mp_obj_t esp32_can_set_filters(size_t n_args, const mp_obj_t *pos_args, m
712717
self->config->filter.acceptance_mask |= mask;
713718
}
714719
// Apply filter
715-
check_esp_err(twai_stop_v2(self->handle));
716-
check_esp_err(twai_driver_uninstall_v2(self->handle));
720+
if (self->handle) {
721+
check_esp_err(twai_stop_v2(self->handle));
722+
check_esp_err(twai_driver_uninstall_v2(self->handle));
723+
}
717724
check_esp_err(twai_driver_install_v2(
718725
&self->config->general,
719726
&self->config->timing,
720727
&self->config->filter,
721728
&self->handle
722729
));
723730
check_esp_err(twai_start_v2(self->handle));
724-
725731
return mp_const_none;
726732
}
727733
static MP_DEFINE_CONST_FUN_OBJ_KW(esp32_can_set_filters_obj, 1, esp32_can_set_filters);
728734

729-
// rxcallback(callable)
735+
// rx_callback(callable)
736+
// static mp_obj_t esp32_hw_can_rxcallback(mp_obj_t self_in, mp_obj_t callback_in) {
737+
// esp32_can_obj_t *self = MP_OBJ_TO_PTR(self_in);
730738
// CAN.irq_recv(callback, hard=False)
731739
//static mp_obj_t esp32_can_init(size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
732740
//static mp_obj_t esp32_can_irq_recv(mp_obj_t self_in, mp_obj_t callback_in) {
733-
static mp_obj_t esp32_can_irq_recv(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
734-
esp32_can_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
735-
mp_obj_t callback_in = NULL;
741+
//static mp_obj_t esp32_can_irq_recv(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
742+
static mp_obj_t esp32_can_irq_recv(mp_obj_t self_in, mp_obj_t callback_in) {
743+
//esp32_can_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
744+
esp32_can_obj_t *self = MP_OBJ_TO_PTR(self_in);
745+
// mp_obj_t callback_in = NULL;
736746
if (callback_in == mp_const_none) {
737747
// disable callback
738-
self->rxcallback = mp_const_none;
748+
self->rx_callback = mp_const_none;
739749
} else if (mp_obj_is_callable(callback_in)) {
740750
// set up interrupt
741-
self->rxcallback = callback_in;
751+
self->rx_callback = callback_in;
742752
}
743-
744753
return mp_const_none;
745754
}
746-
static MP_DEFINE_CONST_FUN_OBJ_KW(esp32_can_irq_recv_obj, 3, esp32_can_irq_recv);
755+
static MP_DEFINE_CONST_FUN_OBJ_2(esp32_can_irq_recv_obj, esp32_can_irq_recv);
756+
//static MP_DEFINE_CONST_FUN_OBJ_KW(esp32_can_irq_recv_obj, 3, esp32_can_irq_recv);
757+
//static MP_DEFINE_CONST_FUN_OBJ_2(esp32_hw_can_rxcallback_obj, esp32_hw_can_rxcallback);
747758

748759
// CAN.irq_send(callback, hard=False)
749760
static mp_obj_t esp32_can_irq_send(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {

ports/esp32/machine_can.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ typedef struct {
110110
typedef struct {
111111
mp_obj_base_t base;
112112
esp32_can_config_t *config;
113-
mp_obj_t rxcallback;
113+
mp_obj_t rx_callback;
114+
mp_obj_t tx_callback;
114115
TaskHandle_t irq_handler;
115116
byte rx_state;
116117
bool extframe : 1;

ports/esp32/mpconfigport.h

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

0 commit comments

Comments
 (0)