View previous topic :: View next topic |
Author |
Message |
championx
Joined: 28 Feb 2006 Posts: 151
|
RS232 hanging |
Posted: Wed Jun 10, 2015 12:13 pm |
|
|
Hi! im making a test program for a 24EP512GU810, I defined the 4 UART ports like this:
Code: | #use rs232(baud=9600, UART1, STREAM = U1, ENABLE = PIN_B15, errors)
#use rs232(baud=9600, UART2, STREAM = U2 ,errors)
#use rs232(baud=9600, UART3, STREAM = U3 ,errors)
#use rs232(baud=9600, UART4, STREAM = U4 ,errors) |
But when the uart overflows it just stops working. The other parts of the program works ok, but the uart just dont.
I remember that on 16F and 18F chips this was solved using the ERRORS keyword on the uart config... Do i neet to have a special consideration with the 24EP chips?
thanks! |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9225 Location: Greensville,Ontario
|
|
Posted: Wed Jun 10, 2015 2:53 pm |
|
|
U1 appears to be RS485 ? If so be sure it's wired correctly as it might hang...
Do you try each port separately? Did they all fail or was it only when your program accessed all four that it crashed?
A look at you complete program might enlighten us.. Are these polled or ISR based? That might help us as well...
Jay
It's always best to show us a complete program that fails,could be a 'setup' issue and not really the comports. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Thu Jun 11, 2015 12:14 am |
|
|
There is a key difference in the PIC24, when handling the serial ISR.
On the PIC16/18, there is only a single character buffer. On the PIC24, there are four characters of buffering. You can program to interrupt when any character is received, or when the buffer is full to a certain point. However whichever method is used, the ISR routine needs to read _all_ characters in the buffer. So a structure like:
Code: |
#INT_RDA
void uart1rx(void) {
char temp;
do
{
temp=fgetc(U1);
tobuff(U1RX, temp);
}
while (kbhit(U1));
}
|
This uses a macro to write to the buffer - obviously substitute your own buffer code.
If this is not done, and the buffer has overrun, only one character gets retrieved, and the buffer is still technically in overrun.
Seriously I also have to ask why the UART's are overrunning?. With interrupt driven communications it is pretty near impossible to overrun the UART's especially at a low rate like 9600bps. Means there has to be over 4mSec when the port is not being handled. You need to solve this first.... |
|
|
championx
Joined: 28 Feb 2006 Posts: 151
|
|
Posted: Thu Jun 11, 2015 8:18 am |
|
|
Thanks for your answer, this is my code, i think that I'm not spending too much time inside the interrupt routine. I'm running the pic at 60mhz.
Code: | #INT_RDA2
void UART2_ISR()
{
input_buffer_U2[U2RX_index] = fgetc(U2);
if(input_buffer_U2[0] == COMMAND_START_U2 && U2RX_index < BUFFER_SIZE_U2)
{
if(input_buffer_U2[U2RX_index] == COMMAND_END_U2)
{
input_buffer_U2[++U2RX_index] = FROM_U2;
input_buffer_U2[++U2RX_index] = 0;
U2RX_index++;
memcpy(input_queue + input_queue_last_index,input_buffer_U2,U2RX_index);
memset(input_buffer_U2,0,sizeof(input_buffer_U2));
input_queue_last_index = input_queue_last_index + U2RX_index;
U2RX_index = 0;
} else U2RX_index++;
}
return;
} |
What happen if i disable interrupts for a second and in that second i get a uart overflow? is there any method to avoid this? |
|
|
RF_Developer
Joined: 07 Feb 2011 Posts: 839
|
|
Posted: Thu Jun 11, 2015 8:27 am |
|
|
Do not use a return statement to exit an ISR. ISRs use a special return from interrupt process that's handled for you by the compiler.
There should not be any need to copy or set large buffers in an UART receive ISR. Just copy the new character into right place in the buffer. Adjust character counts/buffer pointers/full/empty flags etc. as required.
If you need two buffers, one for message just received an one for new message coming in, then use a ping-pong/double buffer arrangement where you switch from one to the other, or a circular buffer. There should not be any need to clear or copy a buffer in the ISR itself. Emptying a buffer should be as simple as resetting a pointer or character count.
Last edited by RF_Developer on Thu Jun 11, 2015 8:33 am; edited 1 time in total |
|
|
championx
Joined: 28 Feb 2006 Posts: 151
|
|
Posted: Thu Jun 11, 2015 8:30 am |
|
|
Hi RF_Developer, i didnt know that. Thanks! is this for all pics? |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Fri Jun 12, 2015 1:03 am |
|
|
RF_Developer wrote: | Do not use a return statement to exit an ISR. ISRs use a special return from interrupt process that's handled for you by the compiler. | I don't agree. It is true that an ISR has some special code added by the compiler and at a lower level uses a special 'return from interrupt' command, but a C-type return statement like this will just exit the current function and return to the calling code. It doesn't matter that this is an ISR.
The only thing here is that a return statement as the last code line is superfluous, i.e. it doesn't add anything extra. Code is easier to read when this 'empty' code is left out. |
|
|
|