1 /*
2 * Copyright (c) 2006-2021, RT-Thread Development Team
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Change Logs:
7 * Date Author Notes
8 * 2021-08-15 Jonas first version
9 */
10
11 #include <board.h>
12 #include "drv_spi.h"
13
14 #ifdef RT_USING_SPI
15 #if !defined(BSP_USING_SPI1) && !defined(BSP_USING_SPI2) && \
16 !defined(BSP_USING_SPI3) && !defined(BSP_USING_SPI4)
17 #error "Please define at least one SPIx"
18 #endif
19
20 //#define DEBUG
21
22 #define ARR_LEN(__N) (sizeof(__N) / sizeof(__N[0]))
23
24 #ifdef DEBUG
25 #define DEBUG_PRINTF(...) rt_kprintf(__VA_ARGS__)
26 #else
27 #define DEBUG_PRINTF(...)
28 #endif
29
30 /* private rt-thread spi ops function */
31 static rt_err_t configure(struct rt_spi_device *device, struct rt_spi_configuration *configuration);
32 static rt_uint32_t xfer(struct rt_spi_device *device, struct rt_spi_message *message);
33
34 static struct rt_spi_ops hk32_spi_ops =
35 {
36 configure,
37 xfer
38 };
39
40 /**
41 * Attach the spi device to SPI bus, this function must be used after initialization.
42 */
rt_hw_spi_device_attach(const char * bus_name,const char * device_name,GPIO_TypeDef * cs_gpiox,uint16_t cs_gpio_pin)43 rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name, GPIO_TypeDef *cs_gpiox, uint16_t cs_gpio_pin)
44 {
45 RT_ASSERT(bus_name != RT_NULL);
46 RT_ASSERT(device_name != RT_NULL);
47
48 rt_err_t result;
49 struct rt_spi_device *spi_device;
50 struct hk32_spi_cs *cs_pin;
51
52 /* initialize the cs pin && select the slave*/
53 GPIO_InitTypeDef GPIO_InitStruct;
54 GPIO_InitStruct.GPIO_Pin = cs_gpio_pin;
55 GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
56 GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
57 GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
58 GPIO_Init(cs_gpiox, &GPIO_InitStruct);
59 GPIO_SetBits(cs_gpiox, cs_gpio_pin);
60
61 /* attach the device to spi bus*/
62 spi_device = (struct rt_spi_device *)rt_malloc(sizeof(struct rt_spi_device));
63 RT_ASSERT(spi_device != RT_NULL);
64 cs_pin = (struct hk32_spi_cs *)rt_malloc(sizeof(struct hk32_spi_cs));
65 RT_ASSERT(cs_pin != RT_NULL);
66 cs_pin->GPIOx = cs_gpiox;
67 cs_pin->GPIO_Pin = cs_gpio_pin;
68 result = rt_spi_bus_attach_device(spi_device, device_name, bus_name, (void *)cs_pin);
69
70 if (result != RT_EOK)
71 {
72 DEBUG_PRINTF("%s attach to %s faild, %d\n", device_name, bus_name, result);
73 }
74
75 RT_ASSERT(result == RT_EOK);
76
77 DEBUG_PRINTF("%s attach to %s done", device_name, bus_name);
78
79 return result;
80 }
81
configure(struct rt_spi_device * device,struct rt_spi_configuration * configuration)82 static rt_err_t configure(struct rt_spi_device *device,
83 struct rt_spi_configuration *configuration)
84 {
85 struct rt_spi_bus *spi_bus = (struct rt_spi_bus *)device->bus;
86 struct hk32_spi *spi_instance = (struct hk32_spi *)spi_bus->parent.user_data;
87
88 SPI_InitTypeDef SPI_InitStruct;
89
90 RT_ASSERT(device != RT_NULL);
91 RT_ASSERT(configuration != RT_NULL);
92
93 hk32_msp_spi_init(spi_instance->config->spix);
94
95 /* data_width */
96 if (configuration->data_width <= 8)
97 {
98 SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b;
99 }
100 else if (configuration->data_width <= 16)
101 {
102 SPI_InitStruct.SPI_DataSize = SPI_DataSize_16b;
103 }
104 else
105 {
106 return -RT_EIO;
107 }
108
109 /* baudrate */
110 {
111 uint32_t spi_apb_clock;
112 uint32_t max_hz;
113 RCC_ClocksTypeDef RCC_Clocks;
114
115 max_hz = configuration->max_hz;
116
117 RCC_GetClocksFreq(&RCC_Clocks);
118 DEBUG_PRINTF("sys freq: %d\n", RCC_Clocks.SYSCLK_Freq);
119 DEBUG_PRINTF("max freq: %d\n", max_hz);
120
121 if (spi_instance->config->spix == SPI1)
122 {
123 spi_apb_clock = RCC_Clocks.PCLK_Frequency;
124 DEBUG_PRINTF("pclk freq: %d\n", RCC_Clocks.PCLK_Frequency);
125 }
126 else
127 {
128 spi_apb_clock = RCC_Clocks.PCLK_Frequency;
129 DEBUG_PRINTF("pclk1 freq: %d\n", RCC_Clocks.PCLK_Frequency);
130 }
131
132 if (max_hz >= spi_apb_clock / 2)
133 {
134 SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
135 }
136 else if (max_hz >= spi_apb_clock / 4)
137 {
138 SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
139 }
140 else if (max_hz >= spi_apb_clock / 8)
141 {
142 SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8;
143 }
144 else if (max_hz >= spi_apb_clock / 16)
145 {
146 SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16;
147 }
148 else if (max_hz >= spi_apb_clock / 32)
149 {
150 SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_32;
151 }
152 else if (max_hz >= spi_apb_clock / 64)
153 {
154 SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64;
155 }
156 else if (max_hz >= spi_apb_clock / 128)
157 {
158 SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_128;
159 }
160 else
161 {
162 /* min prescaler 256 */
163 SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;
164 }
165 } /* baudrate */
166
167 switch (configuration->mode & RT_SPI_MODE_3)
168 {
169 case RT_SPI_MODE_0:
170 SPI_InitStruct.SPI_CPHA = SPI_CPHA_1Edge;
171 SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low;
172 break;
173 case RT_SPI_MODE_1:
174 SPI_InitStruct.SPI_CPHA = SPI_CPHA_2Edge;
175 SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low;
176 break;
177 case RT_SPI_MODE_2:
178 SPI_InitStruct.SPI_CPHA = SPI_CPHA_1Edge;
179 SPI_InitStruct.SPI_CPOL = SPI_CPOL_High;
180 break;
181 case RT_SPI_MODE_3:
182 SPI_InitStruct.SPI_CPHA = SPI_CPHA_2Edge;
183 SPI_InitStruct.SPI_CPOL = SPI_CPOL_High;
184 break;
185 }
186
187 /* MSB or LSB */
188 if (configuration->mode & RT_SPI_MSB)
189 {
190 SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;
191 }
192 else
193 {
194 SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_LSB;
195 }
196
197 SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
198 SPI_InitStruct.SPI_Mode = SPI_Mode_Master;
199 SPI_InitStruct.SPI_NSS = SPI_NSS_Soft;
200
201 /* init SPI */
202 SPI_Init(spi_instance->config->spix, &SPI_InitStruct);
203 /* Enable SPI_MASTER */
204 SPI_Cmd(spi_instance->config->spix, ENABLE);
205 SPI_CalculateCRC(spi_instance->config->spix, DISABLE);
206
207 return RT_EOK;
208 };
209
xfer(struct rt_spi_device * device,struct rt_spi_message * message)210 static rt_uint32_t xfer(struct rt_spi_device *device, struct rt_spi_message *message)
211 {
212 struct rt_spi_bus *hk32_spi_bus = (struct rt_spi_bus *)device->bus;
213 struct hk32_spi *spi_instance = (struct hk32_spi *)hk32_spi_bus->parent.user_data;
214 struct rt_spi_configuration *config = &device->config;
215 struct hk32_spi_cs *hk32_spi_cs = device->parent.user_data;
216
217 RT_ASSERT(device != NULL);
218 RT_ASSERT(message != NULL);
219
220 /* take CS */
221 if (message->cs_take)
222 {
223 GPIO_ResetBits(hk32_spi_cs->GPIOx, hk32_spi_cs->GPIO_Pin);
224 DEBUG_PRINTF("spi take cs\n");
225 }
226
227 {
228 if (config->data_width <= 8)
229 {
230 const rt_uint8_t *send_ptr = message->send_buf;
231 rt_uint8_t *recv_ptr = message->recv_buf;
232 rt_uint32_t size = message->length;
233
234 DEBUG_PRINTF("spi poll transfer start: %d\n", size);
235
236 while (size--)
237 {
238 rt_uint8_t data = 0xFF;
239
240 if (send_ptr != RT_NULL)
241 {
242 data = *send_ptr++;
243 }
244
245 /* Todo: replace register read/write by hk32 lib */
246 /* Wait until the transmit buffer is empty */
247 while (RESET == SPI_I2S_GetFlagStatus(spi_instance->config->spix, SPI_I2S_FLAG_TXE));
248 /* Send the byte */
249 SPI_SendData8(spi_instance->config->spix, data);
250
251 /* Wait until a data is received */
252 while (RESET == SPI_I2S_GetFlagStatus(spi_instance->config->spix, SPI_I2S_FLAG_RXNE));
253 /* Get the received data */
254 data = SPI_ReceiveData8(spi_instance->config->spix);
255
256 if (recv_ptr != RT_NULL)
257 {
258 *recv_ptr++ = data;
259 }
260 }
261 DEBUG_PRINTF("spi poll transfer finsh\n");
262 }
263 else if (config->data_width <= 16)
264 {
265 const rt_uint16_t *send_ptr = message->send_buf;
266 rt_uint16_t *recv_ptr = message->recv_buf;
267 rt_uint32_t size = message->length;
268
269 while (size--)
270 {
271 rt_uint16_t data = 0xFF;
272
273 if (send_ptr != RT_NULL)
274 {
275 data = *send_ptr++;
276 }
277
278 /* Wait until the transmit buffer is empty */
279 while (RESET == SPI_I2S_GetFlagStatus(spi_instance->config->spix, SPI_I2S_FLAG_TXE));
280 /* Send the byte */
281 SPI_I2S_SendData16(spi_instance->config->spix, data);
282
283 /* Wait until a data is received */
284 while (RESET == SPI_I2S_GetFlagStatus(spi_instance->config->spix, SPI_I2S_FLAG_RXNE));
285 /* Get the received data */
286 data = SPI_I2S_ReceiveData16(spi_instance->config->spix);
287
288 if (recv_ptr != RT_NULL)
289 {
290 *recv_ptr++ = data;
291 }
292 }
293 }
294 }
295
296 /* release CS */
297 if (message->cs_release)
298 {
299 GPIO_SetBits(hk32_spi_cs->GPIOx, hk32_spi_cs->GPIO_Pin);
300 DEBUG_PRINTF("spi release cs\n");
301 }
302
303 return message->length;
304 };
305
306 static struct hk32_spi_config configs[] =
307 {
308 #ifdef BSP_USING_SPI1
309 {SPI1, "spi1"},
310 #endif
311
312 #ifdef BSP_USING_SPI2
313 {SPI2, "spi2"},
314 #endif
315
316 #ifdef BSP_USING_SPI3
317 {SPI3, "spi3"},
318 #endif
319
320 #ifdef BSP_USING_SPI4
321 {SPI4, "spi4"},
322 #endif
323 };
324
325 static struct hk32_spi spis[sizeof(configs) / sizeof(configs[0])] = {0};
326
rt_hw_spi_init(void)327 int rt_hw_spi_init(void)
328 {
329 int i;
330 rt_err_t result;
331 rt_size_t obj_num = sizeof(spis) / sizeof(struct hk32_spi);
332
333 for (i = 0; i < obj_num; i++)
334 {
335 spis[i].config = &configs[i];
336 spis[i].spi_bus.parent.user_data = (void *)&spis[i];
337 result = rt_spi_bus_register(&(spis[i].spi_bus), spis[i].config->spi_name, &hk32_spi_ops);
338 }
339
340 return result;
341 }
342
343 INIT_BOARD_EXPORT(rt_hw_spi_init);
344
345 #endif
346