• AVR Freaks

Hot!UART buffer size

Author
ketanVadodariya
New Member
  • Total Posts : 6
  • Reward points : 0
  • Joined: 2021/03/01 02:26:14
  • Location: 0
  • Status: offline
2021/03/02 22:24:42 (permalink)
0

UART buffer size

Dear All,
 
This is first thread i am creating, requesting to all of you to help here to solve my question.
 
Question 1.  How do i get the more data (Chunk of data) with small size of the fifo. ?
 
please have a look for the code. this is source code of the UART driver. here I have received and transfer more than 1 Kb data to the UART driver and to achive that i have declare the @UART1_CONFIG_TX_BYTEQ_LENGTH and @UART1_CONFIG_RX_BYTEQ_LENGTH size as 1550 .
 
This is Too big for the me. how do i reduce this fifo buffer to get chunck data.
 
 
#ifdef DATA_SEND_BY_GSM

#include "../printf/printf_uart.h"

#include "rs232.h"

#include <xc.h>

/** UART Driver Queue Status

  @Summary
    Defines the object required for the status of the queue.
 */

typedef union
{

    struct
    {
        uint8_t full : 1;
        uint8_t empty : 1;
        uint8_t reserved : 6;
    } s;
    uint8_t status;
} UART_BYTEQ_STATUS;

typedef struct
{
    /* RX Byte Q */
    uint8_t *rxTail;

    uint8_t *rxHead;

    /* TX Byte Q */
    uint8_t *txTail;

    uint8_t *txHead;

    uint16_t numberOfByte;

    UART_BYTEQ_STATUS rxStatus;

    UART_BYTEQ_STATUS txStatus;

} UART_OBJECT;

static volatile UART_OBJECT uart1_obj;

/** UART Driver Queue

  @Summary
    Defines the Transmit and Receive Buffers

 */

uint8_t uart1_txByteQ[UART1_CONFIG_TX_BYTEQ_LENGTH];
uint8_t uart1_rxByteQ[UART1_CONFIG_RX_BYTEQ_LENGTH];

uint16_t numberOfByte = 0;

/**
  Section: Driver Interface
 */

void RS232UART_Initialize(long baudrate, UART_STOPbits stopBits, UART_PARITYbits parity)
{

    U1MODE = 0x0008;

    U1MODEbits.PDSEL = parity;

    U1MODEbits.STSEL = stopBits;

    U1STA = 0x0000;

    U1BRG = UBRG_VALUE(baudrate);

    U1STAbits.UTXISEL1 = 0;

    U1STAbits.UTXISEL0 = 0;

    // U1STAbits.URXISEL = 0b01;

    //INTERRUPT ENABLE CONTROL REGISTER

    IEC0bits.U1RXIE = true;

    // IEC0bits.U1TXIE = false;


    //Make sure to set LAT bit corresponding to TxPin as high before UART initialization

    UART1_Enable(); // enabling UARTEN bit

    uart1_obj.txHead = uart1_txByteQ;
    uart1_obj.txTail = uart1_txByteQ;
    uart1_obj.rxHead = uart1_rxByteQ;
    uart1_obj.rxTail = uart1_rxByteQ;
    uart1_obj.rxStatus.s.empty = true;
    uart1_obj.txStatus.s.empty = true;
    uart1_obj.txStatus.s.full = false;
    uart1_obj.rxStatus.s.full = false;
}

/**
    Maintains the driver's transmitter state machine and implements its ISR
 */

void __attribute__((interrupt, no_auto_psv)) _U1TXInterrupt(void)
{
    if ((uart1_obj.txHead == uart1_obj.txTail) && (uart1_obj.txStatus.s.full == false))
    {
        while (U1STAbits.TRMT == 0)
        {
        }

        uart1_obj.txStatus.s.empty = true;
        IEC0bits.U1TXIE = 0;
        return;
    }

    IFS0bits.U1TXIF = 0;

    while (!(U1STAbits.UTXBF == 1))
    {
        U1TXREG = *uart1_obj.txHead;

        uart1_obj.txHead++;

        if (uart1_obj.txHead == (uart1_txByteQ + UART1_CONFIG_TX_BYTEQ_LENGTH))
        {
            uart1_obj.txHead = uart1_txByteQ;
        }

        uart1_obj.txStatus.s.full = false;

        if (uart1_obj.txHead == uart1_obj.txTail)
        {
            break;
        }
    }
}

