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