Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 /* 00002 * Mbed Application program 00003 * RAM Disck function with FatFs on Mbed-os5 00004 * 00005 * Copyright (c) 2018,'19,'20 Kenji Arai / JH1PJL 00006 * http://www7b.biglobe.ne.jp/~kenjia/ 00007 * https://os.mbed.com/users/kenjiArai/ 00008 * Created: April 7th, 2018 00009 * Revised: May 2nd, 2020 00010 */ 00011 00012 // Include -------------------------------------------------------------------- 00013 #include "mbed.h" 00014 #include "FATFileSystem.h" 00015 #include "HeapBlockDevice.h" 00016 #include "mon.h" 00017 #include <stdlib.h> 00018 #include <stdio.h> 00019 #include <errno.h> 00020 00021 // Definition ----------------------------------------------------------------- 00022 #if defined(TARGET_DISCO_F769NI) || defined(TARGET_DISCO_F746NG) 00023 #define USER_SW_ON 1 00024 #else 00025 #define USER_SW_ON 0 00026 #endif 00027 00028 #define DISK_SIZE_LIMIT (5 * 512) 00029 #define RAM_DISK_SIZE_4MB (8192 * 512) 00030 #define RAM_DISK_SIZE_2MB (4096 * 512) 00031 #define RAM_DISK_SIZE_1MB (2048 * 512) 00032 #define RAM_DISK_SIZE_512KB (1024 * 512) 00033 #define RAM_DISK_SIZE_128KB (256 * 512) 00034 #define RAM_DISK_SIZE_64KB (128 * 512) 00035 00036 #if defined(TARGET_NUCLEO_F401RE) || defined(TARGET_NUCLEO_F411RE) ||\ 00037 defined(TARGET_NUCLEO_F446RE) || defined(TARGET_NUCLEO_L476RG) ||\ 00038 defined(TARGET_NUCLEO_L152RE) 00039 #define RAM_DISK_SIZE RAM_DISK_SIZE_64KB 00040 #elif defined(TARGET_K64F) || defined(TARGET_NRF52840_DK) ||\ 00041 defined(TARGET_DISCO_F746NG) || defined(TARGET_DISCO_F469NI) 00042 #define RAM_DISK_SIZE RAM_DISK_SIZE_128KB 00043 #elif defined(TARGET_GR_LYCHEE) 00044 #define RAM_DISK_SIZE RAM_DISK_SIZE_512KB 00045 #elif defined(TARGET_RZ_A1H) 00046 #define RAM_DISK_SIZE RAM_DISK_SIZE_2MB 00047 #else 00048 #warning "make sure disk size for your board!" 00049 #define RAM_DISK_SIZE RAM_DISK_SIZE_64KB 00050 #endif 00051 00052 #define DEBUG 0 00053 #if DEBUG 00054 #define DBG(...) pc.printf(__VA_ARGS__) 00055 #else 00056 #define DBG(...) {;} 00057 #endif 00058 00059 // Constructor ---------------------------------------------------------------- 00060 DigitalOut led(LED1); 00061 DigitalIn user_sw(BUTTON1, PullUp); 00062 Serial pc(USBTX, USBRX, 115200); 00063 HeapBlockDevice bd(RAM_DISK_SIZE, 512); 00064 FATFileSystem fs("fs"); 00065 Timer tmr; 00066 00067 // RAM ------------------------------------------------------------------------ 00068 00069 // ROM / Constant data -------------------------------------------------------- 00070 const char *const opening_msg0 = "RAM Disk (use Heap area) test program"; 00071 const char *const opening_msg1 = " -> run on Mbed OS-5\r\n"; 00072 const char *const opening_msg2 = "microSD Card is ready for use\r\n"; 00073 const char *const opening_msg3 = "Please hit any key to start!\r\n"; 00074 00075 // Function prototypes -------------------------------------------------------- 00076 void return_error (int ret_val); 00077 void errno_error (void* ret_val); 00078 extern void print_revision(void); 00079 extern uint32_t get_disk_freespace(void); 00080 extern uint32_t get_data_file_size(const char *const file_name); 00081 00082 //------------------------------------------------------------------------------ 00083 // Control Program 00084 //------------------------------------------------------------------------------ 00085 int main() 00086 { 00087 time_t seconds; 00088 uint32_t data0 = 10000U; 00089 uint32_t data1 = 20000U; 00090 uint32_t data2 = 30000U; 00091 uint32_t data3 = 40000U; 00092 uint32_t data4 = 50000U; 00093 uint32_t data5 = 60000U; 00094 00095 pc.printf("\r\n\r\n"); 00096 print_revision(); 00097 DBG("line:%d\r\n", __LINE__); 00098 pc.printf("\r\nStart\r\n"); 00099 int error = 0; 00100 pc.printf("Welcome to the filesystem example.\r\n"); 00101 pc.printf("Formatting a FAT, RAM-backed filesystem.\r\n"); 00102 error = FATFileSystem::format(&bd); 00103 return_error(error); 00104 pc.printf("Mounting the filesystem on \"/fs\". \r\n"); 00105 error = fs.mount(&bd); 00106 return_error(error); 00107 00108 FILE* fp = fopen("/fs/mydata.txt", "a"); 00109 errno_error(fp); 00110 if (fp != 0) { 00111 pc.printf("%s%s", opening_msg0, opening_msg1); 00112 fprintf(fp,"%s%s", opening_msg0, opening_msg1); 00113 pc.printf("%s", opening_msg2); 00114 fclose(fp); 00115 } else { 00116 pc.printf("ERROR\r\n"); 00117 } 00118 pc.printf("%s", opening_msg3); 00119 while (pc.readable() == 0) { ;} 00120 char c = pc.getc(); // dummy read 00121 while (true) { 00122 DBG("line:%3d\r\n", __LINE__); 00123 tmr.reset(); 00124 tmr.start(); 00125 uint32_t size_disk = get_disk_freespace(); 00126 uint32_t size_file = get_data_file_size("mydata.txt"); 00127 pc.printf("free disk:%8u, file:%8u ", size_disk, size_file); 00128 fp = fopen("/fs/mydata.txt", "a"); 00129 if (size_disk <= DISK_SIZE_LIMIT) { 00130 pc.printf("Reached RAM Disk size limitation!!\r\n"); 00131 break; 00132 } 00133 DBG("line:%3d\r\n", __LINE__); 00134 if(fp != 0) { 00135 char tmp[64]; 00136 DBG("line:%d\r\n", __LINE__); 00137 seconds = time(NULL); 00138 strftime(tmp, 64, "DATE %H:%M:%S,%Y/%m/%d,", localtime(&seconds)); 00139 pc.printf("%s", tmp); 00140 fprintf(fp, "%s", tmp); 00141 pc.printf("%08d;%08d;%08d;%08d;%08d;%08d\r\n", 00142 ++data0, ++data1, ++data2, ++data3, ++data4, ++data5); 00143 fprintf(fp, "%08d;%08d;%08d;%08d;%08d;%08d\r\n", 00144 data0, data1, data2, data3, data4, data5); 00145 fclose(fp); 00146 } else { 00147 pc.printf("ERROR\r\n"); 00148 } 00149 uint32_t time_sd = tmr.read_ms(); 00150 pc.printf("time[ms]:%3d ,", time_sd); 00151 ThisThread::sleep_for(20); 00152 if (user_sw == USER_SW_ON) { 00153 break; 00154 } 00155 if (pc.readable()) { 00156 mon(); 00157 } 00158 led = !led; 00159 } 00160 while(true) { 00161 mon(); 00162 system_reset(); 00163 } 00164 } 00165 00166 void return_error (int ret_val) 00167 { 00168 if (ret_val) { 00169 pc.printf("retrun error/Failure. %d\r\n", ret_val); 00170 } 00171 } 00172 00173 void errno_error (void* ret_val) 00174 { 00175 if (ret_val == NULL) { 00176 pc.printf("error #/Failure. %d \r\n", errno); 00177 } 00178 }
Generated on Fri Jul 15 2022 19:36:06 by
 1.7.2
 1.7.2