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;
2930static 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
103124static 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,6 +295,18 @@ static rt_uint32_t spixfer(struct rt_spi_device* device, struct rt_spi_message*
261295 data = * send_ptr ++ ;
262296 }
263297
298+ #if defined (SOC_SERIES_GD32VW55x )
299+ // Todo: replace register read/write by gd32f4 lib
300+ //Wait until the transmit buffer is empty
301+ while (RESET == spi_flag_get (SPI_FLAG_TBE ));
302+ // Send the byte
303+ spi_data_transmit (data );
304+
305+ //Wait until a data is received
306+ while (RESET == spi_flag_get (SPI_FLAG_RBNE ));
307+ // Get the received data
308+ data = spi_data_receive ();
309+ #else
264310 // Todo: replace register read/write by gd32f4 lib
265311 //Wait until the transmit buffer is empty
266312 while (RESET == spi_i2s_flag_get (spi_periph , SPI_FLAG_TBE ));
@@ -271,6 +317,7 @@ static rt_uint32_t spixfer(struct rt_spi_device* device, struct rt_spi_message*
271317 while (RESET == spi_i2s_flag_get (spi_periph , SPI_FLAG_RBNE ));
272318 // Get the received data
273319 data = spi_i2s_data_receive (spi_periph );
320+ #endif
274321
275322 if (recv_ptr != RT_NULL )
276323 {
@@ -294,6 +341,19 @@ static rt_uint32_t spixfer(struct rt_spi_device* device, struct rt_spi_message*
294341 data = * send_ptr ++ ;
295342 }
296343
344+ #if defined (SOC_SERIES_GD32VW55x )
345+ // Todo: replace register read/write by gd32f4 lib
346+ //Wait until the transmit buffer is empty
347+ while (RESET == spi_flag_get (SPI_FLAG_TBE ));
348+ // Send the byte
349+ spi_data_transmit (data );
350+
351+ //Wait until a data is received
352+ while (RESET == spi_flag_get (SPI_FLAG_RBNE ));
353+ // Get the received data
354+ data = spi_data_receive ();
355+ #else
356+ // Todo: replace register read/write by gd32f4 lib
297357 //Wait until the transmit buffer is empty
298358 while (RESET == spi_i2s_flag_get (spi_periph , SPI_FLAG_TBE ));
299359 // Send the byte
@@ -303,6 +363,7 @@ static rt_uint32_t spixfer(struct rt_spi_device* device, struct rt_spi_message*
303363 while (RESET == spi_i2s_flag_get (spi_periph , SPI_FLAG_RBNE ));
304364 // Get the received data
305365 data = spi_i2s_data_receive (spi_periph );
366+ #endif
306367
307368 if (recv_ptr != RT_NULL )
308369 {
0 commit comments