sct_8xx.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /*
  2. * @brief LPC8xx State Configurable Timer (SCT) Chip driver
  3. *
  4. * @note
  5. * Copyright(C) NXP Semiconductors, 2013
  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 licenser 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 __SCT_8XX_H_
  32. #define __SCT_8XX_H_
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif
  36. /** @defgroup SCT_8XX CHIP: LPC8xx State Configurable Timer driver
  37. * @ingroup CHIP_8XX_Drivers
  38. * @{
  39. */
  40. /*
  41. * @brief SCT Module configuration
  42. */
  43. #define CONFIG_SCT_nEV (8) /*!< Number of events */
  44. #define CONFIG_SCT_nRG (8) /*!< Number of match/compare registers */
  45. #define CONFIG_SCT_nOU (6) /*!< Number of outputs */
  46. /**
  47. * @brief State Configurable Timer register block structure
  48. */
  49. typedef struct {
  50. __IO uint32_t CONFIG; /*!< configuration Register (offset (0x000) */
  51. union {
  52. __IO uint32_t CTRL_U; /*!< control Register */
  53. struct {
  54. __IO uint16_t CTRL_L; /*!< low control register */
  55. __IO uint16_t CTRL_H; /*!< high control register */
  56. };
  57. };
  58. union {
  59. __IO uint32_t LIMIT_U; /*!< limit Register */
  60. struct {
  61. __IO uint16_t LIMIT_L; /*!< limit register for counter L */
  62. __IO uint16_t LIMIT_H; /*!< limit register for counter H */
  63. };
  64. };
  65. union {
  66. __IO uint32_t HALT_U; /*!< halt Register */
  67. struct {
  68. __IO uint16_t HALT_L; /*!< halt register for counter L */
  69. __IO uint16_t HALT_H; /*!< halt register for counter H */
  70. };
  71. };
  72. union {
  73. __IO uint32_t STOP_U; /*!< stop Register */
  74. struct {
  75. __IO uint16_t STOP_L; /*!< stop register for counter L */
  76. __IO uint16_t STOP_H; /*!< stop register for counter H */
  77. };
  78. };
  79. union {
  80. __IO uint32_t START_U; /*!< start Register */
  81. struct {
  82. __IO uint16_t START_L; /*!< start register for counter L */
  83. __IO uint16_t START_H; /*!< start register for counter H */
  84. };
  85. };
  86. uint32_t RESERVED1[10]; /*!< 0x018 - 0x03C reserved */
  87. union {
  88. __IO uint32_t COUNT_U; /*!< counter register (offset 0x040)*/
  89. struct {
  90. __IO uint16_t COUNT_L; /*!< counter register for counter L */
  91. __IO uint16_t COUNT_H; /*!< counter register for counter H */
  92. };
  93. };
  94. union {
  95. __IO uint32_t STATE_U; /*!< State register */
  96. struct {
  97. __IO uint16_t STATE_L; /*!< state register for counter L */
  98. __IO uint16_t STATE_H; /*!< state register for counter H */
  99. };
  100. };
  101. __I uint32_t INPUT; /*!< input register */
  102. union {
  103. __IO uint32_t REGMODE_U; /*!< RegMode register */
  104. struct {
  105. __IO uint16_t REGMODE_L; /*!< match - capture registers mode register L */
  106. __IO uint16_t REGMODE_H; /*!< match - capture registers mode register H */
  107. };
  108. };
  109. __IO uint32_t OUTPUT; /*!< output register */
  110. __IO uint32_t OUTPUTDIRCTRL; /*!< output counter direction Control Register */
  111. __IO uint32_t RES; /*!< conflict resolution register */
  112. __IO uint32_t DMAREQ0; /*!< DMA0 Request Register */
  113. __IO uint32_t DMAREQ1; /*!< DMA1 Request Register */
  114. uint32_t RESERVED2[35]; /*!< 0x064 - 0x0EC reserved */
  115. __IO uint32_t EVEN; /*!< event enable register (offset 0x0F0)*/
  116. __IO uint32_t EVFLAG; /*!< event flag register */
  117. __IO uint32_t CONEN; /*!< conflict enable register */
  118. __IO uint32_t CONFLAG; /*!< conflict flag register */
  119. union {
  120. __IO union { /*!< ... Match / Capture value */
  121. uint32_t U; /*!< MATCH[i].U Unified 32-bit register */
  122. struct {
  123. uint16_t L; /*!< MATCH[i].L Access to L value */
  124. uint16_t H; /*!< MATCH[i].H Access to H value */
  125. };
  126. } MATCH[CONFIG_SCT_nRG];
  127. __I union {
  128. uint32_t U; /*!< CAP[i].U Unified 32-bit register */
  129. struct {
  130. uint16_t L; /*!< CAP[i].L Access to L value */
  131. uint16_t H; /*!< CAP[i].H Access to H value */
  132. };
  133. } CAP[CONFIG_SCT_nRG];
  134. };
  135. uint32_t RESERVED3[56]; /*!< 0x120 - 0x1FC reserved */
  136. union {
  137. __IO union { /*!< ...Match Reload / Capture Control value (offset 0x200) */
  138. uint32_t U; /*!< MATCHREL[i].U Unified 32-bit register */
  139. struct {
  140. uint16_t L; /*!< MATCHREL[i].L Access to L value */
  141. uint16_t H; /*!< MATCHREL[i].H Access to H value */
  142. };
  143. } MATCHREL[CONFIG_SCT_nRG];
  144. __IO union {
  145. uint32_t U; /*!< CAPCTRL[i].U Unified 32-bit register */
  146. struct {
  147. uint16_t L; /*!< CAPCTRL[i].L Access to L value */
  148. uint16_t H; /*!< CAPCTRL[i].H Access to H value */
  149. };
  150. } CAPCTRL[CONFIG_SCT_nRG];
  151. };
  152. uint32_t RESERVED4[56]; /*!< 0x220 - 0x2FC reserved */
  153. __IO struct { /*!< EV[i].STATE / EV[i].CTRL (offset 0x300) */
  154. uint32_t STATE; /*!< Event State Register */
  155. uint32_t CTRL; /*!< Event Control Register */
  156. } EV[CONFIG_SCT_nEV];
  157. uint32_t RESERVED5[112]; /*!< 0x340 - 0x4FC reserved */
  158. __IO struct { /*!< OUT[i].SET / OUT[i].CLR (offset 0x500) */
  159. uint32_t SET; /*!< Output n Set Register */
  160. uint32_t CLR; /*!< Output n Clear Register */
  161. } OUT[CONFIG_SCT_nOU];
  162. } LPC_SCT_T;
  163. /* Reserved bits masks for registers */
  164. #define SCT_CONFIG_RESERVED 0xfff80000
  165. #define SCT_CTRL_RESERVED ((7<<13)|(7u<<29))
  166. #define SCT_LIMIT_RESERVED (~(0x3f|(0x3f<<16))
  167. #define SCT_HALT_RESERVED (~(0x3f|(0x3f<<16))
  168. #define SCT_STOP_RESERVED (~(0x3f|(0x3f<<16))
  169. #define SCT_START_RESERVED (~(0x3f|(0x3f<<16))
  170. #define SCT_STATE_RESERVED (~(0x1f|(0x1f<<16))
  171. #define SCT_INPUT_RESERVED (~(0xf|(0xf<<16))
  172. #define SCT_REGMODE_RESERVED (~(0x1f|(0x1f<<16))
  173. #define SCT_OUTPUT_RESERVED (~0xf)
  174. #define SCT_OUTPUTDIRCTRL_RESERVED (~0xff)
  175. #define SCT_RES_RESERVED (~0xff)
  176. #define SCT_EVEN_RESERVED (~0x3f)
  177. #define SCT_EVFLAG_RESERVED (~0x3f)
  178. #define SCT_CONEN_RESERVED (~0xf)
  179. #define SCT_CONFLAG_RESERVED (~(0xf|(3u<<30)))
  180. #define SCT_CAPCTRL_RESERVED (~(0x3f|(0x3f<<16)))
  181. #define SCT_EVn_STATE_RESERVED (~3)
  182. #define SCT_EVn_CTRL_RESERVED (0xff800000)
  183. #define SCT_OUTn_SET_RESERVED (~0x3f)
  184. #define SCT_OUTn_CLR_RESERVED (~0x3f)
  185. /*
  186. * @brief Macro defines for SCT configuration register
  187. */
  188. #define SCT_CONFIG_16BIT_COUNTER 0x00000000 /*!< Operate as 2 16-bit counters */
  189. #define SCT_CONFIG_32BIT_COUNTER 0x00000001 /*!< Operate as 1 32-bit counter */
  190. #define SCT_CONFIG_CLKMODE_BUSCLK (0x0 << 1) /*!< Bus clock */
  191. #define SCT_CONFIG_CLKMODE_SCTCLK (0x1 << 1) /*!< SCT clock */
  192. #define SCT_CONFIG_CLKMODE_INCLK (0x2 << 1) /*!< Input clock selected in CLKSEL field */
  193. #define SCT_CONFIG_CLKMODE_INEDGECLK (0x3 << 1) /*!< Input clock edge selected in CLKSEL field */
  194. #define SCT_CONFIG_NORELOAD_U (0x1 << 7) /*!< Prevent match register reload */
  195. #define SCT_CONFIG_NORELOAD_L (0x1 << 7) /*!< Prevent lower match register reload */
  196. #define SCT_CONFIG_NORELOAD_H (0x1 << 8) /*!< Prevent upper match register reload */
  197. #define SCT_CONFIG_AUTOLIMIT_U (0x1 << 17) /*!< Limits counter(unified) based on MATCH0 */
  198. #define SCT_CONFIG_AUTOLIMIT_L (0x1 << 17) /*!< Limits counter(L) based on MATCH0 */
  199. #define SCT_CONFIG_AUTOLIMIT_H (0x1 << 18) /*!< Limits counter(L) based on MATCH0 */
  200. /*
  201. * @brief Macro defines for SCT control register
  202. */
  203. #define COUNTUP_TO_LIMIT_THEN_CLEAR_TO_ZERO 0 /*!< Direction for low or unified counter */
  204. #define COUNTUP_TO LIMIT_THEN_COUNTDOWN_TO_ZERO 1
  205. #define SCT_CTRL_STOP_L (1 << 1) /*!< Stop low counter */
  206. #define SCT_CTRL_HALT_L (1 << 2) /*!< Halt low counter */
  207. #define SCT_CTRL_CLRCTR_L (1 << 3) /*!< Clear low or unified counter */
  208. #define SCT_CTRL_BIDIR_L(x) (((x) & 0x01) << 4) /*!< Bidirectional bit */
  209. #define SCT_CTRL_PRE_L(x) (((x) & 0xFF) << 5) /*!< Prescale clock for low or unified counter */
  210. #define COUNTUP_TO_LIMIT_THEN_CLEAR_TO_ZERO 0 /*!< Direction for high counter */
  211. #define COUNTUP_TO LIMIT_THEN_COUNTDOWN_TO_ZERO 1
  212. #define SCT_CTRL_STOP_H (1 << 17) /*!< Stop high counter */
  213. #define SCT_CTRL_HALT_H (1 << 18) /*!< Halt high counter */
  214. #define SCT_CTRL_CLRCTR_H (1 << 19) /*!< Clear high counter */
  215. #define SCT_CTRL_BIDIR_H(x) (((x) & 0x01) << 20)
  216. #define SCT_CTRL_PRE_H(x) (((x) & 0xFF) << 21) /*!< Prescale clock for high counter */
  217. /*
  218. * @brief Macro defines for SCT Conflict resolution register
  219. */
  220. #define SCT_RES_NOCHANGE (0)
  221. #define SCT_RES_SET_OUTPUT (1)
  222. #define SCT_RES_CLEAR_OUTPUT (2)
  223. #define SCT_RES_TOGGLE_OUTPUT (3)
  224. /**
  225. * SCT Match register values enum
  226. */
  227. typedef enum CHIP_SCT_MATCH_REG {
  228. SCT_MATCH_0 = 0, /*!< SCT Match register 0 */
  229. SCT_MATCH_1 = 1, /*!< SCT Match register 1 */
  230. SCT_MATCH_2 = 2, /*!< SCT Match register 2 */
  231. SCT_MATCH_3 = 3, /*!< SCT Match register 3 */
  232. SCT_MATCH_4 = 4 /*!< SCT Match register 4 */
  233. } CHIP_SCT_MATCH_REG_T;
  234. /**
  235. * SCT Event values enum
  236. */
  237. typedef enum CHIP_SCT_EVENT {
  238. SCT_EVT_0 = (1 << 0), /*!< Event 0 */
  239. SCT_EVT_1 = (1 << 1), /*!< Event 1 */
  240. SCT_EVT_2 = (1 << 2), /*!< Event 2 */
  241. SCT_EVT_3 = (1 << 3), /*!< Event 3 */
  242. SCT_EVT_4 = (1 << 4) /*!< Event 4 */
  243. } CHIP_SCT_EVENT_T;
  244. /**
  245. * @brief Configures the State Configurable Timer
  246. * @param pSCT : The base of SCT peripheral on the chip
  247. * @param value : The 32-bit CONFIG register value
  248. * @return Nothing
  249. */
  250. STATIC INLINE void Chip_SCT_Config(LPC_SCT_T *pSCT, uint32_t value)
  251. {
  252. pSCT->CONFIG = value;
  253. }
  254. /**
  255. * @brief Set or Clear the Control register
  256. * @param pSCT : Pointer to SCT register block
  257. * @param value : SCT Control register value
  258. * @param ena : ENABLE - To set the fields specified by value
  259. * : DISABLE - To clear the field specified by value
  260. * @return Nothing
  261. * Set or clear the control register bits as specified by the \a value
  262. * parameter. If \a ena is set to ENABLE, the mentioned register fields
  263. * will be set. If \a ena is set to DISABLE, the mentioned register
  264. * fields will be cleared
  265. */
  266. void Chip_SCT_SetClrControl(LPC_SCT_T *pSCT, uint32_t value, FunctionalState ena);
  267. /**
  268. * @brief Set the conflict resolution
  269. * @param pSCT : Pointer to SCT register block
  270. * @param outnum : Output number
  271. * @param value : Output value
  272. * - SCT_RES_NOCHANGE :No change
  273. * - SCT_RES_SET_OUTPUT :Set output
  274. * - SCT_RES_CLEAR_OUTPUT :Clear output
  275. * - SCT_RES_TOGGLE_OUTPUT :Toggle output
  276. * : SCT_RES_NOCHANGE
  277. * : DISABLE - To clear the field specified by value
  278. * @return Nothing
  279. * Set conflict resolution for the output \a outnum
  280. */
  281. void Chip_SCT_SetConflictResolution(LPC_SCT_T *pSCT, uint8_t outnum, uint8_t value);
  282. /**
  283. * @brief Set unified count value in State Configurable Timer
  284. * @param pSCT : The base of SCT peripheral on the chip
  285. * @param count : The 32-bit count value
  286. * @return Nothing
  287. */
  288. STATIC INLINE void Chip_SCT_SetCount(LPC_SCT_T *pSCT, uint32_t count)
  289. {
  290. pSCT->COUNT_U = count;
  291. }
  292. /**
  293. * @brief Set lower count value in State Configurable Timer
  294. * @param pSCT : The base of SCT peripheral on the chip
  295. * @param count : The 16-bit count value
  296. * @return Nothing
  297. */
  298. STATIC INLINE void Chip_SCT_SetCountL(LPC_SCT_T *pSCT, uint16_t count)
  299. {
  300. pSCT->COUNT_L = count;
  301. }
  302. /**
  303. * @brief Set higher count value in State Configurable Timer
  304. * @param pSCT : The base of SCT peripheral on the chip
  305. * @param count : The 16-bit count value
  306. * @return Nothing
  307. */
  308. STATIC INLINE void Chip_SCT_SetCountH(LPC_SCT_T *pSCT, uint16_t count)
  309. {
  310. pSCT->COUNT_H = count;
  311. }
  312. /**
  313. * @brief Set unified match count value in State Configurable Timer
  314. * @param pSCT : The base of SCT peripheral on the chip
  315. * @param n : Match register value
  316. * @param value : The 32-bit match count value
  317. * @return Nothing
  318. */
  319. STATIC INLINE void Chip_SCT_SetMatchCount(LPC_SCT_T *pSCT, CHIP_SCT_MATCH_REG_T n, uint32_t value)
  320. {
  321. pSCT->MATCH[n].U = value;
  322. }
  323. /**
  324. * @brief Set unified match reload count value in State Configurable Timer
  325. * @param pSCT : The base of SCT peripheral on the chip
  326. * @param n : Match register value
  327. * @param value : The 32-bit match count reload value
  328. * @return Nothing
  329. */
  330. STATIC INLINE void Chip_SCT_SetMatchReload(LPC_SCT_T *pSCT, CHIP_SCT_MATCH_REG_T n, uint32_t value)
  331. {
  332. pSCT->MATCHREL[n].U = value;
  333. }
  334. /**
  335. * @brief Enable the interrupt for the specified event in State Configurable Timer
  336. * @param pSCT : The base of SCT peripheral on the chip
  337. * @param evt : Event value
  338. * @return Nothing
  339. */
  340. STATIC INLINE void Chip_SCT_EnableEventInt(LPC_SCT_T *pSCT, CHIP_SCT_EVENT_T evt)
  341. {
  342. pSCT->EVEN = evt | (pSCT->EVEN & ~SCT_EVEN_RESERVED);
  343. }
  344. /**
  345. * @brief Disable the interrupt for the specified event in State Configurable Timer
  346. * @param pSCT : The base of SCT peripheral on the chip
  347. * @param evt : Event value
  348. * @return Nothing
  349. */
  350. STATIC INLINE void Chip_SCT_DisableEventInt(LPC_SCT_T *pSCT, CHIP_SCT_EVENT_T evt)
  351. {
  352. pSCT->EVEN &= ~(evt | SCT_EVEN_RESERVED);
  353. }
  354. /**
  355. * @brief Clear the specified event flag in State Configurable Timer
  356. * @param pSCT : The base of SCT peripheral on the chip
  357. * @param evt : Event value
  358. * @return Nothing
  359. */
  360. STATIC INLINE void Chip_SCT_ClearEventFlag(LPC_SCT_T *pSCT, CHIP_SCT_EVENT_T evt)
  361. {
  362. pSCT->EVFLAG = evt | (pSCT->EVFLAG & ~SCT_EVFLAG_RESERVED);
  363. }
  364. /**
  365. * @brief Set control register in State Configurable Timer
  366. * @param pSCT : The base of SCT peripheral on the chip
  367. * @param value : Value (ORed value of SCT_CTRL_* bits)
  368. * @return Nothing
  369. */
  370. STATIC INLINE void Chip_SCT_SetControl(LPC_SCT_T *pSCT, uint32_t value)
  371. {
  372. pSCT->CTRL_U = value | (pSCT->CTRL_U & ~SCT_CTRL_RESERVED);
  373. }
  374. /**
  375. * @brief Clear control register in State Configurable Timer
  376. * @param pSCT : The base of SCT peripheral on the chip
  377. * @param value : Value (ORed value of SCT_CTRL_* bits)
  378. * @return Nothing
  379. */
  380. STATIC INLINE void Chip_SCT_ClearControl(LPC_SCT_T *pSCT, uint32_t value)
  381. {
  382. pSCT->CTRL_U &= ~(value | SCT_CTRL_RESERVED);
  383. }
  384. /**
  385. * @brief Initializes the State Configurable Timer
  386. * @param pSCT : The base of SCT peripheral on the chip
  387. * @return Nothing
  388. */
  389. void Chip_SCT_Init(LPC_SCT_T *pSCT);
  390. /**
  391. * @brief Deinitializes the State Configurable Timer
  392. * @param pSCT : The base of SCT peripheral on the chip
  393. * @return Nothing
  394. */
  395. void Chip_SCT_DeInit(LPC_SCT_T *pSCT);
  396. /**
  397. * @}
  398. */
  399. #ifdef __cplusplus
  400. }
  401. #endif
  402. #endif /* __SCT_8XX_H_ */