change to use non blocking api and use semaphore to sync the transfer

This commit is contained in:
LeeChunHei 2021-02-14 12:15:20 +08:00
parent a4dd106d49
commit 7e5034c67d
1 changed files with 31 additions and 1 deletions

View File

@ -64,6 +64,8 @@ struct imxrt_spi
char *bus_name; char *bus_name;
LPSPI_Type *base; LPSPI_Type *base;
struct rt_spi_bus spi_bus; struct rt_spi_bus spi_bus;
rt_sem_t xfer_sem;
lpspi_master_handle_t spi_normal;
struct dma_config *dma; struct dma_config *dma;
rt_uint8_t dma_flag; rt_uint8_t dma_flag;
}; };
@ -159,9 +161,18 @@ static void spi_get_dma_config(void)
#endif #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) void edma_xfer_callback(LPSPI_Type *base, lpspi_master_edma_handle_t *handle, status_t status, void *userData)
{ {
/* xfer complete callback */ /* 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 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)
return freq; 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) static void lpspi_dma_config(struct imxrt_spi *spi)
{ {
RT_ASSERT(spi != RT_NULL); RT_ASSERT(spi != RT_NULL);
@ -325,12 +347,13 @@ static rt_uint32_t spixfer(struct rt_spi_device *device, struct rt_spi_message *
if(RT_FALSE == spi->dma_flag) if(RT_FALSE == spi->dma_flag)
{ {
status = LPSPI_MasterTransferBlocking(spi->base, &transfer); status = LPSPI_MasterTransferNonBlocking(spi->base, &spi->spi_normal, &transfer);
} }
else else
{ {
status = LPSPI_MasterTransferEDMA(spi->base,&spi->dma->spi_edma,&transfer); status = LPSPI_MasterTransferEDMA(spi->base,&spi->dma->spi_edma,&transfer);
} }
rt_sem_take(spi->xfer_sem, RT_WAITING_FOREVER);
if(message->cs_release) if(message->cs_release)
{ {
@ -369,6 +392,13 @@ int rt_hw_spi_bus_init(void)
{ {
lpspi_dma_config(&lpspis[i]); 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; return ret;