SDIO - Edit files in SD card
Preparation
- Ameba x 1
- SD card or MicroSD card x 1
- SD sniffer x 1 (optional)
Example
Ameba can act as SD Host to handle applications related to SD devices. For instance, this example use Ameba to perform read/write operations on SD card.
Ameba uses the commonly used FAT format, therefore, the data written by Ameba can be accessed through computer easily.
We can connect Ameba to SD card through wires directly. The figure below shows SD card pins:
We can weld wires on these pins and connect to Ameba:
Note: D1 & D0 (correspond to WP & CD) need to be connected to GND. SD host determines status of the SD card according these two pins.
WP & CD staus:
Low: GND
High: Vcc
Instead of welding, there are some transition board can be used to connect Ameba and SD card:
SD Sniffer:
https://www.sparkfun.com/products/11468
SD Breakout:
https://www.sparkfun.com/products/12941
No matter the SD card is connected via welding or transition board, the connecting wire should not be too long, since the wire length affects transmission quality. In the example, we use a 20cm dupont wire.
Below is the wiring diagram of SD transition board:
The wiring of MicroSD card is similar. The figure below shows MicroSD card pins:
Afterwards, open the sample code in “File” -> “Examples” -> “AmebaSdFatFs” -> “file_read_write”
In the sample code, we first create a text file “test.txt” with content “hello world!”, then read the file and print content to serial monitor.
Next, insert SD card into card reader, and check whether the operations succeeded.
WP | CD | Description |
High | High | SD card is not inserted. |
HIGH | LOW | SD card inserted, write pretected. |
LOW | LOW | SD card inserted, without write protected. |
Code reference
In the sample code, first we use a SdFatFs object to manage the FAT file system in the SD card.
SdFatFs fs;Next, user begin() to initialize FatFs, including the initialization of SD Host and mounting the file system.
fs.begin()Use open() to open the file, the argument requires absolute path of the file to be opened. The return value contains a SdFatFile object which is use to manipulate the opened file.
SdFatFile file = fs.open(absolute_filename);SdFatFile class inherits Stream class, therefore we can use println to write to a file.
file.println(write_content);Then close the file.
file.close()To read from the file:
file.read(buf, sizeof(buf));Finally, close the FAT file system:
fs.end();
Realtek IoT/Wi-Fi MCU Solutions . All Rights Reserved. Terms of Use