Simple helloworld program to show reduced overhead of SDFileSystem-RTOS

Dependencies:   SDFileSystem-RTOS mbed-rtos mbed

Hello-world program for RTOS SDFileSystem

One thread stores time values in two buffers, the other thread saves it on the SD card. Since the time thread has low priority the SD thread will take what it needs. This then shows the reduced overhead of the RTOS SDFileSystem compared to regular.

To test it yourself, delete SDFileSystem-RTOS, import the regular one.

The pinouts are for the Wi-Go2 board, but you can easily change it.

After writing the first buffer it stores it on the SD card, and as soon as possible starts filling the second buffer with new timer values. The timer values around this storage point for the RTOS version of SDFileSystem:

183
185
187
189
191
1900
1902
1904
1906
1908
1910

So it stopped the timer thread for 1.7ms.

With regular SD card it is:

183
185
187
189
191
426240
426242
426244
426246
426248
426250

So that is alot worse. Of course you still need to give it enough time to write everything, and later on there will also be short hickups when it has to start writing a new block.

Committer:
Sissors
Date:
Sat Mar 15 22:23:36 2014 +0000
Revision:
2:9aa6f6caf2a8
Parent:
0:1a13ba7627ce
Added pinout comments

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Sissors 0:1a13ba7627ce 1 /*
Sissors 0:1a13ba7627ce 2 Hello-world program for RTOS SDFileSystem
Sissors 0:1a13ba7627ce 3
Sissors 0:1a13ba7627ce 4 One thread stores time values in two buffers, the other thread
Sissors 0:1a13ba7627ce 5 saves it on the SD card. Since the time thread has low priority
Sissors 0:1a13ba7627ce 6 the SD thread will take what it needs. This then shows the reduced
Sissors 0:1a13ba7627ce 7 overhead of the RTOS SDFileSystem compared to regular.
Sissors 0:1a13ba7627ce 8
Sissors 0:1a13ba7627ce 9 To test it yourself, delete SDFileSystem-RTOS, import the regular one.
Sissors 0:1a13ba7627ce 10
Sissors 0:1a13ba7627ce 11 The pinouts are for the Wi-Go2 board, but you can easily change it
Sissors 0:1a13ba7627ce 12 */
Sissors 0:1a13ba7627ce 13
Sissors 0:1a13ba7627ce 14 #include "mbed.h"
Sissors 0:1a13ba7627ce 15 #include "rtos.h"
Sissors 0:1a13ba7627ce 16 #include "SDFileSystem.h"
Sissors 0:1a13ba7627ce 17
Sissors 2:9aa6f6caf2a8 18 DigitalOut SD_EN(PTE3); // Enable signal for SD Card, specific to Wi-Go2, most SD-card boards do not have this pin
Sissors 2:9aa6f6caf2a8 19 // In that case you can remove it, or assign it to a random pin you are not using
Sissors 2:9aa6f6caf2a8 20 SDFileSystem sd(PTD6, PTD7, PTB11, PTE5, "sd"); // The pinout on the Wi-Go2 module (MOSI, MISO, SCLK, CS)
Sissors 0:1a13ba7627ce 21
Sissors 0:1a13ba7627ce 22 unsigned int buffer1[100];
Sissors 0:1a13ba7627ce 23 unsigned int buffer2[100];
Sissors 0:1a13ba7627ce 24
Sissors 0:1a13ba7627ce 25 Thread *sd_pointer;
Sissors 0:1a13ba7627ce 26
Sissors 0:1a13ba7627ce 27 void timer_thread(void const *args) {
Sissors 0:1a13ba7627ce 28 Thread::wait(5000);
Sissors 0:1a13ba7627ce 29
Sissors 0:1a13ba7627ce 30 Timer timey;
Sissors 0:1a13ba7627ce 31 int i = 0;
Sissors 0:1a13ba7627ce 32 printf("Starting timing!\r\n");
Sissors 0:1a13ba7627ce 33
Sissors 0:1a13ba7627ce 34 timey.start();
Sissors 0:1a13ba7627ce 35 while (true) {
Sissors 0:1a13ba7627ce 36 //No RTOS wait here, but since it has low priority that should be fine
Sissors 0:1a13ba7627ce 37 if (i<100)
Sissors 0:1a13ba7627ce 38 buffer1[i]=timey.read_us();
Sissors 0:1a13ba7627ce 39 else if (i<200)
Sissors 0:1a13ba7627ce 40 buffer2[i-100]=timey.read_us();
Sissors 0:1a13ba7627ce 41 else
Sissors 0:1a13ba7627ce 42 break;
Sissors 0:1a13ba7627ce 43
Sissors 0:1a13ba7627ce 44 if (i==99)
Sissors 0:1a13ba7627ce 45 sd_pointer->signal_set(0x1);
Sissors 0:1a13ba7627ce 46 if (i==199)
Sissors 0:1a13ba7627ce 47 sd_pointer->signal_set(0x2);
Sissors 0:1a13ba7627ce 48 i++;
Sissors 0:1a13ba7627ce 49 }
Sissors 0:1a13ba7627ce 50 printf("Timer is done\r\n");
Sissors 0:1a13ba7627ce 51 }
Sissors 0:1a13ba7627ce 52
Sissors 0:1a13ba7627ce 53 void sd_thread(void const *args) {
Sissors 0:1a13ba7627ce 54 //Get ready
Sissors 0:1a13ba7627ce 55 mkdir("/sd/Wi-Go", 0777);
Sissors 0:1a13ba7627ce 56 mkdir("/sd/Wi-Go", 0777);
Sissors 0:1a13ba7627ce 57
Sissors 0:1a13ba7627ce 58 printf("SD thread is ready\r\n");
Sissors 0:1a13ba7627ce 59
Sissors 0:1a13ba7627ce 60 Thread::signal_wait(0x1);
Sissors 0:1a13ba7627ce 61 FILE *fp = fopen("/sd/Wi-Go/timer.txt", "w");
Sissors 0:1a13ba7627ce 62 if(fp == NULL) {
Sissors 0:1a13ba7627ce 63 error("Could not open file for write\n\r");
Sissors 0:1a13ba7627ce 64 }
Sissors 0:1a13ba7627ce 65 for (int i = 0; i<100; i++)
Sissors 0:1a13ba7627ce 66 fprintf(fp, "%d\r\n",buffer1[i]);
Sissors 0:1a13ba7627ce 67 fclose(fp);
Sissors 0:1a13ba7627ce 68
Sissors 0:1a13ba7627ce 69 Thread::signal_wait(0x2);
Sissors 0:1a13ba7627ce 70 fp = fopen("/sd/Wi-Go/timer.txt", "a");
Sissors 0:1a13ba7627ce 71 if(fp == NULL) {
Sissors 0:1a13ba7627ce 72 error("Could not open file for write\n\r");
Sissors 0:1a13ba7627ce 73 }
Sissors 0:1a13ba7627ce 74 for (int i = 0; i<100; i++)
Sissors 0:1a13ba7627ce 75 fprintf(fp, "%d\r\n",buffer2[i]);
Sissors 0:1a13ba7627ce 76 fclose(fp);
Sissors 0:1a13ba7627ce 77
Sissors 0:1a13ba7627ce 78 printf("SD is done \r\n");
Sissors 0:1a13ba7627ce 79 }
Sissors 0:1a13ba7627ce 80
Sissors 0:1a13ba7627ce 81 int main() {
Sissors 0:1a13ba7627ce 82
Sissors 0:1a13ba7627ce 83 SD_EN = 1; // Enable SD Card, wait a bit to make sure SD card is properly inserted
Sissors 0:1a13ba7627ce 84 wait(1);
Sissors 0:1a13ba7627ce 85 printf("Starting SD thread\r\n");
Sissors 0:1a13ba7627ce 86 Thread thread_sd(sd_thread);
Sissors 0:1a13ba7627ce 87 sd_pointer = &thread_sd;
Sissors 0:1a13ba7627ce 88
Sissors 0:1a13ba7627ce 89 printf("Starting timer thread\r\n");
Sissors 0:1a13ba7627ce 90 Thread thread(timer_thread);
Sissors 0:1a13ba7627ce 91 thread.set_priority(osPriorityLow);
Sissors 0:1a13ba7627ce 92 Thread::wait(20000);
Sissors 0:1a13ba7627ce 93 printf("Main thread done\r\n");
Sissors 0:1a13ba7627ce 94
Sissors 0:1a13ba7627ce 95
Sissors 0:1a13ba7627ce 96 }