Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
K210
软件模拟spi
10
K210 模拟SPI 无法挂载SD卡
发布于 2022-09-23 21:46:14 浏览:683
订阅该版
K210由于硬件只有一个可以用的SPI,所以想模拟一个。现在是SPI时序有点问题,逻辑分析仪也用上了,发现MISO数据一直是1 ,代码如下: ```c /* * Change Logs: * Date Author Notes * 2022-09-19 xiaoyu first version */ #include "board.h" #if 1 #include
#include
#include
#include
#include "gpio.h" #define DBG_TAG "softspi" #define DBG_LVL DBG_INFO #include
static struct k210_soft_spi_config soft_spi_config[] ={ #ifdef BSP_USING_SOFT_SPI SOFT_SPI_BUS_CONFIG, #endif }; #define BSP_USING_SOFT_SPI #define BSP_SOFT_SPI_CLK_PIN 27 #define BSP_SOFT_SPI_MOSI_PIN 28 #define BSP_SOFT_SPI_MISO_PIN 26 #define RT_USING_SOFT_SPI_CS0 #define RT_USING_SOFT_SPI_CS0_PIN 29 // #define SOFT_SPI_CS (RT_USING_SOFT_SPI_CS0_PIN-FUNC_GPIOHS0) // #define SOFT_SPI_SCLK (BSP_SOFT_SPI_CLK_PIN-FUNC_GPIOHS0) // #define SOFT_SPI_MOSI (BSP_SOFT_SPI_MOSI_PIN-FUNC_GPIOHS0) // #define SOFT_SPI_MISO (BSP_SOFT_SPI_MISO_PIN-FUNC_GPIOHS0) struct soft_spi_dev { struct rt_spi_bus *spi_bus; }; static struct soft_spi_dev *spi_dev; static struct k210_soft_spi soft_spi_bus_obj[sizeof(soft_spi_config) / sizeof(soft_spi_config[0])] = {0}; /*设置从设备使能 */ static void soft_spi_cs_enable(int enable) { if(enable) { gpiohs_set_pin(RT_USING_SOFT_SPI_CS0_PIN,GPIO_PV_LOW); } else { gpiohs_set_pin(RT_USING_SOFT_SPI_CS0_PIN,GPIO_PV_HIGH); } } static rt_err_t k210_spi_init() { fpioa_set_function(RT_USING_SOFT_SPI_CS0_PIN,RT_USING_SOFT_SPI_CS0_PIN + FUNC_GPIOHS0); fpioa_set_function(BSP_SOFT_SPI_MOSI_PIN,BSP_SOFT_SPI_MOSI_PIN + FUNC_GPIOHS0); fpioa_set_function(BSP_SOFT_SPI_MISO_PIN,BSP_SOFT_SPI_MISO_PIN + FUNC_GPIOHS0); fpioa_set_function(BSP_SOFT_SPI_CLK_PIN,BSP_SOFT_SPI_CLK_PIN + FUNC_GPIOHS0); gpiohs_set_drive_mode(BSP_SOFT_SPI_CLK_PIN, GPIO_DM_OUTPUT); gpiohs_set_drive_mode(BSP_SOFT_SPI_MOSI_PIN, GPIO_DM_OUTPUT); gpiohs_set_drive_mode(RT_USING_SOFT_SPI_CS0_PIN, GPIO_DM_OUTPUT); gpiohs_set_drive_mode(BSP_SOFT_SPI_MISO_PIN,GPIO_DM_INPUT); gpiohs_set_pin(BSP_SOFT_SPI_CLK_PIN, GPIO_PV_LOW); gpiohs_set_pin(RT_USING_SOFT_SPI_CS0_PIN, GPIO_PV_HIGH); return RT_EOK; } static inline void spi_delay(uint8_t time) { } void SPI_CLK (int data) { if(data == 0) { gpiohs_set_pin(BSP_SOFT_SPI_CLK_PIN,GPIO_PV_LOW); } else { gpiohs_set_pin(BSP_SOFT_SPI_CLK_PIN,GPIO_PV_HIGH); } } void SPI_MOSI (int data) { if(data == 0) { gpiohs_set_pin(BSP_SOFT_SPI_MOSI_PIN,GPIO_PV_LOW); } else { gpiohs_set_pin(BSP_SOFT_SPI_MOSI_PIN,GPIO_PV_HIGH); } } int SPI_MISO () { //rt_kprintf("SPI_MISO :%d\n\r", gpiohs_get_pin(BSP_SOFT_SPI_MISO_PIN)); return gpiohs_get_pin(BSP_SOFT_SPI_MISO_PIN); } void SPI_Delay() { usleep(10); } /* 写一个字节 */ static void soft_spi_WriteByte(uint8_t data) { uint8_t i = 0; uint8_t temp = 0; for(i=0; i<8; i++) { temp = ((data&0x80)==0x80)? 1:0; data = data<<1; SPI_CLK(0); //CPOL=0 SPI_Delay(); SPI_MOSI(temp); SPI_Delay(); SPI_CLK(1); //CPHA=0 SPI_Delay(); } SPI_CLK(0); } /* 读写一个字节 */ static uint8_t soft_spi_ReadWriteByte(uint8_t data) { uint8_t i = 0; uint8_t temp = 0; uint8_t redata = 0xFF; SPI_Delay(); for(i=0;i<8;i++) { if(data & 0x80) { SPI_MOSI(1); } else { SPI_MOSI(0); } data <<= 1; SPI_Delay(); SPI_CLK(1); redata<<=1; if(SPI_MISO()) { redata++; } SPI_Delay(); SPI_CLK(0); // temp = ((data&0x80)==0x80)? 1:0; // data = data<<1; // read_data = read_data<<1; // SPI_CLK(0); // SPI_Delay(); // SPI_MOSI(temp); // SPI_Delay(); // SPI_CLK(1); // SPI_Delay(); // if(SPI_MISO()==1) // { // read_data = read_data + 1; // } } return redata; } rt_err_t _soft_spi_configure(struct rt_spi_device *dev,struct rt_spi_configuration *cfg) { return RT_EOK; } /* 模拟 SPI 传输数据 */ rt_uint32_t _soft_spi_xfer(struct rt_spi_device* device, struct rt_spi_message* message) { struct rt_spi_configuration * config = &device->config; rt_uint32_t size = message->length; /* 设置 CS */ if(message->cs_take) { soft_spi_cs_enable(1); } const rt_uint8_t * send_ptr = message->send_buf; rt_uint8_t * recv_ptr = message->recv_buf; while(size--) { rt_uint8_t data = 0xFF; if(send_ptr != RT_NULL) { data = *send_ptr++; //rt_kprintf("send_ptr:%02x\n",data); // 发送数据 soft_spi_ReadWriteByte(data); } // 接收数据 if(recv_ptr != RT_NULL) { // 发送数据 data = soft_spi_ReadWriteByte(data); *recv_ptr++ = data; //rt_kprintf("recv_ptr:%02x\n",data); } } /* 设置 release CS */ if(message->cs_release) { soft_spi_cs_enable(0); } //rt_kprintf("len=%d \n",message->length); return message->length; } static struct rt_spi_ops soft_spi_ops = { .configure = _soft_spi_configure, .xfer = _soft_spi_xfer }; int rt_soft_spi_bus_register(char *name) { int result = RT_EOK; struct rt_spi_bus *spi_bus = RT_NULL; if(spi_dev) { return RT_EOK; } spi_dev = rt_malloc(sizeof(struct soft_spi_dev)); if(!spi_dev) { rt_kprintf("[soft spi]:malloc memory for spi_dev failed\n"); result = -RT_ENOMEM; goto _exit; } memset(spi_dev,0,sizeof(struct soft_spi_dev)); spi_bus = rt_malloc(sizeof(struct rt_spi_bus)); if(!spi_bus) { rt_kprintf("[soft spi]:malloc memory for spi_bus failed\n"); result = -RT_ENOMEM; goto _exit; } memset(spi_bus,0,sizeof(struct rt_spi_bus)); spi_bus->parent.user_data = spi_dev; rt_spi_bus_register(spi_bus, name, &soft_spi_ops); return result; _exit: if (spi_dev) { rt_free(spi_dev); spi_dev = RT_NULL; } if (spi_bus) { rt_free(spi_bus); spi_bus = RT_NULL; } return result; } static struct rt_spi_device *soft_spi_device = RT_NULL; int rt_soft_spi_device_init(void) { k210_spi_init(); int result = RT_EOK; rt_kprintf("[soft spi]:rt_soft_spi_device_init \n"); if(soft_spi_device) { return RT_EOK; } soft_spi_device = rt_malloc(sizeof(struct rt_spi_device)); if(!soft_spi_device) { rt_kprintf("[soft spi]:malloc memory for soft spi_device failed\n"); result = -RT_ENOMEM; } memset(soft_spi_device,0,sizeof(struct rt_spi_device)); /* 注册 SPI BUS */ result = rt_soft_spi_bus_register("soft_spi"); if(result != RT_EOK) { rt_kprintf("[soft spi]:register soft spi bus error : %d !!!\n",result); goto _exit; } /* 绑定 CS */ result = rt_spi_bus_attach_device(soft_spi_device,"spi188","soft_spi",NULL); if(result != RT_EOK) { rt_kprintf("[soft spi]:attact spi bus error :%d !!!\n",result); goto _exit; } rt_kprintf("[soft spi]:rt_soft_spi_device init ok\n"); return RT_EOK; _exit: if(soft_spi_device) { rt_free(soft_spi_device); soft_spi_device = RT_NULL; } return result; } INIT_APP_EXPORT(rt_soft_spi_device_init); #endif ``` ```c /* * Copyright (c) 2006-2018, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes */ #include
#ifdef BSP_USING_SDCARD #if defined(RT_USING_SPI_MSD) && defined(RT_USING_DFS_ELMFAT) #include
#include
#define DBG_TAG "sdcard" #define DBG_LVL DBG_INFO #include
int sd_mount(void) { int ret = 0; ret = msd_init("sd0", "spi188"); if(RT_EOK == ret) { if(dfs_mount("sd0", "/", "elm", 0, 0) == 0) { LOG_I("Mount /sd0 successfully"); return RT_EOK; } else { LOG_E("Mount fail !!1"); return -1; } } LOG_E("msd_init fail !!!"); return -2; } INIT_APP_EXPORT(sd_mount); #endif #endif ``` 自带解析的逻辑分析仪 ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20220923/4b409be8cb84ed11fb9924500f8cecf2.png.webp) ** 现在是MISO 一直是0xFF**
查看更多
3
个回答
默认排序
按发布时间排序
xiaoyu
2022-09-27
这家伙很懒,什么也没写!
已解决 代码自己问题
aozima
2022-09-23
调网络不抓包,调I2C等时序不上逻辑分析仪,就像电工不用万用表!多用整理的好的文字,比截图更省流量,还能在整理过程中思考。
不是总共有4个吗?虽然特性有所不同,合理分配下应该可以用吧?
嚜軒公告
2022-09-24
努力中
你这程序里面不是只有写的没有读的吗
撰写答案
登录
注册新账号
关注者
0
被浏览
683
关于作者
xiaoyu
这家伙很懒,什么也没写!
提问
27
回答
27
被采纳
1
关注TA
发私信
相关问题
1
K210 esp32 初始化完成后程序崩溃
2
K210 BSP 在线调试有问题,裸跑官方没问题
3
关于交叉编译工具链路径问题
4
关于K210使用RW007的注意
5
如何在k210上移植rt-thread nano
6
RTT下K210两路SPI不能同时使用问题,求大神
7
K210平台,开启NTP功能后。stack overflow
8
K210 交流一起共建生态
9
K210 spi驱动问题
10
请教K210编译报错
推荐文章
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组件
最新文章
1
【RT-Thread】记录一次对主仓的bsp进行修复并提交pr的总结
2
【请投票】嵌入式大赛Top10排名等你来选!
3
【24嵌入式设计大赛】HPM5300-EVK 网络测速仪
4
RT-Thread Nano 上线ST CubeMX
5
Nordic移植笔记: RT_Thread v5.1.0(基于Nordic nRF52840的ble_app_hids_mouse工程)
热门标签
RT-Thread Studio
串口
Env
LWIP
SPI
AT
Bootloader
Hardfault
CAN总线
ART-Pi
FinSH
USB
DMA
文件系统
RT-Thread
SCons
RT-Thread Nano
线程
MQTT
STM32
RTC
rt-smart
FAL
ESP8266
I2C_IIC
WIZnet_W5500
ota在线升级
UART
flash
packages_软件包
cubemx
PWM
freemodbus
BSP
潘多拉开发板_Pandora
定时器
ADC
GD32
flashDB
socket
中断
Debug
编译报错
msh
rt_mq_消息队列_msg_queue
keil_MDK
C++_cpp
ulog
at_device
SFUD
本月问答贡献
出出啊
1515
个答案
342
次被采纳
小小李sunny
1438
个答案
289
次被采纳
张世争
786
个答案
168
次被采纳
crystal266
546
个答案
161
次被采纳
whj467467222
1222
个答案
148
次被采纳
本月文章贡献
出出啊
1
篇文章
6
次点赞
小小李sunny
1
篇文章
1
次点赞
张世争
1
篇文章
2
次点赞
crystal266
2
篇文章
1
次点赞
whj467467222
2
篇文章
1
次点赞
回到
顶部
发布
问题
分享
好友
手机
浏览
扫码手机浏览
投诉
建议
回到
底部