Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[SOLVED] PIC18F2550 + High speed serial
13-07-2014, 06:52 PM, (This post was last modified: 15-07-2014, 07:19 PM by didaloca.)
#1
[SOLVED] PIC18F2550 + High speed serial
Hi all,
I seem to have run into a small problem which only seems to affect fairly high speed serial >1Mhz. I'm using it to connect to an ELM327 chip.
At 1Mhz it seems to miss out a lot of chars.
At 2Mhz it will receive a few bytes and then completely stop receiving (even though the other chip is definitely sending).
In both cases sending chars works fine with no errors.
Interestingly if I connect 2 identical pinguino boards together I can run at 2Mhz with zero problems.
I've also tried it on a PIC32 pinguino board which also works perfectly.
My hardware is a PIC18F2550 on a home-brew board running bootloader v4.14.

My suspicions is that the PIC is flagging either OERR or FERR and I'm wondering how I can check for these?

The code is quite simple, it's more or less a virtual COM port.


Code:
void setup()
{
    Serial.begin(2000000);
}

void loop()
{
    u8 array[64];
    u8 len = CDC.read(array);
    
    u8 i = 0;
    if(len > 0)
    {
        
        for(i = 0; i < len; i++)
        {
                Serial.write(array[i]);
        }
    }


    if(Serial.available())
    {
        u8 c = Serial.read();
        CDC.write(&c, 1);
    }        
}
Reply
14-07-2014, 01:10 AM,
#2
RE: PIC18F2550 + High speed serial
After some more testing it seems that the pic is flagging OERR. I can stop the USART RX from freezing by adding:
Code:
if(RCSTAbits.OERR) { RCSTAbits.CREN = 0; RCSTAbits.CREN = 1;

to serial.c, but this doesn't solve any problems as I am now missing chunks of data.

I'm still thinking that 2Mhz is too fast with the overhead from the USB and Pinguino.
Reply
15-07-2014, 10:40 AM,
#3
RE: PIC18F2550 + High speed serial
(14-07-2014, 01:10 AM)didaloca Wrote: I'm still thinking that 2Mhz is too fast with the overhead from the USB and Pinguino.

I also think the reason you're getting overrun errors is because you are writing stuff to the USB CDC between receiving data. You might change your code so that it does all the sending and receiving before you write to the CDC.
It is easier to complain than it is to do, but it is better to do than it is to complain.
Reply
15-07-2014, 07:18 PM,
#4
RE: PIC18F2550 + High speed serial
(15-07-2014, 10:40 AM)regis Wrote: I also think the reason you're getting overrun errors is because you are writing stuff to the USB CDC between receiving data. You might change your code so that it does all the sending and receiving before you write to the CDC.

You're absolutely right! It works perfect at 1Mhz.

Code:
void setup()
{
    Serial.begin(1000000);
}

u8 cdc_read[64];
u8 cdc_read_len = 0;

u8 cdc_write[64];
u8 cdc_write_len = 0;

void cdcReadWrite(u8 *writeBuffer, u8 writeLen, u8 *readBuffer, u8 *readLen)
{
    if(writeLen > 0)
    {
        CDC.write(writeBuffer, writeLen);
    }
    *readLen = CDC.read(readBuffer);
}

void serialReadWrite(u8 *writeBuffer, u8 writeLen, u8 *readBuffer, u8 *readLen, u8 maxRead)
{
    u8 i;
    *readLen = 0;
    while(Serial.available())
    {
        readBuffer[*(readLen)] = Serial.read();
        *readLen = (*readLen)+1;
        if(*readLen >= maxRead) break;
    }
    
    for(i = 0; i < writeLen; i++)
    {
        Serial.write(writeBuffer[i]);
    }
}

void loop()
{
    cdcReadWrite(cdc_write, cdc_write_len, cdc_read, &cdc_read_len);
    serialReadWrite(cdc_read, cdc_read_len, cdc_write, &cdc_write_len, 64);

}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)