Keyboard firmwares for Atmel AVR and Cortex-M
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.

Dataflash.h 8.2KB

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 Board specific Dataflash driver header for the Atmel EVK527.
  28. * \copydetails Group_Dataflash_EVK527
  29. *
  30. * \note This file should not be included directly. It is automatically included as needed by the dataflash driver
  31. * dispatch header located in LUFA/Drivers/Board/Dataflash.h.
  32. */
  33. /** \ingroup Group_Dataflash
  34. * \defgroup Group_Dataflash_EVK527 EVK527
  35. * \brief Board specific Dataflash driver header for the Atmel EVK527.
  36. *
  37. * Board specific Dataflash driver header for the Atmel EVK527.
  38. *
  39. * <table>
  40. * <tr><th>Name</th><th>Info</th><th>Select Pin</th><th>SPI Port</th></tr>
  41. * <tr><td>DATAFLASH_CHIP1</td><td>AT45DB321C (4MB)</td><td>PORTE.6</td><td>SPI0</td></tr>
  42. * </table>
  43. *
  44. * @{
  45. */
  46. #ifndef __DATAFLASH_EVK527_H__
  47. #define __DATAFLASH_EVK527_H__
  48. /* Includes: */
  49. #include "../../../../Common/Common.h"
  50. #include "../../../Misc/AT45DB321C.h"
  51. #include "../../../Peripheral/SPI.h"
  52. /* Preprocessor Checks: */
  53. #if !defined(__INCLUDE_FROM_DATAFLASH_H)
  54. #error Do not include this file directly. Include LUFA/Drivers/Board/Dataflash.h instead.
  55. #endif
  56. /* Private Interface - For use in library only: */
  57. #if !defined(__DOXYGEN__)
  58. /* Macros: */
  59. #define DATAFLASH_CHIPCS_MASK (1 << 6)
  60. #define DATAFLASH_CHIPCS_DDR DDRE
  61. #define DATAFLASH_CHIPCS_PORT PORTE
  62. #endif
  63. /* Public Interface - May be used in end-application: */
  64. /* Macros: */
  65. /** Constant indicating the total number of dataflash ICs mounted on the selected board. */
  66. #define DATAFLASH_TOTALCHIPS 1
  67. /** Mask for no dataflash chip selected. */
  68. #define DATAFLASH_NO_CHIP DATAFLASH_CHIPCS_MASK
  69. /** Mask for the first dataflash chip selected. */
  70. #define DATAFLASH_CHIP1 0
  71. /** Internal main memory page size for the board's dataflash IC. */
  72. #define DATAFLASH_PAGE_SIZE 512
  73. /** Total number of pages inside the board's dataflash IC. */
  74. #define DATAFLASH_PAGES 8192
  75. /* Inline Functions: */
  76. /** Initializes the dataflash driver so that commands and data may be sent to an attached dataflash IC.
  77. * The microcontroller's SPI driver MUST be initialized before any of the dataflash commands are used.
  78. */
  79. static inline void Dataflash_Init(void)
  80. {
  81. DATAFLASH_CHIPCS_DDR |= DATAFLASH_CHIPCS_MASK;
  82. DATAFLASH_CHIPCS_PORT |= DATAFLASH_CHIPCS_MASK;
  83. }
  84. /** Sends a byte to the currently selected dataflash IC, and returns a byte from the dataflash.
  85. *
  86. * \param[in] Byte Byte of data to send to the dataflash
  87. *
  88. * \return Last response byte from the dataflash
  89. */
  90. static inline uint8_t Dataflash_TransferByte(const uint8_t Byte) ATTR_ALWAYS_INLINE;
  91. static inline uint8_t Dataflash_TransferByte(const uint8_t Byte)
  92. {
  93. return SPI_TransferByte(Byte);
  94. }
  95. /** Sends a byte to the currently selected dataflash IC, and ignores the next byte from the dataflash.
  96. *
  97. * \param[in] Byte Byte of data to send to the dataflash
  98. */
  99. static inline void Dataflash_SendByte(const uint8_t Byte) ATTR_ALWAYS_INLINE;
  100. static inline void Dataflash_SendByte(const uint8_t Byte)
  101. {
  102. SPI_SendByte(Byte);
  103. }
  104. /** Sends a dummy byte to the currently selected dataflash IC, and returns the next byte from the dataflash.
  105. *
  106. * \return Last response byte from the dataflash
  107. */
  108. static inline uint8_t Dataflash_ReceiveByte(void) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT;
  109. static inline uint8_t Dataflash_ReceiveByte(void)
  110. {
  111. return SPI_ReceiveByte();
  112. }
  113. /** Determines the currently selected dataflash chip.
  114. *
  115. * \return Mask of the currently selected Dataflash chip, either \ref DATAFLASH_NO_CHIP if no chip is selected
  116. * or a DATAFLASH_CHIPn mask (where n is the chip number).
  117. */
  118. static inline uint8_t Dataflash_GetSelectedChip(void) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT;
  119. static inline uint8_t Dataflash_GetSelectedChip(void)
  120. {
  121. return (DATAFLASH_CHIPCS_PORT & DATAFLASH_CHIPCS_MASK);
  122. }
  123. /** Selects the given dataflash chip.
  124. *
  125. * \param[in] ChipMask Mask of the Dataflash IC to select, in the form of DATAFLASH_CHIPn mask (where n is
  126. * the chip number).
  127. */
  128. static inline void Dataflash_SelectChip(const uint8_t ChipMask) ATTR_ALWAYS_INLINE;
  129. static inline void Dataflash_SelectChip(const uint8_t ChipMask)
  130. {
  131. DATAFLASH_CHIPCS_PORT = ((DATAFLASH_CHIPCS_PORT & ~DATAFLASH_CHIPCS_MASK) | ChipMask);
  132. }
  133. /** Deselects the current dataflash chip, so that no dataflash is selected. */
  134. static inline void Dataflash_DeselectChip(void) ATTR_ALWAYS_INLINE;
  135. static inline void Dataflash_DeselectChip(void)
  136. {
  137. Dataflash_SelectChip(DATAFLASH_NO_CHIP);
  138. }
  139. /** Selects a dataflash IC from the given page number, which should range from 0 to
  140. * ((DATAFLASH_PAGES * DATAFLASH_TOTALCHIPS) - 1). For boards containing only one
  141. * dataflash IC, this will select DATAFLASH_CHIP1. If the given page number is outside
  142. * the total number of pages contained in the boards dataflash ICs, all dataflash ICs
  143. * are deselected.
  144. *
  145. * \param[in] PageAddress Address of the page to manipulate, ranging from
  146. * 0 to ((DATAFLASH_PAGES * DATAFLASH_TOTALCHIPS) - 1).
  147. */
  148. static inline void Dataflash_SelectChipFromPage(const uint16_t PageAddress)
  149. {
  150. Dataflash_DeselectChip();
  151. if (PageAddress >= DATAFLASH_PAGES)
  152. return;
  153. Dataflash_SelectChip(DATAFLASH_CHIP1);
  154. }
  155. /** Toggles the select line of the currently selected dataflash IC, so that it is ready to receive
  156. * a new command.
  157. */
  158. static inline void Dataflash_ToggleSelectedChipCS(void)
  159. {
  160. uint8_t SelectedChipMask = Dataflash_GetSelectedChip();
  161. Dataflash_DeselectChip();
  162. Dataflash_SelectChip(SelectedChipMask);
  163. }
  164. /** Spin-loops while the currently selected dataflash is busy executing a command, such as a main
  165. * memory page program or main memory to buffer transfer.
  166. */
  167. static inline void Dataflash_WaitWhileBusy(void)
  168. {
  169. Dataflash_ToggleSelectedChipCS();
  170. Dataflash_SendByte(DF_CMD_GETSTATUS);
  171. while (!(Dataflash_ReceiveByte() & DF_STATUS_READY));
  172. Dataflash_ToggleSelectedChipCS();
  173. }
  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. {
  183. Dataflash_SendByte(PageAddress >> 5);
  184. Dataflash_SendByte((PageAddress << 3) | (BufferByte >> 8));
  185. Dataflash_SendByte(BufferByte);
  186. }
  187. #endif
  188. /** @} */