Skip to content

Commit ec2182e

Browse files
authored
[bsp/gd32]Add GD32VW553H spi support (#11192)
* Add VW553H spi support * Fix Some Issues
1 parent c5c61db commit ec2182e

File tree

3 files changed

+103
-14
lines changed

3 files changed

+103
-14
lines changed

bsp/gd32/risc-v/gd32vw553h-eval/.ci/attachconfig/ci.attachconfig.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ devices.i2c:
1919
- CONFIG_BSP_HW_I2C1_PIN_PB12_PB13=y
2020
- CONFIG_BSP_HW_I2C1_CLK=100
2121

22+
23+
devices.spi:
24+
kconfig:
25+
- CONFIG_BSP_USING_SPI=y
26+
- CONFIG_BSP_USING_SPI0=y
27+
28+
2229
devices.wlan:
2330
kconfig:
2431
- CONFIG_BSP_USING_WLAN=y
@@ -61,4 +68,4 @@ peripheral.at24c02:
6168
- CONFIG_BSP_USING_HW_I2C=y
6269
- CONFIG_BSP_USING_HW_I2C0=y
6370
- CONFIG_BSP_HW_I2C0_PIN_PA2_PA3=y
64-
- CONFIG_BSP_HW_I2C0_CLK=100
71+
- CONFIG_BSP_HW_I2C0_CLK=100

bsp/gd32/risc-v/gd32vw553h-eval/board/Kconfig

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ menu "On-chip Peripheral Drivers"
107107
bool "Enable PWM16"
108108
default n
109109
endif
110+
110111
menuconfig BSP_USING_HW_I2C
111112
bool "Enable Hardware I2C"
112113
default n
@@ -165,6 +166,26 @@ menu "On-chip Peripheral Drivers"
165166
range 10 1000
166167
endif
167168

169+
170+
menuconfig BSP_USING_SPI
171+
bool "Enable SPI BUS"
172+
default n
173+
select RT_USING_SPI
174+
if BSP_USING_SPI
175+
config BSP_USING_SPI0
176+
bool "Enable SPI0 BUS"
177+
default n
178+
179+
config BSP_SPI0_TX_USING_DMA
180+
bool "Enable SPI0 TX DMA"
181+
depends on BSP_USING_SPI0
182+
default n
183+
184+
config BSP_SPI0_RX_USING_DMA
185+
bool "Enable SPI0 RX DMA"
186+
depends on BSP_USING_SPI0
187+
select BSP_SPI0_TX_USING_DMA
188+
168189
menuconfig BSP_USING_HWTIMER
169190
bool "Enable timer"
170191
default n
@@ -187,6 +208,7 @@ menu "On-chip Peripheral Drivers"
187208
default n
188209
config BSP_USING_HWTIMER16
189210
bool "Enable TIM16"
211+
190212
default n
191213
endif
192214

@@ -219,7 +241,10 @@ menu "Board extended module Drivers"
219241

220242
endif
221243

244+
222245
endmenu
223246

247+
224248
endmenu
225249

250+
endmenu

bsp/gd32/risc-v/libraries/gd32_drivers/drv_spi.c

Lines changed: 70 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* Change Logs:
77
* Date Author Notes
88
* 2022-06-04 BruceOu first implementation
9+
* 2026-01-24 Unknown Add GD32VW553h
910
*/
1011
#include "drv_spi.h"
1112

@@ -29,6 +30,19 @@ static struct rt_spi_bus spi_bus2;
2930
static const struct gd32_spi spi_bus_obj[] = {
3031

3132
#ifdef BSP_USING_SPI0
33+
#if defined (SOC_SERIES_GD32VW55x)
34+
{
35+
SPI,
36+
"spi0",
37+
RCU_SPI,
38+
RCU_GPIOA,
39+
&spi_bus0,
40+
GPIOA,
41+
GPIO_PIN_9,
42+
GPIO_PIN_10,
43+
GPIO_PIN_11,
44+
}
45+
#else
3246
{
3347
SPI0,
3448
"spi0",
@@ -40,6 +54,7 @@ static const struct gd32_spi spi_bus_obj[] = {
4054
GPIO_PIN_6,
4155
GPIO_PIN_7,
4256
}
57+
#endif
4358
#endif /* BSP_USING_SPI0 */
4459

4560
#ifdef BSP_USING_SPI1
@@ -92,16 +107,22 @@ static void gd32_spi_init(struct gd32_spi *gd32_spi)
92107
rcu_periph_clock_enable(gd32_spi->spi_clk);
93108
rcu_periph_clock_enable(gd32_spi->gpio_clk);
94109

110+
#if defined (SOC_SERIES_GD32VW55x)
111+
/* configure SPI GPIO: SCK, MISO, MOSI */
112+
gpio_af_set(gd32_spi->spi_port, GPIO_AF_0, gd32_spi->sck_pin | gd32_spi->miso_pin | gd32_spi->mosi_pin);
113+
114+
gpio_mode_set(gd32_spi->spi_port, GPIO_MODE_AF, GPIO_PUPD_NONE, gd32_spi->sck_pin | gd32_spi->miso_pin | gd32_spi->mosi_pin);
115+
gpio_output_options_set(gd32_spi->spi_port, GPIO_OTYPE_PP, GPIO_OSPEED_25MHZ, gd32_spi->sck_pin | gd32_spi->miso_pin | gd32_spi->mosi_pin);
116+
#else
95117
/* Init SPI SCK MOSI */
96118
gpio_init(gd32_spi->spi_port, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, gd32_spi->sck_pin | gd32_spi->mosi_pin);
97-
98119
/* Init SPI MISO */
99120
gpio_init(gd32_spi->spi_port, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, gd32_spi->miso_pin);
100-
121+
#endif
101122
}
102123

103124
static rt_err_t spi_configure(struct rt_spi_device* device,
104-
struct rt_spi_configuration* configuration)
125+
struct rt_spi_configuration* configuration)
105126
{
106127
struct rt_spi_bus * spi_bus = (struct rt_spi_bus *)device->bus;
107128
struct gd32_spi *spi_device = (struct gd32_spi *)spi_bus->parent.user_data;
@@ -140,6 +161,9 @@ static rt_err_t spi_configure(struct rt_spi_device* device,
140161
LOG_D("CK_APB2 freq: %d\n", rcu_clock_freq_get(CK_APB2));
141162
LOG_D("max freq: %d\n", max_hz);
142163

164+
#if defined (SOC_SERIES_GD32VW55x)
165+
spi_src = CK_APB2;
166+
#else
143167
if (spi_periph == SPI1 || spi_periph == SPI2)
144168
{
145169
spi_src = CK_APB1;
@@ -148,6 +172,9 @@ static rt_err_t spi_configure(struct rt_spi_device* device,
148172
{
149173
spi_src = CK_APB2;
150174
}
175+
#endif
176+
177+
151178
spi_apb_clock = rcu_clock_freq_get(spi_src);
152179

153180
if(max_hz >= spi_apb_clock/2)
@@ -215,12 +242,19 @@ static rt_err_t spi_configure(struct rt_spi_device* device,
215242
spi_init_struct.device_mode = SPI_MASTER;
216243
spi_init_struct.nss = SPI_NSS_SOFT;
217244

245+
#if defined (SOC_SERIES_GD32VW55x)
246+
spi_crc_off();
247+
/* init SPI */
248+
spi_init(&spi_init_struct);
249+
/* Enable SPI_MASTER */
250+
spi_enable();
251+
#else
218252
spi_crc_off(spi_periph);
219-
220253
/* init SPI */
221254
spi_init(spi_periph, &spi_init_struct);
222255
/* Enable SPI_MASTER */
223256
spi_enable(spi_periph);
257+
#endif
224258

225259
return RT_EOK;
226260
};
@@ -261,16 +295,27 @@ static rt_uint32_t spixfer(struct rt_spi_device* device, struct rt_spi_message*
261295
data = *send_ptr++;
262296
}
263297

264-
// Todo: replace register read/write by gd32f4 lib
265-
//Wait until the transmit buffer is empty
298+
#if defined (SOC_SERIES_GD32VW55x)
299+
/* Wait until the transmit buffer is empty */
300+
while(RESET == spi_flag_get(SPI_FLAG_TBE));
301+
/* Send the byte */
302+
spi_data_transmit(data);
303+
304+
/* Wait until a data is received */
305+
while(RESET == spi_flag_get(SPI_FLAG_RBNE));
306+
/* Get the received data */
307+
data = spi_data_receive();
308+
#else
309+
/* Wait until the transmit buffer is empty */
266310
while(RESET == spi_i2s_flag_get(spi_periph, SPI_FLAG_TBE));
267-
// Send the byte
311+
/* Send the byte */
268312
spi_i2s_data_transmit(spi_periph, data);
269313

270-
//Wait until a data is received
314+
/* Wait until a data is received */
271315
while(RESET == spi_i2s_flag_get(spi_periph, SPI_FLAG_RBNE));
272-
// Get the received data
316+
/* Get the received data */
273317
data = spi_i2s_data_receive(spi_periph);
318+
#endif
274319

275320
if(recv_ptr != RT_NULL)
276321
{
@@ -294,15 +339,27 @@ static rt_uint32_t spixfer(struct rt_spi_device* device, struct rt_spi_message*
294339
data = *send_ptr++;
295340
}
296341

297-
//Wait until the transmit buffer is empty
342+
#if defined (SOC_SERIES_GD32VW55x)
343+
/* Wait until the transmit buffer is empty */
344+
while(RESET == spi_flag_get(SPI_FLAG_TBE));
345+
/* Send the byte */
346+
spi_data_transmit(data);
347+
348+
/* Wait until a data is received */
349+
while(RESET == spi_flag_get(SPI_FLAG_RBNE));
350+
/* Get the received data */
351+
data = spi_data_receive();
352+
#else
353+
/* Wait until the transmit buffer is empty */
298354
while(RESET == spi_i2s_flag_get(spi_periph, SPI_FLAG_TBE));
299-
// Send the byte
355+
/* Send the byte */
300356
spi_i2s_data_transmit(spi_periph, data);
301357

302-
//Wait until a data is received
358+
/* Wait until a data is received */
303359
while(RESET == spi_i2s_flag_get(spi_periph, SPI_FLAG_RBNE));
304-
// Get the received data
360+
/* Get the received data */
305361
data = spi_i2s_data_receive(spi_periph);
362+
#endif
306363

307364
if(recv_ptr != RT_NULL)
308365
{

0 commit comments

Comments
 (0)