0
\$\begingroup\$

I am reading sending data to a PIC32MX7xx using UART. The data frames are all 6 bytes long, no exception.

The problem I am facing is that when there are 6 bytes waiting to be read and I send another 6 bytes too fast, the PIC locks up.

I am reading the bytes out in packets of 6 and writing them to a buffer to be put on a CAN network (but that's not important).

typedef struct {
    WORD id;
    DWORD data;
} WiFiRXPacket;

BYTE *WiFiRXTemp = NULL;
WiFiRXPacket* WiFiRXBuffer = NULL;
int RXelements = 0;                         
int RXallocated = 0; 

void TCPReceiving()
{
    int dataAmount = 0;
    int i = 0;
    int j = 0;
    dataAmount = DataRdyUART1();

    if(dataAmount != 0)
    {
        WiFiRXTemp = (BYTE *) malloc(6 * sizeof(BYTE));
        if(WiFiRXTemp)
        {

            while(i<6)
            {
                WiFiRXTemp[i] = getcUART1();
                i++;
            }

            WiFiRXPacket packet;
            packet.id = ((WORD)WiFiRXTemp[j]) << 8;
            packet.id += ((WORD)WiFiRXTemp[j + 1]);

            if (packet.id != 0)
            {
                packet.data = ((DWORD)WiFiRXTemp[j + 2]) << 24;
                packet.data += ((DWORD)WiFiRXTemp[j + 3]) << 16;
                packet.data += ((DWORD)WiFiRXTemp[j + 4]) << 8;
                packet.data += ((DWORD)WiFiRXTemp[j + 5]);

                PutInTXBuffer(packet.id, (QWORD)packet.data);   // Send the received command back for confirmation
            }
        }
    }
    free(WiFiRXTemp);                   // Deallocate the buffers memory
    WiFiRXTemp = NULL;                  // Re-initialize the buffer
}

Does anyone have a clue why this happens?

\$\endgroup\$

2 Answers 2

2
\$\begingroup\$

Most likely your FIFO buffer on your UART is simply full (it's only 8 levels deep). Section 21.7.2 of the PIC32 Family Reference Manual states:

If the FIFO is full and a new character is fully received into the UxRSR register, the overrun error bit, OERR (UxSTA<1>), is set. The word in UxRSR register is not kept, and further transfers to the receive FIFO are inhibited as long as the OERR bit is set. The user must clear the OERR bit in software to allow further data to be received.

Why this would cause the PIC to "lock up" is hard to say from the information you have provided. Maybe it goes into an interrupt handler that isn't properly defined.

\$\endgroup\$
2
  • \$\begingroup\$ What I meant with "lock up" is that my main loop seems to be running fine (debugging with the old toggling led) but it doesn't execute the function where it reads the bytes from the buffer (the TCPReceiving() I posted). I'll try checking the OERR bit and clearing it to see if I can get the function to execute again. \$\endgroup\$
    – JoGe
    Commented Apr 11, 2013 at 7:26
  • \$\begingroup\$ This was the problem. Cleared the flag when it was high and it works again. I'm thinking about putting a re-transmit on the transmitter side and then everything will be solved. Thanks! \$\endgroup\$
    – JoGe
    Commented Apr 11, 2013 at 9:21
0
\$\begingroup\$

Similar to what @fm_andreas was saying, it could be your FIFO and that your buffer is simply full. I'm not sure what "lock up" means, but I had a similar instance where I was sending too much data too fast with a 4 level FIFO.

You can perhaps, return a command to the host, saying "I'm done", which indicates that you have processed all data in the buffer, or you can simply add a delay before you transmit more data. Depending on your baud rate, you can calculate how long it takes to transmit all the bits, and possibly add a bit more of a delay to account for the overhead required to read and clear the buffer.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.