![](/media/cache/profiles/012d0b8e9950a1168ab4bafe9b73750b.jpg.50x50_q85.jpg)
it is test project using DISCO-F469NI target board
Dependencies: BSP_DISCO_F469NI LCD_DISCO_F469NI USBHOST
main.cpp@4:7dfa826f04ba, 2017-08-08 (annotated)
- Committer:
- bcjun@aname.co.kr
- Date:
- Tue Aug 08 16:22:00 2017 +0900
- Revision:
- 4:7dfa826f04ba
- Parent:
- 3:b61f4db32ed5
change usbhost library that forked in my account
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bcjun@aname.co.kr | 3:b61f4db32ed5 | 1 | #include <stdlib.h> |
bcjun@aname.co.kr | 3:b61f4db32ed5 | 2 | |
jeonbc | 0:53746acfed69 | 3 | #include "mbed.h" |
bcjun@aname.co.kr | 3:b61f4db32ed5 | 4 | //#include "LCD_DISCO_F469NI.h" |
bcjun@aname.co.kr | 3:b61f4db32ed5 | 5 | #include "USBHostMSD.h" |
bcjun@aname.co.kr | 3:b61f4db32ed5 | 6 | #include "FATFileSystem.h" |
bcjun@aname.co.kr | 2:1fb6f946b69b | 7 | |
bcjun@aname.co.kr | 3:b61f4db32ed5 | 8 | //LCD_DISCO_F469NI lcd; |
jeonbc | 0:53746acfed69 | 9 | |
jeonbc | 0:53746acfed69 | 10 | DigitalOut led_green(LED1); |
jeonbc | 0:53746acfed69 | 11 | DigitalOut led_orange(LED2); |
jeonbc | 0:53746acfed69 | 12 | DigitalOut led_red(LED3); |
jeonbc | 0:53746acfed69 | 13 | DigitalOut led_blue(LED4); |
jeonbc | 0:53746acfed69 | 14 | |
bcjun@aname.co.kr | 3:b61f4db32ed5 | 15 | Serial pc(SERIAL_TX, SERIAL_RX); |
bcjun@aname.co.kr | 3:b61f4db32ed5 | 16 | |
bcjun@aname.co.kr | 3:b61f4db32ed5 | 17 | void msd_task(void const *) { |
bcjun@aname.co.kr | 3:b61f4db32ed5 | 18 | |
bcjun@aname.co.kr | 3:b61f4db32ed5 | 19 | USBHostMSD msd; |
bcjun@aname.co.kr | 3:b61f4db32ed5 | 20 | int i = 0; |
bcjun@aname.co.kr | 3:b61f4db32ed5 | 21 | FATFileSystem fs("usb"); |
bcjun@aname.co.kr | 3:b61f4db32ed5 | 22 | int err; |
bcjun@aname.co.kr | 3:b61f4db32ed5 | 23 | printf("wait for usb memory stick insertion\n"); |
bcjun@aname.co.kr | 3:b61f4db32ed5 | 24 | while(1) { |
bcjun@aname.co.kr | 3:b61f4db32ed5 | 25 | |
bcjun@aname.co.kr | 3:b61f4db32ed5 | 26 | // try to connect a MSD device |
bcjun@aname.co.kr | 3:b61f4db32ed5 | 27 | while(!msd.connect()) { |
bcjun@aname.co.kr | 3:b61f4db32ed5 | 28 | Thread::wait(500); |
bcjun@aname.co.kr | 3:b61f4db32ed5 | 29 | } |
bcjun@aname.co.kr | 3:b61f4db32ed5 | 30 | if (fs.mount(&msd) != 0) continue; |
bcjun@aname.co.kr | 3:b61f4db32ed5 | 31 | else |
bcjun@aname.co.kr | 3:b61f4db32ed5 | 32 | printf("file system mounted\n"); |
bcjun@aname.co.kr | 3:b61f4db32ed5 | 33 | |
bcjun@aname.co.kr | 3:b61f4db32ed5 | 34 | if (!msd.connect()) { |
bcjun@aname.co.kr | 3:b61f4db32ed5 | 35 | continue; |
bcjun@aname.co.kr | 3:b61f4db32ed5 | 36 | } |
bcjun@aname.co.kr | 3:b61f4db32ed5 | 37 | |
bcjun@aname.co.kr | 3:b61f4db32ed5 | 38 | // in a loop, append a file |
bcjun@aname.co.kr | 3:b61f4db32ed5 | 39 | // if the device is disconnected, we try to connect it again |
bcjun@aname.co.kr | 3:b61f4db32ed5 | 40 | |
bcjun@aname.co.kr | 3:b61f4db32ed5 | 41 | // append a file |
bcjun@aname.co.kr | 3:b61f4db32ed5 | 42 | File file; |
bcjun@aname.co.kr | 3:b61f4db32ed5 | 43 | err = file.open(&fs, "test1.txt", O_WRONLY | O_CREAT |O_APPEND); |
bcjun@aname.co.kr | 3:b61f4db32ed5 | 44 | |
bcjun@aname.co.kr | 3:b61f4db32ed5 | 45 | if (err == 0) { |
bcjun@aname.co.kr | 3:b61f4db32ed5 | 46 | char tmp[100]; |
bcjun@aname.co.kr | 3:b61f4db32ed5 | 47 | sprintf(tmp,"Hello fun USB stick World: %d!\r\n", i++); |
bcjun@aname.co.kr | 3:b61f4db32ed5 | 48 | file.write(tmp,strlen(tmp)); |
bcjun@aname.co.kr | 3:b61f4db32ed5 | 49 | sprintf(tmp,"Goodbye World!\r\n"); |
bcjun@aname.co.kr | 3:b61f4db32ed5 | 50 | file.write(tmp,strlen(tmp)); |
bcjun@aname.co.kr | 3:b61f4db32ed5 | 51 | file.close(); |
bcjun@aname.co.kr | 3:b61f4db32ed5 | 52 | } else { |
bcjun@aname.co.kr | 3:b61f4db32ed5 | 53 | printf("FILE == NULL\r\n"); |
bcjun@aname.co.kr | 3:b61f4db32ed5 | 54 | } |
bcjun@aname.co.kr | 3:b61f4db32ed5 | 55 | Thread::wait(500); |
bcjun@aname.co.kr | 3:b61f4db32ed5 | 56 | printf("again\n"); |
bcjun@aname.co.kr | 3:b61f4db32ed5 | 57 | // if device disconnected, try to connect again |
bcjun@aname.co.kr | 3:b61f4db32ed5 | 58 | while (msd.connected()) { |
bcjun@aname.co.kr | 3:b61f4db32ed5 | 59 | Thread::wait(500); |
bcjun@aname.co.kr | 3:b61f4db32ed5 | 60 | } |
bcjun@aname.co.kr | 3:b61f4db32ed5 | 61 | while (fs.unmount() < 0) { |
bcjun@aname.co.kr | 3:b61f4db32ed5 | 62 | Thread::wait(500); |
bcjun@aname.co.kr | 3:b61f4db32ed5 | 63 | printf("unmount\n"); |
bcjun@aname.co.kr | 3:b61f4db32ed5 | 64 | } |
bcjun@aname.co.kr | 3:b61f4db32ed5 | 65 | } |
bcjun@aname.co.kr | 3:b61f4db32ed5 | 66 | } |
bcjun@aname.co.kr | 3:b61f4db32ed5 | 67 | |
bcjun@aname.co.kr | 2:1fb6f946b69b | 68 | int main() { |
bcjun@aname.co.kr | 2:1fb6f946b69b | 69 | |
bcjun@aname.co.kr | 3:b61f4db32ed5 | 70 | pc.baud(115200); |
bcjun@aname.co.kr | 3:b61f4db32ed5 | 71 | |
bcjun@aname.co.kr | 3:b61f4db32ed5 | 72 | //lcd.DisplayStringAt(0, LINE(1), (uint8_t *)"MBED EXAMPLE", CENTER_MODE); |
bcjun@aname.co.kr | 3:b61f4db32ed5 | 73 | //wait(1); |
bcjun@aname.co.kr | 3:b61f4db32ed5 | 74 | |
bcjun@aname.co.kr | 3:b61f4db32ed5 | 75 | Thread msdTask(msd_task, NULL, osPriorityNormal, 1024 * 4); |
bcjun@aname.co.kr | 3:b61f4db32ed5 | 76 | |
bcjun@aname.co.kr | 3:b61f4db32ed5 | 77 | //lcd.Clear(LCD_COLOR_BLUE); |
bcjun@aname.co.kr | 3:b61f4db32ed5 | 78 | //lcd.SetBackColor(LCD_COLOR_BLUE); |
bcjun@aname.co.kr | 3:b61f4db32ed5 | 79 | //lcd.SetTextColor(LCD_COLOR_WHITE); |
bcjun@aname.co.kr | 3:b61f4db32ed5 | 80 | |
bcjun@aname.co.kr | 3:b61f4db32ed5 | 81 | //BSP_LCD_SetFont(&Font20); |
bcjun@aname.co.kr | 3:b61f4db32ed5 | 82 | //lcd.DisplayStringAt(0, LINE(7), (uint8_t *)"HAVE FUN !!!", CENTER_MODE); |
bcjun@aname.co.kr | 2:1fb6f946b69b | 83 | |
jeonbc | 0:53746acfed69 | 84 | while(1) { |
jeonbc | 0:53746acfed69 | 85 | // WARNING: LEDs are OFF |
jeonbc | 0:53746acfed69 | 86 | led_green = 1; |
jeonbc | 0:53746acfed69 | 87 | led_orange = 1; |
jeonbc | 0:53746acfed69 | 88 | led_red = 1; |
jeonbc | 0:53746acfed69 | 89 | led_blue = 1; |
jeonbc | 0:53746acfed69 | 90 | wait(0.2); // 200 ms |
jeonbc | 0:53746acfed69 | 91 | // WARNING: LEDs are ON |
jeonbc | 0:53746acfed69 | 92 | led_green = 0; |
jeonbc | 0:53746acfed69 | 93 | led_orange = 0; |
jeonbc | 0:53746acfed69 | 94 | led_red = 0; |
jeonbc | 0:53746acfed69 | 95 | led_blue = 0; |
jeonbc | 0:53746acfed69 | 96 | wait(1.0); // 1 sec |
jeonbc | 0:53746acfed69 | 97 | } |
jeonbc | 0:53746acfed69 | 98 | } |