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 AT45DB081D Flash memory.
36 * \version $Id: dataflash.h 20677 2008-02-19 14:29:52Z batt $
37 * \author Daniele Basile <asterix@develer.com>
41 #ifndef DRV_DATAFLASH_H
42 #define DRV_DATAFLASH_H
44 #include <kern/kfile.h>
45 #include <cfg/compiler.h>
47 #include <appconfig.h>
51 * Type definitions for dflash memory.
54 typedef uint32_t dataflash_page_t;
55 typedef uint32_t dataflash_offset_t;
56 typedef uint32_t dataflash_size_t;
62 typedef void (dataflash_setReset_t)(bool);
63 typedef void (dataflash_setCS_t)(bool);
68 * List of supported memory devices by this drive.
69 * Every time we call dataflash_init() we check device id to
70 * ensure we choose the right memory configuration.
71 * (see dataflash.c for more details).
73 typedef enum DataflashType
83 * Dataflash KFile context structure.
85 typedef struct KFileDataflash
87 KFile fd; ///< File descriptor.
88 KFile *channel; ///< Dataflash comm channel (usually SPI).
89 DataflashType dev; ///< Memory device type;
90 dataflash_page_t current_page; ///< Current loaded dataflash page.
91 bool page_dirty; ///< True if current_page is dirty (needs to be flushed).
92 dataflash_setReset_t *setReset; ///< Callback used to set reset pin of dataflash.
93 dataflash_setCS_t *setCS; ///< Callback used to set CS pin of dataflash.
99 #define KFT_DATAFLASH MAKE_ID('D', 'F', 'L', 'H')
102 * Convert + ASSERT from generic KFile to KFileDataflash.
104 INLINE KFileDataflash * KFILEDATAFLASH(KFile *fd)
106 ASSERT(fd->_type == KFT_DATAFLASH);
107 return (KFileDataflash *)fd;
110 #define RESET_PULSE_WIDTH 10 ///< Width of reset pulse in usec.
111 #define BUSY_BIT 0x80 ///< Select a busy bit in status register.
112 #define CMP_BIT 0x40 ///< Select a compare bit in status register.
115 * Select bits 2-5 of status register. These
116 * bits indicate device density (see datasheet for
119 #define GET_ID_DESITY_DEVICE(reg_stat) (((reg_stat) & 0x3C) >> 2)
122 * Data flash opcode commands.
124 typedef enum DataFlashOpcode {
126 * Dataflash read commands.
129 DFO_READ_FLASH_MEM_BYTE_D = 0x0B, ///< Continuos array read for D type memories.
130 DFO_READ_FLASH_MEM_BYTE_B = 0xE8, ///< Continuos array read for B type memories.
132 DFO_READ_FLASH_MEM = 0xD2, ///< Main memory page read.
133 DFO_READ_BUFF1 = 0xD4, ///< SRAM buffer 1 read.
134 DFO_READ_BUFF2 = 0xD6, ///< SRAM buffer 2 read.
138 * Program and erase commands data flash.
141 DFO_WRITE_BUFF1 = 0x84, ///< SRAM buffer 1 write.
142 DFO_WRITE_BUFF2 = 0x87, ///< SRAM buffer 2 write.
143 DFO_WRITE_BUFF1_TO_MEM_E = 0x83, ///< Buffer 1 to main memory page program with built-in erase.
144 DFO_WRITE_BUFF2_TO_MEM_E = 0x86, ///< Buffer 2 to main memory page program with built-in erase.
145 DFO_WRITE_BUFF1_TO_MEM = 0x88, ///< Buffer 1 to main memory page program without built-in erase.
146 DFO_WRITE_BUFF2_TO_MEM = 0x89, ///< Buffer 2 to main memory page program without built-in erase.
147 DFO_ERASE_PAGE = 0x81, ///< Erase page.
148 DFO_ERASE_BLOCK = 0x50, ///< Erase block.
149 DFO_ERASE_SECTOR = 0x7C, ///< Erase sector.
150 DFO_WRITE_MEM_TR_BUFF1 = 0x82, ///< Write main memory page through buffer 1.
151 DFO_WRITE_MEM_TR_BUFF2 = 0x85, ///< Write main memory page through buffer 2.
155 * Additional dataflash commands.
158 DFO_MOV_MEM_TO_BUFF1 = 0x53, ///< Transfer main mmemory to buffer 1.
159 DFO_MOV_MEM_TO_BUFF2 = 0x55, ///< Transfer main mmemory to buffer 2.
160 DFO_CMP_MEM_TO_BUFF1 = 0x60, ///< Compare main mmemory with buffer 1.
161 DFO_CMP_MEM_TO_BUFF2 = 0x61, ///< Compare main mmemory with buffer 2.
162 DFO_ARW_MEM_TR_BUFF1 = 0x58, ///< Auto page rewrite through buffer 1.
163 DFO_ARW_MEM_TR_BUFF2 = 0x59, ///< Auto page rewrite through buffer 2
164 DFO_PWR_DOWN = 0xB9, ///< Deep power-down.
165 DFO_RESUME_PWR_DOWN = 0xAB, ///< Resume from deep power-down.
166 DFO_READ_STATUS = 0xD7, ///< Read status register.
167 DFO_ID_DEV = 0x9F ///< Read manufacturer and device ID.
173 * Structure used to describe a dataflash memory.
175 typedef struct DataflashInfo
177 uint8_t density_id; ///< Density id, used to check memory type.
178 dataflash_size_t page_size; ///< Page size, in bytes.
179 uint8_t page_bits; ///< Number of bits needed to access a page.
180 uint16_t page_cnt; ///< Number of pages on memory.
181 DataFlashOpcode read_cmd; ///< Command to be used to perform a continuous array.
185 bool dataflash_init(KFileDataflash *fd, KFile *ch, DataflashType type, dataflash_setCS_t *setCS, dataflash_setReset_t *setReset);
187 #endif /* DRV_DATAFLASH_H */