am_hal_adc.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. //*****************************************************************************
  2. //
  3. // am_hal_adc.h
  4. //! @file
  5. //!
  6. //! @brief Functions for interfacing with the Analog to Digital Converter
  7. //!
  8. //! @addtogroup adc2 Analog-to-Digital Converter (ADC)
  9. //! @ingroup apollo2hal
  10. //! @{
  11. //
  12. //*****************************************************************************
  13. //*****************************************************************************
  14. //
  15. // Copyright (c) 2017, Ambiq Micro
  16. // All rights reserved.
  17. //
  18. // Redistribution and use in source and binary forms, with or without
  19. // modification, are permitted provided that the following conditions are met:
  20. //
  21. // 1. Redistributions of source code must retain the above copyright notice,
  22. // this list of conditions and the following disclaimer.
  23. //
  24. // 2. Redistributions in binary form must reproduce the above copyright
  25. // notice, this list of conditions and the following disclaimer in the
  26. // documentation and/or other materials provided with the distribution.
  27. //
  28. // 3. Neither the name of the copyright holder nor the names of its
  29. // contributors may be used to endorse or promote products derived from this
  30. // software without specific prior written permission.
  31. //
  32. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  33. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  34. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  35. // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  36. // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  37. // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  38. // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  39. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  40. // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  42. // POSSIBILITY OF SUCH DAMAGE.
  43. //
  44. // This is part of revision 1.2.9 of the AmbiqSuite Development Package.
  45. //
  46. //*****************************************************************************
  47. #ifndef AM_HAL_ADC_H
  48. #define AM_HAL_ADC_H
  49. #ifdef __cplusplus
  50. extern "C"
  51. {
  52. #endif
  53. //*****************************************************************************
  54. //
  55. //! @name Clock Selection
  56. //! @brief These macros may be used to set the ADC module's clock source.
  57. //! @{
  58. //
  59. //*****************************************************************************
  60. #define AM_HAL_ADC_CLOCK_OFF AM_REG_ADC_CFG_CLKSEL_OFF
  61. #define AM_HAL_ADC_CLOCK_HFRC AM_REG_ADC_CFG_CLKSEL_HFRC
  62. #define AM_HAL_ADC_CLOCK_DIV2 AM_REG_ADC_CFG_CLKSEL_HFRC_DIV2
  63. //! @}
  64. //*****************************************************************************
  65. //
  66. //! @name Trigger Settings
  67. //! @brief ADC trigger setting macros.
  68. //!
  69. //! These macros alter the ADC's trigger source and trigger polarity. Note that
  70. //! the external trigger setting needs to be ORed with a POS or NEG option to
  71. //! define the desired trigger polarity.
  72. //! @{
  73. //
  74. //*****************************************************************************
  75. #define AM_HAL_ADC_TRIGGER_SOFT AM_REG_ADC_CFG_TRIGSEL_SWT
  76. #define AM_HAL_ADC_TRIGGER_VCOMP AM_REG_ADC_CFG_TRIGSEL_VCOMP
  77. #define AM_HAL_ADC_TRIGGER_EXT0 AM_REG_ADC_CFG_TRIGSEL_EXT0
  78. #define AM_HAL_ADC_TRIGGER_EXT1 AM_REG_ADC_CFG_TRIGSEL_EXT1
  79. #define AM_HAL_ADC_TRIGGER_EXT2 AM_REG_ADC_CFG_TRIGSEL_EXT2
  80. #define AM_HAL_ADC_TRIGGER_EXT3 AM_REG_ADC_CFG_TRIGSEL_EXT3
  81. #define AM_HAL_ADC_TRIGGER_FALL AM_REG_ADC_CFG_TRIGPOL_FALLING_EDGE
  82. #define AM_HAL_ADC_TRIGGER_RISE AM_REG_ADC_CFG_TRIGPOL_RISING_EDGE
  83. //! @}
  84. //*****************************************************************************
  85. //
  86. //! @name Reference Settings
  87. //! @brief ADC reference voltage setting macros.
  88. //!
  89. //! These macros control the ADC reference voltage source.
  90. //! @{
  91. //
  92. //*****************************************************************************
  93. #define AM_HAL_ADC_REF_EXT_2P0 AM_REG_ADC_CFG_REFSEL_EXT2P0
  94. #define AM_HAL_ADC_REF_EXT_1P5 AM_REG_ADC_CFG_REFSEL_EXT1P5
  95. #define AM_HAL_ADC_REF_INT_2P0 AM_REG_ADC_CFG_REFSEL_INT2P0
  96. #define AM_HAL_ADC_REF_INT_1P5 AM_REG_ADC_CFG_REFSEL_INT1P5
  97. //! @}
  98. //*****************************************************************************
  99. //
  100. //! @name Clock Mode
  101. //! @brief ADC clock mode settings
  102. //!
  103. //! These macros determine whether the ADC shuts down its clock between
  104. //! samples. Shutting down the clock will reduce power consumption, but
  105. //! increase latency. This setting is only valid for LPMODE 0. For other modes,
  106. //! it will be ignored.
  107. //!
  108. //! @{
  109. //
  110. //*****************************************************************************
  111. #define AM_HAL_ADC_CK_LOW_POWER AM_REG_ADC_CFG_CKMODE_LPCKMODE
  112. #define AM_HAL_ADC_CK_LOW_LATENCY AM_REG_ADC_CFG_CKMODE_LLCKMODE
  113. //! @}
  114. //*****************************************************************************
  115. //
  116. //! @name Low Power Mode
  117. //! @brief ADC power conservation settings.
  118. //!
  119. //! These macros select the power state to enter between active scans. Each low
  120. //! power mode has its own set of timing constraints. Please see the datasheet
  121. //! for additional timing information on each power mode.
  122. //! @{
  123. //
  124. //*****************************************************************************
  125. #define AM_HAL_ADC_LPMODE_0 AM_REG_ADC_CFG_LPMODE_MODE0
  126. #define AM_HAL_ADC_LPMODE_1 AM_REG_ADC_CFG_LPMODE_MODE1
  127. //! @}
  128. //*****************************************************************************
  129. //
  130. //! @name Repeat Mode
  131. //! @brief Enable repeating scan mode.
  132. //!
  133. //! Use this macro to enable repeating scans using timer 3.
  134. //!
  135. //! @{
  136. //
  137. //*****************************************************************************
  138. #define AM_HAL_ADC_REPEAT AM_REG_ADC_CFG_RPTEN(1)
  139. #define AM_HAL_ADC_NO_REPEAT AM_REG_ADC_CFG_RPTEN(0)
  140. //! @}
  141. //*****************************************************************************
  142. //
  143. //! @name Slot configuration
  144. //! @brief Slot configuration macros
  145. //!
  146. //! These macros may be used to configure an individual ADC slot.
  147. //! @{
  148. //
  149. //*****************************************************************************
  150. // Set number of samples to average.
  151. #define AM_HAL_ADC_SLOT_AVG_1 AM_REG_ADC_SL0CFG_ADSEL0(0)
  152. #define AM_HAL_ADC_SLOT_AVG_2 AM_REG_ADC_SL0CFG_ADSEL0(1)
  153. #define AM_HAL_ADC_SLOT_AVG_4 AM_REG_ADC_SL0CFG_ADSEL0(2)
  154. #define AM_HAL_ADC_SLOT_AVG_8 AM_REG_ADC_SL0CFG_ADSEL0(3)
  155. #define AM_HAL_ADC_SLOT_AVG_16 AM_REG_ADC_SL0CFG_ADSEL0(4)
  156. #define AM_HAL_ADC_SLOT_AVG_32 AM_REG_ADC_SL0CFG_ADSEL0(5)
  157. #define AM_HAL_ADC_SLOT_AVG_64 AM_REG_ADC_SL0CFG_ADSEL0(6)
  158. #define AM_HAL_ADC_SLOT_AVG_128 AM_REG_ADC_SL0CFG_ADSEL0(7)
  159. // Set slot precision mode.
  160. #define AM_HAL_ADC_SLOT_14BIT AM_REG_ADC_SL0CFG_PRMODE0_P14B
  161. #define AM_HAL_ADC_SLOT_12BIT AM_REG_ADC_SL0CFG_PRMODE0_P14B
  162. #define AM_HAL_ADC_SLOT_10BIT AM_REG_ADC_SL0CFG_PRMODE0_P14B
  163. #define AM_HAL_ADC_SLOT_8BIT AM_REG_ADC_SL0CFG_PRMODE0_P14B
  164. // Select a channel by number.
  165. #define AM_HAL_ADC_SLOT_CHANNEL(n) AM_REG_ADC_SL0CFG_CHSEL0(n)
  166. // Single-ended channels
  167. #define AM_HAL_ADC_SLOT_CHSEL_SE0 AM_REG_ADC_SL0CFG_CHSEL0_SE0
  168. #define AM_HAL_ADC_SLOT_CHSEL_SE1 AM_REG_ADC_SL0CFG_CHSEL0_SE1
  169. #define AM_HAL_ADC_SLOT_CHSEL_SE2 AM_REG_ADC_SL0CFG_CHSEL0_SE2
  170. #define AM_HAL_ADC_SLOT_CHSEL_SE3 AM_REG_ADC_SL0CFG_CHSEL0_SE3
  171. #define AM_HAL_ADC_SLOT_CHSEL_SE4 AM_REG_ADC_SL0CFG_CHSEL0_SE4
  172. #define AM_HAL_ADC_SLOT_CHSEL_SE5 AM_REG_ADC_SL0CFG_CHSEL0_SE5
  173. #define AM_HAL_ADC_SLOT_CHSEL_SE6 AM_REG_ADC_SL0CFG_CHSEL0_SE6
  174. #define AM_HAL_ADC_SLOT_CHSEL_SE7 AM_REG_ADC_SL0CFG_CHSEL0_SE7
  175. #define AM_HAL_ADC_SLOT_CHSEL_SE8 AM_REG_ADC_SL0CFG_CHSEL0_SE8
  176. #define AM_HAL_ADC_SLOT_CHSEL_SE9 AM_REG_ADC_SL0CFG_CHSEL0_SE9
  177. // Differential channels.
  178. #define AM_HAL_ADC_SLOT_CHSEL_DF0 AM_REG_ADC_SL0CFG_CHSEL0_DF0
  179. #define AM_HAL_ADC_SLOT_CHSEL_DF1 AM_REG_ADC_SL0CFG_CHSEL0_DF1
  180. // Miscellaneous other signals.
  181. #define AM_HAL_ADC_SLOT_CHSEL_TEMP AM_REG_ADC_SL0CFG_CHSEL0_TEMP
  182. #define AM_HAL_ADC_SLOT_CHSEL_VSS AM_REG_ADC_SL0CFG_CHSEL0_VSS
  183. #define AM_HAL_ADC_SLOT_CHSEL_VBATT AM_REG_ADC_SL0CFG_CHSEL0_BATT
  184. // Window enable.
  185. #define AM_HAL_ADC_SLOT_WINDOW_EN AM_REG_ADC_SL0CFG_WCEN0(1)
  186. // Enable the slot.
  187. #define AM_HAL_ADC_SLOT_ENABLE AM_REG_ADC_SL0CFG_SLEN0(1)
  188. //! @}
  189. //*****************************************************************************
  190. //
  191. //! @name Interrupt Status Bits
  192. //! @brief Interrupt Status Bits for enable/disble use
  193. //!
  194. //! These macros may be used to enable an individual ADC interrupt cause.
  195. //! @{
  196. //
  197. //*****************************************************************************
  198. #define AM_HAL_ADC_INT_WCINC AM_REG_ADC_INTEN_WCINC(1)
  199. #define AM_HAL_ADC_INT_WCEXC AM_REG_ADC_INTEN_WCEXC(1)
  200. #define AM_HAL_ADC_INT_FIFOOVR2 AM_REG_ADC_INTEN_FIFOOVR2(1)
  201. #define AM_HAL_ADC_INT_FIFOOVR1 AM_REG_ADC_INTEN_FIFOOVR1(1)
  202. #define AM_HAL_ADC_INT_SCNCMP AM_REG_ADC_INTEN_SCNCMP(1)
  203. #define AM_HAL_ADC_INT_CNVCMP AM_REG_ADC_INTEN_CNVCMP(1)
  204. //! @}
  205. //*****************************************************************************
  206. //
  207. //! @name Temperature Trim Value Locations
  208. //! @brief Temperature calibration cofficients are stored in readable space.
  209. //!
  210. //! These macros are used to access the temperature trim values in readable
  211. //! space.
  212. //! @{
  213. //
  214. //*****************************************************************************
  215. #define AM_HAL_ADC_CALIB_TEMP_ADDR (0x50023010)
  216. #define AM_HAL_ADC_CALIB_AMBIENT_ADDR (0x50023014)
  217. #define AM_HAL_ADC_CALIB_ADC_OFFSET_ADDR (0x50023018)
  218. //
  219. // Default coefficients (used when trims not provided):
  220. // TEMP_DEFAULT = Temperature in deg K (e.g. 299.5 - 273.15 = 26.35)
  221. // AMBIENT_DEFAULT = Voltage measurement at default temperature.
  222. // OFFSET_DEFAULT = Default ADC offset at 1v.
  223. //
  224. #define AM_HAL_ADC_CALIB_TEMP_DEFAULT (299.5F)
  225. #define AM_HAL_ADC_CALIB_AMBIENT_DEFAULT (1.02809F)
  226. #define AM_HAL_ADC_CALIB_ADC_OFFSET_DEFAULT (-0.004281F)
  227. //! @}
  228. //*****************************************************************************
  229. //
  230. //! @brief Configuration structure for the ADC.
  231. //
  232. //*****************************************************************************
  233. typedef struct
  234. {
  235. //! Select the ADC Clock source using one of the clock source macros.
  236. uint32_t ui32Clock;
  237. //! Select the ADC trigger source using a trigger source macro.
  238. uint32_t ui32TriggerConfig;
  239. //! Use a macro to select the ADC reference voltage.
  240. uint32_t ui32Reference;
  241. //! Use a macro to decide whether to disable clocks between samples.
  242. uint32_t ui32ClockMode;
  243. //! Use a macro to select the ADC power mode.
  244. uint32_t ui32PowerMode;
  245. //! Select whether the ADC will re-trigger based on a signal from timer 3.
  246. uint32_t ui32Repeat;
  247. }
  248. am_hal_adc_config_t;
  249. //*****************************************************************************
  250. //
  251. //! @brief ADC Fifo Read macros
  252. //!
  253. //! These are helper macros for interpreting FIFO data. Each ADC FIFO entry
  254. //! contains information about the slot number and the FIFO depth alongside the
  255. //! current sample. These macros perform the correct masking and shifting to
  256. //! read those values.
  257. //!
  258. //! The SAMPLE and FULL_SAMPLE options refer to the fractional part of averaged
  259. //! samples. If you are not using hardware averaging or don't need the
  260. //! fractional part of the ADC sample, you should just use
  261. //! AM_HAL_ADC_FIFO_SAMPLE.
  262. //!
  263. //! If you do need the fractional part, use AM_HAL_ADC_FIFO_FULL_SAMPLE. This
  264. //! macro will keep six bits of precision past the decimal point. Depending on
  265. //! the number of averaged samples, anywhere between 1 and 6 of these bits will
  266. //! be valid. Please consult the datasheet to find out how many bits of data
  267. //! are valid for your chosen averaging settings.
  268. //!
  269. //! @{
  270. //
  271. //*****************************************************************************
  272. #define AM_HAL_ADC_FIFO_SAMPLE(value) \
  273. ((((value) & AM_REG_ADC_FIFO_DATA_M) >> AM_REG_ADC_FIFO_DATA_S) >> 6)
  274. #define AM_HAL_ADC_FIFO_FULL_SAMPLE(value) \
  275. (((value) & AM_REG_ADC_FIFO_DATA_M) >> AM_REG_ADC_FIFO_DATA_S )
  276. #define AM_HAL_ADC_FIFO_SLOT(value) \
  277. (((value) & AM_REG_ADC_FIFO_SLOTNUM_M) >> AM_REG_ADC_FIFO_SLOTNUM_S)
  278. #define AM_HAL_ADC_FIFO_COUNT(value) \
  279. (((value) & AM_REG_ADC_FIFO_COUNT_M) >> AM_REG_ADC_FIFO_COUNT_S)
  280. //! @}
  281. //*****************************************************************************
  282. //
  283. // External function definitions
  284. //
  285. //*****************************************************************************
  286. extern void am_hal_adc_config(am_hal_adc_config_t *psConfig);
  287. extern void am_hal_adc_window_set(uint32_t ui32Upper, uint32_t ui32Lower);
  288. extern void am_hal_adc_slot_config(uint32_t ui32SlotNumber,
  289. uint32_t ui32SlotConfig);
  290. extern uint32_t am_hal_adc_fifo_peek(void);
  291. extern uint32_t am_hal_adc_fifo_pop(void);
  292. extern void am_hal_adc_trigger(void);
  293. extern void am_hal_adc_enable(void);
  294. extern void am_hal_adc_disable(void);
  295. extern void am_hal_adc_int_enable(uint32_t ui32Interrupt);
  296. extern uint32_t am_hal_adc_int_enable_get(void);
  297. extern void am_hal_adc_int_disable(uint32_t ui32Interrupt);
  298. extern void am_hal_adc_int_clear(uint32_t ui32Interrupt);
  299. extern void am_hal_adc_int_set(uint32_t ui32Interrupt);
  300. extern uint32_t am_hal_adc_int_status_get(bool bEnabledOnly);
  301. extern float am_hal_adc_volts_to_celsius(float fVoltage);
  302. extern void am_hal_adc_temp_trims_get(float * pfTemp, float * pfVoltage, float * pfOffsetV);
  303. #ifdef __cplusplus
  304. }
  305. #endif
  306. #endif // AM_HAL_ADC_H
  307. //*****************************************************************************
  308. //
  309. // End Doxygen group.
  310. //! @}
  311. //
  312. //*****************************************************************************