Development board: K210 AIRV R3 version widora
Development environment: kendryte IDE official
Required reference documents: Standalone SDK Programming Guide v0.5.0, and Widora schematic
(1) Create a new folder 08my_code_tfcard
To use SD CARD or TF CARD, my side is a TF card. If you use an SD card, it’s the same
Format the TF card in fat32 format. Don’t worry if it’s already in fat32 format.
Play 2 songs, 2 pictures, and 2 documents. I don’t know if the influence of Chinese will not affect it. Just do this first
Old rules gift + earth
Example -> Search -> Select Development -> Download, save to the newly created folder
Installation dependencies
Clean + build
After setting up the environment, modify the code below
(2) Modify the code
First look at the schematic
TF_CS -> IO29
TF_MOSI-> IO28
TF_CLK-> IO27
TF_MISO -> IO26
config -> device bind pins over there
First bind TF_CLK-> IO27 and TF_CS -> IO29
Let’s look at the main function and the pins are not configured. Let’s take a look at how to change the code
The three circled are to set the CPU frequency, initialize the interrupt and open the global interrupt. No need to analyze
Just look at sdcard_init directly
Only this sentence is useful, click in and have a look
F12 jump, ALT+Left key to go back to the previous step
Enter sd_lowlevel_init() to see
There is IO7 as the output here. I don’t know what it does for the time being, just ignore it, it will be useful later and come back and change it.
The following is configured with SPI0, the rate is 200K
Select the chip select terminal. Could it be that TF_CS -> IO29 configures this pin?
Then allocate IO29 to this
This is very important
SPI is in Chapter 16
spi_init(SPI_DEVICE_INDEX, SPI_WORK_MODE_0, SPI_FF_STANDARD, 8, 0);
Used SPI0 as the master device, mode 0, SPI_FF_STANDARD is a standard 3-wire, a single transmission of 8 bits, and the large and small endian 0 is the little endian
Assign TF_CS -> IO29 to SS3 again
The following pins are also bound here
TF_MOSI-> IO28
TF_MISO -> IO26
It seems that the CS signal of GPIOHS is not used
Don't look at other configurations
Here is to configure the SPI to 10M rate, the front is 200K
Clean + build + download to see if it works
Open the serial port, select the serial port connection, press reset on the development board
I have read my SD card
sd init =0 means that the SD card is initialized successfully (it means that the card is read)
The latter is the SD card with a capacity of 32G and a block size of 512
Power off the development board, pull out the TF card, and plug the card reader into the computer
It means that it can be written to the SD card normally
Safely exit the U disk, reinstall the development board and power on again
Open back to the serial port to share file system parameters
The file system above reads the file name of my TF card, Chinese displays garbled characters, and English displays normally, but I don’t care about it.
Open the text of cannan/test.txt, read 1024 bytes, then proof the 64 bytes, and finally enter 1024 A,
-------------------------------------------------------------------------------------------------------------------------------
This shows that our SD card is no problem to read and write
Summarize the SD card SPI mode here
The SPI in the schematic is 4-wire, but in fact, when we configure it, only 3-wire SPI is configured instead of 4-wire.
If you modify the 4-wire, you need to change the spi_receive_data_standard these SPI receiving and sending functions to
The corresponding pins configured here are
TF_CS -> IO29
TF_MOSI-> IO28
TF_CLK-> IO27
TF_MISO -> IO26
This GPIOHS high-speed pin is not used
After I deleted the GPIO, it worked normally
Let me take the card reader, delete the cann folder and reinsert it. Reset
It will show an error in reading the content of the txt file. This is normal because it is written in the last one.
We press reset
It displays normally
TF card can be hot-swappable, just use it as a U disk
I don’t understand the specific configuration process of the SD card. I only use it. I know how to modify it. The transplantation of the TF/SD card in this section is complete. See you next time.
Because the main.c code has not been modified, the code is not posted anymore
Mainly modified on SDCARD.C
So this time, let’s connect to download
Link: https://pan.baidu.com/s/1v3IbitrwVWPVlWrfx8N9xw
Extraction code: x2rr
After thinking about it, although I don't know the file system, I still have to learn
Here is to initialize the file system, and then bind 0: there to the found SD card drive letter, and output status = 0 (OK) when it is found;
Here is to print out the file name of the root directory of the SD/TF card until the last one is printed, then close the root directory and no longer read it.
Then a file system is defined here called file
dir is the file name
After learning Linux, mkdir knows that it is to create a file, the name is the pointer corresponding to dir (pointing to the string "cannan")
(If yes, return err error to indicate that there is already)
I modified it here and changed the folder to my CSDN name
Here is to traverse the txt file to see how many characters are hidden in it (it seems to be the end of EOF)
Then use open to open the file of the path string. The permission is read, and the read is given to the file file system (FIL file;)
80 char caches are used, the following f_read is to read the first 64, and v_ret_len points to the first character read
Finally, f_close closes the file, (open must have close, otherwise there will be insufficient memory)
The file system is opened here. The permission to open is modify (create) + write
1024 pieces of data are prepared and written to the read path (here is a txt file)
Then open the file and close the file after writing (open must write close or leak)
I changed the test a bit here
Note that the ascii code seems to be 0~255, there are things before 127, and some have different standards later, and some don’t. There must be something before I use 127 here.
Go one-stop
First open the serial port
Clean+build+download
I rely on, this Nima created UTF-16 for me and left it alone, this issue is just like that
No, I will change it
Take a look
Still something is wrong, but I won’t change it.
see you later
Link: https://pan.baidu.com/s/1roaFbSgv4uDA_Jq7JuLdSg
Extraction code: 01hn