Skip to content

Commit d434ce3

Browse files
committed
extmod/machine_spi: Factor out software SPI code from esp8266 to extmod.
1 parent a22a676 commit d434ce3

3 files changed

Lines changed: 54 additions & 51 deletions

File tree

esp8266/modpybspi.c

Lines changed: 4 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -28,68 +28,21 @@
2828
#include <stdint.h>
2929
#include <string.h>
3030

31-
#include "ets_sys.h"
32-
#include "etshal.h"
33-
#include "ets_alt_task.h"
34-
3531
#include "py/runtime.h"
3632
#include "py/stream.h"
3733
#include "py/mphal.h"
3834
#include "extmod/machine_spi.h"
3935

40-
typedef struct _pyb_spi_obj_t {
41-
mp_obj_base_t base;
42-
uint32_t baudrate;
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-
} pyb_spi_obj_t;
49-
50-
STATIC void mp_hal_spi_transfer(mp_obj_base_t *self_in, size_t len, const uint8_t *src, uint8_t *dest) {
51-
pyb_spi_obj_t *self = (pyb_spi_obj_t*)self_in;
52-
// only MSB transfer is implemented
53-
uint32_t delay_half = 500000 / self->baudrate + 1;
54-
for (size_t i = 0; i < len; ++i) {
55-
uint8_t data_out = src[i];
56-
uint8_t data_in = 0;
57-
for (int j = 0; j < 8; ++j, data_out <<= 1) {
58-
mp_hal_pin_write(self->mosi, (data_out >> 7) & 1);
59-
if (self->phase == 0) {
60-
ets_delay_us(delay_half);
61-
mp_hal_pin_write(self->sck, 1 - self->polarity);
62-
} else {
63-
mp_hal_pin_write(self->sck, 1 - self->polarity);
64-
ets_delay_us(delay_half);
65-
}
66-
data_in = (data_in << 1) | mp_hal_pin_read(self->miso);
67-
if (self->phase == 0) {
68-
ets_delay_us(delay_half);
69-
mp_hal_pin_write(self->sck, self->polarity);
70-
} else {
71-
mp_hal_pin_write(self->sck, self->polarity);
72-
ets_delay_us(delay_half);
73-
}
74-
}
75-
if (dest != NULL) {
76-
dest[i] = data_in;
77-
}
78-
// make sure pending tasks have a chance to run
79-
ets_loop_iter();
80-
}
81-
}
82-
8336
/******************************************************************************/
8437
// MicroPython bindings for SPI
8538

8639
STATIC void pyb_spi_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
87-
pyb_spi_obj_t *self = MP_OBJ_TO_PTR(self_in);
40+
mp_machine_soft_spi_obj_t *self = MP_OBJ_TO_PTR(self_in);
8841
mp_printf(print, "SPI(baudrate=%u, polarity=%u, phase=%u, sck=%u, mosi=%u, miso=%u)",
8942
self->baudrate, self->polarity, self->phase, self->sck, self->mosi, self->miso);
9043
}
9144

