Fixed incorrect HID interface class and subclass values in the Mouse and KeyboardMous...
[pub/USBasp.git] / LUFA / Drivers / Board / Dataflash.h
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2009.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
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.
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 *
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.
35 *
36 * User code should include this file, which will in turn include the correct dataflash driver header file for
37 * the currently selected board.
38 *
39 * If the BOARD value is set to BOARD_USER, this will include the /Board/Dataflash.h file in the user project
40 * directory.
41 */
42
43 /** \ingroup Group_BoardDrivers
44 * @defgroup Group_Dataflash Dataflash Driver - LUFA/Drivers/Board/Dataflash.h
45 *
46 * \section Sec_Dependencies Module Source Dependencies
47 * The following files must be built with any user project that uses this module:
48 * - None
49 *
50 * \section Module Description
51 * Functions, macros, variables, enums and types related to the control of board Dataflash ICs.
52 *
53 * If the BOARD value is set to BOARD_USER, this will include the /Board/Dataflash.h file in the user project
54 * directory. Otherwise, it will include the appropriate built in board driver header file.
55 *
56 * @{
57 */
58
59 #ifndef __DATAFLASH_H__
60 #define __DATAFLASH_H__
61
62 /* Macros: */
63 #if !defined(__DOXYGEN__)
64 #define INCLUDE_FROM_DATAFLASH_H
65 #define INCLUDE_FROM_BOARD_DRIVER
66 #endif
67
68 /* Includes: */
69 #include "../Peripheral/SPI.h"
70 #include "../../Common/Common.h"
71
72 /* Enable C linkage for C++ Compilers: */
73 #if defined(__cplusplus)
74 extern "C" {
75 #endif
76
77 /* Public Interface - May be used in end-application: */
78 /* Pseudo-Function Macros: */
79 #if defined(__DOXYGEN__)
80 /** Determines the currently selected dataflash chip.
81 *
82 * \return Mask of the currently selected Dataflash chip, either DATAFLASH_NO_CHIP if no chip is selected
83 * or a DATAFLASH_CHIPn mask (where n is the chip number).
84 */
85 static inline uint8_t Dataflash_GetSelectedChip(void);
86
87 /** Selects the given dataflash chip.
88 *
89 * \param ChipMask Mask of the Dataflash IC to select, in the form of DATAFLASH_CHIPn mask (where n is
90 * the chip number).
91 */
92 static inline void Dataflash_SelectChip(uint8_t ChipMask);
93
94 /** Deselects the current dataflash chip, so that no dataflash is selected. */
95 static inline void Dataflash_DeselectChip(void);
96 #else
97 #define Dataflash_GetSelectedChip() (DATAFLASH_CHIPCS_PORT & DATAFLASH_CHIPCS_MASK)
98
99 #define Dataflash_SelectChip(mask) MACROS{ DATAFLASH_CHIPCS_PORT = ((DATAFLASH_CHIPCS_PORT \
100 & ~DATAFLASH_CHIPCS_MASK) | mask); }MACROE
101
102 #define Dataflash_DeselectChip() Dataflash_SelectChip(DATAFLASH_NO_CHIP)
103 #endif
104
105 /* Inline Functions: */
106 /** Sends a byte to the currently selected dataflash IC, and returns a byte from the dataflash.
107 *
108 * \param Byte of data to send to the dataflash
109 *
110 * \return Last response byte from the dataflash
111 */
112 static inline uint8_t Dataflash_TransferByte(const uint8_t Byte) ATTR_ALWAYS_INLINE;
113 static inline uint8_t Dataflash_TransferByte(const uint8_t Byte)
114 {
115 return SPI_TransferByte(Byte);
116 }
117
118 /** Sends a byte to the currently selected dataflash IC, and ignores the next byte from the dataflash.
119 *
120 * \param Byte of data to send to the dataflash
121 */
122 static inline void Dataflash_SendByte(const uint8_t Byte) ATTR_ALWAYS_INLINE;
123 static inline void Dataflash_SendByte(const uint8_t Byte)
124 {
125 SPI_SendByte(Byte);
126 }
127
128 /** Sends a dummy byte to the currently selected dataflash IC, and returns the next byte from the dataflash.
129 *
130 * \return Last response byte from the dataflash
131 */
132 static inline uint8_t Dataflash_ReceiveByte(void) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT;
133 static inline uint8_t Dataflash_ReceiveByte(void)
134 {
135 return SPI_ReceiveByte();
136 }
137
138 /* Includes: */
139 #if !defined(BOARD)
140 #error BOARD must be set in makefile to a value specified in BoardTypes.h.
141 #elif (BOARD == BOARD_USBKEY)
142 #include "USBKEY/Dataflash.h"
143 #elif (BOARD == BOARD_STK525)
144 #include "STK525/Dataflash.h"
145 #elif (BOARD == BOARD_STK526)
146 #include "STK526/Dataflash.h"
147 #elif (BOARD == BOARD_USER)
148 #include "Board/Dataflash.h"
149 #else
150 #error The selected board does not contain a dataflash IC.
151 #endif
152
153 /* Inline Functions: */
154 /** Initializes the dataflash driver (including the SPI driver) so that commands and data may be
155 * sent to an attached dataflash IC.
156 *
157 * \param PrescalerMask SPI prescaler mask, see SPI.h documentation
158 */
159 static inline void Dataflash_Init(const uint8_t PrescalerMask)
160 {
161 DATAFLASH_CHIPCS_DDR |= DATAFLASH_CHIPCS_MASK;
162 DATAFLASH_CHIPCS_PORT |= DATAFLASH_CHIPCS_MASK;
163
164 SPI_Init(PrescalerMask, true);
165 }
166
167 /** Toggles the select line of the currently selected dataflash IC, so that it is ready to receive
168 * a new command.
169 */
170 static inline void Dataflash_ToggleSelectedChipCS(void)
171 {
172 uint8_t SelectedChipMask = Dataflash_GetSelectedChip();
173
174 Dataflash_DeselectChip();
175 Dataflash_SelectChip(SelectedChipMask);
176 }
177
178 /** Spinloops while the currently selected dataflash is busy executing a command, such as a main
179 * memory page program or main memory to buffer transfer.
180 */
181 static inline void Dataflash_WaitWhileBusy(void)
182 {
183 Dataflash_ToggleSelectedChipCS();
184 Dataflash_SendByte(DF_CMD_GETSTATUS);
185 while (!(Dataflash_ReceiveByte() & DF_STATUS_READY));
186 }
187
188 /** Selects a dataflash IC from the given page number, which should range from 0 to
189 * ((DATAFLASH_PAGES * DATAFLASH_TOTALCHIPS) - 1). For boards containing only one
190 * dataflash IC, this will select DATAFLASH_CHIP1. If the given page number is outside
191 * the total number of pages contained in the boards dataflash ICs, all dataflash ICs
192 * are deselected.
193 *
194 * \param PageAddress Address of the page to manipulate, ranging from
195 * ((DATAFLASH_PAGES * DATAFLASH_TOTALCHIPS) - 1).
196 */
197 static inline void Dataflash_SelectChipFromPage(const uint16_t PageAddress);
198
199 /** Sends a set of page and buffer address bytes to the currently selected dataflash IC, for use with
200 * dataflash commands which require a complete 24-byte address.
201 *
202 * \param PageAddress Page address within the selected dataflash IC
203 * \param BufferByte Address within the dataflash's buffer
204 */
205 static inline void Dataflash_SendAddressBytes(uint16_t PageAddress, const uint16_t BufferByte);
206
207 /* Disable C linkage for C++ Compilers: */
208 #if defined(__cplusplus)
209 }
210 #endif
211
212 #endif
213
214 /** @} */