Dataflash.h 10 KB

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