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 dataflash AT45DB family.
37 * \author Daniele Basile <asterix@develer.com>
39 * $WIZ$ module_name = "dataflash"
40 * $WIZ$ module_depends = "kfile"
41 * $WIZ$ module_configuration = "bertos/cfg/cfg_dataflash.h"
42 * $WIZ$ module_hw = "bertos/hw/hw_dataflash.h", "bertos/hw/hw_dataflash.c"
46 #ifndef DRV_DATAFLASH_H
47 #define DRV_DATAFLASH_H
49 #include <cfg/compiler.h>
51 #include <kern/kfile.h>
52 #include <fs/battfs.h>
55 * Type definitions for dflash memory.
58 typedef uint32_t dataflash_page_t;
59 typedef uint32_t dataflash_offset_t;
60 typedef uint32_t dataflash_size_t;
66 typedef void (dataflash_setReset_t)(bool);
67 typedef void (dataflash_setCS_t)(bool);
72 * List of supported memory devices by this drive.
73 * Every time we call dataflash_init() we check device id to
74 * ensure we choose the right memory configuration.
75 * (see dataflash.c for more details).
77 typedef enum DataflashType
88 * Dataflash KFile context structure.
90 typedef struct DataFlash
92 KFile fd; ///< File descriptor.
93 KFile *channel; ///< Dataflash comm channel (usually SPI).
94 DataflashType dev; ///< Memory device type;
95 dataflash_page_t current_page; ///< Current loaded dataflash page.
96 bool page_dirty; ///< True if current_page is dirty (needs to be flushed).
97 dataflash_setReset_t *setReset; ///< Callback used to set reset pin of dataflash.
98 dataflash_setCS_t *setCS; ///< Callback used to set CS pin of dataflash.
104 #define KFT_DATAFLASH MAKE_ID('D', 'F', 'L', 'H')
107 * Convert + ASSERT from generic KFile to DataFlash.
109 INLINE DataFlash * DATAFLASH_CAST(KFile *fd)
111 ASSERT(fd->_type == KFT_DATAFLASH);
112 return (DataFlash *)fd;
115 #define RESET_PULSE_WIDTH 10 ///< Width of reset pulse in usec.
116 #define BUSY_BIT 0x80 ///< Select a busy bit in status register.
117 #define CMP_BIT 0x40 ///< Select a compare bit in status register.
120 * Select bits 2-5 of status register. These
121 * bits indicate device density (see datasheet for
124 #define GET_ID_DESITY_DEVICE(reg_stat) (((reg_stat) & 0x3C) >> 2)
127 * Data flash opcode commands.
129 typedef enum DataFlashOpcode {
131 * Dataflash read commands.
134 DFO_READ_FLASH_MEM_BYTE_D = 0x0B, ///< Continuos array read for D type memories.
135 DFO_READ_FLASH_MEM_BYTE_B = 0xE8, ///< Continuos array read for B type memories.
137 DFO_READ_FLASH_MEM = 0xD2, ///< Main memory page read.
138 DFO_READ_BUFF1 = 0xD4, ///< SRAM buffer 1 read.
139 DFO_READ_BUFF2 = 0xD6, ///< SRAM buffer 2 read.
143 * Program and erase commands data flash.
146 DFO_WRITE_BUFF1 = 0x84, ///< SRAM buffer 1 write.
147 DFO_WRITE_BUFF2 = 0x87, ///< SRAM buffer 2 write.
148 DFO_WRITE_BUFF1_TO_MEM_E = 0x83, ///< Buffer 1 to main memory page program with built-in erase.
149 DFO_WRITE_BUFF2_TO_MEM_E = 0x86, ///< Buffer 2 to main memory page program with built-in erase.
150 DFO_WRITE_BUFF1_TO_MEM = 0x88, ///< Buffer 1 to main memory page program without built-in erase.
151 DFO_WRITE_BUFF2_TO_MEM = 0x89, ///< Buffer 2 to main memory page program without built-in erase.
152 DFO_ERASE_PAGE = 0x81, ///< Erase page.
153 DFO_ERASE_BLOCK = 0x50, ///< Erase block.
154 DFO_ERASE_SECTOR = 0x7C, ///< Erase sector.
155 DFO_WRITE_MEM_TR_BUFF1 = 0x82, ///< Write main memory page through buffer 1.
156 DFO_WRITE_MEM_TR_BUFF2 = 0x85, ///< Write main memory page through buffer 2.
160 * Additional dataflash commands.
163 DFO_MOV_MEM_TO_BUFF1 = 0x53, ///< Transfer main mmemory to buffer 1.
164 DFO_MOV_MEM_TO_BUFF2 = 0x55, ///< Transfer main mmemory to buffer 2.
165 DFO_CMP_MEM_TO_BUFF1 = 0x60, ///< Compare main mmemory with buffer 1.
166 DFO_CMP_MEM_TO_BUFF2 = 0x61, ///< Compare main mmemory with buffer 2.
167 DFO_ARW_MEM_TR_BUFF1 = 0x58, ///< Auto page rewrite through buffer 1.
168 DFO_ARW_MEM_TR_BUFF2 = 0x59, ///< Auto page rewrite through buffer 2
169 DFO_PWR_DOWN = 0xB9, ///< Deep power-down.
170 DFO_RESUME_PWR_DOWN = 0xAB, ///< Resume from deep power-down.
171 DFO_READ_STATUS = 0xD7, ///< Read status register.
172 DFO_ID_DEV = 0x9F ///< Read manufacturer and device ID.
178 * Structure used to describe a dataflash memory.
180 typedef struct DataflashInfo
182 uint8_t density_id; ///< Density id, used to check memory type.
183 dataflash_size_t page_size; ///< Page size, in bytes.
184 uint8_t page_bits; ///< Number of bits needed to access a page.
185 uint16_t page_cnt; ///< Number of pages on memory.
186 DataFlashOpcode read_cmd; ///< Command to be used to perform a continuous array.
190 bool dataflash_init(DataFlash *fd, KFile *ch, DataflashType type, dataflash_setCS_t *setCS, dataflash_setReset_t *setReset);
191 bool dataflash_diskInit(struct BattFsSuper *d, DataFlash *fd, pgcnt_t *page_array);
194 * To test data falsh drive you could use
195 * this functions. To use these functions make sure to include in your make file the
196 * drv/datafalsh_test.c source.
198 * (see drv/datafalsh_test.c for more detail)
200 int dataflash_testSetup(void);
201 /* For backward compatibility */
202 #define dataflash_testSetUp() dataflash_testSetup()
203 int dataflash_testRun(void);
204 int dataflash_testTearDown(void);
206 #endif /* DRV_DATAFLASH_H */