You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

Dataflash.h 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 LUFA Custom Board Dataflash Hardware Driver (Template)
  28. *
  29. * This is a stub driver header file, for implementing custom board
  30. * layout hardware with compatible LUFA board specific drivers. If
  31. * the library is configured to use the BOARD_USER board mode, this
  32. * driver file should be completed and copied into the "/Board/" folder
  33. * inside the application's folder.
  34. *
  35. * This stub is for the board-specific component of the LUFA Dataflash
  36. * driver.
  37. */
  38. #ifndef __DATAFLASH_USER_H__
  39. #define __DATAFLASH_USER_H__
  40. /* Includes: */
  41. // TODO: Add any required includes here
  42. /* Preprocessor Checks: */
  43. #if !defined(__INCLUDE_FROM_DATAFLASH_H)
  44. #error Do not include this file directly. Include LUFA/Drivers/Board/Dataflash.h instead.
  45. #endif
  46. /* Private Interface - For use in library only: */
  47. #if !defined(__DOXYGEN__)
  48. /* Macros: */
  49. #define DATAFLASH_CHIPCS_MASK // TODO: Replace this with a mask of all the /CS pins of all Dataflashes
  50. #define DATAFLASH_CHIPCS_DDR // TODO: Replace with the DDR register name for the board's Dataflash ICs
  51. #define DATAFLASH_CHIPCS_PORT // TODO: Replace with the PORT register name for the board's Dataflash ICs
  52. #endif
  53. /* Public Interface - May be used in end-application: */
  54. /* Macros: */
  55. /** Constant indicating the total number of dataflash ICs mounted on the selected board. */
  56. #define DATAFLASH_TOTALCHIPS 1 // TODO: Replace with the number of Dataflashes on the board, max 2
  57. /** Mask for no dataflash chip selected. */
  58. #define DATAFLASH_NO_CHIP DATAFLASH_CHIPCS_MASK
  59. /** Mask for the first dataflash chip selected. */
  60. #define DATAFLASH_CHIP1 // TODO: Replace with mask to hold /CS of first Dataflash low, and all others high
  61. /** Mask for the second dataflash chip selected. */
  62. #define DATAFLASH_CHIP2 // TODO: Replace with mask to hold /CS of second Dataflash low, and all others high
  63. /** Internal main memory page size for the board's dataflash ICs. */
  64. #define DATAFLASH_PAGE_SIZE // TODO: Replace with the page size for the Dataflash ICs
  65. /** Total number of pages inside each of the board's dataflash ICs. */
  66. #define DATAFLASH_PAGES // TODO: Replace with the total number of pages inside one of the Dataflash ICs
  67. /* Inline Functions: */
  68. /** Initializes the dataflash driver so that commands and data may be sent to an attached dataflash IC.
  69. * The microcontroller's SPI driver MUST be initialized before any of the dataflash commands are used.
  70. */
  71. static inline void Dataflash_Init(void)
  72. {
  73. DATAFLASH_CHIPCS_DDR |= DATAFLASH_CHIPCS_MASK;
  74. DATAFLASH_CHIPCS_PORT |= DATAFLASH_CHIPCS_MASK;
  75. }
  76. /** Sends a byte to the currently selected dataflash IC, and returns a byte from the dataflash.
  77. *
  78. * \param[in] Byte Byte of data to send to the dataflash
  79. *
  80. * \return Last response byte from the dataflash
  81. */
  82. static inline uint8_t Dataflash_TransferByte(const uint8_t Byte) ATTR_ALWAYS_INLINE;
  83. static inline uint8_t Dataflash_TransferByte(const uint8_t Byte)
  84. {
  85. // TODO
  86. }
  87. /** Sends a byte to the currently selected dataflash IC, and ignores the next byte from the dataflash.
  88. *
  89. * \param[in] Byte Byte of data to send to the dataflash
  90. */
  91. static inline void Dataflash_SendByte(const uint8_t Byte) ATTR_ALWAYS_INLINE;
  92. static inline void Dataflash_SendByte(const uint8_t Byte)
  93. {
  94. // TODO
  95. }
  96. /** Sends a dummy byte to the currently selected dataflash IC, and returns the next byte from the dataflash.
  97. *
  98. * \return Last response byte from the dataflash
  99. */
  100. static inline uint8_t Dataflash_ReceiveByte(void) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT;
  101. static inline uint8_t Dataflash_ReceiveByte(void)
  102. {
  103. // TODO
  104. }
  105. /** Determines the currently selected dataflash chip.
  106. *
  107. * \return Mask of the currently selected Dataflash chip, either \ref DATAFLASH_NO_CHIP if no chip is selected
  108. * or a DATAFLASH_CHIPn mask (where n is the chip number).
  109. */
  110. static inline uint8_t Dataflash_GetSelectedChip(void) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT;
  111. static inline uint8_t Dataflash_GetSelectedChip(void)
  112. {
  113. return (DATAFLASH_CHIPCS_PORT & DATAFLASH_CHIPCS_MASK);
  114. }
  115. /** Selects the given dataflash chip.
  116. *
  117. * \param[in] ChipMask Mask of the Dataflash IC to select, in the form of DATAFLASH_CHIPn mask (where n is
  118. * the chip number).
  119. */
  120. static inline void Dataflash_SelectChip(const uint8_t ChipMask) ATTR_ALWAYS_INLINE;
  121. static inline void Dataflash_SelectChip(const uint8_t ChipMask)
  122. {
  123. DATAFLASH_CHIPCS_PORT = ((DATAFLASH_CHIPCS_PORT & ~DATAFLASH_CHIPCS_MASK) | ChipMask);
  124. }
  125. /** Deselects the current dataflash chip, so that no dataflash is selected. */
  126. static inline void Dataflash_DeselectChip(void) ATTR_ALWAYS_INLINE;
  127. static inline void Dataflash_DeselectChip(void)
  128. {
  129. Dataflash_SelectChip(DATAFLASH_NO_CHIP);
  130. }
  131. /** Selects a dataflash IC from the given page number, which should range from 0 to
  132. * ((DATAFLASH_PAGES * DATAFLASH_TOTALCHIPS) - 1). For boards containing only one
  133. * dataflash IC, this will select DATAFLASH_CHIP1. If the given page number is outside
  134. * the total number of pages contained in the boards dataflash ICs, all dataflash ICs
  135. * are deselected.
  136. *
  137. * \param[in] PageAddress Address of the page to manipulate, ranging from
  138. * 0 to ((DATAFLASH_PAGES * DATAFLASH_TOTALCHIPS) - 1).
  139. */
  140. static inline void Dataflash_SelectChipFromPage(const uint16_t PageAddress)
  141. {
  142. Dataflash_DeselectChip();
  143. if (PageAddress >= (DATAFLASH_PAGES * DATAFLASH_TOTALCHIPS))
  144. return;
  145. #if (DATAFLASH_TOTALCHIPS == 2)
  146. if (PageAddress & 0x01)
  147. Dataflash_SelectChip(DATAFLASH_CHIP2);
  148. else
  149. Dataflash_SelectChip(DATAFLASH_CHIP1);
  150. #else
  151. Dataflash_SelectChip(DATAFLASH_CHIP1);
  152. #endif
  153. }
  154. /** Toggles the select line of the currently selected dataflash IC, so that it is ready to receive
  155. * a new command.
  156. */
  157. static inline void Dataflash_ToggleSelectedChipCS(void)
  158. {
  159. uint8_t SelectedChipMask = Dataflash_GetSelectedChip();
  160. Dataflash_DeselectChip();
  161. Dataflash_SelectChip(SelectedChipMask);
  162. }
  163. /** Spin-loops while the currently selected dataflash is busy executing a command, such as a main
  164. * memory page program or main memory to buffer transfer.
  165. */
  166. static inline void Dataflash_WaitWhileBusy(void)
  167. {
  168. Dataflash_ToggleSelectedChipCS();
  169. Dataflash_SendByte(DF_CMD_GETSTATUS);
  170. while (!(Dataflash_ReceiveByte() & DF_STATUS_READY));
  171. Dataflash_ToggleSelectedChipCS();
  172. }
  173. /** Sends a set of page and buffer address bytes to the currently selected dataflash IC, for use with
  174. * dataflash commands which require a complete 24-bit address.
  175. *
  176. * \param[in] PageAddress Page address within the selected dataflash IC
  177. * \param[in] BufferByte Address within the dataflash's buffer
  178. */
  179. static inline void Dataflash_SendAddressBytes(uint16_t PageAddress, const uint16_t BufferByte)
  180. {
  181. #if (DATAFLASH_TOTALCHIPS == 2)
  182. PageAddress >>= 1;
  183. #endif
  184. Dataflash_SendByte(PageAddress >> 5);
  185. Dataflash_SendByte((PageAddress << 3) | (BufferByte >> 8));
  186. Dataflash_SendByte(BufferByte);
  187. }
  188. #endif