Skip to content

Commit 908dc68

Browse files
authored
[bsp][ESP32] add spi custom configuration function and enhance muti object initliz… (#9643)
* add spi custom configuration function and enhance muti object initlization and setting * update changelog * submit ci files * fix ci file * update * formatting * delete yaml files * fix ble ci file * fix undefined reference to app_main error * update
1 parent ebe2926 commit 908dc68

File tree

3 files changed

+147
-28
lines changed

3 files changed

+147
-28
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
scons.args: &scons
2+
scons_arg:
3+
- '--strict'
4+
devices.gpio:
5+
<<: *scons
6+
kconfig:
7+
- CONFIG_RT_USING_PIN=y
8+
- CONFIG_BSP_USING_GPIO=y
9+
devices.uart:
10+
kconfig:
11+
- CONFIG_RT_USING_SERIAL=y
12+
- CONFIG_RT_USING_SERIAL_V1=y
13+
- CONFIG_BSP_USING_UART=y
14+
devices.i2c:
15+
kconfig:
16+
- CONFIG_RT_USING_I2C=y
17+
- CONFIG_BSP_USING_I2C=y
18+
- CONFIG_BSP_USING_I2C0=y
19+
devices.spi:
20+
kconfig:
21+
- CONFIG_RT_USING_SPI=y
22+
- CONFIG_BSP_USING_SPI=y
23+
- CONFIG_BSP_USING_SPI2=y
24+
devices.hwtimer:
25+
kconfig:
26+
- CONFIG_RT_USING_HWTIMER=y
27+
- CONFIG_BSP_USING_HWTIMER=y
28+
- CONFIG_BSP_USING_TIMER0=y
29+
devices.adc:
30+
kconfig:
31+
- CONFIG_RT_USING_ADC=y
32+
- CONFIG_BSP_USING_ADC=y
33+
- CONFIG_BSP_USING_ADC1=y
34+
devices.pwm:
35+
kconfig:
36+
- CONFIG_RT_USING_PWM=y
37+
- CONFIG_BSP_USING_PWM=y
38+
- CONFIG_BSP_USING_PWM0=y
39+
devices.ble:
40+
kconfig:
41+
- CONFIG_BSP_USING_BLE=y
42+
devices.wifi:
43+
kconfig:
44+
- CONFIG_BSP_USING_WIFI=y
45+
- CONFIG_RT_USING_WIFI=y
46+
- CONFIG_RT_USING_LWIP=y
47+
- CONFIG_RT_USING_NETDEV=y

bsp/ESP32_C3/drivers/drv_spi.c

Lines changed: 90 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
* Change Logs:
77
* Date Author Notes
88
* 2024-10-08 wumingzi first implementation
9+
* 2024-10-08 wumingzi add custom configuration and support muti spi obj
910
*/
1011

1112
#include <rtthread.h>
1213
#include <rtdevice.h>
14+
#include <time.h>
1315

1416
#include "rtdef.h"
1517
#include "rttypes.h"
@@ -20,6 +22,7 @@
2022
#include "driver/spi_master.h"
2123

2224
#include "drv_spi.h"
25+
#include "drivers/dev_spi.h"
2326

2427
#ifdef RT_USING_SPI
2528
#ifdef BSP_USING_SPI2
@@ -29,7 +32,6 @@
2932
static struct rt_spi_bus spi_bus2;
3033

3134
static spi_device_handle_t spi;
32-
3335
static spi_bus_config_t buscfg;
3436

3537
static struct esp32_spi spi_bus_obj[] = {
@@ -62,20 +64,9 @@ static void esp32_spi_init(struct esp32_spi *esp32_spi)
6264
spi_configure(NULL,NULL);
6365
}
6466

65-
static void spi_pin_mode(rt_base_t pin)
66-
{
67-
gpio_config_t io_conf;
68-
io_conf.intr_type = GPIO_INTR_DISABLE;
69-
io_conf.mode = GPIO_MODE_OUTPUT;
70-
io_conf.pin_bit_mask = (1ULL << pin);
71-
io_conf.pull_down_en = 0;
72-
io_conf.pull_up_en = 1;
73-
}
74-
7567
static rt_err_t spi_configure(struct rt_spi_device* device,
7668
struct rt_spi_configuration* configuration)
7769
{
78-
/* spi_pin_mode(RT_BSP_SPI_CS_PIN);*/
7970
static spi_bus_config_t buscfg =
8071
{
8172
.miso_io_num=SPI2_IOMUX_PIN_NUM_MISO, /*MISO*/
@@ -89,19 +80,87 @@ static rt_err_t spi_configure(struct rt_spi_device* device,
8980
esp_err_t err = spi_bus_initialize(SPI2_HOST, &buscfg, SPI_DMA_CH_AUTO);
9081
ESP_ERROR_CHECK(err);
9182

92-
static spi_device_interface_config_t devcfg={
93-
.clock_speed_hz = SPI_MASTER_FREQ_8M,
94-
.mode = 0,
95-
.spics_io_num = RT_BSP_SPI_CS_PIN,
96-
.queue_size = 7,
97-
};
83+
static spi_device_interface_config_t devcfg;
84+
if(configuration->data_width == 8)
85+
{
86+
size_t length; /*/< Total data length, in bits*/
87+
size_t rxlength; /*/< Total data length received, should be not greater than ``length`` in full-duplex mode (0 defaults this to the value of ``length``)*/
88+
}
89+
90+
LOG_W("configuration->max_hz = %d \n",configuration->max_hz);
91+
if(configuration->max_hz >= SPI_MASTER_FREQ_80M)
92+
{
93+
devcfg.clock_speed_hz = SPI_MASTER_FREQ_80M; /*/< 80MHz*/
94+
}
95+
else if(configuration->max_hz >= SPI_MASTER_FREQ_40M)
96+
{
97+
devcfg.clock_speed_hz = SPI_MASTER_FREQ_40M; /*/< 40MHz*/
98+
}
99+
else if(configuration->max_hz >= SPI_MASTER_FREQ_26M)
100+
{
101+
devcfg.clock_speed_hz = SPI_MASTER_FREQ_26M; /*/< 26.67MHz*/
102+
}
103+
else if(configuration->max_hz >= SPI_MASTER_FREQ_20M)
104+
{
105+
devcfg.clock_speed_hz = SPI_MASTER_FREQ_20M; /*/< 20MHz*/
106+
}
107+
else if(configuration->max_hz >= SPI_MASTER_FREQ_16M)
108+
{
109+
devcfg.clock_speed_hz = SPI_MASTER_FREQ_16M; /*/< 16MHz*/
110+
}
111+
else if(configuration->max_hz >= SPI_MASTER_FREQ_13M)
112+
{
113+
devcfg.clock_speed_hz = SPI_MASTER_FREQ_13M; /*/< 13.33MHz*/
114+
}
115+
else if(configuration->max_hz >= SPI_MASTER_FREQ_11M)
116+
{
117+
devcfg.clock_speed_hz = SPI_MASTER_FREQ_11M; /*/< 11.43MHz*/
118+
}
119+
else if(configuration->max_hz >= SPI_MASTER_FREQ_10M)
120+
{
121+
devcfg.clock_speed_hz = SPI_MASTER_FREQ_10M; /*/< 10MHz*/
122+
}
123+
else if(configuration->max_hz >= SPI_MASTER_FREQ_9M)
124+
{
125+
devcfg.clock_speed_hz = SPI_MASTER_FREQ_9M ; /*/< 8.89MHz*/
126+
}
127+
else
128+
{
129+
devcfg.clock_speed_hz = SPI_MASTER_FREQ_8M ;
130+
}
131+
132+
switch (configuration->mode)
133+
{
134+
case RT_SPI_MODE_0: /*!< CPOL = 0, CPHA = 0 */
135+
devcfg.mode = 0;
136+
case RT_SPI_MODE_1: /*!< CPOL = 0, CPHA = 1 */
137+
devcfg.mode = 1;
138+
case RT_SPI_MODE_2: /*!< CPOL = 1, CPHA = 0 */
139+
devcfg.mode = 2;
140+
case RT_SPI_MODE_3: /*!< CPOL = 1, CPHA = 1 */
141+
devcfg.mode = 3;
142+
default:
143+
devcfg.mode = 0;
144+
}
145+
146+
/* todo: support changing cs_pin,queue_size or specifing spi_device_interface_config_t and
147+
* spi_transaction_t by resever data.Meanwhile finish the initialization of interrupt
148+
* callback function and dma.
149+
*/
150+
151+
devcfg.spics_io_num = RT_BSP_SPI_CS_PIN;
152+
devcfg.queue_size = 7;
98153

99154
err = spi_bus_add_device(SPI2_HOST, &devcfg, &spi);
100155
ESP_ERROR_CHECK(err);
101156

102-
spi_bus_obj[0].bus_name = "spi2";
103-
spi_bus_obj[0].spi_bus = &spi_bus2;
104-
spi_bus_obj[0].esp32_spi_bus_cfg = &buscfg;
157+
/* Although there is only one spi bus object, it will be a template for other bsps of ESP32 series */
158+
for(int i = 0; i < sizeof(spi_bus_obj)/sizeof(spi_bus_obj[0]); i++)
159+
{
160+
spi_bus_obj[i].bus_name = "spi2";
161+
spi_bus_obj[i].spi_bus = &spi_bus2;
162+
spi_bus_obj[i].esp32_spi_bus_cfg = &buscfg;
163+
}
105164

106165
return RT_EOK;
107166
};
@@ -116,8 +175,8 @@ static rt_ssize_t spixfer(struct rt_spi_device* device, struct rt_spi_message* m
116175

117176
trans.tx_buffer = message->send_buf;
118177
trans.rx_buffer = message->recv_buf;
119-
trans.length = message->length;
120-
trans.rxlength = message->length;
178+
trans.length = (message->length)*8;
179+
trans.rxlength = (message->length)*8;
121180

122181
spi_device_acquire_bus(spi, portMAX_DELAY);
123182
esp_err_t err = spi_device_polling_transmit(spi, &trans);
@@ -161,17 +220,20 @@ int rt_hw_spi_init(void)
161220
{
162221
int result = 0;
163222

164-
spi_bus_obj[0].spi_bus->parent.user_data = (void *)&spi_bus_obj[0];
165-
result = rt_spi_bus_register(spi_bus_obj[0].spi_bus, spi_bus_obj[0].bus_name, &esp32_spi_ops);
223+
for(int i = 0; i < sizeof(spi_bus_obj)/sizeof(spi_bus_obj[0]); i++)
224+
{
225+
spi_bus_obj[i].spi_bus->parent.user_data = (void *)&spi_bus_obj[i];
226+
result = rt_spi_bus_register(spi_bus_obj[i].spi_bus, spi_bus_obj[i].bus_name, &esp32_spi_ops);
166227

167-
RT_ASSERT(result == RT_EOK);
228+
RT_ASSERT(result == RT_EOK);
168229

169-
LOG_D("%s bus init done", spi_bus_obj[i].bus_name);
230+
LOG_D("%s bus init done", spi_bus_obj[i].bus_name);
231+
}
170232

171233
return result;
172234
}
173235

174236
INIT_BOARD_EXPORT(rt_hw_spi_init);
175237

176238
#endif /* BSP_USING_SPI0 || BSP_USING_SPI1 || BSP_USING_SPI2 || BSP_USING_SPI3 || BSP_USING_SPI4*/
177-
#endif /* RT_USING_SPI */
239+
#endif /* RT_USING_SPI */

bsp/ESP32_C3/main/main.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@
1414
#include <rtdevice.h>
1515
#include <board.h>
1616

17+
#ifdef BSP_USING_BLE
18+
void app_main()
19+
{
20+
while(1)
21+
{
22+
23+
}
24+
}
25+
#endif /* BSP_USING_BLE */
26+
1727
int main(void)
1828
{
1929
rt_kprintf("Hello!RT-THREAD!\r\n");

0 commit comments

Comments
 (0)