Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
SPI
NXP 微控制器
RT-Thread一般讨论
【NXP-MCXA153】SPI驱动移植
发布于 2024-07-23 22:01:42 浏览:253
订阅该版
[tocm] ## 介绍 SPI总线由`摩托罗拉公司`开发,是一种`全双工同步串行总线`,由四个IO口组成:CS、SCLK、MISO、MOSI;通常用于CPU和外设之间进行通信,常见的SPI总线设备有:TFT LCD、QSPI FLASH、时钟模块、IMU等;`NXP-MCXA153`开发板上集成了两路SPI总线,本次实验将重点介绍RT-Thread系统中SPI BSP驱动的移植过程 ## 移植流程 **以SPI0为例** ① 在board里边添加相应的外设:配置spi0外设为复位状态、设置GPIO引脚功能 ② 添加相应的Kconfig开关,用以指示相应的外设开启与关闭(本质是通过宏定义或者条件编译的方式) ③ 根据[SDK_2_14_2_FRDM-MCXA153](https://mcuxpresso.nxp.com/zh/builder?hw=FRDM-MCXA153)提供的spi示例工程编写spi总线驱动,需要实现几个关键的函数 - rt_hw_spi_init - spi_configure - spixfer ④ 添加相应的库文件依赖:fsl_lpspi.c、fsl_lpspi_edma.c **引脚对应关系** | 序号 | GPIO | function | | :--: | :--: | :------: | | 1 | P1_3 | CS | | 2 | P1_1 | SCLK | | 3 | P1_2 | MISO | | 4 | P1_0 | MOSI | ## 驱动文件 ### board.c 在`rt_hw_board_init`函数里加入以下代码 ``` edma_config_t userConfig = {0}; EDMA_GetDefaultConfig(&userConfig); EDMA_Init(DMA0, &userConfig); ``` ### pin_mux.c 在`BOARD_InitPins`函数里加入以下代码 ``` #ifdef BSP_USING_SPI0 RESET_ReleasePeripheralReset(kLPSPI0_RST_SHIFT_RSTn); const port_pin_config_t port1_0_pin56_config = {/* Internal pull-up/down resistor is disabled */ kPORT_PullDisable, /* Low internal pull resistor value is selected. */ kPORT_LowPullResistor, /* Fast slew rate is configured */ kPORT_FastSlewRate, /* Passive input filter is disabled */ kPORT_PassiveFilterDisable, /* Open drain output is disabled */ kPORT_OpenDrainDisable, /* Low drive strength is configured */ kPORT_LowDriveStrength, /* Normal drive strength is configured */ kPORT_NormalDriveStrength, /* Pin is configured as LPSPI0_SDO */ kPORT_MuxAlt2, /* Digital input enabled */ kPORT_InputBufferEnable, /* Digital input is not inverted */ kPORT_InputNormal, /* Pin Control Register fields [15:0] are not locked */ kPORT_UnlockRegister}; /* PORT1_0 (pin 56) is configured as LPSPI0_SDO */ PORT_SetPinConfig(PORT1, 0U, &port1_0_pin56_config); const port_pin_config_t port1_1_pin57_config = {/* Internal pull-up/down resistor is disabled */ kPORT_PullDisable, /* Low internal pull resistor value is selected. */ kPORT_LowPullResistor, /* Fast slew rate is configured */ kPORT_FastSlewRate, /* Passive input filter is disabled */ kPORT_PassiveFilterDisable, /* Open drain output is disabled */ kPORT_OpenDrainDisable, /* Low drive strength is configured */ kPORT_LowDriveStrength, /* Normal drive strength is configured */ kPORT_NormalDriveStrength, /* Pin is configured as LPSPI0_SCK */ kPORT_MuxAlt2, /* Digital input enabled */ kPORT_InputBufferEnable, /* Digital input is not inverted */ kPORT_InputNormal, /* Pin Control Register fields [15:0] are not locked */ kPORT_UnlockRegister}; /* PORT1_1 (pin 57) is configured as LPSPI0_SCK */ PORT_SetPinConfig(PORT1, 1U, &port1_1_pin57_config); const port_pin_config_t port1_2_pin58_config = {/* Internal pull-up/down resistor is disabled */ kPORT_PullDisable, /* Low internal pull resistor value is selected. */ kPORT_LowPullResistor, /* Fast slew rate is configured */ kPORT_FastSlewRate, /* Passive input filter is disabled */ kPORT_PassiveFilterDisable, /* Open drain output is disabled */ kPORT_OpenDrainDisable, /* Low drive strength is configured */ kPORT_LowDriveStrength, /* Normal drive strength is configured */ kPORT_NormalDriveStrength, /* Pin is configured as LPSPI0_SDI */ kPORT_MuxAlt2, /* Digital input enabled */ kPORT_InputBufferEnable, /* Digital input is not inverted */ kPORT_InputNormal, /* Pin Control Register fields [15:0] are not locked */ kPORT_UnlockRegister}; /* PORT1_2 (pin 58) is configured as LPSPI0_SDI */ PORT_SetPinConfig(PORT1, 2U, &port1_2_pin58_config); const port_pin_config_t port1_3_pin59_config = {/* Internal pull-up/down resistor is disabled */ kPORT_PullDisable, /* Low internal pull resistor value is selected. */ kPORT_LowPullResistor, /* Fast slew rate is configured */ kPORT_FastSlewRate, /* Passive input filter is disabled */ kPORT_PassiveFilterDisable, /* Open drain output is disabled */ kPORT_OpenDrainDisable, /* Low drive strength is configured */ kPORT_LowDriveStrength, /* Normal drive strength is configured */ kPORT_NormalDriveStrength, /* Pin is configured as LPSPI0_PCS0 */ kPORT_MuxAlt2, /* Digital input enabled */ kPORT_InputBufferEnable, /* Digital input is not inverted */ kPORT_InputNormal, /* Pin Control Register fields [15:0] are not locked */ kPORT_UnlockRegister}; /* PORT1_3 (pin 59) is configured as LPSPI0_PCS0 */ PORT_SetPinConfig(PORT1, 3U, &port1_3_pin59_config); #endif ``` ### board/Kconfig 加入SPI0相关配置 ``` menuconfig BSP_USING_SPI config BSP_USING_SPI bool "Enable SPI" select RT_USING_SPI default y if BSP_USING_SPI config BSP_USING_SPI0 bool "Enable SPI0" default endif ``` ### drv_spi.c spi驱动层修改如下 ```c /* * Copyright (c) 2006-2024, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes * 2024-08-3 hywing The first version for MCXA */ #include "rtdevice.h" #include "drv_spi.h" #include "fsl_common.h" #include "fsl_lpspi.h" #include "fsl_lpspi_edma.h" #define DMA_MAX_TRANSFER_COUNT (32767) enum { #ifdef BSP_USING_SPI0 SPI1_INDEX, #endif }; struct lpc_spi { struct rt_spi_bus parent; LPSPI_Type *LPSPIx; clock_attach_id_t clock_attach_id; clock_div_name_t clock_div_name; clock_name_t clock_name; DMA_Type *DMAx; uint8_t tx_dma_chl; uint8_t rx_dma_chl; edma_handle_t dma_tx_handle; edma_handle_t dma_rx_handle; dma_request_source_t tx_dma_request; dma_request_source_t rx_dma_request; lpspi_master_edma_handle_t spi_dma_handle; rt_sem_t sem; char *name; }; static struct lpc_spi lpc_obj[] = { #ifdef BSP_USING_SPI0 { .LPSPIx = LPSPI0, .clock_attach_id = kFRO12M_to_LPSPI0, .clock_div_name = kCLOCK_DivLPSPI0, .clock_name = kCLOCK_Fro12M, .tx_dma_request = kDma0RequestLPSPI0Tx, .rx_dma_request = kDma0RequestLPSPI0Rx, .DMAx = DMA0, .tx_dma_chl = 0, .rx_dma_chl = 1, .name = "spi0", }, #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 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)); 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) { 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); return ret; } 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; rt_sem_release(spi->sem); } static rt_ssize_t spixfer(struct rt_spi_device *device, struct rt_spi_message *message) { int i; lpspi_transfer_t transfer = {0}; RT_ASSERT(device != RT_NULL); RT_ASSERT(device->bus != 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_sw_spi_cs *cs = device->parent.user_data; if (message->cs_take) { rt_pin_write(cs->pin, PIN_LOW); } transfer.dataSize = message->length; transfer.rxData = (uint8_t *)(message->recv_buf); transfer.txData = (uint8_t *)(message->send_buf); // if(message->length < MAX_DMA_TRANSFER_SIZE) if (0) { LPSPI_MasterTransferBlocking(spi->LPSPIx, &transfer); } else { uint32_t block, remain; block = message->length / DMA_MAX_TRANSFER_COUNT; remain = message->length % DMA_MAX_TRANSFER_COUNT; 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); LPSPI_MasterTransferEDMA(spi->LPSPIx, &spi->spi_dma_handle, &transfer); rt_sem_take(spi->sem, RT_WAITING_FOREVER); } 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); LPSPI_MasterTransferEDMA(spi->LPSPIx, &spi->spi_dma_handle, &transfer); rt_sem_take(spi->sem, RT_WAITING_FOREVER); } } if (message->cs_release) { rt_pin_write(cs->pin, PIN_HIGH); } return message->length; } static struct rt_spi_ops lpc_spi_ops = { .configure = spi_configure, .xfer = spixfer }; int rt_hw_spi_init(void) { int i; for (i = 0; i < ARRAY_SIZE(lpc_obj); i++) { CLOCK_SetClockDiv(lpc_obj[i].clock_div_name, 1u); 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); lpspi_master_config_t masterConfig; LPSPI_MasterGetDefaultConfig(&masterConfig); masterConfig.baudRate = 12 * 1000 * 1000; masterConfig.pcsToSckDelayInNanoSec = 1000000000U / masterConfig.baudRate * 1U; masterConfig.lastSckToPcsDelayInNanoSec = 1000000000U / masterConfig.baudRate * 1U; masterConfig.betweenTransferDelayInNanoSec = 1000000000U / masterConfig.baudRate * 1U; LPSPI_MasterInit(lpc_obj[i].LPSPIx, &masterConfig, CLOCK_GetFreq(lpc_obj[i].clock_name)); EDMA_CreateHandle(&lpc_obj[i].dma_tx_handle, lpc_obj[i].DMAx, lpc_obj[i].tx_dma_chl); EDMA_CreateHandle(&lpc_obj[i].dma_rx_handle, lpc_obj[i].DMAx, lpc_obj[i].rx_dma_chl); EDMA_SetChannelMux(lpc_obj[i].DMAx, lpc_obj[i].tx_dma_chl, lpc_obj[i].tx_dma_request); EDMA_SetChannelMux(lpc_obj[i].DMAx, lpc_obj[i].rx_dma_chl, lpc_obj[i].rx_dma_request); LPSPI_MasterTransferCreateHandleEDMA(lpc_obj[i].LPSPIx, &lpc_obj[i].spi_dma_handle, LPSPI_MasterUserCallback, &lpc_obj[i], &lpc_obj[i].dma_rx_handle, &lpc_obj[i].dma_tx_handle); rt_spi_bus_register(&lpc_obj[i].parent, lpc_obj[i].name, &lpc_spi_ops); } return RT_EOK; } INIT_DEVICE_EXPORT(rt_hw_spi_init); ``` ### SConscript 在`Libraries/MCXA153/SConscript`文件里边加上以下代码 ``` if GetDepend('BSP_USING_SPI'): src += ['MCXA153/drivers/fsl_lpspi.c'] src += ['MCXA153/drivers/fsl_lpspi_edma.c'] ``` ## 测试用例 打开menuconfig使能spi0驱动 ![打开spi0外设.png](https://oss-club.rt-thread.org/uploads/20240813/d1ebd71c1f4db2cc5731956c29e53add.png.webp) 短接MISO和MOSI引脚(P1_0和P1_2)进行自发自收测试 ![短接spi.jpg](https://oss-club.rt-thread.org/uploads/20240727/484108c378e72089fbccf8c8cd1c8023.jpg.webp) 测试程序 ```c #include
#include "rtdevice.h" #include "drv_spi.h" #define SPI_BUS_NAME "spi0" #define SPI_DEV_NAME "spi00" static struct rt_spi_device *spi_device; static void spi_sample(void) { rt_err_t result; struct rt_spi_configuration cfg; uint8_t tx_buf[] = "Hello RT-Thread!"; uint8_t rx_buf[sizeof(tx_buf)]; rt_base_t cs = 1*32+3; rt_hw_spi_device_attach(SPI_BUS_NAME, SPI_DEV_NAME, cs); /* »ñÈ¡SPIÉ豸 */ spi_device = (struct rt_spi_device *)rt_device_find(SPI_DEV_NAME); if (!spi_device) { rt_kprintf("can't find %s device!\n", SPI_BUS_NAME); } /* ÅäÖÃSPIÉ豸 */ cfg.data_width = 8; cfg.mode = RT_SPI_MASTER | RT_SPI_MODE_0 | RT_SPI_MSB; cfg.max_hz = 12* 1000 * 1000; /* ÉèÖÃSPIÉ豸 */ rt_spi_configure(spi_device, &cfg); result = rt_spi_transfer(spi_device, tx_buf, rx_buf, sizeof(tx_buf)); if (result == sizeof(tx_buf)) { rt_kprintf("Send: %s\n", tx_buf); rt_kprintf("Received: %s\n", rx_buf); } else { rt_kprintf("spi transfer failed! error code: %d\n", result); } } int main(void) { spi_sample(); return 0; } ``` 运行结果 ![spi输出结果.png](https://oss-club.rt-thread.org/uploads/20240727/f0888b14520fb3df04b33e4ebcee6924.png) ## 总结 另外,你也可以安装`MCUXpresso Config Tools v16`,通过图形方式配置时钟树、GPIO复用
0
条评论
默认排序
按发布时间排序
登录
注册新账号
关于作者
hywing
嵌入式系统开发工程师,从事物联网、工业自动化、汽车电子开发工作
文章
6
回答
3
被采纳
0
关注TA
发私信
相关文章
1
有关动态模块加载的一篇论文
2
最近的调程序总结
3
晕掉了,这么久都不见layer2的踪影啊
4
继续K9ii的历程
5
[GUI相关] FreeType 2
6
[GUI相关]嵌入式系统中文输入法的设计
7
20081101 RT-Thread开发者聚会总结
8
嵌入式系统基础
9
linux2.4.19在at91rm9200 上的寄存器设置
10
[转]基于嵌入式Linux的通用触摸屏校准程序
推荐文章
1
RT-Thread应用项目汇总
2
玩转RT-Thread系列教程
3
国产MCU移植系列教程汇总,欢迎查看!
4
机器人操作系统 (ROS2) 和 RT-Thread 通信
5
五分钟玩转RT-Thread新社区
6
【技术三千问】之《玩转ART-Pi》,看这篇就够了!干货汇总
7
关于STM32H7开发板上使用SDIO接口驱动SD卡挂载文件系统的问题总结
8
STM32的“GPU”——DMA2D实例详解
9
RT-Thread隐藏的宝藏之completion
10
【ART-PI】RT-Thread 开启RTC 与 Alarm组件
热门标签
RT-Thread Studio
串口
Env
LWIP
SPI
AT
Bootloader
Hardfault
CAN总线
FinSH
ART-Pi
USB
DMA
文件系统
RT-Thread
SCons
RT-Thread Nano
线程
MQTT
STM32
RTC
FAL
rt-smart
ESP8266
I2C_IIC
WIZnet_W5500
UART
ota在线升级
PWM
cubemx
freemodbus
flash
packages_软件包
BSP
潘多拉开发板_Pandora
定时器
ADC
GD32
flashDB
socket
中断
Debug
编译报错
msh
SFUD
keil_MDK
rt_mq_消息队列_msg_queue
ulog
C++_cpp
at_device
本月问答贡献
踩姑娘的小蘑菇
7
个答案
3
次被采纳
a1012112796
13
个答案
2
次被采纳
张世争
9
个答案
2
次被采纳
rv666
5
个答案
2
次被采纳
用户名由3_15位
11
个答案
1
次被采纳
本月文章贡献
程序员阿伟
8
篇文章
2
次点赞
hhart
3
篇文章
4
次点赞
大龄码农
1
篇文章
5
次点赞
ThinkCode
1
篇文章
1
次点赞
Betrayer
1
篇文章
1
次点赞
回到
顶部
发布
问题
投诉
建议
回到
底部