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