14 TF card reading and writing case
1 Case Introduction
This case mainly demonstrates how to mount a TF card to the Linux file system, and perform read and write operations and file management on the TF card.
2 Mount 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-699000005ABB
Check the partition information of the TF card. mmcblk1p1 is a partition of the TF card device and can be mounted to 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 part
Create a mount point and mount the device:
sudo mkdir /mnt/tfcard
sudo mount /dev/mmcblk1p1 /mnt/tfcard
Check the mount result, it has been successfully mounted
root@linaro-alip:/# df -h
………
/dev/mmcblk1p1 7.5G 16K 7.5G 1% /mnt/tfcard
3 Case operation process
After successfully mounting the TF card, you can read, write and manage files on the TF card according to the following operation process.
Open the terminal and copy the executable program smdt_TF in the bin directory of this case (05-Development Materials\Software Development Materials\linux_demo\smdt_TF_demo\bin) to the development board file system (the source code can be viewed in the src path).
#Modify the display level of Linux kernel logs. The kernel log level is set to show only emergency or higher-priority messages.
echo 1 4 1 7 > /proc/sys/kernel/printk
Execute the following command in the terminal to switch to the directory where the smdt_TF executable program is located
#切换到 smdt_TF 可执行程序所在目录
cd ‘the file's directory’
#查看 smdt_TF 是否在该目录下
ls
If the executable file smdt_TF is in the current directory, modify the permissions of the executable file
#修改文件权限
chmod 777 smdt_TF
#查询是否修改成功
ls -ld smdt_TF
After confirming that the file modification permission is successful, execute ./smdt_TF -h to view the help information of the program.
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
Write to TF card
root@linaro-alip:/# ./TF -w "HELLO SMDT" test.txt
Data written to /mnt/tfcard/test.txt
Check the files in the TF card and you can see that test.txt has been created.
root@linaro-alip:/# ./TF -ls
Files and directories in /mnt/tfcard:
System Volume Information
test.txt
Read and write files, consistent with the information written
root@linaro-alip:/# ./TF -r test.txt
Read from file: HELLO SMDT
Delete the file and check the mounted directory to 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 Information
Finally, you can uninstall the device
umount /mnt/TF_Card