// USB Flash Drive Test Program // This program tests USBHostMSD without running the RTOS thread. // The program is running properly executing RTC interrupt // writting timestamp to a file after USB Flash drive is plugged in. #include "mbed.h" #include "USBHostMSD.h" #include "TextLCD.h" #include "RTC.h" #define CLOCK_IS_TICKING (1U) // TextLCD lcd(p15, p16, p17, p18, p19, p20, TextLCD::LCD20x4); // rs, e, d4-d7 TextLCD lcd(p13, p16, p14, p17, p18, p19, p20, TextLCD::LCD20x4); // rs, e, rw, d4-d7 bool Set_RTC_Timer(void); bool Read_RTC_Timer(void); void Clock_Timer(void); void Test_USB_MSD(void); uint8_t gClock_Counter; uint8_t gPrev_Clock_Counter; /***************************************************************************************************************************/ int main(void) { bool RTC_clock_set_status; gClock_Counter = gPrev_Clock_Counter = 0; RTC_clock_set_status = Read_RTC_Timer(); if (RTC_clock_set_status != true) RTC_clock_set_status = Set_RTC_Timer(); if (RTC_clock_set_status != true) { lcd.cls(); lcd.locate(0, 1); lcd.printf(" RTC Timer Error! "); while(1); } RTC::attach(&Clock_Timer, RTC::Second); while (CLOCK_IS_TICKING) { lcd.cls(); lcd.locate(0, 0); lcd.printf("Waiting for USB ... "); // Waiting for USB connection Test_USB_MSD(); lcd.locate(0, 3); lcd.printf("USB is disconnected!"); wait(3); } // end of while (CLOCK_IS_TICKING) loop } /***************************************************************************************************************************/ void Clock_Timer(void) { gClock_Counter = (gClock_Counter < 2)? (gClock_Counter + 1) : 0; } /***************************************************************************************************************************/ void Test_USB_MSD(void) { USBHostMSD msd("usb"); char buffer[32]; char buffer2[32]; time_t seconds_time; uint8_t i = 0; bool gUSB_Done_Flag = false; while(!msd.connect()) // try to connect a MSD device { if (gPrev_Clock_Counter != gClock_Counter) { seconds_time = time(NULL); // read time from RTC strftime(buffer, 32, "%I:%M:%S %p", localtime(&seconds_time)); lcd.locate(4, 1); lcd.printf("%s", buffer); gPrev_Clock_Counter = gClock_Counter; } } lcd.locate(0, 0); lcd.printf("USB is Connected! "); // if the device is disconnected then exit the while loop while (gUSB_Done_Flag != true) // in a loop, appending to a file { if (gPrev_Clock_Counter != gClock_Counter) { seconds_time = time(NULL); // read time from RTC strftime(buffer, 32, "%I:%M:%S %p", localtime(&seconds_time)); strftime(buffer2, 32, "%Y-%m-%d", localtime(&seconds_time)); gPrev_Clock_Counter = gClock_Counter; // append a file FILE * fp = fopen("/usb/test1.txt", "a"); if (fp != NULL) { fprintf(fp, "Today's Date is %s %s \r\n", buffer2, buffer); // Write Date and Time to a Flash Drive's Text file fclose(fp); lcd.locate(0, 1); lcd.printf("Writing to USB! %d", i++); lcd.locate(4, 2); lcd.printf("%s", buffer); } else { lcd.locate(0, 2); lcd.printf("Oops, a File Error! "); } } if (!msd.connected()) // if device disconnected then exit the thread gUSB_Done_Flag = true; } // end of while (gUSB_Done_Flag != true) loop } /***********************************************************************************************************/ bool Set_RTC_Timer(void) { struct tm RTC_clock_param; bool RTC_clock_set_status = false; RTC_clock_param.tm_year = (100 + 15); // time from 1900 = (100 + 15) RTC_clock_param.tm_mon = 5; // 0 is January , 5 Is June etc. RTC_clock_param.tm_mday = 18; RTC_clock_param.tm_wday = 2; // 0 is Monday, 2 is Wednesday etc. RTC_clock_param.tm_hour = 12; // 24 hour format (0-23) RTC_clock_param.tm_min = 5; RTC_clock_param.tm_sec = 0; time_t seconds_clock_time = mktime(&RTC_clock_param); if (seconds_clock_time == (time_t) -1) { // Error in clock setting RTC_clock_set_status = false; } else { set_time(seconds_clock_time); // set the RTC Time RTC_clock_set_status = true; } return RTC_clock_set_status; } /***********************************************************************************************************/ bool Read_RTC_Timer(void) { // int week_day, mday, month, hours, minutes, seconds; int year; bool RTC_clock_set_status = false; time_t seconds_clock_time = time(NULL); tm *RTC_clock_param; RTC_clock_param = localtime(&seconds_clock_time); year = (RTC_clock_param->tm_year + 1900); // week_day = RTC_clock_param->tm_wday; // mday = RTC_clock_param->tm_mday; // month = (RTC_clock_param->tm_mon + 1); // hours = RTC_clock_param->tm_hour; // minutes = RTC_clock_param->tm_min; // seconds = RTC_clock_param->tm_sec; if (year > 2014) { RTC_clock_set_status = true; } return RTC_clock_set_status; } /***********************************************************************************************************/