39
39
#include "esp_err.h"
40
40
#include "esp_log.h"
41
41
#include "soc/soc_caps.h"
42
- #include "soc/dport_reg.h"
42
+ // #include "soc/dport_reg.h"
43
43
#include "soc/clk_tree_defs.h"
44
44
#include "soc/twai_periph.h"
45
45
#include "hal/twai_types.h"
51
51
52
52
#if MICROPY_PY_MACHINE_CAN
53
53
54
+ #define debug_printf (...) mp_printf(&mp_plat_print, __VA_ARGS__); mp_printf(&mp_plat_print, " | %d at %s\n", __LINE__, __FILE__);
55
+
54
56
// Default bitrate: 500kb
55
57
#define CAN_TASK_PRIORITY (ESP_TASK_PRIO_MIN + 1)
56
58
#define CAN_TASK_STACK_SIZE (1024)
60
62
#define CAN_DEFAULT_BS2 (4)
61
63
#define CAN_MAX_DATA_FRAME (8)
62
64
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
-
73
65
// #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}
74
66
75
67
// static const twai_timing_config_t t_config = TWAI_TIMING_CONFIG_500KBITS();
@@ -85,9 +77,22 @@ esp32_can_config_t can_config = {
85
77
86
78
static esp32_can_obj_t esp32_can_obj = {
87
79
{& machine_can_type },
88
- .config = & can_config
80
+ .config = & can_config ,
81
+ .handle = NULL ,
89
82
};
90
83
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
+
91
96
static void esp32_can_print (const mp_print_t * print , mp_obj_t self_in , mp_print_kind_t kind ) {
92
97
esp32_can_obj_t * self = MP_OBJ_TO_PTR (self_in );
93
98
@@ -107,7 +112,7 @@ static void esp32_can_print(const mp_print_t *print, mp_obj_t self_in, mp_print_
107
112
mode = MP_QSTR_UNKNOWN ;
108
113
break ;
109
114
}
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)" ,
111
116
self -> config -> general .tx_io ,
112
117
self -> config -> general .rx_io ,
113
118
self -> config -> bitrate ,
@@ -124,11 +129,11 @@ static void esp32_can_irq_task(void *self_in) {
124
129
esp32_can_obj_t * self = MP_OBJ_TO_PTR (self_in );
125
130
uint32_t alerts ;
126
131
127
- twai_reconfigure_alerts_v2 (self -> handle , TWAI_ALERT_ALL ,
132
+ check_esp_err ( twai_reconfigure_alerts_v2 (self -> handle , TWAI_ALERT_ALL ,
128
133
// TWAI_ALERT_RX_DATA | TWAI_ALERT_RX_QUEUE_FULL | TWAI_ALERT_BUS_OFF | TWAI_ALERT_ERR_PASS |
129
134
// TWAI_ALERT_ABOVE_ERR_WARN | TWAI_ALERT_TX_FAILED | TWAI_ALERT_TX_SUCCESS | TWAI_ALERT_BUS_RECOVERED,
130
135
NULL
131
- );
136
+ )) ;
132
137
133
138
while (1 ) {
134
139
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) {
151
156
self -> bus_recovery_success = 1 ;
152
157
}
153
158
154
- if (self -> rxcallback != mp_const_none ) {
159
+ if (self -> rx_callback != mp_const_none ) {
155
160
if (alerts & TWAI_ALERT_RX_DATA ) {
156
161
check_esp_err (twai_get_status_info_v2 (self -> handle , & self -> status ));
157
162
uint32_t msgs_to_rx = self -> status .msgs_to_rx ;
158
163
159
164
if (msgs_to_rx == 1 ) {
160
165
// 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 ));
162
167
} else if (msgs_to_rx >= self -> config -> general .rx_queue_len ) {
163
168
// 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 ));
165
170
}
166
171
}
167
172
if (alerts & TWAI_ALERT_RX_QUEUE_FULL ) {
168
173
// 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 ));
170
175
}
171
176
}
172
177
}
@@ -235,72 +240,73 @@ static mp_obj_t esp32_can_init_helper(esp32_can_obj_t *self, size_t n_args, cons
235
240
.triple_sampling = false
236
241
};
237
242
break ;
238
- #if 1 // # ifdef TWAI_TIMING_CONFIG_1KBITS
243
+ #ifdef TWAI_TIMING_CONFIG_1KBITS
239
244
case 1000 :
240
245
self -> config -> timing = (twai_timing_config_t )TWAI_TIMING_CONFIG_1KBITS ();
241
246
break ;
242
247
#endif
243
- #if 1 // # ifdef TWAI_TIMING_CONFIG_5KBITS
248
+ #ifdef TWAI_TIMING_CONFIG_5KBITS
244
249
case 5000 :
245
250
self -> config -> timing = (twai_timing_config_t )TWAI_TIMING_CONFIG_5KBITS ();
246
251
break ;
247
252
#endif
248
- #if 1 // # ifdef TWAI_TIMING_CONFIG_10KBITS
253
+ #ifdef TWAI_TIMING_CONFIG_10KBITS
249
254
case 10000 :
250
255
self -> config -> timing = (twai_timing_config_t )TWAI_TIMING_CONFIG_10KBITS ();
251
256
break ;
252
257
#endif
253
- #if 1 // # ifdef TWAI_TIMING_CONFIG_12_5KBITS
258
+ #ifdef TWAI_TIMING_CONFIG_12_5KBITS
254
259
case 12500 :
255
260
self -> config -> timing = (twai_timing_config_t )TWAI_TIMING_CONFIG_12_5KBITS ();
256
261
break ;
257
262
#endif
258
- #if 1 // # ifdef TWAI_TIMING_CONFIG_16KBITS
263
+ #ifdef TWAI_TIMING_CONFIG_16KBITS
259
264
case 16000 :
260
265
self -> config -> timing = (twai_timing_config_t )TWAI_TIMING_CONFIG_16KBITS ();
261
266
break ;
262
267
#endif
263
- #if 1 // # ifdef TWAI_TIMING_CONFIG_20KBITS
268
+ #ifdef TWAI_TIMING_CONFIG_20KBITS
264
269
case 20000 :
265
270
self -> config -> timing = (twai_timing_config_t )TWAI_TIMING_CONFIG_20KBITS ();
266
271
break ;
267
272
#endif
268
- #if 1 // # ifdef TWAI_TIMING_CONFIG_25KBITS
273
+ #ifdef TWAI_TIMING_CONFIG_25KBITS
269
274
case 25000 :
270
275
self -> config -> timing = (twai_timing_config_t )TWAI_TIMING_CONFIG_25KBITS ();
271
276
break ;
272
277
#endif
273
- #if 1 // # ifdef TWAI_TIMING_CONFIG_50KBITS
278
+ #ifdef TWAI_TIMING_CONFIG_50KBITS
274
279
case 50000 :
275
280
self -> config -> timing = (twai_timing_config_t )TWAI_TIMING_CONFIG_50KBITS ();
276
281
break ;
277
282
#endif
278
- #if 1 // # ifdef TWAI_TIMING_CONFIG_100KBITS
283
+ #ifdef TWAI_TIMING_CONFIG_100KBITS
279
284
case 100000 :
280
285
self -> config -> timing = (twai_timing_config_t )TWAI_TIMING_CONFIG_100KBITS ();
281
286
break ;
282
287
#endif
283
- #if 1 // # ifdef TWAI_TIMING_CONFIG_125KBITS
288
+ #ifdef TWAI_TIMING_CONFIG_125KBITS
284
289
case 125000 :
285
290
self -> config -> timing = (twai_timing_config_t )TWAI_TIMING_CONFIG_125KBITS ();
286
291
break ;
287
292
#endif
288
- #if 1 // # ifdef TWAI_TIMING_CONFIG_250KBITS
293
+ #ifdef TWAI_TIMING_CONFIG_250KBITS
289
294
case 250000 :
290
295
self -> config -> timing = (twai_timing_config_t )TWAI_TIMING_CONFIG_250KBITS ();
291
296
break ;
292
297
#endif
293
- #if 1 // # ifdef TWAI_TIMING_CONFIG_500KBITS
298
+ #ifdef TWAI_TIMING_CONFIG_500KBITS
294
299
case 500000 :
295
300
self -> config -> timing = (twai_timing_config_t )TWAI_TIMING_CONFIG_500KBITS ();
301
+ debug_printf ("A" );
296
302
break ;
297
303
#endif
298
- #if 1 // # ifdef TWAI_TIMING_CONFIG_800KBITS
304
+ #ifdef TWAI_TIMING_CONFIG_800KBITS
299
305
case 800000 :
300
306
self -> config -> timing = (twai_timing_config_t )TWAI_TIMING_CONFIG_800KBITS ();
301
307
break ;
302
308
#endif
303
- #if 1 // # ifdef TWAI_TIMING_CONFIG_1MBITS
309
+ #ifdef TWAI_TIMING_CONFIG_1MBITS
304
310
case 1000000 :
305
311
self -> config -> timing = (twai_timing_config_t )TWAI_TIMING_CONFIG_1MBITS ();
306
312
break ;
@@ -317,7 +323,7 @@ static mp_obj_t esp32_can_init_helper(esp32_can_obj_t *self, size_t n_args, cons
317
323
mp_raise_msg (& mp_type_RuntimeError , MP_ERROR_TEXT ("failed to create can irq task handler" ));
318
324
}
319
325
self -> config -> initialized = true;
320
-
326
+ debug_printf ( "B" );
321
327
return mp_const_none ;
322
328
}
323
329
@@ -332,7 +338,7 @@ static mp_obj_t esp32_can_make_new(const mp_obj_type_t *type, size_t n_args, siz
332
338
333
339
// work out port
334
340
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 ) {
336
342
mp_raise_msg_varg (& mp_type_ValueError , "out of CAN controllers:%d" , SOC_TWAI_CONTROLLER_NUM );
337
343
}
338
344
@@ -344,7 +350,7 @@ static mp_obj_t esp32_can_make_new(const mp_obj_type_t *type, size_t n_args, siz
344
350
// this can only be done if the hardware is in init mode
345
351
can_deinit (self );
346
352
}
347
- self -> rxcallback = mp_const_none ;
353
+ self -> rx_callback = mp_const_none ;
348
354
self -> irq_handler = NULL ;
349
355
self -> rx_state = RX_STATE_FIFO_EMPTY ;
350
356
@@ -365,7 +371,6 @@ static mp_obj_t esp32_can_init(size_t n_args, const mp_obj_t *pos_args, mp_map_t
365
371
mp_raise_msg (& mp_type_RuntimeError , "Device is already initialized" );
366
372
return mp_const_none ;
367
373
}
368
-
369
374
return esp32_can_init_helper (self , n_args - 1 , pos_args + 1 , kw_args );
370
375
}
371
376
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
611
616
// Return the result
612
617
return ret_obj ;
613
618
}
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 ) ;
615
620
616
621
// Clear filters setting
617
622
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
712
717
self -> config -> filter .acceptance_mask |= mask ;
713
718
}
714
719
// 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
+ }
717
724
check_esp_err (twai_driver_install_v2 (
718
725
& self -> config -> general ,
719
726
& self -> config -> timing ,
720
727
& self -> config -> filter ,
721
728
& self -> handle
722
729
));
723
730
check_esp_err (twai_start_v2 (self -> handle ));
724
-
725
731
return mp_const_none ;
726
732
}
727
733
static MP_DEFINE_CONST_FUN_OBJ_KW (esp32_can_set_filters_obj , 1 , esp32_can_set_filters ) ;
728
734
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);
730
738
// CAN.irq_recv(callback, hard=False)
731
739
//static mp_obj_t esp32_can_init(size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
732
740
//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;
736
746
if (callback_in == mp_const_none ) {
737
747
// disable callback
738
- self -> rxcallback = mp_const_none ;
748
+ self -> rx_callback = mp_const_none ;
739
749
} else if (mp_obj_is_callable (callback_in )) {
740
750
// set up interrupt
741
- self -> rxcallback = callback_in ;
751
+ self -> rx_callback = callback_in ;
742
752
}
743
-
744
753
return mp_const_none ;
745
754
}
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);
747
758
748
759
// CAN.irq_send(callback, hard=False)
749
760
static mp_obj_t esp32_can_irq_send (size_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
0 commit comments