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 A3BU Xplained.
\r
33 * \copydetails Group_Dataflash_A3BU_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_A3BU_XPLAINED A3BU_XPLAINED
\r
41 * \brief Board specific Dataflash driver header for the Atmel XMEGA A3BU Xplained.
\r
43 * Board specific Dataflash driver header for the Atmel XMEGA A3BU 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>PORTD.2</td><td>USARTC0 (In SPI Mode, Remapped)</td></tr>
\r
53 #ifndef __DATAFLASH_A3BU_XPLAINED_H__
\r
54 #define __DATAFLASH_A3BU_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 << 2)
\r
70 #define DATAFLASH_CHIPCS_PORT PORTD
\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 << 2)
\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
101 PORTC.REMAP |= PORT_USART0_bm;
\r
104 /** Sends a byte to the currently selected dataflash IC, and returns a byte from the dataflash.
\r
106 * \param[in] Byte Byte of data to send to the dataflash
\r
108 * \return Last response byte from the dataflash
\r
110 static inline uint8_t Dataflash_TransferByte(const uint8_t Byte) ATTR_ALWAYS_INLINE;
\r
111 static inline uint8_t Dataflash_TransferByte(const uint8_t Byte)
\r
113 return SerialSPI_TransferByte(&USARTC0, Byte);
\r
116 /** Sends a byte to the currently selected dataflash IC, and ignores the next byte from the dataflash.
\r
118 * \param[in] Byte Byte of data to send to the dataflash
\r
120 static inline void Dataflash_SendByte(const uint8_t Byte) ATTR_ALWAYS_INLINE;
\r
121 static inline void Dataflash_SendByte(const uint8_t Byte)
\r
123 SerialSPI_SendByte(&USARTC0, Byte);
\r
126 /** Sends a dummy byte to the currently selected dataflash IC, and returns the next byte from the dataflash.
\r
128 * \return Last response byte from the dataflash
\r
130 static inline uint8_t Dataflash_ReceiveByte(void) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT;
\r
131 static inline uint8_t Dataflash_ReceiveByte(void)
\r
133 return SerialSPI_ReceiveByte(&USARTC0);
\r
136 /** Determines the currently selected dataflash chip.
\r
138 * \return Mask of the currently selected Dataflash chip, either \ref DATAFLASH_NO_CHIP if no chip is selected
\r
139 * or a DATAFLASH_CHIPn mask (where n is the chip number).
\r
141 static inline uint8_t Dataflash_GetSelectedChip(void) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT;
\r
142 static inline uint8_t Dataflash_GetSelectedChip(void)
\r
144 return (DATAFLASH_CHIPCS_PORT.OUT & DATAFLASH_CHIPCS_MASK);
\r
147 /** Selects the given dataflash chip.
\r
149 * \param[in] ChipMask Mask of the Dataflash IC to select, in the form of DATAFLASH_CHIPn mask (where n is
\r
150 * the chip number).
\r
152 static inline void Dataflash_SelectChip(const uint8_t ChipMask) ATTR_ALWAYS_INLINE;
\r
153 static inline void Dataflash_SelectChip(const uint8_t ChipMask)
\r
155 DATAFLASH_CHIPCS_PORT.OUTCLR = DATAFLASH_CHIPCS_MASK;
\r
156 DATAFLASH_CHIPCS_PORT.OUTSET = (ChipMask & DATAFLASH_CHIPCS_MASK);
\r
159 /** Deselects the current dataflash chip, so that no dataflash is selected. */
\r
160 static inline void Dataflash_DeselectChip(void) ATTR_ALWAYS_INLINE;
\r
161 static inline void Dataflash_DeselectChip(void)
\r
163 Dataflash_SelectChip(DATAFLASH_NO_CHIP);
\r
166 /** Selects a dataflash IC from the given page number, which should range from 0 to
\r
167 * ((DATAFLASH_PAGES * DATAFLASH_TOTALCHIPS) - 1). For boards containing only one
\r
168 * dataflash IC, this will select DATAFLASH_CHIP1. If the given page number is outside
\r
169 * the total number of pages contained in the boards dataflash ICs, all dataflash ICs
\r
172 * \param[in] PageAddress Address of the page to manipulate, ranging from
\r
173 * 0 to ((DATAFLASH_PAGES * DATAFLASH_TOTALCHIPS) - 1).
\r
175 static inline void Dataflash_SelectChipFromPage(const uint16_t PageAddress)
\r
177 Dataflash_DeselectChip();
\r
179 if (PageAddress >= (DATAFLASH_PAGES * DATAFLASH_TOTALCHIPS))
\r
182 Dataflash_SelectChip(DATAFLASH_CHIP1);
\r
185 /** Toggles the select line of the currently selected dataflash IC, so that it is ready to receive
\r
188 static inline void Dataflash_ToggleSelectedChipCS(void)
\r
190 uint8_t SelectedChipMask = Dataflash_GetSelectedChip();
\r
192 Dataflash_DeselectChip();
\r
193 Dataflash_SelectChip(SelectedChipMask);
\r
196 /** Spin-loops while the currently selected dataflash is busy executing a command, such as a main
\r
197 * memory page program or main memory to buffer transfer.
\r
199 static inline void Dataflash_WaitWhileBusy(void)
\r
201 Dataflash_ToggleSelectedChipCS();
\r
202 Dataflash_SendByte(DF_CMD_GETSTATUS);
\r
203 while (!(Dataflash_ReceiveByte() & DF_STATUS_READY));
\r
204 Dataflash_ToggleSelectedChipCS();
\r
207 /** Sends a set of page and buffer address bytes to the currently selected dataflash IC, for use with
\r
208 * dataflash commands which require a complete 24-bit address.
\r
210 * \param[in] PageAddress Page address within the selected dataflash IC
\r
211 * \param[in] BufferByte Address within the dataflash's buffer
\r
213 static inline void Dataflash_SendAddressBytes(uint16_t PageAddress,
\r
214 const uint16_t BufferByte)
\r
216 Dataflash_SendByte(PageAddress >> 5);
\r
217 Dataflash_SendByte((PageAddress << 3) | (BufferByte >> 8));
\r
218 Dataflash_SendByte(BufferByte);
\r