 |
 |
| View previous topic :: View next topic |
| Author |
Message |
RF_Developer
Joined: 07 Feb 2011 Posts: 839
|
|
Posted: Mon Jul 09, 2012 2:11 am |
|
|
| Quote: |
is there an obvious bug in the code below?
|
Yes there is something that stands out to me. Newguy and asmallri have alluded to it. Its something that even the CCS and microchip examples don't get right, and its effect is to cause occasional errors and/or lock-ups. Its a basic system programming issue. It is the lack of interrupt locking by (non) use of critical sections.
Consider carefully what happens if an serial interrupt occurs at every point of your putc code. Is it going to have any effect? As you only read t_next_out once, at the start of DebugPutc you might think all is well, but what happens if after that an interrupt comes in and changes the buffer content? Now your flag, restart, may be wrong. It certainly will be if that was the last character, emptying the buffer. You code will think you don't need to turn the interrupt back on. Result: your code stops sending. In the end your buffer fills up and your code hangs in the while loop. This is timing critical, and in any one code that uses this routine, it may work fine, or only occasionally not. The CCS supplied putc may, or may not even get this right. I've not looked at the assembler it generates it in any detail.
What you need to do is to turn interrupts off before entering this code, and restart them after. You can do it with just INT_TBEn but be careful. The TBE hardware in PICs workd like no other that I know, as Newguy said. Its odd like that. Perhaps better in this instance to do a global interrupt (i.e. turn off all interrupts) lock rather than the more usual local version (turning off only the relevant interrupt).
The same sort of problem comes on receive where you can loose or get duplicated characters due to similar problems. You should treat both getting and putting characters to/from buffers, indeed all interrupt driven buffer operations, as atomic. It isn't just a matter of 16 or more bit variables, though that IS definitely a problem, its more than that. Whole sections of code need to be atomic. You have to make them so.
This used to be a bigger problem, but as processors and baud rates have increased so the probability of visible problems has decreased. That just means it happens less often, not that it doesn't happen at all.
RF Developer |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 20161
|
|
Posted: Mon Jul 09, 2012 2:44 am |
|
|
The 'hack' of the CCS example, looks really cr&p....
There is nothing 'proper' about using INT_TBE as a trigger to 'squirt data out'. INT_TBE, implies just _one_ character can be sent, so send it.
You don't check for buffer _overflow_ when adding the characters.
Now you correctly use the test against buffer full to avoid using the modulus operator in the interrupt code (good, especially with an odd buffer size like '250'), but then don't use the same code in the actual putc routine. The fix should be in both places. Then it is 'messy' programming to enclose putc, in another routine called bputc. Though (hopefully) the compiler's optimiser is smart enough to get rid of this, why create extra complexity?.
There is also no point in subtracting the buffer size when the counter reaches the end. The counter only ever increments by one, so if it has got to the top limit, the next location is '0'. Saves code.
| Code: |
#use rs232(baud=115200, BITS=8, PARITY=N, STOP=1, xmit=PIN_B6, rcv=PIN_B7, ERRORS)
#define DebugGetc() getc()
#define T_BUFFER_SIZE 250
unsigned int8 t_buffer[T_BUFFER_SIZE];
unsigned int8 t_next_in = t_next_out = 0;
#define is_empty() (t_next_in==t_next_out)
#int_tbe2
void bserial_isr(void) {
output_high(PIN_A2); /* V1.19 DEBUG */
if(is_empty()) {
disable_interrupts(INT_TBE2);
}
else {
putc(t_buffer[t_next_out++]);
if (t_next_out >= T_BUFFER_SIZE)
t_next_out = 0;
}
output_low(PIN_A2); /* V1.19 DEBUG */
}
/* ------------------------------------------------------------------------- */
void DebugPutc(char c) {
int ni;
ni=t_next_in+1;
if (ni>=T_BUFFER_SIZE)
ni=0;
disable_interrupts(INT_TBE2);
while(ni==t_next_out) {
//buffer is full - wait for character to transmit
enable_interrupts(INT_TBE2);
disable_interrupts(INT_TBE2);
}
t_buffer[t_next_in]=c;
t_next_in=ni;
enable_interrupts(INT_TBE2);
}
/* ------------------------------------------------------------------------- */
|
Try this.
Best Wishes |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|