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>
45 #inclede <appconfig.h>
49 * Move \a fd file seek position of \a offset bytes
50 * from current position.
51 * This is a generic implementation of seek function, you should redefine
52 * it in your local module.
54 int32_t kfile_seek(struct _KFile *fd, kfile_off_t offset, KSeekMode whence)
65 seek_pos = fd->size - 1;
68 seek_pos = fd->seek_pos;
78 if (seek_pos + offset > fd->size)
84 fd->seek_pos = seek_pos + offset;
85 kprintf("Flash seek to [%lu]\n", fd->seek_pos);
93 * Program memory read/write subtest.
94 * Try to write/read in the same \a f file location \a _size bytes.
95 * \return true if all is ok, false otherwise
96 * \note Restore file position at exit (if no error)
97 * \note Test buffer \a buf must be filled with
98 * the following statement:
103 static bool kfile_rwTest(KFile *f, uint8_t *buf, size_t _size)
105 int32_t size = _size;
108 if (f->write(f, buf, size) != size)
110 f->seek(f, -size, KSM_SEEK_CUR);
113 memset(buf, 0, size);
115 // Read flash in test buffer
116 if (f->read(f, buf, size) != size)
118 f->seek(f, -size, KSM_SEEK_CUR);
121 for (size_t i = 0; i < size; i++)
122 if (buf[i] != (i & 0xff))
129 * Test for program memory read/write.
131 bool kfile_test(uint8_t *buf, size_t _size , uint8_t *save_buf, size_t * save_buf_size)
134 int32_t size = _size;
137 * Part of test buf size that you would write.
138 * This var is useded in test 3 to check fd.write
139 * when write outside size limit. Normaly we want
140 * perform a write until is space to write, otherwise
143 int32_t len = size/2;
146 * Fill in test buffer
148 for (int i = 0; i < size; i++)
149 test_buf[i] = (i & 0xff);
151 kprintf("Generated test string..\n");
156 fd.open(&fd, NULL, 0);
157 kprintf("Opened fd handler..\n");
160 * If necessary, user could save content,
163 if (save_buf != NULL)
165 fd.read(&fd, save_buf, save_buf_size);
166 kprintf("Saved content..form [%lu] to [%lu]\n", fd.seek_pos, fd.seek_pos + save_buf_size);
170 kprintf("Test 1: write from pos 0 to [%lu]\n", fd.size);
175 if (fd.seek(&fd, 0, KSM_SEEK_SET) != 0)
178 kprintf("Seek to [%lu], expected[0]\n", fd.seek_pos);
181 * Test flash read/write to address 0..size
183 if (!Kfile_rwTest(&fd, test_buf, size))
186 kprintf("Test 1: ok!\n");
189 * Restore previous read content
191 if (save_buf != NULL)
193 fd.seek(&fd, 0, KSM_SEEK_SET);
195 if (fd.write(&fd, save_buf, save_buf_size) != size)
198 kprintf("Restore content..form [%lu] to [%lu]\n", fd.seek_pos, fd.seek_pos + save_buf_size);
203 kprintf("Test 2: write from pos [%lu] to [%lu]\n", fd.size/2 , size);
206 * Go to half test size.
208 fd.seek(&fd, (fd.size/ 2), KSM_SEEK_SET);
210 kprintf("Seek to [%lu], expected[%lu]\n", fd.seek_pos, fd.size/2);
213 * If necessary, user could save content,
216 if (save_buf != NULL)
218 fd.read(&fd, save_buf, save_buf_size);
219 fd.seek(&fd, -size, KSM_SEEK_CUR);
220 kprintf("Saved content..form [%lu] to [%lu]\n", fd.seek_pos, fd.seek_pos + save_buf_size);
224 * Test flash read/write to address FLASHEND/2 ... FLASHEND/2 + size
226 if (!Kfile_rwTest(&fd, test_buf, size))
229 kprintf("Test 2: ok!\n");
232 * Restore previous read content
234 if (save_buf != NULL)
236 fd.seek(&fd, -size, KSM_SEEK_CUR);
238 if (fd.write(&fd, save_buf, save_buf_size) != size)
241 kprintf("Restore content..form [%lu] to [%lu]\n", fd.seek_pos, fd.seek_pos + save_buf_size);
247 kprintf("Test 3: write outside of fd.size limit [%lu]\n", fd.size);
250 * Go to the Flash end
252 fd.seek(&fd, -len, KSM_SEEK_END);
253 kprintf("Seek to [%lu], expected[%lu]\n", fd.seek_pos, fd.size - len);
256 * If necessary, user could save content,
259 if (save_buf != NULL)
261 ASSERT(len > save_buf_size);
263 fd.read(&fd, save_buf, len);
264 fd.seek(&fd, -len, KSM_SEEK_CUR);
265 kprintf("Saved content..form [%lu] to [%lu]\n", fd.seek_pos, fd.seek_pos + len);
269 * Test flash read/write to address (FLASHEND - size) ... FLASHEND
271 if (!Kfile_rwTest(&fd, test_buf, size))
274 kprintf("Test 3: ok !\n");
277 * Restore previous read content
279 if (save_buf != NULL)
281 fd.seek(&fd, -len, KSM_SEEK_END);
283 if (fd.write(&fd, save_buf, len) != len)
286 kprintf("Restore content..form [%lu] to [%lu]\n", fd.seek_pos, fd.seek_pos + len);
299 #endif /* CONFIG_TEST */