void __attribute__((interrupt, no_auto_psv)) _U1RXInterrupt(void)
{

    while ((U1STAbits.URXDA == 1))
    {
        *uart1_obj.rxTail = U1RXREG;

        uart1_obj.rxTail++;

        uart1_obj.numberOfByte = uart1_obj.rxTail - uart1_rxByteQ;

        if (uart1_obj.rxTail == (uart1_rxByteQ + UART1_CONFIG_RX_BYTEQ_LENGTH))
        { //check if fifo reached maximum size
            //of the buffer
            uart1_obj.rxTail = uart1_rxByteQ; //initialize back to original address of the buffer
        }

        uart1_obj.rxStatus.s.empty = false;

        if (uart1_obj.rxTail == uart1_obj.rxHead)
        {
            //Sets the flag RX full
            uart1_obj.rxStatus.s.full = true;

            break;
        }

    }

    IFS0bits.U1RXIF = false;
}

void __attribute__((interrupt, no_auto_psv)) _U1ErrInterrupt(void)
{
    if ((U1STAbits.OERR == 1))
    {
        U1STAbits.OERR = 0;
    }

    IFS4bits.U1ERIF = false;
}

/**
  Section: UART Driver Client Routines
 */


uint8_t UART1_Read(void)
{

    uint8_t data = 0;

    data = *uart1_obj.rxHead;

    uart1_obj.rxHead++;

    uart1_obj.numberOfByte--;

    if (uart1_obj.rxHead == (uart1_rxByteQ + UART1_CONFIG_RX_BYTEQ_LENGTH))
    {
        uart1_obj.rxHead = uart1_rxByteQ;
    }

    if (uart1_obj.rxHead == uart1_obj.rxTail)
    {
        uart1_obj.numberOfByte = 0;
        uart1_obj.rxStatus.s.empty = true;
    }

    uart1_obj.rxStatus.s.full = false;

    return data;
}

unsigned int UART1_ReadBuffer(uint8_t *buffer, const unsigned int bufLen)
{

    unsigned int numBytesRead = 0;

    while (numBytesRead < (bufLen))
    {
        if (uart1_obj.rxStatus.s.empty)
        {
            break;
        }
        else
        {
            buffer[numBytesRead++] = UART1_Read();
        }
    }

    return numBytesRead;
}

uint16_t UART1_NumberOfByte(void)
{
    return (uart1_obj.numberOfByte);
}

void UART1_Write(const uint8_t byte)
{
    IEC0bits.U1TXIE = 0;

    *uart1_obj.txTail = byte;

    uart1_obj.txTail++;

    if (uart1_obj.txTail == (uart1_txByteQ + UART1_CONFIG_TX_BYTEQ_LENGTH))
    {
        uart1_obj.txTail = uart1_txByteQ;
    }

    uart1_obj.txStatus.s.empty = false;

    if (uart1_obj.txHead == uart1_obj.txTail)
    {
        uart1_obj.txStatus.s.full = true;
    }

    IEC0bits.U1TXIE = 1;
}

unsigned int UART1_WriteBuffer(const uint8_t *buffer, const unsigned int bufLen)
{
    unsigned int numBytesWritten = 0;

    while (numBytesWritten < (bufLen))
    {
        if ((uart1_obj.txStatus.s.full))
        {
            break;
        }
        else
        {
            UART1_Write(buffer[numBytesWritten++]);
        }
    }

    return numBytesWritten;
}

