11
11
12
12
#ifdef RT_USING_SPI
13
13
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 )
15
15
#define LOG_TAG "drv.spi"
16
16
17
17
#include <rtdbg.h>
@@ -25,6 +25,12 @@ static struct rt_spi_bus spi_bus1;
25
25
#ifdef BSP_USING_SPI2
26
26
static struct rt_spi_bus spi_bus2 ;
27
27
#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
28
34
29
35
static const struct gd32_spi spi_bus_obj [] = {
30
36
@@ -36,10 +42,13 @@ static const struct gd32_spi spi_bus_obj[] = {
36
42
RCU_GPIOA ,
37
43
& spi_bus0 ,
38
44
GPIOA ,
45
+ #if defined SOC_SERIES_GD32F4xx
46
+ GPIO_AF_5 ,
47
+ #endif
39
48
GPIO_PIN_5 ,
40
49
GPIO_PIN_6 ,
41
50
GPIO_PIN_7 ,
42
- }
51
+ },
43
52
#endif /* BSP_USING_SPI0 */
44
53
45
54
#ifdef BSP_USING_SPI1
@@ -50,10 +59,13 @@ static const struct gd32_spi spi_bus_obj[] = {
50
59
RCU_GPIOB ,
51
60
& spi_bus1 ,
52
61
GPIOB ,
62
+ #if defined SOC_SERIES_GD32F4xx
63
+ GPIO_AF_5 ,
64
+ #endif
53
65
GPIO_PIN_12 ,
54
66
GPIO_PIN_14 ,
55
67
GPIO_PIN_15 ,
56
- }
68
+ },
57
69
#endif /* BSP_USING_SPI1 */
58
70
59
71
#ifdef BSP_USING_SPI2
@@ -64,11 +76,48 @@ static const struct gd32_spi spi_bus_obj[] = {
64
76
RCU_GPIOB ,
65
77
& spi_bus2 ,
66
78
GPIOB ,
79
+ #if defined SOC_SERIES_GD32F4xx
80
+ GPIO_AF_6 ,
81
+ #endif
67
82
GPIO_PIN_3 ,
68
83
GPIO_PIN_4 ,
69
84
GPIO_PIN_5 ,
70
- }
85
+ },
71
86
#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 */
72
121
};
73
122
74
123
/* private rt-thread spi ops function */
@@ -94,11 +143,10 @@ static void gd32_spi_init(struct gd32_spi *gd32_spi)
94
143
95
144
#if defined SOC_SERIES_GD32F4xx
96
145
/*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 );
98
147
99
148
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 );
100
149
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
-
102
150
#else
103
151
/* Init SPI SCK MOSI */
104
152
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*
331
379
return message -> length ;
332
380
};
333
381
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
+
334
418
int rt_hw_spi_init (void )
335
419
{
336
420
int result = 0 ;
@@ -352,5 +436,5 @@ int rt_hw_spi_init(void)
352
436
353
437
INIT_BOARD_EXPORT (rt_hw_spi_init );
354
438
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 */
356
440
#endif /* RT_USING_SPI */
0 commit comments