NIOSII中的flash基本测试
altera的nios对flash的操作提供了一系列的函数,使用起来非常方便。具体的讲解参见《NIOSII的那些事》。本文仅对测试程序中出现的一些东西做简单的记录。使用的是DE2-70开发板,板载8MBflash S29GL064A。
整片flash可以分为若干个region,每个region分为若干个block,由于flash本身的性质,如果要擦除位于某一个block中的某个地址(哪怕仅仅是一个),整个block的内容都会被擦除(成1)。
1 #include "sys/alt_flash.h" 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <unistd.h> 5 6 #define BUF_SIZE 100 7 #define FLASH_OFFSET 0x5000 8 9 int main() 10 { 11 printf("Fuck Nios II!\n"); 12 13 int i; 14 alt_flash_fd* fd; 15 flash_region* regions; 16 int number_of_regions; 17 int ret_code = 0x0; 18 unsigned char source[BUF_SIZE]; 19 unsigned char dest[BUF_SIZE]; 20 21 unsigned char block_dest[0x2000]; 22 23 for(i=0;i<100;i++){ 24 source[i] = i; 25 } 26 fd = alt_flash_open_dev("/dev/cfi_flash"); 27 printf("flash opening...\n"); //Debug 28 //fd = alt_flash_open_dev(“/dev/epcs_flash”); //EPCSX 29 if (fd){ 30 printf("flash opened\n"); //Debug 31 printf("fd is %d\n",fd); //Debug 32 ret_code = alt_get_flash_info(fd, ®ions, &number_of_regions); 33 for(i=0;i<number_of_regions;i++){ 34 printf("Start 0x%8x End 0x%8x Number of Blocks %3d Block Size 0x%8x \n", 35 (regions+i)->offset, 36 (regions+i)->region_size+(regions+i)->offset, 37 (regions+i)->number_of_blocks, 38 (regions+i)->block_size); 39 } 40 alt_erase_flash_block(fd, FLASH_OFFSET, BUF_SIZE); 41 42 /* ////////////////////////////////////////////////////////// 43 ///// the entire block is erased to 1 ////// 44 ////////////////////////////////////////////////////////// 45 ret_code = alt_read_flash(fd,0x4000,block_dest,0x2000);/// 46 if(ret_code != 0){ // 47 printf("Can't read flash device\n"); // 48 exit(-1); // 49 } // 50 else{ // 51 printf("Read Flash Device Successfully.\n"); // 52 } // 53 // Print the destination data // 54 for(i=0;i<0x2000;i++){ // 55 printf("OFFSET_Address %4d is %d\n",i,block_dest[i]); 56 } // 57 //////////////////////////////////////////////////////////*/ 58 59 ret_code = alt_read_flash(fd,FLASH_OFFSET,dest,BUF_SIZE); 60 if(ret_code != 0){ 61 printf("Can't read flash device\n"); 62 exit(-1); 63 } 64 else{ 65 printf("Read Flash Device Successfully.\n"); 66 } 67 // Print the destination data 68 for(i=0;i<100;i++){ 69 printf("OFFSET_Address %2d is %d\n",i,dest[i]); 70 } 71 72 ret_code = alt_write_flash(fd,FLASH_OFFSET,source,BUF_SIZE); 73 if(ret_code != 0){ 74 printf("Can't write flash device\n"); 75 exit(-1); 76 } 77 else{ 78 printf("Write Flash Device Successfully.\n"); 79 } 80 81 ret_code = alt_read_flash(fd,FLASH_OFFSET,dest,BUF_SIZE); 82 if(ret_code != 0){ 83 printf("Can't read flash device\n"); 84 exit(-1); 85 } 86 else{ 87 printf("Read Flash Device Successfully.\n"); 88 } 89 90 for(i=0;i<100;i++){ 91 printf("OFFSET_Address %2d is %d\n",i,dest[i]); 92 } 93 } 94 alt_flash_close_dev(fd); 95 printf("flash closed \n"); 96 }
代码注释很清楚,需说明的是第一行中include "sys/alt_flash.h",这个文件位于D:\altera\91\nios2eds\components\altera_hal\HAL\inc\sys路径。
所有的基本flash操作函数:
alt_flash_open_dev(),
alt_write_flash(),
alt_read_flash(),
alt_flash_close_dev(),
alt_get_flash_info()
alt_erase_flash_block()
alt_write_flash_block()
都在这里声明了。所以必须引用上。