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.
Dependencies: SD_DISCO_F469NI BSP_DISCO_F469NI BD_SD_DISCO_F469NI USBHOST
Diff: main.cpp
- Revision:
- 4:0f7797c2d3fe
- Parent:
- 3:7f991a3d4e71
- Child:
- 5:5c29a3f6600b
--- a/main.cpp Wed Apr 04 19:49:22 2018 +0000
+++ b/main.cpp Sat Apr 07 02:10:18 2018 +0000
@@ -1,115 +1,127 @@
-/* Example file of using SD/MMC Block device Library for MBED-OS
- * Copyright 2017 Roy Krikke
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
+/*
+ * Mbed Application program
+ * SD Card file control function with FatFs on Mbed-os5
*
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
+ * Copyright (c) 2018 Kenji Arai / JH1PJL
+ * http://www.page.sannet.ne.jp/kenjia/index.html
+ * https://os.mbed.com/users/kenjiArai/
+ * Created: April 7th, 2018
+ * Revised: April 7th, 2018
*/
-#include "mbed.h"
-#include "FATFileSystem.h"
-#include "SDBlockDeviceDISCOF746NG.h"
-#include <stdio.h>
-#include <errno.h>
+// Include --------------------------------------------------------------------
+#include "mbed.h"
+#include "FATFileSystem.h"
+#include "SDBlockDeviceDISCOF769NI.h"
+#include "mon.h"
+#include <stdlib.h>
+#include <stdio.h>
+#include <errno.h>
-DigitalOut led (LED1);
+// Definition -----------------------------------------------------------------
+#define USER_SW_ON 1
-// Instantiate the Block Device for sd card on DISCO-F746NG
-SDBlockDeviceDISCOF746NG bd;
-FATFileSystem fs ("fs");
+// Constructor ----------------------------------------------------------------
+DigitalOut led(LED1);
+DigitalIn user_sw(USER_BUTTON);
+Serial pc(USBTX, USBRX, 115200);
+// Instantiate the Block Device for sd card on DISCO-F769NI
+SDBlockDeviceDISCOF769NI bd;
+FATFileSystem fs("fs");
-void
-return_error (int ret_val)
-{
- if(ret_val)
- printf ("Failure. %d\r\n", ret_val);
- else
- printf ("done.\r\n");
-}
+// RAM ------------------------------------------------------------------------
+
+// ROM / Constant data --------------------------------------------------------
+char *const opening_msg0 = "microSD Card test program";
+char *const opening_msg1 = " -> run on Mbed OS-5\r\n";
-void
-errno_error (void* ret_val)
-{
- if(ret_val == NULL)
- printf (" Failure. %d \r\n", errno);
- else
- printf (" done.\r\n");
-}
+// Function prototypes --------------------------------------------------------
+void return_error (int ret_val);
+void errno_error (void* ret_val);
-int
-main ()
+//------------------------------------------------------------------------------
+// Control Program
+//------------------------------------------------------------------------------
+int main()
{
- Serial pc (SERIAL_TX, SERIAL_RX);
- pc.baud(115200);
- printf("Start\n");
+ time_t seconds;
+ uint32_t data0 = 10000U;
+ uint32_t data1 = 20000U;
+ uint32_t data2 = 30000U;
+ uint32_t data3 = 40000U;
+ uint32_t data4 = 50000U;
+ uint32_t data5 = 60000U;
+ if (user_sw == USER_SW_ON) {
+ mon();
+ }
+ //pc.printf("line:%d\r\n", __LINE__);
+ pc.printf("\r\nStart\r\n");
int error = 0;
- printf("Welcome to the filesystem example.\r\n"
- "Formatting a FAT, RAM-backed filesystem. ");
- error = FATFileSystem::format(&bd);
- return_error(error);
-
- printf("Mounting the filesystem on \"/fs\". ");
+ pc.printf("Welcome to the filesystem example.\r\n");
+ pc.printf("Mounting the filesystem on \"/fs\". \r\n");
error = fs.mount(&bd);
return_error(error);
- printf("Opening a new file, numbers.txt.");
- FILE* fd = fopen("/fs/numbers.txt", "w");
- errno_error(fd);
-
- for (int i = 0; i < 20; i++) {
- printf("Writing decimal numbers to a file (%d/20)\r", i);
- fprintf(fd, "%d\r\n", i);
+ FILE* fp = fopen("/fs/mydata.txt", "a");
+ errno_error(fp);
+ if (fp != 0) {
+ pc.printf("%s%s", opening_msg0, opening_msg1);
+ fprintf(fp,"%s%s", opening_msg0, opening_msg1);
+ } else {
+ pc.printf("ERROR\r\n");
}
- printf("Writing decimal numbers to a file (20/20) done.\r\n");
-
- printf("Closing file.");
- fclose(fd);
- printf(" done.\r\n");
-
- printf("Re-opening file read-only.");
- fd = fopen("/fs/numbers.txt", "r");
- errno_error(fd);
-
- printf("Dumping file to screen.\r\n");
- char buff[16] = { 0 };
- while(!feof (fd)) {
- int size = fread(&buff[0], 1, 15, fd);
- fwrite(&buff[0], 1, size, stdout);
+ fclose(fp);
+ while (pc.readable()) {
+ char c = pc.getc(); // dummy read
}
- printf("EOF.\r\n");
-
- printf("Closing file.");
- fclose(fd);
- printf(" done.\r\n");
-
- printf("Opening root directory.");
- DIR* dir = opendir("/fs/");
- errno_error(fd);
-
- struct dirent* de;
- printf("Printing all filenames:\r\n");
- while((de = readdir (dir)) != NULL) {
- printf(" %s\r\n", &(de->d_name)[0]);
+ while (true) {
+ uint32_t size = get_disk_freespace();
+ pc.printf("free %u ", size);
+ fp = fopen("/fs/mydata.txt", "a");
+ /*if (size <= DISK_SIZE_LIMIT) {
+ pc.printf("Reached RAM Disk size limitation!!\r\n");
+ break;
+ }*/
+ if(fp != 0) {
+ char tmp[64];
+ seconds = time(NULL);
+ strftime(tmp, 64, "DATE %H:%M:%S,%Y/%m/%d,", localtime(&seconds));
+ pc.printf(tmp);
+ fprintf(fp, "%s", tmp);
+ pc.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);
+ } else {
+ pc.printf("ERROR\r\n");
+ }
+ fclose(fp);
+ Thread::wait(100);
+ if (user_sw == USER_SW_ON) {
+ break;
+ }
+ if (pc.readable()) {
+ mon();
+ }
+ led = !led;
}
-
- printf("Closeing root directory. ");
- error = closedir(dir);
- return_error(error);
- printf("Filesystem Demo complete.\r\n");
-
- // Blink led with 2 Hz
while(true) {
- led = !led;
- wait (0.5);
+ mon();
+ NVIC_SystemReset();
}
}
+
+void return_error (int ret_val)
+{
+ if (ret_val) {
+ pc.printf("retrun error/Failure. %d\r\n", ret_val);
+ }
+}
+
+void errno_error (void* ret_val)
+{
+ if (ret_val == NULL) {
+ pc.printf("error #/Failure. %d \r\n", errno);
+ }
+}