]> git.donarmstrong.com Git - qmk_firmware.git/blob - protocol/lufa/LUFA-120730/LUFA/Drivers/Board/XMEGA/A3BU_XPLAINED/Dataflash.h
Squashed 'tmk_core/' content from commit 05caacc
[qmk_firmware.git] / protocol / lufa / LUFA-120730 / LUFA / Drivers / Board / XMEGA / A3BU_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 B1 Xplained.\r
33  *  \copydetails Group_Dataflash_B1_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_B1_XPLAINED B1_XPLAINED\r
41  *  \brief Board specific Dataflash driver header for the Atmel XMEGA B1 Xplained.\r
42  *\r
43  *  Board specific Dataflash driver header for the Atmel XMEGA B1 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>PORTF.4</td><td>USARTD0 (In SPI Mode)</td></tr>\r
48  *  </table>\r
49  *\r
50  *  @{\r
51  */\r
52 \r
53 #ifndef __DATAFLASH_B1_XPLAINED_H__\r
54 #define __DATAFLASH_B1_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 << 4)\r
70                         #define DATAFLASH_CHIPCS_PORT                PORTF\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 << 4)\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 \r
102                         /** Sends a byte to the currently selected dataflash IC, and returns a byte from the dataflash.\r
103                          *\r
104                          *  \param[in] Byte  Byte of data to send to the dataflash\r
105                          *\r
106                          *  \return Last response byte from the dataflash\r
107                          */\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
110                         {\r
111                                 return SerialSPI_TransferByte(&USARTD0, Byte);\r
112                         }\r
113 \r
114                         /** Sends a byte to the currently selected dataflash IC, and ignores the next byte from the dataflash.\r
115                          *\r
116                          *  \param[in] Byte  Byte of data to send to the dataflash\r
117                          */\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
120                         {\r
121                                 SerialSPI_SendByte(&USARTD0, Byte);\r
122                         }\r
123 \r
124                         /** Sends a dummy byte to the currently selected dataflash IC, and returns the next byte from the dataflash.\r
125                          *\r
126                          *  \return Last response byte from the dataflash\r
127                          */\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
130                         {\r
131                                 return SerialSPI_ReceiveByte(&USARTD0);\r
132                         }\r
133 \r
134                         /** Determines the currently selected dataflash chip.\r
135                          *\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
138                          */\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
141                         {\r
142                                 return (DATAFLASH_CHIPCS_PORT.OUT & DATAFLASH_CHIPCS_MASK);\r
143                         }\r
144 \r
145                         /** Selects the given dataflash chip.\r
146                          *\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
149                          */\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
152                         {\r
153                                 DATAFLASH_CHIPCS_PORT.OUTCLR = DATAFLASH_CHIPCS_MASK;\r
154                                 DATAFLASH_CHIPCS_PORT.OUTSET = ChipMask;\r
155                         }\r
156 \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
160                         {\r
161                                 Dataflash_SelectChip(DATAFLASH_NO_CHIP);\r
162                         }\r
163 \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
168                          *  are deselected.\r
169                          *\r
170                          *  \param[in] PageAddress  Address of the page to manipulate, ranging from\r
171                          *                          0 to ((DATAFLASH_PAGES * DATAFLASH_TOTALCHIPS) - 1).\r
172                          */\r
173                         static inline void Dataflash_SelectChipFromPage(const uint16_t PageAddress)\r
174                         {\r
175                                 Dataflash_DeselectChip();\r
176 \r
177                                 if (PageAddress >= (DATAFLASH_PAGES * DATAFLASH_TOTALCHIPS))\r
178                                   return;\r
179 \r
180                                 Dataflash_SelectChip(DATAFLASH_CHIP1);\r
181                         }\r
182 \r
183                         /** Toggles the select line of the currently selected dataflash IC, so that it is ready to receive\r
184                          *  a new command.\r
185                          */\r
186                         static inline void Dataflash_ToggleSelectedChipCS(void)\r
187                         {\r
188                                 uint8_t SelectedChipMask = Dataflash_GetSelectedChip();\r
189 \r
190                                 Dataflash_DeselectChip();\r
191                                 Dataflash_SelectChip(SelectedChipMask);\r
192                         }\r
193 \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
196                          */\r
197                         static inline void Dataflash_WaitWhileBusy(void)\r
198                         {\r
199                                 Dataflash_ToggleSelectedChipCS();\r
200                                 Dataflash_SendByte(DF_CMD_GETSTATUS);\r
201                                 while (!(Dataflash_ReceiveByte() & DF_STATUS_READY));\r
202                                 Dataflash_ToggleSelectedChipCS();\r
203                         }\r
204 \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
207                          *\r
208                          *  \param[in] PageAddress  Page address within the selected dataflash IC\r
209                          *  \param[in] BufferByte   Address within the dataflash's buffer\r
210                          */\r
211                         static inline void Dataflash_SendAddressBytes(uint16_t PageAddress,\r
212                                                                       const uint16_t BufferByte)\r
213                         {\r
214                                 Dataflash_SendByte(PageAddress >> 5);\r
215                                 Dataflash_SendByte((PageAddress << 3) | (BufferByte >> 8));\r
216                                 Dataflash_SendByte(BufferByte);\r
217                         }\r
218 \r
219 #endif\r
220 \r
221 /** @} */\r
222 \r