1 /*
2  * Copyright (c) 2006-2023, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2021-08-10     charlown      first version
9  */
10 
11 #include <rtthread.h>
12 #include <rtdevice.h>
13 #include "drivers/dev_spi.h"
14 #include "board.h"
15 #include "drv_spi.h"
16 #include "ch32f10x_spi.h"
17 #include "ch32f10x_rcc.h"
18 
19 #ifdef BSP_USING_SPI
20 
21 #define LOG_TAG "drv.spi"
22 #include "drv_log.h"
23 
24 #ifndef ITEM_NUM
25 #define ITEM_NUM(items) sizeof(items) / sizeof(items[0])
26 #endif
27 
28 struct spi_bus_device
29 {
30     struct rt_spi_bus parent;
31     char *name;
32     SPI_TypeDef *periph;
33     rt_base_t cs_pin;
34     struct rt_spi_device spi_device;
35 };
36 
37 static struct spi_bus_device spi_bus_device_list[] = {
38 #ifdef BSP_USING_SPI1
39     {.periph = SPI1,
40      .name = "spi1"},
41 #endif
42 
43 #ifdef BSP_USING_SPI2
44     {.periph = SPI2,
45      .name = "spi2"},
46 #endif
47 };
48 
49 /**
50  * Attach the spi device to SPI bus, this function must be used after initialization.
51  */
rt_hw_spi_device_attach(const char * bus_name,const char * device_name,rt_uint32_t pin)52 rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name, rt_uint32_t pin)
53 {
54     rt_err_t result;
55     struct rt_spi_bus *spi_bus;
56     struct spi_bus_device *spi_bus_dev;
57 
58     RT_ASSERT(bus_name != RT_NULL);
59     RT_ASSERT(device_name != RT_NULL);
60 
61     spi_bus = (struct rt_spi_bus *)rt_device_find(bus_name);
62 
63     RT_ASSERT(spi_bus != RT_NULL);
64 
65     spi_bus_dev = (struct spi_bus_device *)spi_bus;
66 
67     spi_bus_dev->cs_pin = pin;
68 
69     //often active low, output from master
70     rt_pin_mode(spi_bus_dev->cs_pin, PIN_MODE_OUTPUT);
71     rt_pin_write(spi_bus_dev->cs_pin, PIN_HIGH);
72 
73     result = rt_spi_bus_attach_device(&spi_bus_dev->spi_device, device_name, bus_name, RT_NULL);
74 
75     if (result != RT_EOK)
76     {
77         LOG_E("%s attach to %s faild, %d\n", device_name, bus_name, result);
78     }
79 
80     LOG_D("%s attach to %s done", device_name, bus_name);
81 
82     return result;
83 }
84 
ch32f1_spi_configure(struct rt_spi_device * device,struct rt_spi_configuration * configuration)85 static rt_err_t ch32f1_spi_configure(struct rt_spi_device *device, struct rt_spi_configuration *configuration)
86 {
87     struct rt_spi_bus *spi_bus;
88     struct spi_bus_device *spi_bus_dev;
89     rt_uint32_t spi_clock;
90 
91     SPI_InitTypeDef SPI_InitStruct;
92 
93     RT_ASSERT(device != RT_NULL);
94     RT_ASSERT(configuration != RT_NULL);
95 
96     //device is not RT_NULL, so spi_bus not need check
97     spi_bus = (struct rt_spi_bus *)device->bus;
98     spi_bus_dev = (struct spi_bus_device *)spi_bus;
99 
100     ch32f1_spi_clock_and_io_init(spi_bus_dev->periph);
101 
102     spi_clock = ch32f1_spi_clock_get(spi_bus_dev->periph);
103 
104     if (configuration->data_width <= 8)
105     {
106         SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b;
107     }
108     else if (configuration->data_width <= 16)
109     {
110         SPI_InitStruct.SPI_DataSize = SPI_DataSize_16b;
111     }
112     else
113     {
114         return -RT_EIO;
115     }
116 
117     if (configuration->max_hz >= spi_clock / 2)
118     {
119         SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
120     }
121     else if (configuration->max_hz >= spi_clock / 4)
122     {
123         SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
124     }
125     else if (configuration->max_hz >= spi_clock / 8)
126     {
127         SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8;
128     }
129     else if (configuration->max_hz >= spi_clock / 16)
130     {
131         SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16;
132     }
133     else if (configuration->max_hz >= spi_clock / 32)
134     {
135         SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_32;
136     }
137     else if (configuration->max_hz >= spi_clock / 64)
138     {
139         SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64;
140     }
141     else if (configuration->max_hz >= spi_clock / 128)
142     {
143         SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_128;
144     }
145     else
146     {
147         /*  min prescaler 256 */
148         SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;
149     }
150 
151     switch (configuration->mode & RT_SPI_MODE_3)
152     {
153     case RT_SPI_MODE_0:
154         SPI_InitStruct.SPI_CPHA = SPI_CPHA_1Edge;
155         SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low;
156         break;
157     case RT_SPI_MODE_1:
158         SPI_InitStruct.SPI_CPHA = SPI_CPHA_2Edge;
159         SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low;
160         break;
161     case RT_SPI_MODE_2:
162         SPI_InitStruct.SPI_CPHA = SPI_CPHA_1Edge;
163         SPI_InitStruct.SPI_CPOL = SPI_CPOL_High;
164         break;
165     case RT_SPI_MODE_3:
166         SPI_InitStruct.SPI_CPHA = SPI_CPHA_2Edge;
167         SPI_InitStruct.SPI_CPOL = SPI_CPOL_High;
168         break;
169     }
170 
171     /* MSB or LSB */
172     if (configuration->mode & RT_SPI_MSB)
173     {
174         SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;
175     }
176     else
177     {
178         SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_LSB;
179     }
180 
181     SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
182     SPI_InitStruct.SPI_Mode = SPI_Mode_Master;
183     SPI_InitStruct.SPI_NSS = SPI_NSS_Soft;
184 
185     SPI_Init(spi_bus_dev->periph, &SPI_InitStruct);
186     /* Enable SPI_MASTER */
187     SPI_Cmd(spi_bus_dev->periph, ENABLE);
188 
189     return RT_EOK;
190 };
191 
ch32f1_spi_xfer(struct rt_spi_device * device,struct rt_spi_message * message)192 static rt_ssize_t ch32f1_spi_xfer(struct rt_spi_device *device, struct rt_spi_message *message)
193 {
194     struct rt_spi_bus *spi_bus;
195     struct spi_bus_device *spi_bus_dev;
196     struct rt_spi_configuration *config;
197 
198     RT_ASSERT(device != NULL);
199     RT_ASSERT(message != NULL);
200 
201     //device is not RT_NULL, so spi_bus not need check
202     spi_bus = (struct rt_spi_bus *)device->bus;
203     spi_bus_dev = (struct spi_bus_device *)spi_bus;
204     config = &device->config;
205 
206     /* take CS */
207     if (message->cs_take)
208     {
209         rt_pin_write(spi_bus_dev->cs_pin, PIN_LOW);
210         LOG_D("spi take cs\n");
211     }
212 
213     if (config->data_width <= 8)
214     {
215         const rt_uint8_t *send_ptr = message->send_buf;
216         rt_uint8_t *recv_ptr = message->recv_buf;
217         rt_uint32_t size = message->length;
218         rt_uint8_t data;
219 
220         LOG_D("spi poll transfer start: %d\n", size);
221 
222         while (size--)
223         {
224             data = 0xFF;
225 
226             if (send_ptr != RT_NULL)
227             {
228                 data = *send_ptr++;
229             }
230 
231             //Wait until the transmit buffer is empty
232             while (RESET == SPI_I2S_GetFlagStatus(spi_bus_dev->periph, SPI_I2S_FLAG_TXE))
233                 ;
234             // Send the byte
235             SPI_I2S_SendData(spi_bus_dev->periph, data);
236 
237             //Wait until a data is received
238             while (RESET == SPI_I2S_GetFlagStatus(spi_bus_dev->periph, SPI_I2S_FLAG_RXNE))
239                 ;
240             // Get the received data
241             data = SPI_I2S_ReceiveData(spi_bus_dev->periph);
242 
243             if (recv_ptr != RT_NULL)
244             {
245                 *recv_ptr++ = data;
246             }
247         }
248         LOG_D("spi poll transfer finsh\n");
249     }
250     else if (config->data_width <= 16)
251     {
252         const rt_uint16_t *send_ptr = message->send_buf;
253         rt_uint16_t *recv_ptr = message->recv_buf;
254         rt_uint32_t size = message->length;
255         rt_uint16_t data;
256 
257         while (size--)
258         {
259             data = 0xFF;
260 
261             if (send_ptr != RT_NULL)
262             {
263                 data = *send_ptr++;
264             }
265 
266             //Wait until the transmit buffer is empty
267             while (RESET == SPI_I2S_GetFlagStatus(spi_bus_dev->periph, SPI_I2S_FLAG_TXE))
268                 ;
269             // Send the byte
270             SPI_I2S_SendData(spi_bus_dev->periph, data);
271 
272             //Wait until a data is received
273             while (RESET == SPI_I2S_GetFlagStatus(spi_bus_dev->periph, SPI_I2S_FLAG_RXNE))
274                 ;
275             // Get the received data
276             data = SPI_I2S_ReceiveData(spi_bus_dev->periph);
277 
278             if (recv_ptr != RT_NULL)
279             {
280                 *recv_ptr++ = data;
281             }
282         }
283     }
284 
285     /* release CS */
286     if (message->cs_release)
287     {
288         rt_pin_write(spi_bus_dev->cs_pin, PIN_HIGH);
289         LOG_D("spi release cs\n");
290     }
291 
292     return message->length;
293 };
294 
295 static struct rt_spi_ops spi_ops = {
296     .configure = ch32f1_spi_configure,
297     .xfer = ch32f1_spi_xfer};
298 
rt_hw_spi_init(void)299 int rt_hw_spi_init(void)
300 {
301     int index;
302 
303     for (index = 0; index < ITEM_NUM(spi_bus_device_list); index++)
304     {
305         rt_spi_bus_register(&spi_bus_device_list[index].parent, spi_bus_device_list[index].name, &spi_ops);
306     }
307 
308     return RT_EOK;
309 }
310 
311 INIT_BOARD_EXPORT(rt_hw_spi_init);
312 
313 #endif /* BSP_USING_SPI */
314