Skip to content

Commit 5a53b08

Browse files
authored
Merge pull request #4336 from LeeChunHei/spi_test
對imxrt的SPI傳輸改用非阻塞API
2 parents 0f28841 + 6f0b8a5 commit 5a53b08

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

bsp/imxrt/libraries/drivers/drv_spi.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ struct imxrt_spi
6464
char *bus_name;
6565
LPSPI_Type *base;
6666
struct rt_spi_bus spi_bus;
67+
rt_sem_t xfer_sem;
68+
lpspi_master_handle_t spi_normal;
6769
struct dma_config *dma;
6870
rt_uint8_t dma_flag;
6971
};
@@ -159,9 +161,18 @@ static void spi_get_dma_config(void)
159161
#endif
160162
}
161163

164+
void normal_xfer_callback(LPSPI_Type *base, lpspi_master_handle_t *handle, status_t status, void *userData)
165+
{
166+
/* xfer complete callback */
167+
struct imxrt_spi *spi = (struct imxrt_spi *)userData;
168+
rt_sem_release(spi->xfer_sem);
169+
}
170+
162171
void edma_xfer_callback(LPSPI_Type *base, lpspi_master_edma_handle_t *handle, status_t status, void *userData)
163172
{
164173
/* xfer complete callback */
174+
struct imxrt_spi *spi = (struct imxrt_spi *)userData;
175+
rt_sem_release(spi->xfer_sem);
165176
}
166177

167178
rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name, rt_uint32_t pin)
@@ -217,6 +228,17 @@ static uint32_t imxrt_get_lpspi_freq(void)
217228
return freq;
218229
}
219230

231+
static void lpspi_normal_config(struct imxrt_spi *spi)
232+
{
233+
RT_ASSERT(spi != RT_NULL);
234+
235+
LPSPI_MasterTransferCreateHandle(spi->base,
236+
&spi->spi_normal,
237+
normal_xfer_callback,
238+
spi);
239+
LOG_D(LOG_TAG" %s normal config done\n", spi->bus_name);
240+
}
241+
220242
static void lpspi_dma_config(struct imxrt_spi *spi)
221243
{
222244
RT_ASSERT(spi != RT_NULL);
@@ -325,12 +347,17 @@ static rt_uint32_t spixfer(struct rt_spi_device *device, struct rt_spi_message *
325347

326348
if(RT_FALSE == spi->dma_flag)
327349
{
350+
#ifdef BSP_USING_BLOCKING_SPI
328351
status = LPSPI_MasterTransferBlocking(spi->base, &transfer);
352+
#else
353+
status = LPSPI_MasterTransferNonBlocking(spi->base, &spi->spi_normal, &transfer);
354+
#endif
329355
}
330356
else
331357
{
332358
status = LPSPI_MasterTransferEDMA(spi->base,&spi->dma->spi_edma,&transfer);
333359
}
360+
rt_sem_take(spi->xfer_sem, RT_WAITING_FOREVER);
334361

335362
if(message->cs_release)
336363
{
@@ -369,6 +396,13 @@ int rt_hw_spi_bus_init(void)
369396
{
370397
lpspi_dma_config(&lpspis[i]);
371398
}
399+
else
400+
{
401+
lpspi_normal_config(&lpspis[i]);
402+
}
403+
char sem_name[RT_NAME_MAX];
404+
rt_sprintf(sem_name, "%s_s", lpspis[i].bus_name);
405+
lpspis[i].xfer_sem = rt_sem_create(sem_name, 0, RT_IPC_FLAG_PRIO);
372406
}
373407

374408
return ret;

0 commit comments

Comments
 (0)