📄 flash_test.c
字号:
/**************************************************************************
* Copyright ?2004 Altera Corporation, San Jose, California, USA. *
* All rights reserved. All use of this software and documentation is *
* subject to the License Agreement located at the end of this file below.*
*************************************************************************/
/******************************************************************************
*
*
* Description
***************
* This is a test program which demonstrates the use of the flash
* programming API
*
* Requirements
****************
* This is a "Hosted" application. According to the ANSI C standard, hosted
* applications can rely on numerous system-services (including properly-
* initialized device drivers and, in this case, STDOUT).
*
* When this program is compiled, code is added before main(), so that all
* devices are properly-initialized and all system-services (e.g. the <stdio>
* library) are ready-to-use. In this hosted environment, all standard C
* programs will run.
*
* A hosted application (like this example) does not need to concern itself
* with initializing devices. As long as it only calls C Standard Library
* functions, a hosted application can run "as if on a workstation."
*
* An application runs in a hosted environment if it declares the function
* main(), which this application does.
*
* This software example requires a STDOUT component such as a UART or
* JTAG UART, and a CFI flash component. Therefore it can run on the following
* hardware examples:
*
* Stratix:
* - Standard
* - Full Featured
*
* Cyclone:
* - Standard
* - Full Featured
*
* Peripherals Exercised by SW
*******************************
* The example's purpose is to demonstrate the use of the flash API in NiosII.
* It can run on either a HAL or uCOS based system.
*
* The application first opens the flash device with the name EXT_FLASH_NAME,
* which is defined in system.h. The following API functions are then run to
* test the flash interface:
*
* - alt_get_flash_info
* This function queries the flash device and collects various information
* about it. In the example, the results of this query are compared to what
* is expected, and an error is reported in the event of a mismatch.
* - alt_write_flash
* This function writes a specified number of bytes to the flash device.
* In the example, this function is called repeatedly in a loop to write a
* lengthy amount of data.
* - alt_read_flash
* This function reads a specified number of bytes of data from the flash
* device. In the example, alt_read_flash is used to read back and test
* all of the writing routines.
* - alt_erase_flash_block
* This function performs a block erase on the flash device.
* - alt_write_flash_block
* This function writes an erase block of data to the flash device.
*
* During the test, status and error information is passed to the user via
* printf's.
*
* Software Files
******************
* flash_tests.c - Main C file that contains all flash testing code in this
* example.
*
*/
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include "alt_types.h"
#include "sys/alt_flash.h"
#include "system.h"
#include "sys/alt_flash_dev.h"
#define NUM_BYTES_TO_WRITE 512
/*
* test_programming() is called by main to test a range of flash by
* writing incrementing patterns, then reading them back and comparing
* the result
* The start of the range to be tested is defined by test_offset, and
* the size of the range to be tested is defined by NUM_BYTES_TO_WRITE
*/
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);
/*
* 30 iterations takes about 60 seconds
*/
for (j=0;j<30;j++)
{
for(i=0;i<sizeof(data_written)/2;i++)
data_written[i] = j*5;
for(i=sizeof(data_written)/2;i<sizeof(data_written);i++)
data_written[i] = (j*5)+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%#x\n",
test_offset, j);
return ret_code;
}
}
}
printf("*");
if (ret_code)
{
printf( "\nERROR: function alt_write_flash failed. ret_code %d\n",
ret_code);
return ret_code;
}
}
return ret_code;
}
/*
* test_get_info() is called by main to test that the regions, sector
* size, block size and number of blocks can be correctly read from
* the flash
*/
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 %d\n",
ret_code);
}
/*
* If this is the development board check the number of regions etc.
*/
if (!strcmp("/dev/ext_flash_altera", fd->name))
{
if (number_of_regions != 1)
{
printf("\nERROR: number of regions is wrong\n");
ret_code = -EINVAL;
}
else if ( (regions->offset != 0) || (regions->region_size != 0x800000)
|| (regions->block_size != 0x10000) ||
(regions->number_of_blocks != 0x80))
{
printf("\nERROR: region info is wrong\n");
ret_code = -EINVAL;
}
}
else
{
printf("\n\rThis is RCA-CY1C20 NIOSII Board Designed by Red Logic\n\r");
printf("Flash name %s\n\r",fd->name);
printf("This flash has %d erase regions\n\r", 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%8x\n\r",
(regions+i)->offset,
(regions+i)->region_size+(regions+i)->offset,
(regions+i)->number_of_blocks,
(regions+i)->block_size);
}
}
return ret_code;
}
/*
* Run various tests on a small section of the system flash.
*/
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;
fd = alt_flash_open_dev(EXT_FLASH_NAME);
if (fd)
{
printf("\n\r\n\r\n\r<----> Running Flash Tests <---->\n\r");
printf( "\n\r<--> For RCA-CY1C20 NIOSII Board -->\n\r");
printf("-Testing flash info retrieval...");
ret_code = test_get_info(fd);
if (ret_code)
{
printf( "\n\rERROR: function test_get_info failed. ret_code %d\n\r",
ret_code);
goto finished;
}
printf(" passed.\n\r");
printf("-Testing flash write...\n\r");
printf(" 0x10000: ");
test_offset = 0x10000;
ret_code = test_programming(fd, test_offset);
if (ret_code)
goto finished;
printf(" passed.\n\r");
printf(" 0x1ff00: ");
test_offset = 0x1ff00;
ret_code = test_programming(fd, test_offset);
if (ret_code)
goto finished;
printf(" passed.\n\r");
printf(" 0x10100: ");
test_offset = 0x10100;
ret_code = test_programming(fd, test_offset);
if (ret_code)
goto finished;
printf(" passed.\n\r");
printf("-Testing flash block erase...");
ret_code = alt_erase_flash_block(fd, test_offset, 0x10000);
if (ret_code)
{
printf( "\n\rERROR: function alt_erase_flash_block failed. ret_code %d\n\r",
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] != 0xff)
{
printf("\n\rERROR: erase compare failed. %d %#x\n\r", i, read_data[i]);
goto finished;
}
}
}
printf(" passed.\n\r");
printf("-Testing flash block write...");
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( "\n\rERROR: function alt_write_flash_block failed. ret_code %d\n\r",
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( "\n\rERROR: compare failed, expected %#x read %#x\n\r",
write_data[i], read_data[i]);
goto finished;
}
}
}
printf(" passed.\n\r");
test_offset = 0x10003;
printf("-Testing unaligned writes.....");
ret_code = alt_write_flash_block( fd, 0x10000,
test_offset, write_data,
100);
if (ret_code)
{
printf( "\n\rERROR: function alt_write_flash_block failed. ret_code %d\n\r",
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( "\n\rERROR: compare failed, expected %#x read %#x\n\r",
write_data[i], read_data[i]);
goto finished;
}
}
}
printf(" passed.\n\r");
printf("All Tests Passed!\n\r");
}
else
{
printf("Can't open the flash device\n\r");
}
finished:
alt_flash_close_dev(fd);
printf("Exiting Flash Tests\n\r");
return 0;
}
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2004 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* Altera does not recommend, suggest or require that this reference design *
* file be used in conjunction or combination with any other product. *
******************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -