Preliminary main mbed library for nexpaq development
libraries/tests/mbed/dir_sd/main.cpp@1:d96dbedaebdb, 2016-11-04 (annotated)
- Committer:
- nexpaq
- Date:
- Fri Nov 04 20:54:50 2016 +0000
- Revision:
- 1:d96dbedaebdb
- Parent:
- 0:6c56fb4bc5f0
Removed extra directories for other platforms
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nexpaq | 0:6c56fb4bc5f0 | 1 | #include "mbed.h" |
nexpaq | 0:6c56fb4bc5f0 | 2 | #include "SDFileSystem.h" |
nexpaq | 0:6c56fb4bc5f0 | 3 | |
nexpaq | 0:6c56fb4bc5f0 | 4 | void led_blink(PinName led) |
nexpaq | 0:6c56fb4bc5f0 | 5 | { |
nexpaq | 0:6c56fb4bc5f0 | 6 | DigitalOut myled(led); |
nexpaq | 0:6c56fb4bc5f0 | 7 | |
nexpaq | 0:6c56fb4bc5f0 | 8 | while (1) { |
nexpaq | 0:6c56fb4bc5f0 | 9 | myled = !myled; |
nexpaq | 0:6c56fb4bc5f0 | 10 | wait(1.0); |
nexpaq | 0:6c56fb4bc5f0 | 11 | } |
nexpaq | 0:6c56fb4bc5f0 | 12 | } |
nexpaq | 0:6c56fb4bc5f0 | 13 | |
nexpaq | 0:6c56fb4bc5f0 | 14 | void notify_completion(bool success) |
nexpaq | 0:6c56fb4bc5f0 | 15 | { |
nexpaq | 0:6c56fb4bc5f0 | 16 | if (success) |
nexpaq | 0:6c56fb4bc5f0 | 17 | printf("{success}\n"); |
nexpaq | 0:6c56fb4bc5f0 | 18 | else |
nexpaq | 0:6c56fb4bc5f0 | 19 | printf("{failure}\n"); |
nexpaq | 0:6c56fb4bc5f0 | 20 | |
nexpaq | 0:6c56fb4bc5f0 | 21 | printf("{end}\n"); |
nexpaq | 0:6c56fb4bc5f0 | 22 | led_blink(success ? LED1 : LED4); |
nexpaq | 0:6c56fb4bc5f0 | 23 | } |
nexpaq | 0:6c56fb4bc5f0 | 24 | |
nexpaq | 0:6c56fb4bc5f0 | 25 | #define TEST_STRING "Hello World!" |
nexpaq | 0:6c56fb4bc5f0 | 26 | |
nexpaq | 0:6c56fb4bc5f0 | 27 | FILE *test_open(char *path, const char *mode) |
nexpaq | 0:6c56fb4bc5f0 | 28 | { |
nexpaq | 0:6c56fb4bc5f0 | 29 | FILE *f; |
nexpaq | 0:6c56fb4bc5f0 | 30 | |
nexpaq | 0:6c56fb4bc5f0 | 31 | f = fopen(path, mode); |
nexpaq | 0:6c56fb4bc5f0 | 32 | if (f == NULL) { |
nexpaq | 0:6c56fb4bc5f0 | 33 | printf("Error opening file\n"); |
nexpaq | 0:6c56fb4bc5f0 | 34 | notify_completion(false); |
nexpaq | 0:6c56fb4bc5f0 | 35 | } |
nexpaq | 0:6c56fb4bc5f0 | 36 | |
nexpaq | 0:6c56fb4bc5f0 | 37 | return f; |
nexpaq | 0:6c56fb4bc5f0 | 38 | } |
nexpaq | 0:6c56fb4bc5f0 | 39 | |
nexpaq | 0:6c56fb4bc5f0 | 40 | void test_write(FILE *f, const char *str) |
nexpaq | 0:6c56fb4bc5f0 | 41 | { |
nexpaq | 0:6c56fb4bc5f0 | 42 | int n = fprintf(f, str); |
nexpaq | 0:6c56fb4bc5f0 | 43 | |
nexpaq | 0:6c56fb4bc5f0 | 44 | if (n != strlen(str)) { |
nexpaq | 0:6c56fb4bc5f0 | 45 | printf("Error writing file\n"); |
nexpaq | 0:6c56fb4bc5f0 | 46 | notify_completion(false); |
nexpaq | 0:6c56fb4bc5f0 | 47 | } |
nexpaq | 0:6c56fb4bc5f0 | 48 | } |
nexpaq | 0:6c56fb4bc5f0 | 49 | |
nexpaq | 0:6c56fb4bc5f0 | 50 | void test_close(FILE *f) |
nexpaq | 0:6c56fb4bc5f0 | 51 | { |
nexpaq | 0:6c56fb4bc5f0 | 52 | int rc = fclose(f); |
nexpaq | 0:6c56fb4bc5f0 | 53 | |
nexpaq | 0:6c56fb4bc5f0 | 54 | if (rc != 0) { |
nexpaq | 0:6c56fb4bc5f0 | 55 | printf("Error closing file\n"); |
nexpaq | 0:6c56fb4bc5f0 | 56 | notify_completion(false); |
nexpaq | 0:6c56fb4bc5f0 | 57 | } |
nexpaq | 0:6c56fb4bc5f0 | 58 | } |
nexpaq | 0:6c56fb4bc5f0 | 59 | |
nexpaq | 0:6c56fb4bc5f0 | 60 | DigitalOut led2(LED2); |
nexpaq | 0:6c56fb4bc5f0 | 61 | |
nexpaq | 0:6c56fb4bc5f0 | 62 | int main() |
nexpaq | 0:6c56fb4bc5f0 | 63 | { |
nexpaq | 0:6c56fb4bc5f0 | 64 | #if defined(TARGET_KL25Z) |
nexpaq | 0:6c56fb4bc5f0 | 65 | SDFileSystem sd(PTD2, PTD3, PTD1, PTD0, "sd"); |
nexpaq | 0:6c56fb4bc5f0 | 66 | #elif defined(TARGET_nRF51822) |
nexpaq | 0:6c56fb4bc5f0 | 67 | //SDFileSystem sd(p20, p22, p25, p24, "sd"); |
nexpaq | 0:6c56fb4bc5f0 | 68 | SDFileSystem sd(p12, p13, p15, p14, "sd"); |
nexpaq | 0:6c56fb4bc5f0 | 69 | #elif defined(TARGET_NUCLEO_F030R8) || \ |
nexpaq | 0:6c56fb4bc5f0 | 70 | defined(TARGET_NUCLEO_F070RB) || \ |
nexpaq | 0:6c56fb4bc5f0 | 71 | defined(TARGET_NUCLEO_F072RB) || \ |
nexpaq | 0:6c56fb4bc5f0 | 72 | defined(TARGET_NUCLEO_F091RC) || \ |
nexpaq | 0:6c56fb4bc5f0 | 73 | defined(TARGET_NUCLEO_F103RB) || \ |
nexpaq | 0:6c56fb4bc5f0 | 74 | defined(TARGET_NUCLEO_F302R8) || \ |
nexpaq | 0:6c56fb4bc5f0 | 75 | defined(TARGET_NUCLEO_F303RE) || \ |
nexpaq | 0:6c56fb4bc5f0 | 76 | defined(TARGET_NUCLEO_F334R8) || \ |
nexpaq | 0:6c56fb4bc5f0 | 77 | defined(TARGET_NUCLEO_F401RE) || \ |
nexpaq | 0:6c56fb4bc5f0 | 78 | defined(TARGET_NUCLEO_F410RB) || \ |
nexpaq | 0:6c56fb4bc5f0 | 79 | defined(TARGET_NUCLEO_F411RE) || \ |
nexpaq | 0:6c56fb4bc5f0 | 80 | defined(TARGET_NUCLEO_L053R8) || \ |
nexpaq | 0:6c56fb4bc5f0 | 81 | defined(TARGET_NUCLEO_L073RZ) || \ |
nexpaq | 0:6c56fb4bc5f0 | 82 | defined(TARGET_NUCLEO_L152RE) |
nexpaq | 0:6c56fb4bc5f0 | 83 | SDFileSystem sd(D11, D12, D13, D10, "sd"); |
nexpaq | 0:6c56fb4bc5f0 | 84 | #elif defined(TARGET_LPC11U37H_401) |
nexpaq | 0:6c56fb4bc5f0 | 85 | SDFileSystem sd(SDMOSI, SDMISO, SDSCLK, SDSSEL, "sd"); |
nexpaq | 0:6c56fb4bc5f0 | 86 | #else |
nexpaq | 0:6c56fb4bc5f0 | 87 | SDFileSystem sd(p11, p12, p13, p14, "sd"); |
nexpaq | 0:6c56fb4bc5f0 | 88 | #endif |
nexpaq | 0:6c56fb4bc5f0 | 89 | led2 = 1; |
nexpaq | 0:6c56fb4bc5f0 | 90 | wait(0.5); |
nexpaq | 0:6c56fb4bc5f0 | 91 | FILE *f; |
nexpaq | 0:6c56fb4bc5f0 | 92 | char *str = TEST_STRING; |
nexpaq | 0:6c56fb4bc5f0 | 93 | char *buffer = (char *)malloc(sizeof(unsigned char) * strlen(TEST_STRING)); |
nexpaq | 0:6c56fb4bc5f0 | 94 | int str_len = strlen(TEST_STRING); |
nexpaq | 0:6c56fb4bc5f0 | 95 | |
nexpaq | 0:6c56fb4bc5f0 | 96 | printf("Write files\n"); |
nexpaq | 0:6c56fb4bc5f0 | 97 | char filename[32]; |
nexpaq | 0:6c56fb4bc5f0 | 98 | for (int i = 0; i < 10; i++) { |
nexpaq | 0:6c56fb4bc5f0 | 99 | sprintf(filename, "/sd/test_%d.txt", i); |
nexpaq | 0:6c56fb4bc5f0 | 100 | printf("Creating file: %s\n", filename); |
nexpaq | 0:6c56fb4bc5f0 | 101 | f = test_open(filename, "w"); |
nexpaq | 0:6c56fb4bc5f0 | 102 | led2 = 0; |
nexpaq | 0:6c56fb4bc5f0 | 103 | test_write(f, str); |
nexpaq | 0:6c56fb4bc5f0 | 104 | test_close(f); |
nexpaq | 0:6c56fb4bc5f0 | 105 | } |
nexpaq | 0:6c56fb4bc5f0 | 106 | |
nexpaq | 0:6c56fb4bc5f0 | 107 | printf("List files:\n"); |
nexpaq | 0:6c56fb4bc5f0 | 108 | DIR *d = opendir("/sd"); |
nexpaq | 0:6c56fb4bc5f0 | 109 | if (d == NULL) { |
nexpaq | 0:6c56fb4bc5f0 | 110 | printf("Error opening directory\n"); |
nexpaq | 0:6c56fb4bc5f0 | 111 | notify_completion(false); |
nexpaq | 0:6c56fb4bc5f0 | 112 | } |
nexpaq | 0:6c56fb4bc5f0 | 113 | |
nexpaq | 0:6c56fb4bc5f0 | 114 | struct dirent *p; |
nexpaq | 0:6c56fb4bc5f0 | 115 | while ((p = readdir(d)) != NULL) |
nexpaq | 0:6c56fb4bc5f0 | 116 | printf("%s\n", p->d_name); |
nexpaq | 0:6c56fb4bc5f0 | 117 | closedir(d); |
nexpaq | 0:6c56fb4bc5f0 | 118 | |
nexpaq | 0:6c56fb4bc5f0 | 119 | notify_completion(true); |
nexpaq | 0:6c56fb4bc5f0 | 120 | } |