![](/media/cache/profiles/f0fcf351df4eb6786e9bb6fc4e2dee02.jpg.50x50_q85.jpg)
SD-Card Control Program / Using Micro-SD / based on SDCardTest Program (http://mbed.org/users/simon/programs/SDCardTest/gpdz4x/)
Dependencies: mbed SDFileSystem
Please refer following my Notebook page.
/users/kenjiArai/notebook/sd-card-control-new/#
main.cpp@6:b4538dd09336, 2019-12-27 (annotated)
- Committer:
- kenjiArai
- Date:
- Fri Dec 27 03:01:20 2019 +0000
- Revision:
- 6:b4538dd09336
- Parent:
- 5:e222c59853e2
- Child:
- 7:4467fbfa888b
changed fclose() sequence
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kenjiArai | 2:1397a54382ec | 1 | /* |
kenjiArai | 2:1397a54382ec | 2 | * Mbed Application program |
kenjiArai | 4:fc36c8ec2966 | 3 | * SD Card file control function with FatFs on Mbed-os5 or os2 |
kenjiArai | 2:1397a54382ec | 4 | * |
kenjiArai | 4:fc36c8ec2966 | 5 | * Copyright (c) 2018,'19 Kenji Arai / JH1PJL |
kenjiArai | 2:1397a54382ec | 6 | * http://www.page.sannet.ne.jp/kenjia/index.html |
kenjiArai | 2:1397a54382ec | 7 | * https://os.mbed.com/users/kenjiArai/ |
kenjiArai | 2:1397a54382ec | 8 | * Created: April 4th, 2018 |
kenjiArai | 6:b4538dd09336 | 9 | * Revised: December 27th, 2019 |
kenjiArai | 4:fc36c8ec2966 | 10 | */ |
kenjiArai | 4:fc36c8ec2966 | 11 | |
kenjiArai | 4:fc36c8ec2966 | 12 | /* |
kenjiArai | 5:e222c59853e2 | 13 | Tested board on OS2: |
kenjiArai | 4:fc36c8ec2966 | 14 | Nucleo-F401RE, -F411RE, -F446RE, -L073RZ, -L152RE, -L476RG |
kenjiArai | 5:e222c59853e2 | 15 | Tested board on OS5.15.0 |
kenjiArai | 5:e222c59853e2 | 16 | Nucleo-F401RE, -F411RE, -F446RE, -L152RE, -L073RZ, -L476RG |
kenjiArai | 5:e222c59853e2 | 17 | nRF52840-DK, nRF52-DK |
kenjiArai | 5:e222c59853e2 | 18 | FRDM-F64K |
kenjiArai | 2:1397a54382ec | 19 | */ |
kenjiArai | 2:1397a54382ec | 20 | |
kenjiArai | 2:1397a54382ec | 21 | // Include -------------------------------------------------------------------- |
kenjiArai | 2:1397a54382ec | 22 | #include "mbed.h" |
kenjiArai | 2:1397a54382ec | 23 | #if (MBED_MAJOR_VERSION == 2) |
kenjiArai | 2:1397a54382ec | 24 | #include "SDFileSystem.h" |
kenjiArai | 2:1397a54382ec | 25 | #elif (MBED_MAJOR_VERSION == 5) |
kenjiArai | 2:1397a54382ec | 26 | #include "SDBlockDevice.h" |
kenjiArai | 2:1397a54382ec | 27 | #include "FATFileSystem.h" |
kenjiArai | 2:1397a54382ec | 28 | #endif |
kenjiArai | 2:1397a54382ec | 29 | #include "mon.h" |
kenjiArai | 2:1397a54382ec | 30 | #include <stdlib.h> |
kenjiArai | 2:1397a54382ec | 31 | |
kenjiArai | 2:1397a54382ec | 32 | // Definition ----------------------------------------------------------------- |
kenjiArai | 2:1397a54382ec | 33 | #define USER_SW_ON 0 |
kenjiArai | 4:fc36c8ec2966 | 34 | #if (MBED_MAJOR_VERSION == 2) |
kenjiArai | 4:fc36c8ec2966 | 35 | #define LOOP_TIME 100 // 100mS |
kenjiArai | 4:fc36c8ec2966 | 36 | #elif (MBED_MAJOR_VERSION == 5) |
kenjiArai | 4:fc36c8ec2966 | 37 | #define LOOP_TIME 50 // 50mS |
kenjiArai | 4:fc36c8ec2966 | 38 | #endif |
kenjiArai | 4:fc36c8ec2966 | 39 | |
kenjiArai | 4:fc36c8ec2966 | 40 | #define DEBUG 0 |
kenjiArai | 4:fc36c8ec2966 | 41 | |
kenjiArai | 4:fc36c8ec2966 | 42 | #if DEBUG |
kenjiArai | 4:fc36c8ec2966 | 43 | #define DBG(...) pc.printf(__VA_ARGS__) |
kenjiArai | 4:fc36c8ec2966 | 44 | #else |
kenjiArai | 4:fc36c8ec2966 | 45 | #define DBG(...) {;} |
kenjiArai | 4:fc36c8ec2966 | 46 | #endif |
kenjiArai | 2:1397a54382ec | 47 | |
kenjiArai | 2:1397a54382ec | 48 | // Constructor ---------------------------------------------------------------- |
kenjiArai | 4:fc36c8ec2966 | 49 | Serial pc(USBTX, USBRX, 115200); |
kenjiArai | 5:e222c59853e2 | 50 | #if defined(TARGET_NRF52840_DK) || defined(TARGET_NRF52_DK) ||\ |
kenjiArai | 5:e222c59853e2 | 51 | defined(TARGET_K64F) |
kenjiArai | 5:e222c59853e2 | 52 | DigitalIn user_sw(BUTTON1, PullUp); |
kenjiArai | 5:e222c59853e2 | 53 | #else |
kenjiArai | 2:1397a54382ec | 54 | DigitalIn user_sw(USER_BUTTON); |
kenjiArai | 5:e222c59853e2 | 55 | #endif |
kenjiArai | 4:fc36c8ec2966 | 56 | #if (MBED_MAJOR_VERSION == 5) |
kenjiArai | 5:e222c59853e2 | 57 | #if defined(TARGET_NRF52840_DK) || defined(TARGET_NRF52_DK) |
kenjiArai | 5:e222c59853e2 | 58 | SDBlockDevice sd(D11, D12, D13, D10, 8000000); |
kenjiArai | 5:e222c59853e2 | 59 | #else |
kenjiArai | 4:fc36c8ec2966 | 60 | SDBlockDevice sd(SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS, 12000000); |
kenjiArai | 5:e222c59853e2 | 61 | #endif |
kenjiArai | 2:1397a54382ec | 62 | FATFileSystem fs("fs"); |
kenjiArai | 2:1397a54382ec | 63 | #endif |
kenjiArai | 4:fc36c8ec2966 | 64 | Timer tmr; |
kenjiArai | 2:1397a54382ec | 65 | |
kenjiArai | 2:1397a54382ec | 66 | // RAM ------------------------------------------------------------------------ |
kenjiArai | 2:1397a54382ec | 67 | |
kenjiArai | 2:1397a54382ec | 68 | // ROM / Constant data -------------------------------------------------------- |
kenjiArai | 4:fc36c8ec2966 | 69 | const char *const opening_msg0 = "microSD Card test program"; |
kenjiArai | 2:1397a54382ec | 70 | #if (MBED_MAJOR_VERSION == 2) |
kenjiArai | 2:1397a54382ec | 71 | char *const opening_msg1 = " -> run on Mbed Classic\r\n"; |
kenjiArai | 2:1397a54382ec | 72 | #elif (MBED_MAJOR_VERSION == 5) |
kenjiArai | 4:fc36c8ec2966 | 73 | const char *const opening_msg1 = " -> run on Mbed OS-5\r\n"; |
kenjiArai | 2:1397a54382ec | 74 | #endif |
kenjiArai | 2:1397a54382ec | 75 | |
kenjiArai | 2:1397a54382ec | 76 | // Function prototypes -------------------------------------------------------- |
kenjiArai | 2:1397a54382ec | 77 | |
kenjiArai | 2:1397a54382ec | 78 | //------------------------------------------------------------------------------ |
kenjiArai | 2:1397a54382ec | 79 | // Control Program |
kenjiArai | 2:1397a54382ec | 80 | //------------------------------------------------------------------------------ |
kenjiArai | 2:1397a54382ec | 81 | int main() |
kenjiArai | 2:1397a54382ec | 82 | { |
kenjiArai | 2:1397a54382ec | 83 | time_t seconds; |
kenjiArai | 2:1397a54382ec | 84 | uint32_t data0 = 10000U; |
kenjiArai | 2:1397a54382ec | 85 | uint32_t data1 = 20000U; |
kenjiArai | 2:1397a54382ec | 86 | uint32_t data2 = 30000U; |
kenjiArai | 2:1397a54382ec | 87 | uint32_t data3 = 40000U; |
kenjiArai | 2:1397a54382ec | 88 | uint32_t data4 = 50000U; |
kenjiArai | 2:1397a54382ec | 89 | uint32_t data5 = 60000U; |
kenjiArai | 2:1397a54382ec | 90 | |
kenjiArai | 4:fc36c8ec2966 | 91 | if (user_sw == USER_SW_ON) { |
kenjiArai | 4:fc36c8ec2966 | 92 | mon(); |
kenjiArai | 4:fc36c8ec2966 | 93 | } |
kenjiArai | 4:fc36c8ec2966 | 94 | DBG("line:%d\r\n", __LINE__); |
kenjiArai | 2:1397a54382ec | 95 | #if (MBED_MAJOR_VERSION == 5) |
kenjiArai | 2:1397a54382ec | 96 | /* Init SD CARD reader */ |
kenjiArai | 2:1397a54382ec | 97 | sd.init(); |
kenjiArai | 2:1397a54382ec | 98 | fs.mount(&sd); |
kenjiArai | 4:fc36c8ec2966 | 99 | #else |
kenjiArai | 4:fc36c8ec2966 | 100 | SDFileSystem sd(D11, D12, D13, D10, "fs"); // do,di,clk,cs |
kenjiArai | 4:fc36c8ec2966 | 101 | wait_ms(100); |
kenjiArai | 2:1397a54382ec | 102 | #endif |
kenjiArai | 2:1397a54382ec | 103 | FILE* fp = fopen("/fs/mydata.txt", "a"); |
kenjiArai | 2:1397a54382ec | 104 | if (fp != 0) { |
kenjiArai | 4:fc36c8ec2966 | 105 | DBG("line:%d\r\n", __LINE__); |
kenjiArai | 2:1397a54382ec | 106 | pc.printf("%s%s", opening_msg0, opening_msg1); |
kenjiArai | 2:1397a54382ec | 107 | fprintf(fp,"%s%s", opening_msg0, opening_msg1); |
kenjiArai | 6:b4538dd09336 | 108 | fclose(fp); |
kenjiArai | 2:1397a54382ec | 109 | } else { |
kenjiArai | 2:1397a54382ec | 110 | pc.printf("ERROR\r\n"); |
kenjiArai | 2:1397a54382ec | 111 | } |
kenjiArai | 2:1397a54382ec | 112 | while (pc.readable()) { |
kenjiArai | 2:1397a54382ec | 113 | char c = pc.getc(); // dummy read |
kenjiArai | 2:1397a54382ec | 114 | } |
kenjiArai | 2:1397a54382ec | 115 | while (true) { |
kenjiArai | 4:fc36c8ec2966 | 116 | DBG("line:%d\r\n", __LINE__); |
kenjiArai | 4:fc36c8ec2966 | 117 | tmr.reset(); |
kenjiArai | 4:fc36c8ec2966 | 118 | tmr.start(); |
kenjiArai | 5:e222c59853e2 | 119 | uint32_t size_disk = get_disk_freespace(); |
kenjiArai | 5:e222c59853e2 | 120 | uint32_t size_file = get_data_file_size("mydata.txt"); |
kenjiArai | 6:b4538dd09336 | 121 | pc.printf("free disk:%8u, file:%8u ", size_disk, size_file); |
kenjiArai | 2:1397a54382ec | 122 | fp = fopen("/fs/mydata.txt", "a"); |
kenjiArai | 2:1397a54382ec | 123 | if(fp != 0) { |
kenjiArai | 2:1397a54382ec | 124 | char tmp[64]; |
kenjiArai | 4:fc36c8ec2966 | 125 | DBG("line:%d\r\n", __LINE__); |
kenjiArai | 2:1397a54382ec | 126 | seconds = time(NULL); |
kenjiArai | 2:1397a54382ec | 127 | strftime(tmp, 64, "DATE %H:%M:%S,%Y/%m/%d,", localtime(&seconds)); |
kenjiArai | 4:fc36c8ec2966 | 128 | pc.printf("%s", tmp); |
kenjiArai | 2:1397a54382ec | 129 | fprintf(fp, "%s", tmp); |
kenjiArai | 2:1397a54382ec | 130 | pc.printf("%08d;%08d;%08d;%08d;%08d;%08d\r\n", |
kenjiArai | 2:1397a54382ec | 131 | ++data0, ++data1, ++data2, ++data3, ++data4, ++data5); |
kenjiArai | 2:1397a54382ec | 132 | fprintf(fp, "%08d;%08d;%08d;%08d;%08d;%08d\r\n", |
kenjiArai | 4:fc36c8ec2966 | 133 | data0, data1, data2, data3, data4, data5); |
kenjiArai | 6:b4538dd09336 | 134 | fclose(fp); |
kenjiArai | 2:1397a54382ec | 135 | } else { |
kenjiArai | 2:1397a54382ec | 136 | pc.printf("ERROR\r\n"); |
kenjiArai | 2:1397a54382ec | 137 | } |
kenjiArai | 4:fc36c8ec2966 | 138 | uint32_t time_sd = tmr.read_ms(); |
kenjiArai | 5:e222c59853e2 | 139 | pc.printf("time:%3d ", time_sd); |
kenjiArai | 2:1397a54382ec | 140 | #if (MBED_MAJOR_VERSION == 2) |
kenjiArai | 4:fc36c8ec2966 | 141 | if (time_sd < LOOP_TIME -2){ |
kenjiArai | 4:fc36c8ec2966 | 142 | wait_ms(LOOP_TIME - time_sd); |
kenjiArai | 4:fc36c8ec2966 | 143 | } |
kenjiArai | 2:1397a54382ec | 144 | #elif (MBED_MAJOR_VERSION == 5) |
kenjiArai | 4:fc36c8ec2966 | 145 | if (time_sd < LOOP_TIME -2){ |
kenjiArai | 4:fc36c8ec2966 | 146 | ThisThread::sleep_for(LOOP_TIME - time_sd); |
kenjiArai | 4:fc36c8ec2966 | 147 | } |
kenjiArai | 2:1397a54382ec | 148 | #endif |
kenjiArai | 2:1397a54382ec | 149 | if (user_sw == USER_SW_ON) { |
kenjiArai | 2:1397a54382ec | 150 | break; |
kenjiArai | 2:1397a54382ec | 151 | } |
kenjiArai | 2:1397a54382ec | 152 | if (pc.readable()) { |
kenjiArai | 2:1397a54382ec | 153 | mon(); |
kenjiArai | 2:1397a54382ec | 154 | } |
kenjiArai | 2:1397a54382ec | 155 | } |
kenjiArai | 2:1397a54382ec | 156 | while(true) { |
kenjiArai | 2:1397a54382ec | 157 | mon(); |
kenjiArai | 4:fc36c8ec2966 | 158 | system_reset(); |
kenjiArai | 2:1397a54382ec | 159 | } |
kenjiArai | 2:1397a54382ec | 160 | } |