terminal_usbbridge.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #include "terminal_usbbridge.h"
  2. #include "terminal_sbs.h"
  3. #include "usbd_cdc.h"
  4. #include "usbd_desc.h"
  5. #include "cmsis_os.h"
  6. #include "usbd_cdc_interface.h"
  7. #include "fpga_blaster.h"
  8. #include "_config.h"
  9. #include <string.h>
  10. #include <stdio.h>
  11. osThreadId usbBridgeHandle;
  12. osThreadId usbRxHandle;
  13. osThreadId usbTxHandle;
  14. //Usart bridge object for global terminal
  15. UsbBridgeTerminal terminalUsbBridge;
  16. //Pointer to global terminal object
  17. extern Terminal* pTerminal;
  18. //Print callback for terminal object
  19. void print_usbbridge(const char * str) {
  20. terminalUsbBridge.print(str);
  21. }
  22. UsbBridgeTerminal::UsbBridgeTerminal()
  23. {
  24. m_fpga_blast = false;
  25. }
  26. UsbBridgeTerminal::~UsbBridgeTerminal()
  27. {
  28. }
  29. //Configure hardware and link with terminal
  30. void UsbBridgeTerminal::configure()
  31. {
  32. m_recvQueue = xQueueCreate(10, sizeof(char));
  33. m_sendQueue = xQueueCreate(200, sizeof(char));
  34. CDC_SetRecvCb(&USB_RecvByte);
  35. // Добавить функцию печати в терминал
  36. pTerminal->addPrint(::print_usbbridge);
  37. osThreadDef(UsbTx, vUsbTx, osPriorityNormal, 0, 2*128);
  38. usbTxHandle = osThreadCreate(osThread(UsbTx), NULL);;
  39. osThreadDef(UsbRx, vUsbRx, osPriorityNormal, 0, 2*128);
  40. usbRxHandle = osThreadCreate(osThread(UsbRx), NULL);
  41. }
  42. //
  43. void UsbBridgeTerminal::print(const char *str)
  44. {
  45. uint16_t len = strlen(str);
  46. if (len <= 0)
  47. return;
  48. CDC_Itf_Transmit((uint8_t*)str, len);
  49. vTaskDelay(1);
  50. }
  51. // Recv byte from vcp
  52. void UsbBridgeTerminal::recvByte(char byte)
  53. {
  54. portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
  55. xQueueSendFromISR(m_recvQueue, &byte, &xHigherPriorityTaskWoken);
  56. portEND_SWITCHING_ISR(xHigherPriorityTaskWoken);
  57. }
  58. //
  59. void UsbBridgeTerminal::sendByte(uint8_t byte)
  60. {
  61. portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
  62. xQueueSendFromISR(m_sendQueue, &byte, &xHigherPriorityTaskWoken);
  63. }
  64. //
  65. void UsbBridgeTerminal::set_fpga_blast(bool state)
  66. {
  67. m_fpga_blast = state;
  68. }
  69. //
  70. void USB_RecvByte(uint8_t* byte, uint32_t size)
  71. {
  72. if (terminalUsbBridge.m_fpga_blast) {
  73. fpga_blast.receive(byte, size);
  74. }
  75. else {
  76. for (uint32_t i = 0; i < size; i++) {
  77. terminalUsbBridge.recvByte(*byte++);
  78. }
  79. }
  80. }
  81. //
  82. void UsbBridgeTerminal::txProc()
  83. {
  84. uint8_t byte;
  85. if (xQueueReceive(m_sendQueue, &byte, portMAX_DELAY) == pdTRUE)
  86. {
  87. CDC_Itf_Transmit(&byte, 1);
  88. vTaskDelay(1);
  89. }
  90. }
  91. //
  92. void UsbBridgeTerminal::rxProc()
  93. {
  94. uint8_t byte;
  95. if (xQueueReceive(m_recvQueue, &byte, portMAX_DELAY) == pdTRUE)
  96. {
  97. pTerminal->insert(byte);
  98. }
  99. }
  100. //
  101. void vUsbTx(void const *params)
  102. {
  103. for (;;)
  104. {
  105. terminalUsbBridge.txProc();
  106. }
  107. }
  108. //
  109. void vUsbRx(void const *params)
  110. {
  111. for (;;)
  112. {
  113. terminalUsbBridge.rxProc();
  114. }
  115. }