NiosII中Flash的使用(转)
NiosII中Flash的使用
?????? 在嵌入式系統中,Flash是最常用組件之一。許多使用過flash的朋友都了解,Flash的特點是“讀來容易寫來難”。通常,可以直接讀出Flash的內容;但如果要寫入數據,就要發送一長串命令,比如像:555 ,AA,2AA,55,555,A0 ,PA,PD 就表示對PA地址寫入數據PD,實際情況還要復雜一點,因為通常還要包含許多查詢操作。
??? 哎呀,這真是好繁瑣呀,有沒有省力的方法呢?現在好了,NiosII的開發環境提供了對符合CFI標準的Flash的支持,使用幾個簡單的函數,就可以操作Flash,真是方便了許多。
?? 在這里,我粗略得介紹一下nios 中flash的使用;更加詳細的幫助信息請您參考Altera公司提供的文檔。如果本文能對您有一點點幫助,我都會感到非常高興。
一. 準備工作:
???在使用Flash之前,有幾個準備工作要做:
? 1。開發板上至少要有一片符合CFI標準的Flash。
? 2。設計好對應目標板的Flash編程器(Flash Programmer)。
??? 下面我以本站設計的開發板ezNiosDKC6B為目標板,來介紹如何使用Flash,你可以舉一反三,在自己的系統中使用Flash.
二. 為SOPC系統中添加Flash接口:
1。?? 雙擊在Memory中的Flash Memory(Common Flash Interface),即可為系統添加Flash接口。
2。 對于ezNiosDK的用戶,Address Width可以選擇 20 Bits,Data Width 可以選擇 16 Bits,這樣總的容量是2M Bytes
???
3。Timing選項卡中,可以如下添寫:
???
?? 完成后,選擇Finish,即可為系統添加Flash接口。
4。增加三態總線橋。Flash 接口必須通過三態總線橋接到實際的芯片上。雙擊Bridges下的Avalon Tri-State Bridge,請按照下圖設置,為系統增加三態總線橋。
???
5。為Flash接口分配引腳。請參考我之前的文章:Step By Step創建標準(Standard) niosII系統
??? 注意,如果Data Width是16Bits,那么tri_state_bridge_0_address[0]不必接到Flash上,tri_state_bridge_0_address[1]對應Flash的A[0],tri_state_bridge_0_address[2]對應Flash的A[1],以此類推。
三. 在Nios IDE中使用Flash編程器:
3-1。配置Component/Kit Library Search Path。目的是在系統中增加本站設計的Flash編程器目錄,請把光盤CD1上的/Example/ezNiosC6 拷貝到您的電腦的硬盤上。比如,我把他放在h:/DB2005/project/niosDK/CD/CD1/Example/中,然后在Altera SOPC Builder中,選擇File -> SOPC Builder Setup,增加如下搜索路徑:+h:/DB2005/project/niosDK/CD/CD1/Example/ezNiosC6
ezNiosC3的用戶如下操作:請把光盤CD1上的/Example/ezNiosC3 拷貝到您的電腦的硬盤上。比如,我把他放在h:/DB2005/project/niosDK/CD/CD1/Example/中,然后在Altera SOPC Builder中,選擇File -> SOPC Builder Setup,增加如下搜索路徑:+h:/DB2005/project/niosDK/CD/CD1/Example/ezNiosC3
并順便檢察Modelsim Directory是否正確:比如,我的是 g:/w2k/eda/fpga/altera/modeltech_6.0/win32
然后,你需要關閉Altera SOPC Builder,然后再重新通過QuartusII的Tools ->SOPC Builder來開啟SOPC Builder,這樣上述修改才能生效。
???
常見錯誤:有許多朋友往往在設置路徑的時候,犯與下圖類似的錯誤。錯誤在于第一個路徑前面是不需要 + 號的!要去掉第一個路徑前面的 + 號
3-2:使用Flash編程器。
我使用項目:ezC6Be_StdF50_zip_filesystem_0(在CD1ExampleezNiosC6BezC6Be_StdF50softwareezC6Be_StdF50_zip_filesystem_0下,請使用Nios IDE中的File-Import來導入這個項目),來說明如何使用Flash編程器。
??? 首先為系統上電,并連接好下載電纜。
??? 選擇Tools -> Flash Programmer,啟動 Flash 編程器
??? 選擇New,可以看到增加了一個名為 ezC6Be_StdF50_zip_filesystem_0 programmer的flash 編程器
?? 選擇Program software project into flash memory,然后選擇Apply,在選擇Program Flash,就啟動了Flash 編程器!如果順利的話,可以看到如下的提示:
???
如果看到如上提示,OK,小功告成,我們已經可以正常使用Flash編程器啦!
?? 下面我來介紹如何在程序中引用Flash。
使用NiosII提供的系統函數,可以方便得使用Flash,免除了通常操作Flash 的繁瑣,這對用戶來說,真是方便多了。
??? Altera 提供了兩種類型的函數,提供給客戶:Simple Flash Access(簡單的Flash訪問),以及Fine-Grained Flash Access(細粒度Flash訪問)。
??? 一般情況下,我還是推薦使用Fine-Grained Flash Access(細粒度Flash訪問)函數,比 Simple Flash Access也復雜不了多少,但可以避免通常的跨塊擦除問題。因為Flash是按照?? Block組織起來的,通常一次擦除一整個塊。如果寫Flash的地址于Flash塊的組織結構不吻合,比如跨越了Flash塊的邊緣,那么可能會擦除掉其余的數據。比如,即使要寫入1Byte,也要擦除掉4Kbyte,也許這4Kbyte里面還有許多有用的數據,就會被抹掉。
??? 我們下面介紹幾個常用的函數,關于更詳細的內容,請參考Altera提供的文檔。
??? 首先介紹第一步:打開Flash,就像c程序打開硬盤中的數據文件一樣,使用之前要打開Flash.
我們使用alt_flash_open_dev()打開Flash,它返回一個句柄。比如,下面是使用這個函數的片斷:
alt_flash_fd* fd;
fd = alt_flash_open_dev(EXT_FLASH_NAME);
其中,EXT_FLASH_NAME是預先定義的Flash的名字(#define EXT_FLASH_NAME "/dev/ext_flash")
接下來的操作,都是通過該句柄:fd,來訪問Flash的。
??? 讀出Flash使用函數:alt_read_flash,原型如下:
int alt_read_flash( alt_flash_fd* fd,
int offset,
void* dest_addr,
int length )
??? 使用完了,也別忘記關閉該Flash,就象讀寫完硬盤中的數據文件后要關閉一樣。其原型如下:
void alt_flash_close_dev(alt_flash_fd* fd )
??? Fine-Grained Flash Access機制提供了如下幾個函數:alt_get_flash_info(), alt_erase_flash_block(), alt_write_flash_block()。
???alt_get_flash_info()可以提取Flash的信息,比如包含幾個區域,每個區域有幾個塊,每個塊的大小等等。它的原型如下:
??? int alt_get_flash_info( alt_flash_fd* fd,flash_region** info,int* number_of_regions)
比如,如下就是一個調用該函數的程序片斷:
? int ret_code = 0;
? int number_of_regions=0;
? flash_region* regions;
? ret_code = alt_get_flash_info(fd, ®ions, &number_of_regions
?這里涉及到一個結構:flash_region,原型如下:
typedef struct flash_region
{int offset;
int region_size;
int number_of_blocks;
int block_size;
}flash_region;
??擦除一個塊使用函數:alt_flash_fd,函數原型如下:
int alt_erase_flash_block( alt_flash_fd* fd,
int offset,
int length)
??寫入一個塊使用函數:alt_write_flash_block,函數原型如下:
int alt_write_flash_block( alt_flash_fd* fd,
int block_offset,
int data_offset,
const void *data,
int length)
??羅羅嗦嗦說了這么多,下面我們來看一個實際的范例;這個范例是我使用來測試flash的,相信大家可以從中獲益
良多。
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include "alt_types.h"
#include "sys/alt_flash.h"
#include "sys/alt_flash_dev.h"
#define NUM_BYTES_TO_WRITE 512
//#define NUM_BYTES_TO_WRITE 64
int test_programming( alt_flash_fd* fd, int test_offset)
{
int i,j;
alt_u8 data_written[NUM_BYTES_TO_WRITE];
alt_u8 data_read[NUM_BYTES_TO_WRITE];
int ret_code = 0;
int test_length = sizeof(data_written);
for (j=0;j<7;j++)
{
for(i=0;i<sizeof(data_written)/2;i++)
data_written[i] = j*0x15;
for(i=sizeof(data_written)/2;i<sizeof(data_written);i++)
data_written[i] = (j*0x15)+1;
ret_code = alt_write_flash(fd, test_offset, data_written, test_length);
if (!ret_code)
{
ret_code = alt_read_flash(fd, test_offset, data_read, test_length);
if(!ret_code)
{
if (memcmp(data_written, data_read, test_length))
{
printf( "nERROR: compare failed sector offset %#x iteration%#xn",
test_offset, j);
return ret_code;
}
}
}
printf("*");
if (ret_code)
{
printf( "nERROR: function alt_write_flash failed. ret_code %dn",
ret_code);
return ret_code;
}
}
return ret_code;
}
int test_flash_erase( alt_flash_fd* fd)
{
int ret_code = 0;
int number_of_regions=0;
flash_region* regions;
int i,j,k;
int test_offset;
int test_length;
alt_u8 read_data[200] ;
ret_code = alt_get_flash_info(fd, ®ions, &number_of_regions);
if (ret_code)
{
printf( "nERROR: function alt_get_flash_info failed. ret_code %dn",
ret_code);
}
printf("nThis is AMD29LV160DBn");
printf("Flash name %sn",fd->name);
printf("This flash has %d erase regionsn", number_of_regions);
for (i=0;i<number_of_regions;i++)
{
printf("Start 0x%8x End 0x%8x Number of Blocks %3d Block Size 0x%8xn",
(regions+i)->offset,
(regions+i)->region_size+(regions+i)->offset,
(regions+i)->number_of_blocks,
(regions+i)->block_size);
}
for(j=0;j<number_of_regions;j++)
{
for(i=0;i<((regions+j)->number_of_blocks);i++)
{ test_offset = (regions+j)->offset + i*(regions+j)->block_size;
test_length = (regions+j)->block_size;
printf(" ---Testing flash block erase...@ 0x%8x length= 0x%8x n",test_offset,test_length);
ret_code = alt_erase_flash_block(fd, (regions+j)->offset + i*(regions+j)->block_size, (regions+j)->block_size);
if (ret_code)
{
printf( "nERROR: function alt_erase_flash_block failed. ret_code %dn", ret_code);
break;
}
else
{
ret_code = alt_read_flash(fd, test_offset, read_data, 100);
for (k=0;k<100;k++)
{
if (read_data[k] != 0xff)
{
printf("nERROR: erase compare failed. %d %#xn", k, read_data[k]);
break;
}
}
}
}//@for(i=1
}//@for(j=1)
printf(" passed.n");
return ret_code;
}
int test_flash_write( alt_flash_fd* fd)
{
int ret_code = 0;
int number_of_regions=0;
flash_region* regions;
int i,j,k;
int test_offset;
int test_length;
alt_u8 read_data[200] ;
alt_u8 write_data[200];
ret_code = alt_get_flash_info(fd, ®ions, &number_of_regions);
if (ret_code)
{
printf( "nERROR: function alt_get_flash_info failed. ret_code %dn",
ret_code);
}
printf("nThis is AMD29LV160DBn");
printf("Flash name %sn",fd->name);
printf("This flash has %d erase regionsn", number_of_regions);
for (i=0;i<number_of_regions;i++)
{
printf("Start 0x%8x End 0x%8x Number of Blocks %3d Block Size 0x%8xn",
(regions+i)->offset,
(regions+i)->region_size+(regions+i)->offset,
(regions+i)->number_of_blocks,
(regions+i)->block_size);
}
for(j=0;j<number_of_regions;j++)
{
for(i=0;i<((regions+j)->number_of_blocks);i++)
{ test_offset = (regions+j)->offset + i*(regions+j)->block_size;
test_length = (regions+j)->block_size;
printf(" ---Testing flash block erase...@ 0x%8x length= 0x%8x n",test_offset,test_length);
ret_code = alt_erase_flash_block(fd, (regions+j)->offset + i*(regions+j)->block_size, (regions+j)->block_size);
if (ret_code)
{
printf( "nERROR: function alt_erase_flash_block failed. ret_code %dn", ret_code);
break;
}
else
{
ret_code = alt_read_flash(fd, test_offset, read_data, 100);
for (k=0;k<100;k++)
{
if (read_data[k] != 0xff)
{
printf("nERROR: erase compare failed. %d %#xn", k, read_data[k]);
break;
}
}//@for (k=0)
printf(" -----------Now Testing flash block write...@ 0x%8x n",test_offset);
for(k=0;k<100;k++)
write_data[k] = k;
ret_code = alt_write_flash_block( fd, (regions+j)->offset + i*(regions+j)->block_size,
test_offset + i, write_data, 100);
if (ret_code)
{
printf( "nERROR: function aXlt_write_flash_block failed. ret_code %dn", ret_code);
break;
}
else
{ ret_code = alt_read_flash(fd, test_offset + i, read_data, 100);
for (k=0;k<100;k++)
{
if (read_data[k] != write_data[k])
{printf( "nERROR: compare failed, expected %#x read %#xn",write_data[i], read_data[i]);
break;
}
}
}//@ else
}
}//@for(i=1
}//@for(j=1)
printf(" passed.n");
return ret_code;
}
int test_get_info( alt_flash_fd* fd)
{
int ret_code = 0;
int number_of_regions=0;
flash_region* regions;
int i;
ret_code = alt_get_flash_info(fd, ®ions, &number_of_regions);
if (ret_code)
{
printf( "nERROR: function alt_get_flash_info failed. ret_code %dn",
ret_code);
}
if (0)
{
}
else
{
printf("nThis is not the standard reference designn");
printf("Flash name %sn",fd->name);
printf("This flash has %d erase regionsn", number_of_regions);
for (i=0;i<number_of_regions;i++)
{
printf("Start 0x%8x End 0x%8x Number of Blocks %3d Block Size 0x%8xn",
(regions+i)->offset,
(regions+i)->region_size+(regions+i)->offset,
(regions+i)->number_of_blocks,
(regions+i)->block_size);
}
}
return ret_code;
}
int main (void)
{
int ret_code;
int test_offset;
alt_flash_fd* fd;
alt_u8 write_data[100];
alt_u8 read_data[100];
int i,j;
fd = alt_flash_open_dev(EXT_FLASH_NAME);
if (fd)
{
printf("nnn<----> Running Flash Tests <---->n");
printf("This will take approximately 1 minutenn");
printf("-Testing flash info retrieval...");
ret_code = test_get_info(fd);
if (ret_code)
{
printf( "nERROR: function test_get_info failed. ret_code %dn",
ret_code);
goto finished;
}
printf(" passed.n");
printf("-Testing flash block erase,write...n");
ret_code = test_flash_write(fd);
if (ret_code)
{
printf( "nERROR: test_flash_erase failed. ret_code %dn",
ret_code);
goto finished;
}
printf(" passed.n");
printf("-Testing flash write...n");
test_offset = 0x0;
printf("n test_offset=0x%8x ",test_offset);
ret_code = test_programming(fd, test_offset);
if (ret_code)
goto finished;
printf(" passed.n");
test_offset = 0x4000;
printf("n test_offset=0x%8x ",test_offset);
ret_code = test_programming(fd, test_offset);
if (ret_code)
goto finished;
printf(" passed.n");
test_offset = 0x6000;
printf("n test_offset=0x%8x ",test_offset);
ret_code = test_programming(fd, test_offset);
if (ret_code)
goto finished;
printf(" passed.n");
test_offset = 0x8000;
printf("n test_offset=0x%8x ",test_offset);
ret_code = test_programming(fd, test_offset);
if (ret_code)
goto finished;
printf(" passed.n");
for(j=1;j<=31;j++)
{test_offset = 0x10000*j;
printf("n test_offset=0x%8x ",test_offset);
ret_code = test_programming(fd, test_offset);
if (ret_code)
goto finished;
}
printf(" passed.n");
printf(" 0x10ff00: ");
test_offset = 0x10ff00;
ret_code = test_programming(fd, test_offset);
if (ret_code)
goto finished;
printf(" passed.n");
printf(" 0x100100: ");
test_offset = 0x100100;
ret_code = test_programming(fd, test_offset);
if (ret_code)
goto finished;
printf(" passed.n");
printf("-Testing flash block erase...n");
ret_code = test_flash_erase(fd);
if (ret_code)
{
printf( "nERROR: test_flash_erase failed. ret_code %dn",
ret_code);
goto finished;
}
printf(" passed.n");
test_offset = 0x10000;
printf(" ---Testing flash block write...@ 0x%8x n",test_offset);
for(i=0;i<100;i++)
write_data[i] = i;
ret_code = alt_write_flash_block( fd, 0x10000,
test_offset, write_data,
100);
if (ret_code)
{
printf( "nERROR: function alt_write_flash_block failed. ret_code %dn",
ret_code);
goto finished;
}
else
{
ret_code = alt_read_flash(fd, test_offset, read_data, 100);
for (i=0;i<100;i++)
{
if (read_data[i] != write_data[i])
{
printf( "nERROR: compare failed, expected %#x read %#xn",
write_data[i], read_data[i]);
goto finished;
}
}
}
printf(" passed.n");
test_offset = 0x10010;
printf("-Testing unaligned writes.....");
alt_erase_flash_block(fd, 0x10000, 0x100000);
ret_code = alt_write_flash_block( fd, 0x10000,
test_offset, write_data,
100);
if (ret_code)
{
printf( "nERROR: function alt_write_flash_block failed. ret_code %dn",
ret_code);
goto finished;
}
else
{
ret_code = alt_read_flash(fd, test_offset, read_data, 100);
for (i=0;i<100;i++)
{
if (read_data[i] != write_data[i])
{
printf( "nERROR: compare failed, expected %#x read %#xn",
write_data[i], read_data[i]);
goto finished;
}
}
}
printf(" passed.n");
printf("-Testing flash block erase...n");
ret_code = test_flash_erase(fd);
if (ret_code)
{
printf( "nERROR: test_flash_erase failed. ret_code %dn",
ret_code);
goto finished;
}
printf(" passed.n");
printf("All Tests Passed!n");
}
else
{
printf("Can't open the flash devicen");
}
finished:
alt_flash_close_dev(fd);
printf("Exiting Flash Testsn");
return 0;
}
轉載于:https://www.cnblogs.com/just4fun/archive/2011/12/30/2307614.html
總結
以上是生活随笔為你收集整理的NiosII中Flash的使用(转)的全部內容,希望文章能夠幫你解決所遇到的問題。