8aad1f5465ebba249855a7c465bc8e4142d149a2
[bertos.git] / drv / dflash.c
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.c 15379 2007-03-28 15:46:09Z asterix $
37  * \author Daniele Basile <asterix@develer.com>
38  */
39
40
41 #include <appconfig.h>
42
43 #include <avr/io.h>
44 #include <cfg/macros.h>
45 #include <cfg/debug.h>
46 #include <cfg/module.h>
47 #include <drv/timer.h>
48 #include <drv/spi.h>
49 #include <drv/dflash.h>
50
51 #include "hw_spi.h"
52
53 /**
54  * Send a generic command to data flash memory.
55  * This function send only 4 byte, for opcode, page address and
56  * byte address.
57  */
58 static void send_cmd(dflashAddr_t page_addr, dflashAddr_t byte_addr, DFlashOpcode opcode)
59 {
60
61         /*
62          * Make sure to toggle CS signal in order,
63          * and reset dflash command decoder.
64          * \{
65          */
66         CS_DISABLE();
67         CS_ENABLE();
68         /* \} */
69
70         /*
71          * To send one command to data flash memory, we send 4 byte.
72          * First byte is opcode command, second and third byte are
73          * page address, in last byte we write a byte page address.
74          * (see datasheet for more detail).
75          *
76          * \note Generaly a defaul memory page size is more than 256 byte.
77          *  In this case we need for addressing a byte in one page more than
78          *  8 bit, so we put in fourth byte low part of address byte, and
79          *  hight part of address byte in third byte togheter low par of page
80          *  address.
81          *
82          * \{
83          */
84
85         /*
86          * Send opcode.
87          */
88         spi_sendRecv(opcode);
89
90         /*
91          *  Send page address.
92          * \{
93          */
94         spi_sendRecv((uint8_t)(page_addr >> (16 - DFLASH_PAGE_ADDRESS_BIT)));
95         spi_sendRecv((uint8_t)((page_addr << (DFLASH_PAGE_ADDRESS_BIT - 8)) + (byte_addr >> 8)));
96         /*\}*/
97
98         /*
99          * Send byte page address.
100          */
101         spi_sendRecv((uint8_t)byte_addr);
102
103         /* \} */
104
105 }
106
107
108 //TODO: deve ritornare un bool?
109 /**
110  * Init data flash memory interface.
111  */
112 void dflash_init(struct _KFile *fd)
113 {
114         // Set up data flash programming functions.
115         fd->open = dflash_open;
116         fd->close = dflash_close;
117         fd->read = dflash_read;
118         fd->write = dflash_write;
119         fd->seek = dflash_seek;
120
121         // Init data flash memory and micro pin.
122         dflash_pin_init();
123 }
124
125
126 /**
127  * Reset dataflash memory function.
128  *
129  * This function reset data flash memory
130  * with one pulse reset long about 10usec.
131  *
132  */
133 void dflash_reset(void)
134 {
135         CS_ENABLE();
136         RESET_ENABLE();
137         timer_delayHp(us_to_hptime(RESET_PULSE_WIDTH));
138         CS_DISABLE();
139         RESET_DISABLE();
140         timer_delayHp(us_to_hptime(RESET_PULSE_WIDTH));
141 }
142
143 /**
144  * dflash init function.
145  * This function initialize a micro pin and
146  * SPI driver, and test if data flash memory
147  * density is the same wich define in dflash.h.
148  */
149 MOD_DEFINE(dflash);
150 static bool dflash_pin_init(void)
151 {
152         uint8_t stat;
153
154         MOD_CHECK(spi);
155
156         RESET_DISABLE();
157         WRITE_ENABLE(); //pilot wp pin.
158
159         RESET_OUT();
160         WP_OUT();
161
162         dflash_reset();
163
164         stat = dflash_stat();
165
166         MOD_INIT(dflash);
167
168         /*
169          * 2,3,4,5 bit of 1 byte status register
170          * indicate a device density of dflash memory
171          * (see datasheet for more detail.)
172          */
173         GET_ID_DESITY_DEVICE(stat);
174
175         if(stat == DFLASH_ID_DEVICE_DENSITY)
176                 return true;
177         else
178                 return false;
179
180 }
181
182
183 /**
184  * Read status register of dataflah memory.
185  *
186  */
187 static uint8_t dflash_stat(void)
188 {
189         uint8_t stat;
190
191         /*
192          * Make sure to toggle CS signal in order,
193          * and reset dflash command decoder.
194          * \{
195          */
196         CS_DISABLE();
197         CS_ENABLE();
198         /* \} */
199
200         stat = spi_sendRecv(DFO_READ_STATUS);
201         stat = spi_sendRecv(0x00);
202
203         return stat;
204 }
205
206
207 /**
208  * Send one command to data flash memory, and
209  * return status register value.
210  *
211  */
212 static uint8_t dflash_cmd(dflashAddr_t page_addr, dflashAddr_t byte_addr, DFlashOpcode opcode)
213 {
214
215         send_cmd(page_addr, byte_addr, opcode);
216
217         CS_DISABLE();
218         CS_ENABLE();
219
220         /*
221          * We chech data flash memory state, and wait until busy-flag
222          * is hight.
223          */
224         while(!(dflash_stat() & BUSY_BIT));
225
226         return (dflash_stat());
227
228 }
229
230 /**
231  * Read one byte from main data flash memory or buffer data
232  * flash memory.
233  */
234 static uint8_t dflash_read_byte(dflashAddr_t page_addr, dflashAddr_t byte_addr, DFlashOpcode opcode)
235 {
236         uint8_t data;
237
238         send_cmd(page_addr, byte_addr, opcode);
239
240 #if CONFIG_DATA_FLASH == AT45DB041B
241         if(opcode == DFO_READ_FLASH_MEM_BYTE)
242         {
243                 /*
244                  * Send 24 don't care bit.
245                  * \{
246                  */
247                 spi_sendRecv(0x00);
248                 spi_sendRecv(0x00);
249                 spi_sendRecv(0x00);
250                 /* \} */
251
252         }
253 #endif
254
255         spi_sendRecv(0x00);         //Send 8 don't care bit.
256         data = spi_sendRecv(0x00);  //Read byte.
257         CS_DISABLE();
258
259         return data;
260 }
261
262 /**
263  * Read \param len bytes from main data flash memory or buffer data
264  * flash memory, and put it in \param *block.
265  */
266 static void dflash_read_block(dflashAddr_t page_addr, dflashAddr_t byte_addr, DFlashOpcode opcode, uint8_t *block, dflashSize_t len)
267 {
268
269         send_cmd(page_addr, byte_addr, opcode);
270
271         if(opcode == DFO_READ_FLASH_MEM_BYTE)
272         {
273                 /*
274                  * Send 24 don't care bit.
275                  * \{
276                  */
277                 spi_sendRecv(0x00);
278                 spi_sendRecv(0x00);
279                 spi_sendRecv(0x00);
280                 /* \} */
281         }
282
283         spi_sendRecv(0x00);   //Send 8 don't care bit.
284         spi_read(block, len); //Read len bytes ad put in block buffer.
285
286
287         CS_DISABLE();
288
289 }
290
291 /**
292  * Write one byte in buffer buffer data flash memory.
293  *
294  * \note Isn't possible to write byte directly in main memory data
295  * flash. To perform write in main memory you must before write in buffer
296  * data flash memory, an then send command to write page in main memory.
297  */
298 static void dflash_write_byte(dflashAddr_t byte_addr, DFlashOpcode opcode, uint8_t data)
299 {
300         send_cmd(0x00, byte_addr, opcode);
301
302         spi_sendRecv(data); //Write data byte.
303
304         CS_DISABLE();
305 }
306
307 /**
308  * Write \param len bytes in buffer buffer data flash memory.
309  *
310  * \note Isn't possible to write bytes directly in main memory data
311  * flash. To perform write in main memory you must before write in buffer
312  * data flash memory, an then send command to write page in main memory.
313  */
314 static void dflash_write_block(dflashAddr_t byte_addr, DFlashOpcode opcode, uint8_t *block, dflashSize_t len)
315 {
316
317
318         send_cmd(0x00, byte_addr, opcode);
319
320         spi_write(block, len); //Write len bytes.
321
322         CS_DISABLE();
323
324 }