3 Copyright (C) Dean Camera, 2010.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com)
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.
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
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.
36 * User code should include this file, which will in turn include the correct dataflash driver header file for
37 * the currently selected board.
39 * If the BOARD value is set to BOARD_USER, this will include the /Board/Dataflash.h file in the user project
43 /** \ingroup Group_BoardDrivers
44 * @defgroup Group_Dataflash Dataflash Driver - LUFA/Drivers/Board/Dataflash.h
46 * \section Sec_Dependencies Module Source Dependencies
47 * The following files must be built with any user project that uses this module:
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.
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.
60 #ifndef __DATAFLASH_H__
61 #define __DATAFLASH_H__
64 #if !defined(__DOXYGEN__)
65 #define INCLUDE_FROM_DATAFLASH_H
69 #include "../Peripheral/SPI.h"
70 #include "../../Common/Common.h"
72 /* Enable C linkage for C++ Compilers: */
73 #if defined(__cplusplus)
77 /* Public Interface - May be used in end-application: */
79 #if !defined(__DOXYGEN__)
80 #define __GET_DATAFLASH_MASK2(x, y) x ## y
81 #define __GET_DATAFLASH_MASK(x) __GET_DATAFLASH_MASK2(DATAFLASH_CHIP,x)
84 /** Retrieves the Dataflash chip select mask for the given Dataflash chip index.
86 * \param[in] index Index of the dataflash chip mask to retrieve
88 * \return Mask for the given Dataflash chip's /CS pin
90 #define DATAFLASH_CHIP_MASK(index) __GET_DATAFLASH_MASK(index)
92 /* Pseudo-Function Macros: */
93 #if defined(__DOXYGEN__)
94 /** Determines the currently selected dataflash chip.
96 * \return Mask of the currently selected Dataflash chip, either \ref DATAFLASH_NO_CHIP if no chip is selected
97 * or a DATAFLASH_CHIPn mask (where n is the chip number).
99 static inline uint8_t Dataflash_GetSelectedChip(void);
101 /** Selects the given dataflash chip.
103 * \param[in] ChipMask Mask of the Dataflash IC to select, in the form of DATAFLASH_CHIPn mask (where n is
106 static inline void Dataflash_SelectChip(uint8_t ChipMask
);
108 /** Deselects the current dataflash chip, so that no dataflash is selected. */
109 static inline void Dataflash_DeselectChip(void);
111 #define Dataflash_GetSelectedChip() (DATAFLASH_CHIPCS_PORT & DATAFLASH_CHIPCS_MASK)
113 #define Dataflash_SelectChip(mask) MACROS{ DATAFLASH_CHIPCS_PORT = ((DATAFLASH_CHIPCS_PORT \
114 & ~DATAFLASH_CHIPCS_MASK) | (mask)); }MACROE
116 #define Dataflash_DeselectChip() Dataflash_SelectChip(DATAFLASH_NO_CHIP)
119 /* Inline Functions: */
120 /** Sends a byte to the currently selected dataflash IC, and returns a byte from the dataflash.
122 * \param[in] Byte of data to send to the dataflash
124 * \return Last response byte from the dataflash
126 static inline uint8_t Dataflash_TransferByte(const uint8_t Byte
) ATTR_ALWAYS_INLINE
;
127 static inline uint8_t Dataflash_TransferByte(const uint8_t Byte
)
129 return SPI_TransferByte(Byte
);
132 /** Sends a byte to the currently selected dataflash IC, and ignores the next byte from the dataflash.
134 * \param[in] Byte of data to send to the dataflash
136 static inline void Dataflash_SendByte(const uint8_t Byte
) ATTR_ALWAYS_INLINE
;
137 static inline void Dataflash_SendByte(const uint8_t Byte
)
142 /** Sends a dummy byte to the currently selected dataflash IC, and returns the next byte from the dataflash.
144 * \return Last response byte from the dataflash
146 static inline uint8_t Dataflash_ReceiveByte(void) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT
;
147 static inline uint8_t Dataflash_ReceiveByte(void)
149 return SPI_ReceiveByte();
154 #error BOARD must be set in makefile to a value specified in BoardTypes.h.
155 #elif (BOARD == BOARD_USBKEY)
156 #include "USBKEY/Dataflash.h"
157 #elif (BOARD == BOARD_STK525)
158 #include "STK525/Dataflash.h"
159 #elif (BOARD == BOARD_STK526)
160 #include "STK526/Dataflash.h"
161 #elif (BOARD == BOARD_XPLAIN)
162 #include "XPLAIN/Dataflash.h"
163 #elif (BOARD == BOARD_XPLAIN_REV1)
164 #include "XPLAIN/Dataflash.h"
165 #elif (BOARD == BOARD_EVK527)
166 #include "EVK527/Dataflash.h"
167 #elif (BOARD == BOARD_USER)
168 #include "Board/Dataflash.h"
170 #error The selected board does not contain a dataflash IC.
173 /* Inline Functions: */
174 /** Initializes the dataflash driver so that commands and data may be sent to an attached dataflash IC.
175 * The AVR's SPI driver MUST be initialized before any of the dataflash commands are used.
177 static inline void Dataflash_Init(void)
179 DATAFLASH_CHIPCS_DDR
|= DATAFLASH_CHIPCS_MASK
;
180 DATAFLASH_CHIPCS_PORT
|= DATAFLASH_CHIPCS_MASK
;
183 /** Toggles the select line of the currently selected dataflash IC, so that it is ready to receive
186 static inline void Dataflash_ToggleSelectedChipCS(void)
188 uint8_t SelectedChipMask
= Dataflash_GetSelectedChip();
190 Dataflash_DeselectChip();
191 Dataflash_SelectChip(SelectedChipMask
);
194 /** Spin-loops while the currently selected dataflash is busy executing a command, such as a main
195 * memory page program or main memory to buffer transfer.
197 static inline void Dataflash_WaitWhileBusy(void)
199 Dataflash_ToggleSelectedChipCS();
200 Dataflash_SendByte(DF_CMD_GETSTATUS
);
201 while (!(Dataflash_ReceiveByte() & DF_STATUS_READY
));
202 Dataflash_ToggleSelectedChipCS();
205 /** Selects a dataflash IC from the given page number, which should range from 0 to
206 * ((DATAFLASH_PAGES * DATAFLASH_TOTALCHIPS) - 1). For boards containing only one
207 * dataflash IC, this will select DATAFLASH_CHIP1. If the given page number is outside
208 * the total number of pages contained in the boards dataflash ICs, all dataflash ICs
211 * \param[in] PageAddress Address of the page to manipulate, ranging from
212 * ((DATAFLASH_PAGES * DATAFLASH_TOTALCHIPS) - 1).
214 static inline void Dataflash_SelectChipFromPage(const uint16_t PageAddress
);
216 /** Sends a set of page and buffer address bytes to the currently selected dataflash IC, for use with
217 * dataflash commands which require a complete 24-byte address.
219 * \param[in] PageAddress Page address within the selected dataflash IC
220 * \param[in] BufferByte Address within the dataflash's buffer
222 static inline void Dataflash_SendAddressBytes(uint16_t PageAddress
, const uint16_t BufferByte
);
224 /* Disable C linkage for C++ Compilers: */
225 #if defined(__cplusplus)