📄 main.c
字号:
#include <stdio.h>
#include <string.h>
#include <system.h>
#include <sys/alt_irq.h>
//#include "sys/alt_dev.h"
//#include <fcntl.h>
#include "altera_avalon_pio_regs.h"
#include "altera_avalon_uart_regs.h"
#include "altera_avalon_timer_regs.h"
typedef volatile struct
{
char ms[10];
char mr[10];
int ls;
int lr;
int ok;
} uart_mess;
static void uart_manager(void* context, alt_u32 id)
{
alt_u32 status;
int ls;
int lr;
uart_mess* mess_val = (uart_mess *) context;
status=IORD_ALTERA_AVALON_UART_STATUS(UART_0_BASE);//save mark
//IOWR_ALTERA_AVALON_UART_STATUS(UART_0_BASE, 0);//clear mark
if (status & ALTERA_AVALON_UART_STATUS_TRDY_MSK)//send
{
ls=mess_val->ls;
IOWR_ALTERA_AVALON_UART_TXDATA(UART_0_BASE,mess_val->ms[ls]);
mess_val->ls=ls-1;
status=IORD_ALTERA_AVALON_UART_STATUS(UART_0_BASE);
if (ls<0)
{}
else{
IOWR_ALTERA_AVALON_UART_STATUS(UART_0_BASE, 0);
IOWR_ALTERA_AVALON_UART_TXDATA(UART_0_BASE,mess_val->ms[ls]);
mess_val->ls=ls-1;
status=IORD_ALTERA_AVALON_UART_STATUS(UART_0_BASE);
}
}
if (status & ALTERA_AVALON_UART_STATUS_RRDY_MSK)//receive
{
lr=mess_val->lr;
if (lr>9)
{
}
else
{
mess_val->mr[lr]=(char)IORD_ALTERA_AVALON_UART_RXDATA(UART_0_BASE);
mess_val->lr=lr+1;
status=IORD_ALTERA_AVALON_UART_STATUS(UART_0_BASE);
}
}
}
static void uart_irq_init(uart_mess* mess)
{
mess->ls=-1;
mess->lr=-1;
mess->ok=0;
IOWR_ALTERA_AVALON_UART_CONTROL(UART_0_BASE, 0xffff);
alt_irq_register( UART_0_IRQ,(void *)mess , uart_manager );
}
int main ()
{
//char m[10];
int a;
uart_mess mess;
uart_irq_init(&mess);
printf("waiting");
a=IORD_ALTERA_AVALON_UART_STATUS(UART_0_BASE);
IOWR_ALTERA_AVALON_UART_TXDATA(UART_0_BASE,0x60);
while(1)
{
if (mess.ls<0)
{
strcpy((char*)mess.ms,"SNED OK!\0");
//printf("send %s",mess.ms);
mess.ls=8;
}
if (mess.lr>9)
{
printf("\nreceive %s \n",mess.mr);
strcpy((char*)mess.mr,"\0");
mess.lr=0;
}
if (a & ALTERA_AVALON_UART_STATUS_TRDY_MSK)//send
IOWR_ALTERA_AVALON_UART_TXDATA(UART_0_BASE,0x60);
else
a=1;
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -