Skip to content

Commit 58ebeca

Browse files
committed
drivers/bus: Pull out software SPI implementation to dedicated driver.
This patch takes the software SPI implementation from extmod/machine_spi.c and moves it to a dedicated file in drivers/bus/softspi.c. This allows the SPI driver to be used independently of the uPy runtime, making it a more general component.
1 parent ad2a6e5 commit 58ebeca

11 files changed

Lines changed: 202 additions & 98 deletions

File tree

drivers/bus/softspi.c

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2016-2018 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 "drivers/bus/spi.h"
28+
29+
int mp_soft_spi_ioctl(void *self_in, uint32_t cmd) {
30+
mp_soft_spi_obj_t *self = (mp_soft_spi_obj_t*)self_in;
31+
32+
switch (cmd) {
33+
case MP_SPI_IOCTL_INIT:
34+
mp_hal_pin_write(self->sck, self->polarity);
35+
mp_hal_pin_output(self->sck);
36+
mp_hal_pin_output(self->mosi);
37+
mp_hal_pin_input(self->miso);
38+
break;
39+
40+
case MP_SPI_IOCTL_DEINIT:
41+
break;
42+
}
43+
44+
return 0;
45+
}
46+
47+
void mp_soft_spi_transfer(void *self_in, size_t len, const uint8_t *src, uint8_t *dest) {
48+
mp_soft_spi_obj_t *self = (mp_soft_spi_obj_t*)self_in;
49+
uint32_t delay_half = self->delay_half;
50+
51+
// only MSB transfer is implemented
52+
53+
// If a port defines MICROPY_HW_SOFTSPI_MIN_DELAY, and the configured
54+
// delay_half is equal to this value, then the software SPI implementation
55+
// will run as fast as possible, limited only by CPU speed and GPIO time.
56+
#ifdef MICROPY_HW_SOFTSPI_MIN_DELAY
57+
if (delay_half == MICROPY_HW_SOFTSPI_MIN_DELAY) {
58+
for (size_t i = 0; i < len; ++i) {
59+
uint8_t data_out = src[i];
60+
uint8_t data_in = 0;
61+
for (int j = 0; j < 8; ++j, data_out <<= 1) {
62+
mp_hal_pin_write(self->mosi, (data_out >> 7) & 1);
63+
mp_hal_pin_write(self->sck, 1 - self->polarity);
64+
data_in = (data_in << 1) | mp_hal_pin_read(self->miso);
65+
mp_hal_pin_write(self->sck, self->polarity);
66+
}
67+
if (dest != NULL) {
68+
dest[i] = data_in;
69+
}
70+
}
71+
return;
72+
}
73+
#endif
74+
75+
for (size_t i = 0; i < len; ++i) {
76+
uint8_t data_out = src[i];
77+
uint8_t data_in = 0;
78+
for (int j = 0; j < 8; ++j, data_out <<= 1) {
79+
mp_hal_pin_write(self->mosi, (data_out >> 7) & 1);
80+
if (self->phase == 0) {
81+
mp_hal_delay_us_fast(delay_half);
82+
mp_hal_pin_write(self->sck, 1 - self->polarity);
83+
} else {
84+
mp_hal_pin_write(self->sck, 1 - self->polarity);
85+
mp_hal_delay_us_fast(delay_half);
86+
}
87+
data_in = (data_in << 1) | mp_hal_pin_read(self->miso);
88+
if (self->phase == 0) {
89+
mp_hal_delay_us_fast(delay_half);
90+
mp_hal_pin_write(self->sck, self->polarity);
91+
} else {
92+
mp_hal_pin_write(self->sck, self->polarity);
93+
mp_hal_delay_us_fast(delay_half);
94+
}
95+
}
96+
if (dest != NULL) {
97+
dest[i] = data_in;
98+
}
99+
}
100+
}
101+
102+
const mp_spi_proto_t mp_soft_spi_proto = {
103+
.ioctl = mp_soft_spi_ioctl,
104+
.transfer = mp_soft_spi_transfer,
105+
};

