]> git.donarmstrong.com Git - qmk_firmware.git/blob - protocol/lufa/LUFA-120730/LUFA/Drivers/Board/AVR8/USBKEY/Dataflash.h
Change TOP_DIR to TMK_DIR in makefiles
[qmk_firmware.git] / protocol / lufa / LUFA-120730 / LUFA / Drivers / Board / AVR8 / USBKEY / 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 USBKEY.\r
33  *  \copydetails Group_Dataflash_USBKEY\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_USBKEY USBKEY\r
41  *  \brief Board specific Dataflash driver header for the Atmel USBKEY.\r
42  *\r
43  *  Board specific Dataflash driver header for the Atmel USBKEY 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>PORTE.0</td><td>SPI0</td></tr>\r
48  *    <tr><td>DATAFLASH_CHIP2</td><td>AT45DB642D (8MB)</td><td>PORTE.1</td><td>SPI0</td></tr>\r
49  *  </table>\r
50  *\r
51  *  @{\r
52  */\r
53 \r
54 #ifndef __DATAFLASH_USBKEY_H__\r
55 #define __DATAFLASH_USBKEY_H__\r
56 \r
57         /* Includes: */\r
58                 #include "../../../../Common/Common.h"\r
59                 #include "../../../Misc/AT45DB642D.h"\r
60                 #include "../../../Peripheral/SPI.h"\r
61 \r
62         /* Preprocessor Checks: */\r
63                 #if !defined(__INCLUDE_FROM_DATAFLASH_H)\r
64                         #error Do not include this file directly. Include LUFA/Drivers/Board/Dataflash.h instead.\r
65                 #endif\r
66 \r
67         /* Private Interface - For use in library only: */\r
68         #if !defined(__DOXYGEN__)\r
69                 /* Macros: */\r
70                         #define DATAFLASH_CHIPCS_MASK                ((1 << 1) | (1 << 0))\r
71                         #define DATAFLASH_CHIPCS_DDR                 DDRE\r
72                         #define DATAFLASH_CHIPCS_PORT                PORTE\r
73         #endif\r
74 \r
75         /* Public Interface - May be used in end-application: */\r
76                 /* Macros: */\r
77                         /** Constant indicating the total number of dataflash ICs mounted on the selected board. */\r
78                         #define DATAFLASH_TOTALCHIPS                 2\r
79 \r
80                         /** Mask for no dataflash chip selected. */\r
81                         #define DATAFLASH_NO_CHIP                    DATAFLASH_CHIPCS_MASK\r
82 \r
83                         /** Mask for the first dataflash chip selected. */\r
84                         #define DATAFLASH_CHIP1                      (1 << 1)\r
85 \r
86                         /** Mask for the second dataflash chip selected. */\r
87                         #define DATAFLASH_CHIP2                      (1 << 0)\r
88 \r
89                         /** Internal main memory page size for the board's dataflash ICs. */\r
90                         #define DATAFLASH_PAGE_SIZE                  1024\r
91 \r
92                         /** Total number of pages inside each of the board's dataflash ICs. */\r
93                         #define DATAFLASH_PAGES                      8192\r
94 \r
95                 /* Inline Functions: */\r
96                         /** Initializes the dataflash driver so that commands and data may be sent to an attached dataflash IC.\r
97                          *  The microcontroller's SPI driver MUST be initialized before any of the dataflash commands are used.\r
98                          */\r
99                         static inline void Dataflash_Init(void)\r
100                         {\r
101                                 DATAFLASH_CHIPCS_DDR  |= DATAFLASH_CHIPCS_MASK;\r
102                                 DATAFLASH_CHIPCS_PORT |= DATAFLASH_CHIPCS_MASK;\r
103                         }\r
104 \r
105                         /** Sends a byte to the currently selected dataflash IC, and returns a byte from the dataflash.\r
106                          *\r
107                          *  \param[in] Byte  Byte of data to send to the dataflash\r
108                          *\r
109                          *  \return Last response byte from the dataflash\r
110                          */\r
111                         static inline uint8_t Dataflash_TransferByte(const uint8_t Byte) ATTR_ALWAYS_INLINE;\r
112                         static inline uint8_t Dataflash_TransferByte(const uint8_t Byte)\r
113                         {\r
114                                 return SPI_TransferByte(Byte);\r
115                         }\r
116 \r
117                         /** Sends a byte to the currently selected dataflash IC, and ignores the next byte from the dataflash.\r
118                          *\r
119                          *  \param[in] Byte  Byte of data to send to the dataflash\r
120                          */\r
121                         static inline void Dataflash_SendByte(const uint8_t Byte) ATTR_ALWAYS_INLINE;\r
122                         static inline void Dataflash_SendByte(const uint8_t Byte)\r
123                         {\r
124                                 SPI_SendByte(Byte);\r
125                         }\r
126 \r
127                         /** Sends a dummy byte to the currently selected dataflash IC, and returns the next byte from the dataflash.\r
128                          *\r
129                          *  \return Last response byte from the dataflash\r
130                          */\r
131                         static inline uint8_t Dataflash_ReceiveByte(void) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT;\r
132                         static inline uint8_t Dataflash_ReceiveByte(void)\r
133                         {\r
134                                 return SPI_ReceiveByte();\r
135                         }\r
136 \r
137                         /** Determines the currently selected dataflash chip.\r
138                          *\r
139                          *  \return Mask of the currently selected Dataflash chip, either \ref DATAFLASH_NO_CHIP if no chip is selected\r
140                          *  or a DATAFLASH_CHIPn mask (where n is the chip number).\r
141                          */\r
142                         static inline uint8_t Dataflash_GetSelectedChip(void) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT;\r
143                         static inline uint8_t Dataflash_GetSelectedChip(void)\r
144                         {\r
145                                 return (DATAFLASH_CHIPCS_PORT & DATAFLASH_CHIPCS_MASK);\r
146                         }\r
147 \r
148                         /** Selects the given dataflash chip.\r
149                          *\r
150                          *  \param[in]  ChipMask  Mask of the Dataflash IC to select, in the form of DATAFLASH_CHIPn mask (where n is\r
151                          *              the chip number).\r
152                          */\r
153                         static inline void Dataflash_SelectChip(const uint8_t ChipMask) ATTR_ALWAYS_INLINE;\r
154                         static inline void Dataflash_SelectChip(const uint8_t ChipMask)\r
155                         {\r
156                                 DATAFLASH_CHIPCS_PORT = ((DATAFLASH_CHIPCS_PORT & ~DATAFLASH_CHIPCS_MASK) | ChipMask);\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                                 if (PageAddress & 0x01)\r
183                                   Dataflash_SelectChip(DATAFLASH_CHIP2);\r
184                                 else\r
185                                   Dataflash_SelectChip(DATAFLASH_CHIP1);\r
186                         }\r
187 \r
188                         /** Toggles the select line of the currently selected dataflash IC, so that it is ready to receive\r
189                          *  a new command.\r
190                          */\r
191                         static inline void Dataflash_ToggleSelectedChipCS(void)\r
192                         {\r
193                                 uint8_t SelectedChipMask = Dataflash_GetSelectedChip();\r
194 \r
195                                 Dataflash_DeselectChip();\r
196                                 Dataflash_SelectChip(SelectedChipMask);\r
197                         }\r
198 \r
199                         /** Spin-loops while the currently selected dataflash is busy executing a command, such as a main\r
200                          *  memory page program or main memory to buffer transfer.\r
201                          */\r
202                         static inline void Dataflash_WaitWhileBusy(void)\r
203                         {\r
204                                 Dataflash_ToggleSelectedChipCS();\r
205                                 Dataflash_SendByte(DF_CMD_GETSTATUS);\r
206                                 while (!(Dataflash_ReceiveByte() & DF_STATUS_READY));\r
207                                 Dataflash_ToggleSelectedChipCS();\r
208                         }\r
209 \r
210                         /** Sends a set of page and buffer address bytes to the currently selected dataflash IC, for use with\r
211                          *  dataflash commands which require a complete 24-bit address.\r
212                          *\r
213                          *  \param[in] PageAddress  Page address within the selected dataflash IC\r
214                          *  \param[in] BufferByte   Address within the dataflash's buffer\r
215                          */\r
216                         static inline void Dataflash_SendAddressBytes(uint16_t PageAddress,\r
217                                                                       const uint16_t BufferByte)\r
218                         {\r
219                                 PageAddress >>= 1;\r
220 \r
221                                 Dataflash_SendByte(PageAddress >> 5);\r
222                                 Dataflash_SendByte((PageAddress << 3) | (BufferByte >> 8));\r
223                                 Dataflash_SendByte(BufferByte);\r
224                         }\r
225 \r
226 #endif\r
227 \r
228 /** @} */\r
229 \r