TF Card Read/Write Demo
1 Demo Introduction
This demo mainly demonstrates how to mount a TF card into the Linux filesystem, perform read/write operations on the TF card, and manage files.
2 Mount the TF Card
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-699000005ABBView the partition information of the TF card. mmcblk1p1 is a partition of the TF card device and can be mounted into the Linux system.
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 partCreate a mount point and mount the device:
sudo mkdir /mnt/tfcard
sudo mount /dev/mmcblk1p1 /mnt/tfcardView the mount result; it has been mounted successfully.
root@linaro-alip:/# df -h
………
/dev/mmcblk1p1 7.5G 16K 7.5G 1% /mnt/tfcard3 Demo Procedure
After successfully mounting the TF card, follow the procedure below to perform read/write operations and file management on the TF card.
Open a terminal and copy the executable smdt_TF from the bin directory of this demo (05-开发资料\软件开发资料\linux_demo\smdt_TF_demo\bin) to the development board's filesystem (the source code can be found in the src directory).
#修改 Linux 内核日志的显示级别,内核的日志级别被设置为只显示紧急或更高级别的消息
echo 1 4 1 7 > /proc/sys/kernel/printkIn the terminal, run the following commands to switch to the directory containing the smdt_TF executable.
#切换到 smdt_TF 可执行程序所在目录
cd ‘可执行文件所在目录’
#查看 smdt_TF 是否在该目录下
lsIf the executable smdt_TF is in the current directory, modify the executable's permissions.
#修改文件权限
chmod 777 smdt_TF
#查询是否修改成功
ls -ld smdt_TFAfter confirming that the file permissions have been successfully modified, run ./smdt_TF -h to view the program's help information.
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 messagePerform a write operation on the TF card.
root@linaro-alip:/# ./TF -w "HELLO SMDT" test.txt
Data written to /mnt/tfcard/test.txtView the files in the TF card. You can see that test.txt has been created.
root@linaro-alip:/# ./TF -ls
Files and directories in /mnt/tfcard:
System Volume Information
test.txtRead the written file; it matches the written information.
root@linaro-alip:/# ./TF -r test.txt
Read from file: HELLO SMDTDelete the file. Check the mount directory and you can see that the file has been deleted.
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 InformationFinally, unmount the device.
umount /mnt/TF_Card