Skip to content

Commit 014f885

Browse files
Forest-Rainmysterywolf
authored andcommitted
[gd32][spi] add spi3\spi4 support and rt_hw_spi_device_attach
1 parent a2b7a44 commit 014f885

File tree

2 files changed

+96
-7
lines changed

2 files changed

+96
-7
lines changed

bsp/gd32/arm/libraries/gd32_drivers/drv_spi.c

+91-7
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
#ifdef RT_USING_SPI
1313

14-
#if defined(BSP_USING_SPI0) || defined(BSP_USING_SPI1) || defined(BSP_USING_SPI2)
14+
#if defined(BSP_USING_SPI0) || defined(BSP_USING_SPI1) || defined(BSP_USING_SPI2) || defined(BSP_USING_SPI3) || defined(BSP_USING_SPI4)
1515
#define LOG_TAG "drv.spi"
1616

1717
#include <rtdbg.h>
@@ -25,6 +25,12 @@ static struct rt_spi_bus spi_bus1;
2525
#ifdef BSP_USING_SPI2
2626
static struct rt_spi_bus spi_bus2;
2727
#endif
28+
#ifdef BSP_USING_SPI3
29+
static struct rt_spi_bus spi_bus3;
30+
#endif
31+
#ifdef BSP_USING_SPI4
32+
static struct rt_spi_bus spi_bus4;
33+
#endif
2834

2935
static const struct gd32_spi spi_bus_obj[] = {
3036

@@ -36,10 +42,13 @@ static const struct gd32_spi spi_bus_obj[] = {
3642
RCU_GPIOA,
3743
&spi_bus0,
3844
GPIOA,
45+
#if defined SOC_SERIES_GD32F4xx
46+
GPIO_AF_5,
47+
#endif
3948
GPIO_PIN_5,
4049
GPIO_PIN_6,
4150
GPIO_PIN_7,
42-
}
51+
},
4352
#endif /* BSP_USING_SPI0 */
4453

4554
#ifdef BSP_USING_SPI1
@@ -50,10 +59,13 @@ static const struct gd32_spi spi_bus_obj[] = {
5059
RCU_GPIOB,
5160
&spi_bus1,
5261
GPIOB,
62+
#if defined SOC_SERIES_GD32F4xx
63+
GPIO_AF_5,
64+
#endif
5365
GPIO_PIN_12,
5466
GPIO_PIN_14,
5567
GPIO_PIN_15,
56-
}
68+
},
5769
#endif /* BSP_USING_SPI1 */
5870

5971
#ifdef BSP_USING_SPI2
@@ -64,11 +76,48 @@ static const struct gd32_spi spi_bus_obj[] = {
6476
RCU_GPIOB,
6577
&spi_bus2,
6678
GPIOB,
79+
#if defined SOC_SERIES_GD32F4xx
80+
GPIO_AF_6,
81+
#endif
6782
GPIO_PIN_3,
6883
GPIO_PIN_4,
6984
GPIO_PIN_5,
70-
}
85+
},
7186
#endif /* BSP_USING_SPI2 */
87+
88+
#ifdef BSP_USING_SPI3
89+
{
90+
SPI2,
91+
"spi2",
92+
RCU_SPI3,
93+
RCU_GPIOE,
94+
&spi_bus3,
95+
GPIOB,
96+
#if defined SOC_SERIES_GD32F4xx
97+
GPIO_AF_5,
98+
#endif
99+
GPIO_PIN_2,
100+
GPIO_PIN_5,
101+
GPIO_PIN_6,
102+
},
103+
#endif /* BSP_USING_SPI3 */
104+
105+
#ifdef BSP_USING_SPI4
106+
{
107+
SPI4,
108+
"spi4",
109+
RCU_SPI4,
110+
RCU_GPIOF,
111+
&spi_bus4,
112+
GPIOF,
113+
#if defined SOC_SERIES_GD32F4xx
114+
GPIO_AF_5,
115+
#endif
116+
GPIO_PIN_7,
117+
GPIO_PIN_8,
118+
GPIO_PIN_9,
119+
}
120+
#endif /* BSP_USING_SPI4 */
72121
};
73122

