Commit for the 090810 release.
[pub/USBasp.git] / LUFA / Drivers / Board / Dataflash.h
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2009.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12 Permission to use, copy, modify, and distribute this software
13 and its documentation for any purpose and without fee is hereby
14 granted, provided that the above copyright notice appear in all
15 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_BOARD_DRIVER
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 index Index of the dataflash chip mask to retrieve
88 */
89 #define DATAFLASH_CHIP_MASK(index) __GET_DATAFLASH_MASK(index)
90
91 /* Pseudo-Function Macros: */
92 #if defined(__DOXYGEN__)
93 /** Determines the currently selected dataflash chip.
94 *
95 * \return Mask of the currently selected Dataflash chip, either \ref DATAFLASH_NO_CHIP if no chip is selected
96 * or a DATAFLASH_CHIPn mask (where n is the chip number).
97 */
98 static inline uint8_t Dataflash_GetSelectedChip(void);
99
100 /** Selects the given dataflash chip.
101 *
102 * \param[in] ChipMask Mask of the Dataflash IC to select, in the form of DATAFLASH_CHIPn mask (where n is
103 * the chip number).
104 */
105 static inline void Dataflash_SelectChip(uint8_t ChipMask);
106
107 /** Deselects the current dataflash chip, so that no dataflash is selected. */
108 static inline void Dataflash_DeselectChip(void);
109 #else
110 #define Dataflash_GetSelectedChip() (DATAFLASH_CHIPCS_PORT & DATAFLASH_CHIPCS_MASK)
111
112 #define Dataflash_SelectChip(mask) MACROS{ DATAFLASH_CHIPCS_PORT = ((DATAFLASH_CHIPCS_PORT \
113 & ~DATAFLASH_CHIPCS_MASK) | mask); }MACROE
114
115 #define Dataflash_DeselectChip() Dataflash_SelectChip(DATAFLASH_NO_CHIP)
116 #endif
117
118 /* Inline Functions: */
119 /** Sends a byte to the currently selected dataflash IC, and returns a byte from the dataflash.
120 *
121 * \param[in] Byte of data to send to the dataflash
122 *
123 * \return Last response byte from the dataflash
124 */
125 static inline uint8_t Dataflash_TransferByte(const uint8_t Byte) ATTR_ALWAYS_INLINE;
126 static inline uint8_t Dataflash_TransferByte(const uint8_t Byte)
127 {
128 return SPI_TransferByte(Byte);
129 }
130
131 /** Sends a byte to the currently selected dataflash IC, and ignores the next byte from the dataflash.
132 *
133 * \param[in] Byte of data to send to the dataflash
134 */
135 static inline void Dataflash_SendByte(const uint8_t Byte) ATTR_ALWAYS_INLINE;
136 static inline void Dataflash_SendByte(const uint8_t Byte)
137 {
138 SPI_SendByte(Byte);
139 }
140
141 /** Sends a dummy byte to the currently selected dataflash IC, and returns the next byte from the dataflash.
142 *
143 * \return Last response byte from the dataflash
144 */
145 static inline uint8_t Dataflash_ReceiveByte(void) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT;
146 static inline uint8_t Dataflash_ReceiveByte(void)
147 {
148 return SPI_ReceiveByte();
149 }
150
151 /* Includes: */
152 #if !defined(BOARD)
153 #error BOARD must be set in makefile to a value specified in BoardTypes.h.
154 #elif (BOARD == BOARD_USBKEY)
155 #include "USBKEY/Dataflash.h"
156 #elif (BOARD == BOARD_STK525)
157 #include "STK525/Dataflash.h"
158 #elif (BOARD == BOARD_STK526)
159 #include "STK526/Dataflash.h"
160 #elif (BOARD == BOARD_USER)
161 #include "Board/Dataflash.h"
162 #else
163 #error The selected board does not contain a dataflash IC.
164 #endif
165
166 /* Inline Functions: */
167 /** Initializes the dataflash driver (including the SPI driver) so that commands and data may be
168 * sent to an attached dataflash IC.
169 *
170 * \param[in] PrescalerMask SPI prescaler mask, see SPI.h documentation
171 */
172 static inline void Dataflash_Init(const uint8_t PrescalerMask)
173 {
174 DATAFLASH_CHIPCS_DDR |= DATAFLASH_CHIPCS_MASK;
175 DATAFLASH_CHIPCS_PORT |= DATAFLASH_CHIPCS_MASK;
176
177 SPI_Init(PrescalerMask, true);
178 }
179
180 /** Toggles the select line of the currently selected dataflash IC, so that it is ready to receive
181 * a new command.
182 */
183 static inline void Dataflash_ToggleSelectedChipCS(void)
184 {
185 uint8_t SelectedChipMask = Dataflash_GetSelectedChip();
186
187 Dataflash_DeselectChip();
188 Dataflash_SelectChip(SelectedChipMask);
189 }
190
191 /** Spinloops while the currently selected dataflash is busy executing a command, such as a main
192 * memory page program or main memory to buffer transfer.
193 */
194 static inline void Dataflash_WaitWhileBusy(void)
195 {
196 Dataflash_ToggleSelectedChipCS();
197 Dataflash_SendByte(DF_CMD_GETSTATUS);
198 while (!(Dataflash_ReceiveByte() & DF_STATUS_READY));
199 Dataflash_ToggleSelectedChipCS();
200 }
201
202 /** Selects a dataflash IC from the given page number, which should range from 0 to
203 * ((DATAFLASH_PAGES * DATAFLASH_TOTALCHIPS) - 1). For boards containing only one
204 * dataflash IC, this will select DATAFLASH_CHIP1. If the given page number is outside
205 * the total number of pages contained in the boards dataflash ICs, all dataflash ICs
206 * are deselected.
207 *
208 * \param[in] PageAddress Address of the page to manipulate, ranging from
209 * ((DATAFLASH_PAGES * DATAFLASH_TOTALCHIPS) - 1).
210 */
211 static inline void Dataflash_SelectChipFromPage(const uint16_t PageAddress);
212
213 /** Sends a set of page and buffer address bytes to the currently selected dataflash IC, for use with
214 * dataflash commands which require a complete 24-byte address.
215 *
216 * \param[in] PageAddress Page address within the selected dataflash IC
217 * \param[in] BufferByte Address within the dataflash's buffer
218 */
219 static inline void Dataflash_SendAddressBytes(uint16_t PageAddress, const uint16_t BufferByte);
220
221 /* Disable C linkage for C++ Compilers: */
222 #if defined(__cplusplus)
223 }
224 #endif
225
226 #endif
227
228 /** @} */