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 15379 2007-03-28 15:46:09Z asterix $
37 * \author Daniele Basile <asterix@develer.com>
41 #include <appconfig.h>
44 #include <cfg/macros.h>
45 #include <cfg/debug.h>
46 #include <cfg/module.h>
47 #include <drv/timer.h>
49 #include <drv/dataflash.h>
55 * Global variable for store current and previous data
56 * flash memory page address during operation of writing.
58 static dataflash_t previous_page = 0;
59 static bool page_modified = false;
63 * Send a generic command to data flash memory.
64 * This function send only 4 byte, for opcode, page address and
67 static void send_cmd(dataflash_t page_addr, dataflashOffset_t byte_addr, DataFlashOpcode opcode)
71 * Make sure to toggle CS signal in order,
72 * and reset dataflash command decoder.
74 * \note This is equivalent to CS_DISABLE() immediately followed by CS_ENABLE()
80 * To send one command to data flash memory, we send 4 byte.
81 * First byte is opcode command, second and third byte are
82 * page address, in last byte we write a byte page address.
83 * (see datasheet for more detail).
85 * \note Generaly a defaul memory page size is more than 256 byte.
86 * In this case we need for addressing a byte in one page more than
87 * 8 bit, so we put in fourth byte low part of address byte, and
88 * hight part of address byte in third byte togheter low par of page
101 spi_sendRecv((uint8_t)(page_addr >> (16 - DATAFLASH_PAGE_ADDRESS_BIT)));
102 spi_sendRecv((uint8_t)((page_addr << (DATAFLASH_PAGE_ADDRESS_BIT - 8)) + (byte_addr >> 8)));
105 * Send byte page address.
107 spi_sendRecv((uint8_t)byte_addr);
113 * Reset dataflash memory function.
115 * This function reset data flash memory
116 * with one pulse reset long about 10usec.
119 static void dataflash_reset(void)
123 timer_delayHp(us_to_hptime(RESET_PULSE_WIDTH));
126 timer_delayHp(us_to_hptime(RESET_PULSE_WIDTH));
130 * dataflash init function.
131 * This function initialize a micro pin and
132 * SPI driver, and test if data flash memory
133 * density is the same wich define in dataflash.h.
135 MOD_DEFINE(dataflash);
136 static bool dataflash_pin_init(void)
143 WRITE_ENABLE(); //pilot wp pin.
150 stat = dataflash_stat();
155 * 2,3,4,5 bits of 1 byte status register
156 * indicate a device density of dataflash memory
157 * (see datasheet for more detail.)
159 GET_ID_DESITY_DEVICE(stat);
161 if(stat == DATAFLASH_ID_DEVICE_DENSITY)
170 * Read status register of dataflah memory.
173 static uint8_t dataflash_stat(void)
178 * Make sure to toggle CS signal in order,
179 * and reset dataflash command decoder.
183 stat = spi_sendRecv(DFO_READ_STATUS);
184 stat = spi_sendRecv(0x00);
187 * Note: this function could be call one more time
188 * to check register status (es. check if memory has been
189 * teminate one operation), and so we don't disable CS to
190 * allow fast reading of register status.
197 * Send one command to data flash memory, and
198 * return status register value.
201 static uint8_t dataflash_cmd(dataflash_t page_addr, dataflashOffset_t byte_addr, DataFlashOpcode opcode)
206 send_cmd(page_addr, byte_addr, opcode);
211 * We chech data flash memory state, and wait until busy-flag
214 while(!(dataflash_stat() & BUSY_BIT));
216 stat = dataflash_stat();
219 * Data flash has been terminate a sent command, and so
229 * Read one byte from main data flash memory or buffer data
232 static uint8_t dataflash_read_byte(dataflash_t page_addr, dataflashOffset_t byte_addr, DataFlashOpcode opcode)
236 send_cmd(page_addr, byte_addr, opcode);
238 #if CONFIG_DATA_FLASH == DATAFLASH_AT45DB041B
239 if(opcode == DFO_READ_FLASH_MEM_BYTE)
242 * Send 24 don't care bit.
251 spi_sendRecv(0x00); //Send 8 don't care bit.
252 data = spi_sendRecv(0x00); //Read byte.
260 * Read \a len bytes from main data flash memory or buffer data
261 * flash memory, and put it in \a *block.
263 static void dataflash_read_block(dataflash_t page_addr, dataflashOffset_t byte_addr, DataFlashOpcode opcode, uint8_t *block, dataflashSize_t len)
266 send_cmd(page_addr, byte_addr, opcode);
268 if(opcode == DFO_READ_FLASH_MEM_BYTE)
271 * Send 24 don't care bit.
279 spi_sendRecv(0x00); //Send 8 don't care bit.
280 spi_read(block, len); //Read len bytes ad put in block buffer.
289 * Write \a len bytes in buffer buffer data flash memory.
291 * \note Isn't possible to write bytes directly in main memory data
292 * flash. To perform write in main memory you must before write in buffer
293 * data flash memory, an then send command to write page in main memory.
295 static void dataflash_write_block(dataflashOffset_t byte_addr, DataFlashOpcode opcode, uint8_t *block, dataflashSize_t len)
298 send_cmd(0x00, byte_addr, opcode);
300 spi_write(block, len); //Write len bytes.
308 * Load selct page from dataflash memory to buffer.
310 static void dataflash_loadPage(dataflash_t page_addr)
312 dataflash_cmd(page_addr, 0x00, DFO_MOV_MEM_TO_BUFF1);
316 * Flush select page (stored in buffer) in data flash main memory page.
318 void dataflash_flush(void)
322 dataflash_cmd(previous_page, 0x00, DFO_WRITE_BUFF1_TO_MEM_E);
324 page_modified = false;
326 kprintf("\n::=> Flush page:... <%ld>\n", previous_page);
330 /* Kfile interface section */
333 * Open data flash file \a fd
334 * \a name and \a mode are unused, cause flash memory is
335 * threated like one file.
337 static bool dataflash_open(struct _KFile *fd, UNUSED_ARG(const char *, name), UNUSED_ARG(int, mode))
339 MOD_CHECK(dataflash);
343 fd->size = (dataflashAddr_t)DATAFLASH_PAGE_SIZE * (dataflashAddr_t)DATAFLASH_NUM_PAGE;
345 /* Load select page memory from data flash memory*/
346 dataflash_loadPage(previous_page);
348 kprintf("dataflash file opened\n");
355 static bool dataflash_close(UNUSED_ARG(struct _KFile *,fd))
358 kprintf("dataflash file closed\n");
363 * Move \a fd file seek position of \a offset bytes
364 * from current position.
366 static int32_t dataflash_seek(struct _KFile *fd, int32_t offset, KSeekMode whence)
376 seek_pos = fd->size - 1;
379 seek_pos = fd->seek_pos;
388 if (seek_pos + offset > fd->size)
394 fd->seek_pos = seek_pos + offset;
395 kprintf("Flash seek to [%u]\n", fd->seek_pos);
401 * Read from file \a fd \a size bytes and put it in buffer \a buf
402 * \return the number of bytes read.
406 * Read \a _buf lenght \a size byte from data flash memmory.
408 * \note For read in data flash memory, we
409 * check flag page_modified, if is true (that mean
410 * we had been written a byte in buffer memory) we
411 * flush current page in data flash main memory, and
412 * then read byte from memory, else we read byte
413 * directly from data flash main memory.
415 * \return the number of bytes read.
417 static size_t dataflash_read(struct _KFile *fd, void *buf, size_t size)
419 dataflashOffset_t byte_addr;
420 dataflashAddr_t page_addr;
421 uin8_t *data = (uint8_t *)buf;
424 ASSERT(fd->seek_pos + size <= fd->size);
425 size = MIN((uint32_t)size, fd->size - fd->seek_pos);
427 kprintf("Reading at pos[%u]\n", fd->seek_pos);
430 * We select page and offest from absolute address.
432 page_addr = fd->seek_pos / (dataflashAddr_t)DATAFLASH_PAGE_SIZE;
433 byte_addr = fd->seek_pos % (dataflashOffset_t)DATAFLASH_PAGE_SIZE;
436 kprintf(" [page-<%ld>, byte-<%ld>]", page_addr, byte_addr);
439 * Flush current page in main memory if
440 * we had been written a byte in memory
445 * Read byte in main page data flash memory.
447 dataflash_read_block(page_addr, byte_addr, DFO_READ_FLASH_MEM_BYTE, data, size);
449 fd->seek_pos += size;
450 kprintf(" ::=> Read data: %02x\n",data);
456 * Write \a _buf in data flash memory
458 * \note For write \a _buf in data flash memory, we must
459 * before write in buffer data flash memory, and at end of write,
460 * we put page in data flash main memory. If we write in two
461 * different page, we put in main memory current page and reload
462 * page witch we want write.
464 * \return the number of bytes write.
466 static size_t dataflash_write(struct _KFile *fd, const void *_buf, size_t size)
469 dataflashOffset_t byte_addr;
470 dataflashAddr_t current_page;
472 uint8_t *data = (uint8_t *) _buf;
474 ASSERT(fd->seek_pos + size <= fd->size);
475 size = MIN((uint32_t)size, fd->size - fd->seek_pos);
477 kprintf("Writing at pos[%u]\n", fd->seek_pos);
482 * We select page and offest from absolute address.
484 current_page = fd->seek_pos / (dataflashAddr_t)DATAFLASH_PAGE_SIZE;
485 byte_addr = fd->seek_pos % (dataflashOffset_t)DATAFLASH_PAGE_SIZE;
488 size_t wr_len = MIN(size, DATAFLASH_PAGE_SIZE - byte_addr);
490 kprintf(" [page-<%ld>, byte-<%ld>]",current_page, byte_addr);
492 if (current_page != previous_page)
494 /* Flush current page in main memory*/
496 /* Load select page memory from data flash memory*/
497 dataflash_loadPage(current_page);
499 previous_page = current_page;
500 kprintf(" >> Load page: <%ld> ",current_page);
503 * Write byte in current page, and set true
504 * page_modified flag.
506 dataflash_write_byte(byte_addr, DFO_WRITE_BUFF1, data);
507 page_modified = true;
511 fd->seek_pos += wr_len;
513 total_write += wr_len;
516 kprintf("written %u bytes\n", total_write);
521 * Init data flash memory interface.
523 void dataflash_init(struct _KFile *fd)
525 // Set up data flash programming functions.
526 fd->open = dataflash_open;
527 fd->close = dataflash_close;
528 fd->read = dataflash_read;
529 fd->write = dataflash_write;
530 fd->seek = dataflash_seek;
532 // Init data flash memory and micro pin.
533 ASSERT(dataflash_pin_init());
537 * Test function for dataflash.
539 * This function test check low level driver for
540 * AT45xx (see dataflash.h for more info) data flash memory.
541 * We write a string in memory in some page ad read it.
543 void dataflash_test(void)
549 uint8_t test_buf[] = "0123456789 Develer s.r.l.";
552 kprintf("\n======= Data Flash test function =========================================\n");
553 kprintf("\nThe string test is: %s\n\n", test_buf);
555 fd.open(&fd, NULL, 0);
560 if (fd.seek(&fd, 0, SEEK_SET) != 0)
561 goto dataflash_test_end;
563 // Test flash write to address 0 (page 0)
564 if (!fd->write(&fd, test_buf, sizeof(test_buf)))
565 goto dataflash_test_end;
568 if (fd.seek(&fd, 0, SEEK_SET) != 0)
569 goto dataflash_test_end;
571 // Test flash read to address 0 (page 0)
572 if (!fd->read(&fd, cmp_buf, sizeof(test_buf)))
573 goto dataflash_test_end;
575 // Compare if are egual.
576 if ((memcmp(cmp_buf,test_buf, sizeof(test_buf)) == 0)
577 goto dataflash_test_end;
581 // Go to middle address memory.
582 fd.seek(&fd, (((dataflashAddr_t)DFLASH_PAGE_SIZE * (dataflashAddr_t)DFLASH_NUM_PAGE) / 2), SEEK_CUR);
584 // Test flash write at the middle of memory
585 if (!fd->write(&fd, test_buf, sizeof(test_buf)))
586 goto dataflash_test_end;
588 // Go to middle address memory.
589 fd.seek(&fd, (((dataflashAddr_t)DFLASH_PAGE_SIZE * (dataflashAddr_t)DFLASH_NUM_PAGE) / 2), SEEK_CUR);
591 // Test flash read at the middle of memory
592 if (!fd->read(&fd, cmp_buf, sizeof(test_buf)))
593 goto dataflash_test_end;
595 // Compare if are egual.
596 if ((memcmp(cmp_buf,test_buf, sizeof(test_buf)) == 0)
597 goto dataflash_test_end;
601 // Go to end of data flash.
602 fd.seek(&fd, ((dataflashAddr_t)DFLASH_PAGE_SIZE * (dataflashAddr_t)DFLASH_NUM_PAGE) - sizeof(test_buf), SEEK_END);
604 // Test flash write at the end of memory
605 if (!fd->write(&fd, test_buf, sizeof(test_buf)))
606 goto dataflash_test_end;
608 // Go to end of data flash.
609 fd.seek(&fd, ((dataflashAddr_t)DFLASH_PAGE_SIZE * (dataflashAddr_t)DFLASH_NUM_PAGE) - sizeof(test_buf), SEEK_END);
611 // Test flash read at the end of memory
612 if (!fd->read(&fd, cmp_buf, sizeof(test_buf)))
613 goto dataflash_test_end;
615 // Compare if are egual.
616 if ((memcmp(cmp_buf,test_buf, sizeof(test_buf)) == 0)
617 goto dataflash_test_end;
621 kprintf("\n====== Test end ===========================================================\n");