esp8266/modmachinespi: Add a factory method for SoftSPI/HSPI

This commit is contained in:
Radomir Dopieralski 2016-08-19 18:55:07 +02:00 committed by Paul Sokolovsky
parent 8e7dfea803
commit d076fae219
7 changed files with 79 additions and 4 deletions

View File

@ -78,6 +78,7 @@ SRC_C = \
modpybrtc.c \
modpybadc.c \
modpybuart.c \
modmachinespi.c \
modpybspi.c \
modpybhspi.c \
modesp.c \

View File

@ -141,6 +141,7 @@ SECTIONS
*modpybadc.o(.literal*, .text*)
*modpybuart.o(.literal*, .text*)
*modpybi2c.o(.literal*, .text*)
*modmachinespi.o(.literal*, .text*)
*modpybspi.o(.literal*, .text*)
*modpybhspi.o(.literal*, .text*)
*hspi.o(.literal*, .text*)

View File

@ -251,8 +251,9 @@ 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(&pyb_spi_type) },
{ MP_ROM_QSTR(MP_QSTR_SoftSPI), MP_ROM_PTR(&pyb_spi_type) },
{ MP_ROM_QSTR(MP_QSTR_HSPI), MP_ROM_PTR(&pyb_hspi_type) },
{ MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&machine_spi_type) },
// wake abilities
{ MP_ROM_QSTR(MP_QSTR_DEEPSLEEP), MP_ROM_INT(MACHINE_WAKE_DEEPSLEEP) },

71
esp8266/modmachinespi.c Normal file
View File

@ -0,0 +1,71 @@
/*
* 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,
};

View File

@ -11,6 +11,7 @@ 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_spi_type;
extern const mp_obj_type_t pyb_hspi_type;
extern const mp_obj_type_t machine_spi_type;
MP_DECLARE_CONST_FUN_OBJ(pyb_info_obj);

View File

@ -106,7 +106,7 @@ 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);
}
STATIC 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 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);
pyb_hspi_obj_t *self = m_new_obj(pyb_hspi_obj_t);
self->base.type = &pyb_hspi_type;

View File

@ -131,7 +131,7 @@ STATIC void pyb_spi_init_helper(pyb_spi_obj_t *self, size_t n_args, const mp_obj
mp_hal_pin_input(self->miso);
}
STATIC 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_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);
pyb_spi_obj_t *self = m_new_obj(pyb_spi_obj_t);
self->base.type = &pyb_spi_type;
@ -215,7 +215,7 @@ STATIC MP_DEFINE_CONST_DICT(pyb_spi_locals_dict, pyb_spi_locals_dict_table);
const mp_obj_type_t pyb_spi_type = {
{ &mp_type_type },
.name = MP_QSTR_SPI,
.name = MP_QSTR_SoftSPI,
.print = pyb_spi_print,
.make_new = pyb_spi_make_new,
.locals_dict = (mp_obj_dict_t*)&pyb_spi_locals_dict,