From 1eb3c66e9136b5934fc306408f00f11a07dea584 Mon Sep 17 00:00:00 2001
From: Damien George <damien.p.george@gmail.com>
Date: Thu, 8 Dec 2016 13:47:01 +1100
Subject: [PATCH] extmod/machine_spi: Provide reusable software SPI class.

So long as a port defines relevant mp_hal_pin_xxx functions (and delay) it
can make use of this software SPI class without the need for additional
code.
---
 esp8266/esp_mphal.h   |   2 +
 extmod/machine_spi.c  | 204 ++++++++++++++++++++++++++++++++++++++++++
 extmod/machine_spi.h  |   7 ++
 stmhal/mpconfigport.h |   1 +
 stmhal/mphalport.h    |   2 +
 5 files changed, 216 insertions(+)

diff --git a/esp8266/esp_mphal.h b/esp8266/esp_mphal.h
index 1622667f9..d783f1f09 100644
--- a/esp8266/esp_mphal.h
+++ b/esp8266/esp_mphal.h
@@ -76,8 +76,10 @@ void ets_event_poll(void);
 #include "etshal.h"
 #include "gpio.h"
 #include "esp8266/modmachine.h"
+#define MP_HAL_PIN_FMT "%u"
 #define mp_hal_pin_obj_t uint32_t
 #define mp_hal_get_pin_obj(o) mp_obj_get_pin(o)
+#define mp_hal_pin_name(p) (p)
 void mp_hal_pin_input(mp_hal_pin_obj_t pin);
 void mp_hal_pin_output(mp_hal_pin_obj_t pin);
 void mp_hal_pin_open_drain(mp_hal_pin_obj_t pin);
diff --git a/extmod/machine_spi.c b/extmod/machine_spi.c
index e3d72ab58..6e7678498 100644
--- a/extmod/machine_spi.c
+++ b/extmod/machine_spi.c
@@ -32,6 +32,12 @@
 
 #if MICROPY_PY_MACHINE_SPI
 
+// if a port didn't define MSB/LSB constants then provide them
+#ifndef MICROPY_PY_MACHINE_SPI_MSB
+#define MICROPY_PY_MACHINE_SPI_MSB (0)
+#define MICROPY_PY_MACHINE_SPI_LSB (1)
+#endif
+
 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;
     uint32_t delay_half = self->delay_half;
@@ -93,6 +99,49 @@ void mp_machine_soft_spi_transfer(mp_obj_base_t *self_in, size_t len, const uint
     }
 }
 
+/******************************************************************************/
+// MicroPython bindings for generic machine.SPI
+
+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);
+
+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) {
+    // check the id argument, if given
+    if (n_args > 0) {
+        if (args[0] != MP_OBJ_NEW_SMALL_INT(-1)) {
+            #if defined(MICROPY_PY_MACHINE_SPI_MAKE_NEW)
+            // dispatch to port-specific constructor
+            extern mp_obj_t MICROPY_PY_MACHINE_SPI_MAKE_NEW(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args);
+            return MICROPY_PY_MACHINE_SPI_MAKE_NEW(type, n_args, n_kw, args);
+            #else
+            mp_raise_ValueError("invalid SPI peripheral");
+            #endif
+        }
+        --n_args;
+        ++args;
+    }
+
+    // software SPI
+    return mp_machine_soft_spi_make_new(type, n_args, n_kw, args);
+}
+
+STATIC mp_obj_t machine_spi_init(size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
+    mp_obj_base_t *s = (mp_obj_base_t*)MP_OBJ_TO_PTR(args[0]);
+    mp_machine_spi_p_t *spi_p = (mp_machine_spi_p_t*)s->type->protocol;
+    spi_p->init(s, n_args - 1, args + 1, kw_args);
+    return mp_const_none;
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_KW(machine_spi_init_obj, 1, machine_spi_init);
+
+STATIC mp_obj_t machine_spi_deinit(mp_obj_t self) {
+    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;
+    if (spi_p->deinit != NULL) {
+        spi_p->deinit(s);
+    }
+    return mp_const_none;
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_spi_deinit_obj, machine_spi_deinit);
+
 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;
@@ -138,4 +187,159 @@ STATIC mp_obj_t mp_machine_spi_write_readinto(mp_obj_t self, mp_obj_t wr_buf, mp
 }
 MP_DEFINE_CONST_FUN_OBJ_3(mp_machine_spi_write_readinto_obj, mp_machine_spi_write_readinto);
 
+STATIC const mp_rom_map_elem_t machine_spi_locals_dict_table[] = {
+    { MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&machine_spi_init_obj) },
+    { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&machine_spi_deinit_obj) },
+    { MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_machine_spi_read_obj) },
+    { MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_machine_spi_readinto_obj) },
+    { MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_machine_spi_write_obj) },
+    { MP_ROM_QSTR(MP_QSTR_write_readinto), MP_ROM_PTR(&mp_machine_spi_write_readinto_obj) },
+
+    { MP_ROM_QSTR(MP_QSTR_MSB), MP_ROM_INT(MICROPY_PY_MACHINE_SPI_MSB) },
+    { MP_ROM_QSTR(MP_QSTR_LSB), MP_ROM_INT(MICROPY_PY_MACHINE_SPI_LSB) },
+};
+
+MP_DEFINE_CONST_DICT(mp_machine_spi_locals_dict, machine_spi_locals_dict_table);
+
+/******************************************************************************/
+// Implementation of soft SPI
+
+STATIC uint32_t baudrate_from_delay_half(uint32_t delay_half) {
+    #ifdef MICROPY_PY_MACHINE_SPI_MIN_DELAY
+    if (delay_half == MICROPY_PY_MACHINE_SPI_MIN_DELAY) {
+        return MICROPY_PY_MACHINE_SPI_MAX_BAUDRATE;
+    } else
+    #endif
+    {
+        return 500000 / delay_half;
+    }
+}
+
+STATIC uint32_t baudrate_to_delay_half(uint32_t baudrate) {
+    #ifdef MICROPY_PY_MACHINE_SPI_MIN_DELAY
+    if (baudrate >= MICROPY_PY_MACHINE_SPI_MAX_BAUDRATE) {
+        return MICROPY_PY_MACHINE_SPI_MIN_DELAY;
+    } else
+    #endif
+    {
+        uint32_t delay_half = 500000 / baudrate;
+        // round delay_half up so that: actual_baudrate <= requested_baudrate
+        if (500000 % baudrate != 0) {
+            delay_half += 1;
+        }
+        return delay_half;
+    }
+}
+
+STATIC void mp_machine_soft_spi_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
+    mp_machine_soft_spi_obj_t *self = MP_OBJ_TO_PTR(self_in);
+    mp_printf(print, "SoftSPI(baudrate=%u, polarity=%u, phase=%u,"
+        " sck=" MP_HAL_PIN_FMT ", mosi=" MP_HAL_PIN_FMT ", miso=" MP_HAL_PIN_FMT ")",
+        baudrate_from_delay_half(self->delay_half), self->polarity, self->phase,
+        mp_hal_pin_name(self->sck), mp_hal_pin_name(self->mosi), mp_hal_pin_name(self->miso));
+}
+
+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) {
+    enum { ARG_baudrate, ARG_polarity, ARG_phase, ARG_bits, ARG_firstbit, ARG_sck, ARG_mosi, ARG_miso };
+    static const mp_arg_t allowed_args[] = {
+        { MP_QSTR_baudrate, MP_ARG_INT, {.u_int = 500000} },
+        { MP_QSTR_polarity, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
+        { MP_QSTR_phase,    MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
+        { MP_QSTR_bits,     MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 8} },
+        { MP_QSTR_firstbit, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = MICROPY_PY_MACHINE_SPI_MSB} },
+        { MP_QSTR_sck,      MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
+        { MP_QSTR_mosi,     MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
+        { MP_QSTR_miso,     MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
+    };
+    mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
+    mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
+
+    // create new object
+    mp_machine_soft_spi_obj_t *self = m_new_obj(mp_machine_soft_spi_obj_t);
+    self->base.type = &mp_machine_soft_spi_type;
+
+    // set parameters
+    self->delay_half = baudrate_to_delay_half(args[ARG_baudrate].u_int);
+    self->polarity = args[ARG_polarity].u_int;
+    self->phase = args[ARG_phase].u_int;
+    if (args[ARG_bits].u_int != 8) {
+        mp_raise_ValueError("bits must be 8");
+    }
+    if (args[ARG_firstbit].u_int != MICROPY_PY_MACHINE_SPI_MSB) {
+        mp_raise_ValueError("firstbit must be MSB");
+    }
+    if (args[ARG_sck].u_obj == MP_OBJ_NULL
+        || args[ARG_mosi].u_obj == MP_OBJ_NULL
+        || args[ARG_miso].u_obj == MP_OBJ_NULL) {
+        mp_raise_ValueError("must specify all of sck/mosi/miso");
+    }
+    self->sck = mp_hal_get_pin_obj(args[ARG_sck].u_obj);
+    self->mosi = mp_hal_get_pin_obj(args[ARG_mosi].u_obj);
+    self->miso = mp_hal_get_pin_obj(args[ARG_miso].u_obj);
+
+    // configure pins
+    mp_hal_pin_write(self->sck, self->polarity);
+    mp_hal_pin_output(self->sck);
+    mp_hal_pin_output(self->mosi);
+    mp_hal_pin_input(self->miso);
+
+    return MP_OBJ_FROM_PTR(self);
+}
+
+STATIC void mp_machine_soft_spi_init(mp_obj_base_t *self_in, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
+    mp_machine_soft_spi_obj_t *self = (mp_machine_soft_spi_obj_t*)self_in;
+
+    enum { ARG_baudrate, ARG_polarity, ARG_phase, ARG_sck, ARG_mosi, ARG_miso };
+    static const mp_arg_t allowed_args[] = {
+        { MP_QSTR_baudrate, MP_ARG_INT, {.u_int = -1} },
+        { MP_QSTR_polarity, MP_ARG_INT, {.u_int = -1} },
+        { MP_QSTR_phase, MP_ARG_INT, {.u_int = -1} },
+        { MP_QSTR_sck, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
+        { MP_QSTR_mosi, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
+        { MP_QSTR_miso, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
+    };
+    mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
+    mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
+
+    if (args[ARG_baudrate].u_int != -1) {
+        self->delay_half = baudrate_to_delay_half(args[ARG_baudrate].u_int);
+    }
+    if (args[ARG_polarity].u_int != -1) {
+        self->polarity = args[ARG_polarity].u_int;
+    }
+    if (args[ARG_phase].u_int != -1) {
+        self->phase = args[ARG_phase].u_int;
+    }
+    if (args[ARG_sck].u_obj != MP_OBJ_NULL) {
+        self->sck = mp_hal_get_pin_obj(args[ARG_sck].u_obj);
+    }
+    if (args[ARG_mosi].u_obj != MP_OBJ_NULL) {
+        self->mosi = mp_hal_get_pin_obj(args[ARG_mosi].u_obj);
+    }
+    if (args[ARG_miso].u_obj != MP_OBJ_NULL) {
+        self->miso = mp_hal_get_pin_obj(args[ARG_miso].u_obj);
+    }
+
+    // configure pins
+    mp_hal_pin_write(self->sck, self->polarity);
+    mp_hal_pin_output(self->sck);
+    mp_hal_pin_output(self->mosi);
+    mp_hal_pin_input(self->miso);
+}
+
+STATIC const mp_machine_spi_p_t mp_machine_soft_spi_p = {
+    .init = mp_machine_soft_spi_init,
+    .deinit = NULL,
+    .transfer = mp_machine_soft_spi_transfer,
+};
+
+const mp_obj_type_t mp_machine_soft_spi_type = {
+    { &mp_type_type },
+    .name = MP_QSTR_SoftSPI,
+    .print = mp_machine_soft_spi_print,
+    .make_new = mp_machine_spi_make_new, // delegate to master constructor
+    .protocol = &mp_machine_soft_spi_p,
+    .locals_dict = (mp_obj_dict_t*)&mp_machine_spi_locals_dict,
+};
+
 #endif // MICROPY_PY_MACHINE_SPI
diff --git a/extmod/machine_spi.h b/extmod/machine_spi.h
index 62f42a885..88a3e19f4 100644
--- a/extmod/machine_spi.h
+++ b/extmod/machine_spi.h
@@ -32,6 +32,8 @@
 
 // SPI protocol
 typedef struct _mp_machine_spi_p_t {
+    void (*init)(mp_obj_base_t *obj, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
+    void (*deinit)(mp_obj_base_t *obj); // can be NULL
     void (*transfer)(mp_obj_base_t *obj, size_t len, const uint8_t *src, uint8_t *dest);
 } mp_machine_spi_p_t;
 
@@ -45,8 +47,13 @@ typedef struct _mp_machine_soft_spi_obj_t {
     mp_hal_pin_obj_t miso;
 } mp_machine_soft_spi_obj_t;
 
+extern const mp_obj_type_t mp_machine_soft_spi_type;
+extern const mp_obj_dict_t mp_machine_spi_locals_dict;
+
 void mp_machine_soft_spi_transfer(mp_obj_base_t *self, size_t len, const uint8_t *src, uint8_t *dest);
 
+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);
+
 MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(mp_machine_spi_read_obj);
 MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(mp_machine_spi_readinto_obj);
 MP_DECLARE_CONST_FUN_OBJ_2(mp_machine_spi_write_obj);
diff --git a/stmhal/mpconfigport.h b/stmhal/mpconfigport.h
index 9d9487a66..0e3098095 100644
--- a/stmhal/mpconfigport.h
+++ b/stmhal/mpconfigport.h
@@ -106,6 +106,7 @@
 #define MICROPY_PY_MACHINE_I2C_MAKE_NEW machine_hard_i2c_make_new
 #define MICROPY_PY_MACHINE_SPI      (1)
 #define MICROPY_PY_MACHINE_SPI_MIN_DELAY (0)
+#define MICROPY_PY_MACHINE_SPI_MAX_BAUDRATE (HAL_RCC_GetSysClockFreq() / 48)
 #define MICROPY_PY_FRAMEBUF         (1)
 
 #ifndef MICROPY_PY_USOCKET
diff --git a/stmhal/mphalport.h b/stmhal/mphalport.h
index b55c9ccdc..f0c05555d 100644
--- a/stmhal/mphalport.h
+++ b/stmhal/mphalport.h
@@ -48,6 +48,7 @@ static inline mp_uint_t mp_hal_ticks_cpu(void) {
 
 #include "stmhal/pin.h"
 
+#define MP_HAL_PIN_FMT                  "%q"
 #define MP_HAL_PIN_MODE_INPUT           (0)
 #define MP_HAL_PIN_MODE_OUTPUT          (1)
 #define MP_HAL_PIN_MODE_ALT             (2)
@@ -60,6 +61,7 @@ static inline mp_uint_t mp_hal_ticks_cpu(void) {
 
 #define mp_hal_pin_obj_t const pin_obj_t*
 #define mp_hal_get_pin_obj(o)   pin_find(o)
+#define mp_hal_pin_name(p)      ((p)->name)
 #define mp_hal_pin_input(p)     mp_hal_pin_config((p), MP_HAL_PIN_MODE_INPUT, MP_HAL_PIN_PULL_NONE, 0)
 #define mp_hal_pin_output(p)    mp_hal_pin_config((p), MP_HAL_PIN_MODE_OUTPUT, MP_HAL_PIN_PULL_NONE, 0)
 #define mp_hal_pin_open_drain(p) mp_hal_pin_config((p), MP_HAL_PIN_MODE_OPEN_DRAIN, MP_HAL_PIN_PULL_NONE, 0)
-- 
GitLab