ssp_001.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /*
  2. * @brief SSP Registers and control functions
  3. *
  4. * @note
  5. * Copyright(C) NXP Semiconductors, 2012
  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 __SSP_001_H_
  32. #define __SSP_001_H_
  33. #include "sys_config.h"
  34. #include "cmsis.h"
  35. #ifdef __cplusplus
  36. extern "C" {
  37. #endif
  38. /** @defgroup IP_SSP_001 IP: SSP register block and driver
  39. * @ingroup IP_Drivers
  40. * @{
  41. */
  42. /**
  43. * @brief SSP register block structure
  44. */
  45. typedef struct { /*!< SSPn Structure */
  46. __IO uint32_t CR0; /*!< Control Register 0. Selects the serial clock rate, bus type, and data size. */
  47. __IO uint32_t CR1; /*!< Control Register 1. Selects master/slave and other modes. */
  48. __IO uint32_t DR; /*!< Data Register. Writes fill the transmit FIFO, and reads empty the receive FIFO. */
  49. __I uint32_t SR; /*!< Status Register */
  50. __IO uint32_t CPSR; /*!< Clock Prescale Register */
  51. __IO uint32_t IMSC; /*!< Interrupt Mask Set and Clear Register */
  52. __I uint32_t RIS; /*!< Raw Interrupt Status Register */
  53. __I uint32_t MIS; /*!< Masked Interrupt Status Register */
  54. __O uint32_t ICR; /*!< SSPICR Interrupt Clear Register */
  55. #if !defined(CHIP_LPC111X_CXX) && !defined(CHIP_LPC11UXX) /* no DMA on LPC11xx or LPC11Uxx */
  56. __IO uint32_t DMACR; /*!< SSPn DMA control register */
  57. #endif
  58. } IP_SSP_001_Type;
  59. /**
  60. * Macro defines for CR0 register
  61. */
  62. /** SSP data size select, must be 4 bits to 16 bits */
  63. #define SSP_CR0_DSS(n) ((uint32_t) ((n) & 0xF))
  64. /** SSP control 0 Motorola SPI mode */
  65. #define SSP_CR0_FRF_SPI ((uint32_t) (0 << 4))
  66. /** SSP control 0 TI synchronous serial mode */
  67. #define SSP_CR0_FRF_TI ((uint32_t) (1 << 4))
  68. /** SSP control 0 National Micro-wire mode */
  69. #define SSP_CR0_FRF_MICROWIRE ((uint32_t) (2 << 4))
  70. /** SPI clock polarity bit (used in SPI mode only), (1) = maintains the
  71. bus clock high between frames, (0) = low */
  72. #define SSP_CR0_CPOL_LO ((uint32_t) (0))
  73. #define SSP_CR0_CPOL_HI ((uint32_t) (1 << 6))
  74. /** SPI clock out phase bit (used in SPI mode only), (1) = captures data
  75. on the second clock transition of the frame, (0) = first */
  76. #define SSP_CR0_CPHA_FIRST ((uint32_t) (0))
  77. #define SSP_CR0_CPHA_SECOND ((uint32_t) (1 << 7))
  78. /** SSP serial clock rate value load macro, divider rate is
  79. PERIPH_CLK / (cpsr * (SCR + 1)) */
  80. #define SSP_CR0_SCR(n) ((uint32_t) ((n & 0xFF) << 8))
  81. /** SSP CR0 bit mask */
  82. #define SSP_CR0_BITMASK ((uint32_t) (0xFFFF))
  83. /** SSP CR0 bit mask */
  84. #define SSP_CR0_BITMASK ((uint32_t) (0xFFFF))
  85. /** SSP serial clock rate value load macro, divider rate is
  86. PERIPH_CLK / (cpsr * (SCR + 1)) */
  87. #define SSP_CR0_SCR(n) ((uint32_t) ((n & 0xFF) << 8))
  88. /**
  89. * Macro defines for CR1 register
  90. */
  91. /** SSP control 1 loopback mode enable bit */
  92. #define SSP_CR1_LBM_EN ((uint32_t) (1 << 0))
  93. /** SSP control 1 enable bit */
  94. #define SSP_CR1_SSP_EN ((uint32_t) (1 << 1))
  95. /** SSP control 1 slave enable */
  96. #define SSP_CR1_SLAVE_EN ((uint32_t) (1 << 2))
  97. #define SSP_CR1_MASTER_EN ((uint32_t) (0))
  98. /** SSP control 1 slave out disable bit, disables transmit line in slave
  99. mode */
  100. #define SSP_CR1_SO_DISABLE ((uint32_t) (1 << 3))
  101. /** SSP CR1 bit mask */
  102. #define SSP_CR1_BITMASK ((uint32_t) (0x0F))
  103. /** SSP CPSR bit mask */
  104. #define SSP_CPSR_BITMASK ((uint32_t) (0xFF))
  105. /**
  106. * Macro defines for DR register
  107. */
  108. /** SSP data bit mask */
  109. #define SSP_DR_BITMASK(n) ((n) & 0xFFFF)
  110. /**
  111. * Macro defines for SR register
  112. */
  113. /** SSP SR bit mask */
  114. #define SSP_SR_BITMASK ((uint32_t) (0x1F))
  115. /** ICR bit mask */
  116. #define SSP_ICR_BITMASK ((uint32_t) (0x03))
  117. /**
  118. * @brief SSP Type of Status
  119. */
  120. typedef enum {
  121. SSP_STAT_TFE = ((uint32_t)(1 << 0)),/**< TX FIFO Empty */
  122. SSP_STAT_TNF = ((uint32_t)(1 << 1)),/**< TX FIFO not full */
  123. SSP_STAT_RNE = ((uint32_t)(1 << 2)),/**< RX FIFO not empty */
  124. SSP_STAT_RFF = ((uint32_t)(1 << 3)),/**< RX FIFO full */
  125. SSP_STAT_BSY = ((uint32_t)(1 << 4)),/**< SSP Busy */
  126. } SSP_Status_Type;
  127. /**
  128. * @brief SSP Type of Interrupt Mask
  129. */
  130. typedef enum {
  131. SSP_RORIM = ((uint32_t)(1 << 0)), /**< Overun */
  132. SSP_RTIM = ((uint32_t)(1 << 1)),/**< TimeOut */
  133. SSP_RXIM = ((uint32_t)(1 << 2)),/**< Rx FIFO is at least half full */
  134. SSP_TXIM = ((uint32_t)(1 << 3)),/**< Tx FIFO is at least half empty */
  135. SSP_INT_MASK_BITMASK = ((uint32_t)(0xF)),
  136. } SSP_Int_Mask_Type;
  137. /**
  138. * @brief SSP Type of Mask Interrupt Status
  139. */
  140. typedef enum {
  141. SSP_RORMIS = ((uint32_t)(1 << 0)), /**< Overun */
  142. SSP_RTMIS = ((uint32_t)(1 << 1)), /**< TimeOut */
  143. SSP_RXMIS = ((uint32_t)(1 << 2)), /**< Rx FIFO is at least half full */
  144. SSP_TXMIS = ((uint32_t)(1 << 3)), /**< Tx FIFO is at least half empty */
  145. SSP_MASK_INT_STAT_BITMASK = ((uint32_t)(0xF)),
  146. } SSP_Mask_Int_Status_Type;
  147. /**
  148. * @brief SSP Type of Raw Interrupt Status
  149. */
  150. typedef enum {
  151. SSP_RORRIS = ((uint32_t)(1 << 0)), /**< Overun */
  152. SSP_RTRIS = ((uint32_t)(1 << 1)), /**< TimeOut */
  153. SSP_RXRIS = ((uint32_t)(1 << 2)), /**< Rx FIFO is at least half full */
  154. SSP_TXRIS = ((uint32_t)(1 << 3)), /**< Tx FIFO is at least half empty */
  155. SSP_RAW_INT_STAT_BITMASK = ((uint32_t)(0xF)),
  156. } SSP_Raw_Int_Status_Type;
  157. typedef enum {
  158. SSP_RORIC = 0x0,
  159. SSP_RTIC = 0x1,
  160. SSP_INT_CLEAR_BITMASK = 0x3,
  161. } SSP_Int_Clear_Type;
  162. typedef enum SSP_DMA_Type {
  163. SSP_DMA_RX = (1u), /**< DMA RX Enable */
  164. SSP_DMA_TX = (1u << 1), /**< DMA TX Enable */
  165. } SSP_DMA_Type;
  166. /**
  167. * @brief Disable SSP operation
  168. * @param pSSP : The base of SSP peripheral on the chip
  169. * @return Nothing
  170. * The SSP controller is disabled
  171. */
  172. void IP_SSP_DeInit(IP_SSP_001_Type *pSSP);
  173. /**
  174. * @brief Enable/Disable SSP operation
  175. * @param pSSP : The base of SSP peripheral on the chip
  176. * @param NewState : New state, ENABLE or DISABLE
  177. * @return Nothing
  178. */
  179. void IP_SSP_Cmd(IP_SSP_001_Type *pSSP, FunctionalState NewState);
  180. /**
  181. * @brief Enable/Disable loopback mode
  182. * @param pSSP : The base of SSP peripheral on the chip
  183. * @param NewState : New state, ENABLE or DISABLE
  184. * @return Nothing
  185. * Serial input is taken from the serial output (MOSI or MISO) rather
  186. * than the serial input pin
  187. */
  188. void IP_SSP_LoopBackCmd(IP_SSP_001_Type *pSSP, FunctionalState NewState);
  189. /**
  190. * @brief Get the current status of SSP controller
  191. * @param pSSP : The base of SSP peripheral on the chip
  192. * @param Stat : Type of status, should be :
  193. * - SSP_STAT_TFE
  194. * - SSP_STAT_TNF
  195. * - SSP_STAT_RNE
  196. * - SSP_STAT_RFF
  197. * - SSP_STAT_BSY
  198. * @return SSP controller status, SET or RESET
  199. */
  200. FlagStatus IP_SSP_GetStatus(IP_SSP_001_Type *pSSP, SSP_Status_Type Stat);
  201. /**
  202. * @brief Get the masked interrupt status
  203. * @param pSSP : The base of SSP peripheral on the chip
  204. * @return SSP Masked Interrupt Status Register value
  205. * The return value contains a 1 for each interrupt condition that is asserted and enabled (masked)
  206. */
  207. uint32_t IP_SSP_GetIntStatus(IP_SSP_001_Type *pSSP);
  208. /**
  209. * @brief Get the raw interrupt status
  210. * @param pSSP : The base of SSP peripheral on the chip
  211. * @param RawInt : Interrupt condition to be get status, shoud be :
  212. * - SSP_RORRIS
  213. * - SSP_RTRIS
  214. * - SSP_RXRIS
  215. * - SSP_TXRIS
  216. * @return Raw interrupt status corresponding to interrupt condition , SET or RESET
  217. * Get the status of each interrupt condition ,regardless of whether or not the interrupt is enabled
  218. */
  219. IntStatus IP_SSP_GetRawIntStatus(IP_SSP_001_Type *pSSP, SSP_Raw_Int_Status_Type RawInt);
  220. /**
  221. * @brief Get the number of bits transferred in each frame
  222. * @param pSSP : The base of SSP peripheral on the chip
  223. * @return the number of bits transferred in each frame minus one
  224. * The return value is 0x03 -> 0xF corresponding to 4bit -> 16bit transfer
  225. */
  226. uint8_t IP_SSP_GetDataSize(IP_SSP_001_Type *pSSP);
  227. /**
  228. * @brief Clear the corresponding interrupt condition(s) in the SSP controller
  229. * @param pSSP : The base of SSP peripheral on the chip
  230. * @param IntClear: Type of cleared interrupt, should be :
  231. * - SSP_RORIC
  232. * - SSP_RTIC
  233. * @return Nothing
  234. * Software can clear one or more interrupt condition(s) in the SSP controller
  235. */
  236. void IP_SSP_ClearIntPending(IP_SSP_001_Type *pSSP, SSP_Int_Clear_Type IntClear);
  237. /**
  238. * @brief Enable/Disable interrupt for the SSP
  239. * @param pSSP : The base of SSP peripheral on the chip
  240. * @param IntType : Type of interrupt condition to be enable/disable, should be :
  241. * - SSP_RORIM
  242. * - SSP_RTIM
  243. * - SSP_RXIM
  244. * - SSP_TXIM
  245. * @param NewState : New state, ENABLE or DISABLE
  246. * @return Nothing
  247. */
  248. void IP_SSP_Int_Enable(IP_SSP_001_Type *pSSP, SSP_Int_Mask_Type IntType, FunctionalState NewState);
  249. /**
  250. * @brief Get received SSP data
  251. * @param pSSP : The base of SSP peripheral on the chip
  252. * @return SSP 16-bit data received
  253. */
  254. uint16_t IP_SSP_ReceiveFrame(IP_SSP_001_Type *pSSP);
  255. /**
  256. * @brief Send SSP 16-bit data
  257. * @param pSSP : The base of SSP peripheral on the chip
  258. * @param tx_data : SSP 16-bit data to be transmited
  259. * @return Nothing
  260. */
  261. void IP_SSP_SendFrame(IP_SSP_001_Type *pSSP, uint16_t tx_data);
  262. /**
  263. * @brief Set up output clocks per bit for SSP bus
  264. * @param pSSP : The base of SSP peripheral on the chip
  265. * @param clk_rate fs: The number of prescaler-output clocks per bit on the bus, minus one
  266. * @param prescale : The factor by which the Prescaler divides the SSP peripheral clock PCLK
  267. * @return Nothing
  268. * The bit frequency is PCLK / (prescale x[clk_rate+1])
  269. */
  270. void IP_SSP_Set_ClockRate(IP_SSP_001_Type *pSSP, uint32_t clk_rate, uint32_t prescale);
  271. /**
  272. * @brief Set up the SSP frame format
  273. * @param pSSP : The base of SSP peripheral on the chip
  274. * @param bits : The number of bits transferred in each frame, should be SSP_BITS_4 to SSP_BITS_16
  275. * @param frameFormat : Frame format, should be :
  276. * - SSP_FRAMEFORMAT_SPI
  277. * - SSP_FRAMEFORMAT_TI
  278. * - SSP_FRAMEFORMAT_MICROWIRE
  279. * @param clockFormat : Select Clock polarity and Clock phase, should be :
  280. * - SSP_CLOCK_CPHA0_CPOL0
  281. * - SSP_CLOCK_CPHA0_CPOL1
  282. * - SSP_CLOCK_CPHA1_CPOL0
  283. * - SSP_CLOCK_CPHA1_CPOL1
  284. * @return Nothing
  285. * Note: The clockFormat is only used in SPI mode
  286. */
  287. void IP_SSP_Set_Format(IP_SSP_001_Type *pSSP, uint32_t bits, uint32_t frameFormat, uint32_t clockFormat);
  288. /**
  289. * @brief Set the SSP working as master or slave mode
  290. * @param pSSP : The base of SSP peripheral on the chip
  291. * @param mode : Operating mode, should be
  292. * - SSP_MODE_MASTER
  293. * - SSP_MODE_SLAVE
  294. * @return Nothing
  295. */
  296. void IP_SSP_Set_Mode(IP_SSP_001_Type *pSSP, uint32_t mode);
  297. /**
  298. * @brief Enable/Disable DMA for SSP
  299. * @param pSSP : The base of SSP peripheral on the chip
  300. * @param ssp_dma_t : DMA set up for transmit/receive SSP, should be
  301. * - SSP_DMA_RX
  302. * - SSP_DMA_TX
  303. * @param NewState : New state, ENABLE or DISABLE
  304. * @return Nothing
  305. */
  306. void IP_SSP_DMA_Cmd(IP_SSP_001_Type *pSSP, SSP_DMA_Type ssp_dma_t, FunctionalState NewState);
  307. /**
  308. * @}
  309. */
  310. #ifdef __cplusplus
  311. }
  312. #endif
  313. #endif /* __SSP_001_H_ */