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