1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
|
/*
* Copyright (c) 2006-2024, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2024-04-08 QT-one first version
*/
#include <rtdbg.h>
#include "drv_spi.h"
#ifdef RT_USING_SPI
#if !defined(BSP_USING_SPI0) && !defined(BSP_USING_SPI1)
#error "Please define at least one BSP_USING_SPIx"
#endif
struct ht32_spi_config
{
HT_SPI_TypeDef *spi_x;
const char *spi_name;
IRQn_Type irq;
};
struct ht32_spi
{
struct ht32_spi_config *config;
struct rt_spi_bus spi_bus;
};
struct ht32_spi_cs
{
HT_GPIO_TypeDef *gpio_x;
uint32_t gpio_pin;
};
enum
{
#ifdef BSP_USING_SPI0
SPI0_INDEX,
#endif
#ifdef BSP_USING_SPI1
SPI1_INDEX,
#endif
};
static struct ht32_spi_config spi_config[] =
{
#ifdef BSP_USING_SPI0
{
.spi_x = HT_SPI0,
.spi_name = BSP_USING_SPI0_NAME,
.irq = SPI0_IRQn
},
#endif
#ifdef BSP_USING_SPI1
{
.spi_x = HT_SPI1,
.spi_name = BSP_USING_SPI1_NAME,
.irq = SPI1_IRQn
},
#endif
};
static struct ht32_spi spis[sizeof(spi_config) / sizeof(spi_config[0])] = {0};
/* attach the spi device to spi bus, this function must be used after initialization */
rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name, HT_GPIO_TypeDef *cs_gpiox, uint16_t cs_gpio_pin)
{
CKCU_PeripClockConfig_TypeDef CKCUClock = {{0}};
RT_ASSERT(bus_name != RT_NULL);
RT_ASSERT(device_name != RT_NULL);
rt_err_t result;
struct rt_spi_device *spi_device;
struct ht32_spi_cs *cs_pin;
if ((cs_gpiox) == HT_GPIOA)
{
CKCUClock.Bit.PA = 1;
CKCU_PeripClockConfig(CKCUClock, ENABLE);
AFIO_GPxConfig(GPIO_PA, cs_gpio_pin, AFIO_FUN_GPIO);
}
else if ((cs_gpiox) == HT_GPIOB)
{
CKCUClock.Bit.PB = 1;
CKCU_PeripClockConfig(CKCUClock, ENABLE);
AFIO_GPxConfig(GPIO_PB, cs_gpio_pin, AFIO_FUN_GPIO);
}
#if defined(HT_GPIOC)
else if ((cs_gpiox) == HT_GPIOC)
{
CKCUClock.Bit.PC = 1;
CKCU_PeripClockConfig(CKCUClock, ENABLE);
AFIO_GPxConfig(GPIO_PC, cs_gpio_pin, AFIO_FUN_GPIO);
}
#endif
#if defined(HT_GPIOD)
else if ((cs_gpiox) == HT_GPIOD)
{
CKCUClock.Bit.PD = 1;
CKCU_PeripClockConfig(CKCUClock, ENABLE);
AFIO_GPxConfig(GPIO_PD, cs_gpio_pin, AFIO_FUN_GPIO);
}
#endif
#if defined(HT_GPIOE)
else if ((cs_gpiox) == HT_GPIOE)
{
CKCUClock.Bit.PE = 1;
CKCU_PeripClockConfig(CKCUClock, ENABLE);
AFIO_GPxConfig(GPIO_PE, cs_gpio_pin, AFIO_FUN_GPIO);
}
#endif
#if defined(HT_GPIOF)
else if ((cs_gpiox) == HT_GPIOF)
{
CKCUClock.Bit.PF = 1;
CKCU_PeripClockConfig(CKCUClock, ENABLE);
AFIO_GPxConfig(GPIO_PF, cs_gpio_pin, AFIO_FUN_GPIO);
}
#endif
GPIO_PullResistorConfig(cs_gpiox, cs_gpio_pin, GPIO_PR_DISABLE);
GPIO_WriteOutBits(cs_gpiox, cs_gpio_pin, SET);
GPIO_DirectionConfig(cs_gpiox, cs_gpio_pin, GPIO_DIR_OUT);
/* attach the device to spi bus */
spi_device = (struct rt_spi_device *)rt_malloc(sizeof(struct rt_spi_device));
RT_ASSERT(spi_device != RT_NULL);
cs_pin = (struct ht32_spi_cs *)rt_malloc(sizeof(struct ht32_spi_cs));
RT_ASSERT(cs_pin != RT_NULL);
cs_pin->gpio_x = cs_gpiox;
cs_pin->gpio_pin = cs_gpio_pin;
result = rt_spi_bus_attach_device(spi_device, device_name, bus_name, (void *)cs_pin);
if (result != RT_EOK)
{
LOG_D("%s attach to %s faild, %d\n", device_name, bus_name, result);
}
RT_ASSERT(result == RT_EOK);
LOG_D("%s attach to %s done", device_name, bus_name);
return result;
}
static rt_err_t ht32_configure(struct rt_spi_device *device, struct rt_spi_configuration *configuration)
{
struct rt_spi_bus *spi_bus = (struct rt_spi_bus *)device->bus;
struct ht32_spi *spi_instance = (struct ht32_spi *)spi_bus->parent.user_data;
SPI_InitTypeDef SPI_InitStructure;
CKCU_PeripClockConfig_TypeDef CKCUClock = {{0}};
RT_ASSERT(device != RT_NULL);
RT_ASSERT(configuration != RT_NULL);
#ifdef BSP_USING_SPI0
if (HT_SPI0 == spi_instance->config->spi_x)
{
CKCUClock.Bit.SPI0 = 1;
}
#endif
#ifdef BSP_USING_SPI1
if (HT_SPI1 == spi_instance->config->spi_x)
{
CKCUClock.Bit.SPI1 = 1;
}
#endif
CKCUClock.Bit.AFIO = 1;
CKCU_PeripClockConfig(CKCUClock, ENABLE);
ht32_spi_gpio_init(spi_instance->config->spi_x);
/* data_width */
if (configuration->data_width <= 8)
{
SPI_InitStructure.SPI_DataLength = SPI_DATALENGTH_8;
}
else if (configuration->data_width <= 16)
{
SPI_InitStructure.SPI_DataLength = SPI_DATALENGTH_16;
}
else
{
return -RT_ERROR;
}
/* Set the polarity and phase of the SPI */
switch (configuration->mode & RT_SPI_MODE_3)
{
case RT_SPI_MODE_0:
SPI_InitStructure.SPI_CPOL = SPI_CPOL_LOW;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_FIRST;
break;
case RT_SPI_MODE_1:
SPI_InitStructure.SPI_CPOL = SPI_CPOL_LOW;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_SECOND;
break;
case RT_SPI_MODE_2:
SPI_InitStructure.SPI_CPOL = SPI_CPOL_HIGH;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_FIRST;
break;
case RT_SPI_MODE_3:
SPI_InitStructure.SPI_CPOL = SPI_CPOL_HIGH;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_SECOND;
break;
}
/* Set the SPI as a master or slave */
SPI_InitStructure.SPI_Mode = (configuration->mode & RT_SPI_SLAVE) ? (SPI_SLAVE) : (SPI_MASTER);
/* Set the data high or low first */
SPI_InitStructure.SPI_FirstBit = (configuration->mode & RT_SPI_MSB) ? (SPI_FIRSTBIT_MSB) : (SPI_FIRSTBIT_LSB);
/* SEL uses software by default */
SPI_InitStructure.SPI_SELMode = SPI_SEL_SOFTWARE;
/* SEL effective level */
SPI_InitStructure.SPI_SELPolarity = (configuration->mode & RT_SPI_CS_HIGH) ? (SPI_SELPOLARITY_HIGH) : (SPI_SELPOLARITY_LOW);
/* Configure the SCK clock frequency of the SPI */
if (configuration->max_hz < 0xFFFF)
{
SPI_InitStructure.SPI_ClockPrescaler = ((configuration->max_hz) & 0xFFFF);
}
else
{
return -RT_ERROR;
}
SPI_InitStructure.SPI_FIFO = SPI_FIFO_DISABLE;
SPI_InitStructure.SPI_RxFIFOTriggerLevel = 0;
SPI_InitStructure.SPI_TxFIFOTriggerLevel = 0;
SPI_Init(spi_instance->config->spi_x, &SPI_InitStructure);
#if (!LIBCFG_SPI_NO_MULTI_MASTER)
SPI_SELOutputCmd(spi_instance->config->spi_x, ENABLE);
#endif
SPI_Cmd(spi_instance->config->spi_x, ENABLE);
return RT_EOK;
}
static rt_ssize_t ht32_xfer(struct rt_spi_device *device, struct rt_spi_message *message)
{
struct rt_spi_bus *ht32_spi_bus = (struct rt_spi_bus *)device->bus;
struct ht32_spi *spi_instance = (struct ht32_spi *)ht32_spi_bus->parent.user_data;
struct rt_spi_configuration *config = &device->config;
struct ht32_spi_cs *ht32_spi_cs = device->parent.user_data;
RT_ASSERT(device != NULL);
RT_ASSERT(message != NULL);
/* take cs */
if (message->cs_take)
{
GPIO_ClearOutBits(ht32_spi_cs->gpio_x, ht32_spi_cs->gpio_pin);
LOG_D("spi take cs\n");
}
if (config->data_width <= 8)
{
const rt_uint8_t *send_ptr = message->send_buf;
rt_uint8_t *recv_ptr = message->recv_buf;
rt_uint32_t size = message->length;
LOG_D("spi poll transfer start: %d\n", size);
while (size--)
{
rt_uint8_t data = 0xFF;
if (send_ptr != RT_NULL)
{
data = *send_ptr++;
}
/* wait until the transmit buffer is empty */
while (SPI_GetFlagStatus(spi_instance->config->spi_x, SPI_FLAG_TXE) == RESET);
/* send the byte */
SPI_SendData(spi_instance->config->spi_x, data);
/* wait until a data is received */
while (SPI_GetFlagStatus(spi_instance->config->spi_x, SPI_INT_RXBNE) == RESET);
/* get the received data */
data = SPI_ReceiveData(spi_instance->config->spi_x);
if (recv_ptr != RT_NULL)
{
*recv_ptr++ = data;
}
}
LOG_D("spi poll transfer finsh\n");
}
else if (config->data_width <= 16)
{
const rt_uint16_t *send_ptr = message->send_buf;
rt_uint16_t *recv_ptr = message->recv_buf;
rt_uint32_t size = message->length;
while (size--)
{
rt_uint16_t data = 0xFF;
if (send_ptr != RT_NULL)
{
data = *send_ptr++;
}
/* wait until the transmit buffer is empty */
while (SPI_GetFlagStatus(spi_instance->config->spi_x, SPI_FLAG_TXE) == RESET);
/* send the byte */
SPI_SendData(spi_instance->config->spi_x, data);
/* wait until a data is received */
while (SPI_GetFlagStatus(spi_instance->config->spi_x, SPI_INT_RXBNE) == RESET);
/* get the received data */
data = SPI_ReceiveData(spi_instance->config->spi_x);
if (recv_ptr != RT_NULL)
{
*recv_ptr++ = data;
}
}
}
/* release cs */
if (message->cs_release)
{
GPIO_SetOutBits(ht32_spi_cs->gpio_x, ht32_spi_cs->gpio_pin);
LOG_D("spi release cs\n");
}
return message->length;
}
static struct rt_spi_ops ht32_spi_ops =
{
.configure = ht32_configure,
.xfer = ht32_xfer
};
int rt_hw_spi_init(void)
{
int i;
rt_err_t result;
rt_size_t obj_num = sizeof(spis) / sizeof(struct ht32_spi);
for (i = 0; i < obj_num; i++)
{
spis[i].config = &spi_config[i];
spis[i].spi_bus.parent.user_data = (void *)&spis[i];
result = rt_spi_bus_register(&spis[i].spi_bus, spis[i].config->spi_name, &ht32_spi_ops);
}
return result;
}
INIT_BOARD_EXPORT(rt_hw_spi_init);
#endif /* RT_USING_SPI */
|