test_usb_device.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright (c) 2022 OpenLuat & AirM2M
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. * this software and associated documentation files (the "Software"), to deal in
  6. * the Software without restriction, including without limitation the rights to
  7. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  8. * the Software, and to permit persons to whom the Software is furnished to do so,
  9. * subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in all
  12. * copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  16. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  17. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  18. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  19. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. */
  21. #include "user.h"
  22. static uint8_t TestBuf[4096];
  23. static int32_t prvTest_USBCB(void *pData, void *pParam)
  24. {
  25. uint32_t UartID = (uint32_t)pData;
  26. uint32_t State = (uint32_t)pParam;
  27. uint16_t ReadLen;
  28. switch (State)
  29. {
  30. case UART_CB_TX_BUFFER_DONE:
  31. case UART_CB_TX_ALL_DONE:
  32. // DBG("usb send ok");
  33. // Core_PrintMemInfo();
  34. break;
  35. case UART_CB_RX_BUFFER_FULL:
  36. case UART_CB_RX_TIMEOUT:
  37. case UART_CB_RX_NEW:
  38. ReadLen = Core_VUartRxBufferRead(UartID, TestBuf, sizeof(TestBuf));
  39. Core_VUartBufferTx(UartID, TestBuf, ReadLen);
  40. break;
  41. case UART_CB_ERROR:
  42. DBG("usb disconnect");
  43. Core_VUartBufferTxStop(UartID); //也可以选择不清除,USB连上后,会自动重发,但是会有重复发送的情况,需要用户去处理
  44. break;
  45. case UART_CB_CONNECTED:
  46. Core_VUartBufferTx(UartID, "hello\r\n", 7);
  47. break;
  48. }
  49. }
  50. void Test_USBStart(void)
  51. {
  52. Core_VUartInit(VIRTUAL_UART0, 0, 1, 0, 0, 0, prvTest_USBCB);
  53. // Core_VUartSetRxTimeout(VIRTUAL_UART0, 200);
  54. }
  55. void prvUSB_Test(void *p)
  56. {
  57. SDHC_SPICtrlStruct SDHC;
  58. memset(&SDHC, 0, sizeof(SDHC));
  59. SDHC.SpiID = SPI_ID0;
  60. SDHC.CSPin = GPIOC_13;
  61. SDHC.IsSpiDMAMode = 0;
  62. // SDHC.NotifyTask = Task_GetCurrent();
  63. // SDHC.TaskCB = prvEventCB;
  64. SDHC.SDHCReadBlockTo = 5 * CORE_TICK_1MS;
  65. SDHC.SDHCWriteBlockTo = 25 * CORE_TICK_1MS;
  66. SDHC.IsPrintData = 0;
  67. GPIO_Iomux(GPIOC_12,2);
  68. GPIO_Iomux(GPIOC_14,2);
  69. GPIO_Iomux(GPIOC_15,2);
  70. GPIO_Config(SDHC.CSPin, 0, 1);
  71. SPI_MasterInit(SDHC.SpiID, 8, SPI_MODE_0, 400000, NULL, NULL);
  72. SDHC_SpiInitCard(&SDHC);
  73. if (SDHC.IsInitDone)
  74. {
  75. SPI_SetNewConfig(SDHC.SpiID, 24000000, SPI_MODE_0);
  76. SDHC_SpiReadCardConfig(&SDHC);
  77. DBG("卡容量 %ublock", SDHC.Info.LogBlockNbr);
  78. Test_USBStart();
  79. Core_UDiskAttachSDHC(0, &SDHC);
  80. Core_USBDefaultDeviceStart(0);
  81. }
  82. else
  83. {
  84. Test_USBStart();
  85. Core_USBDefaultDeviceStart(0);
  86. }
  87. Task_Exit();
  88. }
  89. void USB_TestInit(void)
  90. {
  91. Task_Create(prvUSB_Test, NULL, 2 * 1024, SERVICE_TASK_PRO, "usb task");
  92. }
  93. //INIT_TASK_EXPORT(USB_TestInit, "3");