]> git.donarmstrong.com Git - qmk_firmware.git/blob - protocol/lufa/LUFA-120730/LUFA/Drivers/Board/AVR8/XPLAIN/Dataflash.h
ef428b0dc4847be49ac6baf94dd14a917da11338
[qmk_firmware.git] / protocol / lufa / LUFA-120730 / LUFA / Drivers / Board / AVR8 / XPLAIN / 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 original Atmel XPLAIN.\r
33  *  \copydetails Group_Dataflash_XPLAIN\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_XPLAIN_REV1 XPLAIN_REV1\r
41  *  \brief Board specific Dataflash driver header for the original Atmel XPLAIN, revision 1.\r
42  *\r
43  *  See \ref Group_Dataflash_XPLAIN for more details.\r
44  */\r
45 \r
46 /** \ingroup Group_Dataflash\r
47  *  \defgroup Group_Dataflash_XPLAIN XPLAIN\r
48  *  \brief Board specific Dataflash driver header for the original Atmel XPLAIN.\r
49  *\r
50  *  \note For the first revision XPLAIN board, compile with <code>BOARD = BOARD_XPLAIN_REV1</code>.\r
51  *\r
52  *  Board specific Dataflash driver header for the Atmel XPLAIN.\r
53  *\r
54  *  <b>Revision 1 Boards</b>:\r
55  *  <table>\r
56  *    <tr><th>Name</th><th>Info</th><th>Select Pin</th><th>SPI Port</th></tr>\r
57  *    <tr><td>DATAFLASH_CHIP1</td><td>AT45DB041D (512KB)</td><td>PORTB.5</td><td>SPI0</td></tr>\r
58  *  </table>\r
59  *\r
60  *  <b>Other Board Revisions</b>:\r
61  *  <table>\r
62  *    <tr><th>Name</th><th>Info</th><th>Select Pin</th><th>SPI Port</th></tr>\r
63  *    <tr><td>DATAFLASH_CHIP1</td><td>AT45DB642D (8MB)</td><td>PORTB.5</td><td>SPI0</td></tr>\r
64  *  </table>\r
65  *\r
66  *  @{\r
67  */\r
68 \r
69 #ifndef __DATAFLASH_XPLAIN_H__\r
70 #define __DATAFLASH_XPLAIN_H__\r
71 \r
72         /* Includes: */\r
73                 #include "../../../../Common/Common.h"\r
74                 \r
75                 #include "../../../Misc/AT45DB642D.h"\r
76                 #include "../../../Peripheral/SPI.h"\r
77 \r
78         /* Preprocessor Checks: */\r
79                 #if !defined(__INCLUDE_FROM_DATAFLASH_H)\r
80                         #error Do not include this file directly. Include LUFA/Drivers/Board/Dataflash.h instead.\r
81                 #endif\r
82 \r
83         /* Private Interface - For use in library only: */\r
84         #if !defined(__DOXYGEN__)\r
85                 /* Macros: */\r
86                         #define DATAFLASH_CHIPCS_MASK                (1 << 5)\r
87                         #define DATAFLASH_CHIPCS_DDR                 DDRB\r
88                         #define DATAFLASH_CHIPCS_PORT                PORTB\r
89         #endif\r
90 \r
91         /* Public Interface - May be used in end-application: */\r
92                 /* Macros: */\r
93                         /** Constant indicating the total number of dataflash ICs mounted on the selected board. */\r
94                         #define DATAFLASH_TOTALCHIPS                 1\r
95 \r
96                         /** Mask for no dataflash chip selected. */\r
97                         #define DATAFLASH_NO_CHIP                    DATAFLASH_CHIPCS_MASK\r
98 \r
99                         /** Mask for the first dataflash chip selected. */\r
100                         #define DATAFLASH_CHIP1                      0\r
101 \r
102                         #if ((BOARD != BOARD_XPLAIN_REV1) || defined(__DOXYGEN__))\r
103                                 /** Internal main memory page size for the board's dataflash ICs. */\r
104                                 #define DATAFLASH_PAGE_SIZE              1024\r
105 \r
106                                 /** Total number of pages inside each of the board's dataflash ICs. */\r
107                                 #define DATAFLASH_PAGES                  8192\r
108                         #else\r
109                                 #define DATAFLASH_PAGE_SIZE              256\r
110 \r
111                                 #define DATAFLASH_PAGES                  2048\r
112                         #endif\r
113 \r
114                 /* Inline Functions: */\r
115                         /** Initializes the dataflash driver so that commands and data may be sent to an attached dataflash IC.\r
116                          *  The microcontroller's SPI driver MUST be initialized before any of the dataflash commands are used.\r
117                          */\r
118                         static inline void Dataflash_Init(void)\r
119                         {\r
120                                 DATAFLASH_CHIPCS_DDR  |= DATAFLASH_CHIPCS_MASK;\r
121                                 DATAFLASH_CHIPCS_PORT |= DATAFLASH_CHIPCS_MASK;\r
122                         }\r
123 \r
124                         /** Sends a byte to the currently selected dataflash IC, and returns a byte from the dataflash.\r
125                          *\r
126                          *  \param[in] Byte  Byte of data to send to the dataflash\r
127                          *\r
128                          *  \return Last response byte from the dataflash\r
129                          */\r
130                         static inline uint8_t Dataflash_TransferByte(const uint8_t Byte) ATTR_ALWAYS_INLINE;\r
131                         static inline uint8_t Dataflash_TransferByte(const uint8_t Byte)\r
132                         {\r
133                                 return SPI_TransferByte(Byte);\r
134                         }\r
135 \r
136                         /** Sends a byte to the currently selected dataflash IC, and ignores the next byte from the dataflash.\r
137                          *\r
138                          *  \param[in] Byte  Byte of data to send to the dataflash\r
139                          */\r
140                         static inline void Dataflash_SendByte(const uint8_t Byte) ATTR_ALWAYS_INLINE;\r
141                         static inline void Dataflash_SendByte(const uint8_t Byte)\r
142                         {\r
143                                 SPI_SendByte(Byte);\r
144                         }\r
145 \r
146                         /** Sends a dummy byte to the currently selected dataflash IC, and returns the next byte from the dataflash.\r
147                          *\r
148                          *  \return Last response byte from the dataflash\r
149                          */\r
150                         static inline uint8_t Dataflash_ReceiveByte(void) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT;\r
151                         static inline uint8_t Dataflash_ReceiveByte(void)\r
152                         {\r
153                                 return SPI_ReceiveByte();\r
154                         }\r
155 \r
156                         /** Determines the currently selected dataflash chip.\r
157                          *\r
158                          *  \return Mask of the currently selected Dataflash chip, either \ref DATAFLASH_NO_CHIP if no chip is selected\r
159                          *  or a DATAFLASH_CHIPn mask (where n is the chip number).\r
160                          */\r
161                         static inline uint8_t Dataflash_GetSelectedChip(void) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT;\r
162                         static inline uint8_t Dataflash_GetSelectedChip(void)\r
163                         {\r
164                                 return (DATAFLASH_CHIPCS_PORT & DATAFLASH_CHIPCS_MASK);\r
165                         }\r
166 \r
167                         /** Selects the given dataflash chip.\r
168                          *\r
169                          *  \param[in]  ChipMask  Mask of the Dataflash IC to select, in the form of DATAFLASH_CHIPn mask (where n is\r
170                          *              the chip number).\r
171                          */\r
172                         static inline void Dataflash_SelectChip(const uint8_t ChipMask) ATTR_ALWAYS_INLINE;\r
173                         static inline void Dataflash_SelectChip(const uint8_t ChipMask)\r
174                         {\r
175                                 DATAFLASH_CHIPCS_PORT = ((DATAFLASH_CHIPCS_PORT & ~DATAFLASH_CHIPCS_MASK) | ChipMask);\r
176                         }\r
177 \r
178                         /** Deselects the current dataflash chip, so that no dataflash is selected. */\r
179                         static inline void Dataflash_DeselectChip(void) ATTR_ALWAYS_INLINE;\r
180                         static inline void Dataflash_DeselectChip(void)\r
181                         {\r
182                                 Dataflash_SelectChip(DATAFLASH_NO_CHIP);\r
183                         }\r
184 \r
185                         /** Selects a dataflash IC from the given page number, which should range from 0 to\r
186                          *  ((DATAFLASH_PAGES * DATAFLASH_TOTALCHIPS) - 1). For boards containing only one\r
187                          *  dataflash IC, this will select DATAFLASH_CHIP1. If the given page number is outside\r
188                          *  the total number of pages contained in the boards dataflash ICs, all dataflash ICs\r
189                          *  are deselected.\r
190                          *\r
191                          *  \param[in] PageAddress  Address of the page to manipulate, ranging from\r
192                          *                          0 to ((DATAFLASH_PAGES * DATAFLASH_TOTALCHIPS) - 1).\r
193                          */\r
194                         static inline void Dataflash_SelectChipFromPage(const uint16_t PageAddress)\r
195                         {\r
196                                 Dataflash_DeselectChip();\r
197 \r
198                                 if (PageAddress >= DATAFLASH_PAGES)\r
199                                   return;\r
200 \r
201                                 Dataflash_SelectChip(DATAFLASH_CHIP1);\r
202                         }\r
203 \r
204                         /** Toggles the select line of the currently selected dataflash IC, so that it is ready to receive\r
205                          *  a new command.\r
206                          */\r
207                         static inline void Dataflash_ToggleSelectedChipCS(void)\r
208                         {\r
209                                 uint8_t SelectedChipMask = Dataflash_GetSelectedChip();\r
210 \r
211                                 Dataflash_DeselectChip();\r
212                                 Dataflash_SelectChip(SelectedChipMask);\r
213                         }\r
214 \r
215                         /** Spin-loops while the currently selected dataflash is busy executing a command, such as a main\r
216                          *  memory page program or main memory to buffer transfer.\r
217                          */\r
218                         static inline void Dataflash_WaitWhileBusy(void)\r
219                         {\r
220                                 Dataflash_ToggleSelectedChipCS();\r
221                                 Dataflash_SendByte(DF_CMD_GETSTATUS);\r
222                                 while (!(Dataflash_ReceiveByte() & DF_STATUS_READY));\r
223                                 Dataflash_ToggleSelectedChipCS();\r
224                         }\r
225 \r
226                         /** Sends a set of page and buffer address bytes to the currently selected dataflash IC, for use with\r
227                          *  dataflash commands which require a complete 24-bit address.\r
228                          *\r
229                          *  \param[in] PageAddress  Page address within the selected dataflash IC\r
230                          *  \param[in] BufferByte   Address within the dataflash's buffer\r
231                          */\r
232                         static inline void Dataflash_SendAddressBytes(uint16_t PageAddress,\r
233                                                                       const uint16_t BufferByte)\r
234                         {\r
235                                 Dataflash_SendByte(PageAddress >> 5);\r
236                                 Dataflash_SendByte((PageAddress << 3) | (BufferByte >> 8));\r
237                                 Dataflash_SendByte(BufferByte);\r
238                         }\r
239 \r
240 #endif\r
241 \r
242 /** @} */\r
243 \r