43
43
#define MICROPY_PY_MACHINE_SPI_LSB (1)
44
44
#endif
45
45
46
+ #if 1
46
47
#define MACHINE_HW_SPI_DEBUG_PRINTF (args ...) printf(args)
48
+ #else
49
+ #define MACHINE_HW_SPI_DEBUG_PRINTF (args ...)
50
+ #endif
47
51
48
- STATIC void machine_hw_spi_deinit_internal (spi_host_device_t host , spi_device_handle_t * spi ) {
52
+ STATIC void machine_hw_spi_deinit_internal (spi_host_device_t host , spi_device_handle_t spi ) {
49
53
50
- switch (spi_bus_remove_device (* spi )) {
54
+ MACHINE_HW_SPI_DEBUG_PRINTF ("machine_hw_spi_deinit_internal(host = %d, spi = %p)\n" , host , spi );
55
+ switch (spi_bus_remove_device (spi )) {
51
56
case ESP_ERR_INVALID_ARG :
52
57
mp_raise_msg (& mp_type_OSError , "Invalid configuration" );
53
58
return ;
@@ -72,7 +77,7 @@ STATIC void machine_hw_spi_deinit(mp_obj_base_t *self_in) {
72
77
machine_hw_spi_obj_t * self = (machine_hw_spi_obj_t * )self_in ;
73
78
if (self -> state == MACHINE_HW_SPI_STATE_INIT ) {
74
79
self -> state = MACHINE_HW_SPI_STATE_DEINIT ;
75
- machine_hw_spi_deinit_internal (self -> host , & self -> spi );
80
+ machine_hw_spi_deinit_internal (self -> host , self -> spi );
76
81
}
77
82
}
78
83
@@ -107,7 +112,9 @@ STATIC void machine_hw_spi_transfer(mp_obj_base_t *self_in, size_t len, const ui
107
112
transaction .rx_buffer = dest ;
108
113
}
109
114
115
+ MACHINE_HW_SPI_DEBUG_PRINTF ("Just before spi_device_transmit()\n" );
110
116
spi_device_transmit (self -> spi , & transaction );
117
+ MACHINE_HW_SPI_DEBUG_PRINTF ("Just after spi_device_transmit()\n" );
111
118
112
119
if (shortMsg && dest != NULL ) {
113
120
memcpy (dest , & transaction .rx_data , len );
@@ -119,12 +126,14 @@ STATIC void machine_hw_spi_transfer(mp_obj_base_t *self_in, size_t len, const ui
119
126
120
127
STATIC void machine_hw_spi_print (const mp_print_t * print , mp_obj_t self_in , mp_print_kind_t kind ) {
121
128
machine_hw_spi_obj_t * self = MP_OBJ_TO_PTR (self_in );
122
- mp_printf (print , "SPI(id=%u, baudrate=%u, polarity=%u, phase=%u, bits=%u, firstbit=%u, sck=%d, mosi=%d, miso=%d)" ,
129
+ mp_printf (print , "SPI(id=%u, baudrate=%u, polarity=%u, phase=%u, bits=%u, firstbit=%u, sck=%d, mosi=%d, miso=%d%s )" ,
123
130
self -> host , self -> baudrate , self -> polarity ,
124
131
self -> phase , self -> bits , self -> firstbit ,
125
- self -> sck , self -> mosi , self -> miso );
132
+ self -> sck , self -> mosi , self -> miso ,
133
+ self -> state != MACHINE_HW_SPI_STATE_INIT ? ", DEINIT" : "" );
126
134
}
127
135
136
+
128
137
STATIC void machine_hw_spi_init_internal (
129
138
machine_hw_spi_obj_t * self ,
130
139
int8_t host ,
@@ -136,7 +145,10 @@ STATIC void machine_hw_spi_init_internal(
136
145
int8_t sck ,
137
146
int8_t mosi ,
138
147
int8_t miso ) {
139
- bool changed = false;
148
+
149
+ // if not initialized, then calling this (init) is a change, by definiton
150
+ bool changed = self -> state != MACHINE_HW_SPI_STATE_INIT ;
151
+
140
152
MACHINE_HW_SPI_DEBUG_PRINTF ("machine_hw_spi_init_internal(self, host = %d, baudrate = %d, polarity = %d, phase = %d, bits = %d, firstbit = %d, sck = %d, mosi = %d, miso = %d)\n" , host , baudrate , polarity , phase , bits , firstbit , sck , mosi , miso );
141
153
142
154
@@ -193,40 +205,41 @@ STATIC void machine_hw_spi_init_internal(
193
205
mp_raise_ValueError ("SPI ID must be either HSPI(1) or VSPI(2)" );
194
206
}
195
207
196
- if (changed ) {
197
- if ( self -> state == MACHINE_HW_SPI_STATE_INIT ) {
198
- MACHINE_HW_SPI_DEBUG_PRINTF ( "machine_hw_spi_init_internal calling deinit()\n" );
199
- self -> state = MACHINE_HW_SPI_STATE_DEINIT ;
200
- machine_hw_spi_deinit_internal ( old_host , & self -> spi );
201
- }
202
- } else {
203
- return ; // no changes
208
+ if (! changed && self -> state == MACHINE_HW_SPI_STATE_INIT ) {
209
+ return ;
210
+ }
211
+
212
+ if ( self -> state == MACHINE_HW_SPI_STATE_INIT ) {
213
+ MACHINE_HW_SPI_DEBUG_PRINTF ( "machine_hw_spi_init_internal calling deinit()\n" );
214
+ self -> state = MACHINE_HW_SPI_STATE_DEINIT ;
215
+ machine_hw_spi_deinit_internal ( old_host , self -> spi );
204
216
}
205
217
206
218
MACHINE_HW_SPI_DEBUG_PRINTF ("machine_hw_spi_init_internal new values: host = %d, baudrate = %d, polarity = %d, phase = %d, bits = %d, firstbit = %d, sck = %d, mosi = %d, miso = %d)\n" , self -> host , self -> baudrate , self -> polarity , self -> phase , self -> bits , self -> firstbit , self -> sck , self -> mosi , self -> miso );
207
219
220
+ spi_bus_config_t bus_config ;
221
+ spi_device_interface_config_t device_config ;
208
222
209
- spi_bus_config_t buscfg = {
210
- .miso_io_num = self -> miso ,
211
- .mosi_io_num = self -> mosi ,
212
- .sclk_io_num = self -> sck ,
213
- .quadwp_io_num = -1 ,
214
- .quadhd_io_num = -1
215
- };
223
+ memset (& bus_config , 0 , sizeof (spi_bus_config_t ));
224
+ memset (& device_config , 0 , sizeof (spi_device_interface_config_t ));
216
225
217
- spi_device_interface_config_t devcfg = {
218
- .clock_speed_hz = self -> baudrate ,
219
- .mode = self -> phase | (self -> polarity << 1 ),
220
- .spics_io_num = -1 , // No CS pin
221
- .queue_size = 1 ,
222
- .flags = self -> firstbit == MICROPY_PY_MACHINE_SPI_LSB ? SPI_DEVICE_TXBIT_LSBFIRST | SPI_DEVICE_RXBIT_LSBFIRST : 0 ,
223
- .pre_cb = NULL
224
- };
226
+ bus_config .miso_io_num = self -> miso ;
227
+ bus_config .mosi_io_num = self -> mosi ;
228
+ bus_config .sclk_io_num = self -> sck ;
229
+ bus_config .quadwp_io_num = -1 ;
230
+ bus_config .quadhd_io_num = -1 ;
231
+
232
+ device_config .clock_speed_hz = self -> baudrate ;
233
+ device_config .mode = self -> phase | (self -> polarity << 1 );
234
+ device_config .spics_io_num = -1 ;
235
+ device_config .queue_size = 1 ;
236
+ device_config .flags = self -> firstbit == MICROPY_PY_MACHINE_SPI_LSB ? SPI_DEVICE_TXBIT_LSBFIRST | SPI_DEVICE_RXBIT_LSBFIRST : 0 ;
237
+ device_config .pre_cb = NULL ;
225
238
226
239
//Initialize the SPI bus
227
240
// FIXME: Does the DMA matter? There are two
228
241
229
- ret = spi_bus_initialize (self -> host , & buscfg , 1 );
242
+ ret = spi_bus_initialize (self -> host , & bus_config , self -> host );
230
243
switch (ret ) {
231
244
case ESP_ERR_INVALID_ARG :
232
245
mp_raise_msg (& mp_type_OSError , "Invalid configuration" );
@@ -237,7 +250,7 @@ STATIC void machine_hw_spi_init_internal(
237
250
return ;
238
251
}
239
252
240
- ret = spi_bus_add_device (self -> host , & devcfg , & self -> spi );
253
+ ret = spi_bus_add_device (self -> host , & device_config , & self -> spi );
241
254
switch (ret ) {
242
255
case ESP_ERR_INVALID_ARG :
243
256
mp_raise_msg (& mp_type_OSError , "Invalid configuration" );
@@ -274,8 +287,6 @@ STATIC void machine_hw_spi_init(mp_obj_base_t *self_in, size_t n_args, const mp_
274
287
{ MP_QSTR_miso , MP_ARG_KW_ONLY | MP_ARG_OBJ , {.u_obj = MP_OBJ_NULL } },
275
288
};
276
289
277
-
278
-
279
290
mp_arg_val_t args [MP_ARRAY_SIZE (allowed_args )];
280
291
mp_arg_parse_all (n_args , pos_args , kw_args , MP_ARRAY_SIZE (allowed_args ),
281
292
allowed_args , args );
@@ -308,7 +319,7 @@ STATIC void machine_hw_spi_init(mp_obj_base_t *self_in, size_t n_args, const mp_
308
319
MACHINE_HW_SPI_DEBUG_PRINTF ("before calling internal\n" );
309
320
machine_hw_spi_init_internal ( self , args [ARG_id ].u_int , args [ARG_baudrate ].u_int ,
310
321
args [ARG_polarity ].u_int , args [ARG_phase ].u_int , args [ARG_bits ].u_int ,
311
- args [ARG_firstbit ].u_int , sck , miso , mosi );
322
+ args [ARG_firstbit ].u_int , sck , mosi , miso );
312
323
}
313
324
314
325
mp_obj_t machine_hw_spi_make_new (const mp_obj_type_t * type , size_t n_args , size_t n_kw , const mp_obj_t * all_args ) {
@@ -339,8 +350,8 @@ mp_obj_t machine_hw_spi_make_new(const mp_obj_type_t *type, size_t n_args, size_
339
350
args [ARG_bits ].u_int ,
340
351
args [ARG_firstbit ].u_int ,
341
352
args [ARG_sck ].u_obj == MP_OBJ_NULL ? -1 : machine_pin_get_id (args [ARG_sck ].u_obj ),
342
- args [ARG_miso ].u_obj == MP_OBJ_NULL ? -1 : machine_pin_get_id (args [ARG_miso ].u_obj ),
343
- args [ARG_mosi ].u_obj == MP_OBJ_NULL ? -1 : machine_pin_get_id (args [ARG_mosi ].u_obj ));
353
+ args [ARG_mosi ].u_obj == MP_OBJ_NULL ? -1 : machine_pin_get_id (args [ARG_mosi ].u_obj ),
354
+ args [ARG_miso ].u_obj == MP_OBJ_NULL ? -1 : machine_pin_get_id (args [ARG_miso ].u_obj ));
344
355
345
356
return MP_OBJ_FROM_PTR (self );
346
357
}
0 commit comments