1 /* Hay, the copyright is usefull for something! */
3 static char copyright[] =
4 "//=========================================================================="
8 "// Test flash operations for the synth target synth flash driver"
10 "//=========================================================================="
11 "//####ECOSGPLCOPYRIGHTBEGIN####"
12 "// -------------------------------------------"
13 "// This file is part of eCos, the Embedded Configurable Operating System."
14 "// Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc."
16 "// eCos is free software; you can redistribute it and/or modify it under"
17 "// the terms of the GNU General Public License as published by the Free"
18 "// Software Foundation; either version 2 or (at your option) any later version."
20 "// eCos is distributed in the hope that it will be useful, but WITHOUT ANY"
21 "// WARRANTY; without even the implied warranty of MERCHANTABILITY or"
22 "// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License"
23 "// for more details."
25 "// You should have received a copy of the GNU General Public License along"
26 "// with eCos; if not, write to the Free Software Foundation, Inc.,"
27 "// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA."
29 "// As a special exception, if other files instantiate templates or use macros"
30 "// or inline functions from this file, or you compile this file and link it"
31 "// with other works to produce a work based on this file, this file does not"
32 "// by itself cause the resulting work to be covered by the GNU General Public"
33 "// License. However the source code for this file must still be made available"
34 "// in accordance with section (3) of the GNU General Public License."
36 "// This exception does not invalidate any other reasons why a work based on"
37 "// this file might be covered by the GNU General Public License."
39 "// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc."
40 "// at http://sources.redhat.com/ecos/ecos-license/"
41 "// -------------------------------------------"
42 "//####ECOSGPLCOPYRIGHTEND####"
43 "//=========================================================================="
44 "//#####DESCRIPTIONBEGIN####"
46 "// Author(s): andrew.lunn@ascom.ch"
47 "// Contributors: andrew.lunn"
49 "// Purpose: Test a flash driver"
50 "// Description: Try out a number of flash operations and make sure"
51 "// what is in the flash is what we expeect."
53 "//####DESCRIPTIONEND####"
55 "//=========================================================================="
58 #include <cyg/io/flash.h>
59 #include <cyg/infra/testcase.h>
60 #include <cyg/infra/diag.h>
64 #ifndef CYGINT_ISO_STRING_STRFUNCS
65 # define NA_MSG "Need string functions for test"
69 void cyg_user_start(void)
72 CYG_TEST_NA( NA_MSG );
76 //==========================================================================
79 void cyg_user_start(void)
83 void *flash_start, *flash_end;
84 int block_size, blocks;
90 ret=flash_init((_printf *)diag_printf);
92 CYG_TEST_PASS_FAIL((ret == FLASH_ERR_OK),"flash_init");
94 flash_dev_query(data);
95 CYG_TEST_PASS_FAIL(!strncmp(data,"Linux Synthetic Flash",sizeof(data)),
98 ret = flash_get_limits(NULL,&flash_start,&flash_end);
99 CYG_TEST_PASS_FAIL((ret == FLASH_ERR_OK),"flash_get_limits");
101 ret = flash_get_block_info(&block_size, &blocks);
102 CYG_TEST_PASS_FAIL((ret == FLASH_ERR_OK),"flash_get_block_info");
104 /* Erase the whole flash. Not recommended on real hardware since this
105 will probably erase the bootloader etc!!! */
106 ret=flash_erase(flash_start,block_size * blocks,NULL);
107 CYG_TEST_PASS_FAIL((ret == FLASH_ERR_OK),"flash_erase1");
109 /* check that its actually been erased, and test the mmap area */
110 for (ptr=flash_start,ret=0; ptr < (unsigned char *)flash_end; ptr++) {
116 CYG_TEST_PASS_FAIL((ret == 0),"flash empty check");
118 ret = flash_program(flash_start,©right,sizeof(copyright),NULL);
119 CYG_TEST_PASS_FAIL((ret == FLASH_ERR_OK),"flash_program1");
121 /* Check the contents made it into the flash */
122 CYG_TEST_PASS_FAIL(!strncmp(flash_start,copyright,sizeof(copyright)),
123 "flash program contents");
125 /* .. and check nothing else changed */
126 for (ptr=(unsigned char *)flash_start+sizeof(copyright),ret=0;
127 ptr < (unsigned char *)flash_end; ptr++) {
133 CYG_TEST_PASS_FAIL((ret == 0),"flash program overrun check");
135 /* Program over a block boundary */
136 prog_start = (char *)flash_start + block_size - sizeof(copyright)/2;
137 ret = flash_program(prog_start,©right,sizeof(copyright),NULL);
138 CYG_TEST_PASS_FAIL((ret == FLASH_ERR_OK),"flash_program2");
140 /* Check the first version is still OK */
141 CYG_TEST_PASS_FAIL(!strncmp(flash_start,copyright,sizeof(copyright)),
142 "Original contents");
144 CYG_TEST_PASS_FAIL(!strncmp(prog_start,copyright,sizeof(copyright)),
145 "New program contents");
147 /* Check the bit in between is still erased */
148 for (ptr=(unsigned char *)flash_start+sizeof(copyright),ret=0;
149 ptr < (unsigned char *)prog_start; ptr++) {
154 CYG_TEST_PASS_FAIL((ret == 0),"flash erase check1");
156 /* Erase the second block and make sure the first is not erased */
157 ret=flash_erase((void *)((unsigned)flash_start+block_size),
159 CYG_TEST_PASS_FAIL((ret == FLASH_ERR_OK),"flash_erase2");
161 /* Check the erase worked */
162 for (ptr=(unsigned char *)flash_start+block_size,ret=0;
163 ptr < (unsigned char *)flash_start+block_size*2; ptr++) {
169 CYG_TEST_PASS_FAIL((ret == 0), "flash erase check2");
171 /* Lastly check the first half of the copyright message is still there */
172 CYG_TEST_PASS_FAIL(!strncmp(prog_start,copyright,sizeof(copyright)/2),
176 /* This test it fatal! Its not run by default!
177 Check the flash is read only, by trying to write to it. We expect
178 to get an exception */
180 *(char *)flash_start = 'a';
183 CYG_TEST_PASS_FINISH("flash1");
186 #endif /* ifndef NA_MSG */