adc_8xx.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. /*
  2. * @brief LPC82x ADC driver
  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 __ADC_8XX_H_
  32. #define __ADC_8XX_H_
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif
  36. /** @defgroup ADC_8XX CHIP: LPC8xx A/D conversion driver (Available on LPC82x Family)
  37. * @ingroup CHIP_8xx_Drivers
  38. * @{
  39. */
  40. /** Sequence index enumerations, used in various parts of the code for
  41. register indexing and sequencer selection */
  42. typedef enum {
  43. ADC_SEQA_IDX,
  44. ADC_SEQB_IDX
  45. } ADC_SEQ_IDX_T;
  46. /**
  47. * @brief ADC register block structure
  48. */
  49. typedef struct { /*!< ADCn Structure */
  50. __IO uint32_t CTRL; /*!< A/D Control Register. The AD0CR register must be written to select the operating mode before A/D conversion can occur. */
  51. __I uint32_t RESERVED0;
  52. __IO uint32_t SEQ_CTRL[ADC_SEQB_IDX + 1]; /*!< A/D Sequence A & B Control Register. Controls triggering and channel selection for sonversion sequence. */
  53. __IO uint32_t SEQ_GDAT[ADC_SEQB_IDX + 1]; /*!< A/D Sequence A & B Global Data Register. Contains the result of the most recent A/D conversion for sequence. */
  54. __I uint32_t RESERVED1[2];
  55. __I uint32_t DR[12]; /*!< A/D Channel Data Register. This register contains the result of the most recent conversion completed on channel n. */
  56. __IO uint32_t THR_LOW[2]; /*!< A/D Low Compare Threshold Register 0 & 1. Contains the lower threshold level for automatic threshold comparison. */
  57. __IO uint32_t THR_HIGH[2]; /*!< A/D High Compare Threshold Register 0 & 1. Contains the higher threshold level for automatic threshold comparison. */
  58. __IO uint32_t CHAN_THRSEL; /*!< A/D Channel Threshold Select Register. Specifies which set of threshold compare registers to use. */
  59. __IO uint32_t INTEN; /*!< A/D Interrupt Enable Register. This register contains enable bits that enable sequence-A, sequence-B, threshold compare and overrun interrupts. */
  60. __IO uint32_t FLAGS; /*!< A/D Flags Register. This register contains interrupt flags. - To be checked */
  61. __IO uint32_t TRM; /*!< A/D Trim Register. */
  62. } LPC_ADC_T;
  63. /** Maximum sample rate in Hz (12-bit conversions) */
  64. #define ADC_MAX_SAMPLE_RATE 30000000
  65. /**
  66. * @brief ADC register support bitfields and mask
  67. */
  68. /** ADC Control register bit fields */
  69. #define ADC_CR_CLKDIV_MASK (0xFF << 0) /*!< Mask for Clock divider value */
  70. #define ADC_CR_CLKDIV_BITPOS (0) /*!< Bit position for Clock divider value */
  71. #define ADC_CR_ASYNMODE (1 << 8) /*!< Asynchronous mode enable bit */
  72. #define ADC_CR_MODE10BIT (1 << 9) /*!< 10-bit mode enable bit */
  73. #define ADC_CR_LPWRMODEBIT (1 << 10) /*!< Low power mode enable bit */
  74. #define ADC_CR_CALMODEBIT (1 << 30) /*!< Self calibration cycle enable bit */
  75. #define ADC_CR_BITACC(n) ((((n) & 0x1) << 9)) /*!< 12-bit or 10-bit ADC accuracy */
  76. #define ADC_CR_CLKDIV(n) ((((n) & 0xFF) << 0)) /*!< The APB clock (PCLK) is divided by (this value plus one) to produce the clock for the A/D */
  77. #define ADC_SAMPLE_RATE_CONFIG_MASK (ADC_CR_CLKDIV(0xFF) | ADC_CR_BITACC(0x01))
  78. /** ADC Sequence Control register bit fields */
  79. #define ADC_SEQ_CTRL_CHANSEL(n) (1 << (n)) /*!< Channel select macro */
  80. #define ADC_SEQ_CTRL_CHANSEL_MASK (0xFFF) /*!< Channel select mask */
  81. /** ADC hardware trigger sources in SEQ_CTRL */
  82. #define ADC_SEQ_CTRL_HWTRIG_ARM_TXEV (0 << 12) /*!< HW trigger input - ARM TXEV */
  83. #define ADC_SEQ_CTRL_HWTRIG_CT32B0_MAT0 (1 << 12) /*!< HW trigger input - Match output 0 of CT32B0 */
  84. #define ADC_SEQ_CTRL_HWTRIG_CT32B0_MAT1 (2 << 12) /*!< HW trigger input - Match output 1 of CT32B1 or SCT_OUT0 */
  85. #define ADC_SEQ_CTRL_HWTRIG_SCT_OUT0 (2 << 12) /*!< HW trigger input - Match output 1 of CT32B1 or SCT_OUT0 */
  86. #define ADC_SEQ_CTRL_HWTRIG_CT16B0_MAT0 (3 << 12) /*!< HW trigger input - Match output 0 of CT16B0 */
  87. #define ADC_SEQ_CTRL_HWTRIG_CT16B0_MAT1 (4 << 12) /*!< HW trigger input - Match output 1 of CT16B1 or SCT_OUT1 */
  88. #define ADC_SEQ_CTRL_HWTRIG_SCT_OUT1 (4 << 12) /*!< HW trigger input - Match output 1 of CT16B1 or SCT_OUT1 */
  89. #define ADC_SEQ_CTRL_HWTRIG_CT16B0_CAP0 (5 << 12) /*!< HW trigger input - Capture input 0 of CT16B0 */
  90. #define ADC_SEQ_CTRL_HWTRIG_CT16B1_CAP0 (6 << 12) /*!< HW trigger input - Capture input 0 of CT16B1 */
  91. #define ADC_SEQ_CTRL_HWTRIG_CT32B0_CAP0 (7 << 12) /*!< HW trigger input - Capture input 0 of CT32B1 */
  92. #define ADC_SEQ_CTRL_HWTRIG_MASK (0x3F << 12) /*!< HW trigger input bitfield mask */
  93. /** SEQ_CTRL register bit fields */
  94. #define ADC_SEQ_CTRL_HWTRIG_POLPOS (1 << 18) /*!< HW trigger polarity - positive edge */
  95. #define ADC_SEQ_CTRL_HWTRIG_SYNCBYPASS (1 << 19) /*!< HW trigger bypass synchronisation */
  96. #define ADC_SEQ_CTRL_START (1 << 26) /*!< Start conversion enable bit */
  97. #define ADC_SEQ_CTRL_BURST (1 << 27) /*!< Repeated conversion enable bit */
  98. #define ADC_SEQ_CTRL_SINGLESTEP (1 << 28) /*!< Single step enable bit */
  99. #define ADC_SEQ_CTRL_LOWPRIO (1 << 29) /*!< High priority enable bit (regardless of name) */
  100. #define ADC_SEQ_CTRL_MODE_EOS (1 << 30) /*!< Mode End of sequence enable bit */
  101. #define ADC_SEQ_CTRL_SEQ_ENA (1UL << 31) /*!< Sequence enable bit */
  102. /** ADC global data register bit fields */
  103. #define ADC_SEQ_GDAT_RESULT_MASK (0xFFF << 4) /*!< Result value mask */
  104. #define ADC_SEQ_GDAT_RESULT_BITPOS (4) /*!< Result start bit position */
  105. #define ADC_SEQ_GDAT_THCMPRANGE_MASK (0x3 << 16) /*!< Comparion range mask */
  106. #define ADC_SEQ_GDAT_THCMPRANGE_BITPOS (16) /*!< Comparison range bit position */
  107. #define ADC_SEQ_GDAT_THCMPCROSS_MASK (0x3 << 18) /*!< Comparion cross mask */
  108. #define ADC_SEQ_GDAT_THCMPCROSS_BITPOS (18) /*!< Comparison cross bit position */
  109. #define ADC_SEQ_GDAT_CHAN_MASK (0xF << 26) /*!< Channel number mask */
  110. #define ADC_SEQ_GDAT_CHAN_BITPOS (26) /*!< Channel number bit position */
  111. #define ADC_SEQ_GDAT_OVERRUN (1 << 30) /*!< Overrun bit */
  112. #define ADC_SEQ_GDAT_DATAVALID (1UL << 31) /*!< Data valid bit */
  113. /** ADC Data register bit fields */
  114. #define ADC_DR_RESULT(n) ((((n) >> 4) & 0xFFF)) /*!< Macro for getting the ADC data value */
  115. #define ADC_DR_THCMPRANGE_MASK (0x3 << 16) /*!< Comparion range mask */
  116. #define ADC_DR_THCMPRANGE_BITPOS (16) /*!< Comparison range bit position */
  117. #define ADC_DR_THCMPRANGE(n) (((n) >> ADC_DR_THCMPRANGE_BITPOS) & 0x3)
  118. #define ADC_DR_THCMPCROSS_MASK (0x3 << 18) /*!< Comparion cross mask */
  119. #define ADC_DR_THCMPCROSS_BITPOS (18) /*!< Comparison cross bit position */
  120. #define ADC_DR_THCMPCROSS(n) (((n) >> ADC_DR_THCMPCROSS_BITPOS) & 0x3)
  121. #define ADC_DR_CHAN_MASK (0xF << 26) /*!< Channel number mask */
  122. #define ADC_DR_CHAN_BITPOS (26) /*!< Channel number bit position */
  123. #define ADC_DR_CHANNEL(n) (((n) >> ADC_DR_CHAN_BITPOS) & 0xF) /*!< Channel number bit position */
  124. #define ADC_DR_OVERRUN (1 << 30) /*!< Overrun bit */
  125. #define ADC_DR_DATAVALID (1UL << 31) /*!< Data valid bit */
  126. #define ADC_DR_DONE(n) (((n) >> 31))
  127. /** ADC low/high Threshold register bit fields */
  128. #define ADC_THR_VAL_MASK (0xFFF << 4) /*!< Threshold value bit mask */
  129. #define ADC_THR_VAL_POS (4) /*!< Threshold value bit position */
  130. /** ADC Threshold select register bit fields */
  131. #define ADC_THRSEL_CHAN_SEL_THR1(n) (1 << (n)) /*!< Select THR1 register for channel n */
  132. /** ADC Interrupt Enable register bit fields */
  133. #define ADC_INTEN_SEQA_ENABLE (1 << 0) /*!< Sequence A Interrupt enable bit */
  134. #define ADC_INTEN_SEQB_ENABLE (1 << 1) /*!< Sequence B Interrupt enable bit */
  135. #define ADC_INTEN_SEQN_ENABLE(seq) (1 << (seq)) /*!< Sequence A/B Interrupt enable bit */
  136. #define ADC_INTEN_OVRRUN_ENABLE (1 << 2) /*!< Overrun Interrupt enable bit */
  137. #define ADC_INTEN_CMP_DISBALE (0) /*!< Disable comparison interrupt value */
  138. #define ADC_INTEN_CMP_OUTSIDETH (1) /*!< Outside threshold interrupt value */
  139. #define ADC_INTEN_CMP_CROSSTH (2) /*!< Crossing threshold interrupt value */
  140. #define ADC_INTEN_CMP_MASK (3) /*!< Comparison interrupt value mask */
  141. #define ADC_INTEN_CMP_ENABLE(isel, ch) (((isel) & ADC_INTEN_CMP_MASK) << ((2 * (ch)) + 3)) /*!< Interrupt selection for channel */
  142. /** ADC Flags register bit fields */
  143. #define ADC_FLAGS_THCMP_MASK(ch) (1 << (ch)) /*!< Threshold comparison status for channel */
  144. #define ADC_FLAGS_OVRRUN_MASK(ch) (1 << (12 + (ch))) /*!< Overrun status for channel */
  145. #define ADC_FLAGS_SEQA_OVRRUN_MASK (1 << 24) /*!< Seq A Overrun status */
  146. #define ADC_FLAGS_SEQB_OVRRUN_MASK (1 << 25) /*!< Seq B Overrun status */
  147. #define ADC_FLAGS_SEQN_OVRRUN_MASK(seq) (1 << (24 + (seq))) /*!< Seq A/B Overrun status */
  148. #define ADC_FLAGS_SEQA_INT_MASK (1 << 28) /*!< Seq A Interrupt status */
  149. #define ADC_FLAGS_SEQB_INT_MASK (1 << 29) /*!< Seq B Interrupt status */
  150. #define ADC_FLAGS_SEQN_INT_MASK(seq) (1 << (28 + (seq)))/*!< Seq A/B Interrupt status */
  151. #define ADC_FLAGS_THCMP_INT_MASK (1 << 30) /*!< Threshold comparison Interrupt status */
  152. #define ADC_FLAGS_OVRRUN_INT_MASK (1UL << 31) /*!< Overrun Interrupt status */
  153. /** ADC Trim register bit fields */
  154. #define ADC_TRIM_VRANGE_HIGHV (0 << 5) /*!< Voltage range bit - High volatge (2.7V to 3.6V) */
  155. #define ADC_TRIM_VRANGE_LOWV (1 << 5) /*!< Voltage range bit - Low volatge (1.8V to 2.7V) */
  156. /** ADC Register reserved bit masks */
  157. #define ADC_CHAN_THRSEL_RES 0xFFFFF000
  158. #define ADC_INTEN_RES 0xF8000000
  159. #define ADC_SEQ_CTRL_RES ((7 << 15) | (0x3F << 20))
  160. /**
  161. * @brief Initialize the ADC peripheral
  162. * @param pADC : The base of ADC peripheral on the chip
  163. * @param flags : ADC flags for init (ADC_CR_MODE10BIT and/or ADC_CR_LPWRMODEBIT)
  164. * @return Nothing
  165. * @note To select low-power ADC mode, enable the ADC_CR_LPWRMODEBIT flag.
  166. * To select 10-bit conversion mode, enable the ADC_CR_MODE10BIT flag.<br>
  167. * Example: Chip_ADC_Init(LPC_ADC, (ADC_CR_MODE10BIT | ADC_CR_LPWRMODEBIT));
  168. */
  169. void Chip_ADC_Init(LPC_ADC_T *pADC, uint32_t flags);
  170. /**
  171. * @brief Shutdown ADC
  172. * @param pADC : The base of ADC peripheral on the chip
  173. * @return Nothing
  174. * @note Disables the ADC clocks and ADC power
  175. */
  176. void Chip_ADC_DeInit(LPC_ADC_T *pADC);
  177. /**
  178. * @brief Set ADC divider
  179. * @param pADC : The base of ADC peripheral on the chip
  180. * @param div : ADC divider value to set minus 1
  181. * @return Nothing
  182. * @note The value is used as a divider to generate the ADC
  183. * clock rate from the ADC input clock. The ADC input clock is based
  184. * on the system clock. Valid values for this function are from 0 to 255
  185. * with 0=divide by 1, 1=divide by 2, 2=divide by 3, etc.<br>
  186. * Do not decrement this value by 1.<br>
  187. * To set the ADC clock rate to 1MHz, use the following function:<br>
  188. * Chip_ADC_SetDivider(LPC_ADC, (Chip_Clock_GetSystemClockRate() / 1000000) - 1);
  189. */
  190. STATIC INLINE void Chip_ADC_SetDivider(LPC_ADC_T *pADC, uint8_t div)
  191. {
  192. uint32_t temp;
  193. temp = pADC->CTRL & ~(ADC_CR_CLKDIV_MASK);
  194. pADC->CTRL = temp | (uint32_t) div;
  195. }
  196. /**
  197. * @brief Set ADC clock rate
  198. * @param pADC : The base of ADC peripheral on the chip
  199. * @param rate : rate in Hz to set ADC clock to (maximum ADC_MAX_SAMPLE_RATE)
  200. * @return Nothing
  201. * @note When ADC is set to ADC_CR_ASYNMODE this function has no effect. The
  202. * rate mentioned in @a rate is the sampling clock rate and not the frequency at
  203. * at which the conversion will be done. Example setting @a rate to 30 MHz will
  204. * get a sampling rate of 1.2M samples per second.
  205. */
  206. STATIC INLINE void Chip_ADC_SetClockRate(LPC_ADC_T *pADC, uint32_t rate)
  207. {
  208. Chip_ADC_SetDivider(pADC, (uint8_t) (Chip_Clock_GetSystemClockRate() / rate) - 1);
  209. }
  210. /**
  211. * @brief Get ADC divider
  212. * @param pADC : The base of ADC peripheral on the chip
  213. * @return the current ADC divider
  214. * @note This function returns the divider that is used to generate the
  215. * ADC frequency. The returned value must be incremented by 1. The
  216. * frequency can be determined with the following function:<br>
  217. * adc_freq = Chip_Clock_GetSystemClockRate() / (Chip_ADC_GetDivider(LPC_ADC) + 1);
  218. */
  219. STATIC INLINE uint8_t Chip_ADC_GetDivider(LPC_ADC_T *pADC)
  220. {
  221. return pADC->CTRL & ADC_CR_CLKDIV_MASK;
  222. }
  223. /**
  224. * @brief Start ADC calibration
  225. * @param pADC : The base of ADC peripheral on the chip
  226. * @return Nothing
  227. * @note Calibration is not done as part of Chip_ADC_Init(), but
  228. * is required after the call to Chip_ADC_Init() or after returning
  229. * from a power-down state. Calibration may alter the ADC_CR_ASYNMODE
  230. * and ADC_CR_LPWRMODEBIT flags ni the CTRL register.
  231. */
  232. void Chip_ADC_StartCalibration(LPC_ADC_T *pADC);
  233. /**
  234. * @brief Start ADC calibration
  235. * @param pADC : The base of ADC peripheral on the chip
  236. * @return TRUE if calibration is complete, otherwise FALSE.
  237. */
  238. STATIC INLINE bool Chip_ADC_IsCalibrationDone(LPC_ADC_T *pADC)
  239. {
  240. return (bool) ((pADC->CTRL & ADC_CR_CALMODEBIT) == 0);
  241. }
  242. /**
  243. * @brief Helper function for safely setting ADC sequencer register bits
  244. * @param pADC : The base of ADC peripheral on the chip
  245. * @param seqIndex : Sequencer to set bits for
  246. * @param bits : Or'ed bits of a sequencer register to set
  247. * @return Nothing
  248. * @note This function will safely set the ADC sequencer register bits
  249. * while maintaining bits 20..25 as 0, regardless of the read state of those bits.
  250. */
  251. STATIC INLINE void Chip_ADC_SetSequencerBits(LPC_ADC_T *pADC, ADC_SEQ_IDX_T seqIndex, uint32_t bits)
  252. {
  253. pADC->SEQ_CTRL[seqIndex] = (pADC->SEQ_CTRL[seqIndex] & ~ADC_SEQ_CTRL_RES) | bits;
  254. }
  255. /**
  256. * @brief Helper function for safely clearing ADC sequencer register bits
  257. * @param pADC : The base of ADC peripheral on the chip
  258. * @param seqIndex : Sequencer to clear bits for
  259. * @param bits : Or'ed bits of a sequencer register to clear
  260. * @return Nothing
  261. * @note This function will safely clear the ADC sequencer register bits
  262. * while maintaining bits 20..25 as 0, regardless of the read state of those bits.
  263. */
  264. STATIC INLINE void Chip_ADC_ClearSequencerBits(LPC_ADC_T *pADC, ADC_SEQ_IDX_T seqIndex, uint32_t bits)
  265. {
  266. pADC->SEQ_CTRL[seqIndex] = pADC->SEQ_CTRL[seqIndex] & ~(ADC_SEQ_CTRL_RES | bits);
  267. }
  268. /**
  269. * @brief Sets up ADC conversion sequencer A or B
  270. * @param pADC : The base of ADC peripheral on the chip
  271. * @param seqIndex : Sequencer to setup
  272. * @param options : OR'ed Sequencer options to setup (see notes)
  273. * @return Nothing
  274. * @note Sets up sequencer options for a conversion sequence. This function
  275. * should be used to setup the selected channels for the sequence, the sequencer
  276. * trigger, the trigger polarity, synchronization bypass, priority, and mode. All
  277. * options are passed to the functions as a OR'ed list of values. This function will
  278. * disable/clear the sequencer start/burst/single step/enable if they are enabled.<br>
  279. * Select the channels by OR'ing in one or more ADC_SEQ_CTRL_CHANSEL(ch) values.<br>
  280. * Select the hardware trigger by OR'ing in one ADC_SEQ_CTRL_HWTRIG_* value.<br>
  281. * Select a positive edge hardware trigger by OR'ing in ADC_SEQ_CTRL_HWTRIG_POLPOS.<br>
  282. * Select trigger bypass synchronisation by OR'ing in ADC_SEQ_CTRL_HWTRIG_SYNCBYPASS.<br>
  283. * Select ADC single step on trigger/start by OR'ing in ADC_SEQ_CTRL_SINGLESTEP.<br>
  284. * Select higher priority conversion on the other sequencer by OR'ing in ADC_SEQ_CTRL_LOWPRIO.<br>
  285. * Select end of seqeuence instead of end of conversion interrupt by OR'ing in ADC_SEQ_CTRL_MODE_EOS.<br>
  286. * Example for setting up sequencer A (channels 0-2, trigger on high edge of PIO0_2, interrupt on end of sequence):<br>
  287. * Chip_ADC_SetupSequencer(LPC_ADC, ADC_SEQA_IDX, (
  288. * ADC_SEQ_CTRL_CHANSEL(0) | ADC_SEQ_CTRL_CHANSEL(1) | ADC_SEQ_CTRL_CHANSEL(2) |
  289. * ADC_SEQ_CTRL_HWTRIG_PIO0_2 | ADC_SEQ_CTRL_HWTRIG_POLPOS | ADC_SEQ_CTRL_MODE_EOS));
  290. */
  291. STATIC INLINE void Chip_ADC_SetupSequencer(LPC_ADC_T *pADC, ADC_SEQ_IDX_T seqIndex, uint32_t options)
  292. {
  293. pADC->SEQ_CTRL[seqIndex] = options;
  294. }
  295. /**
  296. * @brief Enables a sequencer
  297. * @param pADC : The base of ADC peripheral on the chip
  298. * @param seqIndex : Sequencer to enable
  299. * @return Nothing
  300. */
  301. STATIC INLINE void Chip_ADC_EnableSequencer(LPC_ADC_T *pADC, ADC_SEQ_IDX_T seqIndex)
  302. {
  303. Chip_ADC_SetSequencerBits(pADC, seqIndex, ADC_SEQ_CTRL_SEQ_ENA);
  304. }
  305. /**
  306. * @brief Disables a sequencer
  307. * @param pADC : The base of ADC peripheral on the chip
  308. * @param seqIndex : Sequencer to disable
  309. * @return Nothing
  310. */
  311. STATIC INLINE void Chip_ADC_DisableSequencer(LPC_ADC_T *pADC, ADC_SEQ_IDX_T seqIndex)
  312. {
  313. Chip_ADC_ClearSequencerBits(pADC, seqIndex, ADC_SEQ_CTRL_SEQ_ENA);
  314. }
  315. /**
  316. * @brief Forces a sequencer trigger event (software trigger of ADC)
  317. * @param pADC : The base of ADC peripheral on the chip
  318. * @param seqIndex : Sequencer to start
  319. * @return Nothing
  320. * @note This function sets the START bit for the sequencer to force a
  321. * single conversion sequence or a single step conversion.
  322. */
  323. STATIC INLINE void Chip_ADC_StartSequencer(LPC_ADC_T *pADC, ADC_SEQ_IDX_T seqIndex)
  324. {
  325. Chip_ADC_SetSequencerBits(pADC, seqIndex, ADC_SEQ_CTRL_START);
  326. }
  327. /**
  328. * @brief Starts sequencer burst mode
  329. * @param pADC : The base of ADC peripheral on the chip
  330. * @param seqIndex : Sequencer to start burst on
  331. * @return Nothing
  332. * @note This function sets the BURST bit for the sequencer to force
  333. * continuous conversion. Use Chip_ADC_StopBurstSequencer() to stop the
  334. * ADC burst sequence.
  335. */
  336. STATIC INLINE void Chip_ADC_StartBurstSequencer(LPC_ADC_T *pADC, ADC_SEQ_IDX_T seqIndex)
  337. {
  338. Chip_ADC_SetSequencerBits(pADC, seqIndex, ADC_SEQ_CTRL_BURST);
  339. }
  340. /**
  341. * @brief Stops sequencer burst mode
  342. * @param pADC : The base of ADC peripheral on the chip
  343. * @param seqIndex : Sequencer to stop burst on
  344. * @return Nothing
  345. */
  346. STATIC INLINE void Chip_ADC_StopBurstSequencer(LPC_ADC_T *pADC, ADC_SEQ_IDX_T seqIndex)
  347. {
  348. Chip_ADC_ClearSequencerBits(pADC, seqIndex, ADC_SEQ_CTRL_BURST);
  349. }
  350. /** ADC sequence global data register threshold comparison range enumerations */
  351. typedef enum {
  352. ADC_DR_THCMPRANGE_INRANGE,
  353. ADC_DR_THCMPRANGE_RESERVED,
  354. ADC_DR_THCMPRANGE_BELOW,
  355. ADC_DR_THCMPRANGE_ABOVE
  356. } ADC_DR_THCMPRANGE_T;
  357. /** ADC sequence global data register threshold comparison cross enumerations */
  358. typedef enum {
  359. ADC_DR_THCMPCROSS_NOCROSS,
  360. ADC_DR_THCMPCROSS_RESERVED,
  361. ADC_DR_THCMPCROSS_DOWNWARD,
  362. ADC_DR_THCMPCROSS_UPWARD
  363. } ADC_DR_THCMPCROSS_T;
  364. /**
  365. * @brief Read a ADC sequence global data register
  366. * @param pADC : The base of ADC peripheral on the chip
  367. * @param seqIndex : Sequencer to read
  368. * @return Current raw value of the ADC sequence A or B global data register
  369. * @note This function returns the raw value of the data register and clears
  370. * the overrun and datavalid status for the register. Once this register is read,
  371. * the following functions can be used to parse the raw value:<br>
  372. * uint32_t adcDataRawValue = Chip_ADC_ReadSequencerDataReg(LPC_ADC, ADC_SEQA_IDX); // Get raw value
  373. * uint32_t adcDataValue = ADC_DR_RESULT(adcDataRawValue); // Aligned and masked ADC data value
  374. * ADC_DR_THCMPRANGE_T adcRange = (ADC_DR_THCMPRANGE_T) ADC_DR_THCMPRANGE(adcDataRawValue); // Sample range compared to threshold low/high
  375. * ADC_DR_THCMPCROSS_T adcRange = (ADC_DR_THCMPCROSS_T) ADC_DR_THCMPCROSS(adcDataRawValue); // Sample cross compared to threshold low
  376. * uint32_t channel = ADC_DR_CHANNEL(adcDataRawValue); // ADC channel for this sample/data
  377. * bool adcDataOverrun = (bool) ((adcDataRawValue & ADC_DR_OVERRUN) != 0); // Data overrun flag
  378. * bool adcDataValid = (bool) ((adcDataRawValue & ADC_SEQ_GDAT_DATAVALID) != 0); // Data valid flag
  379. */
  380. STATIC INLINE uint32_t Chip_ADC_GetSequencerDataReg(LPC_ADC_T *pADC, ADC_SEQ_IDX_T seqIndex)
  381. {
  382. return pADC->SEQ_GDAT[seqIndex];
  383. }
  384. /**
  385. * @brief Read a ADC data register
  386. * @param pADC : The base of ADC peripheral on the chip
  387. * @param index : Data register to read, 1-8
  388. * @return Current raw value of the ADC data register
  389. * @note This function returns the raw value of the data register and clears
  390. * the overrun and datavalid status for the register. Once this register is read,
  391. * the following functions can be used to parse the raw value:<br>
  392. * uint32_t adcDataRawValue = Chip_ADC_ReadSequencerDataReg(LPC_ADC, ADC_SEQA_IDX); // Get raw value
  393. * uint32_t adcDataValue = ADC_DR_RESULT(adcDataRawValue); // Aligned and masked ADC data value
  394. * ADC_DR_THCMPRANGE_T adcRange = (ADC_DR_THCMPRANGE_T) ADC_DR_THCMPRANGE(adcDataRawValue); // Sample range compared to threshold low/high
  395. * ADC_DR_THCMPCROSS_T adcRange = (ADC_DR_THCMPCROSS_T) ADC_DR_THCMPCROSS(adcDataRawValue); // Sample cross compared to threshold low
  396. * uint32_t channel = ADC_DR_CHANNEL(adcDataRawValue); // ADC channel for this sample/data
  397. * bool adcDataOverrun = (bool) ((adcDataRawValue & ADC_DR_OVERRUN) != 0); // Data overrun flag
  398. * bool adcDataValid = (bool) ((adcDataRawValue & ADC_SEQ_GDAT_DATAVALID) != 0); // Data valid flag
  399. */
  400. STATIC INLINE uint32_t Chip_ADC_GetDataReg(LPC_ADC_T *pADC, uint8_t index)
  401. {
  402. return pADC->DR[index];
  403. }
  404. /**
  405. * @brief Set Threshold low value in ADC
  406. * @param pADC : The base of ADC peripheral on the chip
  407. * @param thrnum : Threshold register value (1 for threshold register 1, 0 for threshold register 0)
  408. * @param value : Threshold low data value (should be 12-bit value)
  409. * @return None
  410. */
  411. STATIC INLINE void Chip_ADC_SetThrLowValue(LPC_ADC_T *pADC, uint8_t thrnum, uint16_t value)
  412. {
  413. pADC->THR_LOW[thrnum] = (((uint32_t) value) << ADC_THR_VAL_POS);
  414. }
  415. /**
  416. * @brief Set Threshold high value in ADC
  417. * @param pADC : The base of ADC peripheral on the chip
  418. * @param thrnum : Threshold register value (1 for threshold register 1, 0 for threshold register 0)
  419. * @param value : Threshold high data value (should be 12-bit value)
  420. * @return None
  421. */
  422. STATIC INLINE void Chip_ADC_SetThrHighValue(LPC_ADC_T *pADC, uint8_t thrnum, uint16_t value)
  423. {
  424. pADC->THR_HIGH[thrnum] = (((uint32_t) value) << ADC_THR_VAL_POS);
  425. }
  426. /**
  427. * @brief Select threshold 0 values for comparison for selected channels
  428. * @param pADC : The base of ADC peripheral on the chip
  429. * @param channels : An OR'ed value of one or more ADC_THRSEL_CHAN_SEL_THR1(ch) values
  430. * @return None
  431. * @note Select multiple channels to use the threshold 0 comparison.<br>
  432. * Example:<br>
  433. * Chip_ADC_SelectTH0Channels(LPC_ADC, (ADC_THRSEL_CHAN_SEL_THR1(1) | ADC_THRSEL_CHAN_SEL_THR1(2))); // Selects channels 1 and 2 for threshold 0
  434. */
  435. STATIC INLINE void Chip_ADC_SelectTH0Channels(LPC_ADC_T *pADC, uint32_t channels)
  436. {
  437. pADC->CHAN_THRSEL = pADC->CHAN_THRSEL & ~(ADC_CHAN_THRSEL_RES | channels);
  438. }
  439. /**
  440. * @brief Select threshold 1 value for comparison for selected channels
  441. * @param pADC : The base of ADC peripheral on the chip
  442. * @param channels : An OR'ed value of one or more ADC_THRSEL_CHAN_SEL_THR1(ch) values
  443. * @return None
  444. * @note Select multiple channels to use the 1 threshold comparison.<br>
  445. * Example:<br>
  446. * Chip_ADC_SelectTH1Channels(LPC_ADC, (ADC_THRSEL_CHAN_SEL_THR1(4) | ADC_THRSEL_CHAN_SEL_THR1(5))); // Selects channels 4 and 5 for 1 threshold
  447. */
  448. STATIC INLINE void Chip_ADC_SelectTH1Channels(LPC_ADC_T *pADC, uint32_t channels)
  449. {
  450. pADC->CHAN_THRSEL = (pADC->CHAN_THRSEL & ~ADC_CHAN_THRSEL_RES) | channels;
  451. }
  452. /**
  453. * @brief Enable interrupts in ADC (sequencers A/B and overrun)
  454. * @param pADC : The base of ADC peripheral on the chip
  455. * @param intMask : Interrupt values to be enabled (see notes)
  456. * @return None
  457. * @note Select one or more OR'ed values of ADC_INTEN_SEQA_ENABLE,
  458. * ADC_INTEN_SEQB_ENABLE, and ADC_INTEN_OVRRUN_ENABLE to enable the
  459. * specific ADC interrupts.
  460. */
  461. STATIC INLINE void Chip_ADC_EnableInt(LPC_ADC_T *pADC, uint32_t intMask)
  462. {
  463. pADC->INTEN = (pADC->INTEN & ~ADC_INTEN_RES) | intMask;
  464. }
  465. /**
  466. * @brief Disable interrupts in ADC (sequencers A/B and overrun)
  467. * @param pADC : The base of ADC peripheral on the chip
  468. * @param intMask : Interrupt values to be disabled (see notes)
  469. * @return None
  470. * @note Select one or more OR'ed values of ADC_INTEN_SEQA_ENABLE,
  471. * ADC_INTEN_SEQB_ENABLE, and ADC_INTEN_OVRRUN_ENABLE to disable the
  472. * specific ADC interrupts.
  473. */
  474. STATIC INLINE void Chip_ADC_DisableInt(LPC_ADC_T *pADC, uint32_t intMask)
  475. {
  476. pADC->INTEN = pADC->INTEN & ~(ADC_INTEN_RES | intMask);
  477. }
  478. /** Threshold interrupt event options */
  479. typedef enum {
  480. ADC_INTEN_THCMP_DISABLE,
  481. ADC_INTEN_THCMP_OUTSIDE,
  482. ADC_INTEN_THCMP_CROSSING,
  483. } ADC_INTEN_THCMP_T;
  484. /**
  485. * @brief Enable a threshold event interrupt in ADC
  486. * @param pADC : The base of ADC peripheral on the chip
  487. * @param ch : ADC channel to set threshold inetrrupt for, 1-8
  488. * @param thInt : Selected threshold interrupt type
  489. * @return None
  490. */
  491. STATIC INLINE void Chip_ADC_SetThresholdInt(LPC_ADC_T *pADC, uint8_t ch, ADC_INTEN_THCMP_T thInt)
  492. {
  493. pADC->INTEN = (pADC->INTEN & ~(ADC_INTEN_RES | (3 << (3 + (ch * 2))))) | (thInt << (3 + (ch * 2)));
  494. }
  495. /**
  496. * @brief Get flags register in ADC
  497. * @param pADC : The base of ADC peripheral on the chip
  498. * @return Flags register value (ORed ADC_FLAG* values)
  499. * @note Mask the return value of this function with the ADC_FLAGS_*
  500. * definitions to determine the overall ADC interrupt events.<br>
  501. * Example:<br>
  502. * if (Chip_ADC_GetFlags(LPC_ADC) & ADC_FLAGS_THCMP_MASK(3) // Check of threshold comp status for ADC channel 3
  503. */
  504. STATIC INLINE uint32_t Chip_ADC_GetFlags(LPC_ADC_T *pADC)
  505. {
  506. return pADC->FLAGS;
  507. }
  508. /**
  509. * @brief Clear flags register in ADC
  510. * @param pADC : The base of ADC peripheral on the chip
  511. * @param flags : An Or'ed values of ADC_FLAGS_* values to clear
  512. * @return Flags register value (ORed ADC_FLAG* values)
  513. */
  514. STATIC INLINE void Chip_ADC_ClearFlags(LPC_ADC_T *pADC, uint32_t flags)
  515. {
  516. pADC->FLAGS = flags;
  517. }
  518. /**
  519. * @brief Set Trim register in ADC
  520. * @param pADC : The base of ADC peripheral on the chip
  521. * @param trim : Trim value (ADC_TRIM_VRANGE_HIGHV or ADC_TRIM_VRANGE_LOWV)
  522. * @return None
  523. */
  524. STATIC INLINE void Chip_ADC_SetTrim(LPC_ADC_T *pADC, uint32_t trim)
  525. {
  526. pADC->TRM = trim;
  527. }
  528. /**
  529. * @}
  530. */
  531. #ifdef __cplusplus
  532. }
  533. #endif
  534. #endif /* __ADC_8XX_H_ */