14 TF卡读写案例
1 案例简介
本案例主要演示如何将TF卡挂载到Linux文件系统,以及对TF卡进行读写操作和文件管理。
2 挂载TF卡
root@linaro-alip:/# fdisk -l
···
Disk /dev/mmcblk1: 29.72 GiB, 31914983424 bytes, 62333952 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: 23000000-0000-4C4A-8000-699000005ABB
查看TF卡的分区信息,mmcblk1p1 是TF卡设备的一个分区,可挂载到linux系统。
root@linaro-alip:/# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
mmcblk1 179:0 0 7.4G 0 disk
└─mmcblk1p1 179:1 0 7.4G 0 part
创建挂载点并挂载设备:
sudo mkdir /mnt/tfcard
sudo mount /dev/mmcblk1p1 /mnt/tfcard
查看挂载结果,已成功挂载
root@linaro-alip:/# df -h
………
/dev/mmcblk1p1 7.5G 16K 7.5G 1% /mnt/tfcard
3 案例操作流程
成功挂载TF卡后,可根据以下操作流程对TF卡进行读写和文件管理。
打开终端,将本案例 bin 目录下(05-开发资料\软件开发资料\linux_demo\smdt_TF_demo\bin)的可执行程序 smdt_TF 拷贝至开发板文件系统 (源码可以在 src 路径下查看)。
#修改 Linux 内核日志的显示级别,内核的日志级别被设置为只显示紧急或更高级别的消息
echo 1 4 1 7 > /proc/sys/kernel/printk
在终端执行如下指令,切换到 smdt_TF 可执行程序所在目录
#切换到 smdt_TF 可执行程序所在目录
cd ‘可执行文件所在目录’
#查看 smdt_TF 是否在该目录下
ls
若可执行文件 smdt_TF 在当前目录下,则修改可执行文件的权限
#修改文件权限
chmod 777 smdt_TF
#查询是否修改成功
ls -ld smdt_TF
查询确认文件修改权限成功后,执行 ./smdt_TF -h 可以查看该程序的帮助信息
root@linaro-alip:/# ./TF -h
Usage:
-w "<text>" <file> Write <text> to <file>
-r <file> Read and print the content of <file>
-d <file> Delete the specified <file>
-ls List all files and directories in the TF card
-h Show this help message
对TF卡进行写操作
root@linaro-alip:/# ./TF -w "HELLO SMDT" test.txt
Data written to /mnt/tfcard/test.txt
查看TF卡中的文件,可看到test.txt已经被创建
root@linaro-alip:/# ./TF -ls
Files and directories in /mnt/tfcard:
System Volume Information
test.txt
读取写入文件,与写入的信息一致
root@linaro-alip:/# ./TF -r test.txt
Read from file: HELLO SMDT
删除文件,查看挂载目录可看到文件已被删除。
root@linaro-alip:/# ./TF -d test.txt
File /mnt/tfcard/test.txt deleted successfully.
root@linaro-alip:/# ./TF -ls
Files and directories in /mnt/tfcard:
System Volume Information
最后可卸载设备
umount /mnt/TF_Card