Writing data to SD Card with SPI

Hi, I have a lot of questions.

Firstly I have a program that is example of SDBlockDevice - API references and tutorials | Mbed OS 6 Documentation

main.cp is :
#include “mbed.h”
#include “SDBlockDevice.h”

// Instantiate the SDBlockDevice by specifying the SPI pins connected to the SDCard
// socket. The PINS are:
// MOSI (Master Out Slave In)
// MISO (Master In Slave Out)
// SCLK (Serial Clock)
// CS (Chip Select)
uint8_t block[512] = “Hello World!\n”;

int main()
// Call the SDBlockDevice instance initialisation method
if (0 != sd.init()) {
printf(“Init failed \n”);
return -1;
printf(“sd size: %llu\n”, sd.size());
printf(“sd read size: %llu\n”, sd.get_read_size());
printf(“sd program size: %llu\n”, sd.get_program_size());
printf(“sd erase size: %llu\n”, sd.get_erase_size());

// Set the frequency
if (0 != sd.frequency(5000000)) {
    printf("Error setting frequency \n");

if (0 != sd.erase(0, sd.get_erase_size())) {
    printf("Error Erasing block \n");

// Write data block to the device
if (0 == sd.program(block, 0, 512)) {
    // Read the data block from the device
    if (0 == sd.read(block, 0, 512)) {
        // Print the contents of the block
        printf("%s", block);

// Call the SDBlockDevice instance de-initialisation method


It is working. Ouput is :

I’dont know which format is written data on SD Card.

1- How can I Write data that what I want. For example I want to write a .txt file on the SD card memory.

2- How can I Read this data when I insert the SD Card to the Computer.

Thank you!


you have to combinate BlockDevice with FATFileSystem - API references and tutorials | Mbed OS 6 Documentation

BR, Jan

Is there any example of this?

Yes, it is on the API page what I posted above. In default it is set to LittleFileSystem, but when you check the comments in the code you will see what is necessary to be done for set it to FATFileSystem.

BR, Jan