150dca1c287c686018fe1176ed47ca5ab5f5b4db
[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 * \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 * @{
63 */
64
65 #ifndef __DATAFLASH_H__
66 #define __DATAFLASH_H__
67
68 /* Macros: */
69 #if !defined(__DOXYGEN__)
70 #define __INCLUDE_FROM_DATAFLASH_H
71 #define INCLUDE_FROM_DATAFLASH_H
72 #endif
73
74 /* Includes: */
75 #include "../Peripheral/SPI.h"
76 #include "../../Common/Common.h"
77
78 /* Enable C linkage for C++ Compilers: */
79 #if defined(__cplusplus)
80 extern "C" {
81 #endif
82
83 /* Public Interface - May be used in end-application: */
84 /* Macros: */
85 #if !defined(__DOXYGEN__)
86 #define __GET_DATAFLASH_MASK2(x, y) x ## y
87 #define __GET_DATAFLASH_MASK(x) __GET_DATAFLASH_MASK2(DATAFLASH_CHIP,x)
88 #endif
89
90 /** Retrieves the Dataflash chip select mask for the given Dataflash chip index.
91 *
92 * \param[in] index Index of the dataflash chip mask to retrieve
93 *
94 * \return Mask for the given Dataflash chip's /CS pin
95 */
96 #define DATAFLASH_CHIP_MASK(index) __GET_DATAFLASH_MASK(index)
97
98 /* Inline Functions: */
99 /** Initialises the dataflash driver so that commands and data may be sent to an attached dataflash IC.
100 * The AVR's SPI driver MUST be initialized before any of the dataflash commands are used.
101 */
102 static inline void Dataflash_Init(void);
103
104 /** Determines the currently selected dataflash chip.
105 *
106 * \return Mask of the currently selected Dataflash chip, either \ref DATAFLASH_NO_CHIP if no chip is selected
107 * or a DATAFLASH_CHIPn mask (where n is the chip number).
108 */
109 static inline uint8_t Dataflash_GetSelectedChip(void) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT;
110
111 /** Selects the given dataflash chip.
112 *
113 * \param[in] ChipMask Mask of the Dataflash IC to select, in the form of DATAFLASH_CHIPn mask (where n is
114 * the chip number).
115 */
116 static inline void Dataflash_SelectChip(const uint8_t ChipMask) ATTR_ALWAYS_INLINE;
117
118 /** Deselects the current dataflash chip, so that no dataflash is selected. */
119 static inline void Dataflash_DeselectChip(void) ATTR_ALWAYS_INLINE;
120
121 /** Selects a dataflash IC from the given page number, which should range from 0 to
122 * ((DATAFLASH_PAGES * DATAFLASH_TOTALCHIPS) - 1). For boards containing only one
123 * dataflash IC, this will select DATAFLASH_CHIP1. If the given page number is outside
124 * the total number of pages contained in the boards dataflash ICs, all dataflash ICs
125 * are deselected.
126 *
127 * \param[in] PageAddress Address of the page to manipulate, ranging from
128 * ((DATAFLASH_PAGES * DATAFLASH_TOTALCHIPS) - 1).
129 */
130 static inline void Dataflash_SelectChipFromPage(const uint16_t PageAddress);
131
132 /** Toggles the select line of the currently selected dataflash IC, so that it is ready to receive
133 * a new command.
134 */
135 static inline void Dataflash_ToggleSelectedChipCS(void);
136
137 /** Spin-loops while the currently selected dataflash is busy executing a command, such as a main
138 * memory page program or main memory to buffer transfer.
139 */
140 static inline void Dataflash_WaitWhileBusy(void);
141
142 /** Sends a set of page and buffer address bytes to the currently selected dataflash IC, for use with
143 * dataflash commands which require a complete 24-byte address.
144 *
145 * \param[in] PageAddress Page address within the selected dataflash IC
146 * \param[in] BufferByte Address within the dataflash's buffer
147 */
148 static inline void Dataflash_SendAddressBytes(uint16_t PageAddress,
149 const uint16_t BufferByte);
150
151 /** Sends a byte to the currently selected dataflash IC, and returns a byte from the dataflash.
152 *
153 * \param[in] Byte of data to send to the dataflash
154 *
155 * \return Last response byte from the dataflash
156 */
157 static inline uint8_t Dataflash_TransferByte(const uint8_t Byte) ATTR_ALWAYS_INLINE;
158 static inline uint8_t Dataflash_TransferByte(const uint8_t Byte)
159 {
160 return SPI_TransferByte(Byte);
161 }
162
163 /** Sends a byte to the currently selected dataflash IC, and ignores the next byte from the dataflash.
164 *
165 * \param[in] Byte of data to send to the dataflash
166 */
167 static inline void Dataflash_SendByte(const uint8_t Byte) ATTR_ALWAYS_INLINE;
168 static inline void Dataflash_SendByte(const uint8_t Byte)
169 {
170 SPI_SendByte(Byte);
171 }
172
173 /** Sends a dummy byte to the currently selected dataflash IC, and returns the next byte from the dataflash.
174 *
175 * \return Last response byte from the dataflash
176 */
177 static inline uint8_t Dataflash_ReceiveByte(void) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT;
178 static inline uint8_t Dataflash_ReceiveByte(void)
179 {
180 return SPI_ReceiveByte();
181 }
182
183 /* Includes: */
184 #if (BOARD == BOARD_NONE)
185 #error The Board Buttons driver cannot be used if the makefile BOARD option is not set.
186 #elif (BOARD == BOARD_USBKEY)
187 #include "USBKEY/Dataflash.h"
188 #elif (BOARD == BOARD_STK525)
189 #include "STK525/Dataflash.h"
190 #elif (BOARD == BOARD_STK526)
191 #include "STK526/Dataflash.h"
192 #elif (BOARD == BOARD_XPLAIN)
193 #include "XPLAIN/Dataflash.h"
194 #elif (BOARD == BOARD_XPLAIN_REV1)
195 #include "XPLAIN/Dataflash.h"
196 #elif (BOARD == BOARD_EVK527)
197 #include "EVK527/Dataflash.h"
198 #elif (BOARD == BOARD_USER)
199 #include "Board/Dataflash.h"
200 #else
201 #error The selected board does not contain a dataflash IC.
202 #endif
203
204 /* Disable C linkage for C++ Compilers: */
205 #if defined(__cplusplus)
206 }
207 #endif
208
209 #endif
210
211 /** @} */
212