@@ -55,25 +55,26 @@ typedef struct _machine_hw_spi_obj_t {
55
55
int8_t miso ;
56
56
spi_device_handle_t spi ;
57
57
enum {
58
- MACHINE_HW_SPI_STATE_NONE ,
59
- MACHINE_HW_SPI_STATE_INIT ,
60
- MACHINE_HW_SPI_STATE_DEINIT } state ;
58
+ MACHINE_HW_SPI_STATE_NONE ,
59
+ MACHINE_HW_SPI_STATE_INIT ,
60
+ MACHINE_HW_SPI_STATE_DEINIT
61
+ } state ;
61
62
} machine_hw_spi_obj_t ;
62
63
63
- STATIC void machine_hw_spi_deinit_internal (machine_hw_spi_obj_t * self ) {
64
- switch (spi_bus_remove_device (self -> spi )) {
64
+ STATIC void machine_hw_spi_deinit_internal (machine_hw_spi_obj_t * self ) {
65
+ switch (spi_bus_remove_device (self -> spi )) {
65
66
case ESP_ERR_INVALID_ARG :
66
- mp_raise_msg (& mp_type_OSError , "Invalid configuration" );
67
+ mp_raise_msg (& mp_type_OSError , "invalid configuration" );
67
68
return ;
68
69
69
70
case ESP_ERR_INVALID_STATE :
70
71
mp_raise_msg (& mp_type_OSError , "SPI device already freed" );
71
72
return ;
72
73
}
73
74
74
- switch (spi_bus_free (self -> host )) {
75
+ switch (spi_bus_free (self -> host )) {
75
76
case ESP_ERR_INVALID_ARG :
76
- mp_raise_msg (& mp_type_OSError , "Invalid configuration" );
77
+ mp_raise_msg (& mp_type_OSError , "invalid configuration" );
77
78
return ;
78
79
79
80
case ESP_ERR_INVALID_STATE :
@@ -93,18 +94,18 @@ STATIC void machine_hw_spi_deinit_internal(machine_hw_spi_obj_t * self) {
93
94
}
94
95
95
96
STATIC void machine_hw_spi_init_internal (
96
- machine_hw_spi_obj_t * self ,
97
- int8_t host ,
98
- int32_t baudrate ,
99
- int8_t polarity ,
100
- int8_t phase ,
101
- int8_t bits ,
102
- int8_t firstbit ,
103
- int8_t sck ,
104
- int8_t mosi ,
105
- int8_t miso ) {
106
-
107
- // if we're not initialized, then we're
97
+ machine_hw_spi_obj_t * self ,
98
+ int8_t host ,
99
+ int32_t baudrate ,
100
+ int8_t polarity ,
101
+ int8_t phase ,
102
+ int8_t bits ,
103
+ int8_t firstbit ,
104
+ int8_t sck ,
105
+ int8_t mosi ,
106
+ int8_t miso ) {
107
+
108
+ // if we're not initialized, then we're
108
109
// implicitly 'changed', since this is the init routine
109
110
bool changed = self -> state != MACHINE_HW_SPI_STATE_INIT ;
110
111
@@ -191,9 +192,9 @@ STATIC void machine_hw_spi_init_internal(
191
192
// FIXME: Does the DMA matter? There are two
192
193
193
194
ret = spi_bus_initialize (self -> host , & buscfg , 1 );
194
- switch (ret ) {
195
+ switch (ret ) {
195
196
case ESP_ERR_INVALID_ARG :
196
- mp_raise_msg (& mp_type_OSError , "Invalid configuration" );
197
+ mp_raise_msg (& mp_type_OSError , "invalid configuration" );
197
198
return ;
198
199
199
200
case ESP_ERR_INVALID_STATE :
@@ -202,27 +203,27 @@ STATIC void machine_hw_spi_init_internal(
202
203
}
203
204
204
205
ret = spi_bus_add_device (self -> host , & devcfg , & self -> spi );
205
- switch (ret ) {
206
+ switch (ret ) {
206
207
case ESP_ERR_INVALID_ARG :
207
- mp_raise_msg (& mp_type_OSError , "Invalid configuration" );
208
+ mp_raise_msg (& mp_type_OSError , "invalid configuration" );
208
209
spi_bus_free (self -> host );
209
210
return ;
210
211
211
212
case ESP_ERR_NO_MEM :
212
- mp_raise_msg (& mp_type_OSError , "Out of memory" );
213
+ mp_raise_msg (& mp_type_OSError , "out of memory" );
213
214
spi_bus_free (self -> host );
214
215
return ;
215
216
216
217
case ESP_ERR_NOT_FOUND :
217
- mp_raise_msg (& mp_type_OSError , "No free CS slots" );
218
+ mp_raise_msg (& mp_type_OSError , "no free slots" );
218
219
spi_bus_free (self -> host );
219
220
return ;
220
221
}
221
222
self -> state = MACHINE_HW_SPI_STATE_INIT ;
222
223
}
223
224
224
225
STATIC void machine_hw_spi_deinit (mp_obj_base_t * self_in ) {
225
- machine_hw_spi_obj_t * self = (machine_hw_spi_obj_t * ) self_in ;
226
+ machine_hw_spi_obj_t * self = (machine_hw_spi_obj_t * ) self_in ;
226
227
if (self -> state == MACHINE_HW_SPI_STATE_INIT ) {
227
228
self -> state = MACHINE_HW_SPI_STATE_DEINIT ;
228
229
machine_hw_spi_deinit_internal (self );
@@ -236,7 +237,7 @@ STATIC void machine_hw_spi_transfer(mp_obj_base_t *self_in, size_t len, const ui
236
237
bool shortMsg = len <= 4 ;
237
238
238
239
if (self -> state == MACHINE_HW_SPI_STATE_DEINIT ) {
239
- mp_raise_msg (& mp_type_OSError , "Transfer on deinitialized SPI" );
240
+ mp_raise_msg (& mp_type_OSError , "transfer on deinitialized SPI" );
240
241
return ;
241
242
}
242
243
@@ -247,7 +248,7 @@ STATIC void machine_hw_spi_transfer(mp_obj_base_t *self_in, size_t len, const ui
247
248
.rx_buffer = NULL ,
248
249
};
249
250
250
- if (shortMsg ) {
251
+ if (shortMsg ) {
251
252
if (src != NULL ) {
252
253
memcpy (& transaction .tx_data , src , len );
253
254
}
@@ -270,13 +271,13 @@ STATIC void machine_hw_spi_transfer(mp_obj_base_t *self_in, size_t len, const ui
270
271
STATIC void machine_hw_spi_print (const mp_print_t * print , mp_obj_t self_in , mp_print_kind_t kind ) {
271
272
machine_hw_spi_obj_t * self = MP_OBJ_TO_PTR (self_in );
272
273
mp_printf (print , "SPI(id=%u, baudrate=%u, polarity=%u, phase=%u, bits=%u, firstbit=%u, sck=%d, mosi=%d, miso=%d)" ,
273
- self -> host , self -> baudrate , self -> polarity ,
274
- self -> phase , self -> bits , self -> firstbit ,
275
- self -> sck , self -> mosi , self -> miso );
274
+ self -> host , self -> baudrate , self -> polarity ,
275
+ self -> phase , self -> bits , self -> firstbit ,
276
+ self -> sck , self -> mosi , self -> miso );
276
277
}
277
278
278
279
STATIC void machine_hw_spi_init (mp_obj_base_t * self_in , size_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
279
- machine_hw_spi_obj_t * self = (machine_hw_spi_obj_t * ) self_in ;
280
+ machine_hw_spi_obj_t * self = (machine_hw_spi_obj_t * ) self_in ;
280
281
281
282
enum { ARG_id , ARG_baudrate , ARG_polarity , ARG_phase , ARG_bits , ARG_firstbit , ARG_sck , ARG_mosi , ARG_miso };
282
283
static const mp_arg_t allowed_args [] = {
@@ -293,36 +294,36 @@ STATIC void machine_hw_spi_init(mp_obj_base_t *self_in, size_t n_args, const mp_
293
294
294
295
mp_arg_val_t args [MP_ARRAY_SIZE (allowed_args )];
295
296
mp_arg_parse_all (n_args , pos_args , kw_args , MP_ARRAY_SIZE (allowed_args ),
296
- allowed_args , args );
297
+ allowed_args , args );
297
298
int8_t sck , mosi , miso ;
298
299
299
- if (args [ARG_sck ].u_obj == MP_OBJ_NULL ) {
300
+ if (args [ARG_sck ].u_obj == MP_OBJ_NULL ) {
300
301
sck = -2 ;
301
302
} else if (args [ARG_sck ].u_obj == mp_const_none ) {
302
303
sck = -1 ;
303
304
} else {
304
305
sck = machine_pin_get_id (args [ARG_sck ].u_obj );
305
306
}
306
307
307
- if (args [ARG_miso ].u_obj == MP_OBJ_NULL ) {
308
+ if (args [ARG_miso ].u_obj == MP_OBJ_NULL ) {
308
309
miso = -2 ;
309
310
} else if (args [ARG_miso ].u_obj == mp_const_none ) {
310
311
miso = -1 ;
311
312
} else {
312
313
miso = machine_pin_get_id (args [ARG_miso ].u_obj );
313
314
}
314
315
315
- if (args [ARG_mosi ].u_obj == MP_OBJ_NULL ) {
316
+ if (args [ARG_mosi ].u_obj == MP_OBJ_NULL ) {
316
317
mosi = -2 ;
317
318
} else if (args [ARG_mosi ].u_obj == mp_const_none ) {
318
319
mosi = -1 ;
319
320
} else {
320
321
mosi = machine_pin_get_id (args [ARG_mosi ].u_obj );
321
322
}
322
323
323
- machine_hw_spi_init_internal ( self , args [ARG_id ].u_int , args [ARG_baudrate ].u_int ,
324
- args [ARG_polarity ].u_int , args [ARG_phase ].u_int , args [ARG_bits ].u_int ,
325
- args [ARG_firstbit ].u_int , sck , mosi , miso );
324
+ machine_hw_spi_init_internal (self , args [ARG_id ].u_int , args [ARG_baudrate ].u_int ,
325
+ args [ARG_polarity ].u_int , args [ARG_phase ].u_int , args [ARG_bits ].u_int ,
326
+ args [ARG_firstbit ].u_int , sck , mosi , miso );
326
327
}
327
328
328
329
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 ) {
@@ -345,16 +346,16 @@ mp_obj_t machine_hw_spi_make_new(const mp_obj_type_t *type, size_t n_args, size_
345
346
self -> base .type = & machine_hw_spi_type ;
346
347
347
348
machine_hw_spi_init_internal (
348
- self ,
349
- args [ARG_id ].u_int ,
350
- args [ARG_baudrate ].u_int ,
351
- args [ARG_polarity ].u_int ,
352
- args [ARG_phase ].u_int ,
353
- args [ARG_bits ].u_int ,
354
- args [ARG_firstbit ].u_int ,
355
- args [ARG_sck ].u_obj == MP_OBJ_NULL ? -1 : machine_pin_get_id (args [ARG_sck ].u_obj ),
356
- args [ARG_mosi ].u_obj == MP_OBJ_NULL ? -1 : machine_pin_get_id (args [ARG_mosi ].u_obj ),
357
- args [ARG_miso ].u_obj == MP_OBJ_NULL ? -1 : machine_pin_get_id (args [ARG_miso ].u_obj ));
349
+ self ,
350
+ args [ARG_id ].u_int ,
351
+ args [ARG_baudrate ].u_int ,
352
+ args [ARG_polarity ].u_int ,
353
+ args [ARG_phase ].u_int ,
354
+ args [ARG_bits ].u_int ,
355
+ args [ARG_firstbit ].u_int ,
356
+ args [ARG_sck ].u_obj == MP_OBJ_NULL ? -1 : machine_pin_get_id (args [ARG_sck ].u_obj ),
357
+ args [ARG_mosi ].u_obj == MP_OBJ_NULL ? -1 : machine_pin_get_id (args [ARG_mosi ].u_obj ),
358
+ args [ARG_miso ].u_obj == MP_OBJ_NULL ? -1 : machine_pin_get_id (args [ARG_miso ].u_obj ));
358
359
359
360
return MP_OBJ_FROM_PTR (self );
360
361
}
@@ -371,5 +372,5 @@ const mp_obj_type_t machine_hw_spi_type = {
371
372
.print = machine_hw_spi_print ,
372
373
.make_new = machine_hw_spi_make_new ,
373
374
.protocol = & machine_hw_spi_p ,
374
- .locals_dict = (mp_obj_dict_t * ) & mp_machine_spi_locals_dict ,
375
+ .locals_dict = (mp_obj_dict_t * ) & mp_machine_spi_locals_dict ,
375
376
};
0 commit comments