Altera_Forum
Honored Contributor
21 years agoprintf("") in an interrupt handler
For debugging, printf's are such a useful tool, that I would like to try to circumvent the troubles associated with its use in irq routines.
it says in the Nios II Software Developer’s manual that <div class='quotetop'>QUOTE </div> --- Quote Start --- In particular, you should not call printf() from within an ISR without careful consideration. If stdout is mapped to a device driver that uses interrupts for proper operation, the printf() call can deadlock the system waiting for an interrupt that never occurs because interrupts are disabled. You can use printf() from within ISRs safely, but only if the device driver does not use nterrupts.[/b] --- Quote End --- How to determine if the UART is using interrupt or not? Is is simply a matter of assigning a level in the SOPC or not? Does it make a difference what ticks are selected in the library settings (reduced device divers, small C library)? What if the UART IRQ is at a high priority IRQ level (smaller number), higher than my irq, say PIO_IRQ, which would be using the printf(""), and in the PIO_IRQ handler make sure that the printf() is encllosed in alt_irq_interruptible(); alt_irq_non_interruptible(); sequence as the follwing code snippet indicates..static void handle_button_interrupts(void* context, alt_u32 id)
{
/* cast the context pointer to an integer. */
int* edge_capture_ptr = (int*) context;
// Read the edge capture register on the button PIO.
*edge_capture_ptr = IORD_ALTERA_AVALON_PIO_EDGE_CAP(BUTTON_PIO_BASE);
/* Write to the edge capture register to reset it. */
IOWR_ALTERA_AVALON_PIO_EDGE_CAP(BUTTON_PIO_BASE, 0);
/* reset interrupt capability for the Button PIO. */
IOWR_ALTERA_AVALON_PIO_IRQ_MASK(BUTTON_PIO_BASE, 0xf);
alt_irq_interruptible(); // Enable higher priority irq levels, in particular the UART serving printf's
printf("pio mask is = %d\n",*edge_capture_ptr );
alt_irq_non_interruptible();
} would that be a workable solution? Its clear that there can be significant overhead in printing. But sometimes this cost is worth the benefit. A side question: can the handle_button_interrupts() routine access global variables? In that case the context parameter could be ignored and edge_capture accessed directly. Hmm, maybe a matter of taste. regards henning