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