USB Memory Control program for DISCO-F469NI

Dependencies:   FatFs_Mon USBHOST

Files at this revision

API Documentation at this revision

Comitter:
kenjiArai
Date:
Mon Apr 30 05:43:40 2018 +0000
Commit message:
USB Memory Control program for DISCO-F469NI

Changed in this revision

FatFs_Mon.lib Show annotated file Show diff for this revision Revisions of this file
USBHALHost_STM_TARGET.h.txt Show annotated file Show diff for this revision Revisions of this file
USBHOST.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-os.lib Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 2661952973e6 FatFs_Mon.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/FatFs_Mon.lib	Mon Apr 30 05:43:40 2018 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/kenjiArai/code/FatFs_Mon/#6f52c89729de
diff -r 000000000000 -r 2661952973e6 USBHALHost_STM_TARGET.h.txt
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBHALHost_STM_TARGET.h.txt	Mon Apr 30 05:43:40 2018 +0000
@@ -0,0 +1,59 @@
+
+USBHOST Revision -> 6:d3ac9e: 26 Apr 2017
+
+https://os.mbed.com/teams/ST/code/USBHOST/
+
+USBHOST\USBHost\TARGET_STM\USBHALHost_STM_TARGET.h
+line 19
+
+---------- Original -----------------------------
+/*  144 pins boards */
+#if defined(TARGET_NUCLEO_F429ZI) || defined(TARGET_NUCLEO_F446ZE)  || defined(TARGET_NUCLEO_F207ZG) \
+|| defined(TARGET_NUCLEO_F767ZI) || defined(TARGET_NUCLEO_F746ZG) || defined(TARGET_NUCLEO_F412ZG)
+#include "USBHALHost_STM_144_64pins.h"
+#endif
+
+/*  64 pins boards */
+#if defined(TARGET_NUCLEO_F401RE) || defined(TARGET_NUCLEO_L476RG) || defined(TARGET_NUCLEO_F411RE)
+#define USBHALHOST_64pins
+#include "USBHALHost_STM_144_64pins.h"
+#endif
+
+/*  DISCO board  */
+
+#ifdef TARGET_DISCO_F429ZI
+#include "USBHALHost_DISCOF429ZI.h"
+#endif
+
+#ifdef TARGET_DISCO_L476VG
+#include "USBHALHost_DISCOL476VG.h"
+#endif
+
+
+---------- Modified -----------------------------
+/*  144 pins boards */
+// April 29th, 2018 by JH1PJL --------------------------------------------------
+#if defined(TARGET_NUCLEO_F429ZI) || defined(TARGET_NUCLEO_F446ZE) \
+ || defined(TARGET_NUCLEO_F207ZG) || defined(TARGET_NUCLEO_F767ZI) \
+ || defined(TARGET_NUCLEO_F746ZG) || defined(TARGET_NUCLEO_F412ZG)
+#include "USBHALHost_STM_144_64pins.h"
+#endif
+
+/*  64 pins boards */
+// April 29th, 2018 by JH1PJL --------------------------------------------------
+#if defined(TARGET_NUCLEO_F401RE) || defined(TARGET_NUCLEO_L476RG) \
+ || defined(TARGET_NUCLEO_F411RE) || defined(TARGET_NUCLEO_F446RE) \
+ || defined(TARGET_DISCO_F469NI)
+#define USBHALHOST_64pins
+#include "USBHALHost_STM_144_64pins.h"
+#endif
+
+/*  DISCO board  */
+
+#ifdef TARGET_DISCO_F429ZI
+#include "USBHALHost_DISCOF429ZI.h"
+#endif
+
+#ifdef TARGET_DISCO_L476VG
+#include "USBHALHost_DISCOL476VG.h"
+#endif
diff -r 000000000000 -r 2661952973e6 USBHOST.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBHOST.lib	Mon Apr 30 05:43:40 2018 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/kenjiArai/code/USBHOST/#7ebd364a6be5
diff -r 000000000000 -r 2661952973e6 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Mon Apr 30 05:43:40 2018 +0000
@@ -0,0 +1,166 @@
+/*
+ * Mbed Application program
+ *
+ * Copyright (c) 2018 Kenji Arai / JH1PJL
+ *  http://www.page.sannet.ne.jp/kenjia/index.html
+ *  http://mbed.org/users/kenjiArai/
+ *      Modify:     March     21st, 2018
+ *      Revised:    April     30th, 2018
+ */
+
+//  Include --------------------------------------------------------------------
+#include    "mbed.h"
+#include    "USBHostMSD.h"
+#include    "FATFileSystem.h"
+#include    "mon.h"
+#include    <stdlib.h>
+
+//  Definition -----------------------------------------------------------------
+
+//  Object ---------------------------------------------------------------------
+DigitalOut  led(LED1);
+DigitalIn   user_sw(USER_BUTTON);
+Serial      pc(USBTX,USBRX);
+USBHostMSD  msd;
+DigitalOut  usb_5v_line(PB_2, 1);
+
+//  RAM ------------------------------------------------------------------------
+uint8_t     usb_memory_status;
+time_t      seconds;
+// ADC
+float       v5;
+float       v3r3;
+
+bool running = true;
+
+//  ROM / Constant data --------------------------------------------------------
+char *const opngmsg =
+    ""__FILE__ "\r\n"__DATE__ " " __TIME__ " (UTC)\r\n""\r\n";
+
+//  Function prototypes --------------------------------------------------------
+void save_logging_data(uint8_t flg);
+void msd_task(void const *);
+void mon(void);
+
+//------------------------------------------------------------------------------
+//  Control Program
+//------------------------------------------------------------------------------
+int main()
+{
+    pc.puts(opngmsg);
+    Thread msdTask(msd_task, NULL, osPriorityNormal, 1024 * 4);
+    while(true) {
+        led=!led;
+        Thread::wait(500);
+        if (running == false) {
+            mon();
+            NVIC_SystemReset();
+        }
+    }
+}
+
+void msd_task(void const *)
+{
+    int i = 0;
+    FATFileSystem fs("usb");
+    int err;
+
+    pc.printf("wait for usb memory stick insertion\r\n");
+    while(1) {
+
+        // try to connect a MSD device
+        while(!msd.connect()) {
+            Thread::wait(500);
+        }
+        if (fs.mount(&msd) != 0) {
+            continue;
+        } else {
+            pc.printf("file system mounted\r\n");
+        }
+        if  (!msd.connect()) {
+            continue;
+        }
+        running = true;
+        // if device disconnected, try to connect again
+        while (msd.connected()) {
+            save_logging_data(1);
+            if (user_sw == 1) {
+                break;
+            }
+            if (pc.readable()) {
+                char c = pc.getc(); // dummy read
+                mon();
+            }
+            Thread::wait(500);
+        }
+        running = false;
+        while (msd.connected()) {
+            Thread::wait(500);
+        }
+        while (fs.unmount() < 0) {
+            Thread::wait(500);
+            pc.printf("unmount\r\n");
+        }
+    }
+}
+
+// Save several data into the USB Memory
+void save_logging_data(uint8_t flg)
+{
+    char buf[64];   // data buffer for text
+    FILE *fp;
+
+    if (!msd.connect()) {
+        error("USB Flash drive not found.\r\n");
+        usb_memory_status = 1;
+    }
+    fp = fopen("/usb/mydata.txt", "a");
+    if (flg) {
+        pc.printf("/usb/mydata.txt\r\n");
+    }
+    if(fp == NULL) {
+        usb_memory_status = 1;
+        if (flg) {
+            pc.printf( "\r\n Could not open file for write\r\n");
+        }
+    }
+    for (int i = 0; 16 > i; i++) {
+        uint32_t size = get_disk_freespace();
+        pc.printf("free %u kB, ", size);
+        //v3r3 = VREF_VOLT / a_vref.read();
+        v3r3 = 3.321f;
+        //v5 = a_5v.read() * v3r3 * FCT_V5;
+        v5 = 5.012f;
+        sprintf(buf, "$DATA0,");
+        fprintf(fp,buf);
+        // RTC
+        seconds = time(NULL);
+        strftime(buf, 64, "RTC,%H:%M:%S,%Y/%m/%d,", localtime(&seconds));
+        if (flg) {
+            pc.printf("%s", buf);
+        }
+        fprintf(fp,buf);
+        // Write speration ',' into the file
+        //            123456789012345678901234567890
+        sprintf(buf, ",,,,,,,,,,,,,,,,,,,,,,,,,,,,");
+        fprintf(fp,buf);
+        // Write data into the file
+        sprintf(buf, "5V,%6.3f,V3R3,%6.3f,", v5, v3r3);
+        fprintf(fp,buf);
+        if (flg) {
+            pc.printf("Volt, %s", buf);
+        }
+        sprintf(buf, "GPS,%d,USB,%d,CAM,%d,CNT,%04d\r\n",
+                0, usb_memory_status, 1, i);
+        fprintf(fp,buf);
+        fprintf(fp,buf);
+        if (flg) {
+            pc.printf("%s", buf);
+        }
+    }
+    fclose(fp);
+    if (flg) {
+        pc.printf("\r\n");
+    }
+}
+
diff -r 000000000000 -r 2661952973e6 mbed-os.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-os.lib	Mon Apr 30 05:43:40 2018 +0000
@@ -0,0 +1,1 @@
+https://github.com/ARMmbed/mbed-os/#c05d72c3c005fbb7e92c3994c32bda45218ae7fe