esp8266: Refactor to use extmod implementation of software SPI class.

This commit is contained in:
Damien George 2016-12-08 14:41:58 +11:00
parent 1b6d94bccd
commit ad166857bc
6 changed files with 27 additions and 202 deletions

View File

@ -82,7 +82,6 @@ SRC_C = \
machine_adc.c \ machine_adc.c \
machine_uart.c \ machine_uart.c \
machine_wdt.c \ machine_wdt.c \
machine_spi.c \
machine_hspi.c \ machine_hspi.c \
modesp.c \ modesp.c \
modnetwork.c \ modnetwork.c \

View File

@ -36,21 +36,17 @@
#include "py/stream.h" #include "py/stream.h"
#include "py/mphal.h" #include "py/mphal.h"
#include "extmod/machine_spi.h" #include "extmod/machine_spi.h"
#include "modmachine.h"
#include "hspi.h" #include "hspi.h"
mp_obj_t pyb_spi_make_new(const mp_obj_type_t *type, size_t n_args, typedef struct _machine_hspi_obj_t {
size_t n_kw, const mp_obj_t *args);
typedef struct _pyb_hspi_obj_t {
mp_obj_base_t base; mp_obj_base_t base;
uint32_t baudrate; uint32_t baudrate;
uint8_t polarity; uint8_t polarity;
uint8_t phase; uint8_t phase;
} pyb_hspi_obj_t; } machine_hspi_obj_t;
STATIC void machine_hspi_transfer(mp_obj_base_t *self_in, size_t len, const uint8_t *src, uint8_t *dest) {
STATIC void hspi_transfer(mp_obj_base_t *self_in, size_t len, const uint8_t *src, uint8_t *dest) {
(void)self_in; (void)self_in;
if (dest == NULL) { if (dest == NULL) {
@ -93,16 +89,17 @@ STATIC void hspi_transfer(mp_obj_base_t *self_in, size_t len, const uint8_t *src
/******************************************************************************/ /******************************************************************************/
// MicroPython bindings for HSPI // MicroPython bindings for HSPI
STATIC void pyb_hspi_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { STATIC void machine_hspi_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
pyb_hspi_obj_t *self = MP_OBJ_TO_PTR(self_in); machine_hspi_obj_t *self = MP_OBJ_TO_PTR(self_in);
mp_printf(print, "HSPI(id=1, baudrate=%u, polarity=%u, phase=%u)", mp_printf(print, "HSPI(id=1, baudrate=%u, polarity=%u, phase=%u)",
self->baudrate, self->polarity, self->phase); self->baudrate, self->polarity, self->phase);
} }
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) { 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) {
enum { ARG_id, ARG_baudrate, ARG_polarity, ARG_phase }; machine_hspi_obj_t *self = (machine_hspi_obj_t*)self_in;
enum { ARG_baudrate, ARG_polarity, ARG_phase };
static const mp_arg_t allowed_args[] = { static const mp_arg_t allowed_args[] = {
{ MP_QSTR_id, MP_ARG_INT, {.u_int = -1} },
{ MP_QSTR_baudrate, MP_ARG_INT, {.u_int = -1} }, { MP_QSTR_baudrate, MP_ARG_INT, {.u_int = -1} },
{ MP_QSTR_polarity, MP_ARG_INT, {.u_int = -1} }, { MP_QSTR_polarity, MP_ARG_INT, {.u_int = -1} },
{ MP_QSTR_phase, MP_ARG_INT, {.u_int = -1} }, { 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
spi_mode(HSPI, self->phase, self->polarity); spi_mode(HSPI, self->phase, self->polarity);
} }
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) { 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) {
mp_arg_check_num(n_args, n_kw, 0, 1, true); // args[0] holds the id of the peripheral
mp_int_t id = -1; if (args[0] != MP_OBJ_NEW_SMALL_INT(1)) {
if (n_args > 0) {
id = mp_obj_get_int(args[0]);
}
if (id == -1) {
// Multiplex to bitbanging SPI
if (n_args > 0) {
args++;
}
return pyb_spi_make_new(type, 0, n_kw, args);
}
if (id != 1) {
// FlashROM is on SPI0, so far we don't support its usage // FlashROM is on SPI0, so far we don't support its usage
mp_raise_ValueError(""); mp_raise_ValueError("");
} }
pyb_hspi_obj_t *self = m_new_obj(pyb_hspi_obj_t); machine_hspi_obj_t *self = m_new_obj(machine_hspi_obj_t);
self->base.type = &pyb_hspi_type; self->base.type = &machine_hspi_type;
// set defaults // set defaults
self->baudrate = 80000000L; self->baudrate = 80000000L;
self->polarity = 0; self->polarity = 0;
self->phase = 0; self->phase = 0;
mp_map_t kw_args; mp_map_t kw_args;
mp_map_init_fixed_table(&kw_args, n_kw, args + n_args); mp_map_init_fixed_table(&kw_args, n_kw, args + n_args);
pyb_hspi_init_helper(self, n_args, args, &kw_args); machine_hspi_init((mp_obj_base_t*)self, n_args - 1, args + 1, &kw_args);
return MP_OBJ_FROM_PTR(self); return MP_OBJ_FROM_PTR(self);
} }
STATIC mp_obj_t pyb_hspi_init(size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) { STATIC const mp_machine_spi_p_t machine_hspi_p = {
pyb_hspi_init_helper(args[0], n_args - 1, args + 1, kw_args); .init = machine_hspi_init,
return mp_const_none; .transfer = machine_hspi_transfer,
}
MP_DEFINE_CONST_FUN_OBJ_KW(pyb_hspi_init_obj, 1, pyb_hspi_init);
STATIC const mp_rom_map_elem_t pyb_hspi_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&pyb_hspi_init_obj) },
{ MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_machine_spi_read_obj) },
{ MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_machine_spi_readinto_obj) },
{ MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_machine_spi_write_obj) },
{ MP_ROM_QSTR(MP_QSTR_write_readinto), MP_ROM_PTR(&mp_machine_spi_write_readinto_obj) },
}; };
STATIC MP_DEFINE_CONST_DICT(pyb_hspi_locals_dict, pyb_hspi_locals_dict_table); const mp_obj_type_t machine_hspi_type = {
STATIC const mp_machine_spi_p_t pyb_hspi_p = {
.transfer = hspi_transfer,
};
const mp_obj_type_t pyb_hspi_type = {
{ &mp_type_type }, { &mp_type_type },
.name = MP_QSTR_HSPI, .name = MP_QSTR_HSPI,
.print = pyb_hspi_print, .print = machine_hspi_print,
.make_new = pyb_hspi_make_new, .make_new = mp_machine_spi_make_new, // delegate to master constructor
.protocol = &pyb_hspi_p, .protocol = &machine_hspi_p,
.locals_dict = (mp_obj_dict_t*)&pyb_hspi_locals_dict, .locals_dict = (mp_obj_dict_t*)&mp_machine_spi_locals_dict,
}; };

