HAL_UART_Transmit_IT: only a few bytes are sent

你。 提交于 2019-12-13 09:35:52

问题


i've got a problem on my STM32F407 with FreeRTOS. So Far most of the messages to a xbee module are sent. Only Sometimes (after 5 to 60 minutes) the TX process will be interrupted.

In the debug session i can see, that TXEIE ist not set anymore after the 2 bytes (of ~40). Also the TxXferCount of the UART_HandleTypeDef is 42 (TxXferSize: 44). The flag TXE in SR-Register ist set but the TXEIE in CR1-Register not.

The ISR was active for 2 times to store them into data/shift registers. This i can see on logic analyzer. But i can't figure out the source of the writing task/isr.

The Xbee module get messages and sends response messages. Thanks for any Response!

The 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);

The Transmit

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();
}

回答1:


where is this isr handler code comming from ? it does not look any similar to what's in stm32f4 HAL 1.8 ?

if you use hal it should be better to just link your irq handler to hal and define callback to managed error and completion.

HAL_UART_TxCpltCallback HAL_UART_RxCpltCallback HAL_UART_ErrorCallback



来源:https://stackoverflow.com/questions/33429920/hal-uart-transmit-it-only-a-few-bytes-are-sent

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!