summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShicheng Chu <[email protected]>2023-06-04 08:16:06 +0800
committerGitHub <[email protected]>2023-06-04 08:16:06 +0800
commitbf07f4183e3c55a2c5a8e16aec59fd257f3920eb (patch)
treea9ae42e5d97c287eda51756b37ac24be15d1882c
parent5d299ffdcd580792fe6e348c9845c24704167955 (diff)
[bsp][pico]update: add drv_soft_spi (#7598)
-rw-r--r--bsp/raspberry-pico/drivers/SConscript14
-rw-r--r--bsp/raspberry-pico/drivers/drv_soft_spi.c194
-rw-r--r--bsp/raspberry-pico/drivers/drv_soft_spi.h56
-rw-r--r--bsp/raspberry-pico/libraries/Kconfig54
4 files changed, 317 insertions, 1 deletions
diff --git a/bsp/raspberry-pico/drivers/SConscript b/bsp/raspberry-pico/drivers/SConscript
index 14a3ad69d0..ef3df3b840 100644
--- a/bsp/raspberry-pico/drivers/SConscript
+++ b/bsp/raspberry-pico/drivers/SConscript
@@ -1,9 +1,21 @@
from building import *
cwd = GetCurrentDir()
-src = Glob('*.c')
+src = []
CPPPATH = [cwd]
+if GetDepend('BSP_USING_PIN'):
+ src += ['drv_gpio.c']
+
+if GetDepend('BSP_USING_UART'):
+ src += ['drv_uart.c']
+
+if GetDepend('BSP_USING_SOFT_I2C'):
+ src += ['drv_soft_i2c.c']
+
+if GetDepend('BSP_USING_SOFT_SPI'):
+ src += ['drv_soft_spi.c']
+
group = DefineGroup('Drivers', src, depend = [''], CPPPATH = CPPPATH)
Return('group')
diff --git a/bsp/raspberry-pico/drivers/drv_soft_spi.c b/bsp/raspberry-pico/drivers/drv_soft_spi.c
new file mode 100644
index 0000000000..e1addb2d90
--- /dev/null
+++ b/bsp/raspberry-pico/drivers/drv_soft_spi.c
@@ -0,0 +1,194 @@
+/*
+ * Copyright (c) 2006-2023, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date Author Notes
+ * 2023-06-02 Chushicheng the first version
+ */
+#include "drv_soft_spi.h"
+#include "board.h"
+
+#if defined BSP_USING_SOFT_SPI
+#define LOG_TAG "drv.soft_spi"
+#include <rtdbg.h>
+
+static struct pico_soft_spi_config soft_spi_config[] =
+{
+#ifdef BSP_USING_SOFT_SPI0
+ SOFT_SPI0_BUS_CONFIG,
+#endif
+#ifdef BSP_USING_SOFT_SPI1
+ SOFT_SPI1_BUS_CONFIG,
+#endif
+};
+
+/**
+ * Attach the spi device to soft SPI bus, this function must be used after initialization.
+ */
+rt_err_t rt_hw_softspi_device_attach(const char *bus_name, const char *device_name, rt_base_t cs_pin)
+{
+ rt_err_t result;
+ struct rt_spi_device *spi_device;
+
+ /* attach the device to soft spi bus*/
+ spi_device = (struct rt_spi_device *)rt_malloc(sizeof(struct rt_spi_device));
+ RT_ASSERT(spi_device != RT_NULL);
+
+ result = rt_spi_bus_attach_device_cspin(spi_device, device_name, bus_name, cs_pin, RT_NULL);
+ return result;
+}
+
+static void pico_spi_gpio_init(struct pico_soft_spi *spi)
+{
+ struct pico_soft_spi_config *cfg = (struct pico_soft_spi_config *)spi->cfg;
+ rt_pin_mode(cfg->sck, PIN_MODE_OUTPUT);
+ rt_pin_mode(cfg->miso, PIN_MODE_INPUT);
+ rt_pin_mode(cfg->mosi, PIN_MODE_OUTPUT);
+
+ rt_pin_write(cfg->miso, PIN_HIGH);
+ rt_pin_write(cfg->sck, PIN_HIGH);
+ rt_pin_write(cfg->mosi, PIN_HIGH);
+}
+
+static void pico_tog_sclk(void *data)
+{
+ struct pico_soft_spi_config* cfg = (struct pico_soft_spi_config*)data;
+ if(rt_pin_read(cfg->sck) == PIN_HIGH)
+ {
+ rt_pin_write(cfg->sck, PIN_LOW);
+ }
+ else
+ {
+ rt_pin_write(cfg->sck, PIN_HIGH);
+ }
+}
+
+static void pico_set_sclk(void *data, rt_int32_t state)
+{
+ struct pico_soft_spi_config* cfg = (struct pico_soft_spi_config*)data;
+ if (state)
+ {
+ rt_pin_write(cfg->sck, PIN_HIGH);
+ }
+ else
+ {
+ rt_pin_write(cfg->sck, PIN_LOW);
+ }
+}
+
+static void pico_set_mosi(void *data, rt_int32_t state)
+{
+ struct pico_soft_spi_config* cfg = (struct pico_soft_spi_config*)data;
+ if (state)
+ {
+ rt_pin_write(cfg->mosi, PIN_HIGH);
+ }
+ else
+ {
+ rt_pin_write(cfg->mosi, PIN_LOW);
+ }
+}
+
+static void pico_set_miso(void *data, rt_int32_t state)
+{
+ struct pico_soft_spi_config* cfg = (struct pico_soft_spi_config*)data;
+ if (state)
+ {
+ rt_pin_write(cfg->miso, PIN_HIGH);
+ }
+ else
+ {
+ rt_pin_write(cfg->miso, PIN_LOW);
+ }
+}
+
+static rt_int32_t pico_get_sclk(void *data)
+{
+ struct pico_soft_spi_config* cfg = (struct pico_soft_spi_config*)data;
+ return rt_pin_read(cfg->sck);
+}
+
+static rt_int32_t pico_get_mosi(void *data)
+{
+ struct pico_soft_spi_config* cfg = (struct pico_soft_spi_config*)data;
+ return rt_pin_read(cfg->mosi);
+}
+
+static rt_int32_t pico_get_miso(void *data)
+{
+ struct pico_soft_spi_config* cfg = (struct pico_soft_spi_config*)data;
+ return rt_pin_read(cfg->miso);
+}
+
+static void pico_dir_mosi(void *data, rt_int32_t state)
+{
+ struct pico_soft_spi_config* cfg = (struct pico_soft_spi_config*)data;
+ if (state)
+ {
+ rt_pin_mode(cfg->mosi, PIN_MODE_INPUT);
+ }
+ else
+ {
+ rt_pin_mode(cfg->mosi, PIN_MODE_OUTPUT);
+ }
+}
+
+static void pico_dir_miso(void *data, rt_int32_t state)
+{
+ struct pico_soft_spi_config* cfg = (struct pico_soft_spi_config*)data;
+ if (state)
+ {
+ rt_pin_mode(cfg->miso, PIN_MODE_INPUT);
+ }
+ else
+ {
+ rt_pin_mode(cfg->miso, PIN_MODE_OUTPUT);
+ }
+}
+
+static void pico_udelay(rt_uint32_t us)
+{
+ busy_wait_us_32(us);
+}
+
+static struct rt_spi_bit_ops pico_soft_spi_ops =
+{
+ .data = RT_NULL,
+ .tog_sclk = pico_tog_sclk,
+ .set_sclk = pico_set_sclk,
+ .set_mosi = pico_set_mosi,
+ .set_miso = pico_set_miso,
+ .get_sclk = pico_get_sclk,
+ .get_mosi = pico_get_mosi,
+ .get_miso = pico_get_miso,
+ .dir_mosi = pico_dir_mosi,
+ .dir_miso = pico_dir_miso,
+ .udelay = pico_udelay,
+ .delay_us = 1,
+};
+
+static struct pico_soft_spi spi_obj[sizeof(soft_spi_config) / sizeof(soft_spi_config[0])];
+
+/* Soft SPI initialization function */
+int rt_hw_softspi_init(void)
+{
+ rt_size_t obj_num = sizeof(spi_obj) / sizeof(struct pico_soft_spi);
+ rt_err_t result;
+
+ for (int i = 0; i < obj_num; i++)
+ {
+ pico_soft_spi_ops.data = (void *)&soft_spi_config[i];
+ spi_obj[i].spi.ops = &pico_soft_spi_ops;
+ spi_obj[i].cfg = (void *)&soft_spi_config[i];
+ pico_spi_gpio_init(&spi_obj[i]);
+ result = rt_spi_bit_add_bus(&spi_obj[i].spi, soft_spi_config[i].bus_name, &pico_soft_spi_ops);
+ RT_ASSERT(result == RT_EOK);
+ }
+
+ return RT_EOK;
+}
+INIT_DEVICE_EXPORT(rt_hw_softspi_init);
+
+#endif /* BSP_USING_SOFT_SPI */
diff --git a/bsp/raspberry-pico/drivers/drv_soft_spi.h b/bsp/raspberry-pico/drivers/drv_soft_spi.h
new file mode 100644
index 0000000000..1d19bb1382
--- /dev/null
+++ b/bsp/raspberry-pico/drivers/drv_soft_spi.h
@@ -0,0 +1,56 @@
+/*
+ * Copyright (c) 2006-2023, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date Author Notes
+ * 2023-06-02 Chushicheng the first version
+ */
+#ifndef DRV_SOFT_SPI_H_
+#define DRV_SOFT_SPI_H_
+
+#ifdef BSP_USING_SOFT_SPI
+#include <rtdevice.h>
+#include <spi-bit-ops.h>
+
+/* pico soft spi config */
+struct pico_soft_spi_config
+{
+ rt_uint8_t sck;
+ rt_uint8_t mosi;
+ rt_uint8_t miso;
+ const char *bus_name;
+};
+
+/* pico soft spi dirver */
+struct pico_soft_spi
+{
+ struct rt_spi_bit_obj spi;
+ struct pico_soft_spi_config *cfg;
+};
+
+#ifdef BSP_USING_SOFT_SPI0
+#define SOFT_SPI0_BUS_CONFIG \
+ { \
+ .sck = BSP_S_SPI0_SCK_PIN, \
+ .mosi = BSP_S_SPI0_MOSI_PIN, \
+ .miso = BSP_S_SPI0_MISO_PIN, \
+ .bus_name = "sspi0", \
+ }
+#endif /* BSP_USING_SOFT_SPI0 */
+#ifdef BSP_USING_SOFT_SPI1
+#define SOFT_SPI1_BUS_CONFIG \
+ { \
+ .sck = BSP_S_SPI1_SCK_PIN, \
+ .mosi = BSP_S_SPI_MOSI_PIN, \
+ .miso = BSP_S_SPI1_MISO_PIN, \
+ .bus_name = "sspi1", \
+ }
+#endif /* BSP_USING_SOFT_SPI1 */
+
+rt_err_t rt_hw_softspi_device_attach(const char *bus_name, const char *device_name, rt_base_t cs_pin);
+int rt_soft_spi_init(void);
+
+#endif /* BSP_USING_SOFT_SPI */
+#endif /* __DRV_SOFT_SPI__ */
diff --git a/bsp/raspberry-pico/libraries/Kconfig b/bsp/raspberry-pico/libraries/Kconfig
index 991f25642d..e1e1e93edd 100644
--- a/bsp/raspberry-pico/libraries/Kconfig
+++ b/bsp/raspberry-pico/libraries/Kconfig
@@ -24,6 +24,16 @@ endmenu
menu "On-chip Peripheral Drivers"
+ config BSP_USING_PIN
+ bool "Enable GPIO"
+ select RT_USING_PIN
+ default y
+
+ config BSP_USING_UART
+ bool "Enable UART"
+ select RT_USING_UART
+ default y
+
menuconfig BSP_USING_SOFT_I2C
bool "Enable I2C BUS (software simulation)"
default n
@@ -45,6 +55,50 @@ menu "On-chip Peripheral Drivers"
default 3
endif
endif
+
+ menuconfig BSP_USING_SOFT_SPI
+ bool "Enable soft SPI BUS"
+ default n
+ select RT_USING_PIN
+ select RT_USING_SPI_BITOPS
+ select RT_USING_SPI
+ if BSP_USING_SOFT_SPI
+ config BSP_USING_SOFT_SPI0
+ bool "Enable SPI0 Bus (software simulation)"
+ default n
+ if BSP_USING_SOFT_SPI0
+ config BSP_S_SPI0_SCK_PIN
+ int "spi0 sck pin number (GP)"
+ range 0 28
+ default 6
+ config BSP_S_SPI0_MOSI_PIN
+ int "spi0 mosi pin number (GP)"
+ range 0 28
+ default 7
+ config BSP_S_SPI0_MISO_PIN
+ int "spi0 miso pin number (GP)"
+ range 0 28
+ default 4
+ endif
+ config BSP_USING_SOFT_SPI1
+ bool "Enable SPI1 Bus (software simulation)"
+ default n
+ if BSP_USING_SOFT_SPI1
+ config BSP_S_SPI1_SCK_PIN
+ int "spi1 sck pin number (GP)"
+ range 0 28
+ default 10
+ config BSP_S_SPI1_MOSI_PIN
+ int "spi1 mosi pin number (GP)"
+ range 0 28
+ default 11
+ config BSP_S_SPI1_MISO_PIN
+ int "spi1 miso pin number (GP)"
+ range 0 28
+ default 12
+ endif
+
+ endif
endmenu
endmenu