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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
|
/*
* Copyright (c) 2006-2023, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2018-03-27 Liuguang the first version.
*/
#include <rtthread.h>
#ifdef BSP_USING_SPI
#include "drv_spi.h"
#include "fsl_common.h"
#include "fsl_iomuxc.h"
#include "fsl_lpspi.h"
#include "fsl_lpspi_edma.h"
#include "fsl_dmamux.h"
#define LOG_TAG "drv.spi"
#include <drv_log.h>
#if defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL
#error "Please don't define 'FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL'!"
#endif
enum
{
#ifdef BSP_USING_SPI1
SPI1_INDEX,
#endif
#ifdef BSP_USING_SPI2
SPI2_INDEX,
#endif
#ifdef BSP_USING_SPI3
SPI3_INDEX,
#endif
#ifdef BSP_USING_SPI4
SPI4_INDEX,
#endif
};
struct imxrt_sw_spi_cs
{
rt_uint32_t pin;
};
struct dma_config
{
lpspi_master_edma_handle_t spi_edma;
edma_handle_t rx_edma;
dma_request_source_t rx_request;
rt_uint8_t rx_channel;
edma_handle_t tx_edma;
dma_request_source_t tx_request;
rt_uint8_t tx_channel;
};
struct imxrt_spi
{
char *bus_name;
LPSPI_Type *base;
struct rt_spi_bus spi_bus;
rt_sem_t xfer_sem;
lpspi_master_handle_t spi_normal;
struct dma_config *dma;
rt_uint8_t dma_flag;
rt_uint16_t masterclock;
};
static struct imxrt_spi lpspis[] =
{
#ifdef BSP_USING_SPI1
{
.bus_name = "spi1",
.base = LPSPI1,
.dma = RT_NULL,
.dma_flag = RT_FALSE,
.masterclock = 171,
},
#endif
#ifdef BSP_USING_SPI2
{
.bus_name = "spi2",
.base = LPSPI2,
.dma = RT_NULL,
.dma_flag = RT_FALSE,
.masterclock = 172,
},
#endif
#ifdef BSP_USING_SPI3
{
.bus_name = "spi3",
.base = LPSPI3,
.dma = RT_NULL,
.dma_flag = RT_FALSE,
.masterclock = 173,
},
#endif
#ifdef BSP_USING_SPI4
{
.bus_name = "spi4",
.base = LPSPI4,
.dma = RT_NULL,
.dma_flag = RT_FALSE,
.masterclock = 174,
},
#endif
};
static void spi_get_dma_config(void)
{
#ifdef BSP_SPI1_USING_DMA
static struct dma_config spi1_dma =
{
.rx_request = kDmaRequestMuxLPSPI1Rx,
.rx_channel = BSP_SPI1_RX_DMA_CHANNEL,
.tx_request = kDmaRequestMuxLPSPI1Tx,
.tx_channel = BSP_SPI1_TX_DMA_CHANNEL,
};
lpspis[SPI1_INDEX].dma = &spi1_dma;
lpspis[SPI1_INDEX].dma_flag = RT_TRUE;
#endif
#ifdef BSP_SPI2_USING_DMA
static struct dma_config spi2_dma =
{
.rx_request = kDmaRequestMuxLPSPI2Rx,
.rx_channel = BSP_SPI2_RX_DMA_CHANNEL,
.tx_request = kDmaRequestMuxLPSPI2Tx,
.tx_channel = BSP_SPI2_TX_DMA_CHANNEL,
};
lpspis[SPI2_INDEX].dma = &spi2_dma;
lpspis[SPI2_INDEX].dma_flag = RT_TRUE;
#endif
#ifdef BSP_SPI3_USING_DMA
static struct dma_config spi3_dma =
{
.rx_request = kDmaRequestMuxLPSPI3Rx,
.rx_channel = BSP_SPI3_RX_DMA_CHANNEL,
.tx_request = kDmaRequestMuxLPSPI3Tx,
.tx_channel = BSP_SPI3_TX_DMA_CHANNEL,
};
lpspis[SPI3_INDEX].dma = &spi3_dma;
lpspis[SPI3_INDEX].dma_flag = RT_TRUE;
#endif
#ifdef BSP_SPI4_USING_DMA
static struct dma_config spi4_dma =
{
.rx_request = kDmaRequestMuxLPSPI4Rx,
.rx_channel = BSP_SPI4_RX_DMA_CHANNEL,
.tx_request = kDmaRequestMuxLPSPI4Tx,
.tx_channel = BSP_SPI4_TX_DMA_CHANNEL,
};
lpspis[SPI4_INDEX].dma = &spi4_dma;
lpspis[SPI4_INDEX].dma_flag = RT_TRUE;
#endif
}
void normal_xfer_callback(LPSPI_Type *base, lpspi_master_handle_t *handle, status_t status, void *userData)
{
/* xfer complete callback */
struct imxrt_spi *spi = (struct imxrt_spi *)userData;
rt_sem_release(spi->xfer_sem);
}
void edma_xfer_callback(LPSPI_Type *base, lpspi_master_edma_handle_t *handle, status_t status, void *userData)
{
/* xfer complete callback */
struct imxrt_spi *spi = (struct imxrt_spi *)userData;
rt_sem_release(spi->xfer_sem);
}
rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name, rt_uint32_t pin)
{
rt_err_t ret = RT_EOK;
struct rt_spi_device *spi_device = (struct rt_spi_device *)rt_malloc(sizeof(struct rt_spi_device));
RT_ASSERT(spi_device != RT_NULL);
struct imxrt_sw_spi_cs *cs_pin = (struct imxrt_sw_spi_cs *)rt_malloc(sizeof(struct imxrt_sw_spi_cs));
RT_ASSERT(cs_pin != RT_NULL);
cs_pin->pin = pin;
rt_pin_mode(pin, PIN_MODE_OUTPUT);
rt_pin_write(pin, PIN_HIGH);
ret = rt_spi_bus_attach_device(spi_device, device_name, bus_name, (void *)cs_pin);
return ret;
}
static uint32_t imxrt_get_lpspi_freq(void)
{
uint32_t freq = 0;
/* CLOCK_GetMux(kCLOCK_LpspiMux):
00b: derive clock from PLL3 PFD1 720M
01b: derive clock from PLL3 PFD0 720M
10b: derive clock from PLL2 528M
11b: derive clock from PLL2 PFD2 396M
*/
switch(CLOCK_GetMux(kCLOCK_LpspiMux))
{
case 0:
freq = CLOCK_GetFreq(kCLOCK_Usb1PllPfd1Clk);
break;
case 1:
freq = CLOCK_GetFreq(kCLOCK_Usb1PllPfd0Clk);
break;
case 2:
freq = CLOCK_GetFreq(kCLOCK_SysPllClk);
break;
case 3:
freq = CLOCK_GetFreq(kCLOCK_SysPllPfd2Clk);
break;
}
freq /= (CLOCK_GetDiv(kCLOCK_LpspiDiv) + 1U);
return freq;
}
static void lpspi_normal_config(struct imxrt_spi *spi)
{
RT_ASSERT(spi != RT_NULL);
LPSPI_MasterTransferCreateHandle(spi->base,
&spi->spi_normal,
normal_xfer_callback,
spi);
LOG_D(LOG_TAG" %s normal config done\n", spi->bus_name);
}
static void lpspi_dma_config(struct imxrt_spi *spi)
{
RT_ASSERT(spi != RT_NULL);
DMAMUX_SetSource(DMAMUX, spi->dma->rx_channel, spi->dma->rx_request);
DMAMUX_EnableChannel(DMAMUX, spi->dma->rx_channel);
EDMA_CreateHandle(&spi->dma->rx_edma, DMA0, spi->dma->rx_channel);
DMAMUX_SetSource(DMAMUX, spi->dma->tx_channel, spi->dma->tx_request);
DMAMUX_EnableChannel(DMAMUX, spi->dma->tx_channel);
EDMA_CreateHandle(&spi->dma->tx_edma, DMA0, spi->dma->tx_channel);
LPSPI_MasterTransferCreateHandleEDMA(spi->base,
&spi->dma->spi_edma,
edma_xfer_callback,
spi,
&spi->dma->rx_edma,
&spi->dma->tx_edma);
LOG_D("%s dma config done\n", spi->bus_name);
}
static rt_err_t spi_configure(struct rt_spi_device *device, struct rt_spi_configuration *cfg)
{
lpspi_master_config_t masterConfig;
struct imxrt_spi *spi = RT_NULL;
RT_ASSERT(cfg != RT_NULL);
RT_ASSERT(device != RT_NULL);
spi = (struct imxrt_spi *)(device->bus->parent.user_data);
RT_ASSERT(spi != RT_NULL);
if(cfg->data_width != 8 && cfg->data_width != 16 && cfg->data_width != 32)
{
return RT_EINVAL;
}
LPSPI_MasterGetDefaultConfig(&masterConfig);
if(cfg->max_hz > 40*1000*1000)
{
cfg->max_hz = 40*1000*1000;
}
masterConfig.baudRate = cfg->max_hz;
masterConfig.bitsPerFrame = cfg->data_width;
if(cfg->mode & RT_SPI_MSB)
{
masterConfig.direction = kLPSPI_MsbFirst;
}
else
{
masterConfig.direction = kLPSPI_LsbFirst;
}
if(cfg->mode & RT_SPI_CPHA)
{
masterConfig.cpha = kLPSPI_ClockPhaseSecondEdge;
}
else
{
masterConfig.cpha = kLPSPI_ClockPhaseFirstEdge;
}
if(cfg->mode & RT_SPI_CPOL)
{
masterConfig.cpol = kLPSPI_ClockPolarityActiveLow;
}
else
{
masterConfig.cpol = kLPSPI_ClockPolarityActiveHigh;
}
masterConfig.whichPcs = kLPSPI_Pcs0;
#if defined(SOC_IMXRT1170_SERIES)
freq = CLOCK_GetFreqFromObs(spi->masterclock, 2);
LPSPI_MasterInit(spi->base, &masterConfig, freq);
#else
masterConfig.pinCfg = kLPSPI_SdiInSdoOut;
masterConfig.pcsToSckDelayInNanoSec = 1000000000 / masterConfig.baudRate;
masterConfig.lastSckToPcsDelayInNanoSec = 1000000000 / masterConfig.baudRate;
masterConfig.betweenTransferDelayInNanoSec = 1000000000 / masterConfig.baudRate;
LPSPI_MasterInit(spi->base, &masterConfig, imxrt_get_lpspi_freq());
spi->base->CFGR1 |= LPSPI_CFGR1_PCSCFG_MASK;
#endif
return RT_EOK;
}
static rt_uint32_t spixfer(struct rt_spi_device *device, struct rt_spi_message *message)
{
lpspi_transfer_t transfer;
status_t status;
RT_ASSERT(device != RT_NULL);
RT_ASSERT(device->bus != RT_NULL);
RT_ASSERT(device->bus->parent.user_data != RT_NULL);
struct imxrt_spi *spi = (struct imxrt_spi *)(device->bus->parent.user_data);
struct imxrt_sw_spi_cs *cs = device->parent.user_data;
if(message->cs_take)
{
rt_pin_write(cs->pin, PIN_LOW);
}
transfer.dataSize = message->length;
transfer.rxData = (uint8_t *)(message->recv_buf);
transfer.txData = (uint8_t *)(message->send_buf);
transfer.configFlags =
kLPSPI_MasterPcs0 | kLPSPI_MasterByteSwap | kLPSPI_MasterPcsContinuous;
if(RT_FALSE == spi->dma_flag)
{
#ifdef BSP_USING_BLOCKING_SPI
status = LPSPI_MasterTransferBlocking(spi->base, &transfer);
#else
status = LPSPI_MasterTransferNonBlocking(spi->base, &spi->spi_normal, &transfer);
#endif
}
else
{
status = LPSPI_MasterTransferEDMA(spi->base,&spi->dma->spi_edma,&transfer);
}
rt_sem_take(spi->xfer_sem, RT_WAITING_FOREVER);
if(message->cs_release)
{
rt_pin_write(cs->pin, PIN_HIGH);
}
if (status != kStatus_Success)
{
LOG_E("%s transfer error : %d", spi->bus_name,status);
message->length = 0;
}
return message->length;
}
static struct rt_spi_ops imxrt_spi_ops =
{
.configure = spi_configure,
.xfer = spixfer
};
int rt_hw_spi_bus_init(void)
{
int i;
rt_err_t ret = RT_EOK;
spi_get_dma_config();
for (i = 0; i < sizeof(lpspis) / sizeof(lpspis[0]); i++)
{
lpspis[i].spi_bus.parent.user_data = &lpspis[i];
ret = rt_spi_bus_register(&lpspis[i].spi_bus, lpspis[i].bus_name, &imxrt_spi_ops);
if(RT_TRUE == lpspis[i].dma_flag)
{
lpspi_dma_config(&lpspis[i]);
}
else
{
lpspi_normal_config(&lpspis[i]);
}
char sem_name[RT_NAME_MAX];
rt_sprintf(sem_name, "%s_s", lpspis[i].bus_name);
lpspis[i].xfer_sem = rt_sem_create(sem_name, 0, RT_IPC_FLAG_PRIO);
}
return ret;
}
INIT_BOARD_EXPORT(rt_hw_spi_bus_init);
#endif /* BSP_USING_SPI */
|