USB Memory control program using USBHost library

Dependencies:   USBHOST

see /users/kenjiArai/notebook/usb-interface--usbhost-and-usbdevice/

Committer:
kenjiArai
Date:
Sat Jan 04 23:51:21 2020 +0000
Revision:
0:a1ca40de3f45
USB Memory control program using USBHost library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kenjiArai 0:a1ca40de3f45 1 /*
kenjiArai 0:a1ca40de3f45 2 * Mbed Application program
kenjiArai 0:a1ca40de3f45 3 * USB Memory control
kenjiArai 0:a1ca40de3f45 4 *
kenjiArai 0:a1ca40de3f45 5 * Copyright (c) 2019,'20 Kenji Arai / JH1PJL
kenjiArai 0:a1ca40de3f45 6 * http://www.page.sannet.ne.jp/kenjia/index.html
kenjiArai 0:a1ca40de3f45 7 * http://mbed.org/users/kenjiArai/
kenjiArai 0:a1ca40de3f45 8 * Created: March 14th, 2019
kenjiArai 0:a1ca40de3f45 9 * Revised: January 5th, 2020
kenjiArai 0:a1ca40de3f45 10 */
kenjiArai 0:a1ca40de3f45 11
kenjiArai 0:a1ca40de3f45 12 /*
kenjiArai 0:a1ca40de3f45 13 Tested board on OS5.14.2
kenjiArai 0:a1ca40de3f45 14 Nucleo-F401RE, -F411RE, -F446RE, -F446ZE
kenjiArai 0:a1ca40de3f45 15 DISCO-F469NI
kenjiArai 0:a1ca40de3f45 16 */
kenjiArai 0:a1ca40de3f45 17
kenjiArai 0:a1ca40de3f45 18 // Include --------------------------------------------------------------------
kenjiArai 0:a1ca40de3f45 19 #include "mbed.h"
kenjiArai 0:a1ca40de3f45 20 #include "USBHostMSD.h"
kenjiArai 0:a1ca40de3f45 21 #include "mon.h"
kenjiArai 0:a1ca40de3f45 22
kenjiArai 0:a1ca40de3f45 23 // Definition -----------------------------------------------------------------
kenjiArai 0:a1ca40de3f45 24 #define USER_SW_ON 0
kenjiArai 0:a1ca40de3f45 25 #define LOOP_TIME 50 // 50mS
kenjiArai 0:a1ca40de3f45 26
kenjiArai 0:a1ca40de3f45 27 #define DEBUG_PRINT 0
kenjiArai 0:a1ca40de3f45 28 #if DEBUG_PRINT
kenjiArai 0:a1ca40de3f45 29 #define DBG(...) pc.printf(__VA_ARGS__)
kenjiArai 0:a1ca40de3f45 30 #else
kenjiArai 0:a1ca40de3f45 31 #define DBG(...) {;}
kenjiArai 0:a1ca40de3f45 32 #endif
kenjiArai 0:a1ca40de3f45 33
kenjiArai 0:a1ca40de3f45 34 // Object ---------------------------------------------------------------------
kenjiArai 0:a1ca40de3f45 35 DigitalIn user_sw(BUTTON1, PullUp);
kenjiArai 0:a1ca40de3f45 36 Serial pc(USBTX, USBRX, 115200);
kenjiArai 0:a1ca40de3f45 37 Timer tmr;
kenjiArai 0:a1ca40de3f45 38
kenjiArai 0:a1ca40de3f45 39 // RAM ------------------------------------------------------------------------
kenjiArai 0:a1ca40de3f45 40
kenjiArai 0:a1ca40de3f45 41 // ROM / Constant data --------------------------------------------------------
kenjiArai 0:a1ca40de3f45 42
kenjiArai 0:a1ca40de3f45 43 // Function prototypes --------------------------------------------------------
kenjiArai 0:a1ca40de3f45 44 void no_good_and_wait_reset(void);
kenjiArai 0:a1ca40de3f45 45
kenjiArai 0:a1ca40de3f45 46 //------------------------------------------------------------------------------
kenjiArai 0:a1ca40de3f45 47 // Control Program
kenjiArai 0:a1ca40de3f45 48 //------------------------------------------------------------------------------
kenjiArai 0:a1ca40de3f45 49 int main()
kenjiArai 0:a1ca40de3f45 50 {
kenjiArai 0:a1ca40de3f45 51 uint32_t data0 = 10000U;
kenjiArai 0:a1ca40de3f45 52 uint32_t data1 = 20000U;
kenjiArai 0:a1ca40de3f45 53 uint32_t data2 = 30000U;
kenjiArai 0:a1ca40de3f45 54 uint32_t data3 = 40000U;
kenjiArai 0:a1ca40de3f45 55 uint32_t data4 = 50000U;
kenjiArai 0:a1ca40de3f45 56 uint32_t data5 = 60000U;
kenjiArai 0:a1ca40de3f45 57
kenjiArai 0:a1ca40de3f45 58 DBG("line:%d\r\n", __LINE__);
kenjiArai 0:a1ca40de3f45 59 while (pc.readable()) {
kenjiArai 0:a1ca40de3f45 60 pc.getc();
kenjiArai 0:a1ca40de3f45 61 }
kenjiArai 0:a1ca40de3f45 62 pc.printf("\r\n\r\nPlease connect USB memory! ");
kenjiArai 0:a1ca40de3f45 63 pc.printf("If you have connected it, please hit any key to go next.\r\n");
kenjiArai 0:a1ca40de3f45 64 while (pc.readable() == 0) {
kenjiArai 0:a1ca40de3f45 65 ThisThread::sleep_for(100);
kenjiArai 0:a1ca40de3f45 66 }
kenjiArai 0:a1ca40de3f45 67 pc.getc();
kenjiArai 0:a1ca40de3f45 68 pc.printf("\nStart USB memory test.\r\n");
kenjiArai 0:a1ca40de3f45 69
kenjiArai 0:a1ca40de3f45 70 USBHostMSD msd;
kenjiArai 0:a1ca40de3f45 71 DBG("line:%d\r\n", __LINE__);
kenjiArai 0:a1ca40de3f45 72 msd.init();
kenjiArai 0:a1ca40de3f45 73
kenjiArai 0:a1ca40de3f45 74 uint32_t out = 50;
kenjiArai 0:a1ca40de3f45 75 DBG("line:%d\r\n", __LINE__);
kenjiArai 0:a1ca40de3f45 76 while(!msd.connect()) {
kenjiArai 0:a1ca40de3f45 77 if (--out == 0) {
kenjiArai 0:a1ca40de3f45 78 DBG("line:%d\r\n", __LINE__);
kenjiArai 0:a1ca40de3f45 79 pc.printf("Cannot detect USB memory!! \r\n");
kenjiArai 0:a1ca40de3f45 80 break;
kenjiArai 0:a1ca40de3f45 81 }
kenjiArai 0:a1ca40de3f45 82 pc.putc('.');
kenjiArai 0:a1ca40de3f45 83 ThisThread::sleep_for(1000);
kenjiArai 0:a1ca40de3f45 84 }
kenjiArai 0:a1ca40de3f45 85 if (out != 0) {
kenjiArai 0:a1ca40de3f45 86 pc.printf("\r\nDetect USB memory on USB connector.\r\n");
kenjiArai 0:a1ca40de3f45 87 } else {
kenjiArai 0:a1ca40de3f45 88 DBG("line:%d\r\n", __LINE__);
kenjiArai 0:a1ca40de3f45 89 no_good_and_wait_reset();
kenjiArai 0:a1ca40de3f45 90 }
kenjiArai 0:a1ca40de3f45 91 FATFileSystem fs("usb");
kenjiArai 0:a1ca40de3f45 92 DBG("line:%d\r\n", __LINE__);
kenjiArai 0:a1ca40de3f45 93 FILE *fp;
kenjiArai 0:a1ca40de3f45 94 time_t seconds;
kenjiArai 0:a1ca40de3f45 95
kenjiArai 0:a1ca40de3f45 96 if (fs.mount(&msd) == 0) {
kenjiArai 0:a1ca40de3f45 97 DBG("line:%d\r\n", __LINE__);
kenjiArai 0:a1ca40de3f45 98 fp = fopen("/usb/mydata.txt", "a");
kenjiArai 0:a1ca40de3f45 99 DBG("line:%d\r\n", __LINE__);
kenjiArai 0:a1ca40de3f45 100 if (fp == 0) {
kenjiArai 0:a1ca40de3f45 101 pc.printf("Cannot write a file into USB memory.\r\n");
kenjiArai 0:a1ca40de3f45 102 no_good_and_wait_reset();
kenjiArai 0:a1ca40de3f45 103 } else {
kenjiArai 0:a1ca40de3f45 104 pc.printf("Succeed file open, write and close.\r\n");
kenjiArai 0:a1ca40de3f45 105 fclose(fp);
kenjiArai 0:a1ca40de3f45 106 }
kenjiArai 0:a1ca40de3f45 107 }
kenjiArai 0:a1ca40de3f45 108 DBG("line:%d\r\n", __LINE__);
kenjiArai 0:a1ca40de3f45 109 while(pc.readable()) {
kenjiArai 0:a1ca40de3f45 110 DBG("line:%d\r\n", __LINE__);
kenjiArai 0:a1ca40de3f45 111 pc.getc();
kenjiArai 0:a1ca40de3f45 112 }
kenjiArai 0:a1ca40de3f45 113 pc.printf("Start contunuos file write.\r\n");
kenjiArai 0:a1ca40de3f45 114 pc.printf("If you want to stop, please hit any key!\r\n");
kenjiArai 0:a1ca40de3f45 115 uint32_t num = 0;
kenjiArai 0:a1ca40de3f45 116 char buf[64];
kenjiArai 0:a1ca40de3f45 117 while (pc.readable() == 0) {
kenjiArai 0:a1ca40de3f45 118 ThisThread::sleep_for(100);
kenjiArai 0:a1ca40de3f45 119 }
kenjiArai 0:a1ca40de3f45 120 pc.getc();
kenjiArai 0:a1ca40de3f45 121 pc.printf("Time[mS]: ");
kenjiArai 0:a1ca40de3f45 122 while(true) {
kenjiArai 0:a1ca40de3f45 123 tmr.reset();
kenjiArai 0:a1ca40de3f45 124 tmr.start();
kenjiArai 0:a1ca40de3f45 125 uint32_t size_disk = get_disk_freespace();
kenjiArai 0:a1ca40de3f45 126 uint32_t size_file = get_data_file_size("mydata.txt");
kenjiArai 0:a1ca40de3f45 127 pc.printf("free disk:%10u, file:%8u ", size_disk, size_file);
kenjiArai 0:a1ca40de3f45 128 fp = fopen("/usb/mydata.txt", "a");
kenjiArai 0:a1ca40de3f45 129 if(fp != 0) {
kenjiArai 0:a1ca40de3f45 130 char tmp[64];
kenjiArai 0:a1ca40de3f45 131 DBG("line:%d\r\n", __LINE__);
kenjiArai 0:a1ca40de3f45 132 seconds = time(NULL);
kenjiArai 0:a1ca40de3f45 133 strftime(tmp, 64, "DATE %H:%M:%S,%Y/%m/%d,", localtime(&seconds));
kenjiArai 0:a1ca40de3f45 134 pc.printf("%s", tmp);
kenjiArai 0:a1ca40de3f45 135 fprintf(fp, "%s", tmp);
kenjiArai 0:a1ca40de3f45 136 pc.printf("%08d;%08d;%08d;%08d;%08d;%08d\r\n",
kenjiArai 0:a1ca40de3f45 137 ++data0, ++data1, ++data2, ++data3, ++data4, ++data5);
kenjiArai 0:a1ca40de3f45 138 fprintf(fp, "%08d;%08d;%08d;%08d;%08d;%08d\r\n",
kenjiArai 0:a1ca40de3f45 139 data0, data1, data2, data3, data4, data5);
kenjiArai 0:a1ca40de3f45 140 fclose(fp);
kenjiArai 0:a1ca40de3f45 141 } else {
kenjiArai 0:a1ca40de3f45 142 pc.printf("ERROR\r\n");
kenjiArai 0:a1ca40de3f45 143 }
kenjiArai 0:a1ca40de3f45 144 uint32_t time_sd = tmr.read_ms();
kenjiArai 0:a1ca40de3f45 145 pc.printf("Time[mS]:%4d ", time_sd);
kenjiArai 0:a1ca40de3f45 146 if (time_sd < LOOP_TIME -2){
kenjiArai 0:a1ca40de3f45 147 ThisThread::sleep_for(LOOP_TIME - time_sd);
kenjiArai 0:a1ca40de3f45 148 }
kenjiArai 0:a1ca40de3f45 149 if (user_sw == USER_SW_ON) {
kenjiArai 0:a1ca40de3f45 150 break;
kenjiArai 0:a1ca40de3f45 151 }
kenjiArai 0:a1ca40de3f45 152 if (pc.readable()) {
kenjiArai 0:a1ca40de3f45 153 break;
kenjiArai 0:a1ca40de3f45 154 }
kenjiArai 0:a1ca40de3f45 155 }
kenjiArai 0:a1ca40de3f45 156 pc.getc();
kenjiArai 0:a1ca40de3f45 157 pc.printf("\r\nStop file write.\r\n");
kenjiArai 0:a1ca40de3f45 158 pc.printf("Do you want to check detail?\r\n");
kenjiArai 0:a1ca40de3f45 159 pc.printf("If so, please enter 'y'.\r\n");
kenjiArai 0:a1ca40de3f45 160 if (pc.getc() == 'y') {
kenjiArai 0:a1ca40de3f45 161 mon();
kenjiArai 0:a1ca40de3f45 162 }
kenjiArai 0:a1ca40de3f45 163 pc.printf("If you want to tray again, ");
kenjiArai 0:a1ca40de3f45 164 no_good_and_wait_reset();
kenjiArai 0:a1ca40de3f45 165 }
kenjiArai 0:a1ca40de3f45 166
kenjiArai 0:a1ca40de3f45 167 void no_good_and_wait_reset()
kenjiArai 0:a1ca40de3f45 168 {
kenjiArai 0:a1ca40de3f45 169 pc.printf("Please enter [Alt]+B for re-start.\r\n");
kenjiArai 0:a1ca40de3f45 170 while(true) {
kenjiArai 0:a1ca40de3f45 171 ThisThread::sleep_for(1000);
kenjiArai 0:a1ca40de3f45 172 }
kenjiArai 0:a1ca40de3f45 173 }