]> git.donarmstrong.com Git - qmk_firmware.git/blob - LUFA/Drivers/Board/Dataflash.h
Squashed 'lib/lufa/' content from commit 385d40300
[qmk_firmware.git] / LUFA / Drivers / Board / Dataflash.h
1 /*
2              LUFA Library
3      Copyright (C) Dean Camera, 2017.
4
5   dean [at] fourwalledcubicle [dot] com
6            www.lufa-lib.org
7 */
8
9 /*
10   Copyright 2017  Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12   Permission to use, copy, modify, distribute, and sell this
13   software and its documentation for any purpose is hereby granted
14   without fee, provided that the above copyright notice appear in
15   all copies and that both that the copyright notice and this
16   permission notice and warranty disclaimer appear in supporting
17   documentation, and that the name of the author not be used in
18   advertising or publicity pertaining to distribution of the
19   software without specific, written prior permission.
20
21   The author disclaims all warranties with regard to this
22   software, including all implied warranties of merchantability
23   and fitness.  In no event shall the author be liable for any
24   special, indirect or consequential damages or any damages
25   whatsoever resulting from loss of use, data or profits, whether
26   in an action of contract, negligence or other tortious action,
27   arising out of or in connection with the use or performance of
28   this software.
29 */
30
31 /** \file
32  *  \brief Master include file for the board dataflash IC driver.
33  *  \brief Atmel Dataflash storage IC board hardware driver.
34  *
35  *  This file is the master dispatch header file for the board-specific Atmel dataflash driver, for boards containing
36  *  Atmel Dataflash ICs for external non-volatile storage.
37  *
38  *  User code should include this file, which will in turn include the correct dataflash driver header file for
39  *  the currently selected board.
40  *
41  *  If the \c BOARD value is set to \c BOARD_USER, this will include the \c /Board/Dataflash.h file in the user project
42  *  directory.
43  *
44  *  For possible \c BOARD makefile values, see \ref Group_BoardTypes.
45  */
46
47 /** \ingroup Group_BoardDrivers
48  *  \defgroup Group_Dataflash Dataflash Driver - LUFA/Drivers/Board/Dataflash.h
49  *  \brief Atmel Dataflash storage IC board hardware driver.
50  *
51  *  \section Sec_Dataflash_Dependencies Module Source Dependencies
52  *  The following files must be built with any user project that uses this module:
53  *    - None
54  *
55  *  \section Sec_Dataflash_ModDescription Module Description
56  *  Dataflash driver. This module provides an easy to use interface for the Dataflash ICs located on many boards,
57  *  for the storage of large amounts of data into the Dataflash's non-volatile memory.
58  *
59  *  If the \c BOARD value is set to \c BOARD_USER, this will include the \c /Board/Dataflash.h file in the user project
60  *  directory. Otherwise, it will include the appropriate built-in board driver header file.
61  *
62  *  For possible \c BOARD makefile values, see \ref Group_BoardTypes.
63  *
64  *  \section Sec_Dataflash_ExampleUsage Example Usage
65  *  The following snippet is an example of how this module may be used within a typical
66  *  application.
67  *
68  *  \code
69  *      // Initialize the board Dataflash driver before first use
70  *      Dataflash_Init();
71  *
72  *      uint8_t WriteBuffer[DATAFLASH_PAGE_SIZE];
73  *      uint8_t ReadBuffer[DATAFLASH_PAGE_SIZE];
74  *
75  *      // Fill page write buffer with a repeating pattern
76  *      for (uint16_t i = 0; i < DATAFLASH_PAGE_SIZE; i++)
77  *        WriteBuffer[i] = (i & 0xFF);
78  *
79  *      // Must select the chip of interest first before operating on it
80  *      Dataflash_SelectChip(DATAFLASH_CHIP1);
81  *
82  *      // Write to the Dataflash's first internal memory buffer
83  *      printf("Writing data to first dataflash buffer:\r\n");
84  *      Dataflash_SendByte(DF_CMD_BUFF1WRITE);
85  *      Dataflash_SendAddressBytes(0, 0);
86  *
87  *      for (uint16_t i = 0; i < DATAFLASH_PAGE_SIZE; i++)
88  *        Dataflash_SendByte(WriteBuffer[i]);
89  *
90  *      // Commit the Dataflash's first memory buffer to the non-volatile FLASH memory
91  *      printf("Committing page to non-volatile memory page index 5:\r\n");
92  *      Dataflash_SendByte(DF_CMD_BUFF1TOMAINMEMWITHERASE);
93  *      Dataflash_SendAddressBytes(5, 0);
94  *      Dataflash_WaitWhileBusy();
95  *
96  *      // Read the page from non-volatile FLASH memory into the Dataflash's second memory buffer
97  *      printf("Reading data into second dataflash buffer:\r\n");
98  *      Dataflash_SendByte(DF_CMD_MAINMEMTOBUFF2);
99  *      Dataflash_SendAddressBytes(5, 0);
100  *      Dataflash_WaitWhileBusy();
101  *
102  *      // Read the Dataflash's second internal memory buffer
103  *      Dataflash_SendByte(DF_CMD_BUFF2READ);
104  *      Dataflash_SendAddressBytes(0, 0);
105  *
106  *      for (uint16_t i = 0; i < DATAFLASH_PAGE_SIZE; i++)
107  *        ReadBuffer[i] = Dataflash_ReceiveByte();
108  *
109  *      // Deselect the chip after use
110  *      Dataflash_DeselectChip();
111  *  \endcode
112  *
113  *  @{
114  */
115
116 #ifndef __DATAFLASH_H__
117 #define __DATAFLASH_H__
118
119         /* Macros: */
120                 #define __INCLUDE_FROM_DATAFLASH_H
121
122         /* Includes: */
123                 #include "../../Common/Common.h"
124
125         /* Enable C linkage for C++ Compilers: */
126                 #if defined(__cplusplus)
127                         extern "C" {
128                 #endif
129
130         /* Public Interface - May be used in end-application: */
131                 /* Macros: */
132                         /** Retrieves the Dataflash chip select mask for the given Dataflash chip index.
133                          *
134                          *  \attention This macro will only work correctly on chip index numbers that are compile-time
135                          *             constants defined by the preprocessor.
136                          *
137                          *  \param[in] index  Index of the dataflash chip mask to retrieve.
138                          *
139                          *  \return Mask for the given Dataflash chip's /CS pin
140                          */
141                         #define DATAFLASH_CHIP_MASK(index)      CONCAT_EXPANDED(DATAFLASH_CHIP, index)
142
143                 /* Inline Functions: */
144                         /** Initializes the dataflash driver so that commands and data may be sent to an attached dataflash IC.
145                          *
146                          *  \note The microcontroller's physical interface driver connected to the Dataflash IC must be initialized before
147                          *        any of the dataflash commands are used. This is usually a SPI hardware port, but on some devices/boards may
148                          *        be a USART operating in SPI Master mode.
149                          */
150                         static inline void Dataflash_Init(void);
151
152                         /** Determines the currently selected dataflash chip.
153                          *
154                          *  \return Mask of the currently selected Dataflash chip, either \ref DATAFLASH_NO_CHIP if no chip is selected
155                          *  or a \c DATAFLASH_CHIPn mask (where n is the chip number).
156                          */
157                         static inline uint8_t Dataflash_GetSelectedChip(void) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT;
158
159                         /** Selects the given dataflash chip.
160                          *
161                          *  \param[in]  ChipMask  Mask of the Dataflash IC to select, in the form of a \c DATAFLASH_CHIPn mask (where n is
162                          *              the chip number).
163                          */
164                         static inline void Dataflash_SelectChip(const uint8_t ChipMask) ATTR_ALWAYS_INLINE;
165
166                         /** Deselects the current dataflash chip, so that no dataflash is selected. */
167                         static inline void Dataflash_DeselectChip(void) ATTR_ALWAYS_INLINE;
168
169                         /** Selects a dataflash IC from the given page number, which should range from 0 to
170                          *  ((DATAFLASH_PAGES * DATAFLASH_TOTALCHIPS) - 1). For boards containing only one
171                          *  dataflash IC, this will select \ref DATAFLASH_CHIP1. If the given page number is outside
172                          *  the total number of pages contained in the boards dataflash ICs, all dataflash ICs
173                          *  are deselected.
174                          *
175                          *  \param[in] PageAddress  Address of the page to manipulate, ranging from
176                          *                          0 to ((DATAFLASH_PAGES * DATAFLASH_TOTALCHIPS) - 1).
177                          */
178                         static inline void Dataflash_SelectChipFromPage(const uint16_t PageAddress);
179
180                         /** Toggles the select line of the currently selected dataflash IC, so that it is ready to receive
181                          *  a new command.
182                          */
183                         static inline void Dataflash_ToggleSelectedChipCS(void);
184
185                         /** Spin-loops while the currently selected dataflash is busy executing a command, such as a main
186                          *  memory page program or main memory to buffer transfer.
187                          */
188                         static inline void Dataflash_WaitWhileBusy(void);
189
190                         /** Sends a set of page and buffer address bytes to the currently selected dataflash IC, for use with
191                          *  dataflash commands which require a complete 24-bit address.
192                          *
193                          *  \param[in] PageAddress  Page address within the selected dataflash IC
194                          *  \param[in] BufferByte   Address within the dataflash's buffer
195                          */
196                         static inline void Dataflash_SendAddressBytes(uint16_t PageAddress,
197                                                                       const uint16_t BufferByte);
198
199                         /** Sends a byte to the currently selected dataflash IC, and returns a byte from the dataflash.
200                          *
201                          *  \param[in] Byte  Byte of data to send to the dataflash
202                          *
203                          *  \return Last response byte from the dataflash
204                          */
205                         static inline uint8_t Dataflash_TransferByte(const uint8_t Byte) ATTR_ALWAYS_INLINE;
206
207                         /** Sends a byte to the currently selected dataflash IC, and ignores the next byte from the dataflash.
208                          *
209                          *  \param[in] Byte  Byte of data to send to the dataflash
210                          */
211                         static inline void Dataflash_SendByte(const uint8_t Byte) ATTR_ALWAYS_INLINE;
212
213                         /** Sends a dummy byte to the currently selected dataflash IC, and returns the next byte from the dataflash.
214                          *
215                          *  \return Last response byte from the dataflash
216                          */
217                         static inline uint8_t Dataflash_ReceiveByte(void) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT;
218
219                 /* Includes: */
220                         #if (BOARD == BOARD_NONE)
221                                 #define DATAFLASH_TOTALCHIPS  0
222                                 #define DATAFLASH_NO_CHIP     0
223                                 #define DATAFLASH_CHIP1       0
224                                 #define DATAFLASH_PAGE_SIZE   0
225                                 #define DATAFLASH_PAGES       0
226                                 static inline void    Dataflash_Init(void) {};
227                                 static inline uint8_t Dataflash_TransferByte(const uint8_t Byte) { return 0; };
228                                 static inline void    Dataflash_SendByte(const uint8_t Byte) {};
229                                 static inline uint8_t Dataflash_ReceiveByte(void) { return 0; };
230                                 static inline uint8_t Dataflash_GetSelectedChip(void) { return 0; };
231                                 static inline void    Dataflash_SelectChip(const uint8_t ChipMask) {};
232                                 static inline void    Dataflash_DeselectChip(void) {};
233                                 static inline void    Dataflash_SelectChipFromPage(const uint16_t PageAddress) {};
234                                 static inline void    Dataflash_ToggleSelectedChipCS(void) {};
235                                 static inline void    Dataflash_WaitWhileBusy(void) {};
236                                 static inline void    Dataflash_SendAddressBytes(uint16_t PageAddress,
237                                                                                  const uint16_t BufferByte) {};
238                         #elif (BOARD == BOARD_USBKEY)
239                                 #include "AVR8/USBKEY/Dataflash.h"
240                         #elif (BOARD == BOARD_STK525)
241                                 #include "AVR8/STK525/Dataflash.h"
242                         #elif (BOARD == BOARD_STK526)
243                                 #include "AVR8/STK526/Dataflash.h"
244                         #elif ((BOARD == BOARD_XPLAIN) || (BOARD == BOARD_XPLAIN_REV1))
245                                 #include "AVR8/XPLAIN/Dataflash.h"
246                         #elif (BOARD == BOARD_EVK527)
247                                 #include "AVR8/EVK527/Dataflash.h"
248                         #elif (BOARD == BOARD_A3BU_XPLAINED)
249                                 #include "XMEGA/A3BU_XPLAINED/Dataflash.h"
250                         #elif (BOARD == BOARD_B1_XPLAINED)
251                                 #include "XMEGA/B1_XPLAINED/Dataflash.h"
252                         #else
253                                 #include "Board/Dataflash.h"
254                         #endif
255
256         /* Disable C linkage for C++ Compilers: */
257                 #if defined(__cplusplus)
258                         }
259                 #endif
260
261 #endif
262
263 /** @} */
264