|
@ -14,6 +14,7 @@ typedef enum { |
|
|
RECEIVE_TO_IDLE_DMA, |
|
|
RECEIVE_TO_IDLE_DMA, |
|
|
AUTO_READ_DMA, |
|
|
AUTO_READ_DMA, |
|
|
AUTO_READ_IT, |
|
|
AUTO_READ_IT, |
|
|
|
|
|
AUTO_READ_IT_ONE_BYTE, |
|
|
} HAL_EX_UART_RX_STATE_T; |
|
|
} HAL_EX_UART_RX_STATE_T; |
|
|
|
|
|
|
|
|
typedef struct { |
|
|
typedef struct { |
|
@ -101,10 +102,27 @@ static inline void processWhenAutoReadIT(EXHAL_UART_t* huart, ZUartIrqDispatcher |
|
|
HAL_UARTEx_ReceiveToIdle_IT(huart->huart, huart->autoRxBuffer, sizeof(huart->autoRxBuffer)); |
|
|
HAL_UARTEx_ReceiveToIdle_IT(huart->huart, huart->autoRxBuffer, sizeof(huart->autoRxBuffer)); |
|
|
} |
|
|
} |
|
|
} else if (event == ZUartIrqDispatcher::UART_IRQ_Error) { |
|
|
} else if (event == ZUartIrqDispatcher::UART_IRQ_Error) { |
|
|
|
|
|
__HAL_UART_CLEAR_PEFLAG(huart->huart); |
|
|
HAL_UARTEx_ReceiveToIdle_IT(huart->huart, huart->autoRxBuffer, sizeof(huart->autoRxBuffer)); |
|
|
HAL_UARTEx_ReceiveToIdle_IT(huart->huart, huart->autoRxBuffer, sizeof(huart->autoRxBuffer)); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static inline void processWhenAutoReadOneByteIT(EXHAL_UART_t* huart, ZUartIrqDispatcher::IRQType event, int size) { |
|
|
|
|
|
// printf("rx: %d size:%d\n", event, size);
|
|
|
|
|
|
// if (huart->huart == &huart3) printf("%d",event);
|
|
|
|
|
|
if (event == ZUartIrqDispatcher::UART_IRQ_RxCplt) { |
|
|
|
|
|
HAL_UART_Receive_IT(huart->huart, huart->autoRxBuffer, 1); |
|
|
|
|
|
huart->autoRxSize = 1; |
|
|
|
|
|
uint8_t rx = huart->autoRxBuffer[0]; |
|
|
|
|
|
if (huart->listener) { |
|
|
|
|
|
huart->listener(huart->huart, &rx, huart->autoRxSize); |
|
|
|
|
|
} |
|
|
|
|
|
} else if (event == ZUartIrqDispatcher::UART_IRQ_Error) { |
|
|
|
|
|
__HAL_UART_CLEAR_PEFLAG(huart->huart); |
|
|
|
|
|
HAL_UART_Receive_IT(huart->huart, huart->autoRxBuffer, 1); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
void EXHAL_UART_OnUartIrq(UART_HandleTypeDef* huart, ZUartIrqDispatcher::IRQType event, int size) { |
|
|
void EXHAL_UART_OnUartIrq(UART_HandleTypeDef* huart, ZUartIrqDispatcher::IRQType event, int size) { |
|
|
EXHAL_UART_t* exhal_uart = EXHAL_UART_Get(huart); |
|
|
EXHAL_UART_t* exhal_uart = EXHAL_UART_Get(huart); |
|
|
if (exhal_uart == NULL) return; |
|
|
if (exhal_uart == NULL) return; |
|
@ -124,6 +142,9 @@ void EXHAL_UART_OnUartIrq(UART_HandleTypeDef* huart, ZUartIrqDispatcher::IRQType |
|
|
case AUTO_READ_IT: |
|
|
case AUTO_READ_IT: |
|
|
processWhenAutoReadIT(exhal_uart, event, size); |
|
|
processWhenAutoReadIT(exhal_uart, event, size); |
|
|
break; |
|
|
break; |
|
|
|
|
|
case AUTO_READ_IT_ONE_BYTE: |
|
|
|
|
|
processWhenAutoReadOneByteIT(exhal_uart, event, size); |
|
|
|
|
|
break; |
|
|
default: |
|
|
default: |
|
|
break; |
|
|
break; |
|
|
} |
|
|
} |
|
@ -183,14 +204,14 @@ void EXHAL_UART_RS485_EnTx(UART_HandleTypeDef* huart, bool en) { |
|
|
HAL_StatusTypeDef EXHAL_UART_RegListener(UART_HandleTypeDef* huart, EXHAL_UART_Listener_t listener) { |
|
|
HAL_StatusTypeDef EXHAL_UART_RegListener(UART_HandleTypeDef* huart, EXHAL_UART_Listener_t listener) { |
|
|
EXHAL_UART_t* exhal_uart = EXHAL_UART_Get(huart); |
|
|
EXHAL_UART_t* exhal_uart = EXHAL_UART_Get(huart); |
|
|
ZEARLY_ASSERT(exhal_uart != NULL); |
|
|
ZEARLY_ASSERT(exhal_uart != NULL); |
|
|
exhal_uart->listener = listener; |
|
|
|
|
|
|
|
|
exhal_uart->listener = listener; |
|
|
return HAL_OK; |
|
|
return HAL_OK; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
HAL_StatusTypeDef EXHAL_UART_TransmitBlock_DMA(UART_HandleTypeDef* huart, uint8_t* data, size_t len) { |
|
|
HAL_StatusTypeDef EXHAL_UART_TransmitBlock_DMA(UART_HandleTypeDef* huart, uint8_t* data, size_t len) { |
|
|
EXHAL_UART_t* exhal_uart = EXHAL_UART_Get(huart); |
|
|
EXHAL_UART_t* exhal_uart = EXHAL_UART_Get(huart); |
|
|
ZEARLY_ASSERT(exhal_uart != NULL); |
|
|
ZEARLY_ASSERT(exhal_uart != NULL); |
|
|
exhal_uart->rxstate = TRANSMIT_BLOCK_DMA; |
|
|
|
|
|
|
|
|
exhal_uart->rxstate = TRANSMIT_BLOCK_DMA; |
|
|
|
|
|
|
|
|
HAL_StatusTypeDef ret = HAL_UART_Transmit_DMA(huart, data, len); |
|
|
HAL_StatusTypeDef ret = HAL_UART_Transmit_DMA(huart, data, len); |
|
|
if (ret != HAL_OK) { |
|
|
if (ret != HAL_OK) { |
|
@ -204,8 +225,8 @@ HAL_StatusTypeDef EXHAL_UART_TransmitBlock_DMA(UART_HandleTypeDef* huart, uint8_ |
|
|
|
|
|
|
|
|
return HAL_OK; |
|
|
return HAL_OK; |
|
|
} |
|
|
} |
|
|
HAL_StatusTypeDef EXHAL_UART_TransmitStringBlock(UART_HandleTypeDef* huart, const char* data){//
|
|
|
|
|
|
return HAL_UART_Transmit(huart, (uint8_t*)data, strlen(data),100); |
|
|
|
|
|
|
|
|
HAL_StatusTypeDef EXHAL_UART_TransmitStringBlock(UART_HandleTypeDef* huart, const char* data) { //
|
|
|
|
|
|
return HAL_UART_Transmit(huart, (uint8_t*)data, strlen(data), 100); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
static void __attribute__((noinline)) clearUartRxCache(UART_HandleTypeDef* huart) { //
|
|
|
static void __attribute__((noinline)) clearUartRxCache(UART_HandleTypeDef* huart) { //
|
|
@ -263,7 +284,7 @@ HAL_StatusTypeDef EXHAL_UART_DMAStartAutoRead(UART_HandleTypeDef* huart) { |
|
|
EXHAL_UART_t* exhal_uart = EXHAL_UART_Get(huart); |
|
|
EXHAL_UART_t* exhal_uart = EXHAL_UART_Get(huart); |
|
|
ZEARLY_ASSERT(exhal_uart != NULL); |
|
|
ZEARLY_ASSERT(exhal_uart != NULL); |
|
|
|
|
|
|
|
|
exhal_uart->rxstate = AUTO_READ_DMA; |
|
|
|
|
|
|
|
|
exhal_uart->rxstate = AUTO_READ_DMA; |
|
|
HAL_UARTEx_ReceiveToIdle_DMA_force(huart, exhal_uart->autoRxBuffer, sizeof(exhal_uart->autoRxBuffer)); |
|
|
HAL_UARTEx_ReceiveToIdle_DMA_force(huart, exhal_uart->autoRxBuffer, sizeof(exhal_uart->autoRxBuffer)); |
|
|
__HAL_DMA_DISABLE_IT(huart->hdmarx, DMA_IT_HT); |
|
|
__HAL_DMA_DISABLE_IT(huart->hdmarx, DMA_IT_HT); |
|
|
|
|
|
|
|
@ -301,7 +322,7 @@ HAL_StatusTypeDef EXHAL_UART_ITStartAutoRead(UART_HandleTypeDef* huart) { |
|
|
EXHAL_UART_t* exhal_uart = EXHAL_UART_Get(huart); |
|
|
EXHAL_UART_t* exhal_uart = EXHAL_UART_Get(huart); |
|
|
ZEARLY_ASSERT(exhal_uart != NULL); |
|
|
ZEARLY_ASSERT(exhal_uart != NULL); |
|
|
|
|
|
|
|
|
exhal_uart->rxstate = AUTO_READ_IT; |
|
|
|
|
|
|
|
|
exhal_uart->rxstate = AUTO_READ_IT; |
|
|
|
|
|
|
|
|
while (true) { |
|
|
while (true) { |
|
|
HAL_StatusTypeDef ret = HAL_UARTEx_ReceiveToIdle_IT(huart, exhal_uart->autoRxBuffer, sizeof(exhal_uart->autoRxBuffer)); |
|
|
HAL_StatusTypeDef ret = HAL_UARTEx_ReceiveToIdle_IT(huart, exhal_uart->autoRxBuffer, sizeof(exhal_uart->autoRxBuffer)); |
|
@ -310,6 +331,22 @@ HAL_StatusTypeDef EXHAL_UART_ITStartAutoRead(UART_HandleTypeDef* huart) { |
|
|
} |
|
|
} |
|
|
return HAL_OK; |
|
|
return HAL_OK; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
HAL_StatusTypeDef EXHAL_UART_ITStartAutoReadOneByte(UART_HandleTypeDef* huart) { |
|
|
|
|
|
clearUartRxCache(huart); |
|
|
|
|
|
EXHAL_UART_t* exhal_uart = EXHAL_UART_Get(huart); |
|
|
|
|
|
ZEARLY_ASSERT(exhal_uart != NULL); |
|
|
|
|
|
|
|
|
|
|
|
exhal_uart->rxstate = AUTO_READ_IT_ONE_BYTE; |
|
|
|
|
|
|
|
|
|
|
|
while (true) { |
|
|
|
|
|
HAL_StatusTypeDef ret = HAL_UART_Receive_IT(huart, exhal_uart->autoRxBuffer, 1); |
|
|
|
|
|
if (ret == HAL_OK) break; |
|
|
|
|
|
HAL_UART_AbortReceive(huart); |
|
|
|
|
|
} |
|
|
|
|
|
return HAL_OK; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
HAL_StatusTypeDef EXHAL_UART_ITStopAutoRead(UART_HandleTypeDef* huart) { |
|
|
HAL_StatusTypeDef EXHAL_UART_ITStopAutoRead(UART_HandleTypeDef* huart) { |
|
|
if (huart == NULL) { |
|
|
if (huart == NULL) { |
|
|
return HAL_ERROR; |
|
|
return HAL_ERROR; |
|
|