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

时间:2016-05-20 01:25:53

标签: interrupt stm32 uart stm32f0 stm32-hal

我正在使用stm32f0 MCU。

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

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
    }
}

尽管代码有效,我对代码感到不舒服。首先,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
    }
}

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

1 个答案:

答案 0 :(得分:9)

首先:

正如您在previous question空超时的答案中所描述的那样,只需排除等待标志状态。如果你打开HAL_UART_Transmit代码 - 你会看到当你发送1个字节而没有超时时没有任何阻塞状态!

<强>第二

从巨大的HAL函数及其回调中发送/接收一个字节不是真正的方法。我想:接下来你的问题将是&#34;我必须在那里实现解析?&#34;。我希望你不要在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 */

<强> 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);
        */
    }
}

<强> 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 */
}

第三名!!!

关于这个:

  

为什么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
    }
}

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