为什么UART发送中断在这种情况下无效?

use*_*486 4 interrupt stm32 uart stm32f0 stm32-hal

我正在使用stm32f0 MCU.

我有一个简单的UART回声代码,其中接收的每个字节都将被传输出去.我测试它是否有效.这里是;

uint8_t Rx_data[5]; 
uint32_t tx_timeout = 0;
//Interrupt callback routine
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
    if (huart->Instance == USART1)  //current UART
    {
        HAL_UART_Transmit(&huart1, &Rx_data[0], 1, tx_timeout);        
        HAL_UART_Receive_IT(&huart1, Rx_data, 1);   //activate UART receive interrupt every time on receiving 1 byte
    }
}
Run Code Online (Sandbox Code Playgroud)

即使它有效,我对代码感到不舒服.首先,tx_timeout是0并且大多数代码示例都是非零的.我不知道副作用.其次,HAL_UART_Transmit()是一个阻塞调用,不建议在中断中使用阻塞调用.所以,我决定使用中断进行uart传输HAL_UART_Transmit_IT()而不是阻塞调用.这是修改后的代码;

uint8_t Rx_data[5]; 
uint32_t tx_timeout = 0;
//Interrupt callback routine
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
    if (huart->Instance == USART1)  //current UART
    {
        HAL_UART_Transmit_IT(&huart1, &Rx_data[0], 1);        
        HAL_UART_Receive_IT(&huart1, Rx_data, 1);   //activate UART receive interrupt every time on receiving 1 byte
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,它没有按预期工作.我的电脑将ASCII 12345678发送到stm32.如果事情按预期工作,PC应该收到12345678.但是,PC收到了1357.这段代码有什么问题HAL_UART_Transmit_IT()

imb*_*arr 9

第一:

正如您在上一个问题的答案中描述的那样, null timeout只是排除等待标志状态.如果你打开HAL_UART_Transmit代码 - 你会看到当你发送1个字节没有超时时没有任何阻塞状态!

第二:

从巨大的HAL函数及其回调中发送/接收一个字节不是真正的方法.我猜:接下来你的问题将是"我必须如何在那里实施解析?".我希望你不会在IRQ回调中插入你的解析函数!

所以通常你需要缓冲区.并且最好使用循环缓冲区.

mxconstants.h:

/* USER CODE BEGIN Private defines */

/* Buffer's length must be select according to real messages frequency */
#define RXBUF_LEN            128 // must be power of 2
#define TXBUF_LEN            128 // must be power of 2
#define RXBUF_MSK            (RXBUF_LEN-1)
#define TXBUF_MSK            (TXBUF_LEN-1)

/* USER CODE END Private defines */
Run Code Online (Sandbox Code Playgroud)

main.c中:

uint8_t rx_buf[RXBUF_LEN], tx_buf[TXBUF_LEN];
/* xx_i - counter of input bytes (tx - pushed for transmit, rx - received)
   xx_o - counter of output bytes (tx - transmitted, rx - parsed)
   xx_e - counter of echoed bytes */
volatile uint16_t rx_i = 0, tx_o = 0;
uint16_t rx_o = 0, rx_e = 0, tx_i = 0;
volatile uint8_t tx_busy = 0;

void transmit(uint8_t byte) 
{
    tx_buf[TXBUF_MSK & tx_i] = byte;
    tx_i++;
    tx_busy = 1;
    __HAL_UART_ENABLE_IT(&huart1, UART_IT_TXE);
}

void main(void)
{
    /* Initialization code */
    /* ... */
    /* Enable usart 1 receive IRQ */
    __HAL_UART_ENABLE_IT(&huart1, UART_IT_RXNE);
    for (;;) {
        /* Main cycle */
        while (rx_i != rx_e) {
            /* echo here */
            transmit(rx_buf[RXBUF_MSK & rx_e]);
            rx_e++;
        }
        while (rx_i != rx_o) {
            /* parse here */
            /* ... */
            rx_o++;
        }
        /* Power save 
        while (tx_busy);
        HAL_UART_DeInit(&huart1);
        */
    }
}
Run Code Online (Sandbox Code Playgroud)

stm32f0xx_it.c:

extern uint8_t rx_buf[RXBUF_LEN], tx_buf[TXBUF_LEN];
extern volatile uint16_t rx_i, tx_o;
extern uint16_t rx_o, rx_e, tx_i;
extern volatile uint8_t tx_busy;

void USART1_IRQHandler(void)
{
    /* USER CODE BEGIN USART1_IRQn 0 */
    if((__HAL_UART_GET_IT(&huart1, UART_IT_RXNE) != RESET) && 
       (__HAL_UART_GET_IT_SOURCE(&huart1, UART_IT_RXNE) != RESET))
    {
        rx_buf[rx_i & RXBUF_MSK] = (uint8_t)(huart1.Instance->RDR & 0x00FF);
        rx_i++;
        /* Clear RXNE interrupt flag */
        __HAL_UART_SEND_REQ(&huart1, UART_RXDATA_FLUSH_REQUEST);
    }
    if((__HAL_UART_GET_IT(&huart1, UART_IT_TXE) != RESET) &&
       (__HAL_UART_GET_IT_SOURCE(&huart1, UART_IT_TXE) != RESET))
    {
        if (tx_i == tx_o) {
            __HAL_UART_DISABLE_IT(&huart1, UART_IT_TXE);
            __HAL_UART_ENABLE_IT(&huart1, UART_IT_TC);
        } else {
            huart1.Instance->TDR = (uint8_t)(tx_buf[TXBUF_MSK & tx_o] & (uint8_t)0xFF);
            tx_o++;
        }
    }
    if((__HAL_UART_GET_IT(&huart1, UART_IT_TC) != RESET) &&
       (__HAL_UART_GET_IT_SOURCE(&huart1, UART_IT_TC) != RESET))
    {
        tx_busy = 0;
        __HAL_UART_DISABLE_IT(&huart1, UART_IT_TC);
    }
    /* And never call default handler */
    return;
    /* USER CODE END USART1_IRQn 0 */

    HAL_UART_IRQHandler(&huart1);

    /* USER CODE BEGIN USART1_IRQn 1 */
    /* USER CODE END USART1_IRQn 1 */
}
Run Code Online (Sandbox Code Playgroud)

第三个!!!

关于这个:

为什么HAL_UART_Transmit_IT无法帮助/工作?

因为它太慢了!如果你试图计算HAL_BUSY结果:

uint8_t Rx_data[5]; 
uint32_t tx_timeout = 0;
//Interrupt callback routine
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
    static uint32_t hal_busy_counter = 0;
    if (huart->Instance == USART1)  //current UART
    {
        if (HAL_UART_Transmit_IT(&huart1, &Rx_data[0], 1) == HAL_BUSY) {
            hal_busy_counter++;
        }        
        HAL_UART_Receive_IT(&huart1, Rx_data, 1);   //activate UART receive interrupt every time on receiving 1 byte
    }
}
Run Code Online (Sandbox Code Playgroud)

当你在数据交换后在调试器中暂停MCU时 - 你会感到惊讶:它将等于错过的字符数.

  • 这段代码`__HAL_UART_ENABLE_IT(&huart1,UART_IT_RXNE);`启用USART RX IRQ,如果你不使用默认处理程序 - 所以你不需要重新发送它.**确保在STM32CubeMx项目中启用USART IRQ.** (2认同)