3 Copyright (C) Dean Camera, 2009.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
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.
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 * Functions, macros, variables, enums and types related to the control of board Dataflash ICs.
51 #ifndef __DATAFLASH_H__
52 #define __DATAFLASH_H__
55 #if !defined(__DOXYGEN__)
56 #define INCLUDE_FROM_DATAFLASH_H
57 #define INCLUDE_FROM_BOARD_DRIVER
61 #include "../AT90USBXXX/SPI.h"
62 #include "../../Common/Common.h"
64 /* Enable C linkage for C++ Compilers: */
65 #if defined(__cplusplus)
69 /* Public Interface - May be used in end-application: */
70 /* Psuedo-Function Macros: */
71 #if defined(__DOXYGEN__)
72 /** Determines the currently selected dataflash chip.
74 * \return Mask of the currently selected Dataflash chip, either DATAFLASH_NO_CHIP if no chip is selected
75 * or a DATAFLASH_CHIPn mask (where n is the chip number).
77 static inline uint8_t Dataflash_GetSelectedChip(void);
79 /** Selects the given dataflash chip.
81 * \param ChipMask Mask of the Dataflash IC to select, in the form of DATAFLASH_CHIPn mask (where n is
84 static inline void Dataflash_SelectChip(uint8_t ChipMask
);
86 /** Deselects the current dataflash chip, so that no dataflash is selected. */
87 static inline void Dataflash_DeselectChip(void);
89 #define Dataflash_GetSelectedChip() (DATAFLASH_CHIPCS_PORT & DATAFLASH_CHIPCS_MASK)
91 #define Dataflash_SelectChip(mask) MACROS{ DATAFLASH_CHIPCS_PORT = ((DATAFLASH_CHIPCS_PORT \
92 & ~DATAFLASH_CHIPCS_MASK) | mask); }MACROE
94 #define Dataflash_DeselectChip() Dataflash_SelectChip(DATAFLASH_NO_CHIP)
97 /* Inline Functions: */
98 /** Sends a byte to the currently selected dataflash IC, and returns a byte from the dataflash.
100 * \param Byte of data to send to the dataflash
102 * \return Last response byte from the dataflash
104 static inline uint8_t Dataflash_TransferByte(const uint8_t Byte
) ATTR_ALWAYS_INLINE
;
105 static inline uint8_t Dataflash_TransferByte(const uint8_t Byte
)
107 return SPI_TransferByte(Byte
);
110 /** Sends a byte to the currently selected dataflash IC, and ignores the next byte from the dataflash.
112 * \param Byte of data to send to the dataflash
114 static inline void Dataflash_SendByte(const uint8_t Byte
) ATTR_ALWAYS_INLINE
;
115 static inline void Dataflash_SendByte(const uint8_t Byte
)
120 /** Sends a dummy byte to the currently selected dataflash IC, and returns the next byte from the dataflash.
122 * \return Last response byte from the dataflash
124 static inline uint8_t Dataflash_ReceiveByte(void) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT
;
125 static inline uint8_t Dataflash_ReceiveByte(void)
127 return SPI_ReceiveByte();
132 #error BOARD must be set in makefile to a value specified in BoardTypes.h.
133 #elif (BOARD == BOARD_USBKEY)
134 #include "USBKEY/Dataflash.h"
135 #elif (BOARD == BOARD_STK525)
136 #include "STK525/Dataflash.h"
137 #elif (BOARD == BOARD_STK526)
138 #include "STK526/Dataflash.h"
139 #elif (BOARD == BOARD_USER)
140 #include "Board/Dataflash.h"
142 #error The selected board does not contain a dataflash IC.
145 /* Inline Functions: */
146 /** Initializes the dataflash driver (including the SPI driver) so that commands and data may be
147 * sent to an attached dataflash IC.
149 * \param PrescalerMask SPI prescaler mask, see SPI.h documentation
151 static inline void Dataflash_Init(const uint8_t PrescalerMask
)
153 DATAFLASH_CHIPCS_DDR
|= DATAFLASH_CHIPCS_MASK
;
154 DATAFLASH_CHIPCS_PORT
|= DATAFLASH_CHIPCS_MASK
;
156 SPI_Init(PrescalerMask
, true);
159 /** Toggles the select line of the currently selected dataflash IC, so that it is ready to receive
162 static inline void Dataflash_ToggleSelectedChipCS(void)
164 uint8_t SelectedChipMask
= Dataflash_GetSelectedChip();
166 Dataflash_DeselectChip();
167 Dataflash_SelectChip(SelectedChipMask
);
170 /** Spinloops while the currently selected dataflash is busy executing a command, such as a main
171 * memory page program or main memory to buffer transfer.
173 static inline void Dataflash_WaitWhileBusy(void)
175 Dataflash_ToggleSelectedChipCS();
176 Dataflash_SendByte(DF_CMD_GETSTATUS
);
177 while (!(Dataflash_ReceiveByte() & DF_STATUS_READY
));
180 /** Selects a dataflash IC from the given page number, which should range from 0 to
181 * ((DATAFLASH_PAGES * DATAFLASH_TOTALCHIPS) - 1). For boards containing only one
182 * dataflash IC, this will select DATAFLASH_CHIP1. If the given page number is outside
183 * the total number of pages contained in the boards dataflash ICs, all dataflash ICs
186 * \param PageAddress Address of the page to manipulate, ranging from
187 * ((DATAFLASH_PAGES * DATAFLASH_TOTALCHIPS) - 1).
189 static inline void Dataflash_SelectChipFromPage(const uint16_t PageAddress
);
191 /** Sends a set of page and buffer address bytes to the currently selected dataflash IC, for use with
192 * dataflash commands which require a complete 24-byte address.
194 * \param PageAddress Page address within the selected dataflash IC
195 * \param BufferByte Address within the dataflash's buffer
197 static inline void Dataflash_SendAddressBytes(uint16_t PageAddress
, const uint16_t BufferByte
);
199 /* Disable C linkage for C++ Compilers: */
200 #if defined(__cplusplus)