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 (interface).
35 * \version $Id: dataflash.h 21658 2008-06-05 16:42:54Z asterix $
37 * \author Daniele Basile <asterix@develer.com>
41 #ifndef DRV_DATAFLASH_H
42 #define DRV_DATAFLASH_H
44 #include <cfg/compiler.h>
46 #include <kern/kfile.h>
49 * Type definitions for dflash memory.
52 typedef uint32_t dataflash_page_t;
53 typedef uint32_t dataflash_offset_t;
54 typedef uint32_t dataflash_size_t;
60 typedef void (dataflash_setReset_t)(bool);
61 typedef void (dataflash_setCS_t)(bool);
66 * List of supported memory devices by this drive.
67 * Every time we call dataflash_init() we check device id to
68 * ensure we choose the right memory configuration.
69 * (see dataflash.c for more details).
71 typedef enum DataflashType
82 * Dataflash KFile context structure.
84 typedef struct KFileDataflash
86 KFile fd; ///< File descriptor.
87 KFile *channel; ///< Dataflash comm channel (usually SPI).
88 DataflashType dev; ///< Memory device type;
89 dataflash_page_t current_page; ///< Current loaded dataflash page.
90 bool page_dirty; ///< True if current_page is dirty (needs to be flushed).
91 dataflash_setReset_t *setReset; ///< Callback used to set reset pin of dataflash.
92 dataflash_setCS_t *setCS; ///< Callback used to set CS pin of dataflash.
98 #define KFT_DATAFLASH MAKE_ID('D', 'F', 'L', 'H')
101 * Convert + ASSERT from generic KFile to KFileDataflash.
103 INLINE KFileDataflash * KFILEDATAFLASH(KFile *fd)
105 ASSERT(fd->_type == KFT_DATAFLASH);
106 return (KFileDataflash *)fd;
109 #define RESET_PULSE_WIDTH 10 ///< Width of reset pulse in usec.
110 #define BUSY_BIT 0x80 ///< Select a busy bit in status register.
111 #define CMP_BIT 0x40 ///< Select a compare bit in status register.
114 * Select bits 2-5 of status register. These
115 * bits indicate device density (see datasheet for
118 #define GET_ID_DESITY_DEVICE(reg_stat) (((reg_stat) & 0x3C) >> 2)
121 * Data flash opcode commands.
123 typedef enum DataFlashOpcode {
125 * Dataflash read commands.
128 DFO_READ_FLASH_MEM_BYTE_D = 0x0B, ///< Continuos array read for D type memories.
129 DFO_READ_FLASH_MEM_BYTE_B = 0xE8, ///< Continuos array read for B type memories.
131 DFO_READ_FLASH_MEM = 0xD2, ///< Main memory page read.
132 DFO_READ_BUFF1 = 0xD4, ///< SRAM buffer 1 read.
133 DFO_READ_BUFF2 = 0xD6, ///< SRAM buffer 2 read.
137 * Program and erase commands data flash.
140 DFO_WRITE_BUFF1 = 0x84, ///< SRAM buffer 1 write.
141 DFO_WRITE_BUFF2 = 0x87, ///< SRAM buffer 2 write.
142 DFO_WRITE_BUFF1_TO_MEM_E = 0x83, ///< Buffer 1 to main memory page program with built-in erase.
143 DFO_WRITE_BUFF2_TO_MEM_E = 0x86, ///< Buffer 2 to main memory page program with built-in erase.
144 DFO_WRITE_BUFF1_TO_MEM = 0x88, ///< Buffer 1 to main memory page program without built-in erase.
145 DFO_WRITE_BUFF2_TO_MEM = 0x89, ///< Buffer 2 to main memory page program without built-in erase.
146 DFO_ERASE_PAGE = 0x81, ///< Erase page.
147 DFO_ERASE_BLOCK = 0x50, ///< Erase block.
148 DFO_ERASE_SECTOR = 0x7C, ///< Erase sector.
149 DFO_WRITE_MEM_TR_BUFF1 = 0x82, ///< Write main memory page through buffer 1.
150 DFO_WRITE_MEM_TR_BUFF2 = 0x85, ///< Write main memory page through buffer 2.
154 * Additional dataflash commands.
157 DFO_MOV_MEM_TO_BUFF1 = 0x53, ///< Transfer main mmemory to buffer 1.
158 DFO_MOV_MEM_TO_BUFF2 = 0x55, ///< Transfer main mmemory to buffer 2.
159 DFO_CMP_MEM_TO_BUFF1 = 0x60, ///< Compare main mmemory with buffer 1.
160 DFO_CMP_MEM_TO_BUFF2 = 0x61, ///< Compare main mmemory with buffer 2.
161 DFO_ARW_MEM_TR_BUFF1 = 0x58, ///< Auto page rewrite through buffer 1.
162 DFO_ARW_MEM_TR_BUFF2 = 0x59, ///< Auto page rewrite through buffer 2
163 DFO_PWR_DOWN = 0xB9, ///< Deep power-down.
164 DFO_RESUME_PWR_DOWN = 0xAB, ///< Resume from deep power-down.
165 DFO_READ_STATUS = 0xD7, ///< Read status register.
166 DFO_ID_DEV = 0x9F ///< Read manufacturer and device ID.
172 * Structure used to describe a dataflash memory.
174 typedef struct DataflashInfo
176 uint8_t density_id; ///< Density id, used to check memory type.
177 dataflash_size_t page_size; ///< Page size, in bytes.
178 uint8_t page_bits; ///< Number of bits needed to access a page.
179 uint16_t page_cnt; ///< Number of pages on memory.
180 DataFlashOpcode read_cmd; ///< Command to be used to perform a continuous array.
184 bool dataflash_init(KFileDataflash *fd, KFile *ch, DataflashType type, dataflash_setCS_t *setCS, dataflash_setReset_t *setReset);
187 * To test data falsh drive you could use
188 * this functions. To use these functions make sure to include in your make file the
189 * drv/datafalsh_test.c source.
191 * (see drv/datafalsh_test.c for more detail)
193 int dataflash_testSetup(void);
194 int dataflash_testRun(void);
195 int dataflash_testTearDown(void);
197 #endif /* DRV_DATAFLASH_H */