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/
Diff: main.cpp
- Revision:
- 7:29fece78d927
- Parent:
- 6:7f8bc932cede
--- 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(); } }