usb_test_demo.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #include "FreeRTOS.h"
  2. #include "board.h"
  3. #include "chip.h"
  4. #include "sfud.h"
  5. #include "romfile.h"
  6. #include "ota_update.h"
  7. #include "sysinfo.h"
  8. #include "ff_stdio.h"
  9. #include "test_demo.h"
  10. #include "delta_update.h"
  11. #ifdef USB_SUPPORT
  12. #if USB_TEST
  13. #define USB_DEV_PLUGED 0
  14. #define USB_DEV_UNPLUGED 1
  15. extern int usb_wait_stor_dev_pluged(uint32_t timeout);
  16. extern void hub_usb_dev_reset(void);
  17. static void usb_read_thread(void *para)
  18. {
  19. unsigned int status;
  20. for (;;) {
  21. status = usb_wait_stor_dev_pluged(portMAX_DELAY);
  22. if (status == USB_DEV_PLUGED) {
  23. printf("usb dev inserted.\n");
  24. #ifdef OTA_UPDATE_SUPPORT
  25. int filetype;
  26. char filename[32];
  27. FF_FILE *fp;
  28. #ifdef DELTA_UPDATE_SUPPORT
  29. //Demo从U盘读取patch文件来模拟接收patch文件
  30. size_t filesize;
  31. uint8_t *filebuf;
  32. size_t leftsize;
  33. size_t wrsize;
  34. uint32_t offset;
  35. #if DEVICE_TYPE_SELECT != EMMC_FLASH
  36. sfud_flash *sflash = sfud_get_device(0);
  37. #endif
  38. filebuf = pvPortMalloc(0x10000);
  39. if (!filebuf) {
  40. printf("%s filebuf malloc fail.\n", __func__);
  41. continue;
  42. }
  43. for (filetype = UPFILE_TYPE_LDR; filetype < UPFILE_TYPE_NUM; filetype++) {
  44. strcpy(filename, "/usb/");
  45. strcat(filename, g_upfilename[filetype]);
  46. strcpy(strrchr(filename, '.'), "_patch.bin");
  47. fp = ff_fopen(filename, "rb");
  48. if (!fp) {
  49. printf("not found patch file %s.\n", filename);
  50. continue;
  51. }
  52. offset = OTA_MEDIA_OFFSET;
  53. filesize = ff_filelength(fp);
  54. leftsize = filesize;
  55. #if DEVICE_TYPE_SELECT != EMMC_FLASH
  56. sfud_erase(sflash, OTA_MEDIA_OFFSET, filesize);
  57. #endif
  58. while (leftsize) {
  59. wrsize = leftsize > 0x10000 ? 0x10000 : leftsize;
  60. ff_fread(filebuf, 1, wrsize, fp);
  61. #if DEVICE_TYPE_SELECT == EMMC_FLASH
  62. emmc_write(offset, wrsize, filebuf);
  63. #else
  64. sfud_write(sflash, offset, wrsize, filebuf);
  65. #endif
  66. offset += wrsize;
  67. leftsize -= wrsize;
  68. }
  69. ff_fclose(fp);
  70. delta_update(filetype, filesize);
  71. }
  72. vPortFree(filebuf);
  73. #else
  74. for (filetype = UPFILE_TYPE_LDR; filetype < UPFILE_TYPE_NUM; filetype++) {
  75. sprintf(filename, "%s/%s", "/usb", g_upfilename[filetype]);
  76. fp = ff_fopen(filename, "rb");
  77. if (fp) {
  78. ff_fclose(fp);
  79. update_from_media("/usb", filetype);
  80. }
  81. }
  82. if (get_update_status()) {
  83. printf("%s, entry wdt reset.\n", __func__);
  84. wdt_cpu_reboot();
  85. }
  86. #endif
  87. #endif
  88. } else if (status == USB_DEV_UNPLUGED) {
  89. printf("usb removed.\n");
  90. }
  91. }
  92. }
  93. void usb_read_demo(void)
  94. {
  95. if (xTaskCreate(usb_read_thread, "usb_read_demo", configMINIMAL_STACK_SIZE * 16, NULL,
  96. 1, NULL) != pdPASS) {
  97. printf("create usbread task fail.\n");
  98. }
  99. }
  100. #endif
  101. #endif