Altera_Forum
Honored Contributor
19 years agouart interrupt program
i debug this program in standard UART,and it run corectly.but when i used this program in my expanded UART,this program isn't go well, it can't receive data.
i checked my hardware and it goes well, i think it is the interrupt's problem. my program is here ,please help me,i did this for a week. /*used in testing uart1 txd and rxd*/# include <stdio.h># include <string.h># include <unistd.h># include "system.h"# include "sys/alt_irq.h"# include "altera_avalon_uart_regs.h"# include "alt_types.h"# include "altera_avalon_pio_regs.h" # define rxbufsize 10 # define rxdatasize 3 unsigned char rxbuf[rxbufsize]; long int rxhead=0; long int rxtail=0; void simplereceive1(void *context,alt_u32 id); unsigned char _getchar(); int main(void) { void* context; int i=0; unsigned char rxdata[rxdatasize]; //int led=0x01; printf("Simple\n"); alt_irq_register(UART1_IRQ,context,simplereceive1);//注册中断 IOWR_ALTERA_AVALON_UART_CONTROL(UART1_BASE,ALTERA_AVALON_UART_CONTROL_RRDY_MSK);//使能串口接收中断 while(1) { usleep(15000); i=0; while(rxtail!=rxhead) { rxdata=_getchar();
iowr_altera_avalon_uart_txdata(uart1_base, rxdata); i++; } /*if (i==rxdatasize && rxdata[0]=='a' && rxdata[1]=='b' && rxdata[2]=='c') IOWR_ALTERA_AVALON_PIO_DATA(LED_GREEN_BASE,led); else led=0; IOWR_ALTERA_AVALON_PIO_DATA(LED_GREEN_BASE,led);*/ } } void simplereceive1(void *context,alt_u32 id)//uart ISR { rxbuf[rxhead]=IORD_ALTERA_AVALON_UART_RXDATA(UART1_BASE);/*串口接 收到新数据,将其放入接收缓冲区数据流尾*/ if ((++rxhead)>(rxbufsize-1)) rxhead=0;/*如果rxhead索引值已到数组 末尾,返回数组头部*/ IOWR_ALTERA_AVALON_UART_STATUS(UART1_BASE, 0);//串口状态寄存器清零 } unsigned char _getchar() //获取数据的函数 { unsigned char ret; ret=rxbuf[rxtail]; if (++rxtail>(rxbufsize-1)) rxtail=0; return(ret); }