UART1_TRANSFER_STATUS UART1_TransferStatusGet(void)
{
    UART1_TRANSFER_STATUS status = 0;

    /* The TX empty must be checked before the full in order to prevent a race
     * condition where a TX transmission could start between these two checks
     * resulting in both full and empty set at the same time.
     */
    if (uart1_obj.txStatus.s.empty)
    {
        status |= UART1_TRANSFER_STATUS_TX_EMPTY;
    }

    if (uart1_obj.txStatus.s.full)
    {
        status |= UART1_TRANSFER_STATUS_TX_FULL;
    }

    /* The RX full must be checked before the empty in order to prevent a race
     * condition where a RX reception could start between these two checks
     * resulting in both empty and full set at the same time.
     */
    if (uart1_obj.rxStatus.s.full)
    {
        status |= UART1_TRANSFER_STATUS_RX_FULL;
    }

    if (uart1_obj.rxStatus.s.empty)
    {
        status |= UART1_TRANSFER_STATUS_RX_EMPTY;
    }
    else
    {
        status |= UART1_TRANSFER_STATUS_RX_DATA_PRESENT;
    }
    return status;
}

void UART1_Enable(void)
{
    U1MODEbits.UARTEN = 1;

    U1STAbits.UTXEN = 1;
}

#endif //DATA_SEND_BY_GSM

#1

6 Replies Related Threads

    Aussie Susan
    Super Member
    • Total Posts : 3857
    • Reward points : 0
    • Joined: 2008/08/18 22:20:40
    • Location: Melbourne, Australia
    • Status: offline
    Re: UART buffer size 2021/03/04 19:14:49 (permalink)
    0
    What you are asking is really a design issue that you have to answer yourself.
    Why is 1550 too big? Have you tried working out the actual maximum packet size you need to send and receive and used that value?
    If all else fails you may name to start thinking about other arrangements. You don't show us how all of that code actually gets used. For example, are you waiting for the entire packet to be received before you start sending? If you are simply receiving a value and then sending it again, then a buffer of a few bytes would suffice. Depending what is in the packet, perhaps read some part, process it and start sending before going back to read the next part - you are using a ring buffer so as long as the buffer is large enough to hold the parts you need to process before being over-written then you can set the ring buffer size accordingly.
    Susan
    #2
    ketanVadodariya
    New Member
    • Total Posts : 6
    • Reward points : 0
    • Joined: 2021/03/01 02:26:14
    • Location: 0
    • Status: offline
    Re: UART buffer size 2021/03/04 22:34:58 (permalink)
    0
    Dear Susan,
     
    I am not getting what you're saying but question is simple as it's. Receive response is not a fixed means it is not a pattern type response, it would be anything from the GSM module. so here HTTP is involved so perhaps may get jSON packet as response.
    #3
    ric
    Super Member
    • Total Posts : 30223
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: UART buffer size 2021/03/04 22:37:49 (permalink)
    0
    Susan is saying you don't need to buffer all the information in the USART buffer before you handle it.
    Just handle the data as it arrives, reading from the USART FIFO whenever it has data ready and handle it "on the fly".
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #4
    ketanVadodariya
    New Member
    • Total Posts : 6
    • Reward points : 0
    • Joined: 2021/03/01 02:26:14
    • Location: 0
    • Status: offline
    Re: UART buffer size 2021/03/04 22:47:33 (permalink)
    0
    Dear Ric,
     
    Here is my reading logic from the USART FIFO. I am reading whole data from the UART FIFO once data present. so for the store of the data i need to have again big size of the buzzer.

    [code]
    //check data is available at uart port
    if (core->QUECTELM66_Available() > 4)
    {
    //read the byte until fifo-empty
    while (UART1_TRANSFER_STATUS_RX_DATA_PRESENT & UART1_TransferStatusGet())
    {
    commandParserBuffer = core->MSD_M66Read();
    //wait to read each char
    delay_ms(2);
    i++;
    }
    #5
    RISC
    Super Member
    • Total Posts : 6056
    • Reward points : 0
    • Status: offline
    Re: UART buffer size 2021/03/05 03:43:36 (permalink)
    0
    Hi,
    Which device do you use ???
    Regards
     

    For support make sure to check first here : http://microchipdeveloper.com
    There are hundreds of PIC, AVR, SAM...which one do YOU use ?
    #6
    ketanVadodariya
    New Member
    • Total Posts : 6
    • Reward points : 0
    • Joined: 2021/03/01 02:26:14
    • Location: 0
    • Status: offline
    Re: UART buffer size 2021/03/05 03:45:20 (permalink)
    0
    Dear 
     
    I am using pic24fj256ga702.
    #7
    Jump to:
    © 2021 APG vNext Commercial Version 4.5