0

I'm trying to read byte per byte from the RX FIFO, that's 8 bytes deep. The problem is that when the first byte is received, I have to wait for 8 other bytes to finally get the first byte on U1RXREG. I'd like to know how to perform some dummy reads on the FIFO to access a particuar byte and "flush" it. Simply putting U1RXREG in an array doesn't do the trick.

Here's my initialization :

void UART_Initialize(void)
{
    // SIDL: Stop in Idle Mode bit : 1 = Discontinue module operation when the device enters Idle mode
    U1MODEbits.SIDL = 0;
    // IREN: IrDA Encoder and Decoder Enable bit : 0 = IrDA is disabled
    U1MODEbits.IREN = 0;
    // RTSMD: Mode Selection for UxRTS Pin bit : 0 = UxRTS pin is in Flow Control mode
    U1MODEbits.RTSMD = 0;
    // UEN<1:0>: UARTx Enable bits : 10 = UxTX, UxRX, UxCTS and UxRTS pins are enabled and used
    U1MODEbits.UEN1 = 1;
    U1MODEbits.UEN0 = 0;
    // WAKE: Enable Wake-up on Start bit Detect During Sleep Mode bit : 0 = Wake-up disabled
    U1MODEbits.WAKE = 0;
    // LPBACK: UARTx Loopback Mode Select bit : 0 = Loopback mode is disabled
    U1MODEbits.LPBACK = 0;
    // ABAUD: Auto-Baud Enable bit : 0 = Baud rate measurement disabled or completed
    U1MODEbits.ABAUD = 0;
    // RXINV: Receive Polarity Inversion bit : 0 = UxRX Idle state is ‘1’
    U1MODEbits.RXINV = 0;
    // BRGH: High Baud Rate Enable bit : 0 = Standard Speed mode – 16x baud clock enabled
    U1MODEbits.BRGH = 0;
    // PDSEL<1:0>: Parity and Data Selection bits : 01 = 8-bit data, even parity
    U1MODEbits.PDSEL1 = 0;
    U1MODEbits.PDSEL0 = 1;
    // STSEL: Stop Selection bit : 0 = 1 Stop bit
    U1MODEbits.STSEL = 0;

    // ADM_EN: Automatic Address Detect Mode Enable bit : 0 = Automatic Address Detect mode is disabled
    U1STAbits.ADM_EN = 0;
    // UTXISEL<1:0>: TX Interrupt Mode Selection bits : 00 = Interrupt is generated and asserted while the transmit buffer contains at least one empty space
    U1STAbits.UTXISEL1 = 0;
    U1STAbits.UTXISEL0 = 0;
    // UTXINV: Transmit Polarity Inversion bit : 0 = UxTX Idle state is ‘1’ (with IrDA disbled)
    U1STAbits.UTXINV = 0;
    // URXEN: Receiver Enable bit : 1 = UARTx receiver is enabled. UxRX pin is controlled by UARTx (if ON = 1)
    U1STAbits.URXEN = 1;
    // UTXBRK: Transmit Break bit : 0 = Break transmission is disabled or completed
    U1STAbits.UTXBRK = 0;
    // URXISEL<1:0>: Receive Interrupt Mode Selection bit : 00 = Interrupt flag bit is asserted while receive buffer is not empty (i.e., has at least 1 data character)
    U1STAbits.URXISEL1 = 0;
    U1STAbits.URXISEL0 = 0;
    // ADDEN: Address Character Detect bit (bit 8 of received data = 1) : 0 = Address Detect mode is disabled
    U1STAbits.ADDEN = 0;

    // Baud Rate Calculation :
    // FPB = 10MHz ; Desired Baud Rate = 9600 bauds
    // => U1BRG = FPB/(16*BaudRate)-1 = 64 (error = 0,16%)
    U1BRG = 64;

    // Enable UART RX interrupts
    //IEC1bits.U1RXIE = 1;

    // Enable UART 
    // ON: UARTx Enable bit : 1 = UARTx is enabled. UARTx pins are controlled by UARTx as defined by the UEN<1:0> and UTXEN control bits.
    U1MODEbits.ON = 1;
    // UTXEN: Transmit Enable bit : 1 = UARTx transmitter is enabled. UxTX pin is controlled by UARTx (if ON = 1).
    U1STAbits.UTXEN = 1;

}

For the moment I tried reading like this, in the while(1) loop, without success :

    while (1)
    {
        uint8_t rxbyte[8];
        bool b;
        //if (U1STAbits.URXDA == 1)
        while(!U1STAbits.URXDA);
        rxbyte[0] = U1RXREG;
        rxbyte[1] = U1RXREG;
        rxbyte[2] = U1RXREG;
        rxbyte[3] = U1RXREG;
        rxbyte[4] = U1RXREG;
        rxbyte[5] = U1RXREG;
        rxbyte[6] = U1RXREG;
        rxbyte[7] = U1RXREG;
        sprintf(s, "I received : %u %u %u %u %u %u %u %u\n\r", rxbyte[0], rxbyte[1], rxbyte[2], rxbyte[3], rxbyte[4], rxbyte[5], rxbyte[6], rxbyte[7]);
        myPrint(s);
        IFS1bits.U1RXIF = 0;
    }

The 8 bytes in rxbytes[] are always the same. I tried to clear the RX interrupt flag between the reads, reading URXDA also between the reads, add a delay, still no success, I still have to wait for 8 incoming bytes to access the first one.

Thanx in advance for your help !

Best regards.

Eric

ricothebrol
  • 81
  • 12

2 Answers2

0

I finally found the solution : I had a double configuration, one by MCC and one by myself, and it seems that having the interruptions activated caused this. Now I switched the interrupts off and I simply poll URXDA, and everything works fine. Sorry for bothering you guys !

ricothebrol
  • 81
  • 12
0

You can use your original method too. The problem with your While(1) loop is that you check if there is "at least 1 byte in the hardware Rx buffer" ( U1STAbits.URXDA tells you as soon as 1 byte is availlable ). When 1 byte is availlable, then you read 8 bytes inside the loop... so of course you will get 8 identical bytes when there is only 1 byte in the buffer.