Anyone know why the CMSIS RTOS kills the fopen for the localfilesystem?

10 Aug 2012

I'm trying to cram my file parsing stuff into a function that gets called from a cmsis thread...

#include "mbed.h"
#include "iostream"
#include "string.h"
#include "stdio.h"
using namespace std;

LocalFileSystem local("local");

int readFile() {
    wait(1);
    FILE *fp = fopen( "/local/myFile.txt", "r");

...etc.....etc....

As soon as the fopen is called.... the thread hangs... Anyone have a direction to point me? Thanks in advance!!

14 Aug 2012

Hi Mark,

Mark x wrote:

I'm trying to cram my file parsing stuff into a function that gets called from a cmsis thread...

As soon as the fopen is called.... the thread hangs... Anyone have a direction to point me? Thanks in advance!!

This program is testing every functionality of the local file system within an mbed-rtos thread and it is working fine, you can use it as a reference:

Import program

00001 #include "mbed.h"
00002 #include "rtos.h"
00003 
00004 void led_blink(PinName led_name) {
00005     DigitalOut led(led_name);
00006     while (1) {
00007         Thread::wait(1000);
00008         led = !led;
00009     }
00010 }
00011 
00012 void notify_completion(bool success) {
00013     if (success) {
00014         printf("{success}\n");
00015     } else {
00016         printf("{failure}\n");
00017     }
00018     
00019     printf("{end}\n");
00020     led_blink(success?LED2:LED4);
00021 }
00022 
00023 #define FILENAME      "/local/out.txt"
00024 #define TEST_STRING   "Hello World!"
00025 
00026 FILE* test_open(const char* mode) {
00027     FILE *f;
00028     f = fopen(FILENAME, mode);
00029     if (f == NULL) {
00030         printf("Error opening file\n");
00031         notify_completion(false);
00032     }
00033     
00034     return f;
00035 }
00036 
00037 void test_write(FILE* f, char* str, int str_len) {
00038     int n = fprintf(f, str);
00039     if (n != str_len) {
00040         printf("Error writing file\n");
00041         notify_completion(false);
00042     }
00043 }
00044 
00045 void test_read(FILE* f, char* str, int str_len) {
00046     int n = fread(str, sizeof(unsigned char), str_len, f);
00047     if (n != str_len) {
00048         printf("Error reading file\n");
00049         notify_completion(false);
00050     }
00051 }
00052 
00053 void test_close(FILE* f) {
00054     int rc = fclose(f);
00055     if (rc != 0) {
00056         printf("Error closing file\n");
00057         notify_completion(false);
00058     }
00059 }
00060 
00061 void test_localfilesystem(void const *argument) {
00062     LocalFileSystem local("local");
00063     
00064     FILE *f;
00065     char* str = TEST_STRING;
00066     char* buffer = (char*) malloc(sizeof(unsigned char)*strlen(TEST_STRING));
00067     int str_len = strlen(TEST_STRING);
00068     
00069     // Write
00070     f = test_open("w");
00071     test_write(f, str, str_len);
00072     test_close(f);
00073     
00074     // Read
00075     f = test_open("r");
00076     test_read(f, buffer, str_len);
00077     test_close(f);
00078     
00079     // Check the two strings are equal
00080     notify_completion((strncmp(buffer, str, str_len) == 0));
00081 }
00082 
00083 int main() {
00084     Thread t(test_localfilesystem);
00085     led_blink(LED1);
00086 }

HTH, Emilio

10 Aug 2012

Hi Emilio, Thanks for the reply.. I'l be sure to go over that very carefully.. Thank you for the info!

One quick question.... is there a chance I'm running out of memory by opening a file too large?? Is there a way to determine this?

14 Aug 2012

Emilio.... I've tried using your code... Doesn't seem to make a difference for me.. I've tried reducing the size of my text file to one line and that doesn't help either...

I'm using cmsis_os.h and not the rtos.h.... Not sure if that makes any difference.. The code I'm using is what was worked out in http://mbed.org/forum/mbed/topic/3763/ ... Works very well for me except in this case...

Just for a shortcut...

#include "mbed.h"
#include "iostream"
#include "string"
 
DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
DigitalOut led4(LED4);
 
LocalFileSystem local("local");
Serial pc(USBTX, USBRX);
 
int main() {
    led1 = 1;
    pc.baud(921600);
    printf("Parsing:\r\n");
    wait(1);
    led2 = 1;
    FILE *fp = fopen( "/local/test.txt", "r");
    printf("\nLoading file:\n");
    if ( fp == NULL ) {
        error("Could not open file for read operation.\r\n");
        led3 = 1;
    }
    char buf[256];
    int n = 0;
    string coord;
    string longitude;
    string latitude;
    
    while (n <= 667) {
        n++;
        fgets(buf, sizeof(buf), fp);
 
        size_t found;
        string str(buf);
        
        found=str.find("COORD:");
        if (found!=string::npos) {
            size_t f = str.find("COORD:");
            waypoint = str.replace(f, std::string("COORD:").length(), "\0");
            printf("\nFound Coord: %s", coord);
        }
        found=str.find("LON:");
        if (found!=string::npos) {
            size_t f = str.find("LON:");
            longitude = str.replace(f, std::string("LON:").length(), "\0");
            printf("        LONGITUDE: %s", longitude);
        }
        found=str.find("LAT:");
        if (found!=string::npos) {
            size_t f = str.find("LAT:");
            latitude = str.replace(f, std::string("LAT:").length(), "\0");
            printf("        LATITUDE:  %s", latitude);
        }
        
        led2 = 0;
    }
    printf("Closing File...\r\n");
    fclose(fp);
    led1 = 0;
    wait(1);
}

Any assistance would be greatly appreciated... I hope it's not a memory thing.. =(

14 Aug 2012

Hi Mark,

Mark x wrote:

Emilio.... I've tried using your code... Doesn't seem to make a difference for me

Could you confirm me that this program opening, writing and reading a file in the local file system within an RTOS thread is working for you?

Import programrtos_file

Simple test for using the local file system within a thread

If the above program does not work for you, your mbed is probably broken and this case is closed.

Cheers, Emilio