Contiki-Inga 3.x
at45db.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012, TU Braunschweig.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 /**
31  * \file
32  * Atmel Flash EEPROM AT45DB interface definitions
33  * \author
34  * Ulf Kulau <kulau@ibr.cs.tu-bs.de>
35  */
36 
37 /**
38  * \addtogroup inga_device_driver
39  * @{
40  */
41 
42 /**
43  * \defgroup AT45DB_interface Atmel Flash EEPROM AT45DB interface
44  *
45  * <p>A fixed Flash EEPROM is always good to store some data. Furthermore
46  * in this project environment, the AT45DBxx1 will be used as a hardware
47  * interface between the boot section and the application section.</p>
48  *
49  * \note
50  * All functions will return immediately if flash is uninitialized
51  * or initialization failed.
52  *
53  * \note
54  * The function of the AT45DBxx1 is a little bit different compared to
55  * an SD-Card. The basic idea of the SD-Card is, to store one block
56  * (512Byte) into a local buffer an transfer the whole block via SPI
57  * to the SD-Card.
58  * The AT45DBxx1 has two page buffer, where the data can be collected by
59  * sending a byte via SPI to one of these buffers. If a buffer is filled,
60  * only a command is necessary and the AT45DBxx1 will copy the buffer into
61  * the flash section. To avoid latency, it is possible (and implemented)
62  * to switch between the page buffers.
63  * @{
64  *
65  */
66 
67 #ifndef FLASHAT45DB_H_
68 #define FLASHAT45DB_H_
69 
70 #include "../dev/mspi.h"
71 #include <stdio.h>
72 #include <util/delay.h>
73 
74 /*!
75  * SPI device order. The chip select number where the
76  * AT45DBxx1 Flash EEPROM is connected to the BCD-decimal
77  * decoder
78  */
79 #define AT45DB_CS 1
80 
81 /*!
82  * Status Register Address. Bit 7 signalizes if the device is
83  * busy.
84  * - 1 : not busy
85  * - 0 : busy
86  */
87 #define AT45DB_STATUS_REG 0xD7
88 
89 /*!
90  * Block Erase Opcode
91  */
92 #define AT45DB_BLOCK_ERASE 0x50
93 /*!
94  * Page Erase Opcode
95  */
96 #define AT45DB_PAGE_ERASE 0x81
97 /*!
98  * Main Memory Page Program Buffer 1
99  */
100 #define AT45DB_PAGE_PROGRAM_1 0x82
101 /*!
102  * Main Memory Page Program Buffer 2
103  */
104 #define AT45DB_PAGE_PROGRAM_2 0x85
105 
106 /*!
107  * Write byte(s) to buffer 1 opcode
108  */
109 #define AT45DB_BUFFER_1 0x84
110 /*!
111  * Write byte(s) to buffer 2 opcode
112  */
113 #define AT45DB_BUFFER_2 0x87
114 
115 /*!
116  * Copy Buffer 1 to page Opcode
117  */
118 #define AT45DB_BUF_1_TO_PAGE 0x83 //0x88 without auto erase
119 /*!
120  * Copy Buffer 2 to page Opcode
121  */
122 #define AT45DB_BUF_2_TO_PAGE 0x86 //0x89 without auto erase
123 /*!
124  * Read direct from Flash EEPROM page Opcode
125  */
126 #define AT45DB_PAGE_READ 0xD2
127 /*!
128  * Transfer page to buffer 2 Opcode
129  * \note Only Buffer 2 is used to readout a page, because the read
130  * respectively transfer latency is only about 200us
131  *
132  */
133 #define AT45DB_PAGE_TO_BUF 0x55 //use buffer 2
134 /*!
135  * Read buffer 2 opcode
136  * \note Only Buffer 2 is used to readout a page, because the read
137  * respectively transfer latency is only about 200us
138  */
139 #define AT45DB_READ_BUFFER 0xD6
140 
141 
142 /*!
143  * This typedef manages the buffer switching, to perform
144  * the write operation
145  */
146 typedef struct{
147 /*!
148  * Holds the active buffer
149  * <ul>
150  * <li> 0 : Active Buffer = Buffer 1
151  * <li> 1 : Active Buffer = Buffer 2
152  * </ul>
153  */
154  volatile uint8_t active_buffer;
155 /*!
156  * The specific "byte(s) to buffer" opcode for buffer 1
157  * and buffer 2
158  */
159  volatile uint8_t buffer_addr[2];
160 /*!
161  * The specific "buffer to page" opcode for buffer 1 and
162  * buffer 2
163  */
164  volatile uint8_t buf_to_page_addr[2];
165 
166 /*!
167  * Main Memory Page Program (Erase Page + Reprogram directly in one operation)
168  */
169  volatile uint8_t page_program[2];
170 }bufmgr_t;
171 
172 /**
173  * \brief Initialize the AT45DBxx1 Flash EEPROM
174  *
175  * \retval 0 at45db available
176  * \retval -1 at45db not available
177  *
178  * \note No special settings are necessary to initialize
179  * the Flash memory. The standard data flash page size is
180  * 528Byte e.g. AT45DB161
181  */
182 int8_t at45db_init(void);
183 
184 /**
185  * \brief This function erases the whole chip
186  *
187  * \note The time to erase the whole chip can take
188  * up to 20sec!
189  */
190 void at45db_erase_chip(void);
191 
192 /**
193  * \brief This function erases one block (4 Kbytes)
194  *
195  * \param addr block address e.g. AT45DB161 (0 ... 511)
196  *
197  * \note The time to erase one block can take
198  * up to 45ms - 100ms!
199  */
200 void at45db_erase_block(uint16_t addr);
201 
202 /**
203  * \brief This function erases one page e.g. AT45DB161 (512 bytes)
204  *
205  * \param addr page address e.g. AT45DB161 (0 ... 4095)
206  *
207  * \note The time to erase one bock can take
208  * up to 15ms - 35ms!
209  */
210 void at45db_erase_page(uint16_t addr);
211 
212 /**
213  * \brief This function writes bytes to the active buffer, while
214  * the buffer management is done automatically.
215  *
216  * \param addr Byte address within the buffer e.g. AT45DB161 (0 ... 527)
217  * \param *buffer Pointer to local byte buffer
218  * \param bytes Number of bytes (e.g. byte buffer size) which have to
219  * be written to the active buffer
220  *
221  */
222 void at45db_write_buffer(uint16_t addr, uint8_t *buffer, uint16_t bytes);
223 
224 /**
225  * \brief This function copies the active buffer into the Flash
226  * EEPROM page. Moreover it switches the active buffer to avoid
227  * latency.
228  *
229  * \param addr page address e.g. AT45DB161 (0 ... 4095)
230  *
231  */
232 void at45db_buffer_to_page(uint16_t addr);
233 
234 /**
235  * \brief This function copies the data from the the pointer
236  * into the buffer, erases the EEPROM page and flashes the new
237  * content directly into the page
238  *
239  * \param p_addr page address e.g. AT45DB161 (0 - 4095)
240  * \param b_addr byte address within the page e.g. AT45DB161 (0 - 527)
241  * \param *buffer Pointer to local byte buffer
242  * \param bytes Number of bytes (e.g. byte buffer size) which have to
243  * be read to the local byte buffer
244  */
245 void at45db_write_page(uint16_t p_addr, uint16_t b_addr, uint8_t *buffer, uint16_t bytes);
246 
247 /**
248  * \brief Bytes can be read via buffer from a Flash EEPROM page. With this
249  * function you select the page, the start byte within the page and the
250  * number of bytes you want to read.
251  *
252  * \param p_addr page address e.g. AT45DB161 (0 - 4095)
253  * \param b_addr byte address within the page e.g. AT45DB161 (0 - 527)
254  * \param *buffer Pointer to local byte buffer
255  * \param bytes Number of bytes (e.g. byte buffer size) which have to
256  * be read to the local byte buffer
257  *
258  */
259 void at45db_read_page_buffered(uint16_t p_addr, uint16_t b_addr, uint8_t *buffer, uint16_t bytes);
260 
261 /**
262  * \brief Bytes can be read direct (bypassed) from a Flash EEPROM page. With this
263  * function you select the page, the start byte within the page and the
264  * number of bytes you want to read.
265  *
266  * \param p_addr page address e.g. AT45DB161 (0 - 4095)
267  * \param b_addr byte address within the page e.g. AT45DB161 (0 - 527)
268  * \param *buffer Pointer to local byte buffer
269  * \param bytes Number of bytes (e.g. byte buffer size) which have to
270  * be read to the local byte buffer
271  *
272  */
273 void at45db_read_page_bypassed(uint16_t p_addr, uint16_t b_addr, uint8_t *buffer, uint16_t bytes);
274 
275 /**
276  * \brief Copies the given page into the buffer 2.
277  * \note Only Buffer 2 is used to readout a page, because the read
278  * respectively transfer latency is only about 200us
279  *
280  * \param addr page address e.g. AT45DB161 (0 - 4095)
281  *
282  */
283 void at45db_page_to_buf(uint16_t addr);
284 
285 /**
286  * \brief This function readouts the buffer 2 data.
287  *
288  * \param b_addr byte address within the page e.g. AT45DB161 (0 - 527)
289  * \param *buffer Pointer to local byte buffer
290  * \param bytes Number of bytes (e.g. byte buffer size) which have to
291  * be read to the local byte buffer
292  *
293  */
294 void at45db_read_buffer(uint16_t b_addr, uint8_t *buffer, uint16_t bytes);
295 
296 /**
297  * \brief The command word of the AT45DBxx1 normally consists of 4 bytes.
298  * This function enables the chip select and sends the command (opcode +
299  * address information) to the AT45DBxx1.
300  *
301  * \param *cmd Pointer to the 4 byte command array
302  *
303  */
304 void at45db_write_cmd(uint8_t *cmd);
305 
306 /**
307  * \brief This function waits until the busy flag of the status register is set,
308  * to detect when the AT45DBxx1 device is ready to receive new commands
309  */
310 void at45db_busy_wait(void);
311 
312 /** @} */
313 /** @} */
314 
315 #endif /* FLASHAT45DB_H_ */