1
0

lpc_ssp.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. /**********************************************************************
  2. * $Id$ lpc_ssp.h 2011-06-02
  3. *//**
  4. * @file lpc_ssp.h
  5. * @brief Contains all macro definitions and function prototypes
  6. * support for SSP firmware library on LPC
  7. * @version 1.0
  8. * @date 02. June. 2011
  9. * @author NXP MCU SW Application Team
  10. *
  11. * Copyright(C) 2011, NXP Semiconductor
  12. * All rights reserved.
  13. *
  14. ***********************************************************************
  15. * Software that is described herein is for illustrative purposes only
  16. * which provides customers with programming information regarding the
  17. * products. This software is supplied "AS IS" without any warranties.
  18. * NXP Semiconductors assumes no responsibility or liability for the
  19. * use of the software, conveys no license or title under any patent,
  20. * copyright, or mask work right to the product. NXP Semiconductors
  21. * reserves the right to make changes in the software without
  22. * notification. NXP Semiconductors also make no representation or
  23. * warranty that such application will be suitable for the specified
  24. * use without further testing or modification.
  25. * Permission to use, copy, modify, and distribute this software and its
  26. * documentation is hereby granted, under NXP Semiconductors'
  27. * relevant copyright in the software, without fee, provided that it
  28. * is used in conjunction with NXP Semiconductors microcontrollers. This
  29. * copyright, permission, and disclaimer notice must appear in all copies of
  30. * this code.
  31. **********************************************************************/
  32. /* Peripheral group ----------------------------------------------------------- */
  33. /** @defgroup SSP SSP (Synchronous Serial Port)
  34. * @ingroup LPC_CMSIS_FwLib_Drivers
  35. * @{
  36. */
  37. #ifndef __LPC_SSP_H_
  38. #define __LPC_SSP_H_
  39. /* Includes ------------------------------------------------------------------- */
  40. #include "LPC407x_8x_177x_8x.h"
  41. #include "lpc_types.h"
  42. #ifdef __cplusplus
  43. extern "C"
  44. {
  45. #endif
  46. /* Public Macros -------------------------------------------------------------- */
  47. /** @defgroup SSP_Public_Macros SSP Public Macros
  48. * @{
  49. */
  50. /*********************************************************************//**
  51. * SSP configuration parameter defines
  52. **********************************************************************/
  53. /** Clock phase control bit */
  54. #define SSP_CPHA_FIRST ((uint32_t)(0))
  55. #define SSP_CPHA_SECOND SSP_CR0_CPHA_SECOND
  56. /** Clock polarity control bit */
  57. /* There's no bug here!!!
  58. * - If bit[6] in SSPnCR0 is 0: SSP controller maintains the bus clock low between frames.
  59. * That means the active clock is in HI state.
  60. * - If bit[6] in SSPnCR0 is 1 (SSP_CR0_CPOL_HI): SSP controller maintains the bus clock
  61. * high between frames. That means the active clock is in LO state.
  62. */
  63. #define SSP_CPOL_HI ((uint32_t)(0))
  64. #define SSP_CPOL_LO SSP_CR0_CPOL_HI
  65. /** SSP master mode enable */
  66. #define SSP_SLAVE_MODE SSP_CR1_SLAVE_EN
  67. #define SSP_MASTER_MODE ((uint32_t)(0))
  68. /** SSP data bit number defines */
  69. #define SSP_DATABIT_4 SSP_CR0_DSS(4) /*!< Databit number = 4 */
  70. #define SSP_DATABIT_5 SSP_CR0_DSS(5) /*!< Databit number = 5 */
  71. #define SSP_DATABIT_6 SSP_CR0_DSS(6) /*!< Databit number = 6 */
  72. #define SSP_DATABIT_7 SSP_CR0_DSS(7) /*!< Databit number = 7 */
  73. #define SSP_DATABIT_8 SSP_CR0_DSS(8) /*!< Databit number = 8 */
  74. #define SSP_DATABIT_9 SSP_CR0_DSS(9) /*!< Databit number = 9 */
  75. #define SSP_DATABIT_10 SSP_CR0_DSS(10) /*!< Databit number = 10 */
  76. #define SSP_DATABIT_11 SSP_CR0_DSS(11) /*!< Databit number = 11 */
  77. #define SSP_DATABIT_12 SSP_CR0_DSS(12) /*!< Databit number = 12 */
  78. #define SSP_DATABIT_13 SSP_CR0_DSS(13) /*!< Databit number = 13 */
  79. #define SSP_DATABIT_14 SSP_CR0_DSS(14) /*!< Databit number = 14 */
  80. #define SSP_DATABIT_15 SSP_CR0_DSS(15) /*!< Databit number = 15 */
  81. #define SSP_DATABIT_16 SSP_CR0_DSS(16) /*!< Databit number = 16 */
  82. /** SSP Frame Format definition */
  83. /** Motorola SPI mode */
  84. #define SSP_FRAME_SPI SSP_CR0_FRF_SPI
  85. /** TI synchronous serial mode */
  86. #define SSP_FRAME_TI SSP_CR0_FRF_TI
  87. /** National Micro-wire mode */
  88. #define SSP_FRAME_MICROWIRE SSP_CR0_FRF_MICROWIRE
  89. /*********************************************************************//**
  90. * SSP Status defines
  91. **********************************************************************/
  92. /** SSP status TX FIFO Empty bit */
  93. #define SSP_STAT_TXFIFO_EMPTY SSP_SR_TFE
  94. /** SSP status TX FIFO not full bit */
  95. #define SSP_STAT_TXFIFO_NOTFULL SSP_SR_TNF
  96. /** SSP status RX FIFO not empty bit */
  97. #define SSP_STAT_RXFIFO_NOTEMPTY SSP_SR_RNE
  98. /** SSP status RX FIFO full bit */
  99. #define SSP_STAT_RXFIFO_FULL SSP_SR_RFF
  100. /** SSP status SSP Busy bit */
  101. #define SSP_STAT_BUSY SSP_SR_BSY
  102. /*********************************************************************//**
  103. * SSP Interrupt Configuration defines
  104. **********************************************************************/
  105. /** Receive Overrun */
  106. #define SSP_INTCFG_ROR SSP_IMSC_ROR
  107. /** Receive TimeOut */
  108. #define SSP_INTCFG_RT SSP_IMSC_RT
  109. /** Rx FIFO is at least half full */
  110. #define SSP_INTCFG_RX SSP_IMSC_RX
  111. /** Tx FIFO is at least half empty */
  112. #define SSP_INTCFG_TX SSP_IMSC_TX
  113. /*********************************************************************//**
  114. * SSP Configured Interrupt Status defines
  115. **********************************************************************/
  116. /** Receive Overrun */
  117. #define SSP_INTSTAT_ROR SSP_MIS_ROR
  118. /** Receive TimeOut */
  119. #define SSP_INTSTAT_RT SSP_MIS_RT
  120. /** Rx FIFO is at least half full */
  121. #define SSP_INTSTAT_RX SSP_MIS_RX
  122. /** Tx FIFO is at least half empty */
  123. #define SSP_INTSTAT_TX SSP_MIS_TX
  124. /*********************************************************************//**
  125. * SSP Raw Interrupt Status defines
  126. **********************************************************************/
  127. /** Receive Overrun */
  128. #define SSP_INTSTAT_RAW_ROR SSP_RIS_ROR
  129. /** Receive TimeOut */
  130. #define SSP_INTSTAT_RAW_RT SSP_RIS_RT
  131. /** Rx FIFO is at least half full */
  132. #define SSP_INTSTAT_RAW_RX SSP_RIS_RX
  133. /** Tx FIFO is at least half empty */
  134. #define SSP_INTSTAT_RAW_TX SSP_RIS_TX
  135. /*********************************************************************//**
  136. * SSP Interrupt Clear defines
  137. **********************************************************************/
  138. /** Writing a 1 to this bit clears the "frame was received when
  139. * RxFIFO was full" interrupt */
  140. #define SSP_INTCLR_ROR SSP_ICR_ROR
  141. /** Writing a 1 to this bit clears the "Rx FIFO was not empty and
  142. * has not been read for a timeout period" interrupt */
  143. #define SSP_INTCLR_RT SSP_ICR_RT
  144. /*********************************************************************//**
  145. * SSP DMA defines
  146. **********************************************************************/
  147. /** SSP bit for enabling RX DMA */
  148. #define SSP_DMA_RX SSP_DMA_RXDMA_EN
  149. /** SSP bit for enabling TX DMA */
  150. #define SSP_DMA_TX SSP_DMA_TXDMA_EN
  151. /* SSP Status Implementation definitions */
  152. #define SSP_STAT_DONE (1UL<<8) /**< Done */
  153. #define SSP_STAT_ERROR (1UL<<9) /**< Error */
  154. /**
  155. * @}
  156. */
  157. /* Private Macros ------------------------------------------------------------- */
  158. /** @defgroup SSP_Private_Macros SSP Private Macros
  159. * @{
  160. */
  161. /* --------------------- BIT DEFINITIONS -------------------------------------- */
  162. /*********************************************************************//**
  163. * Macro defines for CR0 register
  164. **********************************************************************/
  165. /** SSP data size select, must be 4 bits to 16 bits */
  166. #define SSP_CR0_DSS(n) ((uint32_t)((n-1)&0xF))
  167. /** SSP control 0 Motorola SPI mode */
  168. #define SSP_CR0_FRF_SPI ((uint32_t)(0<<4))
  169. /** SSP control 0 TI synchronous serial mode */
  170. #define SSP_CR0_FRF_TI ((uint32_t)(1<<4))
  171. /** SSP control 0 National Micro-wire mode */
  172. #define SSP_CR0_FRF_MICROWIRE ((uint32_t)(2<<4))
  173. /** SPI clock polarity bit (used in SPI mode only), (1) = maintains the
  174. bus clock high between frames, (0) = low */
  175. #define SSP_CR0_CPOL_HI ((uint32_t)(1<<6))
  176. /** SPI clock out phase bit (used in SPI mode only), (1) = captures data
  177. on the second clock transition of the frame, (0) = first */
  178. #define SSP_CR0_CPHA_SECOND ((uint32_t)(1<<7))
  179. /** SSP serial clock rate value load macro, divider rate is
  180. PERIPH_CLK / (cpsr * (SCR + 1)) */
  181. #define SSP_CR0_SCR(n) ((uint32_t)((n&0xFF)<<8))
  182. /** SSP CR0 bit mask */
  183. #define SSP_CR0_BITMASK ((uint32_t)(0xFFFF))
  184. /*********************************************************************//**
  185. * Macro defines for CR1 register
  186. **********************************************************************/
  187. /** SSP control 1 loopback mode enable bit */
  188. #define SSP_CR1_LBM_EN ((uint32_t)(1<<0))
  189. /** SSP control 1 enable bit */
  190. #define SSP_CR1_SSP_EN ((uint32_t)(1<<1))
  191. /** SSP control 1 slave enable */
  192. #define SSP_CR1_SLAVE_EN ((uint32_t)(1<<2))
  193. /** SSP control 1 slave out disable bit, disables transmit line in slave
  194. mode */
  195. #define SSP_CR1_SO_DISABLE ((uint32_t)(1<<3))
  196. /** SSP CR1 bit mask */
  197. #define SSP_CR1_BITMASK ((uint32_t)(0x0F))
  198. /*********************************************************************//**
  199. * Macro defines for DR register
  200. **********************************************************************/
  201. /** SSP data bit mask */
  202. #define SSP_DR_BITMASK(n) ((n)&0xFFFF)
  203. /*********************************************************************//**
  204. * Macro defines for SR register
  205. **********************************************************************/
  206. /** SSP status TX FIFO Empty bit */
  207. #define SSP_SR_TFE ((uint32_t)(1<<0))
  208. /** SSP status TX FIFO not full bit */
  209. #define SSP_SR_TNF ((uint32_t)(1<<1))
  210. /** SSP status RX FIFO not empty bit */
  211. #define SSP_SR_RNE ((uint32_t)(1<<2))
  212. /** SSP status RX FIFO full bit */
  213. #define SSP_SR_RFF ((uint32_t)(1<<3))
  214. /** SSP status SSP Busy bit */
  215. #define SSP_SR_BSY ((uint32_t)(1<<4))
  216. /** SSP SR bit mask */
  217. #define SSP_SR_BITMASK ((uint32_t)(0x1F))
  218. /*********************************************************************//**
  219. * Macro defines for CPSR register
  220. **********************************************************************/
  221. /** SSP clock prescaler */
  222. #define SSP_CPSR_CPDVSR(n) ((uint32_t)(n&0xFF))
  223. /** SSP CPSR bit mask */
  224. #define SSP_CPSR_BITMASK ((uint32_t)(0xFF))
  225. /*********************************************************************//**
  226. * Macro define for (IMSC) Interrupt Mask Set/Clear registers
  227. **********************************************************************/
  228. /** Receive Overrun */
  229. #define SSP_IMSC_ROR ((uint32_t)(1<<0))
  230. /** Receive TimeOut */
  231. #define SSP_IMSC_RT ((uint32_t)(1<<1))
  232. /** Rx FIFO is at least half full */
  233. #define SSP_IMSC_RX ((uint32_t)(1<<2))
  234. /** Tx FIFO is at least half empty */
  235. #define SSP_IMSC_TX ((uint32_t)(1<<3))
  236. /** IMSC bit mask */
  237. #define SSP_IMSC_BITMASK ((uint32_t)(0x0F))
  238. /*********************************************************************//**
  239. * Macro define for (RIS) Raw Interrupt Status registers
  240. **********************************************************************/
  241. /** Receive Overrun */
  242. #define SSP_RIS_ROR ((uint32_t)(1<<0))
  243. /** Receive TimeOut */
  244. #define SSP_RIS_RT ((uint32_t)(1<<1))
  245. /** Rx FIFO is at least half full */
  246. #define SSP_RIS_RX ((uint32_t)(1<<2))
  247. /** Tx FIFO is at least half empty */
  248. #define SSP_RIS_TX ((uint32_t)(1<<3))
  249. /** RIS bit mask */
  250. #define SSP_RIS_BITMASK ((uint32_t)(0x0F))
  251. /*********************************************************************//**
  252. * Macro define for (MIS) Masked Interrupt Status registers
  253. **********************************************************************/
  254. /** Receive Overrun */
  255. #define SSP_MIS_ROR ((uint32_t)(1<<0))
  256. /** Receive TimeOut */
  257. #define SSP_MIS_RT ((uint32_t)(1<<1))
  258. /** Rx FIFO is at least half full */
  259. #define SSP_MIS_RX ((uint32_t)(1<<2))
  260. /** Tx FIFO is at least half empty */
  261. #define SSP_MIS_TX ((uint32_t)(1<<3))
  262. /** MIS bit mask */
  263. #define SSP_MIS_BITMASK ((uint32_t)(0x0F))
  264. /*********************************************************************//**
  265. * Macro define for (ICR) Interrupt Clear registers
  266. **********************************************************************/
  267. /** Writing a 1 to this bit clears the "frame was received when
  268. * RxFIFO was full" interrupt */
  269. #define SSP_ICR_ROR ((uint32_t)(1<<0))
  270. /** Writing a 1 to this bit clears the "Rx FIFO was not empty and
  271. * has not been read for a timeout period" interrupt */
  272. #define SSP_ICR_RT ((uint32_t)(1<<1))
  273. /** ICR bit mask */
  274. #define SSP_ICR_BITMASK ((uint32_t)(0x03))
  275. /*********************************************************************//**
  276. * Macro defines for DMACR register
  277. **********************************************************************/
  278. /** SSP bit for enabling RX DMA */
  279. #define SSP_DMA_RXDMA_EN ((uint32_t)(1<<0))
  280. /** SSP bit for enabling TX DMA */
  281. #define SSP_DMA_TXDMA_EN ((uint32_t)(1<<1))
  282. /** DMACR bit mask */
  283. #define SSP_DMA_BITMASK ((uint32_t)(0x03))
  284. /**
  285. * @}
  286. */
  287. /* Public Types --------------------------------------------------------------- */
  288. /** @defgroup SSP_Public_Types SSP Public Types
  289. * @{
  290. */
  291. /** @brief SSP configuration structure */
  292. typedef struct {
  293. uint32_t Databit; /** Databit number, should be SSP_DATABIT_x,
  294. where x is in range from 4 - 16 */
  295. uint32_t CPHA; /** Clock phase, should be:
  296. - SSP_CPHA_FIRST: first clock edge
  297. - SSP_CPHA_SECOND: second clock edge */
  298. uint32_t CPOL; /** Clock polarity, should be:
  299. - SSP_CPOL_HI: high level
  300. - SSP_CPOL_LO: low level */
  301. uint32_t Mode; /** SSP mode, should be:
  302. - SSP_MASTER_MODE: Master mode
  303. - SSP_SLAVE_MODE: Slave mode */
  304. uint32_t FrameFormat; /** Frame Format:
  305. - SSP_FRAME_SPI: Motorola SPI frame format
  306. - SSP_FRAME_TI: TI frame format
  307. - SSP_FRAME_MICROWIRE: National Microwire frame format */
  308. uint32_t ClockRate; /** Clock rate,in Hz */
  309. } SSP_CFG_Type;
  310. /**
  311. * @brief SSP Transfer Type definitions
  312. */
  313. typedef enum {
  314. SSP_TRANSFER_POLLING = 0, /**< Polling transfer */
  315. SSP_TRANSFER_INTERRUPT /**< Interrupt transfer */
  316. } SSP_TRANSFER_Type;
  317. /**
  318. * @brief SPI Data configuration structure definitions
  319. */
  320. typedef struct {
  321. void *tx_data; /**< Pointer to transmit data */
  322. uint32_t tx_cnt; /**< Transmit counter */
  323. void *rx_data; /**< Pointer to transmit data */
  324. uint32_t rx_cnt; /**< Receive counter */
  325. uint32_t length; /**< Length of transfer data */
  326. uint32_t status; /**< Current status of SSP activity */
  327. } SSP_DATA_SETUP_Type;
  328. /**
  329. * @}
  330. */
  331. /* Public Functions ----------------------------------------------------------- */
  332. /** @defgroup SSP_Public_Functions SSP Public Functions
  333. * @{
  334. */
  335. /* SSP Init/DeInit functions --------------------------------------------------*/
  336. void SSP_Init(LPC_SSP_TypeDef *SSPx, SSP_CFG_Type *SSP_ConfigStruct);
  337. void SSP_DeInit(LPC_SSP_TypeDef* SSPx);
  338. /* SSP configure functions ----------------------------------------------------*/
  339. void SSP_ConfigStructInit(SSP_CFG_Type *SSP_InitStruct);
  340. /* SSP enable/disable functions -----------------------------------------------*/
  341. void SSP_Cmd(LPC_SSP_TypeDef* SSPx, FunctionalState NewState);
  342. void SSP_LoopBackCmd(LPC_SSP_TypeDef* SSPx, FunctionalState NewState);
  343. void SSP_SlaveOutputCmd(LPC_SSP_TypeDef* SSPx, FunctionalState NewState);
  344. void SSP_DMACmd(LPC_SSP_TypeDef *SSPx, uint32_t DMAMode, FunctionalState NewState);
  345. /* SSP get information functions ----------------------------------------------*/
  346. FlagStatus SSP_GetStatus(LPC_SSP_TypeDef* SSPx, uint32_t FlagType);
  347. uint8_t SSP_GetDataSize(LPC_SSP_TypeDef* SSPx);
  348. IntStatus SSP_GetRawIntStatus(LPC_SSP_TypeDef *SSPx, uint32_t RawIntType);
  349. uint32_t SSP_GetRawIntStatusReg(LPC_SSP_TypeDef *SSPx);
  350. IntStatus SSP_GetIntStatus (LPC_SSP_TypeDef *SSPx, uint32_t IntType);
  351. /* SSP transfer data functions ------------------------------------------------*/
  352. void SSP_SendData(LPC_SSP_TypeDef* SSPx, uint16_t Data);
  353. uint16_t SSP_ReceiveData(LPC_SSP_TypeDef* SSPx);
  354. int32_t SSP_ReadWrite (LPC_SSP_TypeDef *SSPx, SSP_DATA_SETUP_Type *dataCfg, \
  355. SSP_TRANSFER_Type xfType);
  356. /* SSP IRQ function ------------------------------------------------------------*/
  357. void SSP_IntConfig(LPC_SSP_TypeDef *SSPx, uint32_t IntType, FunctionalState NewState);
  358. void SSP_ClearIntPending(LPC_SSP_TypeDef *SSPx, uint32_t IntType);
  359. /**
  360. * @}
  361. */
  362. #ifdef __cplusplus
  363. }
  364. #endif
  365. #endif /* __LPC_SSP_H_ */
  366. /**
  367. * @}
  368. */
  369. /* --------------------------------- End Of File ------------------------------ */