3 Copyright (C) Dean Camera, 2011.
5 dean [at] fourwalledcubicle [dot] com
10 Copyright 2011 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
32 * \brief Master include file for the board dataflash IC driver.
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.
37 * User code should include this file, which will in turn include the correct dataflash driver header file for
38 * the currently selected board.
40 * If the \c BOARD value is set to \c BOARD_USER, this will include the \c /Board/Dataflash.h file in the user project
43 * For possible \c BOARD makefile values, see \ref Group_BoardTypes.
46 /** \ingroup Group_BoardDrivers
47 * @defgroup Group_Dataflash Dataflash Driver - LUFA/Drivers/Board/Dataflash.h
49 * \section Sec_Dependencies Module Source Dependencies
50 * The following files must be built with any user project that uses this module:
53 * \section Sec_ModDescription 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.
57 * If the \c BOARD value is set to \c BOARD_USER, this will include the \c /Board/Dataflash.h file in the user project
58 * directory. Otherwise, it will include the appropriate built in board driver header file.
60 * For possible \c BOARD makefile values, see \ref Group_BoardTypes.
62 * \section Sec_ExampleUsage Example Usage
63 * The following snippet is an example of how this module may be used within a typical
67 * // Initialise the SPI and board Dataflash drivers before first use
68 * SPI_Init(SPI_SPEED_FCPU_DIV_2 | SPI_ORDER_MSB_FIRST | SPI_SCK_LEAD_FALLING |
69 * SPI_SAMPLE_TRAILING | SPI_MODE_MASTER);
72 * uint8_t WriteBuffer[DATAFLASH_PAGE_SIZE];
73 * uint8_t ReadBuffer[DATAFLASH_PAGE_SIZE];
75 * // Fill page write buffer with a repeating pattern
76 * for (uint16_t i = 0; i < DATAFLASH_PAGE_SIZE; i++)
77 * WriteBuffer[i] = (i & 0xFF);
79 * // Must select the chip of interest first before operating on it
80 * Dataflash_SelectChip(DATAFLASH_CHIP1);
82 * // Write to the Dataflash's first internal memory buffer
83 * printf("Writing data to first dataflash buffer:\r\n");
84 * Dataflash_SendByte(DF_CMD_BUFF1WRITE);
85 * Dataflash_SendAddressBytes(0, 0);
87 * for (uint16_t i = 0; i < DATAFLASH_PAGE_SIZE; i++)
88 * Dataflash_SendByte(WriteBuffer[i]);
90 * // Commit the Dataflash's first memory buffer to the non-voltatile FLASH memory
91 * printf("Committing page to non-volatile memory page index 5:\r\n");
92 * Dataflash_SendByte(DF_CMD_BUFF1TOMAINMEMWITHERASE);
93 * Dataflash_SendAddressBytes(5, 0);
94 * Dataflash_WaitWhileBusy();
96 * // Read the page from non-voltatile FLASH memory into the Dataflash's second memory buffer
97 * printf("Reading data into second dataflash buffer:\r\n");
98 * Dataflash_SendByte(DF_CMD_MAINMEMTOBUFF2);
99 * Dataflash_SendAddressBytes(5, 0);
100 * Dataflash_WaitWhileBusy();
102 * // Read the Dataflash's second internal memory buffer
103 * Dataflash_SendByte(DF_CMD_BUFF2READ);
104 * Dataflash_SendAddressBytes(0, 0);
106 * for (uint16_t i = 0; i < DATAFLASH_PAGE_SIZE; i++)
107 * ReadBuffer[i] = Dataflash_ReceiveByte();
109 * // Deselect the chip after use
110 * Dataflash_DeselectChip();
116 #ifndef __DATAFLASH_H__
117 #define __DATAFLASH_H__
120 #if !defined(__DOXYGEN__)
121 #define __INCLUDE_FROM_DATAFLASH_H
122 #define INCLUDE_FROM_DATAFLASH_H
126 #include "../Peripheral/SPI.h"
127 #include "../../Common/Common.h"
129 /* Enable C linkage for C++ Compilers: */
130 #if defined(__cplusplus)
134 /* Public Interface - May be used in end-application: */
136 #if !defined(__DOXYGEN__)
137 #define __GET_DATAFLASH_MASK2(x, y) x ## y
138 #define __GET_DATAFLASH_MASK(x) __GET_DATAFLASH_MASK2(DATAFLASH_CHIP,x)
141 /** Retrieves the Dataflash chip select mask for the given Dataflash chip index.
143 * \param[in] index Index of the dataflash chip mask to retrieve
145 * \return Mask for the given Dataflash chip's /CS pin
147 #define DATAFLASH_CHIP_MASK(index) __GET_DATAFLASH_MASK(index)
149 /* Inline Functions: */
150 /** Initialises the dataflash driver so that commands and data may be sent to an attached dataflash IC.
152 * \note The AVR's SPI driver must be initialized before any of the dataflash commands are used.
154 static inline void Dataflash_Init(void);
156 /** Determines the currently selected dataflash chip.
158 * \return Mask of the currently selected Dataflash chip, either \ref DATAFLASH_NO_CHIP if no chip is selected
159 * or a \c DATAFLASH_CHIPn mask (where n is the chip number).
161 static inline uint8_t Dataflash_GetSelectedChip(void) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT
;
163 /** Selects the given dataflash chip.
165 * \param[in] ChipMask Mask of the Dataflash IC to select, in the form of \c DATAFLASH_CHIPn mask (where n is
168 static inline void Dataflash_SelectChip(const uint8_t ChipMask
) ATTR_ALWAYS_INLINE
;
170 /** Deselects the current dataflash chip, so that no dataflash is selected. */
171 static inline void Dataflash_DeselectChip(void) ATTR_ALWAYS_INLINE
;
173 /** Selects a dataflash IC from the given page number, which should range from 0 to
174 * ((DATAFLASH_PAGES * DATAFLASH_TOTALCHIPS) - 1). For boards containing only one
175 * dataflash IC, this will select \ref DATAFLASH_CHIP1. If the given page number is outside
176 * the total number of pages contained in the boards dataflash ICs, all dataflash ICs
179 * \param[in] PageAddress Address of the page to manipulate, ranging from
180 * ((DATAFLASH_PAGES * DATAFLASH_TOTALCHIPS) - 1).
182 static inline void Dataflash_SelectChipFromPage(const uint16_t PageAddress
);
184 /** Toggles the select line of the currently selected dataflash IC, so that it is ready to receive
187 static inline void Dataflash_ToggleSelectedChipCS(void);
189 /** Spin-loops while the currently selected dataflash is busy executing a command, such as a main
190 * memory page program or main memory to buffer transfer.
192 static inline void Dataflash_WaitWhileBusy(void);
194 /** Sends a set of page and buffer address bytes to the currently selected dataflash IC, for use with
195 * dataflash commands which require a complete 24-byte address.
197 * \param[in] PageAddress Page address within the selected dataflash IC
198 * \param[in] BufferByte Address within the dataflash's buffer
200 static inline void Dataflash_SendAddressBytes(uint16_t PageAddress
,
201 const uint16_t BufferByte
);
203 /** Sends a byte to the currently selected dataflash IC, and returns a byte from the dataflash.
205 * \param[in] Byte of data to send to the dataflash
207 * \return Last response byte from the dataflash
209 static inline uint8_t Dataflash_TransferByte(const uint8_t Byte
) ATTR_ALWAYS_INLINE
;
210 static inline uint8_t Dataflash_TransferByte(const uint8_t Byte
)
212 return SPI_TransferByte(Byte
);
215 /** Sends a byte to the currently selected dataflash IC, and ignores the next byte from the dataflash.
217 * \param[in] Byte of data to send to the dataflash
219 static inline void Dataflash_SendByte(const uint8_t Byte
) ATTR_ALWAYS_INLINE
;
220 static inline void Dataflash_SendByte(const uint8_t Byte
)
225 /** Sends a dummy byte to the currently selected dataflash IC, and returns the next byte from the dataflash.
227 * \return Last response byte from the dataflash
229 static inline uint8_t Dataflash_ReceiveByte(void) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT
;
230 static inline uint8_t Dataflash_ReceiveByte(void)
232 return SPI_ReceiveByte();
236 #if (BOARD == BOARD_NONE)
237 #error The Board Dataflash driver cannot be used if the makefile BOARD option is not set.
238 #elif (BOARD == BOARD_USBKEY)
239 #include "USBKEY/Dataflash.h"
240 #elif (BOARD == BOARD_STK525)
241 #include "STK525/Dataflash.h"
242 #elif (BOARD == BOARD_STK526)
243 #include "STK526/Dataflash.h"
244 #elif (BOARD == BOARD_XPLAIN)
245 #include "XPLAIN/Dataflash.h"
246 #elif (BOARD == BOARD_XPLAIN_REV1)
247 #include "XPLAIN/Dataflash.h"
248 #elif (BOARD == BOARD_EVK527)
249 #include "EVK527/Dataflash.h"
250 #elif (BOARD == BOARD_USER)
251 #include "Board/Dataflash.h"
253 #error The selected board does not contain a dataflash IC.
256 /* Disable C linkage for C++ Compilers: */
257 #if defined(__cplusplus)