]> git.donarmstrong.com Git - qmk_firmware.git/blob - protocol/lufa/LUFA-120730/LUFA/Drivers/Board/XMEGA/B1_XPLAINED/Dataflash.h
Squashed 'tmk_core/' content from commit 05caacc
[qmk_firmware.git] / protocol / lufa / LUFA-120730 / LUFA / Drivers / Board / XMEGA / B1_XPLAINED / Dataflash.h
1 /*\r
2              LUFA Library\r
3      Copyright (C) Dean Camera, 2012.\r
4 \r
5   dean [at] fourwalledcubicle [dot] com\r
6            www.lufa-lib.org\r
7 */\r
8 \r
9 /*\r
10   Copyright 2012  Dean Camera (dean [at] fourwalledcubicle [dot] com)\r
11 \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
20 \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
28   this software.\r
29 */\r
30 \r
31 /** \file\r
32  *  \brief Board specific Dataflash driver header for the Atmel XMEGA A3BU Xplained.\r
33  *  \copydetails Group_Dataflash_A3BU_XPLAINED\r
34  *\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
37  */\r
38 \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
42  *\r
43  *  Board specific Dataflash driver header for the Atmel XMEGA A3BU Xplained board.\r
44  *\r
45  *  <table>\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
48  *  </table> \r
49  *\r
50  *  @{\r
51  */\r
52 \r
53 #ifndef __DATAFLASH_A3BU_XPLAINED_H__\r
54 #define __DATAFLASH_A3BU_XPLAINED_H__\r
55 \r
56         /* Includes: */\r
57                 #include "../../../../Common/Common.h"\r
58                 #include "../../../Misc/AT45DB642D.h"\r
59                 #include "../../../Peripheral/SerialSPI.h"\r
60 \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
64                 #endif\r
65 \r
66         /* Private Interface - For use in library only: */\r
67         #if !defined(__DOXYGEN__)\r
68                 /* Macros: */\r
69                         #define DATAFLASH_CHIPCS_MASK                (1 << 2)\r
70                         #define DATAFLASH_CHIPCS_PORT                PORTD\r
71         #endif\r
72 \r
73         /* Public Interface - May be used in end-application: */\r
74                 /* Macros: */\r
75                         /** Constant indicating the total number of dataflash ICs mounted on the selected board. */\r
76                         #define DATAFLASH_TOTALCHIPS                 1\r
77 \r
78                         /** Mask for no dataflash chip selected. */\r
79                         #define DATAFLASH_NO_CHIP                    0\r
80 \r
81                         /** Mask for the first dataflash chip selected. */\r
82                         #define DATAFLASH_CHIP1                      (1 << 2)\r
83 \r
84                         /** Internal main memory page size for the board's dataflash ICs. */\r
85                         #define DATAFLASH_PAGE_SIZE                  1024\r
86 \r
87                         /** Total number of pages inside each of the board's dataflash ICs. */\r
88                         #define DATAFLASH_PAGES                      8192\r
89 \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
93                          */\r
94                         static inline void Dataflash_Init(void)\r
95                         {\r
96                                 DATAFLASH_CHIPCS_PORT.DIRSET   = DATAFLASH_CHIPCS_MASK;\r
97                                 \r
98                                 PORTCFG.MPCMASK                = DATAFLASH_CHIPCS_MASK;\r
99                                 DATAFLASH_CHIPCS_PORT.PIN0CTRL = PORT_INVEN_bm; \r
100                                 \r
101                                 PORTC.REMAP |= PORT_USART0_bm;\r
102                         }\r
103 \r
104                         /** Sends a byte to the currently selected dataflash IC, and returns a byte from the dataflash.\r
105                          *\r
106                          *  \param[in] Byte  Byte of data to send to the dataflash\r
107                          *\r
108                          *  \return Last response byte from the dataflash\r
109                          */\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
112                         {\r
113                                 return SerialSPI_TransferByte(&USARTC0, Byte);\r
114                         }\r
115 \r
116                         /** Sends a byte to the currently selected dataflash IC, and ignores the next byte from the dataflash.\r
117                          *\r
118                          *  \param[in] Byte  Byte of data to send to the dataflash\r
119                          */\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
122                         {\r
123                                 SerialSPI_SendByte(&USARTC0, Byte);\r
124                         }\r
125 \r
126                         /** Sends a dummy byte to the currently selected dataflash IC, and returns the next byte from the dataflash.\r
127                          *\r
128                          *  \return Last response byte from the dataflash\r
129                          */\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
132                         {\r
133                                 return SerialSPI_ReceiveByte(&USARTC0);\r
134                         }\r
135 \r
136                         /** Determines the currently selected dataflash chip.\r
137                          *\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
140                          */\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
143                         {\r
144                                 return (DATAFLASH_CHIPCS_PORT.OUT & DATAFLASH_CHIPCS_MASK);\r
145                         }\r
146 \r
147                         /** Selects the given dataflash chip.\r
148                          *\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
151                          */\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
154                         {\r
155                                 DATAFLASH_CHIPCS_PORT.OUTCLR = DATAFLASH_CHIPCS_MASK;\r
156                                 DATAFLASH_CHIPCS_PORT.OUTSET = (ChipMask & DATAFLASH_CHIPCS_MASK);\r
157                         }\r
158 \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
162                         {\r
163                                 Dataflash_SelectChip(DATAFLASH_NO_CHIP);\r
164                         }\r
165 \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
170                          *  are deselected.\r
171                          *\r
172                          *  \param[in] PageAddress  Address of the page to manipulate, ranging from\r
173                          *                          0 to ((DATAFLASH_PAGES * DATAFLASH_TOTALCHIPS) - 1).\r
174                          */\r
175                         static inline void Dataflash_SelectChipFromPage(const uint16_t PageAddress)\r
176                         {\r
177                                 Dataflash_DeselectChip();\r
178 \r
179                                 if (PageAddress >= (DATAFLASH_PAGES * DATAFLASH_TOTALCHIPS))\r
180                                   return;\r
181 \r
182                                 Dataflash_SelectChip(DATAFLASH_CHIP1);\r
183                         }\r
184 \r
185                         /** Toggles the select line of the currently selected dataflash IC, so that it is ready to receive\r
186                          *  a new command.\r
187                          */\r
188                         static inline void Dataflash_ToggleSelectedChipCS(void)\r
189                         {\r
190                                 uint8_t SelectedChipMask = Dataflash_GetSelectedChip();\r
191 \r
192                                 Dataflash_DeselectChip();\r
193                                 Dataflash_SelectChip(SelectedChipMask);\r
194                         }\r
195 \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
198                          */\r
199                         static inline void Dataflash_WaitWhileBusy(void)\r
200                         {\r
201                                 Dataflash_ToggleSelectedChipCS();\r
202                                 Dataflash_SendByte(DF_CMD_GETSTATUS);\r
203                                 while (!(Dataflash_ReceiveByte() & DF_STATUS_READY));\r
204                                 Dataflash_ToggleSelectedChipCS();\r
205                         }\r
206 \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
209                          *\r
210                          *  \param[in] PageAddress  Page address within the selected dataflash IC\r
211                          *  \param[in] BufferByte   Address within the dataflash's buffer\r
212                          */\r
213                         static inline void Dataflash_SendAddressBytes(uint16_t PageAddress,\r
214                                                                       const uint16_t BufferByte)\r
215                         {\r
216                                 Dataflash_SendByte(PageAddress >> 5);\r
217                                 Dataflash_SendByte((PageAddress << 3) | (BufferByte >> 8));\r
218                                 Dataflash_SendByte(BufferByte);\r
219                         }\r
220 \r
221 #endif\r
222 \r
223 /** @} */\r
224 \r