Commit dba40afa authored by Paul Sokolovsky's avatar Paul Sokolovsky
Browse files

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, ...)
parent 7ddd1a58
......@@ -79,7 +79,6 @@ SRC_C = \
modpybadc.c \
modpybuart.c \
modmachinewdt.c \
modmachinespi.c \
modpybspi.c \
modpybhspi.c \
modesp.c \
......
......@@ -141,7 +141,6 @@ SECTIONS
*modpybadc.o(.literal*, .text*)
*modpybuart.o(.literal*, .text*)
*modpybi2c.o(.literal*, .text*)
*modmachinespi.o(.literal*, .text*)
*modmachinewdt.o(.literal*, .text*)
*modpybspi.o(.literal*, .text*)
*modpybhspi.o(.literal*, .text*)
......
......@@ -254,7 +254,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_UART), MP_ROM_PTR(&pyb_uart_type) },
{ MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&machine_i2c_type) },
{ MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&machine_spi_type) },
{ MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&pyb_hspi_type) },
// wake abilities
{ MP_ROM_QSTR(MP_QSTR_DEEPSLEEP), MP_ROM_INT(MACHINE_WAKE_DEEPSLEEP) },
......
/*
* 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 "ets_sys.h"
#include "etshal.h"
#include "ets_alt_task.h"
#include "py/runtime.h"
#include "py/stream.h"
#include "py/mphal.h"
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_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);
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 *args) {
mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true);
switch (mp_obj_get_int(args[0])) {
case -1:
return pyb_spi_make_new(type, n_args - 1, n_kw, args + 1);
case 0:
return pyb_hspi_make_new(type, n_args - 1, n_kw, args + 1);
default:
nlr_raise(mp_obj_new_exception_msg_varg(
&mp_type_ValueError, "no such SPI peripheral"));
}
}
STATIC const mp_rom_map_elem_t machine_spi_locals_dict_table[] = {};
STATIC MP_DEFINE_CONST_DICT(machine_spi_locals_dict,
machine_spi_locals_dict_table);
const mp_obj_type_t machine_spi_type = {
{ &mp_type_type },
.name = MP_QSTR_SPI,
.make_new = machine_spi_make_new,
.locals_dict = (mp_obj_dict_t*)&machine_spi_locals_dict,
};
......@@ -39,6 +39,8 @@
#include "hspi.h"
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);
typedef struct _pyb_hspi_obj_t {
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
STATIC void pyb_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);
mp_printf(print, "HSPI(baudrate=%u, polarity=%u, phase=%u)",
mp_printf(print, "HSPI(id=1, baudrate=%u, polarity=%u, phase=%u)",
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) {
enum { ARG_baudrate, ARG_polarity, ARG_phase };
enum { ARG_id, ARG_baudrate, ARG_polarity, ARG_phase };
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_polarity, MP_ARG_INT, {.u_int = -1} },
{ 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
}
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_arg_check_num(n_args, n_kw, 0, MP_OBJ_FUN_ARGS_MAX, true);
mp_arg_check_num(n_args, n_kw, 0, 1, true);
mp_int_t id = -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
mp_raise_ValueError("");
}
pyb_hspi_obj_t *self = m_new_obj(pyb_hspi_obj_t);
self->base.type = &pyb_hspi_type;
// set defaults
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment