summaryrefslogtreecommitdiff
path: root/drivers
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2018-03-09 17:25:58 +1100
committerDamien George <damien.p.george@gmail.com>2018-03-10 00:59:43 +1100
commit58ebeca6a9a172a35b9298a911d450722797c409 (patch)
tree06434efb89e6c7a01add2137e817177e89532aea /drivers
parentad2a6e538ca008d5e25bb03223ac4b948a340aff (diff)
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.
Diffstat (limited to 'drivers')
-rw-r--r--drivers/bus/softspi.c105
-rw-r--r--drivers/bus/spi.h55
2 files changed, 160 insertions, 0 deletions
diff --git a/drivers/bus/softspi.c b/drivers/bus/softspi.c
new file mode 100644
index 000000000..bc12d89d3
--- /dev/null
+++ b/drivers/bus/softspi.c
@@ -0,0 +1,105 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2016-2018 Damien P. George
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include "drivers/bus/spi.h"
+
+int mp_soft_spi_ioctl(void *self_in, uint32_t cmd) {
+ mp_soft_spi_obj_t *self = (mp_soft_spi_obj_t*)self_in;
+
+ switch (cmd) {
+ case MP_SPI_IOCTL_INIT:
+ 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);
+ break;
+
+ case MP_SPI_IOCTL_DEINIT:
+ break;
+ }
+
+ return 0;
+}
+
+void mp_soft_spi_transfer(void *self_in, size_t len, const uint8_t *src, uint8_t *dest) {
+ mp_soft_spi_obj_t *self = (mp_soft_spi_obj_t*)self_in;
+ uint32_t delay_half = self->delay_half;
+
+ // only MSB transfer is implemented
+
+ // If a port defines MICROPY_HW_SOFTSPI_MIN_DELAY, and the configured
+ // delay_half is equal to this value, then the software SPI implementation
+ // will run as fast as possible, limited only by CPU speed and GPIO time.
+ #ifdef MICROPY_HW_SOFTSPI_MIN_DELAY
+ if (delay_half == MICROPY_HW_SOFTSPI_MIN_DELAY) {
+ 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);
+ mp_hal_pin_write(self->sck, 1 - self->polarity);
+ data_in = (data_in << 1) | mp_hal_pin_read(self->miso);
+ mp_hal_pin_write(self->sck, self->polarity);
+ }
+ if (dest != NULL) {
+ dest[i] = data_in;
+ }
+ }
+ return;
+ }
+ #endif
+
+ 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;
+ }
+ }
+}
+
+const mp_spi_proto_t mp_soft_spi_proto = {
+ .ioctl = mp_soft_spi_ioctl,
+ .transfer = mp_soft_spi_transfer,
+};
diff --git a/drivers/bus/spi.h b/drivers/bus/spi.h
new file mode 100644
index 000000000..6d1b9c2f8
--- /dev/null
+++ b/drivers/bus/spi.h
@@ -0,0 +1,55 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2016-2018 Damien P. George
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+#ifndef MICROPY_INCLUDED_DRIVERS_BUS_SPI_H
+#define MICROPY_INCLUDED_DRIVERS_BUS_SPI_H
+
+#include "py/mphal.h"
+
+enum {
+ MP_SPI_IOCTL_INIT,
+ MP_SPI_IOCTL_DEINIT,
+};
+
+typedef struct _mp_spi_proto_t {
+ int (*ioctl)(void *self, uint32_t cmd);
+ void (*transfer)(void *self, size_t len, const uint8_t *src, uint8_t *dest);
+} mp_spi_proto_t;
+
+typedef struct _mp_soft_spi_obj_t {
+ uint32_t delay_half; // microsecond delay for half SCK period
+ 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_soft_spi_obj_t;
+
+extern const mp_spi_proto_t mp_soft_spi_proto;
+
+int mp_soft_spi_ioctl(void *self, uint32_t cmd);
+void mp_soft_spi_transfer(void *self, size_t len, const uint8_t *src, uint8_t *dest);
+
+#endif // MICROPY_INCLUDED_DRIVERS_BUS_SPI_H