4 * This file is part of BeRTOS.
6 * Bertos is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * As a special exception, you may use this file as part of a free software
21 * library without restriction. Specifically, if other files instantiate
22 * templates or use macros or inline functions from this file, or you compile
23 * this file and link it with other files to produce an executable, this
24 * file does not by itself cause the resulting executable to be covered by
25 * the GNU General Public License. This exception does not however
26 * invalidate any other reasons why the executable file might be covered by
27 * the GNU General Public License.
29 * Copyright 2007 Develer S.r.l. (http://www.develer.com/)
33 * \brief Virtual KFile I/O interface.
34 * This module implement a standard fd.seek and a kfile
38 * \author Francesco Sacchi <batt@develer.com>
39 * \author Daniele Basile <asterix@develer.com>
49 * Program memory read/write subtest.
50 * Try to write/read in the same \a f file location \a _size bytes.
51 * \return true if all is ok, false otherwise
52 * \note Restore file position at exit (if no error)
53 * \note Test buffer \a buf must be filled with
54 * the following statement:
59 static bool kfile_rwTest(KFile *f, uint8_t *buf, size_t _size)
64 if (f->write(f, buf, size) != size)
66 f->seek(f, -size, KSM_SEEK_CUR);
71 // Read flash in test buffer
72 if (f->read(f, buf, size) != size)
74 f->seek(f, -size, KSM_SEEK_CUR);
77 for (size_t i = 0; i < size; i++)
78 if (buf[i] != (i & 0xff))
85 * Test for program memory read/write.
87 bool kfile_test(uint8_t *buf, size_t _size , uint8_t *save_buf, size_t * save_buf_size)
93 * Part of test buf size that you would write.
94 * This var is useded in test 3 to check fd.write
95 * when write outside size limit. Normaly we want
96 * perform a write until is space to write, otherwise
102 * Fill in test buffer
104 for (int i = 0; i < size; i++)
105 test_buf[i] = (i & 0xff);
107 kprintf("Generated test string..\n");
112 fd.open(&fd, NULL, 0);
113 kprintf("Opened fd handler..\n");
116 * If necessary, user could save content,
119 if (save_buf != NULL)
121 fd.read(&fd, save_buf, save_buf_size);
122 kprintf("Saved content..form [%lu] to [%lu]\n", fd.seek_pos, fd.seek_pos + save_buf_size);
126 kprintf("Test 1: write from pos 0 to [%lu]\n", fd.size);
131 if (fd.seek(&fd, 0, KSM_SEEK_SET) != 0)
134 kprintf("Seek to [%lu], expected[0]\n", fd.seek_pos);
137 * Test flash read/write to address 0..size
139 if (!Kfile_rwTest(&fd, test_buf, size))
142 kprintf("Test 1: ok!\n");
145 * Restore previous read content
147 if (save_buf != NULL)
149 fd.seek(&fd, 0, KSM_SEEK_SET);
151 if (fd.write(&fd, save_buf, save_buf_size) != size)
154 kprintf("Restore content..form [%lu] to [%lu]\n", fd.seek_pos, fd.seek_pos + save_buf_size);
159 kprintf("Test 2: write from pos [%lu] to [%lu]\n", fd.size/2 , size);
162 * Go to half test size.
164 fd.seek(&fd, (fd.size/ 2), KSM_SEEK_SET);
166 kprintf("Seek to [%lu], expected[%lu]\n", fd.seek_pos, fd.size/2);
169 * If necessary, user could save content,
172 if (save_buf != NULL)
174 fd.read(&fd, save_buf, save_buf_size);
175 fd.seek(&fd, -size, KSM_SEEK_CUR);
176 kprintf("Saved content..form [%lu] to [%lu]\n", fd.seek_pos, fd.seek_pos + save_buf_size);
180 * Test flash read/write to address FLASHEND/2 ... FLASHEND/2 + size
182 if (!Kfile_rwTest(&fd, test_buf, size))
185 kprintf("Test 2: ok!\n");
188 * Restore previous read content
190 if (save_buf != NULL)
192 fd.seek(&fd, -size, KSM_SEEK_CUR);
194 if (fd.write(&fd, save_buf, save_buf_size) != size)
197 kprintf("Restore content..form [%lu] to [%lu]\n", fd.seek_pos, fd.seek_pos + save_buf_size);
203 kprintf("Test 3: write outside of fd.size limit [%lu]\n", fd.size);
206 * Go to the Flash end
208 fd.seek(&fd, -len, KSM_SEEK_END);
209 kprintf("Seek to [%lu], expected[%lu]\n", fd.seek_pos, fd.size - len);
212 * If necessary, user could save content,
215 if (save_buf != NULL)
217 ASSERT(len > save_buf_size);
219 fd.read(&fd, save_buf, len);
220 fd.seek(&fd, -len, KSM_SEEK_CUR);
221 kprintf("Saved content..form [%lu] to [%lu]\n", fd.seek_pos, fd.seek_pos + len);
225 * Test flash read/write to address (FLASHEND - size) ... FLASHEND
227 if (!Kfile_rwTest(&fd, test_buf, size))
230 kprintf("Test 3: ok !\n");
233 * Restore previous read content
235 if (save_buf != NULL)
237 fd.seek(&fd, -len, KSM_SEEK_END);
239 if (fd.write(&fd, save_buf, len) != len)
242 kprintf("Restore content..form [%lu] to [%lu]\n", fd.seek_pos, fd.seek_pos + len);
255 #endif /* CONFIG_TEST */