adc_001.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * @brief ADC 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 __ADC_001_H_
  32. #define __ADC_001_H_
  33. #include "sys_config.h"
  34. #include "cmsis.h"
  35. #ifdef __cplusplus
  36. extern "C" {
  37. #endif
  38. /** @defgroup IP_ADC_001 IP: 10 or 12-bit ADC register block and driver
  39. * @ingroup IP_Drivers
  40. * @{
  41. */
  42. /**
  43. * @brief 10 or 12-bit ADC register block structure
  44. */
  45. typedef struct { /*!< ADCn Structure */
  46. __IO uint32_t CR; /*!< A/D Control Register. The AD0CR register must be written to select the operating mode before A/D conversion can occur. */
  47. __I uint32_t GDR; /*!< A/D Global Data Register. Contains the result of the most recent A/D conversion. */
  48. __I uint32_t RESERVED0;
  49. __IO uint32_t INTEN; /*!< A/D Interrupt Enable Register. This register contains enable bits that allow the DONE flag of each A/D channel to be included or excluded from contributing to the generation of an A/D interrupt. */
  50. __I uint32_t DR[8]; /*!< A/D Channel Data Register. This register contains the result of the most recent conversion completed on channel n. */
  51. __I uint32_t STAT; /*!< A/D Status Register. This register contains DONE and OVERRUN flags for all of the A/D channels, as well as the A/D interrupt flag. */
  52. #if !defined(CHIP_LPC18XX) && !defined(CHIP_LPC43XX)
  53. __IO uint32_t ADTRM;
  54. #endif
  55. } IP_ADC_001_Type;
  56. /**
  57. * @brief ADC register support bitfields and mask
  58. */
  59. #define ADC_DR_RESULT(n) ((((n) >> 6) & 0x3FF)) /*!< Mask for getting the ADC data read value */
  60. #define ADC_DR_DONE(n) (((n) >> 31)) /*!< Mask for reading the ADC done status */
  61. #define ADC_DR_OVERRUN(n) ((((n) >> 30) & (1UL))) /*!< Mask for reading the ADC overrun status */
  62. #define ADC_CR_CH_SEL(n) ((1UL << (n))) /*!< Selects which of the AD0.0:7 pins is (are) to be sampled and converted */
  63. #define ADC_CR_CLKDIV(n) ((((n) & 0xFF) << 8)) /*!< The APB clock (PCLK) is divided by (this value plus one) to produce the clock for the A/D */
  64. #define ADC_CR_BURST ((1UL << 16)) /*!< Repeated conversions A/D enable bit */
  65. #define ADC_CR_BITACC(n) ((((n) & 0x7) << 17)) /*!< Number of ADC accuracy bits */
  66. #define ADC_CR_PDN ((1UL << 21)) /*!< ADC convert in power down mode */
  67. #define ADC_CR_START_MASK ((7UL << 24)) /*!< ADC start mask bits */
  68. #define ADC_CR_START_MODE_SEL(SEL) ((SEL << 24)) /*!< Select Start Mode */
  69. #define ADC_CR_START_NOW ((1UL << 24)) /*!< Start conversion now */
  70. #define ADC_CR_START_CTOUT15 ((2UL << 24)) /*!< Start conversion when the edge selected by bit 27 occurs on CTOUT_15 */
  71. #define ADC_CR_START_CTOUT8 ((3UL << 24)) /*!< Start conversion when the edge selected by bit 27 occurs on CTOUT_8 */
  72. #define ADC_CR_START_ADCTRIG0 ((4UL << 24)) /*!< Start conversion when the edge selected by bit 27 occurs on ADCTRIG0 */
  73. #define ADC_CR_START_ADCTRIG1 ((5UL << 24)) /*!< Start conversion when the edge selected by bit 27 occurs on ADCTRIG1 */
  74. #define ADC_CR_START_MCOA2 ((6UL << 24)) /*!< Start conversion when the edge selected by bit 27 occurs on Motocon PWM output MCOA2 */
  75. #define ADC_CR_EDGE ((1UL << 27)) /*!< Start conversion on a falling edge on the selected CAP/MAT signal */
  76. /**
  77. * @brief ADC status register used for IP drivers
  78. */
  79. typedef enum {
  80. ADC_DR_DONE_STAT, /*!< ADC data register staus */
  81. ADC_DR_OVERRUN_STAT,/*!< ADC data overrun staus */
  82. ADC_DR_ADINT_STAT /*!< ADC interrupt status */
  83. } IP_ADC_Status;
  84. /**
  85. * @brief Initialize for ADC
  86. * @param pADC : The base of ADC peripheral on the chip
  87. * @param adcRate : Sample rate of A/D converter
  88. * @param adcPerClock : The APB clock
  89. * @param bitsAccuracy : The accuracy of LSB value, should be ADC_10BITS -> ADC_3BITS
  90. * @return Nothing
  91. * Disable all ADC interrupts, set bit PDN, set ADC clock frequency
  92. * This is not the sample rate, but the clock for the ADC machine, and is usually set to
  93. * maximum. Applications may choose a lower frequency if they have high-impedance sources.
  94. * This is because a lower clock frequency produces a longer sampling time.
  95. */
  96. void IP_ADC_Init(IP_ADC_001_Type *pADC, uint32_t adcRate, uint32_t adcPerClock, uint8_t bitsAccuracy);
  97. /**
  98. * @brief Shutdown ADC
  99. * @param pADC : The base of ADC peripheral on the chip
  100. * @return Nothing
  101. * Reset the ADC control and INTEN Register to reset values (disabled)
  102. */
  103. void IP_ADC_DeInit(IP_ADC_001_Type *pADC);
  104. /**
  105. * @brief Set burst mode for ADC
  106. * @param pADC : The base of ADC peripheral on the chip
  107. * @param NewState : ENABLE for burst mode, or DISABLE for normal mode
  108. * @return Nothing
  109. */
  110. void IP_ADC_SetBurstMode(IP_ADC_001_Type *pADC, FunctionalState NewState);
  111. /**
  112. * @brief Get the ADC value
  113. * @param pADC : The base of ADC peripheral on the chip
  114. * @param channel : Channel to be read value, should be 0..7
  115. * @param data : Data buffer to store the A/D value
  116. * @return Status : SUCCESS or ERROR
  117. */
  118. Status IP_ADC_Get_Val(IP_ADC_001_Type *pADC, uint8_t channel, uint16_t *data);
  119. /**
  120. * @brief Get ADC Channel status from ADC data register
  121. * @param pADC : The base of ADC peripheral on the chip
  122. * @param channel : Channel number, should be 0..7
  123. * @param StatusType : Register to read, ADC_DR_DONE_STAT, ADC_DR_OVERRUN_STAT, or ADC_DR_ADINT_STAT
  124. * @return Channel status, SET or RESET
  125. */
  126. FlagStatus IP_ADC_GetStatus(IP_ADC_001_Type *pADC, uint8_t channel, uint32_t StatusType);
  127. /**
  128. * @brief Set the edge start condition
  129. * @param pADC : The base of ADC peripheral on the chip
  130. * @param edge_mode : 0 = rising, != = falling
  131. * @return Nothing
  132. */
  133. void IP_ADC_EdgeStartConfig(IP_ADC_001_Type *pADC, uint8_t edge_mode);
  134. /**
  135. * @brief Enable/Disable ADC channel number
  136. * @param pADC : The base of ADC peripheral on the chip
  137. * @param channel : Channel number
  138. * @param NewState : New state, ENABLE or DISABLE
  139. * @return Nothing
  140. */
  141. void IP_ADC_SetChannelNumber(IP_ADC_001_Type *pADC, uint8_t channel, FunctionalState NewState);
  142. /**
  143. * @brief Set start mode for ADC
  144. * @param pADC : The base of ADC peripheral on the chip
  145. * @param start_mode : Start mode choose one of modes in 'ADC_START_*' enumeration type definitions
  146. * @return Nothing
  147. */
  148. void IP_ADC_SetStartMode(IP_ADC_001_Type *pADC, uint8_t start_mode);
  149. /**
  150. * @brief Enable/Disable interrupt for ADC channel
  151. * @param pADC : The base of ADC peripheral on the chip
  152. * @param channel : Channel assert the interrupt
  153. * @param NewState : New state, ENABLE or DISABLE
  154. * @return Nothing
  155. */
  156. void IP_ADC_Int_Enable(IP_ADC_001_Type *pADC, uint8_t channel, FunctionalState NewState);
  157. /**
  158. * @}
  159. */
  160. #ifdef __cplusplus
  161. }
  162. #endif
  163. #endif /* __ADC_001_H_ */