74123
/* private rt-thread spi ops function */
@@ -94,11 +143,10 @@ static void gd32_spi_init(struct gd32_spi *gd32_spi)
94143

95144
#if defined SOC_SERIES_GD32F4xx
96145
/*GPIO pin configuration*/
97-
gpio_af_set(gd32_spi->spi_port, GPIO_AF_5, gd32_spi->sck_pin | gd32_spi->mosi_pin | gd32_spi->miso_pin);
146+
gpio_af_set(gd32_spi->spi_port, gd32_spi->alt_func_num, gd32_spi->sck_pin | gd32_spi->mosi_pin | gd32_spi->miso_pin);
98147

99148
gpio_mode_set(gd32_spi->spi_port, GPIO_MODE_AF, GPIO_PUPD_NONE, gd32_spi->sck_pin | gd32_spi->mosi_pin | gd32_spi->miso_pin);
100149
gpio_output_options_set(gd32_spi->spi_port, GPIO_OTYPE_PP, GPIO_OSPEED_200MHZ, gd32_spi->sck_pin | gd32_spi->mosi_pin | gd32_spi->miso_pin);
101-
102150
#else
103151
/* Init SPI SCK MOSI */
104152
gpio_init(gd32_spi->spi_port, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, gd32_spi->sck_pin | gd32_spi->mosi_pin);
@@ -331,6 +379,42 @@ static rt_uint32_t spixfer(struct rt_spi_device* device, struct rt_spi_message*
331379
return message->length;
332380
};
333381

382+
/**
383+
* Attach the spi device to SPI bus, this function must be used after initialization.
384+
*/
385+
rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name, rt_base_t cs_pin)
386+
{
387+
RT_ASSERT(bus_name != RT_NULL);
388+
RT_ASSERT(device_name != RT_NULL);
389+
390+
rt_err_t result;
391+
struct rt_spi_device *spi_device;
392+
393+
/* attach the device to spi bus*/
394+
spi_device = (struct rt_spi_device *)rt_malloc(sizeof(struct rt_spi_device));
395+
RT_ASSERT(spi_device != RT_NULL);
396+
397+
if(cs_pin != PIN_NONE)
398+
{
399+
/* initialize the cs pin && select the slave*/
400+
rt_pin_mode(cs_pin, PIN_MODE_OUTPUT);
401+
rt_pin_write(cs_pin, PIN_HIGH);
402+
}
403+
404+
result = rt_spi_bus_attach_device(spi_device, device_name, bus_name, (void *)cs_pin);
405+
406+
if (result != RT_EOK)
407+
{
408+
LOG_E("%s attach to %s faild, %d\n", device_name, bus_name, result);
409+
}
410+
411+
RT_ASSERT(result == RT_EOK);
412+
413+
LOG_D("%s attach to %s done", device_name, bus_name);
414+
415+
return result;
416+
}
417+
334418
int rt_hw_spi_init(void)
335419
{
336420
int result = 0;
@@ -352,5 +436,5 @@ int rt_hw_spi_init(void)
352436

353437
INIT_BOARD_EXPORT(rt_hw_spi_init);
354438

355-
#endif /* BSP_USING_SPI0 || BSP_USING_SPI1 || BSP_USING_SPI2 */
439+
#endif /* BSP_USING_SPI0 || BSP_USING_SPI1 || BSP_USING_SPI2 || BSP_USING_SPI3 || BSP_USING_SPI4*/
356440
#endif /* RT_USING_SPI */

bsp/gd32/arm/libraries/gd32_drivers/drv_spi.h

+5
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,16 @@ struct gd32_spi
3434
rcu_periph_enum gpio_clk;
3535
struct rt_spi_bus *spi_bus;
3636
uint32_t spi_port;
37+
#if defined SOC_SERIES_GD32F4xx
38+
uint32_t alt_func_num;
39+
#endif
3740
uint16_t sck_pin;
3841
uint16_t miso_pin;
3942
uint16_t mosi_pin;
4043
};
4144

45+
rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name, rt_base_t cs_pin);
46+
4247
#ifdef __cplusplus
4348
}
4449
#endif

0 commit comments

Comments
 (0)