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:
Thu Dec 26 15:02:29 2013 +0000
Revision:
0:1a13ba7627ce
Child:
2:9aa6f6caf2a8
v1.0

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 0:1a13ba7627ce 18 DigitalOut SD_EN(PTE3); // Enable signal for SD Card
Sissors 0:1a13ba7627ce 19 SDFileSystem sd(PTD6, PTD7, PTB11, PTE5, "sd"); // The pinout on the Wi-Go2 module
Sissors 0:1a13ba7627ce 20
Sissors 0:1a13ba7627ce 21 unsigned int buffer1[100];
Sissors 0:1a13ba7627ce 22 unsigned int buffer2[100];
Sissors 0:1a13ba7627ce 23
Sissors 0:1a13ba7627ce 24 Thread *sd_pointer;
Sissors 0:1a13ba7627ce 25
Sissors 0:1a13ba7627ce 26 void timer_thread(void const *args) {
Sissors 0:1a13ba7627ce 27 Thread::wait(5000);
Sissors 0:1a13ba7627ce 28
Sissors 0:1a13ba7627ce 29 Timer timey;
Sissors 0:1a13ba7627ce 30 int i = 0;
Sissors 0:1a13ba7627ce 31 printf("Starting timing!\r\n");
Sissors 0:1a13ba7627ce 32
Sissors 0:1a13ba7627ce 33 timey.start();
Sissors 0:1a13ba7627ce 34 while (true) {
Sissors 0:1a13ba7627ce 35 //No RTOS wait here, but since it has low priority that should be fine
Sissors 0:1a13ba7627ce 36 if (i<100)
Sissors 0:1a13ba7627ce 37 buffer1[i]=timey.read_us();
Sissors 0:1a13ba7627ce 38 else if (i<200)
Sissors 0:1a13ba7627ce 39 buffer2[i-100]=timey.read_us();
Sissors 0:1a13ba7627ce 40 else
Sissors 0:1a13ba7627ce 41 break;
Sissors 0:1a13ba7627ce 42
Sissors 0:1a13ba7627ce 43 if (i==99)
Sissors 0:1a13ba7627ce 44 sd_pointer->signal_set(0x1);
Sissors 0:1a13ba7627ce 45 if (i==199)
Sissors 0:1a13ba7627ce 46 sd_pointer->signal_set(0x2);
Sissors 0:1a13ba7627ce 47 i++;
Sissors 0:1a13ba7627ce 48 }
Sissors 0:1a13ba7627ce 49 printf("Timer is done\r\n");
Sissors 0:1a13ba7627ce 50 }
Sissors 0:1a13ba7627ce 51
Sissors 0:1a13ba7627ce 52 void sd_thread(void const *args) {
Sissors 0:1a13ba7627ce 53 //Get ready
Sissors 0:1a13ba7627ce 54 mkdir("/sd/Wi-Go", 0777);
Sissors 0:1a13ba7627ce 55 mkdir("/sd/Wi-Go", 0777);
Sissors 0:1a13ba7627ce 56
Sissors 0:1a13ba7627ce 57 printf("SD thread is ready\r\n");
Sissors 0:1a13ba7627ce 58
Sissors 0:1a13ba7627ce 59 Thread::signal_wait(0x1);
Sissors 0:1a13ba7627ce 60 FILE *fp = fopen("/sd/Wi-Go/timer.txt", "w");
Sissors 0:1a13ba7627ce 61 if(fp == NULL) {
Sissors 0:1a13ba7627ce 62 error("Could not open file for write\n\r");
Sissors 0:1a13ba7627ce 63 }
Sissors 0:1a13ba7627ce 64 for (int i = 0; i<100; i++)
Sissors 0:1a13ba7627ce 65 fprintf(fp, "%d\r\n",buffer1[i]);
Sissors 0:1a13ba7627ce 66 fclose(fp);
Sissors 0:1a13ba7627ce 67
Sissors 0:1a13ba7627ce 68 Thread::signal_wait(0x2);
Sissors 0:1a13ba7627ce 69 fp = fopen("/sd/Wi-Go/timer.txt", "a");
Sissors 0:1a13ba7627ce 70 if(fp == NULL) {
Sissors 0:1a13ba7627ce 71 error("Could not open file for write\n\r");
Sissors 0:1a13ba7627ce 72 }
Sissors 0:1a13ba7627ce 73 for (int i = 0; i<100; i++)
Sissors 0:1a13ba7627ce 74 fprintf(fp, "%d\r\n",buffer2[i]);
Sissors 0:1a13ba7627ce 75 fclose(fp);
Sissors 0:1a13ba7627ce 76
Sissors 0:1a13ba7627ce 77 printf("SD is done \r\n");
Sissors 0:1a13ba7627ce 78 }
Sissors 0:1a13ba7627ce 79
Sissors 0:1a13ba7627ce 80 int main() {
Sissors 0:1a13ba7627ce 81
Sissors 0:1a13ba7627ce 82 SD_EN = 1; // Enable SD Card, wait a bit to make sure SD card is properly inserted
Sissors 0:1a13ba7627ce 83 wait(1);
Sissors 0:1a13ba7627ce 84 printf("Starting SD thread\r\n");
Sissors 0:1a13ba7627ce 85 Thread thread_sd(sd_thread);
Sissors 0:1a13ba7627ce 86 sd_pointer = &thread_sd;
Sissors 0:1a13ba7627ce 87
Sissors 0:1a13ba7627ce 88 printf("Starting timer thread\r\n");
Sissors 0:1a13ba7627ce 89 Thread thread(timer_thread);
Sissors 0:1a13ba7627ce 90 thread.set_priority(osPriorityLow);
Sissors 0:1a13ba7627ce 91 Thread::wait(20000);
Sissors 0:1a13ba7627ce 92 printf("Main thread done\r\n");
Sissors 0:1a13ba7627ce 93
Sissors 0:1a13ba7627ce 94
Sissors 0:1a13ba7627ce 95 }