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