hw_spi_common.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * @brief SPI ROM API declarations and functions
  3. *
  4. * @note
  5. * Copyright(C) NXP Semiconductors, 2014
  6. * All rights reserved.
  7. *
  8. * @par
  9. * Software that is described herein is for illustrative purposes only
  10. * which provides customers with programming information regarding the
  11. * LPC products. This software is supplied "AS IS" without any warranties of
  12. * any kind, and NXP Semiconductors and its licensor disclaim any and
  13. * all warranties, express or implied, including all implied warranties of
  14. * merchantability, fitness for a particular purpose and non-infringement of
  15. * intellectual property rights. NXP Semiconductors assumes no responsibility
  16. * or liability for the use of the software, conveys no license or rights under any
  17. * patent, copyright, mask work right, or any other intellectual property rights in
  18. * or to any products. NXP Semiconductors reserves the right to make changes
  19. * in the software without notification. NXP Semiconductors also makes no
  20. * representation or warranty that such application will be suitable for the
  21. * specified use without further testing or modification.
  22. *
  23. * @par
  24. * Permission to use, copy, modify, and distribute this software and its
  25. * documentation is hereby granted, under NXP Semiconductors' and its
  26. * licensor's relevant copyrights in the software, without fee, provided that it
  27. * is used in conjunction with NXP Semiconductors microcontrollers. This
  28. * copyright, permission, and disclaimer notice must appear in all copies of
  29. * this code.
  30. */
  31. #ifndef __HW_SPI_COMMON_H_
  32. #define __HW_SPI_COMMON_H_
  33. /**
  34. * @brief SPI register block structure
  35. */
  36. typedef struct { /*!< SPI Structure */
  37. volatile uint32_t CFG; /*!< SPI Configuration register */
  38. volatile uint32_t DLY; /*!< SPI Delay register */
  39. volatile uint32_t STAT; /*!< SPI Status register */
  40. volatile uint32_t INTENSET; /*!< SPI Interrupt Enable Set register */
  41. volatile uint32_t INTENCLR; /*!< SPI Interrupt Enable Clear register */
  42. volatile uint32_t RXDAT; /*!< SPI Receive Data register */
  43. volatile uint32_t TXDATCTL; /*!< SPI Transmit Data with Control register */
  44. volatile uint32_t TXDAT; /*!< SPI Transmit Data register */
  45. volatile uint32_t TXCTRL; /*!< SPI Transmit Control register */
  46. volatile uint32_t DIV; /*!< SPI clock Divider register */
  47. volatile uint32_t INTSTAT; /*!< SPI Interrupt Status register */
  48. } LPC_SPI_T;
  49. /**
  50. * Macro defines for SPI Configuration register
  51. */
  52. #define SPI_CFG_BITMASK (0xFBD) /** SPI register bit mask */
  53. #define SPI_CFG_SPI_EN (1 << 0) /** SPI Slave Mode Select */
  54. #define SPI_CFG_SLAVE_EN (0 << 0) /** SPI Master Mode Select */
  55. #define SPI_CFG_MASTER_EN (1 << 2) /** SPI MSB First mode enable */
  56. #define SPI_CFG_MSB_FIRST_EN (0 << 3) /** SPI LSB First mode enable */
  57. #define SPI_CFG_LSB_FIRST_EN (1 << 3) /** SPI Clock Phase Select */
  58. #define SPI_CFG_CPHA_FIRST (0 << 4) /** Capture data on the first edge, Change data on the following edge */
  59. #define SPI_CFG_CPHA_SECOND (1 << 4) /** SPI Clock Polarity Select */
  60. #define SPI_CFG_CPOL_LO (0 << 5) /** The rest state of the clock (between frames) is low. */
  61. #define SPI_CFG_CPOL_HI (1 << 5) /** The rest state of the clock (between frames) is high. */
  62. #define SPI_CFG_LBM_EN (1 << 7) /** SPI control 1 loopback mode enable */
  63. #define SPI_CFG_SPOL_LO (0 << 8) /** SPI SSEL0 Polarity Select */
  64. #define SPI_CFG_SPOL_HI (1 << 8) /** SSEL0 is active High */
  65. #define SPI_CFG_SPOLNUM_HI(n) (1 << ((n) + 8)) /** SSELN is active High, selects 0 - 3 */
  66. /**
  67. * Macro defines for SPI Delay register
  68. */
  69. #define SPI_DLY_BITMASK (0xFFFF) /** SPI DLY Register Mask */
  70. #define SPI_DLY_PRE_DELAY(n) (((n) & 0x0F) << 0) /** Time in SPI clocks between SSEL assertion and the beginning of a data frame */
  71. #define SPI_DLY_POST_DELAY(n) (((n) & 0x0F) << 4) /** Time in SPI clocks between the end of a data frame and SSEL deassertion. */
  72. #define SPI_DLY_FRAME_DELAY(n) (((n) & 0x0F) << 8) /** Minimum time in SPI clocks between adjacent data frames. */
  73. #define SPI_DLY_TRANSFER_DELAY(n) (((n) & 0x0F) << 12) /** Minimum time in SPI clocks that the SSEL is deasserted between transfers. */
  74. /**
  75. * Macro defines for SPI Status register
  76. */
  77. #define SPI_STAT_BITMASK (0x1FF) /** SPI STAT Register BitMask */
  78. #define SPI_STAT_RXRDY (1 << 0) /** Receiver Ready Flag */
  79. #define SPI_STAT_TXRDY (1 << 1) /** Transmitter Ready Flag */
  80. #define SPI_STAT_RXOV (1 << 2) /** Receiver Overrun interrupt flag */
  81. #define SPI_STAT_TXUR (1 << 3) /** Transmitter Underrun interrupt flag (In Slave Mode only) */
  82. #define SPI_STAT_SSA (1 << 4) /** Slave Select Assert */
  83. #define SPI_STAT_SSD (1 << 5) /** Slave Select Deassert */
  84. #define SPI_STAT_STALLED (1 << 6) /** Stalled status flag */
  85. #define SPI_STAT_EOT (1 << 7) /** End Transfer flag */
  86. #define SPI_STAT_MSTIDLE (1 << 8) /** Idle status flag */
  87. /**
  88. * Macro defines for SPI Interrupt Enable read and Set register
  89. */
  90. #define SPI_INTENSET_BITMASK (0x3F) /** SPI INTENSET Register BitMask */
  91. #define SPI_INTENSET_RXDYEN (1 << 0) /** Enable Interrupt when receiver data is available */
  92. #define SPI_INTENSET_TXDYEN (1 << 1) /** Enable Interrupt when the transmitter holding register is available. */
  93. #define SPI_INTENSET_RXOVEN (1 << 2) /** Enable Interrupt when a receiver overrun occurs */
  94. #define SPI_INTENSET_TXUREN (1 << 3) /** Enable Interrupt when a transmitter underrun occurs (In Slave Mode Only)*/
  95. #define SPI_INTENSET_SSAEN (1 << 4) /** Enable Interrupt when the Slave Select is asserted.*/
  96. #define SPI_INTENSET_SSDEN (1 << 5) /** Enable Interrupt when the Slave Select is deasserted..*/
  97. /**
  98. * Macro defines for SPI Interrupt Enable Clear register
  99. */
  100. #define SPI_INTENCLR_BITMASK (0x3F) /** SPI INTENCLR Register BitMask */
  101. #define SPI_INTENCLR_RXDYEN (1 << 0) /** Disable Interrupt when receiver data is available */
  102. #define SPI_INTENCLR_TXDYEN (1 << 1) /** Disable Interrupt when the transmitter holding register is available. */
  103. #define SPI_INTENCLR_RXOVEN (1 << 2) /** Disable Interrupt when a receiver overrun occurs */
  104. #define SPI_INTENCLR_TXUREN (1 << 3) /** Disable Interrupt when a transmitter underrun occurs (In Slave Mode Only) */
  105. #define SPI_INTENCLR_SSAEN (1 << 4) /** Disable Interrupt when the Slave Select is asserted. */
  106. #define SPI_INTENCLR_SSDEN (1 << 5) /** Disable Interrupt when the Slave Select is deasserted.. */
  107. /**
  108. * Macro defines for SPI Receiver Data register
  109. */
  110. #define SPI_RXDAT_BITMASK (0x1FFFFF) /** SPI RXDAT Register BitMask */
  111. #define SPI_RXDAT_DATA(n) ((n) & 0xFFFF) /** Receiver Data */
  112. #define SPI_RXDAT_RXSSELN_ACTIVE (0 << 16) /** The state of SSEL pin is active */
  113. #define SPI_RXDAT_RXSSELN_INACTIVE ((1 << 16) /** The state of SSEL pin is inactive */
  114. #define SPI_RXDAT_RXSSELNUM_INACTIVE(n) (1 << ((n) + 16)) /** The state of SSELN pin is inactive */
  115. #define SPI_RXDAT_SOT (1 << 20) /** Start of Transfer flag */
  116. /**
  117. * Macro defines for SPI Transmitter Data and Control register
  118. */
  119. #define SPI_TXDATCTL_BITMASK (0xF7FFFFF) /** SPI TXDATCTL Register BitMask */
  120. #define SPI_TXDATCTL_DATA(n) ((n) & 0xFFFF) /** SPI Transmit Data */
  121. #define SPI_TXDATCTL_CTRLMASK (0xF7F0000) /** SPI TXDATCTL Register BitMask for control bits only */
  122. #define SPI_TXDATCTL_ASSERT_SSEL (0 << 16) /** Assert SSEL0 pin */
  123. #define SPI_TXDATCTL_DEASSERT_SSEL (1 << 16) /** Deassert SSEL0 pin */
  124. #define SPI_TXDATCTL_DEASSERTNUM_SSEL(n) (1 << ((n) + 16)) /** Deassert SSELN pin */
  125. #define SPI_TXDATCTL_DEASSERT_ALL (0xF << 16) /** Deassert all SSEL pins */
  126. #define SPI_TXDATCTL_EOT (1 << 20) /** End of Transfer flag (TRANSFER_DELAY is applied after sending the current frame) */
  127. #define SPI_TXDATCTL_EOF (1 << 21) /** End of Frame flag (FRAME_DELAY is applied after sending the current part) */
  128. #define SPI_TXDATCTL_RXIGNORE (1 << 22) /** Receive Ignore Flag */
  129. #define SPI_TXDATCTL_FLEN(n) (((n) & 0x0F) << 24) /** Frame length - 1 */
  130. /**
  131. * Macro defines for SPI Transmitter Data Register
  132. */
  133. #define SPI_TXDAT_DATA(n) ((n) & 0xFFFF) /** SPI Transmit Data */
  134. /**
  135. * Macro defines for SPI Transmitter Control register
  136. */
  137. #define SPI_TXCTL_BITMASK (0xF7F0000) /** SPI TXDATCTL Register BitMask */
  138. #define SPI_TXCTL_ASSERT_SSEL (0 << 16) /** Assert SSEL0 pin */
  139. #define SPI_TXCTL_DEASSERT_SSEL (1 << 16) /** Deassert SSEL0 pin */
  140. #define SPI_TXCTL_DEASSERTNUM_SSEL(n) (1 << ((n) + 16)) /** Deassert SSELN pin */
  141. #define SPI_TXDATCTL_DEASSERT_ALL (0xF << 16) /** Deassert all SSEL pins */
  142. #define SPI_TXCTL_EOT (1 << 20) /** End of Transfer flag (TRANSFER_DELAY is applied after sending the current frame) */
  143. #define SPI_TXCTL_EOF (1 << 21) /** End of Frame flag (FRAME_DELAY is applied after sending the current part) */
  144. #define SPI_TXCTL_RXIGNORE (1 << 22) /** Receive Ignore Flag */
  145. #define SPI_TXCTL_FLEN(n) ((((n) - 1) & 0x0F) << 24) /** Frame length, 0 - 16 */
  146. #define SPI_TXCTL_FLENMASK (0xF << 24) /** Frame length mask */
  147. /**
  148. * Macro defines for SPI Divider register
  149. */
  150. #define SPI_DIV_VAL(n) ((n) & 0xFFFF) /** Rate divider value mask (In Master Mode only)*/
  151. /**
  152. * Macro defines for SPI Interrupt Status register
  153. */
  154. #define SPI_INTSTAT_BITMASK (0x3F) /** SPI INTSTAT Register Bitmask */
  155. #define SPI_INTSTAT_RXRDY (1 << 0) /** Receiver Ready Flag */
  156. #define SPI_INTSTAT_TXRDY (1 << 1) /** Transmitter Ready Flag */
  157. #define SPI_INTSTAT_RXOV (1 << 2) /** Receiver Overrun interrupt flag */
  158. #define SPI_INTSTAT_TXUR (1 << 3) /** Transmitter Underrun interrupt flag (In Slave Mode only) */
  159. #define SPI_INTSTAT_SSA (1 << 4) /** Slave Select Assert */
  160. #define SPI_INTSTAT_SSD (1 << 5) /** Slave Select Deassert */
  161. /** @brief SPI Clock Mode*/
  162. typedef enum {
  163. ROM_SPI_CLOCK_CPHA0_CPOL0 = 0, /**< CPHA = 0, CPOL = 0 */
  164. ROM_SPI_CLOCK_MODE0 = ROM_SPI_CLOCK_CPHA0_CPOL0, /**< Alias for CPHA = 0, CPOL = 0 */
  165. ROM_SPI_CLOCK_CPHA1_CPOL0 = 1, /**< CPHA = 0, CPOL = 1 */
  166. ROM_SPI_CLOCK_MODE1 = ROM_SPI_CLOCK_CPHA1_CPOL0, /**< Alias for CPHA = 0, CPOL = 1 */
  167. ROM_SPI_CLOCK_CPHA0_CPOL1 = 2, /**< CPHA = 1, CPOL = 0 */
  168. ROM_SPI_CLOCK_MODE2 = ROM_SPI_CLOCK_CPHA0_CPOL1, /**< Alias for CPHA = 1, CPOL = 0 */
  169. ROM_SPI_CLOCK_CPHA1_CPOL1 = 3, /**< CPHA = 1, CPOL = 1 */
  170. ROM_SPI_CLOCK_MODE3 = ROM_SPI_CLOCK_CPHA1_CPOL1, /**< Alias for CPHA = 1, CPOL = 1 */
  171. } ROM_SPI_CLOCK_MODE_T;
  172. #endif /* __HW_SPI_COMMON_H_ */