View File

@ -1,142 +0,0 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2016 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include "py/runtime.h"
#include "py/stream.h"
#include "py/mphal.h"
#include "extmod/machine_spi.h"
/******************************************************************************/
// MicroPython bindings for SPI
STATIC uint32_t baudrate_from_delay_half(uint32_t delay_half) {
return 500000 / delay_half;
}
STATIC uint32_t baudrate_to_delay_half(uint32_t baudrate) {
uint32_t delay_half = 500000 / baudrate;
// round delay_half up so that: actual_baudrate <= requested_baudrate
if (500000 % baudrate != 0) {
delay_half += 1;
}
return delay_half;
}
STATIC void pyb_spi_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
mp_machine_soft_spi_obj_t *self = MP_OBJ_TO_PTR(self_in);
mp_printf(print, "SPI(baudrate=%u, polarity=%u, phase=%u, sck=%u, mosi=%u, miso=%u)",
baudrate_from_delay_half(self->delay_half),
self->polarity, self->phase, self->sck, self->mosi, self->miso);
}
STATIC void pyb_spi_init_helper(mp_machine_soft_spi_obj_t *self, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum { ARG_baudrate, ARG_polarity, ARG_phase, ARG_sck, ARG_mosi, ARG_miso };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_baudrate, MP_ARG_INT, {.u_int = -1} },
{ MP_QSTR_polarity, MP_ARG_INT, {.u_int = -1} },
{ MP_QSTR_phase, MP_ARG_INT, {.u_int = -1} },
{ MP_QSTR_sck, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
{ MP_QSTR_mosi, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
{ MP_QSTR_miso, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
};
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
if (args[ARG_baudrate].u_int != -1) {
self->delay_half = baudrate_to_delay_half(args[ARG_baudrate].u_int);
}
if (args[ARG_polarity].u_int != -1) {
self->polarity = args[ARG_polarity].u_int;
}
if (args[ARG_phase].u_int != -1) {
self->phase = args[ARG_phase].u_int;
}
if (args[ARG_sck].u_obj != MP_OBJ_NULL) {
self->sck = mp_hal_get_pin_obj(args[ARG_sck].u_obj);
}
if (args[ARG_mosi].u_obj != MP_OBJ_NULL) {
self->mosi = mp_hal_get_pin_obj(args[ARG_mosi].u_obj);
}
if (args[ARG_miso].u_obj != MP_OBJ_NULL) {
self->miso = mp_hal_get_pin_obj(args[ARG_miso].u_obj);
}
// configure pins
mp_hal_pin_write(self->sck, self->polarity);
mp_hal_pin_output(self->sck);
mp_hal_pin_output(self->mosi);
mp_hal_pin_input(self->miso);
}
mp_obj_t pyb_spi_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
mp_arg_check_num(n_args, n_kw, 0, MP_OBJ_FUN_ARGS_MAX, true);
mp_machine_soft_spi_obj_t *self = m_new_obj(mp_machine_soft_spi_obj_t);
self->base.type = &pyb_spi_type;
// set defaults
self->delay_half = baudrate_to_delay_half(500000);
self->polarity = 0;
self->phase = 0;
self->sck = 14;
self->mosi = 13;
self->miso = 12;
mp_map_t kw_args;
mp_map_init_fixed_table(&kw_args, n_kw, args + n_args);
pyb_spi_init_helper(self, n_args, args, &kw_args);
return MP_OBJ_FROM_PTR(self);
}
STATIC mp_obj_t pyb_spi_init(size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
pyb_spi_init_helper(args[0], n_args - 1, args + 1, kw_args);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_KW(pyb_spi_init_obj, 1, pyb_spi_init);
STATIC const mp_rom_map_elem_t pyb_spi_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&pyb_spi_init_obj) },
{ MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_machine_spi_read_obj) },
{ MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_machine_spi_readinto_obj) },
{ MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_machine_spi_write_obj) },
{ MP_ROM_QSTR(MP_QSTR_write_readinto), MP_ROM_PTR(&mp_machine_spi_write_readinto_obj) },
};
STATIC MP_DEFINE_CONST_DICT(pyb_spi_locals_dict, pyb_spi_locals_dict_table);
STATIC const mp_machine_spi_p_t pyb_spi_p = {
.transfer = mp_machine_soft_spi_transfer,
};
const mp_obj_type_t pyb_spi_type = {
{ &mp_type_type },
.name = MP_QSTR_SoftSPI,
.print = pyb_spi_print,
.make_new = pyb_spi_make_new,
.protocol = &pyb_spi_p,
.locals_dict = (mp_obj_dict_t*)&pyb_spi_locals_dict,
};

