Skip to content

Commit 3f77e97

Browse files
eren-terziogluxiaoxiang781216
authored andcommitted
esp32[s2|s3]: Add SPI bitbang support
1 parent 243a2ad commit 3f77e97

File tree

11 files changed

+884
-22
lines changed

11 files changed

+884
-22
lines changed

arch/xtensa/src/common/espressif/Kconfig

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,22 @@ config ESPRESSIF_I2C_BITBANG
4040
---help---
4141
Software implemented I2C peripheral with GPIOs. Suggested to use if I2C peripherals are already in use.
4242

43+
config ESPRESSIF_SPI_PERIPH
44+
bool
45+
depends on (ESP32S3_SPI2 || ESP32S3_SPI3) || (ESP32_SPI2 || ESP32_SPI3) || (ESP32S2_SPI2 || ESP32S2_SPI3)
46+
default n
47+
48+
config ESPRESSIF_SPI_BITBANG
49+
bool "SPI Bitbang"
50+
default n
51+
select ESP32_SPI if ARCH_CHIP_ESP32
52+
select ESP32S2_SPI if ARCH_CHIP_ESP32S2
53+
select ESP32S3_SPI if ARCH_CHIP_ESP32S3
54+
select SPI
55+
select SPI_BITBANG
56+
---help---
57+
Software implemented SPI peripheral with GPIOs. Suggested to use if SPI peripherals are already in use.
58+
4359
config ESPRESSIF_SPIFLASH
4460
bool "SPI Flash"
4561
depends on ARCH_CHIP_ESP32S2
@@ -104,6 +120,49 @@ config ESPRESSIF_I2C_BITBANG_SDAPIN
104120

105121
endmenu # I2C bitbang configuration
106122

123+
menu "SPI bitbang configuration"
124+
depends on ESPRESSIF_SPI_BITBANG
125+
126+
config ESPRESSIF_SPI_BITBANG_CSPIN
127+
int "SPI Bitbang CS Pin"
128+
default 0
129+
range 0 21
130+
131+
config ESPRESSIF_SPI_BITBANG_CLKPIN
132+
int "SPI Bitbang CLK Pin"
133+
default 1
134+
range 0 21
135+
136+
config ESPRESSIF_SPI_BITBANG_MOSIPIN
137+
int "SPI Bitbang MOSI Pin"
138+
default 2
139+
range 0 21
140+
141+
config ESPRESSIF_SPI_BITBANG_MISOPIN
142+
int "SPI Bitbang MISO Pin"
143+
default 3
144+
range 0 21
145+
146+
choice ESPRESSIF_SPI_BITBANG_MODE
147+
prompt "SPI Bitbang mode"
148+
default ESPRESSIF_SPI_BITBANG_MODE0
149+
150+
config ESPRESSIF_SPI_BITBANG_MODE0
151+
bool "SPI MODE0"
152+
153+
config ESPRESSIF_SPI_BITBANG_MODE1
154+
bool "SPI MODE1"
155+
156+
config ESPRESSIF_SPI_BITBANG_MODE2
157+
bool "SPI MODE2"
158+
159+
config ESPRESSIF_SPI_BITBANG_MODE3
160+
bool "SPI MODE3"
161+
162+
endchoice # ESPRESSIF_SPI_BITBANG_MODE
163+
164+
endmenu # SPI bitbang configuration
165+
107166
config ESPRESSIF_HAVE_OTA_PARTITION
108167
bool
109168
default n

arch/xtensa/src/common/espressif/Make.defs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ ifeq ($(CONFIG_ESPRESSIF_I2C_BITBANG),y)
4444
CHIP_CSRCS += esp_i2c_bitbang.c
4545
endif
4646

47+
ifeq ($(CONFIG_ESPRESSIF_SPI_BITBANG),y)
48+
CHIP_CSRCS += esp_spi_bitbang.c
49+
endif
50+
4751
ifeq ($(CONFIG_ESPRESSIF_SPIFLASH),y)
4852
CHIP_CSRCS += esp_spiflash.c
4953
ifeq ($(CONFIG_ESPRESSIF_MTD),y)
Lines changed: 273 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,273 @@
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

Comments
 (0)