8000 esp8266: Refactor to use extmod implementation of software SPI class. · Felipeasg/micropython@ad16685 · GitHub
[go: up one dir, main page]

Skip to content

Commit ad16685

Browse files
committed
esp8266: Refactor to use extmod implementation of software SPI class.
1 parent 1b6d94b commit ad16685

File tree

6 files changed

+27
-202
lines changed

6 files changed

+27
-202
lines changed

esp8266/Makefile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ SRC_C = \
8282
machine_adc.c \
8383
machine_uart.c \
8484
machine_wdt.c \
85-
machine_spi.c \
8685
machine_hspi.c \
8786
modesp.c \
8887
modnetwork.c \

esp8266/machine_hspi.c

Lines changed: 24 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,17 @@
3636
#include "py/stream.h"
3737
#include "py/mphal.h"
3838
#include "extmod/machine_spi.h"
39-
39+
#include "modmachine.h"
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);
44-
45-
typedef struct _pyb_hspi_obj_t {
42+
typedef struct _machine_hspi_obj_t {
4643
mp_obj_base_t base;
4744
uint32_t baudrate;
4845
uint8_t polarity;
4946
uint8_t phase;
50-
} pyb_hspi_obj_t;
47+
} machine_hspi_obj_t;
5148

52-
53-
STATIC void hspi_transfer(mp_obj_base_t *self_in, size_t len, const uint8_t *src, uint8_t *dest) {
49+
STATIC void machine_hspi_transfer(mp_obj_base_t *self_in, size_t len, const uint8_t *src, uint8_t *dest) {
5450
(void)self_in;
5551

5652
if (dest == NULL) {
@@ -93,16 +89,17 @@ STATIC void hspi_transfer(mp_obj_base_t *self_in, size_t len, const uint8_t *src
9389
/******************************************************************************/
9490
// MicroPython bindings for HSPI
9591

96-
STATIC void pyb_hspi_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
97-
pyb_hspi_obj_t *self = MP_OBJ_TO_PTR(self_in);
92+
STATIC void machine_hspi_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
93+
machine_hspi_obj_t *self = MP_OBJ_TO_PTR(self_in);
9894
mp_printf(print, "HSPI(id=1, baudrate=%u, polarity=%u, phase=%u)",
9995
self->baudrate, self->polarity, self->phase);
10096
}
10197

102-
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) {
103-
enum { ARG_id, ARG_baudrate, ARG_polarity, ARG_phase };
98+
STATIC void machine_hspi_init(mp_obj_base_t *self_in, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
99+
machine_hspi_obj_t *self = (machine_hspi_obj_t*)self_in;
100+
101+
enum { ARG_baudrate, ARG_polarity, ARG_phase };
104102
static const mp_arg_t allowed_args[] = {
105-
{ MP_QSTR_id, MP_ARG_INT, {.u_int = -1} },
106103
{ MP_QSTR_baudrate, MP_ARG_INT, {.u_int = -1} },
107104
{ MP_QSTR_polarity, MP_ARG_INT, {.u_int = -1} },
108105
{ MP_QSTR_phase, MP_ARG_INT, {.u_int = -1} },
@@ -150,63 +147,35 @@ STATIC void pyb_hspi_init_helper(pyb_hspi_obj_t *self, size_t n_args, const mp_o
150147
spi_mode(HSPI, self->phase, self->polarity);
151148
}
152149

153-
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) {
154-
mp_arg_check_num(n_args, n_kw, 0, 1, true);
155-
mp_int_t id = -1;
156-
if (n_args > 0) {
157-
id = mp_obj_get_int(args[0]);
158-
}
159-
160-
if (id == -1) {
161-
// Multiplex to bitbanging SPI
162-
if (n_args > 0) {
163-
args++;
164-
}
165-
return pyb_spi_make_new(type, 0, n_kw, args);
166-
}
167-
168-
if (id != 1) {
150+
mp_obj_t machine_hspi_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
151+
// args[0] holds the id of the peripheral
152+
if (args[0] != MP_OBJ_NEW_SMALL_INT(1)) {
169153
// FlashROM is on SPI0, so far we don't support its usage
170154
mp_raise_ValueError("");
171155
}
172156

173-
pyb_hspi_obj_t *self = m_new_obj(pyb_hspi_obj_t);
174-
self->base.type = &pyb_hspi_type;
157+
machine_hspi_obj_t *self = m_new_obj(machine_hspi_obj_t);
158+
self->base.type = &machine_hspi_type;
175159
// set defaults
176160
self->baudrate = 80000000L;
177161
self->polarity = 0;
178162
self->phase = 0;
179163
mp_map_t kw_args;
180164
mp_map_init_fixed_table(&kw_args, n_kw, args + n_args);
181-
pyb_hspi_init_helper(self, n_args, args, &kw_args);
165+
machine_hspi_init((mp_obj_base_t*)self, n_args - 1, args + 1, &kw_args);
182166
return MP_OBJ_FROM_PTR(self);
183167
}
184168

185-
STATIC mp_obj_t pyb_hspi_init(size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
186-
pyb_hspi_init_helper(args[0], n_args - 1, args + 1, kw_args);
187-
return mp_const_none;
188-
}
189-
MP_DEFINE_CONST_FUN_OBJ_KW(pyb_hspi_init_obj, 1, pyb_hspi_init);
190-
191-
STATIC const mp_rom_map_elem_t pyb_hspi_locals_dict_table[] = {
192-
{ MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&pyb_hspi_init_obj) },
193-
{ MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_machine_spi_read_obj) },
194-
{ MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_machine_spi_readinto_obj) },
195-
{ MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_machine_spi_write_obj) },
196-
{ MP_ROM_QSTR(MP_QSTR_write_readinto), MP_ROM_PTR(&mp_machine_spi_write_readinto_obj) },
197-
};
198-
199-
STATIC MP_DEFINE_CONST_DICT(pyb_hspi_locals_dict, pyb_hspi_locals_dict_table);
200-
201-
STATIC const mp_machine_spi_p_t pyb_hspi_p = {
202-
.transfer = hspi_transfer,
169+
STATIC const mp_machine_spi_p_t machine_hspi_p = {
170+
.init = machine_hspi_init,
171+
.transfer = machine_hspi_transfer,
203172
};
204173

205-
const mp_obj_type_t pyb_hspi_type = {
174+
const mp_obj_type_t machine_hspi_type = {
206175
{ &mp_type_type },
207176
.name = MP_QSTR_HSPI,
208-
.print = pyb_hspi_print,
209-
.make_new = pyb_hspi_make_new,
210-
.protocol = &pyb_hspi_p,
211-
.locals_dict = (mp_obj_dict_t*)&pyb_hspi_locals_dict,
177+
.print = machine_hspi_print,
178+
.make_new = mp_machine_spi_make_new, // delegate to master constructor
179+
.protocol = &machine_hspi_p,
180+
.locals_dict = (mp_obj_dict_t*)&mp_machine_spi_locals_dict,
212181
};

esp8266/machine_spi.c

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

esp8266/modmachine.c

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

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

esp8266/modmachine.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ extern const mp_obj_type_t pyb_adc_type;
99
extern const mp_obj_type_t pyb_rtc_type;
1010
extern const mp_obj_type_t pyb_uart_type;
1111
extern const mp_obj_type_t pyb_i2c_type;
12-
extern const mp_obj_type_t pyb_spi_type;
13-
extern const mp_obj_type_t pyb_hspi_type;
14-
extern const mp_obj_type_t machine_spi_type;
12+
extern const mp_obj_type_t machine_hspi_type;
1513

1614
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_info_obj);
1715

esp8266/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
#define MICROPY_PY_MACHINE_PULSE (1)
7272
#define MICROPY_PY_MACHINE_I2C (1)
7373
#define MICROPY_PY_MACHINE_SPI (1)
74+
#define MICROPY_PY_MACHINE_SPI_MAKE_NEW machine_hspi_make_new
7475
#define MICROPY_PY_WEBSOCKET (1)
7576
#define MICROPY_PY_WEBREPL (1)
7677
#define MICROPY_PY_WEBREPL_DELAY (20)

0 commit comments

Comments
 (0)
0