Add dataflashOffset_t typedef, and use it where is needed.
[bertos.git] / 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: dflash.h 15402 2007-04-10 09:10:56Z asterix $
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 #include <drv/spi.h>
47 #include "hw_spi.h"
48
49 #include <appconfig.h>
50
51 /**
52  * Type definition for dflash memory.
53  */
54 typedef uint32_t dataflashAddr_t;
55 typedef uint32_t dataflashOffset_t;
56 typedef uint32_t dataflashSize_t;
57
58 #define RESET_PULSE_WIDTH     10 // Width of reset pulse in usec.
59 #define BUSY_BIT            0x80 // Select a busy bit in status register.
60 #define CMP_BIT             0x40 // Select a compare bit in status register.
61
62 /**
63  * Select 2,3,4,5 bits of status register, those
64  * bits indicate a id of density device (see datasheet for
65  * more detail).
66  */
67 #define GET_ID_DESITY_DEVICE(reg_stat)\
68   do {\
69                 reg_stat &= 0x3C;\
70                 reg_stat >>= 2;\
71   } while (0)
72
73 /**
74  * Pin definition.
75  *
76  * \note RESET and WP are asserted when logic
77  * level is low.
78  * \{
79  */
80 #define RESET                   PC0 ///<  Connect to RESET pin of flash memory
81 #define WP                      PC1 ///<  Connect to WP pin of flash memory
82 #define DATAFLASH_PORT        PORTC ///<  Micro pin PORT register.
83 #define DATAFLASH_PIN          PINC ///<  Micro pin PIN register.
84 #define DATAFLASH_DDR          DDRC ///<  Micro pin DDR register.
85 /* \} */
86
87 /**
88  * Pin logic level.
89  *
90  * \{
91  */
92 #define RESET_LOW()      do { DATAFLASHs_PORT &= ~BV(RESET); } while(0)
93 #define RESET_HIGH()     do { DATAFLASH_PORT |= BV(RESET); } while(0)
94 #define WP_LOW()         do { DATAFLASH_PORT &= ~BV(WP); } while(0)
95 #define WP_HIGH()        do { DATAFLASH_PORT |= BV(WP); } while(0)
96 /* \} */
97
98 /**
99  * Commands pin.
100  *
101  * \note To reset flash memory it needs a pulse
102  * long about 10 usec. To do this we insert a
103  * for cycle.
104  *
105  * \{
106  */
107 #define RESET_OUT()       do { DATAFLASH_DDR |= BV(RESET); } while(0)
108 #define WP_OUT()          do { DATAFLASH_DDR |= BV(WP); } while(0)
109 #define WRITE_ENABLE()    WP_HIGH()
110 #define WRITE_DISABLE()   WP_LOW()
111 #define RESET_ENABLE()    RESET_LOW()
112 #define RESET_DISABLE()   RESET_HIGH()
113 /* \} */
114
115
116 /**
117  * Memory definition.
118  *
119  * \note Below are defined valid flash memory support to
120  * this drive. Every time we call dataflash_init() function we check
121  * if memory defined are right (see dataflash.c form more detail).
122  * \{
123  */
124 #define DATAFLASH_AT45DB041B         1
125 #define DATAFLASH_AT45DB081D         2
126 #define DATAFLASH_AT45DB161D         3
127
128 #if CONFIG_DATA_FLASH == DATAFLASH_AT45DB161D
129         #define DATAFLASH_ID_DEVICE_DENSITY      0xb ///< This indicate AT45DB161D data flah memory.
130         #define DATAFLASH_PAGE_SIZE              528 ///< Number of byte in one page.
131         #define DATAFLASH_PAGE_ADDRESS_BIT        10 ///< Number bit for addressing one page.
132         #define DATAFLASH_NUM_PAGE              4096 ///< Number page in data flash memory.
133 #elif CONFIG_DATA_FLASH == DATAFLASH_AT45DB081D
134         #define DATAFLASH_ID_DEVICE_DENSITY      0x9  ///< This indicate AT45DB081D data flah memory.
135         #define DATAFLASH_PAGE_SIZE              264  ///< Number of byte in one page.
136         #define DATAFLASH_PAGE_ADDRESS_BIT         9  ///< Number bit for addressing one page.
137         #define DATAFLASH_NUM_PAGE              4096  ///< Number page in data flash memory.
138 #elif CONFIG_DATA_FLASH == DATAFLASH_AT45DB041B
139         #define DATAFLASH_ID_DEVICE_DENSITY      0x7  ///< This indicate AT45DB041B data flah memory.
140         #define DATAFLASH_PAGE_SIZE              264  ///< Number of byte in one page.
141         #define DATAFLASH_PAGE_ADDRESS_BIT         9  ///< Number bit for addressing one page.
142         #define DATAFLASH_NUM_PAGE              2048  ///< Number page in data flash memory.
143 #else
144         #error Nothing memory defined in CONFIG_DATA_FLASH are support.
145 #endif
146 /* \} */
147
148
149 /**
150  * Data flash opcode commands.
151  */
152 typedef enum {
153         /**
154         * Read commands data flash.
155         * \{
156         */
157
158 #if CONFIG_DATA_FLASH == DATAFLASH_AT45DB081D || CONFIG_DATA_FLASH == AT45DB161D
159         DFO_READ_FLASH_MEM_BYTE  = 0x0B, ///< Continuos array read.
160 #elif CONFIG_DATA_FLASH == DATAFLASH_AT45DB041B
161         DFO_READ_FLASH_MEM_BYTE  = 0xE8, ///< Continuos array read.
162 #else
163         #error No supported memory defined in CONFIG_DATA_FLASH.
164 #endif
165         DFO_READ_FLASH_MEM       = 0xD2, ///< Main memory page read.
166         DFO_READ_BUFF1           = 0xD4, ///< SRAM buffer 1 read.
167         DFO_READ_BUFF2           = 0xD6, ///< SRAM buffer 2 read.
168         /* \}*/
169
170         /**
171         * Program and erase commands data flash.
172         * \{
173         */
174         DFO_WRITE_BUFF1          =  0x84, ///< SRAM buffer 1 write.
175         DFO_WRITE_BUFF2          =  0x87, ///< SRAM buffer 2 write.
176         DFO_WRITE_BUFF1_TO_MEM_E =  0x83, ///< Buffer 1 to main memory page program with build-in erase.
177         DFO_WRITE_BUFF2_TO_MEM_E =  0x86, ///< Buffer 2 to main memory page program with build-in erase.
178         DFO_WRITE_BUFF1_TO_MEM   =  0x88, ///< Buffer 1 to main memory page program without build-in erase.
179         DFO_WRITE_BUFF2_TO_MEM   =  0x89, ///< Buffer 2 to main memory page program without build-in erase.
180         DFO_ERASE_PAGE           =  0x81, ///< Erase page.
181         DFO_ERASE_BLOCK          =  0x50, ///< Erase block.
182         DFO_ERASE_SECTOR         =  0x7C, ///< Erase sector.
183         DFO_WRITE_MEM_TR_BUFF1   =  0x82, ///< Write main memory page program through buffer 1.
184         DFO_WRITE_MEM_TR_BUFF2   =  0x85, ///< Write main memory page program through buffer 2.
185         /* \}*/
186
187         /**
188         * Additional commands data flash.
189         * \{
190         */
191         DFO_MOV_MEM_TO_BUFF1     =  0x53, ///< Main mmemory to buffer 1 transfer.
192         DFO_MOV_MEM_TO_BUFF2     =  0x55, ///< Main mmemory to buffer 2 transfer.
193         DFO_CMP_MEM_TO_BUFF1     =  0x60, ///< Main mmemory to buffer 1 compare.
194         DFO_CMP_MEM_TO_BUFF2     =  0x61, ///< Main mmemory to buffer 2 compare.
195         DFO_ARW_MEM_TR_BUFF1     =  0x58, ///< Auto page rewrite through buffer 1.
196         DFO_ARW_MEM_TR_BUFF2     =  0x59, ///< Auto page rewrite through buffer 2
197         DFO_PWR_DOWN             =  0xB9, ///< Deep power-down.
198         DFO_RESUME_PWR_DOWN      =  0xAB, ///< Resume from deep power-down.
199         DFO_READ_STATUS          =  0xD7, ///< Status register read.
200         DFO_ID_DEV               =  0x9F  ///< Manufacturer and device ID read.
201         /* \}*/
202 } DataFlashOpcode;
203
204 void dataflash_init(struct _KFile *fd);
205 void hal_dflash_test(void);
206
207 #endif /* DRV_DATAFLASH_H */
208