Altera_Forum
Honored Contributor
15 years agoprintf crashes SW with ISR
Hi there all!
I've been trying to debug this code for a couple of days now but I'm still stuck. I've been looking for similar threads here and read most if it. I've also read "User guide embedded peripherals" and the "Software developer's handbook". The program is running without uCos and on an FPGA configuration with a UART configured as IRQ-level 1. When running the code below: I only get "1" in a Terminal software. The program crashes after the serial_init() call if I have the second printf("2"). If I remove printf("2"), the software proceeds to the while(1) loop since I can see my led is flashing. Any help is very welcome.
# include <stdio.h>
# include "system.h"
# include "altera_avalon_pio_regs.h"
# include "sys/alt_irq.h"
# include "alt_types.h"
# include "altera_avalon_uart_regs.h"
# include "altera_avalon_uart.h"
void serial_irq_0(void*, alt_u32);
void serial_init(unsigned int);
void serial_init(unsigned int baud)
{
unsigned int i;
/* inhibit all IRQ sources */
IOWR(UART_BASE, 3, 0x00);
/* set Baud rate */
IOWR(UART_BASE, 4, baud);
/* flush any characters sitting in the holding register */
i = IORD(UART_BASE, 0);
i = IORD(UART_BASE, 0);
/* reset most of the status register bits */
IOWR(UART_BASE, 2, 0x00);
/* install IRQ service routine */
alt_irq_register(UART_IRQ, 0, serial_irq_0);
/* enable irq for Rx. */
IOWR(UART_BASE, 3, 0x0080);
}
static void serial_irq_0(void* context, alt_u32 id)
{
unsigned int stat, chr, temp, counter, counter_max;
counter_max = 50000;
/* get serial status */
stat = IORD(UART_BASE, 2);
/* character Rx */
if (stat & 0x0080) {
chr = IORD(UART_BASE, 0);
}
// Clear the status register
IOWR(UART_BASE,2,0x0000);
}
int main()
{
int counter_max = 50000;
int counter = 0;
printf("1");
serial_init(115200);
printf("2");
while(1){
counter=0;
while(counter<counter_max){
counter++;
IOWR_ALTERA_AVALON_PIO_DATA(LED_BASE, 0x1);
}
counter=0;
while(counter<counter_max){
counter++;
IOWR_ALTERA_AVALON_PIO_DATA(LED_BASE, 0x0);
}
}
}
Regards, mr_embedded