📄 nios_test.c
字号:
#include <stdio.h>
#include <string.h>
#include "io.h"
#include "sys/alt_flash.h"
#include "system.h"
void test_Uart();
void test_sram();
void test_Flash();
//////////////////////////////////////////////////////////////////////////
// global variables
//////////////////////////////////////////////////////////////////////////
int foo __attribute__ ((section (".sram.rwdata"))) = 0;
unsigned char led=0x05;
int main()
{
unsigned long i = 8;
while(1)
{
while(--i)
{
usleep(500000);
IOWR(LED_BASE,1,0xFF);
IOWR(LED_BASE,0,led);
printf("\nwww.icwin.net Hello from Nios II!");
usleep(800000);
led =~led;
IOWR(LED_BASE,0,led);
}
//test_Uart(); //test is OK
test_Flash();
usleep(800000);
led = ~led;
IOWR(LED_BASE,0,led);
i = 8;
}
return 0;
}
void test_Uart()
{
char buffer[100] ;
FILE *uart= NULL;
if( uart = fopen(UART_RS232_NAME,"rb+") )
{
while(1)
{
fprintf(uart , "\n please input:%s" );
printf("\n please input:%s" );
fgets(buffer,100,uart);
printf("\n you have inputed! %s" ,buffer);
fprintf(uart,"\nyou have inputed:%s",buffer);
IOWR(LED_BASE,0,buffer[0]);
usleep(1000000);
if( (buffer[0]=='B')&& (buffer[1]=='r') &&(buffer[2]=='e')&&(buffer[3]=='a')&&(buffer[4]=='k') )
break;
}
fclose(uart);
printf("\n uart has be closed! %s" ,buffer);
}
else
{
printf("\n can't open the uart");
}
}
void test_Sram()
{
while(1)
{
unsigned long i = 0;
unsigned char data = 0;
unsigned long offset = 0x3FFFF;
unsigned char *p = SRAM_BASE+offset;
while(i < 0x20)
{
data =*p;
printf("%x ",data);
*p = 0x55;
data =*(unsigned char*)p;
printf(" %x\n",data);
i++;
p++;
}
printf("\n Read Over");
break;
//print_content_mem(SRAM_NAME,SRAM_BASE,0xFFFF);
}
}
void test_Flash()
{
#define BUF_SIZE 1024
alt_flash_fd* fd;
volatile unsigned char * p = NULL;
int i ;
int ret_code;
char source[BUF_SIZE];
char dest[BUF_SIZE];
/* Initialize the source buffer to all 0xAA */
printf("\n Enter test_flash()");
memset(source, 0xAA, BUF_SIZE);
fd = alt_flash_open_dev(FLASH_NAME);
printf("\n open flash fd=%x",fd);
usleep(2000000);
p = (unsigned char *)(FLASH_BASE+0x10);
for(i = 0 ; i<0x20;i++)
{
printf(" %02x" , *p++);
}
printf("\n\n open ");
p = (unsigned char *)(FLASH_BASE+0x20);
for(i = 0 ; i<0x20;i++)
{
printf(" %02x" , *p++);
}
if (fd)
{
printf("\n open flash successfull fd=%x",fd);
//ret_code = alt_write_flash(fd, 0, source, BUF_SIZE);
if (1||!ret_code)
{
int index=0;
printf("\n write flash successfull : ret_code=%x\n",ret_code);
ret_code = alt_read_flash(fd, 0, dest, BUF_SIZE);
printf("\n read flash successfull : ret_code=%x\n",ret_code);
if (1|| !ret_code)
{
/*
* Success.
* At this point, the flash is be all 0xa and we
* should have read that all back into dest
*/
for(index = 0 ; index < BUF_SIZE ; index ++)
{
printf(" %x" ,dest[index] );
usleep(800000);
led = ~led;
IOWR(LED_BASE,0,led);
}
}
}
alt_flash_close_dev(fd);
}
else
{
printf("\nCan’t open flash device,fd=%x",fd);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -