|
| 1 | +/**************************************************************************** |
| 2 | + * arch/xtensa/src/common/espressif/esp_spi_bitbang.c |
| 3 | + * |
| 4 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 5 | + * contributor license agreements. See the NOTICE file distributed with |
| 6 | + * this work for additional information regarding copyright ownership. The |
| 7 | + * ASF licenses this file to you under the Apache License, Version 2.0 (the |
| 8 | + * "License"); you may not use this file except in compliance with the |
| 9 | + * License. You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 16 | + * License for the specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + * |
| 19 | + ****************************************************************************/ |
| 20 | + |
| 21 | +/**************************************************************************** |
| 22 | + * Included Files |
| 23 | + ****************************************************************************/ |
| 24 | + |
| 25 | +#include <nuttx/config.h> |
| 26 | + |
| 27 | +#ifdef CONFIG_ESPRESSIF_SPI_BITBANG |
| 28 | +#include <sys/param.h> |
| 29 | +#include <sys/types.h> |
| 30 | +#include <stdint.h> |
| 31 | +#include <string.h> |
| 32 | +#include <assert.h> |
| 33 | +#include <debug.h> |
| 34 | + |
| 35 | +#include <nuttx/arch.h> |
| 36 | +#include <nuttx/irq.h> |
| 37 | +#include <nuttx/mutex.h> |
| 38 | +#include <nuttx/kmalloc.h> |
| 39 | +#include <nuttx/spi/spi.h> |
| 40 | +#include <nuttx/spi/spi_bitbang.h> |
| 41 | + |
| 42 | +#include "esp_spi_bitbang.h" |
| 43 | +#include <nuttx/spi/spi_bitbang.c> |
| 44 | + |
| 45 | +#if defined(CONFIG_ARCH_CHIP_ESP32S3) |
| 46 | +#include "esp32s3_gpio.h" |
| 47 | +#include "hardware/esp32s3_gpio_sigmap.h" |
| 48 | +#elif defined(CONFIG_ARCH_CHIP_ESP32S2) |
| 49 | +#include "esp32s2_gpio.h" |
| 50 | +#include "esp32s2_gpio_sigmap.h" |
| 51 | +#else |
| 52 | +#include "esp32_gpio.h" |
| 53 | +#include "esp32_gpio_sigmap.h" |
| 54 | +#endif |
| 55 | + |
| 56 | +/**************************************************************************** |
| 57 | + * Pre-processor Definitions |
| 58 | + ****************************************************************************/ |
| 59 | + |
| 60 | +#if defined(CONFIG_ARCH_CHIP_ESP32S3) |
| 61 | +#define CONFIG_GPIO(pin, attr) esp32s3_configgpio(pin, attr) |
| 62 | +#define GPIO_MATRIX_OUT(pin, idx, inv, en_inv) esp32s3_gpio_matrix_out(pin, \ |
| 63 | + idx, inv, en_inv) |
| 64 | +#elif defined(CONFIG_ARCH_CHIP_ESP32S2) |
| 65 | +#define CONFIG_GPIO(pin, attr) esp32s2_configgpio(pin, attr) |
| 66 | +#define GPIO_MATRIX_OUT(pin, idx, inv, en_inv) esp32s2_gpio_matrix_out(pin, \ |
| 67 | + idx, inv, en_inv) |
| 68 | +#else |
| 69 | +#define CONFIG_GPIO(pin, attr) esp32_configgpio(pin, attr) |
| 70 | +#define GPIO_MATRIX_OUT(pin, idx, inv, en_inv) esp32_gpio_matrix_out(pin, \ |
| 71 | + idx, inv, en_inv) |
| 72 | +#endif |
| 73 | + |
| 74 | +#if defined(CONFIG_ESPRESSIF_SPI_BITBANG_MODE0) |
| 75 | +#undef SPI_BITBANG_DISABLEMODE0 |
| 76 | +#define SPI_BITBANG_DISABLEMODE1 1 |
| 77 | +#define SPI_BITBANG_DISABLEMODE2 1 |
| 78 | +#define SPI_BITBANG_DISABLEMODE3 1 |
| 79 | +#elif defined(CONFIG_ESPRESSIF_SPI_BITBANG_MODE1) |
| 80 | +#undef SPI_BITBANG_DISABLEMODE1 |
| 81 | +#define SPI_BITBANG_DISABLEMODE0 1 |
| 82 | +#define SPI_BITBANG_DISABLEMODE2 1 |
| 83 | +#define SPI_BITBANG_DISABLEMODE3 1 |
| 84 | +#elif defined(CONFIG_ESPRESSIF_SPI_BITBANG_MODE2) |
| 85 | +#undef SPI_BITBANG_DISABLEMODE2 |
| 86 | +#define SPI_BITBANG_DISABLEMODE0 1 |
| 87 | +#define SPI_BITBANG_DISABLEMODE1 1 |
| 88 | +#define SPI_BITBANG_DISABLEMODE3 1 |
| 89 | +#else |
| 90 | +#undef SPI_BITBANG_DISABLEMODE3 |
| 91 | +#define SPI_BITBANG_DISABLEMODE0 1 |
| 92 | +#define SPI_BITBANG_DISABLEMODE1 1 |
| 93 | +#define SPI_BITBANG_DISABLEMODE2 1 |
| 94 | +#endif |
| 95 | + |
| 96 | +/**************************************************************************** |
| 97 | + * Private Function Prototypes |
| 98 | + ****************************************************************************/ |
| 99 | + |
| 100 | +/* Lower-half SPI */ |
| 101 | + |
| 102 | +static void spi_select(struct spi_bitbang_s *priv, uint32_t devid, |
| 103 | + bool selected); |
| 104 | +static uint8_t spi_status(struct spi_bitbang_s *priv, uint32_t devid); |
| 105 | +#ifdef CONFIG_SPI_CMDDATA |
| 106 | +static int spi_cmddata(struct spi_bitbang_s *priv, uint32_t devid, |
| 107 | + bool cmd); |
| 108 | +#endif |
| 109 | + |
| 110 | +/**************************************************************************** |
| 111 | + * Private Data |
| 112 | + ****************************************************************************/ |
| 113 | + |
| 114 | +struct spi_bitbang_ops_s esp_spi_bitbang_ops = |
| 115 | +{ |
| 116 | + .select = spi_select, |
| 117 | + .status = spi_status, |
| 118 | +#ifdef CONFIG_SPI_CMDDATA |
| 119 | + .cmddata = spi_cmddata, |
| 120 | +#endif |
| 121 | + .setfrequency = spi_setfrequency, |
| 122 | + .setmode = spi_setmode, |
| 123 | + .exchange = spi_exchange, |
| 124 | +}; |
| 125 | + |
| 126 | +/**************************************************************************** |
| 127 | + * Private Functions |
| 128 | + ****************************************************************************/ |
| 129 | + |
| 130 | +/**************************************************************************** |
| 131 | + * Name: spi_select |
| 132 | + * |
| 133 | + * Description: |
| 134 | + * Select or de-selected the SPI device specified by 'devid' |
| 135 | + * |
| 136 | + * Input Parameters: |
| 137 | + * priv - An instance of the bit-bang driver structure |
| 138 | + * devid - The device to select or de-select |
| 139 | + * selected - True:select false:de-select |
| 140 | + * |
| 141 | + * Returned Value: |
| 142 | + * None |
| 143 | + * |
| 144 | + ****************************************************************************/ |
| 145 | + |
| 146 | +static void spi_select(struct spi_bitbang_s *priv, uint32_t devid, |
| 147 | + bool selected) |
| 148 | +{ |
| 149 | + if (selected) |
| 150 | + { |
| 151 | + SPI_CLRCS; |
| 152 | + } |
| 153 | + else |
| 154 | + { |
| 155 | + SPI_SETCS; |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +/**************************************************************************** |
| 160 | + * Name: spi_status |
| 161 | + * |
| 162 | + * Description: |
| 163 | + * Return status of the SPI device specified by 'devid' |
| 164 | + * |
| 165 | + * Input Parameters: |
| 166 | + * priv - An instance of the bit-bang driver structure |
| 167 | + * devid - The device to select or de-select |
| 168 | + * |
| 169 | + * Returned Value: |
| 170 | + * An 8-bit, bit-encoded status byte |
| 171 | + * |
| 172 | + ****************************************************************************/ |
| 173 | + |
| 174 | +static uint8_t spi_status(struct spi_bitbang_s *priv, uint32_t devid) |
| 175 | +{ |
| 176 | + if (devid == SPIDEV_MMCSD(0)) |
| 177 | + { |
| 178 | + return SPI_STATUS_PRESENT; |
| 179 | + } |
| 180 | + |
| 181 | + return 0; |
| 182 | +} |
| 183 | + |
| 184 | +/**************************************************************************** |
| 185 | + * Name: spi_cmddata |
| 186 | + * |
| 187 | + * Description: |
| 188 | + * If there were was a CMD/DATA line, this function would manage it |
| 189 | + * |
| 190 | + * Input Parameters: |
| 191 | + * priv - An instance of the bit-bang driver structure |
| 192 | + * devid - The device to use |
| 193 | + * cmd - True=MCD false=DATA |
| 194 | + * |
| 195 | + * Returned Value: |
| 196 | + * OK |
| 197 | + * |
| 198 | + ****************************************************************************/ |
| 199 | + |
| 200 | +#ifdef CONFIG_SPI_CMDDATA |
| 201 | +static int spi_cmddata(struct spi_bitbang_s *priv, uint32_t devid, |
| 202 | + bool cmd) |
| 203 | +{ |
| 204 | + return OK; |
| 205 | +} |
| 206 | +#endif |
| 207 | + |
| 208 | +/**************************************************************************** |
| 209 | + * Public Functions |
| 210 | + ****************************************************************************/ |
| 211 | + |
| 212 | +/**************************************************************************** |
| 213 | + * Name: esp_spi_bitbang_init |
| 214 | + * |
| 215 | + * Description: |
| 216 | + * Initialize the SPI bit-bang driver |
| 217 | + * |
| 218 | + * Input Parameters: |
| 219 | + * None |
| 220 | + * |
| 221 | + * Returned Value: |
| 222 | + * A non-NULL reference to the SPI driver on success |
| 223 | + * |
| 224 | + ****************************************************************************/ |
| 225 | + |
| 226 | +struct spi_dev_s *esp_spi_bitbang_init(void) |
| 227 | +{ |
| 228 | + /* Configure the SPI bit-bang pins */ |
| 229 | + |
| 230 | + GPIO_WRITE(CONFIG_ESPRESSIF_SPI_BITBANG_CSPIN, true); |
| 231 | + GPIO_WRITE(CONFIG_ESPRESSIF_SPI_BITBANG_MOSIPIN, true); |
| 232 | + GPIO_WRITE(CONFIG_ESPRESSIF_SPI_BITBANG_CLKPIN, true); |
| 233 | + |
| 234 | +#if CONFIG_ESPRESSIF_SPI_SWCS |
| 235 | + CONFIG_GPIO(CONFIG_ESPRESSIF_SPI_BITBANG_CSPIN, OUTPUT_FUNCTION_1); |
| 236 | + GPIO_MATRIX_OUT(CONFIG_ESPRESSIF_SPI_BITBANG_CSPIN, SIG_GPIO_OUT_IDX, |
| 237 | + 0, 0); |
| 238 | +#endif |
| 239 | + CONFIG_GPIO(CONFIG_ESPRESSIF_SPI_BITBANG_MOSIPIN, OUTPUT_FUNCTION_1); |
| 240 | + GPIO_MATRIX_OUT(CONFIG_ESPRESSIF_SPI_BITBANG_MOSIPIN, SIG_GPIO_OUT_IDX, |
| 241 | + 0, 0); |
| 242 | + |
| 243 | + CONFIG_GPIO(CONFIG_ESPRESSIF_SPI_BITBANG_MISOPIN, |
| 244 | + INPUT_FUNCTION_1 | PULLUP); |
| 245 | + GPIO_MATRIX_OUT(CONFIG_ESPRESSIF_SPI_BITBANG_MISOPIN, SIG_GPIO_OUT_IDX, |
| 246 | + 0, 0); |
| 247 | + |
| 248 | + CONFIG_GPIO(CONFIG_ESPRESSIF_SPI_BITBANG_CLKPIN, OUTPUT_FUNCTION_1); |
| 249 | + GPIO_MATRIX_OUT(CONFIG_ESPRESSIF_SPI_BITBANG_CLKPIN, SIG_GPIO_OUT_IDX, |
| 250 | + 0, 0); |
| 251 | + |
| 252 | + /* Create the SPI driver instance */ |
| 253 | + |
| 254 | + return spi_create_bitbang(&esp_spi_bitbang_ops, NULL); |
| 255 | +} |
| 256 | + |
| 257 | +/**************************************************************************** |
| 258 | + * Name: esp_spi_bitbang_uninitialize |
| 259 | + * |
| 260 | + * Description: |
| 261 | + * Destroy an instance of the SPI bit-bang driver. |
| 262 | + * |
| 263 | + * Input Parameters: |
| 264 | + * dev - device instance, target driver to destroy. |
| 265 | + * |
| 266 | + ****************************************************************************/ |
| 267 | + |
| 268 | +void esp_spi_bitbang_uninitialize(struct spi_dev_s *dev) |
| 269 | +{ |
| 270 | + spi_destroy_bitbang(dev); |
| 271 | +} |
| 272 | + |
| 273 | +#endif /* CONFIG_ESPRESSIF_SPI_BITBANG */ |
0 commit comments