Statistics
| Branch: | Tag: | Revision:

root / LUFA / Drivers / Board / Dataflash.h @ 978b99e5

History | View | Annotate | Download (10.1 KB)

1
/*
2
             LUFA Library
3
     Copyright (C) Dean Camera, 2011.
4

5
  dean [at] fourwalledcubicle [dot] com
6
           www.lufa-lib.org
7
*/
8

    
9
/*
10
  Copyright 2011  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 disclaim 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_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_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_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 SPI and board Dataflash drivers before first use
70
 *      SPI_Init(SPI_SPEED_FCPU_DIV_2 | SPI_ORDER_MSB_FIRST | SPI_SCK_LEAD_FALLING |
71
 *               SPI_SAMPLE_TRAILING | SPI_MODE_MASTER);
72
 *      Dataflash_Init();
73
 *
74
 *      uint8_t WriteBuffer[DATAFLASH_PAGE_SIZE];
75
 *      uint8_t ReadBuffer[DATAFLASH_PAGE_SIZE];
76
 *
77
 *      // Fill page write buffer with a repeating pattern
78
 *      for (uint16_t i = 0; i < DATAFLASH_PAGE_SIZE; i++)
79
 *        WriteBuffer[i] = (i & 0xFF);
80
 *
81
 *      // Must select the chip of interest first before operating on it
82
 *      Dataflash_SelectChip(DATAFLASH_CHIP1);
83
 *
84
 *      // Write to the Dataflash's first internal memory buffer
85
 *      printf("Writing data to first dataflash buffer:\r\n");
86
 *      Dataflash_SendByte(DF_CMD_BUFF1WRITE);
87
 *      Dataflash_SendAddressBytes(0, 0);
88
 *
89
 *      for (uint16_t i = 0; i < DATAFLASH_PAGE_SIZE; i++)
90
 *        Dataflash_SendByte(WriteBuffer[i]);
91
 *
92
 *      // Commit the Dataflash's first memory buffer to the non-volatile FLASH memory
93
 *      printf("Committing page to non-volatile memory page index 5:\r\n");
94
 *      Dataflash_SendByte(DF_CMD_BUFF1TOMAINMEMWITHERASE);
95
 *      Dataflash_SendAddressBytes(5, 0);
96
 *      Dataflash_WaitWhileBusy();
97
 *
98
 *      // Read the page from non-volatile FLASH memory into the Dataflash's second memory buffer
99
 *      printf("Reading data into second dataflash buffer:\r\n");
100
 *      Dataflash_SendByte(DF_CMD_MAINMEMTOBUFF2);
101
 *      Dataflash_SendAddressBytes(5, 0);
102
 *      Dataflash_WaitWhileBusy();
103
 *
104
 *      // Read the Dataflash's second internal memory buffer
105
 *      Dataflash_SendByte(DF_CMD_BUFF2READ);
106
 *      Dataflash_SendAddressBytes(0, 0);
107
 *
108
 *      for (uint16_t i = 0; i < DATAFLASH_PAGE_SIZE; i++)
109
 *        ReadBuffer[i] = Dataflash_ReceiveByte();
110
 *
111
 *      // Deselect the chip after use
112
 *      Dataflash_DeselectChip();
113
 *  \endcode
114
 *
115
 *  @{
116
 */
117

    
118
#ifndef __DATAFLASH_H__
119
#define __DATAFLASH_H__
120

    
121
        /* Macros: */
122
                #define __INCLUDE_FROM_DATAFLASH_H
123

    
124
        /* Includes: */
125
        #include "../../Common/Common.h"
126
        #include "../Peripheral/SPI.h"
127

    
128
        /* Enable C linkage for C++ Compilers: */
129
                #if defined(__cplusplus)
