bsp: nxp/mcx: Use standard SPI CS pin. (#9312)

* bsp: nxp/mcx/mcxn: SPI: Added missing bracket and re-format code.

The closing bracket is missing from SPI6 struct.
Introduced since commit fc1cfd6d36 .

Use standard rt_spi_device->cs_pin instead of user_data pointer.

---------

Signed-off-by: Yilin Sun <imi415@imi.moe>
This commit is contained in:
Yilin Sun 2024-08-16 16:47:16 +08:00 committed by GitHub
parent 9822b46291
commit 0864456947
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 83 additions and 115 deletions

View File

@ -60,25 +60,15 @@ static struct lpc_spi lpc_obj[] =
#endif #endif
}; };
struct lpc_sw_spi_cs
{
rt_uint32_t pin;
};
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)
{ {
rt_err_t ret = RT_EOK; struct rt_spi_device *spi_device = rt_malloc(sizeof(struct rt_spi_device));
if (!spi_device)
{
return -RT_ENOMEM;
}
struct rt_spi_device *spi_device = (struct rt_spi_device *)rt_malloc(sizeof(struct rt_spi_device)); return rt_spi_bus_attach_device_cspin(spi_device, device_name, bus_name, pin, NULL);
struct lpc_sw_spi_cs *cs_pin = (struct lpc_sw_spi_cs *)rt_malloc(sizeof(struct lpc_sw_spi_cs));
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 rt_err_t spi_configure(struct rt_spi_device *device, struct rt_spi_configuration *cfg) static rt_err_t spi_configure(struct rt_spi_device *device, struct rt_spi_configuration *cfg)
@ -102,12 +92,11 @@ static rt_ssize_t spixfer(struct rt_spi_device *device, struct rt_spi_message *m
RT_ASSERT(device->bus != RT_NULL); RT_ASSERT(device->bus != RT_NULL);
RT_ASSERT(device->bus->parent.user_data != RT_NULL); RT_ASSERT(device->bus->parent.user_data != RT_NULL);
struct lpc_spi *spi = (struct lpc_spi *)(device->bus->parent.user_data); struct lpc_spi *spi = device->bus->parent.user_data;
struct lpc_sw_spi_cs *cs = device->parent.user_data;
if (message->cs_take) if (message->cs_take)
{ {
rt_pin_write(cs->pin, PIN_LOW); rt_pin_write(device->cs_pin, PIN_LOW);
} }
transfer.dataSize = message->length; transfer.dataSize = message->length;
@ -122,8 +111,8 @@ static rt_ssize_t spixfer(struct rt_spi_device *device, struct rt_spi_message *m
for (i = 0; i < block; i++) for (i = 0; i < block; i++)
{ {
transfer.dataSize = DMA_MAX_TRANSFER_SIZE; transfer.dataSize = DMA_MAX_TRANSFER_SIZE;
if (message->recv_buf) transfer.rxData = (uint8_t *)(message->recv_buf + i * DMA_MAX_TRANSFER_SIZE); if (message->recv_buf) transfer.rxData = (uint8_t *)(message->recv_buf + i *DMA_MAX_TRANSFER_SIZE);
if (message->send_buf) transfer.txData = (uint8_t *)(message->send_buf + i * DMA_MAX_TRANSFER_SIZE); if (message->send_buf) transfer.txData = (uint8_t *)(message->send_buf + i *DMA_MAX_TRANSFER_SIZE);
LPSPI_MasterTransferEDMA(spi->LPSPIx, &spi->spi_dma_handle, &transfer); LPSPI_MasterTransferEDMA(spi->LPSPIx, &spi->spi_dma_handle, &transfer);
rt_sem_take(spi->sem, RT_WAITING_FOREVER); rt_sem_take(spi->sem, RT_WAITING_FOREVER);
@ -132,8 +121,8 @@ static rt_ssize_t spixfer(struct rt_spi_device *device, struct rt_spi_message *m
if (remain) if (remain)
{ {
transfer.dataSize = remain; transfer.dataSize = remain;
if (message->recv_buf) transfer.rxData = (uint8_t *)(message->recv_buf + i * DMA_MAX_TRANSFER_SIZE); if (message->recv_buf) transfer.rxData = (uint8_t *)(message->recv_buf + i *DMA_MAX_TRANSFER_SIZE);
if (message->send_buf) transfer.txData = (uint8_t *)(message->send_buf + i * DMA_MAX_TRANSFER_SIZE); if (message->send_buf) transfer.txData = (uint8_t *)(message->send_buf + i *DMA_MAX_TRANSFER_SIZE);
LPSPI_MasterTransferEDMA(spi->LPSPIx, &spi->spi_dma_handle, &transfer); LPSPI_MasterTransferEDMA(spi->LPSPIx, &spi->spi_dma_handle, &transfer);
rt_sem_take(spi->sem, RT_WAITING_FOREVER); rt_sem_take(spi->sem, RT_WAITING_FOREVER);
@ -141,7 +130,7 @@ static rt_ssize_t spixfer(struct rt_spi_device *device, struct rt_spi_message *m
if (message->cs_release) if (message->cs_release)
{ {
rt_pin_write(cs->pin, PIN_HIGH); rt_pin_write(device->cs_pin, PIN_HIGH);
} }
return message->length; return message->length;

View File

@ -33,7 +33,6 @@ enum
#endif #endif
}; };
struct lpc_spi struct lpc_spi
{ {
struct rt_spi_bus parent; struct rt_spi_bus parent;
@ -55,7 +54,6 @@ struct lpc_spi
char *name; char *name;
}; };
static struct lpc_spi lpc_obj[] = static struct lpc_spi lpc_obj[] =
{ {
#ifdef BSP_USING_SPI1 #ifdef BSP_USING_SPI1
@ -98,6 +96,7 @@ static struct lpc_spi lpc_obj[] =
.tx_dma_chl = 4, .tx_dma_chl = 4,
.rx_dma_chl = 5, .rx_dma_chl = 5,
.name = "spi6", .name = "spi6",
},
#endif /* BSP_USING_SPI6 */ #endif /* BSP_USING_SPI6 */
#ifdef BSP_USING_SPI7 #ifdef BSP_USING_SPI7
{ {
@ -115,30 +114,17 @@ static struct lpc_spi lpc_obj[] =
#endif /* BSP_USING_SPI7 */ #endif /* BSP_USING_SPI7 */
}; };
struct lpc_sw_spi_cs
{
rt_uint32_t pin;
};
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)
{ {
rt_err_t ret = RT_EOK; struct rt_spi_device *spi_device = rt_malloc(sizeof(struct rt_spi_device));
if (!spi_device)
{
return -RT_ENOMEM;
}
struct rt_spi_device *spi_device = (struct rt_spi_device *)rt_malloc(sizeof(struct rt_spi_device)); return rt_spi_bus_attach_device_cspin(spi_device, device_name, bus_name, pin, RT_NULL);
struct lpc_sw_spi_cs *cs_pin = (struct lpc_sw_spi_cs *)rt_malloc(sizeof(struct lpc_sw_spi_cs));
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 rt_err_t spi_configure(struct rt_spi_device *device, struct rt_spi_configuration *cfg) static rt_err_t spi_configure(struct rt_spi_device *device, struct rt_spi_configuration *cfg)
{ {
rt_err_t ret = RT_EOK; rt_err_t ret = RT_EOK;
@ -148,10 +134,9 @@ static rt_err_t spi_configure(struct rt_spi_device *device, struct rt_spi_config
return ret; return ret;
} }
static void LPSPI_MasterUserCallback(LPSPI_Type *base, lpspi_master_edma_handle_t *handle, status_t status, void *userData) static void LPSPI_MasterUserCallback(LPSPI_Type *base, lpspi_master_edma_handle_t *handle, status_t status, void *userData)
{ {
struct lpc_spi *spi = (struct lpc_spi*)userData; struct lpc_spi *spi = (struct lpc_spi *)userData;
rt_sem_release(spi->sem); rt_sem_release(spi->sem);
} }
@ -166,12 +151,11 @@ static rt_ssize_t spixfer(struct rt_spi_device *device, struct rt_spi_message *m
RT_ASSERT(device->bus->parent.user_data != RT_NULL); RT_ASSERT(device->bus->parent.user_data != RT_NULL);
struct lpc_spi *spi = (struct lpc_spi *)(device->bus->parent.user_data); struct lpc_spi *spi = device->bus->parent.user_data;
struct lpc_sw_spi_cs *cs = device->parent.user_data;
if(message->cs_take) if (message->cs_take)
{ {
rt_pin_write(cs->pin, PIN_LOW); rt_pin_write(device->cs_pin, PIN_LOW);
} }
transfer.dataSize = message->length; transfer.dataSize = message->length;
@ -179,7 +163,7 @@ static rt_ssize_t spixfer(struct rt_spi_device *device, struct rt_spi_message *m
transfer.txData = (uint8_t *)(message->send_buf); transfer.txData = (uint8_t *)(message->send_buf);
// if(message->length < MAX_DMA_TRANSFER_SIZE) // if(message->length < MAX_DMA_TRANSFER_SIZE)
if(0) if (0)
{ {
LPSPI_MasterTransferBlocking(spi->LPSPIx, &transfer); LPSPI_MasterTransferBlocking(spi->LPSPIx, &transfer);
} }
@ -189,51 +173,46 @@ static rt_ssize_t spixfer(struct rt_spi_device *device, struct rt_spi_message *m
block = message->length / DMA_MAX_TRANSFER_COUNT; block = message->length / DMA_MAX_TRANSFER_COUNT;
remain = message->length % DMA_MAX_TRANSFER_COUNT; remain = message->length % DMA_MAX_TRANSFER_COUNT;
for(i=0; i<block; i++) for (i = 0; i < block; i++)
{ {
transfer.dataSize = DMA_MAX_TRANSFER_COUNT; transfer.dataSize = DMA_MAX_TRANSFER_COUNT;
if(message->recv_buf) transfer.rxData = (uint8_t *)(message->recv_buf + i*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); if (message->send_buf) transfer.txData = (uint8_t *)(message->send_buf + i *DMA_MAX_TRANSFER_COUNT);
LPSPI_MasterTransferEDMA(spi->LPSPIx, &spi->spi_dma_handle, &transfer); LPSPI_MasterTransferEDMA(spi->LPSPIx, &spi->spi_dma_handle, &transfer);
rt_sem_take(spi->sem, RT_WAITING_FOREVER); rt_sem_take(spi->sem, RT_WAITING_FOREVER);
} }
if(remain) if (remain)
{ {
transfer.dataSize = remain; transfer.dataSize = remain;
if(message->recv_buf) transfer.rxData = (uint8_t *)(message->recv_buf + i*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); if (message->send_buf) transfer.txData = (uint8_t *)(message->send_buf + i *DMA_MAX_TRANSFER_COUNT);
LPSPI_MasterTransferEDMA(spi->LPSPIx, &spi->spi_dma_handle, &transfer); LPSPI_MasterTransferEDMA(spi->LPSPIx, &spi->spi_dma_handle, &transfer);
rt_sem_take(spi->sem, RT_WAITING_FOREVER); rt_sem_take(spi->sem, RT_WAITING_FOREVER);
} }
} }
if (message->cs_release)
if(message->cs_release)
{ {
rt_pin_write(cs->pin, PIN_HIGH); rt_pin_write(device->cs_pin, PIN_HIGH);
} }
return message->length; return message->length;
} }
static struct rt_spi_ops lpc_spi_ops = static struct rt_spi_ops lpc_spi_ops =
{ {
.configure = spi_configure, .configure = spi_configure,
.xfer = spixfer .xfer = spixfer
}; };
int rt_hw_spi_init(void) int rt_hw_spi_init(void)
{ {
int i; int i;
for(i=0; i<ARRAY_SIZE(lpc_obj); i++) for (i = 0; i < ARRAY_SIZE(lpc_obj); i++)
{ {
CLOCK_SetClkDiv(lpc_obj[i].clock_div_name, 1u); CLOCK_SetClkDiv(lpc_obj[i].clock_div_name, 1u);
CLOCK_AttachClk(lpc_obj[i].clock_attach_id); CLOCK_AttachClk(lpc_obj[i].clock_attach_id);
@ -243,7 +222,7 @@ int rt_hw_spi_init(void)
lpspi_master_config_t masterConfig; lpspi_master_config_t masterConfig;
LPSPI_MasterGetDefaultConfig(&masterConfig); LPSPI_MasterGetDefaultConfig(&masterConfig);
masterConfig.baudRate = 24*1000*1000; masterConfig.baudRate = 24 * 1000 * 1000;
masterConfig.pcsToSckDelayInNanoSec = 1000000000U / masterConfig.baudRate * 1U; masterConfig.pcsToSckDelayInNanoSec = 1000000000U / masterConfig.baudRate * 1U;
masterConfig.lastSckToPcsDelayInNanoSec = 1000000000U / masterConfig.baudRate * 1U; masterConfig.lastSckToPcsDelayInNanoSec = 1000000000U / masterConfig.baudRate * 1U;
masterConfig.betweenTransferDelayInNanoSec = 1000000000U / masterConfig.baudRate * 1U; masterConfig.betweenTransferDelayInNanoSec = 1000000000U / masterConfig.baudRate * 1U;