You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

221 lines
5.5 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. #include "zuart.hpp"
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include "critical_context.hpp"
  5. #include "malloc.h"
  6. #include "zhal_core.hpp"
  7. using namespace iflytop;
  8. static ZUART *s_uart_table[10];
  9. static int s_numUart;
  10. static void prv_reg_uart(ZUART *uart) {
  11. if (s_numUart < 10) {
  12. s_uart_table[s_numUart++] = uart;
  13. }
  14. }
  15. ZUART *prv_find_uart(UART_HandleTypeDef *huart) {
  16. for (int i = 0; i < s_numUart; i++) {
  17. if (s_uart_table[i]->getHuart() == huart) {
  18. return s_uart_table[i];
  19. }
  20. }
  21. return NULL;
  22. }
  23. extern "C" {
  24. void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart) {
  25. ZUART *stm32uart = prv_find_uart(huart);
  26. if (stm32uart != NULL) stm32uart->HAL_UART_TxCpltCallback();
  27. }
  28. void HAL_UART_TxHalfCpltCallback(UART_HandleTypeDef *huart) {
  29. ZUART *stm32uart = prv_find_uart(huart);
  30. if (stm32uart != NULL) stm32uart->HAL_UART_TxHalfCpltCallback();
  31. }
  32. void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) {
  33. ZUART *stm32uart = prv_find_uart(huart);
  34. if (stm32uart != NULL) stm32uart->HAL_UART_RxCpltCallback();
  35. }
  36. void HAL_UART_RxHalfCpltCallback(UART_HandleTypeDef *huart) {
  37. ZUART *stm32uart = prv_find_uart(huart);
  38. if (stm32uart != NULL) stm32uart->HAL_UART_RxHalfCpltCallback();
  39. }
  40. void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart) {
  41. ZUART *stm32uart = prv_find_uart(huart);
  42. if (stm32uart != NULL) stm32uart->HAL_UART_ErrorCallback();
  43. }
  44. void HAL_UART_AbortCpltCallback(UART_HandleTypeDef *huart) {
  45. ZUART *stm32uart = prv_find_uart(huart);
  46. if (stm32uart != NULL) stm32uart->HAL_UART_AbortCpltCallback();
  47. }
  48. void HAL_UART_AbortTransmitCpltCallback(UART_HandleTypeDef *huart) {
  49. ZUART *stm32uart = prv_find_uart(huart);
  50. if (stm32uart != NULL) stm32uart->HAL_UART_AbortTransmitCpltCallback();
  51. }
  52. void HAL_UART_AbortReceiveCpltCallback(UART_HandleTypeDef *huart) {
  53. ZUART *stm32uart = prv_find_uart(huart);
  54. if (stm32uart != NULL) stm32uart->HAL_UART_AbortReceiveCpltCallback();
  55. }
  56. }
  57. IRQn_Type ZUART::getUartIRQType() {
  58. #ifdef USART1
  59. if (m_huart->Instance == USART1) {
  60. return USART1_IRQn;
  61. }
  62. #endif
  63. #ifdef USART2
  64. if (m_huart->Instance == USART2) {
  65. return USART2_IRQn;
  66. }
  67. #endif
  68. #ifdef USART3
  69. if (m_huart->Instance == USART3) {
  70. return USART3_IRQn;
  71. }
  72. #endif
  73. #ifdef UART4
  74. if (m_huart->Instance == UART4) {
  75. return UART4_IRQn;
  76. }
  77. #endif
  78. #ifdef UART5
  79. if (m_huart->Instance == UART5) {
  80. return UART5_IRQn;
  81. }
  82. #endif
  83. #ifdef USART6
  84. if (m_huart->Instance == USART6) {
  85. return USART6_IRQn;
  86. }
  87. #endif
  88. ZASSERT(false);
  89. return USART1_IRQn;
  90. }
  91. void ZUART::initialize(cfg_t *cfg, callback_t cb) {
  92. m_name = cfg->name;
  93. m_huart = cfg->huart;
  94. m_cb = cb;
  95. m_rxovertime_ms = cfg->rxovertime_ms;
  96. m_rxBuffer = (uint8_t *)malloc(cfg->rxbuffersize);
  97. memset(m_rxBuffer, 0, cfg->rxbuffersize);
  98. ZASSERT(m_rxBuffer != NULL);
  99. m_rxBufferLen = cfg->rxbuffersize;
  100. m_isRxing = false;
  101. m_dataIsReady = false;
  102. m_rxBufferPos = 0;
  103. m_lastRxTime = 0;
  104. onebyte = 0;
  105. ZHALCORE::getInstance()->regPeriodJob([this](ZHALCORE::Context &context) { periodicJob(); }, 1);
  106. prv_reg_uart(this);
  107. }
  108. void ZUART::initialize(cfg_t *cfg) { initialize(cfg, NULL); }
  109. bool ZUART::tx(uint8_t *data, size_t len) {
  110. HAL_UART_Transmit(m_huart, data, len, 0xffff);
  111. return true;
  112. }
  113. bool ZUART::tx(const char *data) {
  114. HAL_UART_Transmit(m_huart, (uint8_t *)data, strlen(data), 0xffff);
  115. return true;
  116. }
  117. bool ZUART::startRxIt() {
  118. ZASSERT(m_rxBuffer != NULL);
  119. ZASSERT(NVIC_GetEnableIRQ(getUartIRQType()) != 0);
  120. if (m_isRxing) return true;
  121. m_isRxing = true;
  122. HAL_UART_Receive_IT(m_huart, &onebyte, 1);
  123. return true;
  124. }
  125. bool ZUART::dataIsReady() {
  126. CriticalContext criticalContext;
  127. if (m_dataIsReady) {
  128. return true;
  129. }
  130. if (!m_dataIsReady && m_rxBufferPos != 0) {
  131. if (ifly_has_passedms(m_lastRxTime) > (uint32_t)m_rxovertime_ms) {
  132. m_dataIsReady = true;
  133. return true;
  134. }
  135. }
  136. return false;
  137. }
  138. void ZUART::clearRxData() {
  139. ZCriticalContext criticalContext;
  140. m_dataIsReady = false;
  141. memset(m_rxBuffer, 0, m_rxBufferLen);
  142. m_rxBufferPos = 0;
  143. }
  144. void ZUART::periodicJob() {
  145. if (dataIsReady()) {
  146. if (m_cb) {
  147. m_cb(m_rxBuffer, m_rxBufferPos);
  148. }
  149. clearRxData();
  150. }
  151. }
  152. /*******************************************************************************
  153. * *
  154. *******************************************************************************/
  155. void ZUART::HAL_UART_TxCpltCallback() {}
  156. void ZUART::HAL_UART_TxHalfCpltCallback() {}
  157. void ZUART::HAL_UART_RxCpltCallback() {
  158. if (m_dataIsReady) {
  159. HAL_UART_Receive_IT(m_huart, &onebyte, 1);
  160. return;
  161. }
  162. m_rxBuffer[m_rxBufferPos] = onebyte;
  163. m_rxBufferPos++;
  164. m_lastRxTime = chip_get_ticket();
  165. if (m_rxBufferPos >= m_rxBufferLen) {
  166. m_dataIsReady = true;
  167. }
  168. HAL_UART_Receive_IT(m_huart, &onebyte, 1);
  169. }
  170. void ZUART::HAL_UART_RxHalfCpltCallback() {}
  171. void ZUART::HAL_UART_ErrorCallback() {
  172. HAL_UART_AbortReceive_IT(m_huart);
  173. HAL_UART_Receive_IT(m_huart, &onebyte, 1);
  174. }
  175. void ZUART::HAL_UART_AbortCpltCallback() {}
  176. void ZUART::HAL_UART_AbortTransmitCpltCallback() {}
  177. void ZUART::HAL_UART_AbortReceiveCpltCallback() {}
  178. namespace iflytop {
  179. #if 0
  180. #ifdef USART1
  181. ZUART STM32_UART1("uart1", USART1);
  182. #endif
  183. #ifdef USART2
  184. ZUART STM32_UART2("uart2", USART2);
  185. #endif
  186. #ifdef USART3
  187. ZUART STM32_UART3("uart3", USART3);
  188. #endif
  189. #ifdef UART4
  190. ZUART STM32_UART4("uart4", UART4);
  191. #endif
  192. #ifdef UART5
  193. ZUART STM32_UART5("uart5", UART5);
  194. #endif
  195. #ifdef USART6
  196. ZUART STM32_UART6("uart6", USART6);
  197. #endif
  198. #endif
  199. } // namespace iflytop