e5bdd23c2da4727c6326870ccd8eee3d0e59eef4
[bertos.git] / bertos / drv / dataflash.h
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
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.
10  *
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.
15  *
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
19  *
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.
28  *
29  * Copyright 2007 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief Function library for dataflash AT45DB family (interface).
34  *
35  * \version $Id: dataflash.h 21658 2008-06-05 16:42:54Z asterix $
36  *
37  * \author Daniele Basile <asterix@develer.com>
38  *
39  * $WIZARD_MODULE = {
40  * "name" : "dataflash",
41  * "depends" : [kfile],
42  * "configuration" : "bertos/cfg/cfg_dataflash.h"
43  * }
44  */
45
46
47 #ifndef DRV_DATAFLASH_H
48 #define DRV_DATAFLASH_H
49
50 #include <cfg/compiler.h>
51
52 #include <kern/kfile.h>
53 #include <fs/battfs.h>
54
55 /**
56  * Type definitions for dflash memory.
57  * \{
58  */
59 typedef uint32_t dataflash_page_t;
60 typedef uint32_t dataflash_offset_t;
61 typedef uint32_t dataflash_size_t;
62 /*\}*/
63
64 /**
65  *
66  */
67 typedef void (dataflash_setReset_t)(bool);
68 typedef void (dataflash_setCS_t)(bool);
69
70 /**
71  * Memory definitions.
72  *
73  * List of supported memory devices by this drive.
74  * Every time we call dataflash_init() we check device id to
75  * ensure we choose the right memory configuration.
76  * (see dataflash.c for more details).
77  */
78 typedef enum DataflashType
79 {
80         DFT_AT45DB041B = 0,
81         DFT_AT45DB081D,
82         DFT_AT45DB161D,
83         DFT_AT45DB642D,
84         DFT_CNT
85 } DataflashType;
86
87
88 /**
89  * Dataflash KFile context structure.
90  */
91 typedef struct DataFlash
92 {
93         KFile fd;                       ///< File descriptor.
94         KFile *channel;                 ///< Dataflash comm channel (usually SPI).
95         DataflashType dev;              ///< Memory device type;
96         dataflash_page_t current_page;  ///< Current loaded dataflash page.
97         bool page_dirty;                ///< True if current_page is dirty (needs to be flushed).
98         dataflash_setReset_t *setReset; ///< Callback used to set reset pin of dataflash.
99         dataflash_setCS_t *setCS;       ///< Callback used to set CS pin of dataflash.
100 } DataFlash;
101
102 /**
103  * ID for dataflash.
104  */
105 #define KFT_DATAFLASH MAKE_ID('D', 'F', 'L', 'H')
106
107 /**
108  * Convert + ASSERT from generic KFile to DataFlash.
109  */
110 INLINE DataFlash * DATAFLASH_CAST(KFile *fd)
111 {
112         ASSERT(fd->_type == KFT_DATAFLASH);
113         return (DataFlash *)fd;
114 }
115
116 #define RESET_PULSE_WIDTH     10 ///< Width of reset pulse in usec.
117 #define BUSY_BIT            0x80 ///< Select a busy bit in status register.
118 #define CMP_BIT             0x40 ///< Select a compare bit in status register.
119
120 /**
121  * Select bits 2-5 of status register. These
122  * bits indicate device density (see datasheet for
123  * more details).
124  */
125 #define GET_ID_DESITY_DEVICE(reg_stat) (((reg_stat) & 0x3C) >> 2)
126
127 /**
128  * Data flash opcode commands.
129  */
130 typedef enum DataFlashOpcode {
131         /**
132         * Dataflash read commands.
133         * \{
134         */
135         DFO_READ_FLASH_MEM_BYTE_D  = 0x0B, ///< Continuos array read for D type memories.
136         DFO_READ_FLASH_MEM_BYTE_B  = 0xE8, ///< Continuos array read for B type memories.
137
138         DFO_READ_FLASH_MEM       = 0xD2, ///< Main memory page read.
139         DFO_READ_BUFF1           = 0xD4, ///< SRAM buffer 1 read.
140         DFO_READ_BUFF2           = 0xD6, ///< SRAM buffer 2 read.
141         /* \}*/
142
143         /**
144         * Program and erase commands data flash.
145         * \{
146         */
147         DFO_WRITE_BUFF1          =  0x84, ///< SRAM buffer 1 write.
148         DFO_WRITE_BUFF2          =  0x87, ///< SRAM buffer 2 write.
149         DFO_WRITE_BUFF1_TO_MEM_E =  0x83, ///< Buffer 1 to main memory page program with built-in erase.
150         DFO_WRITE_BUFF2_TO_MEM_E =  0x86, ///< Buffer 2 to main memory page program with built-in erase.
151         DFO_WRITE_BUFF1_TO_MEM   =  0x88, ///< Buffer 1 to main memory page program without built-in erase.
152         DFO_WRITE_BUFF2_TO_MEM   =  0x89, ///< Buffer 2 to main memory page program without built-in erase.
153         DFO_ERASE_PAGE           =  0x81, ///< Erase page.
154         DFO_ERASE_BLOCK          =  0x50, ///< Erase block.
155         DFO_ERASE_SECTOR         =  0x7C, ///< Erase sector.
156         DFO_WRITE_MEM_TR_BUFF1   =  0x82, ///< Write main memory page through buffer 1.
157         DFO_WRITE_MEM_TR_BUFF2   =  0x85, ///< Write main memory page through buffer 2.
158         /* \}*/
159
160         /**
161         * Additional dataflash commands.
162         * \{
163         */
164         DFO_MOV_MEM_TO_BUFF1     =  0x53, ///< Transfer main mmemory to buffer 1.
165         DFO_MOV_MEM_TO_BUFF2     =  0x55, ///< Transfer main mmemory to buffer 2.
166         DFO_CMP_MEM_TO_BUFF1     =  0x60, ///< Compare main mmemory with buffer 1.
167         DFO_CMP_MEM_TO_BUFF2     =  0x61, ///< Compare main mmemory with buffer 2.
168         DFO_ARW_MEM_TR_BUFF1     =  0x58, ///< Auto page rewrite through buffer 1.
169         DFO_ARW_MEM_TR_BUFF2     =  0x59, ///< Auto page rewrite through buffer 2
170         DFO_PWR_DOWN             =  0xB9, ///< Deep power-down.
171         DFO_RESUME_PWR_DOWN      =  0xAB, ///< Resume from deep power-down.
172         DFO_READ_STATUS          =  0xD7, ///< Read status register.
173         DFO_ID_DEV               =  0x9F  ///< Read manufacturer and device ID.
174         /* \}*/
175 } DataFlashOpcode;
176
177
178 /**
179  * Structure used to describe a dataflash memory.
180  */
181 typedef struct DataflashInfo
182 {
183         uint8_t density_id;       ///< Density id, used to check memory type.
184         dataflash_size_t page_size;       ///< Page size, in bytes.
185         uint8_t page_bits;        ///< Number of bits needed to access a page.
186         uint16_t page_cnt;        ///< Number of pages on memory.
187         DataFlashOpcode read_cmd; ///< Command to be used to perform a continuous array.
188 } DataflashInfo;
189
190
191 bool dataflash_init(DataFlash *fd, KFile *ch, DataflashType type, dataflash_setCS_t *setCS, dataflash_setReset_t *setReset);
192 bool dataflash_diskInit(struct BattFsSuper *d, DataFlash *fd, pgcnt_t *page_array);
193
194 /**
195  * To test data falsh drive you could use
196  * this functions. To use these functions make sure to include in your make file the
197  * drv/datafalsh_test.c source.
198  *
199  * (see drv/datafalsh_test.c for more detail)
200  */
201 int dataflash_testSetUp(void);
202 int dataflash_testRun(void);
203 int dataflash_testTearDown(void);
204
205 #endif /* DRV_DATAFLASH_H */