rt-thread/bsp/lpc55sxx/Libraries/drivers/drv_spi.c

290 lines
7.1 KiB
C
Raw Normal View History

2019-10-24 17:56:09 +08:00
/*
2023-01-06 16:40:35 +08:00
* Copyright (c) 2006-2023, RT-Thread Development Team
2019-10-24 17:56:09 +08:00
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2019-07-15 Magicoe The first version for LPC55S6x
*/
#include "rtdevice.h"
2019-10-24 17:56:09 +08:00
2021-03-17 02:26:35 +08:00
#include "fsl_common.h"
2019-10-24 17:56:09 +08:00
#include "fsl_iocon.h"
#include "fsl_spi.h"
#include "fsl_spi_dma.h"
2019-10-24 17:56:09 +08:00
enum
{
#ifdef BSP_USING_SPI3
SPI3_INDEX,
#endif
#ifdef BSP_USING_SPI8
SPI8_INDEX,
2019-10-24 17:56:09 +08:00
#endif
};
2019-10-24 17:56:09 +08:00
struct lpc_spi
{
struct rt_spi_bus parent;
SPI_Type *SPIx;
clock_attach_id_t clock_attach_id;
clock_ip_name_t clock_name;
DMA_Type *DMAx;
uint8_t tx_dma_chl;
uint8_t rx_dma_chl;
dma_handle_t dma_tx_handle;
dma_handle_t dma_rx_handle;
spi_dma_handle_t spi_dma_handle;
rt_sem_t sem;
char *device_name;
2019-10-24 17:56:09 +08:00
};
static struct lpc_spi lpc_obj[] =
2019-10-24 17:56:09 +08:00
{
#ifdef BSP_USING_SPI3
{
.SPIx = SPI3,
.clock_attach_id = kMAIN_CLK_to_FLEXCOMM3,
.clock_name = kCLOCK_FlexComm3,
.device_name = "spi3",
2021-03-17 02:26:35 +08:00
.DMAx = DMA0,
.tx_dma_chl = 9,
.rx_dma_chl = 8,
2021-03-17 02:26:35 +08:00
},
2019-10-24 17:56:09 +08:00
#endif
#ifdef BSP_USING_SPI8
{
.SPIx = SPI8,
.clock_attach_id = kMAIN_CLK_to_HSLSPI,
.clock_name = kCLOCK_Hs_Lspi,
.device_name = "spi8",
2019-10-24 17:56:09 +08:00
.DMAx = DMA0,
.tx_dma_chl = 3,
.rx_dma_chl = 2,
2019-10-24 17:56:09 +08:00
},
2019-10-24 17:56:09 +08:00
#endif
};
2021-03-17 02:26:35 +08:00
2019-10-24 17:56:09 +08:00
struct lpc_sw_spi_cs
{
rt_uint32_t pin;
};
2019-10-24 17:56:09 +08:00
static uint32_t lpc_get_spi_freq(SPI_Type *base)
{
uint32_t freq = 0;
if(base == SPI3)
2019-10-24 17:56:09 +08:00
{
freq = CLOCK_GetFlexCommClkFreq(kCLOCK_FlexComm3);
2019-10-24 17:56:09 +08:00
}
if(base == SPI8)
{
freq = CLOCK_GetFlexCommClkFreq(kCLOCK_Hs_Lspi);
2019-10-24 17:56:09 +08:00
}
return freq;
}
static rt_err_t lpc_spi_init(SPI_Type *base, struct rt_spi_configuration *cfg)
{
spi_master_config_t masterConfig = {0};
2021-03-17 02:26:35 +08:00
SPI_MasterGetDefaultConfig(&masterConfig);
2019-10-24 17:56:09 +08:00
if(cfg->data_width != 8 && cfg->data_width != 16)
2019-10-24 17:56:09 +08:00
{
cfg->data_width = 8;
2019-10-24 17:56:09 +08:00
}
2021-03-17 02:26:35 +08:00
2019-10-24 17:56:09 +08:00
masterConfig.baudRate_Bps = cfg->max_hz;
if(cfg->data_width == 8)
{
2021-03-17 02:26:35 +08:00
masterConfig.dataWidth = kSPI_Data8Bits;
2019-10-24 17:56:09 +08:00
}
else if(cfg->data_width == 16)
{
2021-03-17 02:26:35 +08:00
masterConfig.dataWidth = kSPI_Data16Bits;
2019-10-24 17:56:09 +08:00
}
if(cfg->mode & RT_SPI_MSB)
{
2021-03-17 02:26:35 +08:00
masterConfig.direction = kSPI_MsbFirst;
2019-10-24 17:56:09 +08:00
}
else
{
2021-03-17 02:26:35 +08:00
masterConfig.direction = kSPI_LsbFirst;
2019-10-24 17:56:09 +08:00
}
if(cfg->mode & RT_SPI_CPHA)
{
2021-03-17 02:26:35 +08:00
masterConfig.phase = kSPI_ClockPhaseSecondEdge;
2019-10-24 17:56:09 +08:00
}
else
{
2021-03-17 02:26:35 +08:00
masterConfig.phase = kSPI_ClockPhaseFirstEdge;
2019-10-24 17:56:09 +08:00
}
if(cfg->mode & RT_SPI_CPOL)
{
2021-03-17 02:26:35 +08:00
masterConfig.polarity = kSPI_ClockPolarityActiveLow;
2019-10-24 17:56:09 +08:00
}
else
{
2021-03-17 02:26:35 +08:00
masterConfig.polarity = kSPI_ClockPolarityActiveHigh;
2019-10-24 17:56:09 +08:00
}
2021-03-17 02:26:35 +08:00
SPI_MasterInit(base, &masterConfig, lpc_get_spi_freq(base));
2019-10-24 17:56:09 +08:00
2021-03-17 02:26:35 +08:00
return RT_EOK;
2019-10-24 17:56:09 +08:00
}
rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name, rt_uint32_t pin)
2019-10-24 17:56:09 +08:00
{
2021-03-17 02:26:35 +08:00
rt_err_t ret = RT_EOK;
struct rt_spi_device *spi_device = (struct rt_spi_device *)rt_malloc(sizeof(struct rt_spi_device));
struct lpc_sw_spi_cs *cs_pin = (struct lpc_sw_spi_cs *)rt_malloc(sizeof(struct lpc_sw_spi_cs));
2021-03-17 02:26:35 +08:00
cs_pin->pin = pin;
2021-03-17 02:26:35 +08:00
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);
2021-03-17 02:26:35 +08:00
return ret;
2019-10-24 17:56:09 +08:00
}
2019-10-24 17:56:09 +08:00
static rt_err_t spi_configure(struct rt_spi_device *device, struct rt_spi_configuration *cfg)
{
2021-03-17 02:26:35 +08:00
rt_err_t ret = RT_EOK;
struct lpc_spi *spi = RT_NULL;
spi = (struct lpc_spi *)(device->bus->parent.user_data);
ret = lpc_spi_init(spi->SPIx, cfg);
2021-03-17 02:26:35 +08:00
2019-10-24 17:56:09 +08:00
return ret;
}
static void SPI_MasterUserCallback(SPI_Type *base, spi_dma_handle_t *handle, status_t status, void *userData)
{
struct lpc_spi *spi = (struct lpc_spi*)userData;
rt_sem_release(spi->sem);
}
static rt_ssize_t spixfer(struct rt_spi_device *device, struct rt_spi_message *message)
2019-10-24 17:56:09 +08:00
{
int i;
spi_transfer_t transfer = {0};
2021-03-17 02:26:35 +08:00
2019-10-24 17:56:09 +08:00
RT_ASSERT(device != RT_NULL);
RT_ASSERT(device->bus != RT_NULL);
RT_ASSERT(device->bus->parent.user_data != RT_NULL);
2021-03-17 02:26:35 +08:00
2021-03-17 02:26:35 +08:00
struct lpc_spi *spi = (struct lpc_spi *)(device->bus->parent.user_data);
struct lpc_sw_spi_cs *cs = device->parent.user_data;
2021-03-17 02:26:35 +08:00
2019-10-24 17:56:09 +08:00
if(message->cs_take)
{
rt_pin_write(cs->pin, PIN_LOW);
2019-10-24 17:56:09 +08:00
}
2021-03-17 02:26:35 +08:00
transfer.dataSize = message->length;
transfer.rxData = (uint8_t *)(message->recv_buf);
transfer.txData = (uint8_t *)(message->send_buf);
transfer.configFlags = kSPI_FrameAssert;
2019-10-24 17:56:09 +08:00
// if(message->length < MAX_DMA_TRANSFER_SIZE)
if(0)
2019-10-24 17:56:09 +08:00
{
SPI_MasterTransferBlocking(spi->SPIx, &transfer);
2021-03-17 02:26:35 +08:00
}
else
2019-10-24 17:56:09 +08:00
{
uint32_t block, remain;
block = message->length / DMA_MAX_TRANSFER_COUNT;
remain = message->length % DMA_MAX_TRANSFER_COUNT;
2021-03-17 02:26:35 +08:00
for(i=0; i<block; i++)
{
transfer.dataSize = DMA_MAX_TRANSFER_COUNT;
if(message->recv_buf) transfer.rxData = (uint8_t *)(message->recv_buf + i*DMA_MAX_TRANSFER_COUNT);
if(message->send_buf) transfer.txData = (uint8_t *)(message->send_buf + i*DMA_MAX_TRANSFER_COUNT);
2019-10-24 17:56:09 +08:00
SPI_MasterTransferDMA(spi->SPIx, &spi->spi_dma_handle, &transfer);
rt_sem_take(spi->sem, RT_WAITING_FOREVER);
}
2019-10-24 17:56:09 +08:00
if(remain)
{
transfer.dataSize = remain;
if(message->recv_buf) transfer.rxData = (uint8_t *)(message->recv_buf + i*DMA_MAX_TRANSFER_COUNT);
if(message->send_buf) transfer.txData = (uint8_t *)(message->send_buf + i*DMA_MAX_TRANSFER_COUNT);
2019-10-24 17:56:09 +08:00
SPI_MasterTransferDMA(spi->SPIx, &spi->spi_dma_handle, &transfer);
rt_sem_take(spi->sem, RT_WAITING_FOREVER);
}
}
2019-10-24 17:56:09 +08:00
if(message->cs_release)
{
rt_pin_write(cs->pin, PIN_HIGH);
}
2019-10-24 17:56:09 +08:00
return message->length;
}
2019-10-24 17:56:09 +08:00
2021-03-17 02:26:35 +08:00
static struct rt_spi_ops lpc_spi_ops =
2019-10-24 17:56:09 +08:00
{
2021-03-17 02:26:35 +08:00
.configure = spi_configure,
2019-10-24 17:56:09 +08:00
.xfer = spixfer
2021-03-17 02:26:35 +08:00
};
2019-10-24 17:56:09 +08:00
int rt_hw_spi_init(void)
{
int i;
2019-10-24 17:56:09 +08:00
for(i=0; i<ARRAY_SIZE(lpc_obj); i++)
{
CLOCK_AttachClk(lpc_obj[i].clock_attach_id);
lpc_obj[i].parent.parent.user_data = &lpc_obj[i];
lpc_obj[i].sem = rt_sem_create("sem_spi", 0, RT_IPC_FLAG_FIFO);
DMA_EnableChannel(lpc_obj[i].DMAx, lpc_obj[i].tx_dma_chl);
DMA_EnableChannel(lpc_obj[i].DMAx, lpc_obj[i].rx_dma_chl);
DMA_SetChannelPriority(lpc_obj[i].DMAx, lpc_obj[i].tx_dma_chl, kDMA_ChannelPriority3);
DMA_SetChannelPriority(lpc_obj[i].DMAx, lpc_obj[i].rx_dma_chl, kDMA_ChannelPriority2);
DMA_CreateHandle(&lpc_obj[i].dma_tx_handle, lpc_obj[i].DMAx, lpc_obj[i].tx_dma_chl);
DMA_CreateHandle(&lpc_obj[i].dma_rx_handle, lpc_obj[i].DMAx, lpc_obj[i].rx_dma_chl);
SPI_MasterTransferCreateHandleDMA(lpc_obj[i].SPIx, &lpc_obj[i].spi_dma_handle, SPI_MasterUserCallback, &lpc_obj[i], &lpc_obj[i].dma_tx_handle, &lpc_obj[i].dma_rx_handle);
rt_spi_bus_register(&lpc_obj[i].parent, lpc_obj[i].device_name, &lpc_spi_ops);
}
2021-03-17 02:26:35 +08:00
return RT_EOK;
2019-10-24 17:56:09 +08:00
}
INIT_DEVICE_EXPORT(rt_hw_spi_init);
2019-10-24 17:56:09 +08:00