Add more include protection macros to give the user warnings when they try to manuall...
[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.fourwalledcubicle.com
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 *
33 * This file is the master dispatch header file for the board-specific dataflash driver, for boards containing
34 * dataflash ICs for external non-volatile storage.
35 *
36 * User code should include this file, which will in turn include the correct dataflash driver header file for
37 * the currently selected board.
38 *
39 * If the BOARD value is set to BOARD_USER, this will include the /Board/Dataflash.h file in the user project
40 * directory.
41 */
42
43 /** \ingroup Group_BoardDrivers
44 * @defgroup Group_Dataflash Dataflash Driver - LUFA/Drivers/Board/Dataflash.h
45 *
46 * \section Sec_Dependencies Module Source Dependencies
47 * The following files must be built with any user project that uses this module:
48 * - None
49 *
50 * \section Module Description
51 * Dataflash driver. This module provides an easy to use interface for the Dataflash ICs located on many boards,
52 * for the storage of large amounts of data into the Dataflash's non-volatile memory.
53 *
54 * If the BOARD value is set to BOARD_USER, this will include the /Board/Dataflash.h file in the user project
55 * directory. Otherwise, it will include the appropriate built in board driver header file.
56 *
57 * @{
58 */
59
60 #ifndef __DATAFLASH_H__
61 #define __DATAFLASH_H__
62
63 /* Macros: */
64 #if !defined(__DOXYGEN__)
65 #define __INCLUDE_FROM_DATAFLASH_H
66 #define INCLUDE_FROM_DATAFLASH_H
67 #endif
68
69 /* Includes: */
70 #include "../Peripheral/SPI.h"
71 #include "../../Common/Common.h"
72
73 /* Enable C linkage for C++ Compilers: */
74 #if defined(__cplusplus)
75 extern "C" {
76 #endif
77
78 /* Public Interface - May be used in end-application: */
79 /* Macros: */
80 #if !defined(__DOXYGEN__)
81 #define __GET_DATAFLASH_MASK2(x, y) x ## y
82 #define __GET_DATAFLASH_MASK(x) __GET_DATAFLASH_MASK2(DATAFLASH_CHIP,x)
83 #endif
84
85 /** Retrieves the Dataflash chip select mask for the given Dataflash chip index.
86 *
87 * \param[in] index Index of the dataflash chip mask to retrieve
88 *
89 * \return Mask for the given Dataflash chip's /CS pin
90 */
91 #define DATAFLASH_CHIP_MASK(index) __GET_DATAFLASH_MASK(index)
92
93 /* Pseudo-Function Macros: */
94 #if defined(__DOXYGEN__)
95 /** Determines the currently selected dataflash chip.
96 *
97 * \return Mask of the currently selected Dataflash chip, either \ref DATAFLASH_NO_CHIP if no chip is selected
98 * or a DATAFLASH_CHIPn mask (where n is the chip number).
99 */
100 static inline uint8_t Dataflash_GetSelectedChip(void);
101
102 /** Selects the given dataflash chip.
103 *
104 * \param[in] ChipMask Mask of the Dataflash IC to select, in the form of DATAFLASH_CHIPn mask (where n is
105 * the chip number).
106 */
107 static inline void Dataflash_SelectChip(uint8_t ChipMask);
108
109 /** Deselects the current dataflash chip, so that no dataflash is selected. */
110 static inline void Dataflash_DeselectChip(void);
111 #else
112 #define Dataflash_GetSelectedChip() (DATAFLASH_CHIPCS_PORT & DATAFLASH_CHIPCS_MASK)
113
114 #define Dataflash_SelectChip(mask) MACROS{ DATAFLASH_CHIPCS_PORT = ((DATAFLASH_CHIPCS_PORT \
115 & ~DATAFLASH_CHIPCS_MASK) | (mask)); }MACROE
116
117 #define Dataflash_DeselectChip() Dataflash_SelectChip(DATAFLASH_NO_CHIP)
118 #endif
119
120 /* Inline Functions: */
121 /** Sends a byte to the currently selected dataflash IC, and returns a byte from the dataflash.
122 *
123 * \param[in] Byte of data to send to the dataflash
124 *
125 * \return Last response byte from the dataflash
126 */
127 static inline uint8_t Dataflash_TransferByte(const uint8_t Byte) ATTR_ALWAYS_INLINE;
128 static inline uint8_t Dataflash_TransferByte(const uint8_t Byte)
129 {
130 return SPI_TransferByte(Byte);
131 }
132
133 /** Sends a byte to the currently selected dataflash IC, and ignores the next byte from the dataflash.
134 *
135 * \param[in] Byte of data to send to the dataflash
136 */
137 static inline void Dataflash_SendByte(const uint8_t Byte) ATTR_ALWAYS_INLINE;
138 static inline void Dataflash_SendByte(const uint8_t Byte)
139 {
140 SPI_SendByte(Byte);
141 }
142
143 /** Sends a dummy byte to the currently selected dataflash IC, and returns the next byte from the dataflash.
144 *
145 * \return Last response byte from the dataflash
146 */
147 static inline uint8_t Dataflash_ReceiveByte(void) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT;
148 static inline uint8_t Dataflash_ReceiveByte(void)
149 {
150 return SPI_ReceiveByte();
151 }
152
153 /* Includes: */
154 #if (BOARD == BOARD_NONE)
155 #error The Board Buttons driver cannot be used if the makefile BOARD option is not set.
156 #elif (BOARD == BOARD_USBKEY)
157 #include "USBKEY/Dataflash.h"
158 #elif (BOARD == BOARD_STK525)
159 #include "STK525/Dataflash.h"
160 #elif (BOARD == BOARD_STK526)
161 #include "STK526/Dataflash.h"
162 #elif (BOARD == BOARD_XPLAIN)
163 #include "XPLAIN/Dataflash.h"
164 #elif (BOARD == BOARD_XPLAIN_REV1)
165 #include "XPLAIN/Dataflash.h"
166 #elif (BOARD == BOARD_EVK527)
167 #include "EVK527/Dataflash.h"
168 #elif (BOARD == BOARD_USER)
169 #include "Board/Dataflash.h"
170 #else
171 #error The selected board does not contain a dataflash IC.
172 #endif
173
174 /* Inline Functions: */
175 /** Initializes the dataflash driver so that commands and data may be sent to an attached dataflash IC.
176 * The AVR's SPI driver MUST be initialized before any of the dataflash commands are used.
177 */
178 static inline void Dataflash_Init(void)
179 {
180 DATAFLASH_CHIPCS_DDR |= DATAFLASH_CHIPCS_MASK;
181 DATAFLASH_CHIPCS_PORT |= DATAFLASH_CHIPCS_MASK;
182 }
183
184 /** Toggles the select line of the currently selected dataflash IC, so that it is ready to receive
185 * a new command.
186 */
187 static inline void Dataflash_ToggleSelectedChipCS(void)
188 {
189 uint8_t SelectedChipMask = Dataflash_GetSelectedChip();
190
191 Dataflash_DeselectChip();
192 Dataflash_SelectChip(SelectedChipMask);
193 }
194
195 /** Spin-loops while the currently selected dataflash is busy executing a command, such as a main
196 * memory page program or main memory to buffer transfer.
197 */
198 static inline void Dataflash_WaitWhileBusy(void)
199 {
200 Dataflash_ToggleSelectedChipCS();
201 Dataflash_SendByte(DF_CMD_GETSTATUS);
202 while (!(Dataflash_ReceiveByte() & DF_STATUS_READY));
203 Dataflash_ToggleSelectedChipCS();
204 }
205
206 /** Selects a dataflash IC from the given page number, which should range from 0 to
207 * ((DATAFLASH_PAGES * DATAFLASH_TOTALCHIPS) - 1). For boards containing only one
208 * dataflash IC, this will select DATAFLASH_CHIP1. If the given page number is outside
209 * the total number of pages contained in the boards dataflash ICs, all dataflash ICs
210 * are deselected.
211 *
212 * \param[in] PageAddress Address of the page to manipulate, ranging from
213 * ((DATAFLASH_PAGES * DATAFLASH_TOTALCHIPS) - 1).
214 */
215 static inline void Dataflash_SelectChipFromPage(const uint16_t PageAddress);
216
217 /** Sends a set of page and buffer address bytes to the currently selected dataflash IC, for use with
218 * dataflash commands which require a complete 24-byte address.
219 *
220 * \param[in] PageAddress Page address within the selected dataflash IC
221 * \param[in] BufferByte Address within the dataflash's buffer
222 */
223 static inline void Dataflash_SendAddressBytes(uint16_t PageAddress, const uint16_t BufferByte);
224
225 /* Disable C linkage for C++ Compilers: */
226 #if defined(__cplusplus)
227 }
228 #endif
229
230 #endif
231
232 /** @} */