run on mbed-os6.6.0
Num | SD Card pin | Module pin | Pin Name | Port Name | SPI |
---|---|---|---|---|---|
1 | CD/DAT3 | CS | D10 | PB6 | - |
2 | CMD/DI | SDI | D11 | PA_7 | MOSI |
3 | VSS1 | GND | GND | - | - |
4 | VDD | 3.3V | 3V3 | - | - |
5 | CLK | CLK | D13 | PA_5 | SCLK |
6 | VSS2 | GND | GND | - | - |
7 | DAT0/DO | SDO | D12 | PA_6 | MISO |
8 | DAT1 | - | - | - | - |
9 | DAT2 | - | - | - | - |
電源+/-を含め合計6本の結線で接続します。
Nucleoシリーズは、ボード上のLED(プログラムでLED1)がD13に接続されSPIモード設定時にSCLKとなる構成が多いので、LED1制御を止める必要があります。
For DISCO-F769NI
https://os.mbed.com/users/kenjiArai/code/DISCO-F769NI_BD_SD_Card_Control/
For DISCO-F469NI
https://os.mbed.com/users/kenjiArai/code/DISCO-F469NI_BD_SD_Card_Control/
Revision 7:29fece78d927, committed 2021-01-14
- Comitter:
- kenjiArai
- Date:
- Thu Jan 14 00:07:07 2021 +0000
- Parent:
- 6:7f8bc932cede
- Commit message:
- run on mbed-os6.6.0
Changed in this revision
--- a/FatFs_Mon/mon.cpp Sat May 02 03:44:56 2020 +0000 +++ b/FatFs_Mon/mon.cpp Thu Jan 14 00:07:07 2021 +0000 @@ -2,13 +2,14 @@ * mbed Application program for the mbed * FatFs Check program / monitor part * - * Copyright (c) 2015,'18,'19,'20 Kenji Arai / JH1PJL + * Copyright (c) 2015,'18,'19,'20,'21 Kenji Arai / JH1PJL * http://www7b.biglobe.ne.jp/~kenjia/ * https://os.mbed.com/users/kenjiArai/ * Created: May 5th, 2015 * Revised: June 14th, 2015 * Revised: April 7th, 2018 - * Revised: May 2nd, 2020 + * Revised: August 8th, 2020 + * Revised: January 14th, 2021 */ /* @@ -24,35 +25,30 @@ // Include -------------------------------------------------------------------- #include "mbed.h" -#if (MBED_MAJOR_VERSION == 2) -#include "SDFileSystem.h" -#elif (MBED_MAJOR_VERSION == 5) #include "FATFileSystem.h" -#endif #include "ff.h" #include "ffconf.h" #include "diskio.h" #include "mon.h" +#include "uart_as_stdio.h" // Definition ----------------------------------------------------------------- #define DO_DEBUG 0 #if DO_DEBUG -#define DEBUG_LINE pc.printf("line:%d\r\n", __LINE__); +#define DEBUG_LINE printf("line:%d\r\n", __LINE__); #else #define DEBUG_LINE {;} #endif // Com #if 1 -#define BAUD(x) pc.baud(x) -#define GETC(x) pc.getc(x) -#define PUTC(x) pc.putc(x) -#define PUTS(x) pc.puts(x) -#define PRINTF(...) pc.printf(__VA_ARGS__) -#define READABLE(x) pc.readable(x) +#define GETC(x) getc(x) +#define PUTC(x) putc(x) +#define PUTS(x) puts(x) +#define PRINTF(...) printf(__VA_ARGS__) +#define READABLE(x) readable(x) #else -#define BAUD(x) {;} #define GETC(x) {;} #define PUTC(x) {;} #define PRINTF(...) {;} @@ -119,7 +115,7 @@ "x extend commands mode\r\n" "q Return to main\r\n" "t Show current time or Adjust time\r\n" - " e.g. t 20 5 2 10 11 12 -> May 02,'20, 10:11:12\r\n" + " e.g. t 21 1 13 10 11 12 -> January 13,'21, 10:11:12\r\n" "? Help/You know the command\r\n" "\r\n"; @@ -164,12 +160,8 @@ "\r\n"; // Function prototypes -------------------------------------------------------- -#if (MBED_MAJOR_VERSION == 2) -extern SDFileSystem fs; -#elif (MBED_MAJOR_VERSION == 5) extern HeapBlockDevice bd; extern FATFileSystem fs; -#endif static void extended_mon( char *ptr ); static void v_next( char *ptr ); @@ -192,7 +184,6 @@ void get_line (char *buff, int len); // Object --------------------------------------------------------------------- -extern Serial pc; Timer t; //------------------------------------------------------------------------------ @@ -214,23 +205,23 @@ ptr = Linebuf; get_line( ptr, sizeof(Linebuf) ); switch ( *ptr++ ) { - // vol + // vol case 'v' : v_next(ptr); break; - // dir + // dir case 'd' : d_next(ptr); break; - // cd, copy + // cd, copy case 'c' : c_next(ptr); break; - // mkdir + // mkdir case 'm' : m_next(ptr); break; - // ren + // ren case 'r' : r_next(ptr); break; @@ -240,15 +231,15 @@ case 'x' : extended_mon(ptr); break; - // Help + // Help case '?' : PUTS(HelpMsg0); break; - // Exit monitor (return to main()) + // Exit monitor (return to main()) case 'q' : PUTS("Return to main\r\n"); return; - // Not a command + // Not a command default: PUTS("? [HELP]=?"); crlf(); @@ -257,36 +248,30 @@ } } -uint32_t get_disk_freespace(void) +float get_disk_freespace(void) { - long p1; + DWORD p1; UINT s1, s2; FATFS *fs; BYTE res; - if (Dirx == NULL){ + if (Dirx == NULL) { Dirx = new FATFS_DIR; } - char p = NULL; - res = f_opendir(Dirx, &p); + //char p = NULL; + char p[8] = {0}; + res = f_opendir(Dirx, p); if (res) { return 0; } - p1 = s1 = s2 = 0; - for(;;) { - res = f_readdir(Dirx, &Finfo); - if ((res != FR_OK) || !Finfo.fname[0]) break; - if (Finfo.fattrib & AM_DIR) { - s2++; - } else { - s1++; - p1 += Finfo.fsize; - } - } - res = f_getfree(&p, (DWORD*)&p1, &fs); - uint32_t size = p1 * fs->csize * 512; + p1 = 0; + p[0] = '0'; + p[1] = ':'; + p[2] = 0; + res = f_getfree(p, (DWORD*)&p1, &fs); + float size_float = (float)p1 * (float)fs->csize * (float)fs->ssize; if (res == FR_OK) { - return size; + return size_float; } else { return 0; } @@ -296,8 +281,8 @@ { BYTE res; //const char *file_name ="acc_data.txt"; - - if (Dirx == NULL){ + + if (Dirx == NULL) { Dirx = new FATFS_DIR; } char p = NULL; @@ -309,7 +294,7 @@ for(;;) { res = f_readdir(Dirx, &Finfo); if ((res != FR_OK) || !Finfo.fname[0]) break; - if (strcmp(Finfo.fname, file_name) == 0){ + if (strcmp(Finfo.fname, file_name) == 0) { return Finfo.fsize; } } @@ -675,7 +660,7 @@ p2 += s2; if (cnt != s2) break; } - tim = t.read_ms(); + tim = t.elapsed_time().count(); PRINTF("%lu bytes read with %lu kB/sec.\r\n", p2, tim ? (p2 / tim) : 0); break; @@ -702,7 +687,7 @@ p2 += s2; if (cnt != s2) break; } - tim = t.read_ms(); + tim = t.elapsed_time().count(); PRINTF("%lu bytes written with %lu kB/sec.\r\n", p2, tim ? (p2 / tim) : 0); break; @@ -737,8 +722,8 @@ break; #endif #if 0 - /* ft <year> <month> <day> <hour> <min> <sec> <name> - - Change timestamp of an object */ + /* ft <year> <month> <day> <hour> <min> <sec> <name> + - Change timestamp of an object */ case 't' : if (!xatoi(&ptr, &p1) || !xatoi(&ptr, &p2) || !xatoi(&ptr, &p3)) { break; @@ -866,7 +851,7 @@ put_rc(f_mkfs("", (BYTE)p2, (DWORD)p3, Buff, sizeof Buff)); break; #endif /* FF_USE_MKFS */ - /* fz [<size>] - Change/Show R/W length for fr/fw/fx command */ + /* fz [<size>] - Change/Show R/W length for fr/fw/fx command */ case 'z' : if (xatoi(&ptr, &p1) && p1 >= 1 && p1 <= (long)sizeof Buff) blen = p1; @@ -1098,29 +1083,29 @@ if (xatoi(&ptr, &p1)) { t.tm_year = (uint8_t)p1 + 100; - pc.printf("Year:%ld ",p1); + printf("Year:%ld ",p1); xatoi( &ptr, &p1 ); t.tm_mon = (uint8_t)p1 - 1; - pc.printf("Month:%ld ",p1); + printf("Month:%ld ",p1); xatoi( &ptr, &p1 ); t.tm_mday = (uint8_t)p1; - pc.printf("Day:%ld ",p1); + printf("Day:%ld ",p1); xatoi( &ptr, &p1 ); t.tm_hour = (uint8_t)p1; - pc.printf("Hour:%ld ",p1); + printf("Hour:%ld ",p1); xatoi( &ptr, &p1 ); t.tm_min = (uint8_t)p1; - pc.printf("Min:%ld ",p1); + printf("Min:%ld ",p1); xatoi( &ptr, &p1 ); t.tm_sec = (uint8_t)p1; - pc.printf("Sec: %ld \r\n",p1); + printf("Sec: %ld \r\n",p1); seconds = mktime(&t); set_time(seconds); } else { seconds = time(NULL); } strftime(buf, 50, " %B %d,'%y, %H:%M:%S\r\n", localtime(&seconds)); - pc.printf("[Time] %s", buf); + printf("[Time] %s", buf); } // Get key input data @@ -1130,6 +1115,7 @@ int idx = 0; for (;;) { + //DEBUG_LINE c = GETC(); // Added by Kenji Arai / JH1PJL May 9th, 2010 if (c == '\r') {
--- a/FatFs_Mon/mon.h Sat May 02 03:44:56 2020 +0000 +++ b/FatFs_Mon/mon.h Thu Jan 14 00:07:07 2021 +0000 @@ -2,15 +2,16 @@ * mbed Application program for the mbed * FatFs Check program /monitor part * - * Copyright (c) 2015,'18,'19,'20 Kenji Arai / JH1PJL + * Copyright (c) 2015,'18,'19,'20,'21 Kenji Arai / JH1PJL * http://www7b.biglobe.ne.jp/~kenjia/ * https://os.mbed.com/users/kenjiArai/ * Created: May 5th, 2015 * Revised: June 14th, 2015 - * Revised: May 2nd, 2020 + * Revised: August 8th, 2020 + * Revised: January 14th, 2021 */ // Function prototypes -------------------------------------------------------- void mon(void); -uint32_t get_disk_freespace(void); +float get_disk_freespace(void); uint32_t get_data_file_size(const char *const file_name);
--- a/check_revision.cpp Sat May 02 03:44:56 2020 +0000 +++ b/check_revision.cpp Thu Jan 14 00:07:07 2021 +0000 @@ -1,26 +1,22 @@ /* * Check Mbed revision * - * Copyright (c) 2019,'20 Kenji Arai / JH1PJL + * Copyright (c) 2019,'20,'21 Kenji Arai / JH1PJL * http://www7b.biglobe.ne.jp/~kenjia/ * https://os.mbed.com/users/kenjiArai/ * Created: July 17th, 2019 - * Revised: May 2nd, 2020 + * Revised: January 11th, 2021 */ #include "mbed.h" - -// RUN ONLY ON mbed-os-5.15.3 -// https://github.com/ARMmbed/mbed-os/releases/tag/mbed-os-5.15.3 -#if (MBED_MAJOR_VERSION == 5) &&\ - (MBED_MINOR_VERSION == 15) &&\ - (MBED_PATCH_VERSION == 3) -#elif (MBED_MAJOR_VERSION == 2) &&\ - (MBED_MINOR_VERSION == 0) &&\ - (MBED_PATCH_VERSION == 165) + +// RUN ONLY ON mbed-os-6.6.0 +// https://github.com/ARMmbed/mbed-os/releases/tag/mbed-os-6.6.0 +#if (MBED_MAJOR_VERSION == 6) &&\ + (MBED_MINOR_VERSION == 6) &&\ + (MBED_PATCH_VERSION == 0) #else - //#warning "Please use mbed-os-5.15.3 or mbed 2.0.165" - #error "Please use mbed-os-5.15.3 or mbed 2.0.165" +# error "Please use mbed-os-6.6.0" #endif void print_revision(void)
--- a/main.cpp Sat May 02 03:44:56 2020 +0000 +++ b/main.cpp Thu Jan 14 00:07:07 2021 +0000 @@ -1,84 +1,70 @@ /* * Mbed Application program - * SD Card file control function with FatFs on Mbed-os5 or os2 + * SD Card file control function with FatFs on Mbed-os6 * - * Copyright (c) 2018,'19,'20 Kenji Arai / JH1PJL + * Copyright (c) 2018,'19,'20,'21 Kenji Arai / JH1PJL * http://www7b.biglobe.ne.jp/~kenjia/ * https://os.mbed.com/users/kenjiArai/ * Created: April 4th, 2018 - * Revised: May 2nd, 2020 + * Revised: January 14th, 2021 */ /* - Tested board on OS2: - Nucleo-F401RE, -F411RE, -F446RE, -L073RZ, -L152RE, -L476RG - Tested board on OS5.15.0 - Nucleo-F401RE, -F411RE, -F446RE, -L152RE, -L073RZ, -L476RG + Tested board on OS6.6.0 + Nucleo-F446RE + FRDM-K64F + Tested board on OS5.15.0(Expected to work on OS6.6.0) + Nucleo-F401RE, -F411RE, -L152RE, -L073RZ, -L476RG nRF52840-DK, nRF52-DK - FRDM-K64F */ // Include -------------------------------------------------------------------- -#include "mbed.h" -#if (MBED_MAJOR_VERSION == 2) -#include "SDFileSystem.h" -#elif (MBED_MAJOR_VERSION == 5) -#include "SDBlockDevice.h" -#include "FATFileSystem.h" -#endif -#include "mon.h" -#include <stdlib.h> +#include "mbed.h" +#include "SDBlockDevice.h" +#include "FATFileSystem.h" +#include "mon.h" +#include "uart_as_stdio.h" +#include <stdlib.h> // Definition ----------------------------------------------------------------- #define USER_SW_ON 0 -#if (MBED_MAJOR_VERSION == 2) -#define LOOP_TIME 100 // 100mS -#elif (MBED_MAJOR_VERSION == 5) -#define LOOP_TIME 50 // 50mS -#endif +#define LOOP_TIME 150 // 150mS #define DEBUG 0 #if DEBUG -#define DBG(...) pc.printf(__VA_ARGS__) +#define DBG(...) printf(__VA_ARGS__) #else #define DBG(...) {;} #endif +//using namespace std::chrono; + // Constructor ---------------------------------------------------------------- -Serial pc(USBTX, USBRX, 115200); #if defined(TARGET_NRF52840_DK) || defined(TARGET_NRF52_DK) ||\ defined(TARGET_K64F) DigitalIn user_sw(BUTTON1, PullUp); #else DigitalIn user_sw(USER_BUTTON); #endif -#if (MBED_MAJOR_VERSION == 5) #if defined(TARGET_NRF52840_DK) || defined(TARGET_NRF52_DK) SDBlockDevice sd(D11, D12, D13, D10, 8000000); #else SDBlockDevice sd(SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS, 12000000); #endif FATFileSystem fs("fs"); -#endif Timer tmr; // RAM ------------------------------------------------------------------------ // ROM / Constant data -------------------------------------------------------- const char *const opening_msg0 = "microSD Card test program"; -#if (MBED_MAJOR_VERSION == 2) -char *const opening_msg1 = " -> run on Mbed Classic\r\n"; -#elif (MBED_MAJOR_VERSION == 5) -const char *const opening_msg1 = " -> run on Mbed OS-5\r\n"; -#endif +const char *const opening_msg1 = " -> run on Mbed OS-6\r\n"; const char *const opening_msg2 = "microSD Card is ready for use\r\n"; const char *const opening_msg3 = "Please hit any key to start!\r\n"; // Function prototypes -------------------------------------------------------- extern void print_revision(void); -extern uint32_t get_disk_freespace(void); -extern uint32_t get_data_file_size(const char *const file_name); //------------------------------------------------------------------------------ // Control Program @@ -93,68 +79,58 @@ uint32_t data4 = 50000U; uint32_t data5 = 60000U; - pc.printf("\r\n\r\n"); + printf("\r\n\r\n"); print_revision(); DBG("line:%d\r\n", __LINE__); -#if (MBED_MAJOR_VERSION == 5) /* Init SD CARD reader */ sd.init(); fs.mount(&sd); -#else - SDFileSystem sd(D11, D12, D13, D10, "fs"); // do,di,clk,cs - wait_ms(100); -#endif FILE* fp = fopen("/fs/mydata.txt", "a"); if (fp != 0) { DBG("line:%d\r\n", __LINE__); - pc.printf("%s%s", opening_msg0, opening_msg1); + printf("%s%s", opening_msg0, opening_msg1); fprintf(fp,"%s%s", opening_msg0, opening_msg1); - pc.printf("%s", opening_msg2); + printf("%s", opening_msg2); fclose(fp); } else { - pc.printf("ERROR\r\n"); + printf("ERROR\r\n"); } - pc.printf("%s", opening_msg3); - while (pc.readable() == 0) { ;} - char c = pc.getc(); // dummy read + printf("%s", opening_msg3); + while (readable() == 0) { ;} + char c = getc(); // dummy read while (true) { DBG("line:%d\r\n", __LINE__); tmr.reset(); tmr.start(); - uint32_t size_disk = get_disk_freespace(); - uint32_t size_file = get_data_file_size("mydata.txt"); - pc.printf("free disk:%10u, file:%8u ", size_disk, size_file); + float size_disk = get_disk_freespace() / (1024.0f * 1024.0f * 1024.0f); + uint32_t size_file = get_data_file_size("mydata.txt") / 1024; + printf(", free disk: %.3f GB, file: %u kB, ", size_disk, size_file); fp = fopen("/fs/mydata.txt", "a"); if(fp != 0) { char tmp[64]; DBG("line:%d\r\n", __LINE__); seconds = time(NULL); strftime(tmp, 64, "DATE %H:%M:%S,%Y/%m/%d,", localtime(&seconds)); - pc.printf("%s", tmp); + printf("%s", tmp); fprintf(fp, "%s", tmp); - pc.printf("%08d;%08d;%08d;%08d;%08d;%08d\r\n", - ++data0, ++data1, ++data2, ++data3, ++data4, ++data5); + printf("%08d;%08d;%08d;%08d;%08d;%08d\r\n", + ++data0, ++data1, ++data2, ++data3, ++data4, ++data5); fprintf(fp, "%08d;%08d;%08d;%08d;%08d;%08d\r\n", - data0, data1, data2, data3, data4, data5); + data0, data1, data2, data3, data4, data5); fclose(fp); } else { - pc.printf("ERROR\r\n"); + printf("ERROR\r\n"); } - uint32_t time_sd = tmr.read_ms(); - pc.printf("time:%3d ", time_sd); -#if (MBED_MAJOR_VERSION == 2) - if (time_sd < LOOP_TIME -2){ - wait_ms(LOOP_TIME - time_sd); + uint32_t time_sd = chrono::duration_cast<chrono::milliseconds>( + tmr.elapsed_time()).count(); + printf("time:%3d ", time_sd); + if (time_sd < LOOP_TIME -2) { + ThisThread::sleep_for(chrono::milliseconds(LOOP_TIME - time_sd)); } -#elif (MBED_MAJOR_VERSION == 5) - if (time_sd < LOOP_TIME -2){ - ThisThread::sleep_for(LOOP_TIME - time_sd); - } -#endif if (user_sw == USER_SW_ON) { break; } - if (pc.readable()) { + if (readable()) { mon(); } }
--- a/mbed-os.lib Sat May 02 03:44:56 2020 +0000 +++ b/mbed-os.lib Thu Jan 14 00:07:07 2021 +0000 @@ -1,1 +1,1 @@ -https://github.com/ARMmbed/mbed-os/#b6370b4c37f3d4665ed1cdcb1afea85396bba1b3 +https://github.com/ARMmbed/mbed-os/#84d991342a41011fc417aab62c0f5a8832f1d18f
--- a/mbed_app.json Sat May 02 03:44:56 2020 +0000 +++ b/mbed_app.json Thu Jan 14 00:07:07 2021 +0000 @@ -17,6 +17,7 @@ "sd.SPI_CS" : "D0" }, "*": { + "target.printf_lib": "std", "target.components_add": ["SD"], "target.features_add":["STORAGE"] }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/uart_as_stdio/uart_as_stdio.cpp Thu Jan 14 00:07:07 2021 +0000 @@ -0,0 +1,46 @@ +/* + * mbed Application program + * Redirect Standard Input/Output + * + * Copyright (c) 2021 Kenji Arai / JH1PJL + * http://www7b.biglobe.ne.jp/~kenjia/ + * https://os.mbed.com/users/kenjiArai/ + * Created: January 13th, 2021 + * Revised: January 14th, 2021 + */ + +// Include -------------------------------------------------------------------- +#include "mbed.h" + +// Definition ----------------------------------------------------------------- + +// Constructor ---------------------------------------------------------------- +static BufferedSerial pc(USBTX, USBRX, 115200); + +// RAM ------------------------------------------------------------------------ + +// ROM / Constant data -------------------------------------------------------- + +// Function prototypes -------------------------------------------------------- + +//------------------------------------------------------------------------------ +// Control Program +//------------------------------------------------------------------------------ +uint8_t readable() +{ + return pc.readable(); +} + +void putc(uint8_t c) +{ + char dt[4]; + dt[0] = (char)c; + pc.write(dt, 1); +} + +uint8_t getc() +{ + char dt[4]; + pc.read(dt, 1); + return (uint8_t)dt[0]; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/uart_as_stdio/uart_as_stdio.h Thu Jan 14 00:07:07 2021 +0000 @@ -0,0 +1,17 @@ +/* + * mbed Application program + * Redirect Standard Input/Output + * + * Copyright (c) 2021 Kenji Arai / JH1PJL + * http://www7b.biglobe.ne.jp/~kenjia/ + * https://os.mbed.com/users/kenjiArai/ + * Created: January 13th, 2021 + * Revised: January 14th, 2021 + */ + +#include "mbed.h" + +// Function prototypes -------------------------------------------------------- +uint8_t readable(void); +void putc(uint8_t c); +uint8_t getc(void);