View previous topic :: View next topic |
Author |
Message |
SamMeredith
Joined: 13 Jul 2018 Posts: 23
|
'Flush' fprintf |
Posted: Wed Aug 05, 2020 7:35 am |
|
|
Pic: DSPIC33EP512MC806
Compiler Version: 5.093
I have serial output set up for debugging:
Code: |
#use rs232(UART3, baud=115200, parity=N, bits=8, errors, TIMEOUT=1, stream=DEBUG)
#define DEBUGPRINTF( x ) fprintf( DEBUG, x )
|
When doing some basic debugging, printing where the code reaches before a crash, I'm finding that my output gets truncated.
In a console application I would call fflush() to force the output to write.
Is there an equivalent in the CCS compiler to force output before continuing? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19546
|
|
Posted: Wed Aug 05, 2020 9:22 am |
|
|
The output takes place as fast as it can. So there is nothing you can do
to speed it up. You just have to add code to wait while the port has
any data to send.
Normally on a UART, you could wait while the TBE bit is not set.
So in your case use a #bit to allocate the TBE3 bit, and wait for this
to go high. |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1355
|
|
Posted: Wed Aug 05, 2020 5:00 pm |
|
|
For PIC24/dsPIC, you can watch the TRMT bit (see the datasheet for details).
It's been a while, but if you create the TRMT variable using #bit, then you can do something like this:
Code: |
#word U3STA = getenv("SFR:U3STA")
#bit U3TRMT = U3STA.8
#define u3_flush() while(!U3TRMT){}
|
Then just call
I might have my logic backwards, it's been a while, so I don't recall if it is active high or low. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19546
|
|
Posted: Thu Aug 06, 2020 1:15 am |
|
|
Yes. TRMT is better. On the PIC24, this reflects both the shift register and the
buffer register being empty.
Use this.
It's a '1' when the buffer is empty. So wait while it is low.
#define u3_flush() while(U3TRMT==0){} |
|
|
SamMeredith
Joined: 13 Jul 2018 Posts: 23
|
|
Posted: Thu Aug 06, 2020 1:44 am |
|
|
Thanks!
I have defined u3_flush() as you both suggest. Seems to be working well.
Now to find the bug itself... |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19546
|
|
Posted: Thu Aug 06, 2020 2:35 am |
|
|
The hard bit.....
I'm having one at the moment, with fp maths (fifth order polynomial) being
done on four PIC33's, occasionally triggering #INT_MATHERR. If I don't trap
this, the chip crashes. I've had to add a flag, and set this in the MATHERR
handler, then after the arithmetic, if this is set, invalidate the number
returned. There doesn't seem to be a 'tidy' way to handle this presently
in the compiler.
Took some tracking down... |
|
|
SamMeredith
Joined: 13 Jul 2018 Posts: 23
|
|
Posted: Thu Aug 06, 2020 7:37 am |
|
|
That does sound nasty.
I'm not that familiar with traps, #INT_MATHERR triggers on an attempted divide by 0?
If it were as simple as that you'd have caught it in the code, so it must be a fp precision error part way through a calculation that turns a small (but not actually 0) value into a 0?
Well beyond my level, but I came across an article on hackernews yesterday on fp error analysis.
What would you like to happen in an ideal compiler?
Some way to give up on the calculation as soon as #INT_MATHERR triggers?
I've located my bug. The CCS TCP/IP stack detects an invalid MAC header from the ENC28J60 chip I'm using and resets the PIC in response.
I'm unsure why a) it resets rather than just discarding the packet and b) it happens on this project and not others using the same circuit. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9245 Location: Greensville,Ontario
|
|
Posted: Thu Aug 06, 2020 8:08 am |
|
|
re: Quote: | I'm unsure why a) it resets rather than just discarding the packet and b) it happens on this project and not others using the same circuit. |
Murphy's Law maybe ???? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19546
|
|
Posted: Thu Aug 06, 2020 8:42 am |
|
|
Yes.
On my maths error. This is an internal division sub,
inside the fp maths, not necessarily a actual division by zero. I'd have
to take the fp routines apart to find what is actually triggering it, but it
seems that some particular fp values are resulting in a /0 trigger.
Now the compiler does have a maths error 'errno', which is meant to
be set if an operation involves a /0. However the hardware error trips,
rather than this being set.
In your case I'd suspect that some issue with the particular chip/setup,
is triggering your error. Not something like you have the WDT enabled
on this project and not the others?. |
|
|
|