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 AT45DBXX Data Flash memory.
36 * \version $Id: dataflash.c 20677 2008-02-19 14:29:52Z batt $
37 * \author Daniele Basile <asterix@develer.com>
38 * \author Francesco Sacchi <batt@develer.com>
41 #include "dataflash.h"
43 #include <cfg/macros.h>
44 #include <cfg/debug.h>
45 #include <cfg/module.h>
47 #include <drv/timer.h>
49 #include <kern/kfile.h>
52 #include <kern/proc.h>
58 * Array used to describe dataflash memory devices currently supported.
60 static const DataflashInfo mem_info[] =
68 .read_cmd = DFO_READ_FLASH_MEM_BYTE_B,
76 .read_cmd = DFO_READ_FLASH_MEM_BYTE_D,
84 .read_cmd = DFO_READ_FLASH_MEM_BYTE_D,
92 .read_cmd = DFO_READ_FLASH_MEM_BYTE_D,
94 /* Add other memories here */
97 STATIC_ASSERT(countof(mem_info) == DFT_CNT);
100 * Macro that toggle CS of dataflash.
101 * \note This is equivalent to fd->setCS(false) immediately followed by fd->setCS(true).
103 INLINE void CS_TOGGLE(KFileDataflash *fd)
110 * Send a generic command to dataflash memory.
111 * This function send only 4 byte: opcode, page address and
114 static void send_cmd(KFileDataflash *fd, dataflash_page_t page_addr, dataflash_offset_t byte_addr, DataFlashOpcode opcode)
118 * Make sure to toggle CS signal in order,
119 * and reset dataflash command decoder.
125 * To send one command to data flash memory, we send 4 byte.
126 * First byte is opcode command, second and third byte are
127 * page address, in last byte we write a byte page address.
128 * (see datasheet for more detail).
130 * \note Generaly a defaul memory page size is more than 256 byte.
131 * In this case we need for addressing a byte in one page more than
132 * 8 bit, so we put in fourth byte low part of address byte, and
133 * hight part of address byte in third byte togheter low par of page
141 kfile_putc(opcode, fd->channel);
146 kfile_putc((uint8_t)(page_addr >> (16 - mem_info[fd->dev].page_bits)), fd->channel);
147 kfile_putc((uint8_t)((page_addr << (mem_info[fd->dev].page_bits - 8)) + (byte_addr >> 8)), fd->channel);
150 * Send byte page address.
152 kfile_putc((uint8_t)byte_addr, fd->channel);
156 * Reset dataflash memory function.
158 * If \a fd->setReset function is not NULL,
159 * this function resets data flash memory
160 * with one pulse reset long about 10usec.
163 static void dataflash_reset(KFileDataflash *fd)
170 timer_delayHp(us_to_hptime(RESET_PULSE_WIDTH));
172 timer_delayHp(us_to_hptime(RESET_PULSE_WIDTH));
178 * Read status register of dataflah memory.
180 static uint8_t dataflash_stat(KFileDataflash *fd)
183 * Make sure to toggle CS signal
184 * and reset dataflash command decoder.
188 kfile_putc(DFO_READ_STATUS, fd->channel);
190 return kfile_getc(fd->channel);
195 * Send one command to data flash memory, and
196 * return status register value.
199 static uint8_t dataflash_cmd(KFileDataflash *fd, dataflash_page_t page_addr, dataflash_offset_t byte_addr, DataFlashOpcode opcode)
203 send_cmd(fd, page_addr, byte_addr, opcode);
208 * We chech data flash memory state, and wait until busy-flag
211 while (!(dataflash_stat(fd) & BUSY_BIT))
218 stat = dataflash_stat(fd);
220 kfile_flush(fd->channel); // Flush channel
222 * Data flash has completed a bus cycle, so disable CS.
230 * Read \a len bytes from main data flash memory or buffer data
231 * flash memory, and put it in \a *block.
233 static void dataflash_readBlock(KFileDataflash *fd, dataflash_page_t page_addr, dataflash_offset_t byte_addr, DataFlashOpcode opcode, uint8_t *block, dataflash_size_t len)
235 send_cmd(fd, page_addr, byte_addr, opcode);
237 if (opcode == DFO_READ_FLASH_MEM_BYTE_B)
240 * Send 24 don't care bits.
242 uint8_t dummy[] = { 0, 0, 0 };
243 kfile_write(fd->channel, dummy, sizeof(dummy));
246 kfile_putc(0, fd->channel); //Send 8 don't care bit.
247 kfile_read(fd->channel, block, len); //Read len bytes ad put in block buffer.
248 kfile_flush(fd->channel); // Flush channel
254 * Write \a len bytes in dataflash memory buffer.
256 * \note Is not possible to write directly in dataflash main memory.
257 * To perform a write in main memory you must first write in dataflash buffer
258 * memory and then send a command to write the page in main memory.
260 static void dataflash_writeBlock(KFileDataflash *fd, dataflash_offset_t offset, DataFlashOpcode opcode, const uint8_t *block, dataflash_size_t len)
262 ASSERT(offset + len <= mem_info[fd->dev].page_size);
264 send_cmd(fd, 0x00, offset, opcode);
266 kfile_write(fd->channel, block, len); //Write len bytes.
267 kfile_flush(fd->channel); // Flush channel
274 * Load selct page from dataflash memory to buffer.
276 static void dataflash_loadPage(KFileDataflash *fd, dataflash_page_t page_addr)
278 dataflash_cmd(fd, page_addr, 0x00, DFO_MOV_MEM_TO_BUFF1);
282 * Flush select page (stored in buffer) in data flash main memory page.
284 static int dataflash_flush(KFile *_fd)
286 KFileDataflash *fd = KFILEDATAFLASH(_fd);
289 dataflash_cmd(fd, fd->current_page, 0x00, DFO_WRITE_BUFF1_TO_MEM_E);
291 fd->page_dirty = false;
293 kprintf("Flushing page <%ld>\n", fd->current_page);
298 /* Kfile interface section */
303 static int dataflash_close(struct KFile *_fd)
305 dataflash_flush(_fd);
311 * Reopen dataflash file \a fd.
313 static KFile *dataflash_reopen(KFile *_fd)
315 KFileDataflash *fd = KFILEDATAFLASH(_fd);
316 dataflash_close(_fd);
318 fd->current_page = 0;
321 /* Load selected page from dataflash memory */
322 dataflash_loadPage(fd, fd->current_page);
330 * Read in \a buf \a size bytes from dataflash memmory.
332 * \note For reading data flash memory, we
333 * check flag page_dirty, if is true (that mean
334 * we have written a byte in buffer memory) we
335 * flush current page in main memory and
336 * then read from memory, else we read byte
337 * directly from data flash main memory.
339 * \return the number of bytes read.
341 static size_t dataflash_read(struct KFile *_fd, void *buf, size_t size)
343 KFileDataflash *fd = KFILEDATAFLASH(_fd);
345 dataflash_offset_t byte_addr;
346 dataflash_page_t page_addr;
347 uint8_t *data = (uint8_t *)buf;
350 ASSERT(fd->fd.seek_pos + size <= fd->fd.size);
351 size = MIN((kfile_size_t)size, fd->fd.size - fd->fd.seek_pos);
353 kprintf("Reading at pos[%lu]\n", fd->fd.seek_pos);
356 * We select page and offest from absolute address.
358 page_addr = fd->fd.seek_pos / mem_info[fd->dev].page_size;
359 byte_addr = fd->fd.seek_pos % mem_info[fd->dev].page_size;
361 kprintf("[page-<%ld>, byte-<%ld>]", page_addr, byte_addr);
364 * Flush current page in main memory if
365 * we had been written a byte in memory
367 dataflash_flush(&fd->fd);
370 * Read byte in main page data flash memory.
372 dataflash_readBlock(fd, page_addr, byte_addr, mem_info[fd->dev].read_cmd, data, size);
374 fd->fd.seek_pos += size;
375 kprintf("Read %ld bytes\n", size);
381 * Write \a _buf in dataflash memory
383 * \note For writing \a _buf in dataflash memory, we must
384 * first write in buffer data flash memory. At the end of write,
385 * we can put page in dataflash main memory.
386 * If we write in two contiguous pages, we put in main memory current
387 * page and then reload the page which we want to write.
389 * \return the number of bytes write.
391 static size_t dataflash_write(struct KFile *_fd, const void *_buf, size_t size)
393 KFileDataflash *fd = KFILEDATAFLASH(_fd);
395 dataflash_offset_t offset;
396 dataflash_page_t new_page;
397 size_t total_write = 0;
399 const uint8_t *data = (const uint8_t *) _buf;
401 ASSERT(fd->fd.seek_pos + size <= fd->fd.size);
402 size = MIN((kfile_size_t)size, fd->fd.size - fd->fd.seek_pos);
404 kprintf("Writing at pos[%lu]\n", fd->fd.seek_pos);
409 * We select page and offest from absolute address.
411 new_page = fd->fd.seek_pos / mem_info[fd->dev].page_size;
412 offset = fd->fd.seek_pos % mem_info[fd->dev].page_size;
415 size_t wr_len = MIN((dataflash_size_t)size, mem_info[fd->dev].page_size - offset);
417 kprintf(" [page-<%ld>, byte-<%ld>]",new_page, offset);
419 if (new_page != fd->current_page)
421 /* Flush current page in main memory*/
422 dataflash_flush(&fd->fd);
423 /* Load select page memory from data flash memory*/
424 dataflash_loadPage(fd, new_page);
426 fd->current_page = new_page;
427 kprintf(" >> Load page: <%ld> ", new_page);
430 * Write byte in current page, and set true
433 dataflash_writeBlock(fd, offset, DFO_WRITE_BUFF1, data, wr_len);
434 fd->page_dirty = true;
437 fd->fd.seek_pos += wr_len;
439 total_write += wr_len;
442 kprintf("written %lu bytes\n", total_write);
446 MOD_DEFINE(dataflash);
449 * Dataflash init function.
450 * This function initialize \a fd with SPI channel \a ch and test if data flash memory
451 * density is the same specified by device \a dev.
452 * \a setCS is a callback used to set/reset CS line.
453 * \a setReset is a callback used to set/reset the dataflash (can be NULL if reset is unconnected)
454 * \return true if ok, false if memory density read from dataflash is not compliant with the
457 bool dataflash_init(KFileDataflash *fd, KFile *ch, DataflashType dev, dataflash_setCS_t *setCS, dataflash_setReset_t *setReset)
461 MOD_CHECK(hw_dataflash);
466 ASSERT(dev < DFT_CNT);
468 memset(fd, 0, sizeof(*fd));
469 DB(fd->fd._type = KFT_DATAFLASH);
472 fd->setReset = setReset;
475 // Setup data flash programming functions.
476 fd->fd.reopen = dataflash_reopen;
477 fd->fd.close = dataflash_close;
478 fd->fd.read = dataflash_read;
479 fd->fd.write = dataflash_write;
480 fd->fd.seek = kfile_genericSeek;
481 fd->fd.flush = dataflash_flush;
485 stat = dataflash_stat(fd);
488 * 2,3,4,5 bits of 1 byte status register
489 * indicate a device density of dataflash memory
490 * (see datasheet for more detail.)
492 if (GET_ID_DESITY_DEVICE(stat) != mem_info[fd->dev].density_id)
495 fd->current_page = 0;
497 fd->fd.size = mem_info[fd->dev].page_size * mem_info[fd->dev].page_cnt;
499 /* Load selected page from dataflash memory */
500 dataflash_loadPage(fd, fd->current_page);