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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. LUFA Library
  3. Copyright (C) Dean Camera, 2014.
  4. dean [at] fourwalledcubicle [dot] com
  5. www.lufa-lib.org
  6. */
  7. /*
  8. Copyright 2014 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 Board specific Dataflash driver header for the Atmel USBKEY.
  28. * \copydetails Group_Dataflash_USBKEY
  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_USBKEY USBKEY
  35. * \brief Board specific Dataflash driver header for the Atmel USBKEY.
  36. *
  37. * Board specific Dataflash driver header for the Atmel USBKEY board.
  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>AT45DB642D (8MB)</td><td>PORTE.0</td><td>SPI0</td></tr>
  42. * <tr><td>DATAFLASH_CHIP2</td><td>AT45DB642D (8MB)</td><td>PORTE.1</td><td>SPI0</td></tr>
  43. * </table>
  44. *
  45. * @{
  46. */
  47. #ifndef __DATAFLASH_USBKEY_H__
  48. #define __DATAFLASH_USBKEY_H__
  49. /* Includes: */
  50. #include "../../../../Common/Common.h"
  51. #include "../../../Misc/AT45DB642D.h"
  52. #include "../../../Peripheral/SPI.h"
  53. /* Preprocessor Checks: */
  54. #if !defined(__INCLUDE_FROM_DATAFLASH_H)
  55. #error Do not include this file directly. Include LUFA/Drivers/Board/Dataflash.h instead.
  56. #endif
  57. /* Private Interface - For use in library only: */
  58. #if !defined(__DOXYGEN__)
  59. /* Macros: */
  60. #define DATAFLASH_CHIPCS_MASK (DATAFLASH_CHIP1 | DATAFLASH_CHIP2)
  61. #define DATAFLASH_CHIPCS_DDR DDRE
  62. #define DATAFLASH_CHIPCS_PORT PORTE
  63. #endif
  64. /* Public Interface - May be used in end-application: */
  65. /* Macros: */
  66. /** Constant indicating the total number of dataflash ICs mounted on the selected board. */
  67. #define DATAFLASH_TOTALCHIPS 2
  68. /** Mask for no dataflash chip selected. */
  69. #define DATAFLASH_NO_CHIP 0
  70. /** Mask for the first dataflash chip selected. */
  71. #define DATAFLASH_CHIP1 (1 << 0)
  72. /** Mask for the second dataflash chip selected. */
  73. #define DATAFLASH_CHIP2 (1 << 1)
  74. /** Internal main memory page size for the board's dataflash ICs. */
  75. #define DATAFLASH_PAGE_SIZE 1024
  76. /** Total number of pages inside each of the board's dataflash ICs. */
  77. #define DATAFLASH_PAGES 8192
  78. /* Inline Functions: */
  79. /** Initializes the dataflash driver so that commands and data may be sent to an attached dataflash IC.
  80. * The appropriate SPI interface will be automatically configured.
  81. */
  82. static inline void Dataflash_Init(void)
  83. {
  84. DATAFLASH_CHIPCS_DDR |= DATAFLASH_CHIPCS_MASK;
  85. DATAFLASH_CHIPCS_PORT |= DATAFLASH_CHIPCS_MASK;
  86. SPI_Init(SPI_SPEED_FCPU_DIV_2 | SPI_ORDER_MSB_FIRST | SPI_SCK_LEAD_FALLING | SPI_SAMPLE_TRAILING | SPI_MODE_MASTER);
  87. }
  88. /** Sends a byte to the currently selected dataflash IC, and returns a byte from the dataflash.
  89. *
  90. * \param[in] Byte Byte of data to send to the dataflash
  91. *
  92. * \return Last response byte from the dataflash
  93. */
  94. static inline uint8_t Dataflash_TransferByte(const uint8_t Byte) ATTR_ALWAYS_INLINE;
  95. static inline uint8_t Dataflash_TransferByte(const uint8_t Byte)
  96. {
  97. return SPI_TransferByte(Byte);
  98. }
  99. /** Sends a byte to the currently selected dataflash IC, and ignores the next byte from the dataflash.
  100. *
  101. * \param[in] Byte Byte of data to send to the dataflash
  102. */
  103. static inline void Dataflash_SendByte(const uint8_t Byte) ATTR_ALWAYS_INLINE;
  104. static inline void Dataflash_SendByte(const uint8_t Byte)
  105. {
  106. SPI_SendByte(Byte);
  107. }
  108. /** Sends a dummy byte to the currently selected dataflash IC, and returns the next byte from the dataflash.
  109. *
  110. * \return Last response byte from the dataflash
  111. */
  112. static inline uint8_t Dataflash_ReceiveByte(void) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT;
  113. static inline uint8_t Dataflash_ReceiveByte(void)
  114. {
  115. return SPI_ReceiveByte();
  116. }
  117. /** Determines the currently selected dataflash chip.
  118. *
  119. * \return Mask of the currently selected Dataflash chip, either \ref DATAFLASH_NO_CHIP if no chip is selected
  120. * or a DATAFLASH_CHIPn mask (where n is the chip number).
  121. */
  122. static inline uint8_t Dataflash_GetSelectedChip(void) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT;
  123. static inline uint8_t Dataflash_GetSelectedChip(void)
  124. {
  125. return (~DATAFLASH_CHIPCS_PORT & DATAFLASH_CHIPCS_MASK);
  126. }
  127. /** Selects the given dataflash chip.
  128. *
  129. * \param[in] ChipMask Mask of the Dataflash IC to select, in the form of a \c DATAFLASH_CHIPn mask (where n is
  130. * the chip number).
  131. */
  132. static inline void Dataflash_SelectChip(const uint8_t ChipMask) ATTR_ALWAYS_INLINE;
  133. static inline void Dataflash_SelectChip(const uint8_t ChipMask)
  134. {
  135. DATAFLASH_CHIPCS_PORT = ((DATAFLASH_CHIPCS_PORT | DATAFLASH_CHIPCS_MASK) & ~ChipMask);
  136. }
  137. /** Deselects the current dataflash chip, so that no dataflash is selected. */
  138. static inline void Dataflash_DeselectChip(void) ATTR_ALWAYS_INLINE;
  139. static inline void Dataflash_DeselectChip(void)
  140. {
  141. Dataflash_SelectChip(DATAFLASH_NO_CHIP);
  142. }
  143. /** Selects a dataflash IC from the given page number, which should range from 0 to
  144. * ((DATAFLASH_PAGES * DATAFLASH_TOTALCHIPS) - 1). For boards containing only one
  145. * dataflash IC, this will select DATAFLASH_CHIP1. If the given page number is outside
  146. * the total number of pages contained in the boards dataflash ICs, all dataflash ICs
  147. * are deselected.
  148. *
  149. * \param[in] PageAddress Address of the page to manipulate, ranging from
  150. * 0 to ((DATAFLASH_PAGES * DATAFLASH_TOTALCHIPS) - 1).
  151. */
  152. static inline void Dataflash_SelectChipFromPage(const uint16_t PageAddress)
  153. {
  154. Dataflash_DeselectChip();
  155. if (PageAddress >= (DATAFLASH_PAGES * DATAFLASH_TOTALCHIPS))
  156. return;
  157. #if (DATAFLASH_TOTALCHIPS == 2)
  158. if (PageAddress & 0x01)
  159. Dataflash_SelectChip(DATAFLASH_CHIP2);
  160. else
  161. Dataflash_SelectChip(DATAFLASH_CHIP1);
  162. #else
  163. Dataflash_SelectChip(DATAFLASH_CHIP1);
  164. #endif
  165. }
  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. {
  171. uint8_t SelectedChipMask = Dataflash_GetSelectedChip();
  172. Dataflash_DeselectChip();
  173. Dataflash_SelectChip(SelectedChipMask);
  174. }
  175. /** Spin-loops while the currently selected dataflash is busy executing a command, such as a main
  176. * memory page program or main memory to buffer transfer.
  177. */
  178. static inline void Dataflash_WaitWhileBusy(void)
  179. {
  180. Dataflash_ToggleSelectedChipCS();
  181. Dataflash_SendByte(DF_CMD_GETSTATUS);
  182. while (!(Dataflash_ReceiveByte() & DF_STATUS_READY));
  183. Dataflash_ToggleSelectedChipCS();
  184. }
  185. /** Sends a set of page and buffer address bytes to the currently selected dataflash IC, for use with
  186. * dataflash commands which require a complete 24-bit address.
  187. *
  188. * \param[in] PageAddress Page address within the selected dataflash IC
  189. * \param[in] BufferByte Address within the dataflash's buffer
  190. */
  191. static inline void Dataflash_SendAddressBytes(uint16_t PageAddress,
  192. const uint16_t BufferByte)
  193. {
  194. #if (DATAFLASH_TOTALCHIPS == 2)
  195. PageAddress >>= 1;
  196. #endif
  197. Dataflash_SendByte(PageAddress >> 5);
  198. Dataflash_SendByte((PageAddress << 3) | (BufferByte >> 8));
  199. Dataflash_SendByte(BufferByte);
  200. }
  201. #endif
  202. /** @} */