Skip to content

Commit d076fae

Browse files
deshipupfalcon
authored andcommitted
esp8266/modmachinespi: Add a factory method for SoftSPI/HSPI
1 parent 8e7dfea commit d076fae

File tree

7 files changed

+79
-4
lines changed

7 files changed

+79
-4
lines changed

esp8266/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ SRC_C = \
7878
modpybrtc.c \
7979
modpybadc.c \
8080
modpybuart.c \
81+
modmachinespi.c \
8182
modpybspi.c \
8283
modpybhspi.c \
8384
modesp.c \

esp8266/esp8266.ld

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ SECTIONS
141141
*modpybadc.o(.literal*, .text*)
142142
*modpybuart.o(.literal*, .text*)
143143
*modpybi2c.o(.literal*, .text*)
144+
*modmachinespi.o(.literal*, .text*)
144145
*modpybspi.o(.literal*, .text*)
145146
*modpybhspi.o(.literal*, .text*)
146147
*hspi.o(.literal*, .text*)

esp8266/modmachine.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,9 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = {
251251
{ MP_ROM_QSTR(MP_QSTR_ADC), MP_ROM_PTR(&pyb_adc_type) },
252252
{ MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&pyb_uart_type) },
253253
{ MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&machine_i2c_type) },
254-
{ MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&pyb_spi_type) },
254+
{ MP_ROM_QSTR(MP_QSTR_SoftSPI), MP_ROM_PTR(&pyb_spi_type) },
255255
{ MP_ROM_QSTR(MP_QSTR_HSPI), MP_ROM_PTR(&pyb_hspi_type) },
256+
{ MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&machine_spi_type) },
256257

257258
// wake abilities
258259
{ MP_ROM_QSTR(MP_QSTR_DEEPSLEEP), MP_ROM_INT(MACHINE_WAKE_DEEPSLEEP) },

esp8266/modmachinespi.c

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2016 Damien P. George
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include <stdio.h>
28+
#include <stdint.h>
29+
#include <string.h>
30+
31+
#include "ets_sys.h"
32+
#include "etshal.h"
33+
#include "ets_alt_task.h"
34+
35+
#include "py/runtime.h"
36+
#include "py/stream.h"
37+
#include "py/mphal.h"
38+
39+
40+
mp_obj_t pyb_spi_make_new(const mp_obj_type_t *type, size_t n_args,
41+
size_t n_kw, const mp_obj_t *args);
42+
mp_obj_t pyb_hspi_make_new(const mp_obj_type_t *type, size_t n_args,
43+
size_t n_kw, const mp_obj_t *args);
44+
45+
46+
STATIC mp_obj_t machine_spi_make_new(const mp_obj_type_t *type, size_t n_args,
47+
size_t n_kw, const mp_obj_t *args) {
48+
mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true);
49+
switch (mp_obj_get_int(args[0])) {
50+
case -1:
51+
return pyb_spi_make_new(type, n_args - 1, n_kw, args + 1);
52+
case 0:
53+
return pyb_hspi_make_new(type, n_args - 1, n_kw, args + 1);
54+
default:
55+
nlr_raise(mp_obj_new_exception_msg_varg(
56+
&mp_type_ValueError, "no such SPI peripheral"));
57+
}
58+
}
59+
60+
61+
STATIC const mp_rom_map_elem_t machine_spi_locals_dict_table[] = {};
62+
63+
STATIC MP_DEFINE_CONST_DICT(machine_spi_locals_dict,
64+
machine_spi_locals_dict_table);
65+
66+
const mp_obj_type_t machine_spi_type = {
67+
{ &mp_type_type },
68+
.name = MP_QSTR_SPI,
69+
.make_new = machine_spi_make_new,
70+
.locals_dict = (mp_obj_dict_t*)&machine_spi_locals_dict,
71+
};

esp8266/modpyb.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ extern const mp_obj_type_t pyb_uart_type;
1111
extern const mp_obj_type_t pyb_i2c_type;
1212
extern const mp_obj_type_t pyb_spi_type;
1313
extern const mp_obj_type_t pyb_hspi_type;
14+
extern const mp_obj_type_t machine_spi_type;
1415

1516
MP_DECLARE_CONST_FUN_OBJ(pyb_info_obj);
1617

esp8266/modpybhspi.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ STATIC void pyb_hspi_init_helper(pyb_hspi_obj_t *self, size_t n_args, const mp_o
106106
spi_mode(HSPI, self->phase, self->polarity);
107107
}
108108

109-
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) {
109+
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) {
110110
mp_arg_check_num(n_args, n_kw, 0, MP_OBJ_FUN_ARGS_MAX, true);
111111
pyb_hspi_obj_t *self = m_new_obj(pyb_hspi_obj_t);
112112
self->base.type = &pyb_hspi_type;

esp8266/modpybspi.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ STATIC void pyb_spi_init_helper(pyb_spi_obj_t *self, size_t n_args, const mp_obj
131131
mp_hal_pin_input(self->miso);
132132
}
133133

134-
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) {
134+
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) {
135135
mp_arg_check_num(n_args, n_kw, 0, MP_OBJ_FUN_ARGS_MAX, true);
136136
pyb_spi_obj_t *self = m_new_obj(pyb_spi_obj_t);
137137
self->base.type = &pyb_spi_type;
@@ -215,7 +215,7 @@ STATIC MP_DEFINE_CONST_DICT(pyb_spi_locals_dict, pyb_spi_locals_dict_table);
215215

216216
const mp_obj_type_t pyb_spi_type = {
217217
{ &mp_type_type },
218-
.name = MP_QSTR_SPI,
218+
.name = MP_QSTR_SoftSPI,
219219
.print = pyb_spi_print,
220220
.make_new = pyb_spi_make_new,
221221
.locals_dict = (mp_obj_dict_t*)&pyb_spi_locals_dict,

0 commit comments

Comments
 (0)