nkjnm

Dependencies:   MAX44000 nexpaq_mdk

Fork of LED_Demo by Maxim nexpaq

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "SDFileSystem.h"
00003 #include "test_env.h"
00004 #include "rtos.h"
00005 
00006 #if defined(MBED_RTOS_SINGLE_THREAD)
00007   #error [NOT_SUPPORTED] test not supported
00008 #endif
00009 
00010 DigitalOut led2(LED2);
00011 
00012 #define SIZE 100
00013 
00014 namespace {
00015 // Allocate data buffers
00016 uint8_t data_written[SIZE] = { 0 };
00017 uint8_t data_read[SIZE] = { 0 };
00018 }
00019 
00020 void sd_thread(void const *argument)
00021 {
00022     const char *FILE_NAME = "/sd/rtos9_test.txt";
00023 
00024 #if defined(TARGET_KL25Z)
00025     SDFileSystem sd(PTD2, PTD3, PTD1, PTD0, "sd");
00026 
00027 #elif defined(TARGET_KL46Z)
00028     SDFileSystem sd(PTD6, PTD7, PTD5, PTD4, "sd");
00029 
00030 #elif defined(TARGET_K64F) || defined(TARGET_K66F)
00031     SDFileSystem sd(PTD2, PTD3, PTD1, PTD0, "sd");
00032 
00033 #elif defined(TARGET_RZ_A1H)
00034     SDFileSystem sd(P8_5, P8_6, P8_3, P8_4, "sd");
00035 
00036 #else
00037     SDFileSystem sd(p11, p12, p13, p14, "sd");
00038 #endif
00039 
00040     {
00041         // fill data_written buffer with random data
00042         FILE *f = fopen(FILE_NAME, "w+");
00043         if (f) {
00044             // write these data into the file
00045             printf("Writing %d bytes to file:" NL, SIZE);
00046             for (int i = 0; i < SIZE; i++) {
00047                 data_written[i] = rand() % 0xff;
00048                 fprintf(f, "%c", data_written[i]);
00049                 printf("%02X ", data_written[i]);
00050                 if (i && ((i % 20) == 19))
00051                     printf(NL);
00052             }
00053             fclose(f);
00054             printf("MBED: Done" NL);
00055         } else {
00056             printf("MBED: Can't open '%s'" NL, FILE_NAME);
00057             MBED_HOSTTEST_RESULT(false);
00058         }
00059     }
00060 
00061     printf(NL);
00062 
00063     {
00064         // read back the data from the file and store them in data_read
00065         FILE *f = fopen(FILE_NAME, "r");
00066         if (f) {
00067             printf("MBED: Reading %d bytes from file:" NL, SIZE);
00068             for (int i = 0; i < SIZE; i++) {
00069                 data_read[i] = fgetc(f);
00070                 printf("%02X ", data_read[i]);
00071                 if (i && ((i % 20) == 19))
00072                     printf(NL);
00073             }
00074             fclose(f);
00075             printf("MBED: Done\r\n");
00076         } else {
00077             printf("MBED: Can't open '%s'" NL, FILE_NAME);
00078             MBED_HOSTTEST_RESULT(false);
00079         }
00080     }
00081 
00082     // check that the data written == data read
00083     for (int i = 0; i < SIZE; i++) {
00084         if (data_written[i] != data_read[i]) {
00085             printf("MBED: Data index=%d: w[0x%02X] != r[0x%02X]" NL, i, data_written[i], data_read[i]);
00086             MBED_HOSTTEST_RESULT(false);
00087         }
00088     }
00089     MBED_HOSTTEST_RESULT(true);
00090 }
00091 
00092 int main() {
00093     MBED_HOSTTEST_TIMEOUT(20);
00094     MBED_HOSTTEST_SELECT(default_auto);
00095     MBED_HOSTTEST_DESCRIPTION(SD File write read);
00096     MBED_HOSTTEST_START("RTOS_9");
00097 
00098     Thread t(sd_thread, NULL, osPriorityNormal, (DEFAULT_STACK_SIZE * 2.25));
00099 
00100     while (true) {
00101         led2 = !led2;
00102         Thread::wait(1000);
00103     }
00104 }