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

440 lines
9.2 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 "drv_spi.h"
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"
#if defined(BSP_USING_SPIBUS0) || \
defined(BSP_USING_SPIBUS1) || \
defined(BSP_USING_SPIBUS2) || \
defined(BSP_USING_SPIBUS3) || \
defined(BSP_USING_SPIBUS4) || \
defined(BSP_USING_SPIBUS5) || \
defined(BSP_USING_SPIBUS6) || \
defined(BSP_USING_SPIBUS7) || \
defined(BSP_USING_SPIBUS8)
2021-03-17 02:26:35 +08:00
2019-10-24 17:56:09 +08:00
#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
struct lpc_spi
{
2019-10-24 17:56:09 +08:00
SPI_Type *base;
struct rt_spi_configuration *cfg;
SYSCON_RSTn_t spi_rst;
};
static uint32_t lpc_get_spi_freq(SPI_Type *base)
{
uint32_t freq = 0;
2021-03-17 02:26:35 +08:00
2019-10-24 17:56:09 +08:00
#if defined(BSP_USING_SPIBUS0)
if(base == SPI0)
{
freq = CLOCK_GetFlexCommClkFreq(0);
2019-10-24 17:56:09 +08:00
}
2021-03-17 02:26:35 +08:00
#endif
2019-10-24 17:56:09 +08:00
#if defined(BSP_USING_SPIBUS1)
if(base == SPI1)
{
freq = CLOCK_GetFlexCommClkFreq(1);
2019-10-24 17:56:09 +08:00
}
#endif
#if defined(BSP_USING_SPIBUS2)
if(base == SPI2)
{
freq = CLOCK_GetFlexCommClkFreq(2);
2019-10-24 17:56:09 +08:00
}
#endif
#if defined(BSP_USING_SPIBUS3)
if(base == SPI3)
{
freq = CLOCK_GetFlexCommClkFreq(3);
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
#if defined(BSP_USING_SPIBUS4)
if(base == SPI4)
{
freq = CLOCK_GetFlexCommClkFreq(4);
2019-10-24 17:56:09 +08:00
}
#endif
#if defined(BSP_USING_SPIBUS5)
if(base == SPI5)
{
freq = CLOCK_GetFlexCommClkFreq(5);
2019-10-24 17:56:09 +08:00
}
#endif
#if defined(BSP_USING_SPIBUS6)
if(base == SPI6)
{
freq = CLOCK_GetFlexCommClkFreq(6);
2019-10-24 17:56:09 +08:00
}
#endif
#if defined(BSP_USING_SPIBUS7)
if(base == SPI7)
{
freq = CLOCK_GetFlexCommClkFreq(7);
2019-10-24 17:56:09 +08:00
}
#endif
/* High Speed SPI - 50MHz */
#if defined(BSP_USING_SPIBUS8)
if(base == SPI8)
{
freq = CLOCK_GetHsLspiClkFreq();
2019-10-24 17:56:09 +08:00
}
#endif
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
RT_ASSERT(cfg != RT_NULL);
2019-10-24 17:56:09 +08:00
if(cfg->data_width != 8 && cfg->data_width != 16)
{
2021-03-17 02:26:35 +08:00
return (-RT_EINVAL);
2019-10-24 17:56:09 +08:00
}
2021-03-17 02:26:35 +08:00
SPI_MasterGetDefaultConfig(&masterConfig);
2019-10-24 17:56:09 +08:00
#if defined(BSP_USING_SPIBUS8)
if(base == SPI8)
{
if(cfg->max_hz > 50*1000*1000)
{
cfg->max_hz = 50*1000*1000;
}
}
#else
if(cfg->max_hz > 12*1000*1000)
{
cfg->max_hz = 12*1000*1000;
}
#endif
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 lpc_spi_bus_attach_device(const char *bus_name, const char *device_name, rt_uint32_t pin)
{
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));
RT_ASSERT(spi_device != RT_NULL);
rt_pin_mode(pin, PIN_MODE_OUTPUT);
rt_pin_write(pin, PIN_HIGH);
ret = rt_spi_bus_attach_device_cspin(spi_device, device_name, bus_name, pin, NULL);
2021-03-17 02:26:35 +08:00
return ret;
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;
2019-10-24 17:56:09 +08:00
RT_ASSERT(cfg != RT_NULL);
RT_ASSERT(device != RT_NULL);
2021-03-17 02:26:35 +08:00
spi = (struct lpc_spi *)(device->bus->parent.user_data);
spi->cfg = cfg;
ret = lpc_spi_init(spi->base, cfg);
2019-10-24 17:56:09 +08:00
return ret;
}
#define SPISTEP(datalen) (((datalen) == 8) ? 1 : 2)
static rt_uint32_t spixfer(struct rt_spi_device *device, struct rt_spi_message *message)
{
uint32_t length;
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
struct lpc_spi *spi = (struct lpc_spi *)(device->bus->parent.user_data);
int cs_pin = device->cs_pin;
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
2019-10-24 17:56:09 +08:00
length = message->length;
2021-03-17 02:26:35 +08:00
const rt_uint8_t *txData = (uint8_t *)(message->send_buf);
rt_uint8_t *rxData = (uint8_t *)(message->recv_buf);
2019-10-24 17:56:09 +08:00
rt_kprintf("*** spi send %d\r\n", length);
while (length)
{
/* clear tx/rx errors and empty FIFOs */
spi->base->FIFOCFG |= SPI_FIFOCFG_EMPTYTX_MASK | SPI_FIFOCFG_EMPTYRX_MASK;
spi->base->FIFOSTAT |= SPI_FIFOSTAT_TXERR_MASK | SPI_FIFOSTAT_RXERR_MASK;
spi->base->FIFOWR = *txData | 0x07300000;
/* wait if TX FIFO of previous transfer is not empty */
while ((spi->base->FIFOSTAT & SPI_FIFOSTAT_RXNOTEMPTY_MASK) == 0) {
}
if(rxData != NULL)
{
*rxData = spi->base->FIFORD;
rxData += SPISTEP(spi->cfg->data_width);
}
txData += SPISTEP(spi->cfg->data_width);;
length--;
2021-03-17 02:26:35 +08:00
}
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
}
2021-03-17 02:26:35 +08:00
return (message->length - length);
2019-10-24 17:56:09 +08:00
}
#if defined(BSP_USING_SPIBUS0)
2021-03-17 02:26:35 +08:00
static struct lpc_spi spi0 =
2019-10-24 17:56:09 +08:00
{
.base = SPI0
2021-03-17 02:26:35 +08:00
};
static struct rt_spi_bus spi0_bus =
2019-10-24 17:56:09 +08:00
{
.parent.user_data = &spi0
2021-03-17 02:26:35 +08:00
};
2019-10-24 17:56:09 +08:00
#endif
#if defined(BSP_USING_SPIBUS1)
2021-03-17 02:26:35 +08:00
static struct lpc_spi spi1 =
2019-10-24 17:56:09 +08:00
{
.base = SPI1
2021-03-17 02:26:35 +08:00
};
static struct rt_spi_bus spi1_bus =
2019-10-24 17:56:09 +08:00
{
.parent.user_data = &spi1
2021-03-17 02:26:35 +08:00
};
2019-10-24 17:56:09 +08:00
#endif
#if defined(BSP_USING_SPIBUS2)
2021-03-17 02:26:35 +08:00
static struct lpc_spi spi2 =
2019-10-24 17:56:09 +08:00
{
.base = SPI2
2021-03-17 02:26:35 +08:00
};
static struct rt_spi_bus spi2_bus =
2019-10-24 17:56:09 +08:00
{
.parent.user_data = &spi2
2021-03-17 02:26:35 +08:00
};
2019-10-24 17:56:09 +08:00
#endif
#if defined(BSP_USING_SPIBUS3)
2021-03-17 02:26:35 +08:00
static struct lpc_spi spi3 =
2019-10-24 17:56:09 +08:00
{
.base = SPI3
2021-03-17 02:26:35 +08:00
};
static struct rt_spi_bus spi3_bus =
2019-10-24 17:56:09 +08:00
{
.parent.user_data = &spi3
2021-03-17 02:26:35 +08:00
};
2019-10-24 17:56:09 +08:00
#endif
#if defined(BSP_USING_SPIBUS4)
2021-03-17 02:26:35 +08:00
static struct lpc_spi spi4 =
2019-10-24 17:56:09 +08:00
{
.base = SPI4
2021-03-17 02:26:35 +08:00
};
static struct rt_spi_bus spi4_bus =
2019-10-24 17:56:09 +08:00
{
.parent.user_data = &spi4
2021-03-17 02:26:35 +08:00
};
2019-10-24 17:56:09 +08:00
#endif
#if defined(BSP_USING_SPIBUS5)
2021-03-17 02:26:35 +08:00
static struct lpc_spi spi5 =
2019-10-24 17:56:09 +08:00
{
.base = SPI5
2021-03-17 02:26:35 +08:00
};
static struct rt_spi_bus spi5_bus =
2019-10-24 17:56:09 +08:00
{
.parent.user_data = &spi5
2021-03-17 02:26:35 +08:00
};
2019-10-24 17:56:09 +08:00
#endif
#if defined(BSP_USING_SPIBUS6)
2021-03-17 02:26:35 +08:00
static struct lpc_spi spi6 =
2019-10-24 17:56:09 +08:00
{
.base = SPI6
2021-03-17 02:26:35 +08:00
};
static struct rt_spi_bus spi6_bus =
2019-10-24 17:56:09 +08:00
{
.parent.user_data = &spi6
2021-03-17 02:26:35 +08:00
};
2019-10-24 17:56:09 +08:00
#endif
#if defined(BSP_USING_SPIBUS7)
2021-03-17 02:26:35 +08:00
static struct lpc_spi spi7 =
2019-10-24 17:56:09 +08:00
{
.base = SPI7
2021-03-17 02:26:35 +08:00
};
static struct rt_spi_bus spi7_bus =
2019-10-24 17:56:09 +08:00
{
.parent.user_data = &spi7
2021-03-17 02:26:35 +08:00
};
2019-10-24 17:56:09 +08:00
#endif
#if defined(BSP_USING_SPIBUS8)
2021-03-17 02:26:35 +08:00
static struct lpc_spi spi8 =
2019-10-24 17:56:09 +08:00
{
.base = SPI8
2021-03-17 02:26:35 +08:00
};
static struct rt_spi_bus spi8_bus =
2019-10-24 17:56:09 +08:00
{
.parent.user_data = &spi8
2021-03-17 02:26:35 +08:00
};
2019-10-24 17:56:09 +08:00
#endif
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)
{
#if defined(BSP_USING_SPIBUS0)
CLOCK_AttachClk(kFRO12M_to_FLEXCOMM0);
RESET_PeripheralReset(kFC0_RST_SHIFT_RSTn);
2021-03-17 02:26:35 +08:00
spi0.cfg = RT_NULL;
rt_spi_bus_register(&spi0_bus, "spi0", &lpc_spi_ops);
2019-10-24 17:56:09 +08:00
#endif
#if defined(BSP_USING_SPIBUS1)
CLOCK_AttachClk(kFRO12M_to_FLEXCOMM1);
RESET_PeripheralReset(kFC1_RST_SHIFT_RSTn);
2021-03-17 02:26:35 +08:00
spi1.cfg = RT_NULL;
rt_spi_bus_register(&spi1_bus, "spi1", &lpc_spi_ops);
2019-10-24 17:56:09 +08:00
#endif
#if defined(BSP_USING_SPIBUS2)
CLOCK_AttachClk(kFRO12M_to_FLEXCOMM2);
RESET_PeripheralReset(kFC2_RST_SHIFT_RSTn);
2021-03-17 02:26:35 +08:00
spi2.cfg = RT_NULL;
rt_spi_bus_register(&spi2_bus, "spi2", &lpc_spi_ops);
2019-10-24 17:56:09 +08:00
#endif
#if defined(BSP_USING_SPIBUS3)
CLOCK_AttachClk(kFRO12M_to_FLEXCOMM3);
RESET_PeripheralReset(kFC3_RST_SHIFT_RSTn);
2021-03-17 02:26:35 +08:00
spi3.cfg = RT_NULL;
rt_spi_bus_register(&spi3_bus, "spi3", &lpc_spi_ops);
2019-10-24 17:56:09 +08:00
#endif
#if defined(BSP_USING_SPIBUS4)
CLOCK_AttachClk(kFRO12M_to_FLEXCOMM4);
RESET_PeripheralReset(kFC4_RST_SHIFT_RSTn);
2021-03-17 02:26:35 +08:00
spi4.cfg = RT_NULL;
rt_spi_bus_register(&spi4_bus, "spi4", &lpc_spi_ops);
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
#if defined(BSP_USING_SPIBUS5)
CLOCK_AttachClk(kFRO12M_to_FLEXCOMM5);
RESET_PeripheralReset(kFC5_RST_SHIFT_RSTn);
2021-03-17 02:26:35 +08:00
spi5.cfg = RT_NULL;
rt_spi_bus_register(&spi5_bus, "spi5", &lpc_spi_ops);
2019-10-24 17:56:09 +08:00
#endif
#if defined(BSP_USING_SPIBUS6)
CLOCK_AttachClk(kFRO12M_to_FLEXCOMM6);
RESET_PeripheralReset(kFC6_RST_SHIFT_RSTn);
2021-03-17 02:26:35 +08:00
spi6.cfg = RT_NULL;
rt_spi_bus_register(&spi6_bus, "spi6", &lpc_spi_ops);
2019-10-24 17:56:09 +08:00
#endif
#if defined(BSP_USING_SPIBUS7)
CLOCK_AttachClk(kFRO12M_to_FLEXCOMM7);
RESET_PeripheralReset(kFC7_RST_SHIFT_RSTn);
2021-03-17 02:26:35 +08:00
spi7.cfg = RT_NULL;
rt_spi_bus_register(&spi7_bus, "spi7", &lpc_spi_ops);
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
#if defined(BSP_USING_SPIBUS8)
CLOCK_AttachClk(kMAIN_CLK_to_HSLSPI);
RESET_PeripheralReset(kHSLSPI_RST_SHIFT_RSTn);
spi8.cfg = RT_NULL;
spi8.spi_rst = kHSLSPI_RST_SHIFT_RSTn;
rt_spi_bus_register(&spi8_bus, "spi8", &lpc_spi_ops);
#endif
2021-03-17 02:26:35 +08:00
return RT_EOK;
2019-10-24 17:56:09 +08:00
}
INIT_BOARD_EXPORT(rt_hw_spi_init);
#endif