View File

@ -255,7 +255,7 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR_ADC), MP_ROM_PTR(&pyb_adc_type) }, { MP_ROM_QSTR(MP_QSTR_ADC), MP_ROM_PTR(&pyb_adc_type) },
{ MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&pyb_uart_type) }, { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&pyb_uart_type) },
{ MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&machine_i2c_type) }, { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&machine_i2c_type) },
{ MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&pyb_hspi_type) }, { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&machine_hspi_type) },
// wake abilities // wake abilities
{ MP_ROM_QSTR(MP_QSTR_DEEPSLEEP), MP_ROM_INT(MACHINE_WAKE_DEEPSLEEP) }, { MP_ROM_QSTR(MP_QSTR_DEEPSLEEP), MP_ROM_INT(MACHINE_WAKE_DEEPSLEEP) },

View File

@ -9,9 +9,7 @@ extern const mp_obj_type_t pyb_adc_type;
extern const mp_obj_type_t pyb_rtc_type; extern const mp_obj_type_t pyb_rtc_type;
extern const mp_obj_type_t pyb_uart_type; extern const mp_obj_type_t pyb_uart_type;
extern const mp_obj_type_t pyb_i2c_type; extern const mp_obj_type_t pyb_i2c_type;
extern const mp_obj_type_t pyb_spi_type; extern const mp_obj_type_t machine_hspi_type;
extern const mp_obj_type_t pyb_hspi_type;
extern const mp_obj_type_t machine_spi_type;
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_info_obj); MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_info_obj);

View File

@ -71,6 +71,7 @@
#define MICROPY_PY_MACHINE_PULSE (1) #define MICROPY_PY_MACHINE_PULSE (1)
#define MICROPY_PY_MACHINE_I2C (1) #define MICROPY_PY_MACHINE_I2C (1)
#define MICROPY_PY_MACHINE_SPI (1) #define MICROPY_PY_MACHINE_SPI (1)
#define MICROPY_PY_MACHINE_SPI_MAKE_NEW machine_hspi_make_new
#define MICROPY_PY_WEBSOCKET (1) #define MICROPY_PY_WEBSOCKET (1)
#define MICROPY_PY_WEBREPL (1) #define MICROPY_PY_WEBREPL (1)
#define MICROPY_PY_WEBREPL_DELAY (20) #define MICROPY_PY_WEBREPL_DELAY (20)