drivers/bus/spi.h

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2016-2018 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+
#ifndef MICROPY_INCLUDED_DRIVERS_BUS_SPI_H
27+
#define MICROPY_INCLUDED_DRIVERS_BUS_SPI_H
28+
29+
#include "py/mphal.h"
30+
31+
enum {
32+
MP_SPI_IOCTL_INIT,
33+
MP_SPI_IOCTL_DEINIT,
34+
};
35+
36+
typedef struct _mp_spi_proto_t {
37+
int (*ioctl)(void *self, uint32_t cmd);
38+
void (*transfer)(void *self, size_t len, const uint8_t *src, uint8_t *dest);
39+
} mp_spi_proto_t;
40+
41+
typedef struct _mp_soft_spi_obj_t {
42+
uint32_t delay_half; // microsecond delay for half SCK period
43+
uint8_t polarity;
44+
uint8_t phase;
45+
mp_hal_pin_obj_t sck;
46+
mp_hal_pin_obj_t mosi;
47+
mp_hal_pin_obj_t miso;
48+
} mp_soft_spi_obj_t;
49+
50+
extern const mp_spi_proto_t mp_soft_spi_proto;
51+
52+
int mp_soft_spi_ioctl(void *self, uint32_t cmd);
53+
void mp_soft_spi_transfer(void *self, size_t len, const uint8_t *src, uint8_t *dest);
54+
55+
#endif // MICROPY_INCLUDED_DRIVERS_BUS_SPI_H

extmod/machine_spi.c

Lines changed: 29 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -38,61 +38,6 @@
3838
#define MICROPY_PY_MACHINE_SPI_LSB (1)
3939
#endif
4040

41-
void mp_machine_soft_spi_transfer(mp_obj_base_t *self_in, size_t len, const uint8_t *src, uint8_t *dest) {
42-
mp_machine_soft_spi_obj_t *self = (mp_machine_soft_spi_obj_t*)self_in;
43-
uint32_t delay_half = self->delay_half;
44-
45-
// only MSB transfer is implemented
46-
47-
// If a port defines MICROPY_PY_MACHINE_SPI_MIN_DELAY, and the configured
48-
// delay_half is equal to this value, then the software SPI implementation
49-
// will run as fast as possible, limited only by CPU speed and GPIO time.
50-
#ifdef MICROPY_PY_MACHINE_SPI_MIN_DELAY
51-
if (delay_half == MICROPY_PY_MACHINE_SPI_MIN_DELAY) {
52-
for (size_t i = 0; i < len; ++i) {
53-
uint8_t data_out = src[i];
54-
uint8_t data_in = 0;
55-
for (int j = 0; j < 8; ++j, data_out <<= 1) {
56-
mp_hal_pin_write(self->mosi, (data_out >> 7) & 1);
57-
mp_hal_pin_write(self->sck, 1 - self->polarity);
58-
data_in = (data_in << 1) | mp_hal_pin_read(self->miso);
59-
mp_hal_pin_write(self->sck, self->polarity);
60-
}
61-
if (dest != NULL) {
62-
dest[i] = data_in;
63-
}
64-
}
65-
return;
66-
}
67-
#endif
68-
69-
for (size_t i = 0; i < len; ++i) {
70-
uint8_t data_out = src[i];
71-
uint8_t data_in = 0;
72-
for (int j = 0; j < 8; ++j, data_out <<= 1) {
73-
mp_hal_pin_write(self->mosi, (data_out >> 7) & 1);
74-
if (self->phase == 0) {
75-
mp_hal_delay_us_fast(delay_half);
76-
mp_hal_pin_write(self->sck, 1 - self->polarity);
77-
} else {
78-
mp_hal_pin_write(self->sck, 1 - self->polarity);
79-
mp_hal_delay_us_fast(delay_half);
80-
}
81-
data_in = (data_in << 1) | mp_hal_pin_read(self->miso);
82-
if (self->phase == 0) {
83-
mp_hal_delay_us_fast(delay_half);
84-
mp_hal_pin_write(self->sck, self->polarity);
85-
} else {
86-
mp_hal_pin_write(self->sck, self->polarity);
87-
mp_hal_delay_us_fast(delay_half);
88-
}
89-
}
90-
if (dest != NULL) {
91-
dest[i] = data_in;
92-
}
93-
}
94-
}
95-
9641
/******************************************************************************/
9742
// MicroPython bindings for generic machine.SPI
9843

@@ -199,9 +144,9 @@ MP_DEFINE_CONST_DICT(mp_machine_spi_locals_dict, machine_spi_locals_dict_table);
199144
// Implementation of soft SPI
200145