92-
STATIC void pyb_spi_init_helper(pyb_spi_obj_t *self, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
45+
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) {
9346
enum { ARG_baudrate, ARG_polarity, ARG_phase, ARG_sck, ARG_mosi, ARG_miso };
9447
static const mp_arg_t allowed_args[] = {
9548
{ MP_QSTR_baudrate, MP_ARG_INT, {.u_int = -1} },
@@ -130,7 +83,7 @@ STATIC void pyb_spi_init_helper(pyb_spi_obj_t *self, size_t n_args, const mp_obj
13083

13184
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) {
13285
mp_arg_check_num(n_args, n_kw, 0, MP_OBJ_FUN_ARGS_MAX, true);
133-
pyb_spi_obj_t *self = m_new_obj(pyb_spi_obj_t);
86+
mp_machine_soft_spi_obj_t *self = m_new_obj(mp_machine_soft_spi_obj_t);
13487
self->base.type = &pyb_spi_type;
13588
// set defaults
13689
self->baudrate = 500000;
@@ -162,7 +115,7 @@ STATIC const mp_rom_map_elem_t pyb_spi_locals_dict_table[] = {
162115
STATIC MP_DEFINE_CONST_DICT(pyb_spi_locals_dict, pyb_spi_locals_dict_table);
163116

164117
STATIC const mp_machine_spi_p_t pyb_spi_p = {
165-
.transfer = mp_hal_spi_transfer,
118+
.transfer = mp_machine_soft_spi_transfer,
166119
};
167120

168121
const mp_obj_type_t pyb_spi_type = {

extmod/machine_spi.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,43 @@
3232

3333
#if MICROPY_PY_MACHINE_SPI
3434

35+
void mp_machine_soft_spi_transfer(mp_obj_base_t *self_in, size_t len, const uint8_t *src, uint8_t *dest) {
36+
mp_machine_soft_spi_obj_t *self = (mp_machine_soft_spi_obj_t*)self_in;
37+
// only MSB transfer is implemented
38+
uint32_t delay_half = 500000 / self->baudrate + 1;
39+
for (size_t i = 0; i < len; ++i) {
40+
uint8_t data_out = src[i];
41+
uint8_t data_in = 0;
42+
for (int j = 0; j < 8; ++j, data_out <<= 1) {
43+
mp_hal_pin_write(self->mosi, (data_out >> 7) & 1);
44+
if (self->phase == 0) {
45+
mp_hal_delay_us_fast(delay_half);
46+
mp_hal_pin_write(self->sck, 1 - self->polarity);
47+
} else {
48+
mp_hal_pin_write(self->sck, 1 - self->polarity);
49+
mp_hal_delay_us_fast(delay_half);
50+
}
51+
data_in = (data_in << 1) | mp_hal_pin_read(self->miso);
52+
if (self->phase == 0) {
53+
mp_hal_delay_us_fast(delay_half);
54+
mp_hal_pin_write(self->sck, self->polarity);
55+
} else {
56+
mp_hal_pin_write(self->sck, self->polarity);
57+
mp_hal_delay_us_fast(delay_half);
58+
}
59+
}
60+
if (dest != NULL) {
61+
dest[i] = data_in;
62+
}
63+
64+
// Some ports need a regular callback, but probably we don't need
65+
// to do this every byte, or even at all.
66+
#ifdef MICROPY_EVENT_POLL_HOOK
67+
MICROPY_EVENT_POLL_HOOK;
68+
#endif
69+
}
70+
}
71+
3572
STATIC void mp_machine_spi_transfer(mp_obj_t self, size_t len, const void *src, void *dest) {
3673
mp_obj_base_t *s = (mp_obj_base_t*)MP_OBJ_TO_PTR(self);
3774
mp_machine_spi_p_t *spi_p = (mp_machine_spi_p_t*)s->type->protocol;

extmod/machine_spi.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,25 @@
2828
#define MICROPY_INCLUDED_EXTMOD_MACHINE_SPI_H
2929

3030
#include "py/obj.h"
31+
#include "py/mphal.h"
3132

3233
// SPI protocol
3334
typedef struct _mp_machine_spi_p_t {
3435
void (*transfer)(mp_obj_base_t *obj, size_t len, const uint8_t *src, uint8_t *dest);
3536
} mp_machine_spi_p_t;
3637

38+
typedef struct _mp_machine_soft_spi_obj_t {
39+
mp_obj_base_t base;
40+
uint32_t baudrate;
41+
uint8_t polarity;
42+
uint8_t phase;
43+
mp_hal_pin_obj_t sck;
44+
mp_hal_pin_obj_t mosi;
45+
mp_hal_pin_obj_t miso;
46+
} mp_machine_soft_spi_obj_t;
47+
48+
void mp_machine_soft_spi_transfer(mp_obj_base_t *self, size_t len, const uint8_t *src, uint8_t *dest);
49+
3750
MP_DECLARE_CONST_FUN_OBJ(mp_machine_spi_read_obj);
3851
MP_DECLARE_CONST_FUN_OBJ(mp_machine_spi_readinto_obj);
3952
MP_DECLARE_CONST_FUN_OBJ(mp_machine_spi_write_obj);

0 commit comments

Comments
 (0)