test_usb_device.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. int32_t prvTest_HIDCB(void *pData, void *pParam)
  51. {
  52. Buffer_Struct *Buf;
  53. switch((uint32_t)pParam)
  54. {
  55. case USB_HID_NOT_READY:
  56. DBG("hid disconnected");
  57. break;
  58. case USB_HID_READY:
  59. DBG("hid connected");
  60. break;
  61. case USB_HID_SEND_DONE:
  62. DBG("hid send ok");
  63. break;
  64. default:
  65. Buf = (Buffer_Struct *)pParam;
  66. DBG("hid get data:");
  67. DBG_HexPrintf(Buf->Data, Buf->Pos);
  68. Core_VHIDSendRawData(0, "just Test Once!\n", 16);
  69. break;
  70. }
  71. return 0;
  72. }
  73. void Test_USBStart(void)
  74. {
  75. Core_VUartInit(VIRTUAL_UART0, 0, 1, 0, 0, 0, prvTest_USBCB);
  76. // Core_VHIDInit(0, prvTest_HIDCB);
  77. // Core_VUartSetRxTimeout(VIRTUAL_UART0, 200);
  78. }
  79. void prvUSB_Test(void *p)
  80. {
  81. SDHC_SPICtrlStruct SDHC;
  82. memset(&SDHC, 0, sizeof(SDHC));
  83. SDHC.SpiID = SPI_ID0;
  84. SDHC.CSPin = GPIOB_13;
  85. SDHC.IsSpiDMAMode = 0;
  86. // SDHC.NotifyTask = Task_GetCurrent();
  87. // SDHC.TaskCB = prvEventCB;
  88. SDHC.SDHCReadBlockTo = 50 * CORE_TICK_1MS;
  89. SDHC.SDHCWriteBlockTo = 250 * CORE_TICK_1MS;
  90. SDHC.IsPrintData = 0;
  91. GPIO_Iomux(GPIOB_12,0);
  92. GPIO_Iomux(GPIOB_14,0);
  93. GPIO_Iomux(GPIOB_15,0);
  94. GPIO_Config(SDHC.CSPin, 0, 1);
  95. SPI_MasterInit(SDHC.SpiID, 8, SPI_MODE_0, 400000, NULL, NULL);
  96. SDHC_SpiInitCard(&SDHC);
  97. if (SDHC.IsInitDone)
  98. {
  99. SPI_SetNewConfig(SDHC.SpiID, 24000000, SPI_MODE_0);
  100. SDHC_SpiReadCardConfig(&SDHC);
  101. DBG("卡容量 %ublock", SDHC.Info.LogBlockNbr);
  102. Test_USBStart();
  103. Core_UDiskAttachSDHC(0, &SDHC);
  104. Core_USBDefaultDeviceStart(0);
  105. }
  106. else
  107. {
  108. Test_USBStart();
  109. Core_USBDefaultDeviceStart(0);
  110. }
  111. Task_Exit(NULL);
  112. }
  113. void USB_TestInit(void)
  114. {
  115. Task_Create(prvUSB_Test, NULL, 2 * 1024, SERVICE_TASK_PRO, "usb task");
  116. }
  117. //INIT_TASK_EXPORT(USB_TestInit, "3");