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 Function library for serial Flash memory.
35 * Module provide a kfile interface, that ensure an abstraction
36 * from comunication channel and give a standard interface.
37 * Typicaly this kind of memory use an SPI bus, but you should
38 * use another comunication channel you have defined.
41 * \author Daniele Basile <asterix@develer.com>
45 * We use a spi bus, thus include hardware specific definition.
46 * If you use another channel you must redefine this macros.
50 #include <appconfig.h>
52 #include <cfg/macros.h>
53 #include <cfg/debug.h>
55 #include <drv/timer.h>
56 #include <drv/flash25.h>
58 #include <kern/kfile.h>
61 #include <kern/proc.h>
64 #warning this file was change, but is untest!
67 * Wait until flash memory is ready.
69 static void flash25_waitReady(KFileFlash25 *fd)
77 kfile_putc(FLASH25_RDSR, fd->channel);
78 stat = kfile_getc(fd->channel);
82 if (!(stat & RDY_BIT))
92 * Send a single command to serial flash memory.
94 static void flash25_sendCmd(KFileFlash25 *fd, Flash25Opcode cmd)
98 kfile_putc(cmd, fd->channel);
104 * flash25 init function.
105 * This function init a comunication channel and
106 * try to read manufacturer id of serial memory,
107 * then check if is equal to selected type.
109 static bool flash25_pin_init(KFileFlash25 *fd)
112 uint8_t manufacturer;
118 * Send read id productor opcode on
119 * comunication channel
120 * TODO:controllare se ha senso
122 kfile_putc(FLASH25_RDID, fd->channel);
124 manufacturer = kfile_getc(fd->channel);
125 device_id = kfile_getc(fd->channel);
129 if((FLASH25_MANUFACTURER_ID == manufacturer) &&
130 (FLASH25_DEVICE_ID == device_id))
137 * Reopen a serial memory interface.
139 * For serial memory this function reinit only
140 * the size and seek_pos in kfile stucture.
141 * Return a kfile pointer, after assert check.
143 static KFile * flash25_reopen(struct KFile *_fd)
145 KFileFlash25 *fd = KFILEFLASH25(_fd);
148 fd->fd.size = FLASH25_MEM_SIZE;
150 kprintf("flash25 file opened\n");
155 * Close a serial memory interface.
157 * For serial memory this funtion do nothing,
158 * and return always 0.
160 static int flash25_close(UNUSED_ARG(struct KFile *,fd))
162 kprintf("flash25 file closed\n");
167 * Read \a _buf lenght \a size byte from serial flash memmory.
169 * For read in serial flash memory we
170 * enble cs pin and send one byte of read opcode,
171 * and then 3 byte of address of memory cell we
172 * want to read. After the last byte of address we
173 * can read data from so pin.
175 * \return the number of bytes read.
177 static size_t flash25_read(struct KFile *_fd, void *buf, size_t size)
179 uint8_t *data = (uint8_t *)buf;
181 KFileFlash25 *fd = KFILEFLASH25(_fd);
183 ASSERT(fd->fd.seek_pos + size <= fd->fd.size);
184 size = MIN((uint32_t)size, fd->fd.size - fd->fd.seek_pos);
186 //kprintf("Reading at addr[%lu], size[%d]\n", fd->seek_pos, size);
189 kfile_putc(FLASH25_READ, fd->channel);
193 * Address that we want to read.
195 kfile_putc((fd->fd.seek_pos >> 16) & 0xFF, fd->channel);
196 kfile_putc((fd->fd.seek_pos >> 8) & 0xFF, fd->channel);
197 kfile_putc(fd->fd.seek_pos & 0xFF, fd->channel);
199 kfile_read(fd->channel, data, size);
203 fd->fd.seek_pos += size;
209 * Write \a _buf in serial flash memory
211 * Before to write data into flash we must enable
212 * memory writing. To do this we send a WRE command opcode.
213 * After this command the flash is ready to be write, and so
214 * we send a PROGRAM opcode followed to 3 byte of
215 * address memory, at the end of last address byte
216 * we can send the data.
217 * When we finish to send all data, we disable cs
218 * and flash write received data bytes on its memory.
220 * \note: WARNING: you could write only on erased memory section!
221 * Each write time you could write max a memory page size,
222 * because if you write more than memory page size the
223 * address roll over to first byte of page.
225 * \return the number of bytes write.
227 static size_t flash25_write(struct KFile *_fd, const void *_buf, size_t size)
229 flash25Offset_t offset;
230 flash25Size_t total_write = 0;
231 flash25Size_t wr_len;
232 const uint8_t *data = (const uint8_t *) _buf;
234 KFileFlash25 *fd = KFILEFLASH25(_fd);
236 ASSERT(fd->fd.seek_pos + size <= fd->fd.size);
238 size = MIN((flash25Size_t)size, fd->fd.size - fd->fd.seek_pos);
242 offset = fd->fd.seek_pos % (flash25Size_t)FLASH25_PAGE_SIZE;
243 wr_len = MIN((flash25Size_t)size, FLASH25_PAGE_SIZE - (flash25Size_t)offset);
245 kprintf("[seek_pos-<%lu>, offset-<%d>]\n", fd->fd.seek_pos, offset);
248 * We check serial flash memory state, and wait until ready-flag
251 flash25_waitReady(fd);
255 * We could write only data not more long than one
258 * To write on serial flash memory we must first
259 * enable write with a WREN opcode command, before
260 * the PROGRAM opcode.
262 * \note: the same byte cannot be reprogrammed without
263 * erasing the whole sector first.
265 flash25_sendCmd(fd, FLASH25_WREN);
268 kfile_putc(FLASH25_PROGRAM, fd->channel);
271 * Address that we want to write.
273 kfile_putc((fd->fd.seek_pos >> 16) & 0xFF, fd->channel);
274 kfile_putc((fd->fd.seek_pos >> 8) & 0xFF, fd->channel);
275 kfile_putc(fd->fd.seek_pos & 0xFF, fd->channel);
277 kfile_write(fd->channel, data, wr_len);
282 fd->fd.seek_pos += wr_len;
284 total_write += wr_len;
287 kprintf("written %u bytes\n", total_write);
292 * Sector erase function.
294 * Erase a select \p sector of serial flash memory.
296 * \note A sector size is FLASH25_SECTOR_SIZE.
297 * This operation could take a while.
299 void flash25_sectorErase(KFileFlash25 *fd, Flash25Sector sector)
303 * Erase a sector could take a while,
304 * for debug we measure that time
305 * see datasheet to compare this time.
307 DB(ticks_t start_time = timer_clock());
312 * To erase a sector of serial flash memory we must first
313 * enable write with a WREN opcode command, before
314 * the SECTOR_ERASE opcode. Sector is automatically
315 * determinate if any address within the sector
318 kfile_putc(FLASH25_WREN, fd->channel);
319 kfile_putc(FLASH25_SECTORE_ERASE,fd-> channel);
322 * Address inside the sector that we want to
325 kfile_putc(sector, fd->channel);
330 * We check serial flash memory state, and wait until ready-flag
333 flash25_waitReady(fd);
335 DB(kprintf("Erased sector [%d] in %d ms\n", sector, ticks_to_ms(timer_clock() - start_time)));
340 * Chip erase function.
342 * Erase all sector of serial flash memory.
344 * \note This operation could take a while.
346 void flash25_chipErase(KFileFlash25 *fd)
349 * Erase all chip could take a while,
350 * for debug we measure that time
351 * see datasheet to compare this time.
353 DB(ticks_t start_time = timer_clock());
356 * To erase serial flash memory we must first
357 * enable write with a WREN opcode command, before
358 * the CHIP_ERASE opcode.
360 flash25_sendCmd(fd, FLASH25_WREN);
361 flash25_sendCmd(fd, FLASH25_CHIP_ERASE);
364 * We check serial flash memory state, and wait until ready-flag
367 flash25_waitReady(fd);
369 DB(kprintf("Erased all memory in %d ms\n", ticks_to_ms(timer_clock() - start_time)));
374 * Init data flash memory interface.
376 void flash25_init(KFileFlash25 *fd, KFile *ch)
382 //Set kfile struct type as a generic kfile structure.
383 DB(fd->fd._type = KFT_FLASH25);
385 // Set up data flash programming functions.
386 fd->fd.reopen = flash25_reopen;
387 fd->fd.close = flash25_close;
388 fd->fd.read = flash25_read;
389 fd->fd.write = flash25_write;
390 fd->fd.seek = kfile_genericSeek;
393 * Init a local channel structure and flash kfile interface.
396 flash25_reopen(&fd->fd);
399 * Init data flash memory and micro pin.
401 if (!flash25_pin_init(fd))