24
24
* THE SOFTWARE.
25
25
*/
26
26
27
+ #include "py/enum.h"
27
28
#include "common-hal/_canio/CAN.h"
28
29
#include "common-hal/_canio/Listener.h"
30
+ #include "shared-bindings/_canio/__init__.h"
29
31
#include "shared-bindings/_canio/CAN.h"
30
32
#include "shared-bindings/_canio/Listener.h"
31
33
#include "shared-bindings/_canio/Match.h"
45
47
//| *,
46
48
//| baudrate: int = 250000,
47
49
//| loopback: bool = False,
50
+ //| auto_restart: bool = False,
48
51
//| ):
49
52
//| """A common shared-bus protocol. The rx and tx pins are generally
50
53
//| connected to a transceiver which controls the H and L pins on a shared
54
57
//| :param ~microcontrller.Pin tx: the pin to transmit with, or None if the peripheral should operate in "silent" mode.
55
58
//| :param int baudrate: The bit rate of the bus in Hz. All devices on the bus must agree on this value.
56
59
//| :param bool loopback: True if the peripheral will be operated in loopback mode.
60
+ //| :param bool auto_restart: If True, will restart communications after entering bus-off state
57
61
//| """
58
62
//| ...
59
63
//|
60
- //## auto_restart: bool = False, # Whether to restart communications after entering bus-off state
61
- //## sample_point: float = .875, # When to sample within bit time (0.0-1.0)
62
64
STATIC mp_obj_t canio_can_make_new (const mp_obj_type_t * type , size_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
63
- enum { ARG_rx , ARG_tx , ARG_baudrate , ARG_loopback , ARG_silent , NUM_ARGS };
65
+ enum { ARG_rx , ARG_tx , ARG_baudrate , ARG_loopback , ARG_silent , ARG_auto_restart , NUM_ARGS };
64
66
static const mp_arg_t allowed_args [] = {
65
67
{ MP_QSTR_rx , MP_ARG_OBJ | MP_ARG_REQUIRED , {.u_obj = 0 } },
66
68
{ MP_QSTR_tx , MP_ARG_OBJ , {.u_obj = 0 } },
67
69
{ MP_QSTR_baudrate , MP_ARG_INT , {.u_int = 250000 } },
68
70
{ MP_QSTR_loopback , MP_ARG_BOOL , {.u_bool = false} },
69
71
{ MP_QSTR_silent , MP_ARG_BOOL , {.u_bool = false} },
72
+ { MP_QSTR_auto_restart , MP_ARG_BOOL , {.u_bool = false} },
70
73
};
71
74
mp_arg_val_t args [MP_ARRAY_SIZE (allowed_args )];
72
75
MP_STATIC_ASSERT ( MP_ARRAY_SIZE (allowed_args ) == NUM_ARGS );
@@ -85,40 +88,51 @@ mp_printf(&mp_plat_print, "tx_pin=%p\n", tx_pin);
85
88
self -> base .type = & canio_can_type ;
86
89
common_hal_canio_can_construct (self , rx_pin , tx_pin , args [ARG_baudrate ].u_int , args [ARG_loopback ].u_bool , args [ARG_silent ].u_bool );
87
90
91
+ common_hal_canio_can_auto_restart_set (self , args [ARG_auto_restart ].u_bool );
92
+
88
93
return MP_OBJ_FROM_PTR (self );
89
94
}
90
95
91
96
92
- //| baudrate : int
93
- //| """The baud rate(read-only) """
97
+ //| auto_restart : int
98
+ //| """If True, will restart communications after entering bus-off state """
94
99
//|
95
- STATIC mp_obj_t canio_can_baudrate_get (mp_obj_t self_in ) {
100
+ STATIC mp_obj_t canio_can_auto_restart_get (mp_obj_t self_in ) {
96
101
canio_can_obj_t * self = MP_OBJ_TO_PTR (self_in );
97
102
common_hal_canio_can_check_for_deinit (self );
98
- return MP_OBJ_NEW_SMALL_INT ( common_hal_canio_can_baudrate_get (self ));
103
+ return mp_obj_new_bool ( common_hal_canio_can_auto_restart_get (self ));
99
104
}
100
- MP_DEFINE_CONST_FUN_OBJ_1 (canio_can_baudrate_get_obj , canio_can_baudrate_get );
105
+ MP_DEFINE_CONST_FUN_OBJ_1 (canio_can_auto_restart_get_obj , canio_can_auto_restart_get );
101
106
102
- STATIC const mp_obj_property_t canio_can_baudrate_obj = {
107
+ STATIC mp_obj_t canio_can_auto_restart_set (mp_obj_t self_in , mp_obj_t flag_in ) {
108
+ canio_can_obj_t * self = MP_OBJ_TO_PTR (self_in );
109
+ common_hal_canio_can_check_for_deinit (self );
110
+ common_hal_canio_can_auto_restart_set (self , mp_obj_is_true (flag_in ));
111
+ return mp_const_none ;
112
+ }
113
+ MP_DEFINE_CONST_FUN_OBJ_2 (canio_can_auto_restart_set_obj , canio_can_auto_restart_set );
114
+
115
+ STATIC const mp_obj_property_t canio_can_auto_restart_obj = {
103
116
.base .type = & mp_type_property ,
104
- .proxy = {(mp_obj_t )& canio_can_baudrate_get_obj ,
105
- (mp_obj_t )mp_const_none ,
117
+ .proxy = {(mp_obj_t )& canio_can_auto_restart_get_obj ,
118
+ (mp_obj_t )& canio_can_auto_restart_set_obj ,
106
119
(mp_obj_t )mp_const_none },
107
120
};
108
121
109
- //| state: State
110
- //| """The status of the hardware (read-only)"""
122
+
123
+ //| baudrate: int
124
+ //| """The baud rate (read-only)"""
111
125
//|
112
- STATIC mp_obj_t canio_can_state_get (mp_obj_t self_in ) {
126
+ STATIC mp_obj_t canio_can_baudrate_get (mp_obj_t self_in ) {
113
127
canio_can_obj_t * self = MP_OBJ_TO_PTR (self_in );
114
128
common_hal_canio_can_check_for_deinit (self );
115
- return MP_OBJ_NEW_SMALL_INT (common_hal_canio_can_state_get (self ));
129
+ return MP_OBJ_NEW_SMALL_INT (common_hal_canio_can_baudrate_get (self ));
116
130
}
117
- MP_DEFINE_CONST_FUN_OBJ_1 (canio_can_state_get_obj , canio_can_state_get );
131
+ MP_DEFINE_CONST_FUN_OBJ_1 (canio_can_baudrate_get_obj , canio_can_baudrate_get );
118
132
119
- STATIC const mp_obj_property_t canio_can_state_obj = {
133
+ STATIC const mp_obj_property_t canio_can_baudrate_obj = {
120
134
.base .type = & mp_type_property ,
121
- .proxy = {(mp_obj_t )& canio_can_state_get_obj ,
135
+ .proxy = {(mp_obj_t )& canio_can_baudrate_get_obj ,
122
136
(mp_obj_t )mp_const_none ,
123
137
(mp_obj_t )mp_const_none },
124
138
};
@@ -208,12 +222,43 @@ STATIC const mp_obj_property_t canio_can_bus_off_state_count_obj = {
208
222
(mp_obj_t )mp_const_none },
209
223
};
210
224
225
+ //| state: State
226
+ //| """The current state of the bus."""
227
+ STATIC mp_obj_t canio_can_state_get (mp_obj_t self_in ) {
228
+ canio_can_obj_t * self = MP_OBJ_TO_PTR (self_in );
229
+ common_hal_canio_can_check_for_deinit (self );
230
+ return cp_enum_find (& canio_bus_state_type , common_hal_canio_can_state_get (self ));
231
+ }
232
+
233
+ MP_DEFINE_CONST_FUN_OBJ_1 (canio_can_state_get_obj , canio_can_state_get );
234
+
235
+ STATIC const mp_obj_property_t canio_can_state_obj = {
236
+ .base .type = & mp_type_property ,
237
+ .proxy = {(mp_obj_t )& canio_can_state_get_obj ,
238
+ (mp_obj_t )mp_const_none ,
239
+ (mp_obj_t )mp_const_none },
240
+ };
241
+
242
+
211
243
#if 0
212
244
//| # pending_tx_count: int
213
245
//| # """The number of messages waiting to be transmitted. (read-only)"""
246
+ //|
214
247
#endif
215
248
216
- //| def listen(filters: Optional[Sequence[Filter]]=None, *, timeout: float=10) -> Listener:
249
+ //| def restart(self) -> None:
250
+ //| """If the device is in the bus off state, restart it."""
251
+ //| ...
252
+ //|
253
+ STATIC mp_obj_t canio_can_restart (mp_obj_t self_in ) {
254
+ canio_can_obj_t * self = MP_OBJ_TO_PTR (self_in );
255
+ common_hal_canio_can_check_for_deinit (self );
256
+ common_hal_canio_can_restart (self );
257
+ return mp_const_none ;
258
+ }
259
+ STATIC MP_DEFINE_CONST_FUN_OBJ_1 (canio_can_restart_obj , canio_can_restart );
260
+
261
+ //| def listen(self, filters: Optional[Sequence[Filter]]=None, *, timeout: float=10) -> Listener:
217
262
//| """Start receiving messages that match any one of the filters.
218
263
//| Creating a listener is an expensive operation and can interfere with reception of messages by other listeners.
219
264
//| There is an implementation-defined maximum number of listeners and limit to the complexity of the filters.
@@ -317,15 +362,17 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(canio_can_exit_obj, 4, 4, canio_can_e
317
362
STATIC const mp_rom_map_elem_t canio_can_locals_dict_table [] = {
318
363
{ MP_ROM_QSTR (MP_QSTR___enter__ ), MP_ROM_PTR (& canio_can_enter_obj ) },
319
364
{ MP_ROM_QSTR (MP_QSTR___exit__ ), MP_ROM_PTR (& canio_can_exit_obj ) },
365
+ { MP_ROM_QSTR (MP_QSTR_auto_restart ), MP_ROM_PTR (& canio_can_auto_restart_obj ) },
366
+ { MP_ROM_QSTR (MP_QSTR_baudrate ), MP_ROM_PTR (& canio_can_baudrate_obj ) },
320
367
{ MP_ROM_QSTR (MP_QSTR_bus_off_state_count ), MP_ROM_PTR (& canio_can_bus_off_state_count_obj ) },
321
368
{ MP_ROM_QSTR (MP_QSTR_deinit ), MP_ROM_PTR (& canio_can_deinit_obj ) },
322
369
{ MP_ROM_QSTR (MP_QSTR_error_passive_state_count ), MP_ROM_PTR (& canio_can_error_passive_state_count_obj ) },
323
370
{ MP_ROM_QSTR (MP_QSTR_error_warning_state_count ), MP_ROM_PTR (& canio_can_error_warning_state_count_obj ) },
324
371
{ MP_ROM_QSTR (MP_QSTR_listen ), MP_ROM_PTR (& canio_can_listen_obj ) },
325
372
{ MP_ROM_QSTR (MP_QSTR_receive_error_count ), MP_ROM_PTR (& canio_can_receive_error_count_obj ) },
373
+ { MP_ROM_QSTR (MP_QSTR_restart ), MP_ROM_PTR (& canio_can_restart_obj ) },
326
374
{ MP_ROM_QSTR (MP_QSTR_send ), MP_ROM_PTR (& canio_can_send_obj ) },
327
375
{ MP_ROM_QSTR (MP_QSTR_state ), MP_ROM_PTR (& canio_can_state_obj ) },
328
- { MP_ROM_QSTR (MP_QSTR_baudrate ), MP_ROM_PTR (& canio_can_baudrate_obj ) },
329
376
{ MP_ROM_QSTR (MP_QSTR_transmit_error_count ), MP_ROM_PTR (& canio_can_transmit_error_count_obj ) },
330
377
};
331
378
STATIC MP_DEFINE_CONST_DICT (canio_can_locals_dict , canio_can_locals_dict_table );
0 commit comments