USB Memory Control program for DISCO-F469NI
Dependencies: FatFs_Mon USBHOST
main.cpp@0:2661952973e6, 2018-04-30 (annotated)
- Committer:
- kenjiArai
- Date:
- Mon Apr 30 05:43:40 2018 +0000
- Revision:
- 0:2661952973e6
USB Memory Control program for DISCO-F469NI
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kenjiArai | 0:2661952973e6 | 1 | /* |
kenjiArai | 0:2661952973e6 | 2 | * Mbed Application program |
kenjiArai | 0:2661952973e6 | 3 | * |
kenjiArai | 0:2661952973e6 | 4 | * Copyright (c) 2018 Kenji Arai / JH1PJL |
kenjiArai | 0:2661952973e6 | 5 | * http://www.page.sannet.ne.jp/kenjia/index.html |
kenjiArai | 0:2661952973e6 | 6 | * http://mbed.org/users/kenjiArai/ |
kenjiArai | 0:2661952973e6 | 7 | * Modify: March 21st, 2018 |
kenjiArai | 0:2661952973e6 | 8 | * Revised: April 30th, 2018 |
kenjiArai | 0:2661952973e6 | 9 | */ |
kenjiArai | 0:2661952973e6 | 10 | |
kenjiArai | 0:2661952973e6 | 11 | // Include -------------------------------------------------------------------- |
kenjiArai | 0:2661952973e6 | 12 | #include "mbed.h" |
kenjiArai | 0:2661952973e6 | 13 | #include "USBHostMSD.h" |
kenjiArai | 0:2661952973e6 | 14 | #include "FATFileSystem.h" |
kenjiArai | 0:2661952973e6 | 15 | #include "mon.h" |
kenjiArai | 0:2661952973e6 | 16 | #include <stdlib.h> |
kenjiArai | 0:2661952973e6 | 17 | |
kenjiArai | 0:2661952973e6 | 18 | // Definition ----------------------------------------------------------------- |
kenjiArai | 0:2661952973e6 | 19 | |
kenjiArai | 0:2661952973e6 | 20 | // Object --------------------------------------------------------------------- |
kenjiArai | 0:2661952973e6 | 21 | DigitalOut led(LED1); |
kenjiArai | 0:2661952973e6 | 22 | DigitalIn user_sw(USER_BUTTON); |
kenjiArai | 0:2661952973e6 | 23 | Serial pc(USBTX,USBRX); |
kenjiArai | 0:2661952973e6 | 24 | USBHostMSD msd; |
kenjiArai | 0:2661952973e6 | 25 | DigitalOut usb_5v_line(PB_2, 1); |
kenjiArai | 0:2661952973e6 | 26 | |
kenjiArai | 0:2661952973e6 | 27 | // RAM ------------------------------------------------------------------------ |
kenjiArai | 0:2661952973e6 | 28 | uint8_t usb_memory_status; |
kenjiArai | 0:2661952973e6 | 29 | time_t seconds; |
kenjiArai | 0:2661952973e6 | 30 | // ADC |
kenjiArai | 0:2661952973e6 | 31 | float v5; |
kenjiArai | 0:2661952973e6 | 32 | float v3r3; |
kenjiArai | 0:2661952973e6 | 33 | |
kenjiArai | 0:2661952973e6 | 34 | bool running = true; |
kenjiArai | 0:2661952973e6 | 35 | |
kenjiArai | 0:2661952973e6 | 36 | // ROM / Constant data -------------------------------------------------------- |
kenjiArai | 0:2661952973e6 | 37 | char *const opngmsg = |
kenjiArai | 0:2661952973e6 | 38 | ""__FILE__ "\r\n"__DATE__ " " __TIME__ " (UTC)\r\n""\r\n"; |
kenjiArai | 0:2661952973e6 | 39 | |
kenjiArai | 0:2661952973e6 | 40 | // Function prototypes -------------------------------------------------------- |
kenjiArai | 0:2661952973e6 | 41 | void save_logging_data(uint8_t flg); |
kenjiArai | 0:2661952973e6 | 42 | void msd_task(void const *); |
kenjiArai | 0:2661952973e6 | 43 | void mon(void); |
kenjiArai | 0:2661952973e6 | 44 | |
kenjiArai | 0:2661952973e6 | 45 | //------------------------------------------------------------------------------ |
kenjiArai | 0:2661952973e6 | 46 | // Control Program |
kenjiArai | 0:2661952973e6 | 47 | //------------------------------------------------------------------------------ |
kenjiArai | 0:2661952973e6 | 48 | int main() |
kenjiArai | 0:2661952973e6 | 49 | { |
kenjiArai | 0:2661952973e6 | 50 | pc.puts(opngmsg); |
kenjiArai | 0:2661952973e6 | 51 | Thread msdTask(msd_task, NULL, osPriorityNormal, 1024 * 4); |
kenjiArai | 0:2661952973e6 | 52 | while(true) { |
kenjiArai | 0:2661952973e6 | 53 | led=!led; |
kenjiArai | 0:2661952973e6 | 54 | Thread::wait(500); |
kenjiArai | 0:2661952973e6 | 55 | if (running == false) { |
kenjiArai | 0:2661952973e6 | 56 | mon(); |
kenjiArai | 0:2661952973e6 | 57 | NVIC_SystemReset(); |
kenjiArai | 0:2661952973e6 | 58 | } |
kenjiArai | 0:2661952973e6 | 59 | } |
kenjiArai | 0:2661952973e6 | 60 | } |
kenjiArai | 0:2661952973e6 | 61 | |
kenjiArai | 0:2661952973e6 | 62 | void msd_task(void const *) |
kenjiArai | 0:2661952973e6 | 63 | { |
kenjiArai | 0:2661952973e6 | 64 | int i = 0; |
kenjiArai | 0:2661952973e6 | 65 | FATFileSystem fs("usb"); |
kenjiArai | 0:2661952973e6 | 66 | int err; |
kenjiArai | 0:2661952973e6 | 67 | |
kenjiArai | 0:2661952973e6 | 68 | pc.printf("wait for usb memory stick insertion\r\n"); |
kenjiArai | 0:2661952973e6 | 69 | while(1) { |
kenjiArai | 0:2661952973e6 | 70 | |
kenjiArai | 0:2661952973e6 | 71 | // try to connect a MSD device |
kenjiArai | 0:2661952973e6 | 72 | while(!msd.connect()) { |
kenjiArai | 0:2661952973e6 | 73 | Thread::wait(500); |
kenjiArai | 0:2661952973e6 | 74 | } |
kenjiArai | 0:2661952973e6 | 75 | if (fs.mount(&msd) != 0) { |
kenjiArai | 0:2661952973e6 | 76 | continue; |
kenjiArai | 0:2661952973e6 | 77 | } else { |
kenjiArai | 0:2661952973e6 | 78 | pc.printf("file system mounted\r\n"); |
kenjiArai | 0:2661952973e6 | 79 | } |
kenjiArai | 0:2661952973e6 | 80 | if (!msd.connect()) { |
kenjiArai | 0:2661952973e6 | 81 | continue; |
kenjiArai | 0:2661952973e6 | 82 | } |
kenjiArai | 0:2661952973e6 | 83 | running = true; |
kenjiArai | 0:2661952973e6 | 84 | // if device disconnected, try to connect again |
kenjiArai | 0:2661952973e6 | 85 | while (msd.connected()) { |
kenjiArai | 0:2661952973e6 | 86 | save_logging_data(1); |
kenjiArai | 0:2661952973e6 | 87 | if (user_sw == 1) { |
kenjiArai | 0:2661952973e6 | 88 | break; |
kenjiArai | 0:2661952973e6 | 89 | } |
kenjiArai | 0:2661952973e6 | 90 | if (pc.readable()) { |
kenjiArai | 0:2661952973e6 | 91 | char c = pc.getc(); // dummy read |
kenjiArai | 0:2661952973e6 | 92 | mon(); |
kenjiArai | 0:2661952973e6 | 93 | } |
kenjiArai | 0:2661952973e6 | 94 | Thread::wait(500); |
kenjiArai | 0:2661952973e6 | 95 | } |
kenjiArai | 0:2661952973e6 | 96 | running = false; |
kenjiArai | 0:2661952973e6 | 97 | while (msd.connected()) { |
kenjiArai | 0:2661952973e6 | 98 | Thread::wait(500); |
kenjiArai | 0:2661952973e6 | 99 | } |
kenjiArai | 0:2661952973e6 | 100 | while (fs.unmount() < 0) { |
kenjiArai | 0:2661952973e6 | 101 | Thread::wait(500); |
kenjiArai | 0:2661952973e6 | 102 | pc.printf("unmount\r\n"); |
kenjiArai | 0:2661952973e6 | 103 | } |
kenjiArai | 0:2661952973e6 | 104 | } |
kenjiArai | 0:2661952973e6 | 105 | } |
kenjiArai | 0:2661952973e6 | 106 | |
kenjiArai | 0:2661952973e6 | 107 | // Save several data into the USB Memory |
kenjiArai | 0:2661952973e6 | 108 | void save_logging_data(uint8_t flg) |
kenjiArai | 0:2661952973e6 | 109 | { |
kenjiArai | 0:2661952973e6 | 110 | char buf[64]; // data buffer for text |
kenjiArai | 0:2661952973e6 | 111 | FILE *fp; |
kenjiArai | 0:2661952973e6 | 112 | |
kenjiArai | 0:2661952973e6 | 113 | if (!msd.connect()) { |
kenjiArai | 0:2661952973e6 | 114 | error("USB Flash drive not found.\r\n"); |
kenjiArai | 0:2661952973e6 | 115 | usb_memory_status = 1; |
kenjiArai | 0:2661952973e6 | 116 | } |
kenjiArai | 0:2661952973e6 | 117 | fp = fopen("/usb/mydata.txt", "a"); |
kenjiArai | 0:2661952973e6 | 118 | if (flg) { |
kenjiArai | 0:2661952973e6 | 119 | pc.printf("/usb/mydata.txt\r\n"); |
kenjiArai | 0:2661952973e6 | 120 | } |
kenjiArai | 0:2661952973e6 | 121 | if(fp == NULL) { |
kenjiArai | 0:2661952973e6 | 122 | usb_memory_status = 1; |
kenjiArai | 0:2661952973e6 | 123 | if (flg) { |
kenjiArai | 0:2661952973e6 | 124 | pc.printf( "\r\n Could not open file for write\r\n"); |
kenjiArai | 0:2661952973e6 | 125 | } |
kenjiArai | 0:2661952973e6 | 126 | } |
kenjiArai | 0:2661952973e6 | 127 | for (int i = 0; 16 > i; i++) { |
kenjiArai | 0:2661952973e6 | 128 | uint32_t size = get_disk_freespace(); |
kenjiArai | 0:2661952973e6 | 129 | pc.printf("free %u kB, ", size); |
kenjiArai | 0:2661952973e6 | 130 | //v3r3 = VREF_VOLT / a_vref.read(); |
kenjiArai | 0:2661952973e6 | 131 | v3r3 = 3.321f; |
kenjiArai | 0:2661952973e6 | 132 | //v5 = a_5v.read() * v3r3 * FCT_V5; |
kenjiArai | 0:2661952973e6 | 133 | v5 = 5.012f; |
kenjiArai | 0:2661952973e6 | 134 | sprintf(buf, "$DATA0,"); |
kenjiArai | 0:2661952973e6 | 135 | fprintf(fp,buf); |
kenjiArai | 0:2661952973e6 | 136 | // RTC |
kenjiArai | 0:2661952973e6 | 137 | seconds = time(NULL); |
kenjiArai | 0:2661952973e6 | 138 | strftime(buf, 64, "RTC,%H:%M:%S,%Y/%m/%d,", localtime(&seconds)); |
kenjiArai | 0:2661952973e6 | 139 | if (flg) { |
kenjiArai | 0:2661952973e6 | 140 | pc.printf("%s", buf); |
kenjiArai | 0:2661952973e6 | 141 | } |
kenjiArai | 0:2661952973e6 | 142 | fprintf(fp,buf); |
kenjiArai | 0:2661952973e6 | 143 | // Write speration ',' into the file |
kenjiArai | 0:2661952973e6 | 144 | // 123456789012345678901234567890 |
kenjiArai | 0:2661952973e6 | 145 | sprintf(buf, ",,,,,,,,,,,,,,,,,,,,,,,,,,,,"); |
kenjiArai | 0:2661952973e6 | 146 | fprintf(fp,buf); |
kenjiArai | 0:2661952973e6 | 147 | // Write data into the file |
kenjiArai | 0:2661952973e6 | 148 | sprintf(buf, "5V,%6.3f,V3R3,%6.3f,", v5, v3r3); |
kenjiArai | 0:2661952973e6 | 149 | fprintf(fp,buf); |
kenjiArai | 0:2661952973e6 | 150 | if (flg) { |
kenjiArai | 0:2661952973e6 | 151 | pc.printf("Volt, %s", buf); |
kenjiArai | 0:2661952973e6 | 152 | } |
kenjiArai | 0:2661952973e6 | 153 | sprintf(buf, "GPS,%d,USB,%d,CAM,%d,CNT,%04d\r\n", |
kenjiArai | 0:2661952973e6 | 154 | 0, usb_memory_status, 1, i); |
kenjiArai | 0:2661952973e6 | 155 | fprintf(fp,buf); |
kenjiArai | 0:2661952973e6 | 156 | fprintf(fp,buf); |
kenjiArai | 0:2661952973e6 | 157 | if (flg) { |
kenjiArai | 0:2661952973e6 | 158 | pc.printf("%s", buf); |
kenjiArai | 0:2661952973e6 | 159 | } |
kenjiArai | 0:2661952973e6 | 160 | } |
kenjiArai | 0:2661952973e6 | 161 | fclose(fp); |
kenjiArai | 0:2661952973e6 | 162 | if (flg) { |
kenjiArai | 0:2661952973e6 | 163 | pc.printf("\r\n"); |
kenjiArai | 0:2661952973e6 | 164 | } |
kenjiArai | 0:2661952973e6 | 165 | } |
kenjiArai | 0:2661952973e6 | 166 |