修复UART溢出



我有一个GSM模块通过硬件UART与我的微控制器交谈。当我接收到小数据时,一切都工作了。当我试图获得存储在SIM卡中的所有短信列表时,我得到UART缓冲区溢出错误。

我有一个动态字符串函数,它在使用malloc的缓冲区中附加每个接收到的字符。这就是我接受的方式。我把有问题的那行注释掉了。

while( uart_available() > 0 )
    {
        /*
         *  There are characters in the buffer so
         *  keep receiving until buffer in empty.
         */
        unsigned int c = uart_getc();
        if ( c & UART_NO_DATA )
        {
            /*
             * No data available from UART. This is not an error.
             */
            error( AT, "No error but - No uart data" );// TODO: Delete it. It is not an error actually.
        }
        else if ( c & UART_FRAME_ERROR )
        {
            /* Framing Error detected, i.e no stop bit detected */
            errorOccured( ER_UART_FRAME_ERROR );
            error( AT, "Frame error" );
        }
        else if ( c & UART_OVERRUN_ERROR )
        {
            /* 
             * Overrun, a character already present in the UART UDR register was 
             * not read by the interrupt handler before the next character arrived,
             * one or more received characters have been dropped.
             */
            errorOccured( ER_UART_OVERRUN_ERROR );
            error( AT, "Overrun error" );
        }
        else if ( c & UART_BUFFER_OVERFLOW )
        {
            /* 
             * We are not reading the receive buffer fast enough,
             * one or more received characters have been dropped.
             */
            errorOccured( ER_UART_BUFFER_OVERFLOW );
            error( AT, "Buffer overflow error" );
        }
        else
        {
            /*
             * There are data available add them to cString.
             */
            appendChar( data, (const char)c );// This line is slow and causes UART overflow.
            if ( *data == 0 )
            {
                /*
                 * Report bad pointer.
                 */
                errorOccured( ER_SIM900_BAD_POINTER );
                error( AT, "Bad pointer" );
            }
            /*
             * We received something so change the flag
             * even if an error occurs in the next loop.
             */
            isReceivedData = true;
        }
    }

我想问你们,如果你有关于malloc很慢的线索,或者如果你知道UART被UART接收中断中断是一个问题。如何在不降低UART比特率而不增加时钟速度的情况下解决这个问题?

更新:

调试后,我确信malloc正在工作(如果malloc失败,我得到一个断言),所以我的下一个猜测是appendChar很慢。虽然只是为了阅读短信,我将尝试马特奥建议使用固定字符数组一次一个短信(每条短信最多160个字符+发件人信息)

更新2 -新代码

我成功了,但我很幸运。代码中有一行我不明白为什么我需要它。我有8Mhz和19200 UART比特率。我猜是时间不对。

这是如果我删除函数不起作用的行:

_delay_us(150); // TODO: WEIRD KNOWN BUG!!!

这里是整个函数:

/**
 * @brief   Receives raw data from SIM900.
 *
 * @param   data    Get SIM900's raw data on exit.
 *
 * @return  True if at least one character received.
 */
bool SIM900_receive( cString* data )
{
    /*
     * Because we append chars we need to init it.
     */
    #define SIM900_MAX_RECEIVE_CHARACTERS 300
    char temp[SIM900_MAX_RECEIVE_CHARACTERS+1];
    strcpy_P( temp, PSTR("") );
    int32_t i = 0;
    /*
     * There are no received data so far.
     */
    bool isReceivedData = false;
    /*
     * Check UART buffer
     */
    while( uart_available() > 0 )
    {
        /*
         *  There are characters in the buffer so
         *  keep receiving until buffer in empty.
         */
        unsigned int c = uart_getc();
        _delay_us(150); // TODO: WEIRD KNOWN BUG!!!
        if ( c & UART_NO_DATA )
        {
            /*
             * No data available from UART. This is not an error.
             */
            error( AT, "No error but - No uart data" );// TODO: Delete it. It is not an error actually.
        }
        else if ( c & UART_FRAME_ERROR )
        {
            /* Framing Error detected, i.e no stop bit detected */
            errorOccured( ER_UART_FRAME_ERROR );
            error( AT, "Frame error" );
        }
        else if ( c & UART_OVERRUN_ERROR )
        {
            /* 
             * Overrun, a character already present in the UART UDR register was 
             * not read by the interrupt handler before the next character arrived,
             * one or more received characters have been dropped.
             */
            errorOccured( ER_UART_OVERRUN_ERROR );
            error( AT, "Overrun error" );
        }
        else if ( c & UART_BUFFER_OVERFLOW )
        {
            /* 
             * We are not reading the receive buffer fast enough,
             * one or more received characters have been dropped.
             */
            errorOccured( ER_UART_BUFFER_OVERFLOW );
            error( AT, "Buffer overflow error" );
        }
        else
        {
            /*
             * There are data available add them to cString.
             */
            if( i<SIM900_MAX_RECEIVE_CHARACTERS )
            {
                temp[i] = (char) c;
                temp[i+1] = '';
                i++;
            }
            /*
             * We received something so change the flag
             * even if an error occurs in the next loop.
             */
            isReceivedData = true;
        }
    }
    copyString( data, temp );
    /*
     * Exit.
     */
    return isReceivedData;
}

读取基于堆的动态内存分配工作原理;这太宽泛了,无法解释。点,它的运行时是不确定的,它可能会导致堆碎片,所以你最终不会在一段时间后得到任何块。

更进一步,我认为你正在印刷一些东西。这也可能是一个问题。总而言之:不要使用运行时间(可能)超过实时场景可接受时间的函数(UART接收器施加了处理时间的上限)。

使用足够长的缓冲区来获得最大值。包的大小。如果你坚持使用线性处理顺序,你必须为这种情况做好准备。

更好的方法是使用中断来接收数据。这样,您就可以使用两个交替的缓冲区(每个缓冲区的max。大小),并在正常代码中处理一个缓冲区,同时通过中断处理程序接收下一个。

注意:除非你真的必须考虑了所有的含义,否则不要强制转换。

相关内容

  • 没有找到相关文章

最新更新