合宙Air103|SDIO (扩展) |LuatOS-SOC接口|官方demo|学习(15-3):SDIO - 文件读写操作
创始人
2024-05-25 01:46:18
0

基础资料

基于Air103开发板:🚗 Air103 - LuatOS 文档

上手:开发上手 - LuatOS 文档

探讨重点

对官方社区库接口SDIO库调用及示例进行复现及分析,了解SDIO模式读写SD卡的基本原理及操作方法。

软件及工具版本

LuatOS@AIR103 base 22.12 bsp V0015 32bit

硬件准备

Air103开发板1块,面包板1块,SPI/SDIO双功能版本读写模块,导线若干。

接线

 需采用:SPI/SDIO双功能版本读写模块


--[[接线要求:SDIO             使用6线接法Air103开发板         SDIO模块
GND                    GNDGPIO26(SDIO_02)       D02GPIO27                D03GPIO23(SDIO_CMD)     CMDGPIO22(SDIO_CK)      CLKGPIO24               D00GPIO25               D013.3V                 VCC

Demo代码及解析:


-- LuaTools需要PROJECT和VERSION这两个信息
PROJECT = "sdiodemo"
VERSION = "1.0.0"log.info("main", PROJECT, VERSION)-- sys库是标配
_G.sys = require("sys")--添加硬狗防止程序卡死
if wdt thenwdt.init(9000)--初始化watchdog设置为9ssys.timerLoopStart(wdt.feed, 3000)--3s喂一次狗
end--[[接线方式:Air103开发板         SDIO模块GND                    GND
GPIO26(SDIO_02)       D02
GPIO27			      D03
GPIO23(SDIO_CMD)    CMD
GPIO22(SDIO_CK)     CLK
GPIO24      		D00
GPIO25				D01
3.3V                   VCC
]]-- 特别提醒, 由于FAT32是DOS时代的产物, 文件名超过8个字节是需要额外支持的(需要更大的ROM)
-- 例如 /sd/boottime 是合法文件名, 而/sd/boot_time就不是合法文件名, 需要启用长文件名支持.local function fatfs_test()log.info("sdio", "call sdio.init")sdio.init(0)log.info("sdio", "call sdio.sd_mount")status=sdio.sd_mount(0, "/sd")log.info("sdio-status", status)local f = io.open("/sd/boottime.txt", "rb")local c = 0if f thenlocal data = f:read("*a")log.info("fs", "data", data, data:toHex())c = tonumber(data)f:close()endlog.info("fs", "boot count", c)c = c + 1f = io.open("/sd/boottime.txt", "wb")if f ~= nil thenlog.info("fs", "write c to file", c, tostring(c))f:write(tostring(c))f:close()elselog.warn("sdio", "mount not good?!")endif fs thenlog.info("fsstat", fs.fsstat("/"))log.info("fsstat", fs.fsstat("/sd"))end-- 测试一下追加, fix in 2021.12.21os.remove("/sd/test_a.txt")sys.wait(50)f = io.open("/sd/test_a.txt", "w")if f thenf:write("ABC")f:close()endf = io.open("/sd/test_a.txt", "a+")if f thenf:write("def")f:close()endf = io.open("/sd/test_a.txt", "r")if  f thenlocal data = f:read("*a")log.info("data", data, data == "ABCdef")f:close()end-- 测试一下按行读取, fix in 2022-01-16f = io.open("/sd/testline.txt", "w")if f thenf:write("abc\n")f:write("123\n")f:write("wendal\n")f:close()endsys.wait(100)f = io.open("/sd/testline.txt", "r")if f thenlog.info("sdio", "line1", f:read("*l"))log.info("sdio", "line2", f:read("*l"))log.info("sdio", "line3", f:read("*l"))f:close()end
endsys.taskInit(function()fatfs_test() -- 每次开机,把记录的数值+1while 1 dosys.wait(500)end
end)-- 用户代码已结束---------------------------------------------
-- 结尾总是这一句
sys.run()
-- sys.run()之后后面不要加任何语句!!!!!

LOG:

[2023-02-09 22:43:28.949] OK[2023-02-09 22:43:29.166] I/main auth ok 85104250303938373506004A521456060078 AIR103[2023-02-09 22:43:29.166] I/main LuatOS@AIR103 base 22.12 bsp V0015 32bit[2023-02-09 22:43:29.166] I/main ROM Build: Dec 24 2022 10:04:06[2023-02-09 22:43:29.171] D/main loadlibs luavm 180208 11680 12168[2023-02-09 22:43:29.171] D/main loadlibs sys   12288 5184 5184[2023-02-09 22:43:29.171] I/user.main  sdiodemo        1.0.0[2023-02-09 22:43:29.171] I/user.sdio   call sdio.init[2023-02-09 22:43:29.171] I/user.sdio   call sdio.sd_mount[2023-02-09 22:43:29.179] I/user.sdio-status        true[2023-02-09 22:43:29.179] I/user.fs       data 2       32    2[2023-02-09 22:43:29.179] I/user.fs       boot count      2[2023-02-09 22:43:29.213] I/user.fs       write c to file   3       3[2023-02-09 22:43:29.214] I/user.fsstat true 12    2       4096         lfs[2023-02-09 22:43:29.214] I/user.fsstat true 1932286  1932275  16    fatfs[2023-02-09 22:43:29.307] I/user.data  ABCdef    true[2023-02-09 22:43:29.417] I/user.sdio   line1 abc[2023-02-09 22:43:29.417] I/user.sdio   line2 123[2023-02-09 22:43:29.417] I/user.sdio   line3 wendal

PS:sdio - sdio

目前仅在Air103上成功验证

备注

本页文档由这个文件自动生成。如有错误,请提交issue或帮忙修改后pr,谢谢!

sdio.init(id)

初始化sdio

参数

传入值类型

解释

int

通道id,与具体设备有关,通常从0开始,默认0

返回值

返回值类型

解释

boolean

打开结果

例子


sdio.sd_read(id, offset, len)

直接读写sd卡上的数据

参数

传入值类型

解释

int

sdio总线id

int

偏移量,必须是512的倍数

int

长度,必须是512的倍数

返回值

返回值类型

解释

string

若读取成功,返回字符串,否则返回nil

例子

-- 初始化sdio并直接读取sd卡数据

sdio.init(0)

local t = sdio.sd_read(0, 0, 1024)

if t then

    --- xxx

end


sdio.sd_write(id, data, offset)

直接写sd卡

参数

传入值类型

解释

int

sdio总线id

string

待写入的数据,长度必须是512的倍数

int

偏移量,必须是512的倍数

返回值

返回值类型

解释

bool

若读取成功,返回true,否则返回false

例子

-- 初始化sdio并直接读取sd卡数据

sdio.init(0)

local t = sdio.sd_write(0, data, 0)

if t then

    --- xxx

end


sdio.sd_mount(id, path, auto_format)

挂载SD卡, 使用FATFS文件系统

参数

传入值类型

解释

int

sdio总线id

string

挂载路径, 默认”/sd”, 不允许以”/”结尾

bool

是否自动格式化,默认是true

返回值

返回值类型

解释

bool

挂载成功返回true,否则返回false

int

底层返回的具体结果码,用于调试

例子


sdio.sd_umount(id, path)

卸载SD卡(视硬件情况, 不一定支持)

参数

传入值类型

解释

int

sdio总线id

string

挂载路径, 默认”/sd”, 不允许以”/”结尾

返回值

返回值类型

解释

bool

挂载成功返回true,否则返回false

例子


sdio.sd_format(id)

格式化SD卡

参数

传入值类型

解释

int

sdio总线id

返回值

返回值类型

解释

bool

挂载成功返回true,否则返回false

例子

相关内容

热门资讯

监控摄像头接入GB28181平... 流程简介将监控摄像头的视频在网站和APP中直播,要解决的几个问题是:1&...
Windows10添加群晖磁盘... 在使用群晖NAS时,我们需要通过本地映射的方式把NAS映射成本地的一块磁盘使用。 通过...
protocol buffer... 目录 目录 什么是protocol buffer 1.protobuf 1.1安装  1.2使用...
在Word、WPS中插入AxM... 引言 我最近需要写一些文章,在排版时发现AxMath插入的公式竟然会导致行间距异常&#...
【PdgCntEditor】解... 一、问题背景 大部分的图书对应的PDF,目录中的页码并非PDF中直接索引的页码...
Fluent中创建监测点 1 概述某些仿真问题,需要创建监测点,用于获取空间定点的数据࿰...
educoder数据结构与算法...                                                   ...
MySQL下载和安装(Wind... 前言:刚换了一台电脑,里面所有东西都需要重新配置,习惯了所...
修复 爱普生 EPSON L4... L4151 L4153 L4156 L4158 L4163 L4165 L4166 L4168 L4...
MFC文件操作  MFC提供了一个文件操作的基类CFile,这个类提供了一个没有缓存的二进制格式的磁盘...