Hello, I have similiar problem with CMSIS RTOS and SD card library. My attempt was to have thread which add line of data to file on SD card each 1 second. It is as kind of log for logging data. Sometimes ( I think dependent on placement of FILE *fp = fopen("/sd/_log/_log_001.txt", "a"); statement - in thread or in main)
Here is my very testing code
#include "mbed.h"
#include "cmsis_os.h"
#include "SDFileSystem.h"
DigitalOut led1(LED1);
DigitalOut led2(LED2);
SDFileSystem sd(p5, p6, p7, p8, "sd");
// FILE *fp;
void save_fake_data() {
FILE *fp = fopen("/sd/_log/_log_001.txt", "a");
fseek (fp, 0, SEEK_END);
fprintf(fp, "\nVoltage: V\n\r");
fclose (fp);
}
void led2_thread(void const *argument) {
while (true) {
led2 = !led2;
osDelay(1000);
}
}
void write_SD_thread(void const *argument) {
while (true) {
led1 = !led1;
save_fake_data();
// osDelay(1000);
}
}
osThreadDef(led2_thread, osPriorityNormal, 1, DEFAULT_STACK_SIZE);
osThreadDef(write_SD_thread, osPriorityNormal, 1, DEFAULT_STACK_SIZE);
int main (void) {
osThreadCreate(osThread(write_SD_thread), NULL);
osThreadCreate(osThread(led2_thread), NULL);
// wait(1);
// FILE *fp = fopen("/sd/_rec/_rec_001.txt", "a");
// if (fp == NULL) {
// printf("Could not open file for write\n");
// }
//
// while (!fopen());
// mkdir("/sd/_log", 0777);
// fclose (fp);
// fseek ( fp , 0 , SEEK_END );
// fprintf(fp, "\nVoltage: V\n\r");
// fprintf(fp, "\n\r Current: A \n\r");
// fprintf(fp, "Number of cycles: U\n\r");
// printf("Current: A\n\r");
}
In one case after restart all leds started flashing in pairs like runtime error but data was written - only once but written.
In second case there was no flashing leds but no data was written.
Please can anybody point me to right direction? Is it fprintf()? and I would write data to file char by char with say fputc()?
Can this happen because SD card library and fat filesystem library isn't made for use with RTOS?
Thank You for any help.
Whenever I try to use fopen to read a file from the USB filesystem, while using RTOS threading, the mbed immediately goes to siren lights.
I would use the internal filesystem, but I need to stream audio, and when I used fopen on a file on the internal filesystem, the program would just hang. If there is a workaround to either problem, please let me know.
Thanks