概述
RT-Thread 对SPI总线的驱动,抽象出了spi bus 的设备驱动,我们基于S32K146 的硬件学习spi bus 设备驱动。
RT-Thread 官方文档https://www.rt-thread.org/document/site/#/rt-thread-version/rt-thread-standard/programming-manual/device/spi/spi对SPI总线驱动有详细说明。
SPI总线驱动适配
驱动涉及的主要结构体为如下:
rt_spi_device:app 通过该结构体和bus 绑定通过bus 控制spi总线
rt_spi_bus spi bus总线设备抽象
rt_spi_ops bus设备依赖的底层操作函数
结构体关系如下:
对于总线设备的驱动对接我们需要实现对应的ops函数。
使用S32 Design Studio 工具配置spi1
本次实验使用的是lpspi1 ,首先使用S32 Design Studio 工具生成pinmux 和 clk 初始化配置代码。
配置SPI1 master 参数
配置pimmux
配置clk
适配SPI1 ops 函数至RT-Thread
根据上面的梳理,依赖芯片层实现ops函数并开启spi设备驱动
config ops函数
tatic rt_err_t spi_configure(struct rt_spi_device* device,
struct rt_spi_configuration* configuration)
{
struct rt_spi_bus * spi_bus = (struct rt_spi_bus *)device->bus;
struct s32k_spi *spi_device = (struct s32k_spi *)spi_bus->parent.user_data;
status_t ret;
RT_ASSERT(device != RT_NULL);
RT_ASSERT(configuration != RT_NULL);
/* config spi init spi bus */
//LPSPI_DRV_MasterDeinit(spi_device->instance);
switch(configuration->mode & RT_SPI_MODE_3)
{
case RT_SPI_MODE_0:
spi_device->masterconfig->clkPhase = LPSPI_CLOCK_PHASE_2ND_EDGE;
spi_device->masterconfig->clkPolarity = LPSPI_SCK_ACTIVE_LOW;
break;
case RT_SPI_MODE_1:
spi_device->masterconfig->clkPhase = LPSPI_CLOCK_PHASE_1ST_EDGE;
spi_device->masterconfig->clkPolarity = LPSPI_SCK_ACTIVE_LOW;
break;
case RT_SPI_MODE_2:
spi_device->masterconfig->clkPhase = LPSPI_CLOCK_PHASE_2ND_EDGE;
spi_device->masterconfig->clkPolarity = LPSPI_SCK_ACTIVE_HIGH;
break;
case RT_SPI_MODE_3:
spi_device->masterconfig->clkPhase = LPSPI_CLOCK_PHASE_1ST_EDGE;
spi_device->masterconfig->clkPolarity = LPSPI_SCK_ACTIVE_HIGH;
break;
}
/* MSB or LSB */
if(configuration->mode & RT_SPI_MSB)
{
spi_device->masterconfig->lsbFirst = false;
}
else
{
spi_device->masterconfig->lsbFirst = true;
}
if(configuration->max_hz < 10000000)
spi_device->masterconfig->bitsPerSec = configuration->max_hz;
spi_device->masterconfig->bitcount = configuration->data_width;
ret = LPSPI_DRV_MasterInit(spi_device->instance,spi_device->status,spi_device->masterconfig);
RT_ASSERT(ret == STATUS_SUCCESS);
ret = LPSPI_DRV_MasterSetDelay(spi_device->instance, 1, 1, 1);
RT_ASSERT(ret == STATUS_SUCCESS);
return ret == STATUS_SUCCESS ? RT_EOK:RT_ERROR;
xfer ops 函数
truct s32k_spi
{
struct rt_spi_bus spi_bus; /* spi bus device */
lpspi_master_config_t * masterconfig; /* lpspi master config */
lpspi_state_t * status; /* lpspi driver status */
uint32_t instance; /* spi instance id */
char * bus_name;
}s32k_spi_t;
static rt_ssize_t spixfer(struct rt_spi_device* device, struct rt_spi_message* message)
{
struct rt_spi_bus * spi_bus = (struct rt_spi_bus *)device->bus;
struct s32k_spi *spi_device = (struct s32k_spi *)spi_bus->parent.user_data;
status_t ret;
RT_ASSERT(device != NULL);
RT_ASSERT(message != NULL);
ret = LPSPI_DRV_MasterTransferBlocking(spi_device->instance,message->send_buf,message->recv_buf,message->length,1000);
RT_ASSERT(ret == STATUS_SUCCESS);
return ret == STATUS_SUCCESS ? message->length:0;
添加如下初始化代码像系统注册spibus设备
xtern void LPSPI_DRV_IRQHandler(uint32_t instance);
void S32K14X_LPspi1_Master_Slave_IRQHandler(void)
{
/* enter interrupt */
rt_interrupt_enter();
LPSPI_DRV_IRQHandler(1);
/* leave interrupt */
rt_interrupt_leave();
}
/* private rt-thread spi ops function */
static struct rt_spi_ops s32k_spi_ops =
{
.configure = spi_configure,
.xfer = spixfer,
};
static struct s32k_spi spi1;
int rt_hw_spi_init(void)
{
int result = 0;
/* config spi strruct */
spi1.instance = INST_LPSPI_1;
spi1.masterconfig = &lpspi_1_MasterConfig0;
spi1.status = &lpspi_1State;
spi1.spi_bus.parent.user_data = (void *)&spi1;
spi1.bus_name = "spi1";
result = rt_spi_bus_register(&spi1.spi_bus, spi1.bus_name, &s32k_spi_ops);
RT_ASSERT(result == RT_EOK);
LOG_D("%s bus init done", spi1.bus_name);
if(result == RT_EOK)
{
INT_SYS_InstallHandler(LPSPI1_IRQn,S32K14X_LPspi1_Master_Slave_IRQHandler,NULL);
}
return result;
}
INIT_APP_EXPORT(rt_hw_spi_init);
验证
添加如下测试代码输入 spi10 open/config/readid 命令打开并挂载到spi1总线,并读取板子上的spi flash 的id信息
include
struct rt_spi_device spi1_device;
static void spi10(int argc,char *argv[])
{
rt_err_t ret;
struct rt_spi_configuration cfg;
uint8_t sendbuff[4] = {SPF_R_JEDEC_CMD,0x00,0x00,0x00};
uint8_t recvbuff[4] = {0x00,0x00,0x00,0x00};
if(!strcmp(argv[1], "readid"))
{
rt_spi_transfer(&spi1_device,sendbuff,recvbuff,4);
rt_kprintf("read did [%x][%x][%x]\n",recvbuff[1],recvbuff[2],recvbuff[3]);
}
else if(!strcmp(argv[1], "open"))
{
ret = rt_spi_bus_attach_device(&spi1_device,"spi10","spi1",NULL);
if(ret != RT_EOK)
rt_kprintf("attach spi1 faied %d\n",ret);
}
else if(!strcmp(argv[1], "config"))
{
cfg.data_width = 8;
cfg.max_hz = 8000000;
cfg.mode = RT_SPI_MODE_3 | RT_SPI_MSB;
rt_spi_configure(&spi1_device,&cfg);
}
else if(!strcmp(argv[1], "read"))
{
}
}
MSH_CMD_EXPORT(spi10, spi10 flash test)
输入命令验证已经读取到flash 的Id 信息说明SPI 总线和spi flash 通信ok ,而且系统内也已经多了spi1 bus设备和spi10 的spi 设备节点。
试验读取的did和芯片手册的也是一致的。
测试代码已提交如下路径:
https://gitee.com/andeyqi/rt-thread/tree/dev/bsp/s32k14x
想要在RT-Thread平台或社区投放内容?或想参与相关直播活动及赛事?RT-Thread已开放对接窗口,请通过邮件与我们取得联系,期待合作!