HAL_UART_Transmit_IT:只发送几个字节



我在使用FreeRTOS的STM32F407上遇到问题。到目前为止,发送到xbee模块的大多数消息都是发送的。只是有时(5到60分钟后)TX过程会中断。

在调试会话中,我可以看到,TXEIE在2个字节(约40个)之后不再设置。UART_HandleTypeDef的TxXferCount也是42(TxXferSize:44)。SR寄存器中的标志TXE已设置,但CR1寄存器中的TXEIE未设置。

ISR激活2次,将它们存储到数据/移位寄存器中。这个我可以在逻辑分析器上看到。但我搞不清写作任务/isr的来源。

Xbee模块获取消息并发送响应消息。感谢您的回复!

init:

GPIO_InitTypeDef GPIO_InitStruct;
__GPIOA_CLK_ENABLE();
__GPIOD_CLK_ENABLE();
__GPIOF_CLK_ENABLE();

/* RX-TX PIN */
GPIO_InitStruct.Pin = XBEE1_TX_PIN | XBEE1_RX_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
GPIO_InitStruct.Alternate = GPIO_AF7_USART2;
HAL_GPIO_Init(XBEE1_TX_PORT, &GPIO_InitStruct);
/* Power*/
GPIO_InitStruct.Pin =  XBEE1_PWR_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
HAL_GPIO_Init(XBEE1_PWR_PORT, &GPIO_InitStruct);
HAL_GPIO_WritePin(XBEE1_PWR_PORT, XBEE1_PWR_PIN, GPIO_PIN_SET);
/* Reset PIN */
GPIO_InitStruct.Pin = XBEE1_RESET_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
HAL_GPIO_Init(XBEE1_RESET_PORT, &GPIO_InitStruct);
/* SLEEP_RQ PIN */
GPIO_InitStruct.Pin = XBEE1_SLEEP_RQ_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
HAL_GPIO_Init(XBEE1_SLEEP_RQ_PORT, &GPIO_InitStruct);

__USART2_CLK_ENABLE();
uart.Instance = USART2;
uart.Init.BaudRate = baud;
uart.Init.WordLength = UART_WORDLENGTH_8B;
uart.Init.StopBits = UART_STOPBITS_1;
uart.Init.Parity = UART_PARITY_NONE;
uart.Init.HwFlowCtl = UART_HWCONTROL_NONE;
uart.Init.Mode = UART_MODE_TX_RX;
if (HAL_UART_Init(&uart) != HAL_OK) {
    Error_Handler(XBEE1_ERROR);
}
//clear pending IRQs
__HAL_UART_CLEAR_FLAG(&uart, UART_FLAG_RXNE);
__HAL_UART_CLEAR_FLAG(&uart, USART_FLAG_TC);
__HAL_UART_CLEAR_FLAG(&uart, USART_FLAG_ORE);
__HAL_UART_CLEAR_FLAG(&uart, USART_FLAG_PE);
__HAL_UART_CLEAR_FLAG(&uart, USART_FLAG_FE);
__HAL_UART_CLEAR_FLAG(&uart, USART_FLAG_NE);
__HAL_UART_CLEAR_FLAG(&uart, USART_FLAG_TXE);
/* NVIC */
HAL_NVIC_SetPriority(USART2_IRQn, 6, 0);
HAL_NVIC_EnableIRQ(USART2_IRQn);

传输

HAL_StatusTypeDef UART_Xbee1::send( uint8_t* pdata, size_t sz ) {
    return HAL_UART_Transmit_IT(&uart, pdata, sz);
}

ISR

extern "C" void USART2_IRQHandler(void){
    testpin1.on();
    UART_HandleTypeDef* uart_c = &(Argos::xbee1->uart);
    uint32_t tmp1 = 0;
    uint32_t tmp2 = 0;
    tmp1 = __HAL_UART_GET_FLAG(uart_c, UART_FLAG_RXNE);
    tmp2 = __HAL_UART_GET_IT_SOURCE(uart_c, UART_IT_RXNE);
    if((tmp1 != RESET) && (tmp2 != RESET)){
        uint8_t in = (uint8_t)(uart_c->Instance->DR & (uint8_t)0x00FF);
        *uart_c->pRxBuffPtr++ = in;
        //overwrite expected bytes, when packet size is known
        if(xbee1->inApiRecMode){
            if( (uart_c->RxXferSize-uart_c->RxXferCount) == 1 ){
                xbee1->apiSize = 0;
                xbee1->apiSize = in << 8;
            }else if( (uart_c->RxXferSize-uart_c->RxXferCount) == 2 ){
                xbee1->apiSize |= in;
                uart_c->RxXferCount = xbee1->apiSize + 2;
            }
        }
        if( ((uart_c->RxXferCount)-1) == 0){
            (uart_c->RxXferCount)--;
              __HAL_UART_DISABLE_IT(uart_c, UART_IT_RXNE);
              /* Check if a transmit process is ongoing or not */
              if(uart_c->State == HAL_UART_STATE_BUSY_TX_RX){
                  uart_c->State   = HAL_UART_STATE_BUSY_TX;
              }else{
                /* Disable the UART Parity Error Interrupt */
                __HAL_UART_DISABLE_IT(uart_c, UART_IT_PE);
                /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error) */
                __HAL_UART_DISABLE_IT(uart_c, UART_IT_ERR);
                uart_c->State = HAL_UART_STATE_READY;
              }
              Message* msg = &(xbee1->msg_callBack);
              msg->code = (uint8_t) XbeeContext::MessageType::RECEIVE_EVENT;
              msg->value = 0;
              xbee1->rcvCallback->sendMsg(msg);
            }else if( (uart_c->RxXferCount) == 0){
                Error_Handler(XBEE1_ERROR);
            }else{
                (uart_c->RxXferCount)--;
            }
    }else{
        //other cases
        HAL_UART_IRQHandler( uart_c );
    }
    testpin1.off();
}

这个isr处理程序代码是从哪里来的?它看起来与stm32f4 HAL 1.8中的内容没有任何相似之处?

如果你使用hal,最好只是链接你的irq处理程序hal,并定义对托管错误和完成的回调。

HAL_UART_TxCplt回调HAL_UART_RxCplt回调HAL_UART_ErrorCallback

相关内容

  • 没有找到相关文章

最新更新