Add new Master SPI Mode USART serial peripheral driver for the XMEGA and AVR8 archite...
[pub/USBasp.git] / LUFA / Drivers / Board / AVR8 / USBKEY / Dataflash.h
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2012.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2012 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
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.
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 * \brief Board specific Dataflash driver header for the Atmel USBKEY.
33 * \copydetails Group_Dataflash_USBKEY
34 *
35 * \note This file should not be included directly. It is automatically included as needed by the dataflash driver
36 * dispatch header located in LUFA/Drivers/Board/Dataflash.h.
37 */
38
39 /** \ingroup Group_Dataflash
40 * \defgroup Group_Dataflash_USBKEY USBKEY
41 * \brief Board specific Dataflash driver header for the Atmel USBKEY.
42 *
43 * Board specific Dataflash driver header for the Atmel USBKEY board.
44 *
45 * @{
46 */
47
48 #ifndef __DATAFLASH_USBKEY_H__
49 #define __DATAFLASH_USBKEY_H__
50
51 /* Includes: */
52 #include "../../../../Common/Common.h"
53 #include "../../../Misc/AT45DB642D.h"
54 #include "../../../Peripheral/SPI.h"
55
56 /* Preprocessor Checks: */
57 #if !defined(__INCLUDE_FROM_DATAFLASH_H)
58 #error Do not include this file directly. Include LUFA/Drivers/Board/Dataflash.h instead.
59 #endif
60
61 /* Private Interface - For use in library only: */
62 #if !defined(__DOXYGEN__)
63 /* Macros: */
64 #define DATAFLASH_CHIPCS_MASK ((1 << 1) | (1 << 0))
65 #define DATAFLASH_CHIPCS_DDR DDRE
66 #define DATAFLASH_CHIPCS_PORT PORTE
67 #endif
68
69 /* Public Interface - May be used in end-application: */
70 /* Macros: */
71 /** Constant indicating the total number of dataflash ICs mounted on the selected board. */
72 #define DATAFLASH_TOTALCHIPS 2
73
74 /** Mask for no dataflash chip selected. */
75 #define DATAFLASH_NO_CHIP DATAFLASH_CHIPCS_MASK
76
77 /** Mask for the first dataflash chip selected. */
78 #define DATAFLASH_CHIP1 (1 << 1)
79
80 /** Mask for the second dataflash chip selected. */
81 #define DATAFLASH_CHIP2 (1 << 0)
82
83 /** Internal main memory page size for the board's dataflash ICs. */
84 #define DATAFLASH_PAGE_SIZE 1024
85
86 /** Total number of pages inside each of the board's dataflash ICs. */
87 #define DATAFLASH_PAGES 8192
88
89 /* Inline Functions: */
90 /** Initializes the dataflash driver so that commands and data may be sent to an attached dataflash IC.
91 * The microcontroller's SPI driver MUST be initialized before any of the dataflash commands are used.
92 */
93 static inline void Dataflash_Init(void)
94 {
95 DATAFLASH_CHIPCS_DDR |= DATAFLASH_CHIPCS_MASK;
96 DATAFLASH_CHIPCS_PORT |= DATAFLASH_CHIPCS_MASK;
97 }
98
99 /** Sends a byte to the currently selected dataflash IC, and returns a byte from the dataflash.
100 *
101 * \param[in] Byte Byte of data to send to the dataflash
102 *
103 * \return Last response byte from the dataflash
104 */
105 static inline uint8_t Dataflash_TransferByte(const uint8_t Byte) ATTR_ALWAYS_INLINE;
106 static inline uint8_t Dataflash_TransferByte(const uint8_t Byte)
107 {
108 return SPI_TransferByte(Byte);
109 }
110
111 /** Sends a byte to the currently selected dataflash IC, and ignores the next byte from the dataflash.
112 *
113 * \param[in] Byte Byte of data to send to the dataflash
114 */
115 static inline void Dataflash_SendByte(const uint8_t Byte) ATTR_ALWAYS_INLINE;
116 static inline void Dataflash_SendByte(const uint8_t Byte)
117 {
118 SPI_SendByte(Byte);
119 }
120
121 /** Sends a dummy byte to the currently selected dataflash IC, and returns the next byte from the dataflash.
122 *
123 * \return Last response byte from the dataflash
124 */
125 static inline uint8_t Dataflash_ReceiveByte(void) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT;
126 static inline uint8_t Dataflash_ReceiveByte(void)
127 {
128 return SPI_ReceiveByte();
129 }
130
131 /** Determines the currently selected dataflash chip.
132 *
133 * \return Mask of the currently selected Dataflash chip, either \ref DATAFLASH_NO_CHIP if no chip is selected
134 * or a DATAFLASH_CHIPn mask (where n is the chip number).
135 */
136 static inline uint8_t Dataflash_GetSelectedChip(void) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT;
137 static inline uint8_t Dataflash_GetSelectedChip(void)
138 {
139 return (DATAFLASH_CHIPCS_PORT & DATAFLASH_CHIPCS_MASK);
140 }
141
142 /** Selects the given dataflash chip.
143 *
144 * \param[in] ChipMask Mask of the Dataflash IC to select, in the form of DATAFLASH_CHIPn mask (where n is
145 * the chip number).
146 */
147 static inline void Dataflash_SelectChip(const uint8_t ChipMask) ATTR_ALWAYS_INLINE;
148 static inline void Dataflash_SelectChip(const uint8_t ChipMask)
149 {
150 DATAFLASH_CHIPCS_PORT = ((DATAFLASH_CHIPCS_PORT & ~DATAFLASH_CHIPCS_MASK) | ChipMask);
151 }
152
153 /** Deselects the current dataflash chip, so that no dataflash is selected. */
154 static inline void Dataflash_DeselectChip(void) ATTR_ALWAYS_INLINE;
155 static inline void Dataflash_DeselectChip(void)
156 {
157 Dataflash_SelectChip(DATAFLASH_NO_CHIP);
158 }
159
160 /** Selects a dataflash IC from the given page number, which should range from 0 to
161 * ((DATAFLASH_PAGES * DATAFLASH_TOTALCHIPS) - 1). For boards containing only one
162 * dataflash IC, this will select DATAFLASH_CHIP1. If the given page number is outside
163 * the total number of pages contained in the boards dataflash ICs, all dataflash ICs
164 * are deselected.
165 *
166 * \param[in] PageAddress Address of the page to manipulate, ranging from
167 * 0 to ((DATAFLASH_PAGES * DATAFLASH_TOTALCHIPS) - 1).
168 */
169 static inline void Dataflash_SelectChipFromPage(const uint16_t PageAddress)
170 {
171 Dataflash_DeselectChip();
172
173 if (PageAddress >= (DATAFLASH_PAGES * DATAFLASH_TOTALCHIPS))
174 return;
175
176 if (PageAddress & 0x01)
177 Dataflash_SelectChip(DATAFLASH_CHIP2);
178 else
179 Dataflash_SelectChip(DATAFLASH_CHIP1);
180 }
181
182 /** Toggles the select line of the currently selected dataflash IC, so that it is ready to receive
183 * a new command.
184 */
185 static inline void Dataflash_ToggleSelectedChipCS(void)
186 {
187 uint8_t SelectedChipMask = Dataflash_GetSelectedChip();
188
189 Dataflash_DeselectChip();
190 Dataflash_SelectChip(SelectedChipMask);
191 }
192
193 /** Spin-loops while the currently selected dataflash is busy executing a command, such as a main
194 * memory page program or main memory to buffer transfer.
195 */
196 static inline void Dataflash_WaitWhileBusy(void)
197 {
198 Dataflash_ToggleSelectedChipCS();
199 Dataflash_SendByte(DF_CMD_GETSTATUS);
200 while (!(Dataflash_ReceiveByte() & DF_STATUS_READY));
201 Dataflash_ToggleSelectedChipCS();
202 }
203
204 /** Sends a set of page and buffer address bytes to the currently selected dataflash IC, for use with
205 * dataflash commands which require a complete 24-bit address.
206 *
207 * \param[in] PageAddress Page address within the selected dataflash IC
208 * \param[in] BufferByte Address within the dataflash's buffer
209 */
210 static inline void Dataflash_SendAddressBytes(uint16_t PageAddress,
211 const uint16_t BufferByte)
212 {
213 PageAddress >>= 1;
214
215 Dataflash_SendByte(PageAddress >> 5);
216 Dataflash_SendByte((PageAddress << 3) | (BufferByte >> 8));
217 Dataflash_SendByte(BufferByte);
218 }
219
220 #endif
221
222 /** @} */
223