SD Card Logging
LOS SD Driver: calls the FatFS middleware. This implements the generic storage device interface.
LOS SD Driver SPI Implementation: this layer implements the SPI communication required for the FatFS middleware to communicate with the SD card
Implementation: https://controllerstech.com/sd-card-using-spi-in-stm32/
Example usage of the LOS SD Driver (...
indicates other parts of the code in a stm32 project)
#include "LOS_D_sd.hpp"
...
int main(void)
{
...
/* USER CODE BEGIN 2 */
uint8_t res;
SDCard sd;
StorageDevice* storage_device = &sd;
const char* file_name = "test.txt";
const char* write_data = "test\ntest";
size_t write_data_size = strlen(write_data);
char read_data[100];
size_t file_size;
/* Check if test file 1 exists */
bool file_exist = storage_device->checkExist(file_name);
if (file_exist) {
myprintf("File exists!\r\n");
} else {
myprintf("File does not exist!\r\n");
}
/* Write to file (will create the file first if it does not exist) */
res = storage_device->write(file_name, write_data, write_data_size);
if (res != 0) {
myprintf("Failed to write to file! Error code = %d\r\n", res);
}
/* Get file size */
file_size = storage_device->length(file_name);
myprintf("File size: %d\r\n", file_size);
/* Read from file */
memset(read_data, 0, 100);
res = storage_device->read(file_name, read_data, file_size);
if (res == 0) {
myprintf("Read file content:\r\n%s\r\n", (char*)readData);
} else {
myprintf("Failed to read from file! Error code = %d\r\n", res);
}
/* Perform I/O operation on another file */
const char* file_name_2 = "test2.txt";
res = storage_device->write(file_name_2, write_data, write_data_size);
if (res != 0) {
myprintf("Failed to write to another file! Error code = %d\r\n", res);
}
/* Clean up SD card */
res = storage_device->cleanup();
if (res != 0) {
myprintf("Failed to clean up SD card! Error code = %d\r\n", res);
}
/* USER CODE END 2 */
...
}
...