Skip to content

Commit 6989aba

Browse files
committed
nrf/modules/machine: Use SPI Python bindings provided by extmod.
Signed-off-by: Damien George <damien@micropython.org>
1 parent d336c1b commit 6989aba

2 files changed

Lines changed: 34 additions & 123 deletions

File tree

ports/nrf/modules/machine/spi.c

Lines changed: 31 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -188,13 +188,14 @@ enum {
188188
ARG_INIT_firstbit
189189
};
190190

191-
STATIC mp_obj_t machine_hard_spi_make_new(mp_arg_val_t *args);
192-
STATIC void machine_hard_spi_init(mp_obj_t self, mp_arg_val_t *args);
193-
STATIC void machine_hard_spi_deinit(mp_obj_t self);
191+
STATIC void machine_hard_spi_init_helper(const machine_hard_spi_obj_t *self, mp_arg_val_t *args);
194192

195-
/* common code for both soft and hard implementations *************************/
193+
STATIC void machine_hard_spi_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
194+
machine_hard_spi_obj_t *self = self_in;
195+
mp_printf(print, "SPI(%u)", self->p_spi->drv_inst_idx);
196+
}
196197

197-
STATIC mp_obj_t machine_spi_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
198+
STATIC mp_obj_t machine_hard_spi_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
198199
static const mp_arg_t allowed_args[] = {
199200
{ MP_QSTR_id, MP_ARG_OBJ, {.u_obj = MP_OBJ_NEW_SMALL_INT(-1)} },
200201
{ MP_QSTR_baudrate, MP_ARG_INT, {.u_int = 1000000} },
@@ -211,71 +212,6 @@ STATIC mp_obj_t machine_spi_make_new(const mp_obj_type_t *type, size_t n_args, s
211212
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
212213
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
213214

214-
if (args[ARG_NEW_id].u_obj == MP_OBJ_NEW_SMALL_INT(-1)) {
215-
// TODO: implement soft SPI
216-
// return machine_soft_spi_make_new(args);
217-
return mp_const_none;
218-
} else {
219-
// hardware peripheral id given
220-
return machine_hard_spi_make_new(args);
221-
}
222-
}
223-
224-
STATIC mp_obj_t machine_spi_init(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
225-
static const mp_arg_t allowed_args[] = {
226-
{ MP_QSTR_baudrate, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1000000} },
227-
{ MP_QSTR_polarity, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
228-
{ MP_QSTR_phase, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
229-
{ MP_QSTR_bits, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 8} },
230-
{ MP_QSTR_firstbit, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
231-
};
232-
233-
// parse args
234-
mp_obj_t self = pos_args[0];
235-
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
236-
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
237-
238-
// dispatch to specific implementation
239-
if (mp_obj_get_type(self) == &machine_spi_type) {
240-
machine_hard_spi_init(self, args);
241-
}
242-
243-
return mp_const_none;
244-
}
245-
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(machine_spi_init_obj, 1, machine_spi_init);
246-
247-
STATIC mp_obj_t machine_spi_deinit(mp_obj_t self) {
248-
// dispatch to specific implementation
249-
if (mp_obj_get_type(self) == &machine_spi_type) {
250-
machine_hard_spi_deinit(self);
251-
}
252-
return mp_const_none;
253-
}
254-
STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_spi_deinit_obj, machine_spi_deinit);
255-
256-
STATIC const mp_rom_map_elem_t machine_spi_locals_dict_table[] = {
257-
{ MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&machine_spi_init_obj) },
258-
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&machine_spi_deinit_obj) },
259-
260-
{ MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_machine_spi_read_obj) },
261-
{ MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_machine_spi_readinto_obj) },
262-
{ MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_machine_spi_write_obj) },
263-
{ MP_ROM_QSTR(MP_QSTR_write_readinto), MP_ROM_PTR(&mp_machine_spi_write_readinto_obj) },
264-
265-
{ MP_ROM_QSTR(MP_QSTR_MSB), MP_ROM_INT(NRF_SPI_BIT_ORDER_MSB_FIRST) },
266-
{ MP_ROM_QSTR(MP_QSTR_LSB), MP_ROM_INT(NRF_SPI_BIT_ORDER_LSB_FIRST) },
267-
};
268-
269-
STATIC MP_DEFINE_CONST_DICT(machine_spi_locals_dict, machine_spi_locals_dict_table);
270-
271-
/* code for hard implementation ***********************************************/
272-
273-
STATIC void machine_hard_spi_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
274-
machine_hard_spi_obj_t *self = self_in;
275-
mp_printf(print, "SPI(%u)", self->p_spi->drv_inst_idx);
276-
}
277-
278-
STATIC mp_obj_t machine_hard_spi_make_new(mp_arg_val_t *args) {
279215
// get static peripheral object
280216
int spi_id = spi_find(args[ARG_NEW_id].u_obj);
281217
const machine_hard_spi_obj_t *self = &machine_hard_spi_obj[spi_id];
@@ -303,16 +239,12 @@ STATIC mp_obj_t machine_hard_spi_make_new(mp_arg_val_t *args) {
303239
self->p_config->irq_priority = 6;
304240
#endif
305241

306-
mp_obj_t self_obj = MP_OBJ_FROM_PTR(self);
307-
machine_hard_spi_init(self_obj, &args[1]); // Skip instance id param.
242+
machine_hard_spi_init_helper(self, &args[1]); // Skip instance id param.
308243

309-
return self_obj;
244+
return MP_OBJ_FROM_PTR(self);
310245
}
311246

312-
STATIC void machine_hard_spi_init(mp_obj_t self_in, mp_arg_val_t *args) {
313-
314-
const machine_hard_spi_obj_t *self = MP_OBJ_TO_PTR(self_in);
315-
247+
STATIC void machine_hard_spi_init_helper(const machine_hard_spi_obj_t *self, mp_arg_val_t *args) {
316248
int baudrate = args[ARG_INIT_baudrate].u_int;
317249

318250
if (baudrate <= 125000) {
@@ -373,7 +305,24 @@ STATIC void machine_hard_spi_init(mp_obj_t self_in, mp_arg_val_t *args) {
373305
}
374306
}
375307

376-
STATIC void machine_hard_spi_deinit(mp_obj_t self_in) {
308+
STATIC void machine_hard_spi_init(mp_obj_base_t *self_in, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
309+
static const mp_arg_t allowed_args[] = {
310+
{ MP_QSTR_baudrate, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1000000} },
311+
{ MP_QSTR_polarity, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
312+
{ MP_QSTR_phase, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
313+
{ MP_QSTR_bits, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 8} },
314+
{ MP_QSTR_firstbit, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
315+
};
316+
317+
// parse args
318+
machine_hard_spi_obj_t *self = (machine_hard_spi_obj_t *)self_in;
319+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
320+
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
321+
322+
machine_hard_spi_init_helper(self, args);
323+
}
324+
325+
STATIC void machine_hard_spi_deinit(mp_obj_base_t *self_in) {
377326
const machine_hard_spi_obj_t *self = MP_OBJ_TO_PTR(self_in);
378327
nrfx_spi_uninit(self->p_spi);
379328
}
@@ -383,59 +332,20 @@ STATIC void machine_hard_spi_transfer(mp_obj_base_t *self_in, size_t len, const
383332
spi_transfer(self, len, src, dest);
384333
}
385334

386-
387-
STATIC mp_obj_t mp_machine_spi_read(size_t n_args, const mp_obj_t *args) {
388-
vstr_t vstr;
389-
vstr_init_len(&vstr, mp_obj_get_int(args[1]));
390-
memset(vstr.buf, n_args == 3 ? mp_obj_get_int(args[2]) : 0, vstr.len);
391-
spi_transfer(args[0], vstr.len, vstr.buf, vstr.buf);
392-
return mp_obj_new_bytes_from_vstr(&vstr);
393-
}
394-
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_machine_spi_read_obj, 2, 3, mp_machine_spi_read);
395-
396-
STATIC mp_obj_t mp_machine_spi_readinto(size_t n_args, const mp_obj_t *args) {
397-
mp_buffer_info_t bufinfo;
398-
mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_WRITE);
399-
memset(bufinfo.buf, n_args == 3 ? mp_obj_get_int(args[2]) : 0, bufinfo.len);
400-
spi_transfer(args[0], bufinfo.len, bufinfo.buf, bufinfo.buf);
401-
return mp_const_none;
402-
}
403-
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_machine_spi_readinto_obj, 2, 3, mp_machine_spi_readinto);
404-
405-
STATIC mp_obj_t mp_machine_spi_write(mp_obj_t self, mp_obj_t wr_buf) {
406-
mp_buffer_info_t src;
407-
mp_get_buffer_raise(wr_buf, &src, MP_BUFFER_READ);
408-
spi_transfer(self, src.len, (const uint8_t*)src.buf, NULL);
409-
return mp_const_none;
410-
}
411-
MP_DEFINE_CONST_FUN_OBJ_2(mp_machine_spi_write_obj, mp_machine_spi_write);
412-
413-
STATIC mp_obj_t mp_machine_spi_write_readinto(mp_obj_t self, mp_obj_t wr_buf, mp_obj_t rd_buf) {
414-
mp_buffer_info_t src;
415-
mp_get_buffer_raise(wr_buf, &src, MP_BUFFER_READ);
416-
mp_buffer_info_t dest;
417-
mp_get_buffer_raise(rd_buf, &dest, MP_BUFFER_WRITE);
418-
if (src.len != dest.len) {
419-
mp_raise_ValueError(MP_ERROR_TEXT("buffers must be the same length"));
420-
}
421-
spi_transfer(self, src.len, src.buf, dest.buf);
422-
return mp_const_none;
423-
}
424-
MP_DEFINE_CONST_FUN_OBJ_3(mp_machine_spi_write_readinto_obj, mp_machine_spi_write_readinto);
425-
426-
427335
STATIC const mp_machine_spi_p_t machine_hard_spi_p = {
336+
.init = machine_hard_spi_init,
337+
.deinit = machine_hard_spi_deinit,
428338
.transfer = machine_hard_spi_transfer,
429339
};
430340

431341
MP_DEFINE_CONST_OBJ_TYPE(
432342
machine_spi_type,
433343
MP_QSTR_SPI,
434344
MP_TYPE_FLAG_NONE,
435-
make_new, machine_spi_make_new,
345+
make_new, machine_hard_spi_make_new,
436346
print, machine_hard_spi_print,
437347
protocol, &machine_hard_spi_p,
438-
locals_dict, &machine_spi_locals_dict
348+
locals_dict, &mp_machine_spi_locals_dict
439349
);
440350

441351
#endif // MICROPY_PY_MACHINE_HW_SPI

ports/nrf/mpconfigport.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,6 @@
161161
#define MICROPY_PY_MACHINE (1)
162162
#define MICROPY_PY_MACHINE_PULSE (0)
163163
#define MICROPY_PY_MACHINE_SOFTI2C (MICROPY_PY_MACHINE_I2C)
164-
#define MICROPY_PY_MACHINE_SPI (0)
165-
#define MICROPY_PY_MACHINE_SPI_MIN_DELAY (0)
166164

167165
#ifndef MICROPY_HW_LED_COUNT
168166
#define MICROPY_HW_LED_COUNT (0)
@@ -189,6 +187,9 @@
189187
#define MICROPY_PY_MACHINE_HW_SPI (1)
190188
#endif
191189

190+
#define MICROPY_PY_MACHINE_SPI (MICROPY_PY_MACHINE_HW_SPI)
191+
#define MICROPY_PY_MACHINE_SPI_MIN_DELAY (0)
192+
192193
#ifndef MICROPY_PY_MACHINE_HW_PWM
193194
#define MICROPY_PY_MACHINE_HW_PWM (0)
194195
#endif

0 commit comments

Comments
 (0)