130
                        extern "C" {
131
                #endif
132

    
133
        /* Public Interface - May be used in end-application: */
134
                /* Macros: */
135
                        #if !defined(__DOXYGEN__)
136
                                #define __GET_DATAFLASH_MASK2(x, y) x ## y
137
                                #define __GET_DATAFLASH_MASK(x)     __GET_DATAFLASH_MASK2(DATAFLASH_CHIP,x)
138
                        #endif
139

    
140
                        /** Retrieves the Dataflash chip select mask for the given Dataflash chip index.
141
                         *
142
                         *  \param[in] index  Index of the dataflash chip mask to retrieve
143
                         *
144
                         *  \return Mask for the given Dataflash chip's /CS pin
145
                         */
146
                        #define DATAFLASH_CHIP_MASK(index)      __GET_DATAFLASH_MASK(index)
147

    
148
                /* Inline Functions: */
149
                        /** Initializes the dataflash driver so that commands and data may be sent to an attached dataflash IC.
150
                         *
151
                         *  \note The microcontroller's SPI driver must be initialized before any of the dataflash commands are used.
152
                         */
153
                        static inline void Dataflash_Init(void);
154

    
155
                        /** Determines the currently selected dataflash chip.
156
                         *
157
                         *  \return Mask of the currently selected Dataflash chip, either \ref DATAFLASH_NO_CHIP if no chip is selected
158
                         *  or a \c DATAFLASH_CHIPn mask (where n is the chip number).
159
                         */
160
                        static inline uint8_t Dataflash_GetSelectedChip(void) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT;
161

    
162
                        /** Selects the given dataflash chip.
163
                         *
164
                         *  \param[in]  ChipMask  Mask of the Dataflash IC to select, in the form of \c DATAFLASH_CHIPn mask (where n is
165
                         *              the chip number).
166
                         */
167
                        static inline void Dataflash_SelectChip(const uint8_t ChipMask) ATTR_ALWAYS_INLINE;
168

    
169
                        /** Deselects the current dataflash chip, so that no dataflash is selected. */
170
                        static inline void Dataflash_DeselectChip(void) ATTR_ALWAYS_INLINE;
171

    
172
                        /** Selects a dataflash IC from the given page number, which should range from 0 to
173
                         *  ((DATAFLASH_PAGES * DATAFLASH_TOTALCHIPS) - 1). For boards containing only one
174
                         *  dataflash IC, this will select \ref DATAFLASH_CHIP1. If the given page number is outside
175
                         *  the total number of pages contained in the boards dataflash ICs, all dataflash ICs
176
                         *  are deselected.
177
                         *
178
                         *  \param[in] PageAddress  Address of the page to manipulate, ranging from
179
                         *                          0 to ((DATAFLASH_PAGES * DATAFLASH_TOTALCHIPS) - 1).
180
                         */
181
                        static inline void Dataflash_SelectChipFromPage(const uint16_t PageAddress);
182

    
183
                        /** Toggles the select line of the currently selected dataflash IC, so that it is ready to receive
184
                         *  a new command.
185
                         */
186
                        static inline void Dataflash_ToggleSelectedChipCS(void);
187

    
188
                        /** Spin-loops while the currently selected dataflash is busy executing a command, such as a main
189
                         *  memory page program or main memory to buffer transfer.
190
                         */
191
                        static inline void Dataflash_WaitWhileBusy(void);
192

    
193
                        /** Sends a set of page and buffer address bytes to the currently selected dataflash IC, for use with
194
                         *  dataflash commands which require a complete 24-bit address.
195
                         *
196
                         *  \param[in] PageAddress  Page address within the selected dataflash IC
197
                         *  \param[in] BufferByte   Address within the dataflash's buffer
198
                         */
199
                        static inline void Dataflash_SendAddressBytes(uint16_t PageAddress,
200
                                                                      const uint16_t BufferByte);
201

    
202
                        /** Sends a byte to the currently selected dataflash IC, and returns a byte from the dataflash.
203
                         *
204
                         *  \param[in] Byte of data to send to the dataflash
205
                         *
206
                         *  \return Last response byte from the dataflash
207
                         */
208
                        static inline uint8_t Dataflash_TransferByte(const uint8_t Byte) ATTR_ALWAYS_INLINE;
209
                        static inline uint8_t Dataflash_TransferByte(const uint8_t Byte)
210
                        {
211
                                return SPI_TransferByte(Byte);
212
                        }
213

    
214
                        /** Sends a byte to the currently selected dataflash IC, and ignores the next byte from the dataflash.
215
                         *
216
                         *  \param[in] Byte of data to send to the dataflash
217
                         */
218
                        static inline void Dataflash_SendByte(const uint8_t Byte) ATTR_ALWAYS_INLINE;
219
                        static inline void Dataflash_SendByte(const uint8_t Byte)
220
                        {
221
                                SPI_SendByte(Byte);
222
                        }
223

    
224
                        /** Sends a dummy byte to the currently selected dataflash IC, and returns the next byte from the dataflash.
225
                         *
226
                         *  \return Last response byte from the dataflash
227
                         */
228
                        static inline uint8_t Dataflash_ReceiveByte(void) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT;
229
                        static inline uint8_t Dataflash_ReceiveByte(void)
230
                        {
231
                                return SPI_ReceiveByte();
232
                        }
233

    
234
                /* Includes: */
235
                        #if (BOARD == BOARD_NONE)
236
                                #error The Board Dataflash driver cannot be used if the makefile BOARD option is not set.
237
                        #elif (BOARD == BOARD_USBKEY)
238
                                #include "AVR8/USBKEY/Dataflash.h"
239
                        #elif (BOARD == BOARD_STK525)
240
                                #include "AVR8/STK525/Dataflash.h"
241
                        #elif (BOARD == BOARD_STK526)
242
                                #include "AVR8/STK526/Dataflash.h"
243
                        #elif ((BOARD == BOARD_XPLAIN) || (BOARD == BOARD_XPLAIN_REV1))
244
                                #include "AVR8/XPLAIN/Dataflash.h"
245
                        #elif (BOARD == BOARD_EVK527)
246
                                #include "AVR8/EVK527/Dataflash.h"
247
                        #else
248
                                #include "Board/Dataflash.h"
249
                        #endif
250

    
251
        /* Disable C linkage for C++ Compilers: */
252
                #if defined(__cplusplus)
253
                        }
254
                #endif
255

    
256
#endif
257

    
258
/** @} */
259