]> git.donarmstrong.com Git - qmk_firmware.git/blob - protocol/lufa/LUFA-120730/LUFA/Drivers/Board/AVR8/STK525/Dataflash.h
Change TOP_DIR to TMK_DIR in makefiles
[qmk_firmware.git] / protocol / lufa / LUFA-120730 / LUFA / Drivers / Board / AVR8 / STK525 / 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 STK525.\r
33  *  \copydetails Group_Dataflash_STK525\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_STK525 STK525\r
41  *  \brief Board specific Dataflash driver header for the Atmel STK525.\r
42  *\r
43  *  Board specific Dataflash driver header for the Atmel STK525.\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>AT45DB321C (4MB)</td><td>PORTB.4</td><td>SPI0</td></tr>\r
48  *  </table>\r
49  *\r
50  *  @{\r
51  */\r
52 \r
53 #ifndef __DATAFLASH_STK525_H__\r
54 #define __DATAFLASH_STK525_H__\r
55 \r
56         /* Includes: */\r
57                 #include "../../../../Common/Common.h"\r
58                 #include "../../../Misc/AT45DB321C.h"\r
59                 #include "../../../Peripheral/SPI.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_DDR                 DDRB\r
71                         #define DATAFLASH_CHIPCS_PORT                PORTB\r
72         #endif\r
73 \r
74         /* Public Interface - May be used in end-application: */\r
75                 /* Macros: */\r
76                         /** Constant indicating the total number of dataflash ICs mounted on the selected board. */\r
77                         #define DATAFLASH_TOTALCHIPS                 1\r
78 \r
79                         /** Mask for no dataflash chip selected. */\r
80                         #define DATAFLASH_NO_CHIP                    DATAFLASH_CHIPCS_MASK\r
81 \r
82                         /** Mask for the first dataflash chip selected. */\r
83                         #define DATAFLASH_CHIP1                      0\r
84 \r
85                         /** Internal main memory page size for the board's dataflash IC. */\r
86                         #define DATAFLASH_PAGE_SIZE                  512\r
87 \r
88                         /** Total number of pages inside the board's dataflash IC. */\r
89                         #define DATAFLASH_PAGES                      8192\r
90 \r
91                 /* Inline Functions: */\r
92                         /** Initializes the dataflash driver so that commands and data may be sent to an attached dataflash IC.\r
93                          *  The microcontroller's SPI driver MUST be initialized before any of the dataflash commands are used.\r
94                          */\r
95                         static inline void Dataflash_Init(void)\r
96                         {\r
97                                 DATAFLASH_CHIPCS_DDR  |= DATAFLASH_CHIPCS_MASK;\r
98                                 DATAFLASH_CHIPCS_PORT |= DATAFLASH_CHIPCS_MASK;\r
99                         }\r
100 \r
101                         /** Sends a byte to the currently selected dataflash IC, and returns a byte from the dataflash.\r
102                          *\r
103                          *  \param[in] Byte  Byte of data to send to the dataflash\r
104                          *\r
105                          *  \return Last response byte from the dataflash\r
106                          */\r
107                         static inline uint8_t Dataflash_TransferByte(const uint8_t Byte) ATTR_ALWAYS_INLINE;\r
108                         static inline uint8_t Dataflash_TransferByte(const uint8_t Byte)\r
109                         {\r
110                                 return SPI_TransferByte(Byte);\r
111                         }\r
112 \r
113                         /** Sends a byte to the currently selected dataflash IC, and ignores the next byte from the dataflash.\r
114                          *\r
115                          *  \param[in] Byte  Byte of data to send to the dataflash\r
116                          */\r
117                         static inline void Dataflash_SendByte(const uint8_t Byte) ATTR_ALWAYS_INLINE;\r
118                         static inline void Dataflash_SendByte(const uint8_t Byte)\r
119                         {\r
120                                 SPI_SendByte(Byte);\r
121                         }\r
122 \r
123                         /** Sends a dummy byte to the currently selected dataflash IC, and returns the next byte from the dataflash.\r
124                          *\r
125                          *  \return Last response byte from the dataflash\r
126                          */\r
127                         static inline uint8_t Dataflash_ReceiveByte(void) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT;\r
128                         static inline uint8_t Dataflash_ReceiveByte(void)\r
129                         {\r
130                                 return SPI_ReceiveByte();\r
131                         }\r
132 \r
133                         /** Determines the currently selected dataflash chip.\r
134                          *\r
135                          *  \return Mask of the currently selected Dataflash chip, either \ref DATAFLASH_NO_CHIP if no chip is selected\r
136                          *  or a DATAFLASH_CHIPn mask (where n is the chip number).\r
137                          */\r
138                         static inline uint8_t Dataflash_GetSelectedChip(void) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT;\r
139                         static inline uint8_t Dataflash_GetSelectedChip(void)\r
140                         {\r
141                                 return (DATAFLASH_CHIPCS_PORT & DATAFLASH_CHIPCS_MASK);\r
142                         }\r
143 \r
144                         /** Selects the given dataflash chip.\r
145                          *\r
146                          *  \param[in]  ChipMask  Mask of the Dataflash IC to select, in the form of DATAFLASH_CHIPn mask (where n is\r
147                          *              the chip number).\r
148                          */\r
149                         static inline void Dataflash_SelectChip(const uint8_t ChipMask) ATTR_ALWAYS_INLINE;\r
150                         static inline void Dataflash_SelectChip(const uint8_t ChipMask)\r
151                         {\r
152                                 DATAFLASH_CHIPCS_PORT = ((DATAFLASH_CHIPCS_PORT & ~DATAFLASH_CHIPCS_MASK) | ChipMask);\r
153                         }\r
154 \r
155                         /** Deselects the current dataflash chip, so that no dataflash is selected. */\r
156                         static inline void Dataflash_DeselectChip(void) ATTR_ALWAYS_INLINE;\r
157                         static inline void Dataflash_DeselectChip(void)\r
158                         {\r
159                                 Dataflash_SelectChip(DATAFLASH_NO_CHIP);\r
160                         }\r
161 \r
162                         /** Selects a dataflash IC from the given page number, which should range from 0 to\r
163                          *  ((DATAFLASH_PAGES * DATAFLASH_TOTALCHIPS) - 1). For boards containing only one\r
164                          *  dataflash IC, this will select DATAFLASH_CHIP1. If the given page number is outside\r
165                          *  the total number of pages contained in the boards dataflash ICs, all dataflash ICs\r
166                          *  are deselected.\r
167                          *\r
168                          *  \param[in] PageAddress  Address of the page to manipulate, ranging from\r
169                          *                          0 to ((DATAFLASH_PAGES * DATAFLASH_TOTALCHIPS) - 1).\r
170                          */\r
171                         static inline void Dataflash_SelectChipFromPage(const uint16_t PageAddress)\r
172                         {\r
173                                 Dataflash_DeselectChip();\r
174 \r
175                                 if (PageAddress >= DATAFLASH_PAGES)\r
176                                   return;\r
177 \r
178                                 Dataflash_SelectChip(DATAFLASH_CHIP1);\r
179                         }\r
180 \r
181                         /** Toggles the select line of the currently selected dataflash IC, so that it is ready to receive\r
182                          *  a new command.\r
183                          */\r
184                         static inline void Dataflash_ToggleSelectedChipCS(void)\r
185                         {\r
186                                 uint8_t SelectedChipMask = Dataflash_GetSelectedChip();\r
187 \r
188                                 Dataflash_DeselectChip();\r
189                                 Dataflash_SelectChip(SelectedChipMask);\r
190                         }\r
191 \r
192                         /** Spin-loops while the currently selected dataflash is busy executing a command, such as a main\r
193                          *  memory page program or main memory to buffer transfer.\r
194                          */\r
195                         static inline void Dataflash_WaitWhileBusy(void)\r
196                         {\r
197                                 Dataflash_ToggleSelectedChipCS();\r
198                                 Dataflash_SendByte(DF_CMD_GETSTATUS);\r
199                                 while (!(Dataflash_ReceiveByte() & DF_STATUS_READY));\r
200                                 Dataflash_ToggleSelectedChipCS();\r
201                         }\r
202 \r
203                         /** Sends a set of page and buffer address bytes to the currently selected dataflash IC, for use with\r
204                          *  dataflash commands which require a complete 24-bit address.\r
205                          *\r
206                          *  \param[in] PageAddress  Page address within the selected dataflash IC\r
207                          *  \param[in] BufferByte   Address within the dataflash's buffer\r
208                          */\r
209                         static inline void Dataflash_SendAddressBytes(uint16_t PageAddress,\r
210                                                                       const uint16_t BufferByte)\r
211                         {\r
212                                 Dataflash_SendByte(PageAddress >> 6);\r
213                                 Dataflash_SendByte((PageAddress << 2) | (BufferByte >> 8));\r
214                                 Dataflash_SendByte(BufferByte);\r
215                         }\r
216 \r
217 #endif\r
218 \r
219 /** @} */\r
220 \r