201146
STATIC uint32_t baudrate_from_delay_half(uint32_t delay_half) {
202-
#ifdef MICROPY_PY_MACHINE_SPI_MIN_DELAY
203-
if (delay_half == MICROPY_PY_MACHINE_SPI_MIN_DELAY) {
204-
return MICROPY_PY_MACHINE_SPI_MAX_BAUDRATE;
147+
#ifdef MICROPY_HW_SOFTSPI_MIN_DELAY
148+
if (delay_half == MICROPY_HW_SOFTSPI_MIN_DELAY) {
149+
return MICROPY_HW_SOFTSPI_MAX_BAUDRATE;
205150
} else
206151
#endif
207152
{
@@ -210,9 +155,9 @@ STATIC uint32_t baudrate_from_delay_half(uint32_t delay_half) {
210155
}
211156

212157
STATIC uint32_t baudrate_to_delay_half(uint32_t baudrate) {
213-
#ifdef MICROPY_PY_MACHINE_SPI_MIN_DELAY
214-
if (baudrate >= MICROPY_PY_MACHINE_SPI_MAX_BAUDRATE) {
215-
return MICROPY_PY_MACHINE_SPI_MIN_DELAY;
158+
#ifdef MICROPY_HW_SOFTSPI_MIN_DELAY
159+
if (baudrate >= MICROPY_HW_SOFTSPI_MAX_BAUDRATE) {
160+
return MICROPY_HW_SOFTSPI_MIN_DELAY;
216161
} else
217162
#endif
218163
{
@@ -229,8 +174,8 @@ STATIC void mp_machine_soft_spi_print(const mp_print_t *print, mp_obj_t self_in,
229174
mp_machine_soft_spi_obj_t *self = MP_OBJ_TO_PTR(self_in);
230175
mp_printf(print, "SoftSPI(baudrate=%u, polarity=%u, phase=%u,"
231176
" sck=" MP_HAL_PIN_FMT ", mosi=" MP_HAL_PIN_FMT ", miso=" MP_HAL_PIN_FMT ")",
232-
baudrate_from_delay_half(self->delay_half), self->polarity, self->phase,
233-
mp_hal_pin_name(self->sck), mp_hal_pin_name(self->mosi), mp_hal_pin_name(self->miso));
177+
baudrate_from_delay_half(self->spi.delay_half), self->spi.polarity, self->spi.phase,
178+
mp_hal_pin_name(self->spi.sck), mp_hal_pin_name(self->spi.mosi), mp_hal_pin_name(self->spi.miso));
234179
}
235180

236181
STATIC mp_obj_t mp_machine_soft_spi_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
@@ -253,9 +198,9 @@ STATIC mp_obj_t mp_machine_soft_spi_make_new(const mp_obj_type_t *type, size_t n
253198
self->base.type = &mp_machine_soft_spi_type;
254199

255200
// set parameters
256-
self->delay_half = baudrate_to_delay_half(args[ARG_baudrate].u_int);
257-
self->polarity = args[ARG_polarity].u_int;
258-
self->phase = args[ARG_phase].u_int;
201+
self->spi.delay_half = baudrate_to_delay_half(args[ARG_baudrate].u_int);
202+
self->spi.polarity = args[ARG_polarity].u_int;
203+
self->spi.phase = args[ARG_phase].u_int;
259204
if (args[ARG_bits].u_int != 8) {
260205
mp_raise_ValueError("bits must be 8");
261206
}
@@ -267,15 +212,12 @@ STATIC mp_obj_t mp_machine_soft_spi_make_new(const mp_obj_type_t *type, size_t n
267212
|| args[ARG_miso].u_obj == MP_OBJ_NULL) {
268213
mp_raise_ValueError("must specify all of sck/mosi/miso");
269214
}
270-
self->sck = mp_hal_get_pin_obj(args[ARG_sck].u_obj);
271-
self->mosi = mp_hal_get_pin_obj(args[ARG_mosi].u_obj);
272-
self->miso = mp_hal_get_pin_obj(args[ARG_miso].u_obj);
215+
self->spi.sck = mp_hal_get_pin_obj(args[ARG_sck].u_obj);
216+
self->spi.mosi = mp_hal_get_pin_obj(args[ARG_mosi].u_obj);
217+
self->spi.miso = mp_hal_get_pin_obj(args[ARG_miso].u_obj);
273218

274-
// configure pins
275-
mp_hal_pin_write(self->sck, self->polarity);
276-
mp_hal_pin_output(self->sck);
277-
mp_hal_pin_output(self->mosi);
278-
mp_hal_pin_input(self->miso);
219+
// configure bus
220+
mp_soft_spi_ioctl(&self->spi, MP_SPI_IOCTL_INIT);
279221

280222
return MP_OBJ_FROM_PTR(self);
281223
}
@@ -296,29 +238,31 @@ STATIC void mp_machine_soft_spi_init(mp_obj_base_t *self_in, size_t n_args, cons
296238
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
297239

298240
if (args[ARG_baudrate].u_int != -1) {
299-
self->delay_half = baudrate_to_delay_half(args[ARG_baudrate].u_int);
241+
self->spi.delay_half = baudrate_to_delay_half(args[ARG_baudrate].u_int);
300242
}
301243
if (args[ARG_polarity].u_int != -1) {
302-
self->polarity = args[ARG_polarity].u_int;
244+
self->spi.polarity = args[ARG_polarity].u_int;
303245
}
304246
if (args[ARG_phase].u_int != -1) {
305-
self->phase = args[ARG_phase].u_int;
247+
self->spi.phase = args[ARG_phase].u_int;
306248
}
307249
if (args[ARG_sck].u_obj != MP_OBJ_NULL) {
308-
self->sck = mp_hal_get_pin_obj(args[ARG_sck].u_obj);
250+
self->spi.sck = mp_hal_get_pin_obj(args[ARG_sck].u_obj);
309251
}
310252
if (args[ARG_mosi].u_obj != MP_OBJ_NULL) {
311-
self->mosi = mp_hal_get_pin_obj(args[ARG_mosi].u_obj);
253+
self->spi.mosi = mp_hal_get_pin_obj(args[ARG_mosi].u_obj);
312254
}
313255
if (args[ARG_miso].u_obj != MP_OBJ_NULL) {
314-
self->miso = mp_hal_get_pin_obj(args[ARG_miso].u_obj);
256+
self->spi.miso = mp_hal_get_pin_obj(args[ARG_miso].u_obj);
315257
}
316258

317-
// configure pins
318-
mp_hal_pin_write(self->sck, self->polarity);
319-
mp_hal_pin_output(self->sck);
320-
mp_hal_pin_output(self->mosi);
321-
mp_hal_pin_input(self->miso);
259+
// configure bus
260+
mp_soft_spi_ioctl(&self->spi, MP_SPI_IOCTL_INIT);
261+
}
262+
263+
STATIC void mp_machine_soft_spi_transfer(mp_obj_base_t *self_in, size_t len, const uint8_t *src, uint8_t *dest) {
264+
mp_machine_soft_spi_obj_t *self = (mp_machine_soft_spi_obj_t*)self_in;
265+
mp_soft_spi_transfer(&self->spi, len, src, dest);
322266
}
323267

324268
const mp_machine_spi_p_t mp_machine_soft_spi_p = {

extmod/machine_spi.h

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
#include "py/obj.h"
3030
#include "py/mphal.h"
31+
#include "drivers/bus/spi.h"
3132

3233
// SPI protocol
3334
typedef struct _mp_machine_spi_p_t {
@@ -38,20 +39,13 @@ typedef struct _mp_machine_spi_p_t {
3839

3940
typedef struct _mp_machine_soft_spi_obj_t {
4041
mp_obj_base_t base;
41-
uint32_t delay_half; // microsecond delay for half SCK period
42-
uint8_t polarity;
43-
uint8_t phase;
44-
mp_hal_pin_obj_t sck;
45-
mp_hal_pin_obj_t mosi;
46-
mp_hal_pin_obj_t miso;
42+
mp_soft_spi_obj_t spi;
4743
} mp_machine_soft_spi_obj_t;
4844

4945
extern const mp_machine_spi_p_t mp_machine_soft_spi_p;
5046
extern const mp_obj_type_t mp_machine_soft_spi_type;
5147
extern const mp_obj_dict_t mp_machine_spi_locals_dict;
5248

53-
void mp_machine_soft_spi_transfer(mp_obj_base_t *self, size_t len, const uint8_t *src, uint8_t *dest);
54-
5549
mp_obj_t mp_machine_spi_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args);
5650

5751
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(mp_machine_spi_read_obj);

ports/esp32/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ LIB_SRC_C += \
195195
endif
196196

197197
DRIVERS_SRC_C = $(addprefix drivers/,\
198+
bus/softspi.c \
198199
dht/dht.c \
199200
)
200201

ports/esp32/mpconfigport.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@
135135
#define MICROPY_PY_MACHINE_SPI_MSB (0)
136136
#define MICROPY_PY_MACHINE_SPI_LSB (1)
137137
#define MICROPY_PY_MACHINE_SPI_MAKE_NEW machine_hw_spi_make_new
138-
#define MICROPY_PY_MACHINE_SPI_MIN_DELAY (0)
139-
#define MICROPY_PY_MACHINE_SPI_MAX_BAUDRATE (ets_get_cpu_frequency() * 1000000 / 200) // roughly
138+
#define MICROPY_HW_SOFTSPI_MIN_DELAY (0)
139+
#define MICROPY_HW_SOFTSPI_MAX_BAUDRATE (ets_get_cpu_frequency() * 1000000 / 200) // roughly
140140
#define MICROPY_PY_USSL (1)
141141
#define MICROPY_SSL_MBEDTLS (1)
142142
#define MICROPY_PY_USSL_FINALISER (1)

ports/esp8266/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ LIB_SRC_C += \
130130
endif
131131

132132
DRIVERS_SRC_C = $(addprefix drivers/,\
133+
bus/softspi.c \
133134
dht/dht.c \
134135
)
135136

0 commit comments

Comments
 (0)