d443c8f5ace4a72b3abdc12cfde30d212317237e
[pub/USBasp.git] / LUFA / Drivers / Board / Dataflash.h
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2010.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12 Permission to use, copy, modify, distribute, and sell this
13 software and its documentation for any purpose is hereby granted
14 without fee, provided that the above copyright notice appear in
15 all copies and that both that the copyright notice and this
16 permission notice and warranty disclaimer appear in supporting
17 documentation, and that the name of the author not be used in
18 advertising or publicity pertaining to distribution of the
19 software without specific, written prior permission.
20
21 The author disclaim all warranties with regard to this
22 software, including all implied warranties of merchantability
23 and fitness. In no event shall the author be liable for any
24 special, indirect or consequential damages or any damages
25 whatsoever resulting from loss of use, data or profits, whether
26 in an action of contract, negligence or other tortious action,
27 arising out of or in connection with the use or performance of
28 this software.
29 */
30
31 /** \file
32 * \brief Master include file for the board dataflash IC driver.
33 *
34 * This file is the master dispatch header file for the board-specific dataflash driver, for boards containing
35 * dataflash ICs for external non-volatile storage.
36 *
37 * User code should include this file, which will in turn include the correct dataflash driver header file for
38 * the currently selected board.
39 *
40 * If the BOARD value is set to BOARD_USER, this will include the /Board/Dataflash.h file in the user project
41 * directory.
42 *
43 * For possible BOARD makefile values, see \ref Group_BoardTypes.
44 */
45
46 /** \ingroup Group_BoardDrivers
47 * @defgroup Group_Dataflash Dataflash Driver - LUFA/Drivers/Board/Dataflash.h
48 *
49 * \section Sec_Dependencies Module Source Dependencies
50 * The following files must be built with any user project that uses this module:
51 * - None
52 *
53 * \section Module Description
54 * Dataflash driver. This module provides an easy to use interface for the Dataflash ICs located on many boards,
55 * for the storage of large amounts of data into the Dataflash's non-volatile memory.
56 *
57 * If the BOARD value is set to BOARD_USER, this will include the /Board/Dataflash.h file in the user project
58 * directory. Otherwise, it will include the appropriate built in board driver header file.
59 *
60 * For possible BOARD makefile values, see \ref Group_BoardTypes.
61 *
62 * <b>Example Usage:</b>
63 * \code
64 * // Initialise the SPI and board Dataflash drivers before first use
65 * SPI_Init(SPI_SPEED_FCPU_DIV_2 | SPI_ORDER_MSB_FIRST | SPI_SCK_LEAD_FALLING |
66 * SPI_SAMPLE_TRAILING | SPI_MODE_MASTER);
67 * Dataflash_Init();
68 *
69 * uint8_t WriteBuffer[DATAFLASH_PAGE_SIZE];
70 * uint8_t ReadBuffer[DATAFLASH_PAGE_SIZE];
71 *
72 * // Fill page write buffer with a repeating pattern
73 * for (uint16_t i = 0; i < DATAFLASH_PAGE_SIZE; i++)
74 * WriteBuffer[i] = (i & 0xFF);
75 *
76 * // Must select the chip of interest first before operating on it
77 * Dataflash_SelectChip(DATAFLASH_CHIP1);
78 *
79 * // Write to the Dataflash's first internal memory buffer
80 * printf("Writing data to first dataflash buffer:\r\n");
81 * Dataflash_SendByte(DF_CMD_BUFF1WRITE);
82 * Dataflash_SendAddressBytes(0, 0);
83 *
84 * for (uint16_t i = 0; i < DATAFLASH_PAGE_SIZE; i++)
85 * Dataflash_SendByte(WriteBuffer[i]);
86 *
87 * // Commit the Dataflash's first memory buffer to the non-voltatile FLASH memory
88 * printf("Committing page to non-volatile memory page index 5:\r\n");
89 * Dataflash_SendByte(DF_CMD_BUFF1TOMAINMEMWITHERASE);
90 * Dataflash_SendAddressBytes(5, 0);
91 * Dataflash_WaitWhileBusy();
92 *
93 * // Read the page from non-voltatile FLASH memory into the Dataflash's second memory buffer
94 * printf("Reading data into second dataflash buffer:\r\n");
95 * Dataflash_SendByte(DF_CMD_MAINMEMTOBUFF2);
96 * Dataflash_SendAddressBytes(5, 0);
97 * Dataflash_WaitWhileBusy();
98 *
99 * // Read the Dataflash's second internal memory buffer
100 * Dataflash_SendByte(DF_CMD_BUFF2READ);
101 * Dataflash_SendAddressBytes(0, 0);
102 *
103 * for (uint16_t i = 0; i < DATAFLASH_PAGE_SIZE; i++)
104 * ReadBuffer[i] = Dataflash_ReceiveByte();
105 *
106 * // Deselect the chip after use
107 * Dataflash_DeselectChip();
108 * \endcode
109 *
110 * @{
111 */
112
113 #ifndef __DATAFLASH_H__
114 #define __DATAFLASH_H__
115
116 /* Macros: */
117 #if !defined(__DOXYGEN__)
118 #define __INCLUDE_FROM_DATAFLASH_H
119 #define INCLUDE_FROM_DATAFLASH_H
120 #endif
121
122 /* Includes: */
123 #include "../Peripheral/SPI.h"
124 #include "../../Common/Common.h"
125
126 /* Enable C linkage for C++ Compilers: */
127 #if defined(__cplusplus)
128 extern "C" {
129 #endif
130
131 /* Public Interface - May be used in end-application: */
132 /* Macros: */
133 #if !defined(__DOXYGEN__)
134 #define __GET_DATAFLASH_MASK2(x, y) x ## y
135 #define __GET_DATAFLASH_MASK(x) __GET_DATAFLASH_MASK2(DATAFLASH_CHIP,x)
136 #endif
137
138 /** Retrieves the Dataflash chip select mask for the given Dataflash chip index.
139 *
140 * \param[in] index Index of the dataflash chip mask to retrieve
141 *
142 * \return Mask for the given Dataflash chip's /CS pin
143 */
144 #define DATAFLASH_CHIP_MASK(index) __GET_DATAFLASH_MASK(index)
145
146 /* Inline Functions: */
147 /** Initialises the dataflash driver so that commands and data may be sent to an attached dataflash IC.
148 *
149 * \note The AVR's SPI driver must be initialized before any of the dataflash commands are used.
150 */
151 static inline void Dataflash_Init(void);
152
153 /** Determines the currently selected dataflash chip.
154 *
155 * \return Mask of the currently selected Dataflash chip, either \ref DATAFLASH_NO_CHIP if no chip is selected
156 * or a DATAFLASH_CHIPn mask (where n is the chip number).
157 */
158 static inline uint8_t Dataflash_GetSelectedChip(void) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT;
159
160 /** Selects the given dataflash chip.
161 *
162 * \param[in] ChipMask Mask of the Dataflash IC to select, in the form of DATAFLASH_CHIPn mask (where n is
163 * the chip number).
164 */
165 static inline void Dataflash_SelectChip(const uint8_t ChipMask) ATTR_ALWAYS_INLINE;
166
167 /** Deselects the current dataflash chip, so that no dataflash is selected. */
168 static inline void Dataflash_DeselectChip(void) ATTR_ALWAYS_INLINE;
169
170 /** Selects a dataflash IC from the given page number, which should range from 0 to
171 * ((DATAFLASH_PAGES * DATAFLASH_TOTALCHIPS) - 1). For boards containing only one
172 * dataflash IC, this will select DATAFLASH_CHIP1. If the given page number is outside
173 * the total number of pages contained in the boards dataflash ICs, all dataflash ICs
174 * are deselected.
175 *
176 * \param[in] PageAddress Address of the page to manipulate, ranging from
177 * ((DATAFLASH_PAGES * DATAFLASH_TOTALCHIPS) - 1).
178 */
179 static inline void Dataflash_SelectChipFromPage(const uint16_t PageAddress);
180
181 /** Toggles the select line of the currently selected dataflash IC, so that it is ready to receive
182 * a new command.
183 */
184 static inline void Dataflash_ToggleSelectedChipCS(void);
185
186 /** Spin-loops while the currently selected dataflash is busy executing a command, such as a main
187 * memory page program or main memory to buffer transfer.
188 */
189 static inline void Dataflash_WaitWhileBusy(void);
190
191 /** Sends a set of page and buffer address bytes to the currently selected dataflash IC, for use with
192 * dataflash commands which require a complete 24-byte address.
193 *
194 * \param[in] PageAddress Page address within the selected dataflash IC
195 * \param[in] BufferByte Address within the dataflash's buffer
196 */
197 static inline void Dataflash_SendAddressBytes(uint16_t PageAddress,
198 const uint16_t BufferByte);
199
200 /** Sends a byte to the currently selected dataflash IC, and returns a byte from the dataflash.
201 *
202 * \param[in] Byte of data to send to the dataflash
203 *
204 * \return Last response byte from the dataflash
205 */
206 static inline uint8_t Dataflash_TransferByte(const uint8_t Byte) ATTR_ALWAYS_INLINE;
207 static inline uint8_t Dataflash_TransferByte(const uint8_t Byte)
208 {
209 return SPI_TransferByte(Byte);
210 }
211
212 /** Sends a byte to the currently selected dataflash IC, and ignores the next byte from the dataflash.
213 *
214 * \param[in] Byte of data to send to the dataflash
215 */
216 static inline void Dataflash_SendByte(const uint8_t Byte) ATTR_ALWAYS_INLINE;
217 static inline void Dataflash_SendByte(const uint8_t Byte)
218 {
219 SPI_SendByte(Byte);
220 }
221
222 /** Sends a dummy byte to the currently selected dataflash IC, and returns the next byte from the dataflash.
223 *
224 * \return Last response byte from the dataflash
225 */
226 static inline uint8_t Dataflash_ReceiveByte(void) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT;
227 static inline uint8_t Dataflash_ReceiveByte(void)
228 {
229 return SPI_ReceiveByte();
230 }
231
232 /* Includes: */
233 #if (BOARD == BOARD_NONE)
234 #error The Board Buttons driver cannot be used if the makefile BOARD option is not set.
235 #elif (BOARD == BOARD_USBKEY)
236 #include "USBKEY/Dataflash.h"
237 #elif (BOARD == BOARD_STK525)
238 #include "STK525/Dataflash.h"
239 #elif (BOARD == BOARD_STK526)
240 #include "STK526/Dataflash.h"
241 #elif (BOARD == BOARD_XPLAIN)
242 #include "XPLAIN/Dataflash.h"
243 #elif (BOARD == BOARD_XPLAIN_REV1)
244 #include "XPLAIN/Dataflash.h"
245 #elif (BOARD == BOARD_EVK527)
246 #include "EVK527/Dataflash.h"
247 #elif (BOARD == BOARD_USER)
248 #include "Board/Dataflash.h"
249 #else
250 #error The selected board does not contain a dataflash IC.
251 #endif
252
253 /* Disable C linkage for C++ Compilers: */
254 #if defined(__cplusplus)
255 }
256 #endif
257
258 #endif
259
260 /** @} */
261