1
votes

I am debugging some interrupt service routines (ISR) and need to print out the contents of a few registers whenever I enter the ISR. The firmware is in C.

There is an interrupt raised every few milli-seconds and if I printf() to the console, it is too slow and I end up missing interrupts. What is the fastest way to print something - either to a buffer or file or console- and any tips to do that?

[Additional info: I need to do some other things in the ISR -such as interrupt mask and compare, reset interrupts, enable interrupts and finally print out some registers.]

Thanks

2
Whilst the bulk of the time in printf will be the console interaction, you're probably better off just storing the register values somewhere, and then doing the text formatting outside your ISR. - Oliver Charlesworth
Speed isn't a concept defined by C. - autistic

2 Answers

1
votes

The fastest way would be to store the relevant data somewhere in a cache, and then print it, when you have time for it. Printing to the console is definitely slow, and using printf is maybe also not a good idea, especially if there are several variables to convert.

Since I don't know the dynamics of your code, I can only give some recommendation.

Define a datastructure for your data. Preallocate a big enough array, and then put ringbuffer mechanism, that handles the indexes of where the ISR can currently write. For the ISR this should be rather fast, because it just fills outr the values in the next empty slot.

In the main routine you can then print at leisure. However, you have to synchronize the access and also take care that the ISR doesn't produce data much faster than you can discard it. At least with a proper ringbuffer, it shouldn't crash, but you might loose information.

0
votes

You can switch buffering on by using C89's setvbuf()