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.11 of the AmbiqSuite Development Package.
  45. //
  46. //*****************************************************************************
  47. #ifndef AM_HAL_ADC_H
  48. #define AM_HAL_ADC_H
  49. //*****************************************************************************
  50. //
  51. //! @name Clock Selection
  52. //! @brief These macros may be used to set the ADC module's clock source.
  53. //! @{
  54. //
  55. //*****************************************************************************
  56. #define AM_HAL_ADC_CLOCK_OFF AM_REG_ADC_CFG_CLKSEL_OFF
  57. #define AM_HAL_ADC_CLOCK_HFRC AM_REG_ADC_CFG_CLKSEL_HFRC
  58. #define AM_HAL_ADC_CLOCK_DIV2 AM_REG_ADC_CFG_CLKSEL_HFRC_DIV2
  59. //! @}
  60. //*****************************************************************************
  61. //
  62. //! @name Trigger Settings
  63. //! @brief ADC trigger setting macros.
  64. //!
  65. //! These macros alter the ADC's trigger source and trigger polarity. Note that
  66. //! the external trigger setting needs to be ORed with a POS or NEG option to
  67. //! define the desired trigger polarity.
  68. //! @{
  69. //
  70. //*****************************************************************************
  71. #define AM_HAL_ADC_TRIGGER_SOFT AM_REG_ADC_CFG_TRIGSEL_SWT
  72. #define AM_HAL_ADC_TRIGGER_VCOMP AM_REG_ADC_CFG_TRIGSEL_VCOMP
  73. #define AM_HAL_ADC_TRIGGER_EXT0 AM_REG_ADC_CFG_TRIGSEL_EXT0
  74. #define AM_HAL_ADC_TRIGGER_EXT1 AM_REG_ADC_CFG_TRIGSEL_EXT1
  75. #define AM_HAL_ADC_TRIGGER_EXT2 AM_REG_ADC_CFG_TRIGSEL_EXT2
  76. #define AM_HAL_ADC_TRIGGER_EXT3 AM_REG_ADC_CFG_TRIGSEL_EXT3
  77. #define AM_HAL_ADC_TRIGGER_FALL AM_REG_ADC_CFG_TRIGPOL_FALLING_EDGE
  78. #define AM_HAL_ADC_TRIGGER_RISE AM_REG_ADC_CFG_TRIGPOL_RISING_EDGE
  79. //! @}
  80. //*****************************************************************************
  81. //
  82. //! @name Reference Settings
  83. //! @brief ADC reference voltage setting macros.
  84. //!
  85. //! These macros control the ADC reference voltage source.
  86. //! @{
  87. //
  88. //*****************************************************************************
  89. #define AM_HAL_ADC_REF_EXT_2P0 AM_REG_ADC_CFG_REFSEL_EXT2P0
  90. #define AM_HAL_ADC_REF_EXT_1P5 AM_REG_ADC_CFG_REFSEL_EXT1P5
  91. #define AM_HAL_ADC_REF_INT_2P0 AM_REG_ADC_CFG_REFSEL_INT2P0
  92. #define AM_HAL_ADC_REF_INT_1P5 AM_REG_ADC_CFG_REFSEL_INT1P5
  93. //! @}
  94. //*****************************************************************************
  95. //
  96. //! @name Clock Mode
  97. //! @brief ADC clock mode settings
  98. //!
  99. //! These macros determine whether the ADC shuts down its clock between
  100. //! samples. Shutting down the clock will reduce power consumption, but
  101. //! increase latency. This setting is only valid for LPMODE 0. For other modes,
  102. //! it will be ignored.
  103. //!
  104. //! @{
  105. //
  106. //*****************************************************************************
  107. #define AM_HAL_ADC_CK_LOW_POWER AM_REG_ADC_CFG_CKMODE_LPCKMODE
  108. #define AM_HAL_ADC_CK_LOW_LATENCY AM_REG_ADC_CFG_CKMODE_LLCKMODE
  109. //! @}
  110. //*****************************************************************************
  111. //
  112. //! @name Low Power Mode
  113. //! @brief ADC power conservation settings.
  114. //!
  115. //! These macros select the power state to enter between active scans. Each low
  116. //! power mode has its own set of timing constraints. Please see the datasheet
  117. //! for additional timing information on each power mode.
  118. //! @{
  119. //
  120. //*****************************************************************************
  121. #define AM_HAL_ADC_LPMODE_0 AM_REG_ADC_CFG_LPMODE_MODE0
  122. #define AM_HAL_ADC_LPMODE_1 AM_REG_ADC_CFG_LPMODE_MODE1
  123. //! @}
  124. //*****************************************************************************
  125. //
  126. //! @name Repeat Mode
  127. //! @brief Enable repeating scan mode.
  128. //!
  129. //! Use this macro to enable repeating scans using timer 3.
  130. //!
  131. //! @{
  132. //
  133. //*****************************************************************************
  134. #define AM_HAL_ADC_REPEAT AM_REG_ADC_CFG_RPTEN(1)
  135. #define AM_HAL_ADC_NO_REPEAT AM_REG_ADC_CFG_RPTEN(0)
  136. //! @}
  137. //*****************************************************************************
  138. //
  139. //! @name Slot configuration
  140. //! @brief Slot configuration macros
  141. //!
  142. //! These macros may be used to configure an individual ADC slot.
  143. //! @{
  144. //
  145. //*****************************************************************************
  146. // Set number of samples to average.
  147. #define AM_HAL_ADC_SLOT_AVG_1 AM_REG_ADC_SL0CFG_ADSEL0(0)
  148. #define AM_HAL_ADC_SLOT_AVG_2 AM_REG_ADC_SL0CFG_ADSEL0(1)
  149. #define AM_HAL_ADC_SLOT_AVG_4 AM_REG_ADC_SL0CFG_ADSEL0(2)
  150. #define AM_HAL_ADC_SLOT_AVG_8 AM_REG_ADC_SL0CFG_ADSEL0(3)
  151. #define AM_HAL_ADC_SLOT_AVG_16 AM_REG_ADC_SL0CFG_ADSEL0(4)
  152. #define AM_HAL_ADC_SLOT_AVG_32 AM_REG_ADC_SL0CFG_ADSEL0(5)
  153. #define AM_HAL_ADC_SLOT_AVG_64 AM_REG_ADC_SL0CFG_ADSEL0(6)
  154. #define AM_HAL_ADC_SLOT_AVG_128 AM_REG_ADC_SL0CFG_ADSEL0(7)
  155. // Set slot precision mode.
  156. #define AM_HAL_ADC_SLOT_14BIT AM_REG_ADC_SL0CFG_PRMODE0_P14B
  157. #define AM_HAL_ADC_SLOT_12BIT AM_REG_ADC_SL0CFG_PRMODE0_P14B
  158. #define AM_HAL_ADC_SLOT_10BIT AM_REG_ADC_SL0CFG_PRMODE0_P14B
  159. #define AM_HAL_ADC_SLOT_8BIT AM_REG_ADC_SL0CFG_PRMODE0_P14B
  160. // Select a channel by number.
  161. #define AM_HAL_ADC_SLOT_CHANNEL(n) AM_REG_ADC_SL0CFG_CHSEL0(n)
  162. // Single-ended channels
  163. #define AM_HAL_ADC_SLOT_CHSEL_SE0 AM_REG_ADC_SL0CFG_CHSEL0_SE0
  164. #define AM_HAL_ADC_SLOT_CHSEL_SE1 AM_REG_ADC_SL0CFG_CHSEL0_SE1
  165. #define AM_HAL_ADC_SLOT_CHSEL_SE2 AM_REG_ADC_SL0CFG_CHSEL0_SE2
  166. #define AM_HAL_ADC_SLOT_CHSEL_SE3 AM_REG_ADC_SL0CFG_CHSEL0_SE3
  167. #define AM_HAL_ADC_SLOT_CHSEL_SE4 AM_REG_ADC_SL0CFG_CHSEL0_SE4
  168. #define AM_HAL_ADC_SLOT_CHSEL_SE5 AM_REG_ADC_SL0CFG_CHSEL0_SE5
  169. #define AM_HAL_ADC_SLOT_CHSEL_SE6 AM_REG_ADC_SL0CFG_CHSEL0_SE6
  170. #define AM_HAL_ADC_SLOT_CHSEL_SE7 AM_REG_ADC_SL0CFG_CHSEL0_SE7
  171. #define AM_HAL_ADC_SLOT_CHSEL_SE8 AM_REG_ADC_SL0CFG_CHSEL0_SE8
  172. #define AM_HAL_ADC_SLOT_CHSEL_SE9 AM_REG_ADC_SL0CFG_CHSEL0_SE9
  173. // Differential channels.
  174. #define AM_HAL_ADC_SLOT_CHSEL_DF0 AM_REG_ADC_SL0CFG_CHSEL0_DF0
  175. #define AM_HAL_ADC_SLOT_CHSEL_DF1 AM_REG_ADC_SL0CFG_CHSEL0_DF1
  176. // Miscellaneous other signals.
  177. #define AM_HAL_ADC_SLOT_CHSEL_TEMP AM_REG_ADC_SL0CFG_CHSEL0_TEMP
  178. #define AM_HAL_ADC_SLOT_CHSEL_VSS AM_REG_ADC_SL0CFG_CHSEL0_VSS
  179. #define AM_HAL_ADC_SLOT_CHSEL_VBATT AM_REG_ADC_SL0CFG_CHSEL0_BATT
  180. // Window enable.
  181. #define AM_HAL_ADC_SLOT_WINDOW_EN AM_REG_ADC_SL0CFG_WCEN0(1)
  182. // Enable the slot.
  183. #define AM_HAL_ADC_SLOT_ENABLE AM_REG_ADC_SL0CFG_SLEN0(1)
  184. //! @}
  185. //*****************************************************************************
  186. //
  187. //! @name Interrupt Status Bits
  188. //! @brief Interrupt Status Bits for enable/disble use
  189. //!
  190. //! These macros may be used to enable an individual ADC interrupt cause.
  191. //! @{
  192. //
  193. //*****************************************************************************
  194. #define AM_HAL_ADC_INT_WCINC AM_REG_ADC_INTEN_WCINC(1)
  195. #define AM_HAL_ADC_INT_WCEXC AM_REG_ADC_INTEN_WCEXC(1)
  196. #define AM_HAL_ADC_INT_FIFOOVR2 AM_REG_ADC_INTEN_FIFOOVR2(1)
  197. #define AM_HAL_ADC_INT_FIFOOVR1 AM_REG_ADC_INTEN_FIFOOVR1(1)
  198. #define AM_HAL_ADC_INT_SCNCMP AM_REG_ADC_INTEN_SCNCMP(1)
  199. #define AM_HAL_ADC_INT_CNVCMP AM_REG_ADC_INTEN_CNVCMP(1)
  200. //! @}
  201. //*****************************************************************************
  202. //
  203. //! @name Temperature Trim Value Locations
  204. //! @brief Temperature calibration cofficients are stored in readable space.
  205. //!
  206. //! These macros are used to access the temperature trim values in readable
  207. //! space.
  208. //! @{
  209. //
  210. //*****************************************************************************
  211. #define AM_HAL_ADC_CALIB_TEMP_ADDR (0x50023010)
  212. #define AM_HAL_ADC_CALIB_AMBIENT_ADDR (0x50023014)
  213. #define AM_HAL_ADC_CALIB_ADC_OFFSET_ADDR (0x50023018)
  214. //
  215. // Default coefficients (used when trims not provided):
  216. // TEMP_DEFAULT = Temperature in deg K (e.g. 299.5 - 273.15 = 26.35)
  217. // AMBIENT_DEFAULT = Voltage measurement at default temperature.
  218. // OFFSET_DEFAULT = Default ADC offset at 1v.
  219. //
  220. #define AM_HAL_ADC_CALIB_TEMP_DEFAULT (299.5F)
  221. #define AM_HAL_ADC_CALIB_AMBIENT_DEFAULT (1.02809F)
  222. #define AM_HAL_ADC_CALIB_ADC_OFFSET_DEFAULT (-0.004281F)
  223. //! @}
  224. //*****************************************************************************
  225. //
  226. //! @brief Configuration structure for the ADC.
  227. //
  228. //*****************************************************************************
  229. typedef struct
  230. {
  231. //! Select the ADC Clock source using one of the clock source macros.
  232. uint32_t ui32Clock;
  233. //! Select the ADC trigger source using a trigger source macro.
  234. uint32_t ui32TriggerConfig;
  235. //! Use a macro to select the ADC reference voltage.
  236. uint32_t ui32Reference;
  237. //! Use a macro to decide whether to disable clocks between samples.
  238. uint32_t ui32ClockMode;
  239. //! Use a macro to select the ADC power mode.
  240. uint32_t ui32PowerMode;
  241. //! Select whether the ADC will re-trigger based on a signal from timer 3.
  242. uint32_t ui32Repeat;
  243. }
  244. am_hal_adc_config_t;
  245. //*****************************************************************************
  246. //
  247. //! @brief ADC Fifo Read macros
  248. //!
  249. //! These are helper macros for interpreting FIFO data. Each ADC FIFO entry
  250. //! contains information about the slot number and the FIFO depth alongside the
  251. //! current sample. These macros perform the correct masking and shifting to
  252. //! read those values.
  253. //!
  254. //! The SAMPLE and FULL_SAMPLE options refer to the fractional part of averaged
  255. //! samples. If you are not using hardware averaging or don't need the
  256. //! fractional part of the ADC sample, you should just use
  257. //! AM_HAL_ADC_FIFO_SAMPLE.
  258. //!
  259. //! If you do need the fractional part, use AM_HAL_ADC_FIFO_FULL_SAMPLE. This
  260. //! macro will keep six bits of precision past the decimal point. Depending on
  261. //! the number of averaged samples, anywhere between 1 and 6 of these bits will
  262. //! be valid. Please consult the datasheet to find out how many bits of data
  263. //! are valid for your chosen averaging settings.
  264. //!
  265. //! @{
  266. //
  267. //*****************************************************************************
  268. #define AM_HAL_ADC_FIFO_SAMPLE(value) \
  269. ((((value) & AM_REG_ADC_FIFO_DATA_M) >> AM_REG_ADC_FIFO_DATA_S) >> 6)
  270. #define AM_HAL_ADC_FIFO_FULL_SAMPLE(value) \
  271. (((value) & AM_REG_ADC_FIFO_DATA_M) >> AM_REG_ADC_FIFO_DATA_S )
  272. #define AM_HAL_ADC_FIFO_SLOT(value) \
  273. (((value) & AM_REG_ADC_FIFO_SLOTNUM_M) >> AM_REG_ADC_FIFO_SLOTNUM_S)
  274. #define AM_HAL_ADC_FIFO_COUNT(value) \
  275. (((value) & AM_REG_ADC_FIFO_COUNT_M) >> AM_REG_ADC_FIFO_COUNT_S)
  276. //! @}
  277. #ifdef __cplusplus
  278. extern "C"
  279. {
  280. #endif
  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. //*****************************************************************************