3 Copyright (C) Dean Camera, 2012.
\r
5 dean [at] fourwalledcubicle [dot] com
\r
10 Copyright 2012 Dean Camera (dean [at] fourwalledcubicle [dot] com)
\r
12 Permission to use, copy, modify, distribute, and sell this
\r
13 software and its documentation for any purpose is hereby granted
\r
14 without fee, provided that the above copyright notice appear in
\r
15 all copies and that both that the copyright notice and this
\r
16 permission notice and warranty disclaimer appear in supporting
\r
17 documentation, and that the name of the author not be used in
\r
18 advertising or publicity pertaining to distribution of the
\r
19 software without specific, written prior permission.
\r
21 The author disclaim all warranties with regard to this
\r
22 software, including all implied warranties of merchantability
\r
23 and fitness. In no event shall the author be liable for any
\r
24 special, indirect or consequential damages or any damages
\r
25 whatsoever resulting from loss of use, data or profits, whether
\r
26 in an action of contract, negligence or other tortious action,
\r
27 arising out of or in connection with the use or performance of
\r
32 * \brief Board specific Dataflash driver header for the Atmel XMEGA B1 Xplained.
\r
33 * \copydetails Group_Dataflash_B1_XPLAINED
\r
35 * \note This file should not be included directly. It is automatically included as needed by the dataflash driver
\r
36 * dispatch header located in LUFA/Drivers/Board/Dataflash.h.
\r
39 /** \ingroup Group_Dataflash
\r
40 * \defgroup Group_Dataflash_B1_XPLAINED B1_XPLAINED
\r
41 * \brief Board specific Dataflash driver header for the Atmel XMEGA B1 Xplained.
\r
43 * Board specific Dataflash driver header for the Atmel XMEGA B1 Xplained board.
\r
46 * <tr><th>Name</th><th>Info</th><th>Select Pin</th><th>SPI Port</th></tr>
\r
47 * <tr><td>DATAFLASH_CHIP1</td><td>AT45DB642D (8MB)</td><td>PORTF.4</td><td>USARTD0 (In SPI Mode)</td></tr>
\r
53 #ifndef __DATAFLASH_B1_XPLAINED_H__
\r
54 #define __DATAFLASH_B1_XPLAINED_H__
\r
57 #include "../../../../Common/Common.h"
\r
58 #include "../../../Misc/AT45DB642D.h"
\r
59 #include "../../../Peripheral/SerialSPI.h"
\r
61 /* Preprocessor Checks: */
\r
62 #if !defined(__INCLUDE_FROM_DATAFLASH_H)
\r
63 #error Do not include this file directly. Include LUFA/Drivers/Board/Dataflash.h instead.
\r
66 /* Private Interface - For use in library only: */
\r
67 #if !defined(__DOXYGEN__)
\r
69 #define DATAFLASH_CHIPCS_MASK (1 << 4)
\r
70 #define DATAFLASH_CHIPCS_PORT PORTF
\r
73 /* Public Interface - May be used in end-application: */
\r
75 /** Constant indicating the total number of dataflash ICs mounted on the selected board. */
\r
76 #define DATAFLASH_TOTALCHIPS 1
\r
78 /** Mask for no dataflash chip selected. */
\r
79 #define DATAFLASH_NO_CHIP 0
\r
81 /** Mask for the first dataflash chip selected. */
\r
82 #define DATAFLASH_CHIP1 (1 << 4)
\r
84 /** Internal main memory page size for the board's dataflash ICs. */
\r
85 #define DATAFLASH_PAGE_SIZE 1024
\r
87 /** Total number of pages inside each of the board's dataflash ICs. */
\r
88 #define DATAFLASH_PAGES 8192
\r
90 /* Inline Functions: */
\r
91 /** Initializes the dataflash driver so that commands and data may be sent to an attached dataflash IC.
\r
92 * The microcontroller's SPI driver MUST be initialized before any of the dataflash commands are used.
\r
94 static inline void Dataflash_Init(void)
\r
96 DATAFLASH_CHIPCS_PORT.DIRSET = DATAFLASH_CHIPCS_MASK;
\r
98 PORTCFG.MPCMASK = DATAFLASH_CHIPCS_MASK;
\r
99 DATAFLASH_CHIPCS_PORT.PIN0CTRL = PORT_INVEN_bm;
\r
102 /** Sends a byte to the currently selected dataflash IC, and returns a byte from the dataflash.
\r
104 * \param[in] Byte Byte of data to send to the dataflash
\r
106 * \return Last response byte from the dataflash
\r
108 static inline uint8_t Dataflash_TransferByte(const uint8_t Byte) ATTR_ALWAYS_INLINE;
\r
109 static inline uint8_t Dataflash_TransferByte(const uint8_t Byte)
\r
111 return SerialSPI_TransferByte(&USARTD0, Byte);
\r
114 /** Sends a byte to the currently selected dataflash IC, and ignores the next byte from the dataflash.
\r
116 * \param[in] Byte Byte of data to send to the dataflash
\r
118 static inline void Dataflash_SendByte(const uint8_t Byte) ATTR_ALWAYS_INLINE;
\r
119 static inline void Dataflash_SendByte(const uint8_t Byte)
\r
121 SerialSPI_SendByte(&USARTD0, Byte);
\r
124 /** Sends a dummy byte to the currently selected dataflash IC, and returns the next byte from the dataflash.
\r
126 * \return Last response byte from the dataflash
\r
128 static inline uint8_t Dataflash_ReceiveByte(void) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT;
\r
129 static inline uint8_t Dataflash_ReceiveByte(void)
\r
131 return SerialSPI_ReceiveByte(&USARTD0);
\r
134 /** Determines the currently selected dataflash chip.
\r
136 * \return Mask of the currently selected Dataflash chip, either \ref DATAFLASH_NO_CHIP if no chip is selected
\r
137 * or a DATAFLASH_CHIPn mask (where n is the chip number).
\r
139 static inline uint8_t Dataflash_GetSelectedChip(void) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT;
\r
140 static inline uint8_t Dataflash_GetSelectedChip(void)
\r
142 return (DATAFLASH_CHIPCS_PORT.OUT & DATAFLASH_CHIPCS_MASK);
\r
145 /** Selects the given dataflash chip.
\r
147 * \param[in] ChipMask Mask of the Dataflash IC to select, in the form of DATAFLASH_CHIPn mask (where n is
\r
148 * the chip number).
\r
150 static inline void Dataflash_SelectChip(const uint8_t ChipMask) ATTR_ALWAYS_INLINE;
\r
151 static inline void Dataflash_SelectChip(const uint8_t ChipMask)
\r
153 DATAFLASH_CHIPCS_PORT.OUTCLR = DATAFLASH_CHIPCS_MASK;
\r
154 DATAFLASH_CHIPCS_PORT.OUTSET = ChipMask;
\r
157 /** Deselects the current dataflash chip, so that no dataflash is selected. */
\r
158 static inline void Dataflash_DeselectChip(void) ATTR_ALWAYS_INLINE;
\r
159 static inline void Dataflash_DeselectChip(void)
\r
161 Dataflash_SelectChip(DATAFLASH_NO_CHIP);
\r
164 /** Selects a dataflash IC from the given page number, which should range from 0 to
\r
165 * ((DATAFLASH_PAGES * DATAFLASH_TOTALCHIPS) - 1). For boards containing only one
\r
166 * dataflash IC, this will select DATAFLASH_CHIP1. If the given page number is outside
\r
167 * the total number of pages contained in the boards dataflash ICs, all dataflash ICs
\r
170 * \param[in] PageAddress Address of the page to manipulate, ranging from
\r
171 * 0 to ((DATAFLASH_PAGES * DATAFLASH_TOTALCHIPS) - 1).
\r
173 static inline void Dataflash_SelectChipFromPage(const uint16_t PageAddress)
\r
175 Dataflash_DeselectChip();
\r
177 if (PageAddress >= (DATAFLASH_PAGES * DATAFLASH_TOTALCHIPS))
\r
180 Dataflash_SelectChip(DATAFLASH_CHIP1);
\r
183 /** Toggles the select line of the currently selected dataflash IC, so that it is ready to receive
\r
186 static inline void Dataflash_ToggleSelectedChipCS(void)
\r
188 uint8_t SelectedChipMask = Dataflash_GetSelectedChip();
\r
190 Dataflash_DeselectChip();
\r
191 Dataflash_SelectChip(SelectedChipMask);
\r
194 /** Spin-loops while the currently selected dataflash is busy executing a command, such as a main
\r
195 * memory page program or main memory to buffer transfer.
\r
197 static inline void Dataflash_WaitWhileBusy(void)
\r
199 Dataflash_ToggleSelectedChipCS();
\r
200 Dataflash_SendByte(DF_CMD_GETSTATUS);
\r
201 while (!(Dataflash_ReceiveByte() & DF_STATUS_READY));
\r
202 Dataflash_ToggleSelectedChipCS();
\r
205 /** Sends a set of page and buffer address bytes to the currently selected dataflash IC, for use with
\r
206 * dataflash commands which require a complete 24-bit address.
\r
208 * \param[in] PageAddress Page address within the selected dataflash IC
\r
209 * \param[in] BufferByte Address within the dataflash's buffer
\r
211 static inline void Dataflash_SendAddressBytes(uint16_t PageAddress,
\r
212 const uint16_t BufferByte)
\r
214 Dataflash_SendByte(PageAddress >> 5);
\r
215 Dataflash_SendByte((PageAddress << 3) | (BufferByte >> 8));
\r
216 Dataflash_SendByte(BufferByte);
\r