Skip to content

Commit 471a908

Browse files
committed
esp32/machine_hw_spi.c: Code and error message formatting
1 parent 0cf672e commit 471a908

File tree

1 file changed

+52
-51
lines changed

1 file changed

+52
-51
lines changed

esp32/machine_hw_spi.c

Lines changed: 52 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -55,25 +55,26 @@ typedef struct _machine_hw_spi_obj_t {
5555
int8_t miso;
5656
spi_device_handle_t spi;
5757
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;
6162
} machine_hw_spi_obj_t;
6263

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)) {
6566
case ESP_ERR_INVALID_ARG:
66-
mp_raise_msg(&mp_type_OSError, "Invalid configuration");
67+
mp_raise_msg(&mp_type_OSError, "invalid configuration");
6768
return;
6869

6970
case ESP_ERR_INVALID_STATE:
7071
mp_raise_msg(&mp_type_OSError, "SPI device already freed");
7172
return;
7273
}
7374

74-
switch(spi_bus_free(self->host)) {
75+
switch (spi_bus_free(self->host)) {
7576
case ESP_ERR_INVALID_ARG:
76-
mp_raise_msg(&mp_type_OSError, "Invalid configuration");
77+
mp_raise_msg(&mp_type_OSError, "invalid configuration");
7778
return;
7879

7980
case ESP_ERR_INVALID_STATE:
@@ -93,18 +94,18 @@ STATIC void machine_hw_spi_deinit_internal(machine_hw_spi_obj_t * self) {
9394
}
9495

9596
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
108109
// implicitly 'changed', since this is the init routine
109110
bool changed = self->state != MACHINE_HW_SPI_STATE_INIT;
110111

@@ -191,9 +192,9 @@ STATIC void machine_hw_spi_init_internal(
191192
// FIXME: Does the DMA matter? There are two
192193

193194
ret = spi_bus_initialize(self->host, &buscfg, 1);
194-
switch (ret) {
195+
switch (ret) {
195196
case ESP_ERR_INVALID_ARG:
196-
mp_raise_msg(&mp_type_OSError, "Invalid configuration");
197+
mp_raise_msg(&mp_type_OSError, "invalid configuration");
197198
return;
198199

199200
case ESP_ERR_INVALID_STATE:
@@ -202,27 +203,27 @@ STATIC void machine_hw_spi_init_internal(
202203
}
203204

204205
ret = spi_bus_add_device(self->host, &devcfg, &self->spi);
205-
switch (ret) {
206+
switch (ret) {
206207
case ESP_ERR_INVALID_ARG:
207-
mp_raise_msg(&mp_type_OSError, "Invalid configuration");
208+
mp_raise_msg(&mp_type_OSError, "invalid configuration");
208209
spi_bus_free(self->host);
209210
return;
210211

211212
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");
213214
spi_bus_free(self->host);
214215
return;
215216

216217
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");
218219
spi_bus_free(self->host);
219220
return;
220221
}
221222
self->state = MACHINE_HW_SPI_STATE_INIT;
222223
}
223224

224225
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;
226227
if (self->state == MACHINE_HW_SPI_STATE_INIT) {
227228
self->state = MACHINE_HW_SPI_STATE_DEINIT;
228229
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
236237
bool shortMsg = len <= 4;
237238

238239
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");
240241
return;
241242
}
242243

@@ -247,7 +248,7 @@ STATIC void machine_hw_spi_transfer(mp_obj_base_t *self_in, size_t len, const ui
247248
.rx_buffer = NULL,
248249
};
249250

250-
if(shortMsg) {
251+
if (shortMsg) {
251252
if (src != NULL) {
252253
memcpy(&transaction.tx_data, src, len);
253254
}
@@ -270,13 +271,13 @@ STATIC void machine_hw_spi_transfer(mp_obj_base_t *self_in, size_t len, const ui
270271
STATIC void machine_hw_spi_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
271272
machine_hw_spi_obj_t *self = MP_OBJ_TO_PTR(self_in);
272273
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);
276277
}
277278

278279
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;
280281

281282
enum { ARG_id, ARG_baudrate, ARG_polarity, ARG_phase, ARG_bits, ARG_firstbit, ARG_sck, ARG_mosi, ARG_miso };
282283
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_
293294

294295
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
295296
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args),
296-
allowed_args, args);
297+
allowed_args, args);
297298
int8_t sck, mosi, miso;
298299

299-
if(args[ARG_sck].u_obj == MP_OBJ_NULL) {
300+
if (args[ARG_sck].u_obj == MP_OBJ_NULL) {
300301
sck = -2;
301302
} else if (args[ARG_sck].u_obj == mp_const_none) {
302303
sck = -1;
303304
} else {
304305
sck = machine_pin_get_id(args[ARG_sck].u_obj);
305306
}
306307

307-
if(args[ARG_miso].u_obj == MP_OBJ_NULL) {
308+
if (args[ARG_miso].u_obj == MP_OBJ_NULL) {
308309
miso = -2;
309310
} else if (args[ARG_miso].u_obj == mp_const_none) {
310311
miso = -1;
311312
} else {
312313
miso = machine_pin_get_id(args[ARG_miso].u_obj);
313314
}
314315

315-
if(args[ARG_mosi].u_obj == MP_OBJ_NULL) {
316+
if (args[ARG_mosi].u_obj == MP_OBJ_NULL) {
316317
mosi = -2;
317318
} else if (args[ARG_mosi].u_obj == mp_const_none) {
318319
mosi = -1;
319320
} else {
320321
mosi = machine_pin_get_id(args[ARG_mosi].u_obj);
321322
}
322323

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);
326327
}
327328

328329
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_
345346
self->base.type = &machine_hw_spi_type;
346347

347348
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));
358359

359360
return MP_OBJ_FROM_PTR(self);
360361
}
@@ -371,5 +372,5 @@ const mp_obj_type_t machine_hw_spi_type = {
371372
.print = machine_hw_spi_print,
372373
.make_new = machine_hw_spi_make_new,
373374
.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,
375376
};

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy