Dataflash.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. LUFA Library
  3. Copyright (C) Dean Camera, 2018.
  4. dean [at] fourwalledcubicle [dot] com
  5. www.lufa-lib.org
  6. */
  7. /*
  8. Copyright 2018 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 disclaims 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_Dataflash_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_Dataflash_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_Dataflash_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 board Dataflash driver before first use
  64. * Dataflash_Init();
  65. *
  66. * uint8_t WriteBuffer[DATAFLASH_PAGE_SIZE];
  67. * uint8_t ReadBuffer[DATAFLASH_PAGE_SIZE];
  68. *
  69. * // Fill page write buffer with a repeating pattern
  70. * for (uint16_t i = 0; i < DATAFLASH_PAGE_SIZE; i++)
  71. * WriteBuffer[i] = (i & 0xFF);
  72. *
  73. * // Must select the chip of interest first before operating on it
  74. * Dataflash_SelectChip(DATAFLASH_CHIP1);
  75. *
  76. * // Write to the Dataflash's first internal memory buffer
  77. * printf("Writing data to first dataflash buffer:\r\n");
  78. * Dataflash_SendByte(DF_CMD_BUFF1WRITE);
  79. * Dataflash_SendAddressBytes(0, 0);
  80. *
  81. * for (uint16_t i = 0; i < DATAFLASH_PAGE_SIZE; i++)
  82. * Dataflash_SendByte(WriteBuffer[i]);
  83. *
  84. * // Commit the Dataflash's first memory buffer to the non-volatile FLASH memory
  85. * printf("Committing page to non-volatile memory page index 5:\r\n");
  86. * Dataflash_SendByte(DF_CMD_BUFF1TOMAINMEMWITHERASE);
  87. * Dataflash_SendAddressBytes(5, 0);
  88. * Dataflash_WaitWhileBusy();
  89. *
  90. * // Read the page from non-volatile FLASH memory into the Dataflash's second memory buffer
  91. * printf("Reading data into second dataflash buffer:\r\n");
  92. * Dataflash_SendByte(DF_CMD_MAINMEMTOBUFF2);
  93. * Dataflash_SendAddressBytes(5, 0);
  94. * Dataflash_WaitWhileBusy();
  95. *
  96. * // Read the Dataflash's second internal memory buffer
  97. * Dataflash_SendByte(DF_CMD_BUFF2READ);
  98. * Dataflash_SendAddressBytes(0, 0);
  99. *
  100. * for (uint16_t i = 0; i < DATAFLASH_PAGE_SIZE; i++)
  101. * ReadBuffer[i] = Dataflash_ReceiveByte();
  102. *
  103. * // Deselect the chip after use
  104. * Dataflash_DeselectChip();
  105. * \endcode
  106. *
  107. * @{
  108. */
  109. #ifndef __DATAFLASH_H__
  110. #define __DATAFLASH_H__
  111. /* Macros: */
  112. #define __INCLUDE_FROM_DATAFLASH_H
  113. /* Includes: */
  114. #include "../../Common/Common.h"
  115. /* Enable C linkage for C++ Compilers: */
  116. #if defined(__cplusplus)
  117. extern "C" {
  118. #endif
  119. /* Public Interface - May be used in end-application: */
  120. /* Macros: */
  121. /** Retrieves the Dataflash chip select mask for the given Dataflash chip index.
  122. *
  123. * \attention This macro will only work correctly on chip index numbers that are compile-time
  124. * constants defined by the preprocessor.
  125. *
  126. * \param[in] index Index of the dataflash chip mask to retrieve.
  127. *
  128. * \return Mask for the given Dataflash chip's /CS pin
  129. */
  130. #define DATAFLASH_CHIP_MASK(index) CONCAT_EXPANDED(DATAFLASH_CHIP, index)
  131. /* Inline Functions: */
  132. /** Initializes the dataflash driver so that commands and data may be sent to an attached dataflash IC.
  133. *
  134. * \note The microcontroller's physical interface driver connected to the Dataflash IC must be initialized before
  135. * any of the dataflash commands are used. This is usually a SPI hardware port, but on some devices/boards may
  136. * be a USART operating in SPI Master mode.
  137. */
  138. static inline void Dataflash_Init(void);
  139. /** Determines the currently selected dataflash chip.
  140. *
  141. * \return Mask of the currently selected Dataflash chip, either \ref DATAFLASH_NO_CHIP if no chip is selected
  142. * or a \c DATAFLASH_CHIPn mask (where n is the chip number).
  143. */
  144. static inline uint8_t Dataflash_GetSelectedChip(void) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT;
  145. /** Selects the given dataflash chip.
  146. *
  147. * \param[in] ChipMask Mask of the Dataflash IC to select, in the form of a \c DATAFLASH_CHIPn mask (where n is
  148. * the chip number).
  149. */
  150. static inline void Dataflash_SelectChip(const uint8_t ChipMask) ATTR_ALWAYS_INLINE;
  151. /** Deselects the current dataflash chip, so that no dataflash is selected. */
  152. static inline void Dataflash_DeselectChip(void) ATTR_ALWAYS_INLINE;
  153. /** Selects a dataflash IC from the given page number, which should range from 0 to
  154. * ((DATAFLASH_PAGES * DATAFLASH_TOTALCHIPS) - 1). For boards containing only one
  155. * dataflash IC, this will select \ref DATAFLASH_CHIP1. If the given page number is outside
  156. * the total number of pages contained in the boards dataflash ICs, all dataflash ICs
  157. * are deselected.
  158. *
  159. * \param[in] PageAddress Address of the page to manipulate, ranging from
  160. * 0 to ((DATAFLASH_PAGES * DATAFLASH_TOTALCHIPS) - 1).
  161. */
  162. static inline void Dataflash_SelectChipFromPage(const uint16_t PageAddress);
  163. /** Toggles the select line of the currently selected dataflash IC, so that it is ready to receive
  164. * a new command.
  165. */
  166. static inline void Dataflash_ToggleSelectedChipCS(void);
  167. /** Spin-loops while the currently selected dataflash is busy executing a command, such as a main
  168. * memory page program or main memory to buffer transfer.
  169. */
  170. static inline void Dataflash_WaitWhileBusy(void);
  171. /** Sends a set of page and buffer address bytes to the currently selected dataflash IC, for use with
  172. * dataflash commands which require a complete 24-bit address.
  173. *
  174. * \param[in] PageAddress Page address within the selected dataflash IC
  175. * \param[in] BufferByte Address within the dataflash's buffer
  176. */
  177. static inline void Dataflash_SendAddressBytes(uint16_t PageAddress,
  178. const uint16_t BufferByte);
  179. /** Sends a byte to the currently selected dataflash IC, and returns a byte from the dataflash.
  180. *
  181. * \param[in] Byte Byte of data to send to the dataflash
  182. *
  183. * \return Last response byte from the dataflash
  184. */
  185. static inline uint8_t Dataflash_TransferByte(const uint8_t Byte) ATTR_ALWAYS_INLINE;
  186. /** Sends a byte to the currently selected dataflash IC, and ignores the next byte from the dataflash.
  187. *
  188. * \param[in] Byte Byte of data to send to the dataflash
  189. */
  190. static inline void Dataflash_SendByte(const uint8_t Byte) ATTR_ALWAYS_INLINE;
  191. /** Sends a dummy byte to the currently selected dataflash IC, and returns the next byte from the dataflash.
  192. *
  193. * \return Last response byte from the dataflash
  194. */
  195. static inline uint8_t Dataflash_ReceiveByte(void) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT;
  196. /* Includes: */
  197. #if (BOARD == BOARD_NONE)
  198. #define DATAFLASH_TOTALCHIPS 0
  199. #define DATAFLASH_NO_CHIP 0
  200. #define DATAFLASH_CHIP1 0
  201. #define DATAFLASH_PAGE_SIZE 0
  202. #define DATAFLASH_PAGES 0
  203. static inline void Dataflash_Init(void) {};
  204. static inline uint8_t Dataflash_TransferByte(const uint8_t Byte) { return 0; };
  205. static inline void Dataflash_SendByte(const uint8_t Byte) {};
  206. static inline uint8_t Dataflash_ReceiveByte(void) { return 0; };
  207. static inline uint8_t Dataflash_GetSelectedChip(void) { return 0; };
  208. static inline void Dataflash_SelectChip(const uint8_t ChipMask) {};
  209. static inline void Dataflash_DeselectChip(void) {};
  210. static inline void Dataflash_SelectChipFromPage(const uint16_t PageAddress) {};
  211. static inline void Dataflash_ToggleSelectedChipCS(void) {};
  212. static inline void Dataflash_WaitWhileBusy(void) {};
  213. static inline void Dataflash_SendAddressBytes(uint16_t PageAddress,
  214. const uint16_t BufferByte) {};
  215. #elif (BOARD == BOARD_USBKEY)
  216. #include "AVR8/USBKEY/Dataflash.h"
  217. #elif (BOARD == BOARD_STK525)
  218. #include "AVR8/STK525/Dataflash.h"
  219. #elif (BOARD == BOARD_STK526)
  220. #include "AVR8/STK526/Dataflash.h"
  221. #elif ((BOARD == BOARD_XPLAIN) || (BOARD == BOARD_XPLAIN_REV1))
  222. #include "AVR8/XPLAIN/Dataflash.h"
  223. #elif (BOARD == BOARD_EVK527)
  224. #include "AVR8/EVK527/Dataflash.h"
  225. #elif (BOARD == BOARD_A3BU_XPLAINED)
  226. #include "XMEGA/A3BU_XPLAINED/Dataflash.h"
  227. #elif (BOARD == BOARD_B1_XPLAINED)
  228. #include "XMEGA/B1_XPLAINED/Dataflash.h"
  229. #else
  230. #include "Board/Dataflash.h"
  231. #endif
  232. /* Disable C linkage for C++ Compilers: */
  233. #if defined(__cplusplus)
  234. }
  235. #endif
  236. #endif
  237. /** @} */