Skip to content

Commit dba40af

Browse files
committed
esp8266/modmachine: Simplify SPI class implementation multiplexing.
modpybhspi now does the needed multiplexing, calling out to modpybspi (bitbanging SPI) for suitable peripheral ID's. modmachinespi (previous multiplexer class) thus not needed and removed. modpybhspi also updated to following standard SPI peripheral naming: SPI0 is used for FlashROM and thus not supported so far. SPI1 is available for users, and thus needs to be instantiated as: spi = machine.SPI(1, ...)
1 parent 7ddd1a5 commit dba40af

5 files changed

Lines changed: 25 additions & 77 deletions

File tree

esp8266/Makefile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ SRC_C = \
7979
modpybadc.c \
8080
modpybuart.c \
8181
modmachinewdt.c \
82-
modmachinespi.c \
8382
modpybspi.c \
8483
modpybhspi.c \
8584
modesp.c \

esp8266/esp8266.ld

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,6 @@ SECTIONS
141141
*modpybadc.o(.literal*, .text*)
142142
*modpybuart.o(.literal*, .text*)
143143
*modpybi2c.o(.literal*, .text*)
144-
*modmachinespi.o(.literal*, .text*)
145144
*modmachinewdt.o(.literal*, .text*)
146145
*modpybspi.o(.literal*, .text*)
147146
*modpybhspi.o(.literal*, .text*)

esp8266/modmachine.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = {
254254
{ MP_ROM_QSTR(MP_QSTR_ADC), MP_ROM_PTR(&pyb_adc_type) },
255255
{ MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&pyb_uart_type) },
256256
{ MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&machine_i2c_type) },
257-
{ MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&machine_spi_type) },
257+
{ MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&pyb_hspi_type) },
258258

259259
// wake abilities
260260
{ MP_ROM_QSTR(MP_QSTR_DEEPSLEEP), MP_ROM_INT(MACHINE_WAKE_DEEPSLEEP) },

esp8266/modmachinespi.c

Lines changed: 0 additions & 71 deletions
This file was deleted.

esp8266/modpybhspi.c

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939

4040
#include "hspi.h"
4141

42+
mp_obj_t pyb_spi_make_new(const mp_obj_type_t *type, size_t n_args,
43+
size_t n_kw, const mp_obj_t *args);
4244

4345
typedef struct _pyb_hspi_obj_t {
4446
mp_obj_base_t base;
@@ -105,13 +107,14 @@ STATIC void hspi_transfer(mp_obj_base_t *self_in, size_t src_len, const uint8_t
105107

106108
STATIC void pyb_hspi_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
107109
pyb_hspi_obj_t *self = MP_OBJ_TO_PTR(self_in);
108-
mp_printf(print, "HSPI(baudrate=%u, polarity=%u, phase=%u)",
110+
mp_printf(print, "HSPI(id=1, baudrate=%u, polarity=%u, phase=%u)",
109111
self->baudrate, self->polarity, self->phase);
110112
}
111113

112114
STATIC void pyb_hspi_init_helper(pyb_hspi_obj_t *self, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
113-
enum { ARG_baudrate, ARG_polarity, ARG_phase };
115+
enum { ARG_id, ARG_baudrate, ARG_polarity, ARG_phase };
114116
static const mp_arg_t allowed_args[] = {
117+
{ MP_QSTR_id, MP_ARG_INT, {.u_int = -1} },
115118
{ MP_QSTR_baudrate, MP_ARG_INT, {.u_int = -1} },
116119
{ MP_QSTR_polarity, MP_ARG_INT, {.u_int = -1} },
117120
{ MP_QSTR_phase, MP_ARG_INT, {.u_int = -1} },
@@ -160,7 +163,25 @@ STATIC void pyb_hspi_init_helper(pyb_hspi_obj_t *self, size_t n_args, const mp_o
160163
}
161164

162165
mp_obj_t pyb_hspi_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
163-
mp_arg_check_num(n_args, n_kw, 0, MP_OBJ_FUN_ARGS_MAX, true);
166+
mp_arg_check_num(n_args, n_kw, 0, 1, true);
167+
mp_int_t id = -1;
168+
if (n_args > 0) {
169+
id = mp_obj_get_int(args[0]);
170+
}
171+
172+
if (id == -1) {
173+
// Multiplex to bitbanging SPI
174+
if (n_args > 0) {
175+
args++;
176+
}
177+
return pyb_spi_make_new(type, 0, n_kw, args);
178+
}
179+
180+
if (id != 1) {
181+
// FlashROM is on SPI0, so far we don't support its usage
182+
mp_raise_ValueError("");
183+
}
184+
164185
pyb_hspi_obj_t *self = m_new_obj(pyb_hspi_obj_t);
165186
self->base.type = &pyb_hspi_type;
166187
// set defaults

0 commit comments

Comments
 (0)