]> git.gir.st - tmk_keyboard.git/blob - tool/mbed/mbed-sdk/libraries/tests/rtos/mbed/file/main.cpp
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[tmk_keyboard.git] / tool / mbed / mbed-sdk / libraries / tests / rtos / mbed / file / main.cpp
1 #include "mbed.h"
2 #include "SDFileSystem.h"
3 #include "test_env.h"
4 #include "rtos.h"
5
6 DigitalOut led2(LED2);
7
8 #define SIZE 100
9
10 namespace {
11 // Allocate data buffers
12 uint8_t data_written[SIZE] = { 0 };
13 uint8_t data_read[SIZE] = { 0 };
14 }
15
16 void sd_thread(void const *argument)
17 {
18 const char *FILE_NAME = "/sd/rtos9_test.txt";
19
20 #if defined(TARGET_KL25Z)
21 SDFileSystem sd(PTD2, PTD3, PTD1, PTD0, "sd");
22
23 #elif defined(TARGET_KL46Z)
24 SDFileSystem sd(PTD6, PTD7, PTD5, PTD4, "sd");
25
26 #elif defined(TARGET_K64F)
27 SDFileSystem sd(PTD2, PTD3, PTD1, PTD0, "sd");
28
29 #elif defined(TARGET_RZ_A1H)
30 SDFileSystem sd(P8_5, P8_6, P8_3, P8_4, "sd");
31
32 #else
33 SDFileSystem sd(p11, p12, p13, p14, "sd");
34 #endif
35
36 {
37 // fill data_written buffer with random data
38 FILE *f = fopen(FILE_NAME, "w+");
39 if (f) {
40 // write these data into the file
41 printf("Writing %d bytes to file:" NL, SIZE);
42 for (int i = 0; i < SIZE; i++) {
43 data_written[i] = rand() % 0xff;
44 fprintf(f, "%c", data_written[i]);
45 printf("%02X ", data_written[i]);
46 if (i && ((i % 20) == 19))
47 printf(NL);
48 }
49 fclose(f);
50 printf("MBED: Done" NL);
51 } else {
52 printf("MBED: Can't open '%s'" NL, FILE_NAME);
53 MBED_HOSTTEST_RESULT(false);
54 }
55 }
56
57 printf(NL);
58
59 {
60 // read back the data from the file and store them in data_read
61 FILE *f = fopen(FILE_NAME, "r");
62 if (f) {
63 printf("MBED: Reading %d bytes from file:" NL, SIZE);
64 for (int i = 0; i < SIZE; i++) {
65 data_read[i] = fgetc(f);
66 printf("%02X ", data_read[i]);
67 if (i && ((i % 20) == 19))
68 printf(NL);
69 }
70 fclose(f);
71 printf("MBED: Done\r\n");
72 } else {
73 printf("MBED: Can't open '%s'" NL, FILE_NAME);
74 MBED_HOSTTEST_RESULT(false);
75 }
76 }
77
78 // check that the data written == data read
79 for (int i = 0; i < SIZE; i++) {
80 if (data_written[i] != data_read[i]) {
81 printf("MBED: Data index=%d: w[0x%02X] != r[0x%02X]" NL, i, data_written[i], data_read[i]);
82 MBED_HOSTTEST_RESULT(false);
83 }
84 }
85 MBED_HOSTTEST_RESULT(true);
86 }
87
88 int main() {
89 MBED_HOSTTEST_TIMEOUT(20);
90 MBED_HOSTTEST_SELECT(default_auto);
91 MBED_HOSTTEST_DESCRIPTION(SD File write read);
92 MBED_HOSTTEST_START("RTOS_9");
93
94 Thread t(sd_thread, NULL, osPriorityNormal, (DEFAULT_STACK_SIZE * 2.25));
95
96 while (true) {
97 led2 = !led2;
98 Thread::wait(1000);
99 }
100 }
Imprint / Impressum