aboutsummaryrefslogtreecommitdiff
path: root/extmod
diff options
context:
space:
mode:
authorDamien George2016-10-03 16:43:44 +1100
committerDamien George2016-10-03 16:43:44 +1100
commitd434ce3fca088959c4a4bf525b9d730ec9bdb7b4 (patch)
tree12e8b38116036759da7ce741c5be904bec58778d /extmod
parenta22a67661aa4f6309efa1e81b279629050fd1966 (diff)
extmod/machine_spi: Factor out software SPI code from esp8266 to extmod.
Diffstat (limited to 'extmod')
-rw-r--r--extmod/machine_spi.c37
-rw-r--r--extmod/machine_spi.h13
2 files changed, 50 insertions, 0 deletions
diff --git a/extmod/machine_spi.c b/extmod/machine_spi.c
index 1c64cf511..3a34b7fb0 100644
--- a/extmod/machine_spi.c
+++ b/extmod/machine_spi.c
@@ -32,6 +32,43 @@
#if MICROPY_PY_MACHINE_SPI
+void mp_machine_soft_spi_transfer(mp_obj_base_t *self_in, size_t len, const uint8_t *src, uint8_t *dest) {
+ mp_machine_soft_spi_obj_t *self = (mp_machine_soft_spi_obj_t*)self_in;
+ // only MSB transfer is implemented
+ uint32_t delay_half = 500000 / self->baudrate + 1;
+ for (size_t i = 0; i < len; ++i) {
+ uint8_t data_out = src[i];
+ uint8_t data_in = 0;
+ for (int j = 0; j < 8; ++j, data_out <<= 1) {
+ mp_hal_pin_write(self->mosi, (data_out >> 7) & 1);
+ if (self->phase == 0) {
+ mp_hal_delay_us_fast(delay_half);
+ mp_hal_pin_write(self->sck, 1 - self->polarity);
+ } else {
+ mp_hal_pin_write(self->sck, 1 - self->polarity);
+ mp_hal_delay_us_fast(delay_half);
+ }
+ data_in = (data_in << 1) | mp_hal_pin_read(self->miso);
+ if (self->phase == 0) {
+ mp_hal_delay_us_fast(delay_half);
+ mp_hal_pin_write(self->sck, self->polarity);
+ } else {
+ mp_hal_pin_write(self->sck, self->polarity);
+ mp_hal_delay_us_fast(delay_half);
+ }
+ }
+ if (dest != NULL) {
+ dest[i] = data_in;
+ }
+
+ // Some ports need a regular callback, but probably we don't need
+ // to do this every byte, or even at all.
+ #ifdef MICROPY_EVENT_POLL_HOOK
+ MICROPY_EVENT_POLL_HOOK;
+ #endif
+ }
+}
+
STATIC void mp_machine_spi_transfer(mp_obj_t self, size_t len, const void *src, void *dest) {
mp_obj_base_t *s = (mp_obj_base_t*)MP_OBJ_TO_PTR(self);
mp_machine_spi_p_t *spi_p = (mp_machine_spi_p_t*)s->type->protocol;
diff --git a/extmod/machine_spi.h b/extmod/machine_spi.h
index 35a911d91..e1922c6e8 100644
--- a/extmod/machine_spi.h
+++ b/extmod/machine_spi.h
@@ -28,12 +28,25 @@
#define MICROPY_INCLUDED_EXTMOD_MACHINE_SPI_H
#include "py/obj.h"
+#include "py/mphal.h"
// SPI protocol
typedef struct _mp_machine_spi_p_t {
void (*transfer)(mp_obj_base_t *obj, size_t len, const uint8_t *src, uint8_t *dest);
} mp_machine_spi_p_t;
+typedef struct _mp_machine_soft_spi_obj_t {
+ mp_obj_base_t base;
+ uint32_t baudrate;
+ uint8_t polarity;
+ uint8_t phase;
+ mp_hal_pin_obj_t sck;
+ mp_hal_pin_obj_t mosi;
+ mp_hal_pin_obj_t miso;
+} mp_machine_soft_spi_obj_t;
+
+void mp_machine_soft_spi_transfer(mp_obj_base_t *self, size_t len, const uint8_t *src, uint8_t *dest);
+
MP_DECLARE_CONST_FUN_OBJ(mp_machine_spi_read_obj);
MP_DECLARE_CONST_FUN_OBJ(mp_machine_spi_readinto_obj);
MP_DECLARE_CONST_FUN_OBJ(mp_machine_spi_write_obj);