CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

'Flush' fprintf

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
SamMeredith



Joined: 13 Jul 2018
Posts: 23

View user's profile Send private message

'Flush' fprintf
PostPosted: Wed Aug 05, 2020 7:35 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Aug 05, 2020 9:22 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Aug 05, 2020 5:00 pm     Reply with quote

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

Code:
u3_flush();


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

View user's profile Send private message

PostPosted: Thu Aug 06, 2020 1:15 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Aug 06, 2020 1:44 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Aug 06, 2020 2:35 am     Reply with quote

The hard bit..... Very Happy

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... Sad
SamMeredith



Joined: 13 Jul 2018
Posts: 23

View user's profile Send private message

PostPosted: Thu Aug 06, 2020 7:37 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Aug 06, 2020 8:08 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Aug 06, 2020 8:42 am     Reply with quote

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?.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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