Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
SPI
w25q128
M2354
【NuMaker-M2354试用】SPI 驱动W25Q128
发布于 2021-12-12 23:44:59 浏览:1708
订阅该版
任务是测试spi接口 #功能模块的硬件介绍 开发版本身没有spi外设,没办法需要焊接或者连接spi外设。 经过芯片文档查询M2354由四组spi总线0-3  随机定使用SPI1 芯片手册、开发板用户手册查了一番,好多引脚有复用功能    这个就要确定程序demo里使用的是四个引脚 ,打开工程板级文件夹中引脚配置文件 board->nutool_pincfg.c  一番查找确定使用的: > SPI1_CS->PH9 > SPI1_CLK->PH8 > SPI1_MISO->PE1 > SPI1_MOSI->PE0 开发版对应丝印 > SPI1_CS->PH9 ------- JP9.12 108 > SPI1_CLK->PH8 ------- JP9.11 107 > SPI1_MISO->PE1 ------- JP9.9 105 > SPI1_MOSI->PE0 ------- JP9.10 106 SPI外设接W25Q128 懒得找板子割线再跳线焊线,直接找了个芯片手焊了    最终连接效果图  #功能模块的使用说明 接下来修改程序 目录rt-thread-4.0.4\bsp\nuvoton\numaker-m2354打开env 输入命令menuconfig Hardware Drivers Config → On-chip Peripheral Drivers → Enable Serial Peripheral Interface(SPI)  保存退出 输入scons --target=mdk5 打开keil 编译下载,查看spi1总线是否使能  接下来需要挂载spi外设 添加spi_flash_init.c  ``` /* * Change Logs: * Date Author Notes * 2021-12-10 lizhengyang add spi flash port file */ #include
#include "spi_flash.h" #include "spi_flash_sfud.h" #include "drv_spi.h" #include "drv_gpio.h" static int rt_hw_spi_flash_init(void) { rt_err_t result; struct rt_spi_device *spi_device; char *bus_name="spi1"; char *device_name="spi10"; char *spi_flash_dev_name="W25Q128"; nutool_modclkcfg_init_gph(); nutool_modclkcfg_init_gpe(); CLK_EnableModuleClock(SPI1_MODULE); /* Select SPI1 peripheral clock source as PCLK1 */ CLK_SetModuleClock(SPI1_MODULE, CLK_CLKSEL2_SPI1SEL_PCLK0, MODULE_NoMsk); /*---------------------------------------------------------------------------------------------------------*/ /* Init I/O Multi-function */ /*---------------------------------------------------------------------------------------------------------*/ SYS->GPE_MFPL &= ~(SYS_GPE_MFPL_PE1MFP_Msk | SYS_GPE_MFPL_PE0MFP_Msk); SYS->GPE_MFPL |= (SYS_GPE_MFPL_PE1MFP_SPI1_MISO | SYS_GPE_MFPL_PE0MFP_SPI1_MOSI); SYS->GPH_MFPH &= ~(SYS_GPH_MFPH_PH9MFP_Msk | SYS_GPH_MFPH_PH8MFP_Msk); SYS->GPH_MFPH |= (SYS_GPH_MFPH_PH9MFP_SPI1_SS | SYS_GPH_MFPH_PH8MFP_SPI1_CLK); /* attach the device to spi bus*/ spi_device = (struct rt_spi_device *)rt_malloc(sizeof(struct rt_spi_device)); RT_ASSERT(spi_device != RT_NULL); result=rt_spi_bus_attach_device(spi_device,device_name,bus_name,RT_NULL); if (result != RT_EOK) { LOG_E("%s attach to %s faild, %d\n", device_name, bus_name, result); } RT_ASSERT(result == RT_EOK); LOG_D("%s attach to %s done", device_name, bus_name); if (RT_NULL == rt_sfud_flash_probe(spi_flash_dev_name, device_name)) { return -RT_ERROR; } return RT_EOK; } INIT_COMPONENT_EXPORT(rt_hw_spi_flash_init); ```  接下就是参考 [rtthread文档中心](https://www.rt-thread.org/document/site/#/rt-thread-version/rt-thread-standard/programming-manual/device/spi/spi)  ``` /* * 程序清单:这是一个 SPI 设备使用例程 * 例程导出了 spi_w25q_test 命令到控制终端 * 命令调用格式:spi_w25q_test spi10 * 命令解释:命令第二个参数是要使用的SPI设备名称,为空则使用默认的SPI设备 * 程序功能:通过SPI设备读取 w25q 的 ID 数据 */ #include
#include
#define W25Q_SPI_DEVICE_NAME "W25Q128" static void spi_w25q_test(int argc, char *argv[]) { struct rt_spi_device *spi_dev_w25q; char name[RT_NAME_MAX]; rt_uint8_t w25x_read_id = 0x90; rt_uint8_t id[5] = {0}; if (argc == 2) { rt_strncpy(name, argv[1], RT_NAME_MAX); } else { rt_strncpy(name, W25Q_SPI_DEVICE_NAME, RT_NAME_MAX); } /* 查找 spi 设备获取设备句柄 */ spi_dev_w25q = (struct rt_spi_device *)rt_device_find(name); if (!spi_dev_w25q) { rt_kprintf("spi sample run failed! can't find %s device!\n", name); } else { // /* 方式1:使用 rt_spi_send_then_recv()发送命令读取ID */ // rt_spi_send_then_recv(spi_dev_w25q, &w25x_read_id, 1, id, 5); // rt_kprintf("use rt_spi_send_then_recv() read w25q ID is:%x%x\n", id[3], id[4]); /* 方式2:使用 rt_spi_transfer_message()发送命令读取ID */ struct rt_spi_message msg1, msg2; msg1.send_buf = &w25x_read_id; msg1.recv_buf = RT_NULL; msg1.length = 1; msg1.cs_take = 1; msg1.cs_release = 0; msg1.next = &msg2; msg2.send_buf = RT_NULL; msg2.recv_buf = id; msg2.length = 5; msg2.cs_take = 0; msg2.cs_release = 1; msg2.next = RT_NULL; rt_spi_transfer_message(spi_dev_w25q, &msg1); rt_kprintf("\r\nRead w25q ID is:%x%x\r\n", id[3], id[4]); } } /* 导出到 msh 命令列表中 */ MSH_CMD_EXPORT(spi_w25q_test, spi w25q sample); ``` list_device 查看w25q128是否挂载成功  TAB键查看测试程序是否进入RT-Thread shell commands列表中  #模块功能演示  bilibili演示地址https://www.bilibili.com/video/BV1cF41167M6/ #代码下载链接 https://gitee.com/want_to_be_a_vassal/numaker-m2354-spi_wdt_test.git #心得体会 在编写spi_flash_init.c中static int rt_hw_spi_flash_init(void)根据stm32模板修改出现了spi10挂在spi1成功,但是w25q128死活挂在不上去,程序一直卡顿如图  经过一顿仿真程序卡在sfud初始化配置w25q128 具体位置如图显示。在没有找到问题的时候一度怀疑我的焊接技术出了问题,或者这个片子已经坏掉了,为了排除问题不得不使用标准库函数裸驱动测试了一番。 测试证明焊接没有问题,测试都正常。经过一天多的查找没有头绪,最终确定stm32驱动模式不适合新唐,stm32挂在spi外设需要把具体的cs引脚传入到rt_spi_bus_attach_device,新唐只需要把spi1的基地址传入就可以,最终尝试初始化一遍引脚,结果可以了。  接着又出现rtt默认的频率超过新唐设定频率  修改频率  最后有个疑惑spi_w25q128_test 在没有带参数的情况出现如图情况,这个找到问题所在。大佬可以接货一下。 
5
条评论
默认排序
按发布时间排序
登录
注册新账号
关于作者
想当诸侯的小蚂蚁
这家伙很懒,什么也没写!
文章
7
回答
5
被采纳
0
关注TA
发私信
相关文章
1
BBB的SPI驱动
2
求个SPI上挂两个或多个设备的使用例子
3
SPI设备有个bug
4
spi flash 的fatfs使用一段时间后读写文件出现故障
5
SPI驱动
6
请教rt_spi_configure函数理解
7
SPI FLASH挂载的问题
8
SPI-FLASH 文件系统 SPIFFS
9
求助一个完整的 spi flash 驱动
10
关于同时使用文件系统与SPI FLASH的问题
推荐文章
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
DMA
USB
文件系统
RT-Thread
SCons
RT-Thread Nano
线程
MQTT
STM32
RTC
rt-smart
FAL
I2C_IIC
UART
ESP8266
cubemx
WIZnet_W5500
ota在线升级
PWM
BSP
flash
freemodbus
packages_软件包
潘多拉开发板_Pandora
定时器
ADC
GD32
flashDB
socket
编译报错
中断
Debug
rt_mq_消息队列_msg_queue
keil_MDK
ulog
SFUD
msh
C++_cpp
MicroPython
本月问答贡献
RTT_逍遥
10
个答案
3
次被采纳
xiaorui
3
个答案
2
次被采纳
winfeng
2
个答案
2
次被采纳
三世执戟
8
个答案
1
次被采纳
KunYi
8
个答案
1
次被采纳
本月文章贡献
catcatbing
3
篇文章
5
次点赞
lizimu
2
篇文章
8
次点赞
swet123
1
篇文章
4
次点赞
Days
1
篇文章
4
次点赞
YZRD
1
篇文章
2
次点赞
回到
顶部
发布
问题
投诉
建议
回到
底部