apm32f10x_adc.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /*!
  2. * @file apm32f10x_adc.h
  3. *
  4. * @brief This file contains all the functions prototypes for the ADC firmware library
  5. *
  6. * @version V1.0.2
  7. *
  8. * @date 2022-01-05
  9. *
  10. * @attention
  11. *
  12. * Copyright (C) 2020-2022 Geehy Semiconductor
  13. *
  14. * You may not use this file except in compliance with the
  15. * GEEHY COPYRIGHT NOTICE (GEEHY SOFTWARE PACKAGE LICENSE).
  16. *
  17. * The program is only for reference, which is distributed in the hope
  18. * that it will be usefull and instructional for customers to develop
  19. * their software. Unless required by applicable law or agreed to in
  20. * writing, the program is distributed on an "AS IS" BASIS, WITHOUT
  21. * ANY WARRANTY OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the GEEHY SOFTWARE PACKAGE LICENSE for the governing permissions
  23. * and limitations under the License.
  24. */
  25. #ifndef __APM32F10X_ADC_H
  26. #define __APM32F10X_ADC_H
  27. #ifdef __cplusplus
  28. extern "C" {
  29. #endif
  30. #include "apm32f10x.h"
  31. /** @addtogroup Peripherals_Library Standard Peripheral Library
  32. @{
  33. */
  34. /** @addtogroup ADC_Driver ADC Driver
  35. @{
  36. */
  37. /** @addtogroup ADC_Enumerations Enumerations
  38. @{
  39. */
  40. /**
  41. * @brief ADC configuration Mode
  42. */
  43. typedef enum
  44. {
  45. ADC_MODE_INDEPENDENT = ((uint32_t)0x00000000), //!< Independent mode
  46. ADC_MODE_REG_INJEC_SIMULT = ((uint32_t)0x00010000), //!< Combined regular simultaneous and injected simultaneous mode
  47. ADC_MODE_REG_SIMULT_ALTER_TRIG = ((uint32_t)0x00020000), //!< Combined regular simultaneous and alternate trigger mode
  48. ADC_MODE_INJEC_SIMULT_FAST_TNTERL = ((uint32_t)0x00030000), //!< Combined injected simultaneous and fast interleaved mode
  49. ADC_MODE_INJEC_SIMULT_SLOW_INTERL = ((uint32_t)0x00040000), //!< Combined injected simultaneous and slow interleaved mode
  50. ADC_MODE_INJEC_SIMULT = ((uint32_t)0x00050000), //!< Injected simultaneous mode
  51. ADC_MODE_REG_SIMULT = ((uint32_t)0x00060000), //!< Regular simultaneous mode
  52. ADC_MODE_FAST_INTERL = ((uint32_t)0x00070000), //!< Fast interleaved mode
  53. ADC_MODE_SLOW_INTERL = ((uint32_t)0x00080000), //!< Slow interleaved mode
  54. ADC_MODE_ALTER_TRIG = ((uint32_t)0x00090000) //!< Alternate trigger mode
  55. } ADC_MODE_T;
  56. /**
  57. * @brief ADC external trigger sources for regular channels conversion enumeration
  58. */
  59. typedef enum
  60. {
  61. ADC_EXT_TRIG_CONV_TMR1_CC1 = ((uint32_t)0x00000000),
  62. ADC_EXT_TRIG_CONV_TMR1_CC2 = ((uint32_t)0x00020000),
  63. ADC_EXT_TRIG_CONV_TMR2_CC2 = ((uint32_t)0x00060000),
  64. ADC_EXT_TRIG_CONV_TMR3_TRGO = ((uint32_t)0x00080000),
  65. ADC_EXT_TRIG_CONV_TMR4_CC4 = ((uint32_t)0x000A0000),
  66. ADC_EXT_TRIG_CONV_EINT11_T8_TRGO = ((uint32_t)0x000C0000),
  67. ADC_EXT_TRIG_CONV_TMR1_CC3 = ((uint32_t)0x00040000),
  68. ADC_EXT_TRIG_CONV_None = ((uint32_t)0x000E0000),
  69. ADC_EXT_TRIG_CONV_TMR3_CC1 = ((uint32_t)0x00000000),
  70. ADC_EXT_TRIG_CONV_TMR2_CC3 = ((uint32_t)0x00020000),
  71. ADC_EXT_TRIG_CONV_TMR8_CC1 = ((uint32_t)0x00060000),
  72. ADC_EXT_TRIG_CONV_TMR8_TRGO = ((uint32_t)0x00080000),
  73. ADC_EXT_TRIG_CONV_TMR5_CC1 = ((uint32_t)0x000A0000),
  74. ADC_EXT_TRIG_CONV_TMR5_CC3 = ((uint32_t)0x000C0000)
  75. } ADC_EXT_TRIG_CONV_T;
  76. /**
  77. * @brief ADC Data Align
  78. */
  79. typedef enum
  80. {
  81. ADC_DATA_ALIGN_RIGHT = 0x00000000,
  82. ADC_DATA_ALIGN_LEFT = 0x00000800
  83. } ADC_DATA_ALIGN_T;
  84. /**
  85. * @brief ADC Channels
  86. */
  87. typedef enum
  88. {
  89. ADC_CHANNEL_0 = ((uint8_t)0x00),
  90. ADC_CHANNEL_1 = ((uint8_t)0x01),
  91. ADC_CHANNEL_2 = ((uint8_t)0x02),
  92. ADC_CHANNEL_3 = ((uint8_t)0x03),
  93. ADC_CHANNEL_4 = ((uint8_t)0x04),
  94. ADC_CHANNEL_5 = ((uint8_t)0x05),
  95. ADC_CHANNEL_6 = ((uint8_t)0x06),
  96. ADC_CHANNEL_7 = ((uint8_t)0x07),
  97. ADC_CHANNEL_8 = ((uint8_t)0x08),
  98. ADC_CHANNEL_9 = ((uint8_t)0x09),
  99. ADC_CHANNEL_10 = ((uint8_t)0x0A),
  100. ADC_CHANNEL_11 = ((uint8_t)0x0B),
  101. ADC_CHANNEL_12 = ((uint8_t)0x0C),
  102. ADC_CHANNEL_13 = ((uint8_t)0x0D),
  103. ADC_CHANNEL_14 = ((uint8_t)0x0E),
  104. ADC_CHANNEL_15 = ((uint8_t)0x0F),
  105. ADC_CHANNEL_16 = ((uint8_t)0x10),
  106. ADC_CHANNEL_TEMP_SENSOR = ((uint8_t)0x10),
  107. ADC_CHANNEL_17 = ((uint8_t)0x11),
  108. ADC_CHANNEL_V_REFINT = ((uint8_t)0x11)
  109. } ADC_CHANNEL_T;
  110. /**
  111. * @brief ADC Sampling Time
  112. */
  113. typedef enum
  114. {
  115. ADC_SAMPLETIME_1CYCLES5 = ((uint8_t)0x00),
  116. ADC_SAMPLETIME_7CYCLES5 = ((uint8_t)0x01),
  117. ADC_SAMPLETIME_13CYCLES5 = ((uint8_t)0x02),
  118. ADC_SAMPLETIME_28CYCLES5 = ((uint8_t)0x03),
  119. ADC_SAMPLETIME_41CYCLES5 = ((uint8_t)0x04),
  120. ADC_SAMPLETIME_55CYCLES5 = ((uint8_t)0x05),
  121. ADC_SAMPLETIME_71CYCLES5 = ((uint8_t)0x06),
  122. ADC_SAMPLETIME_239CYCLES5 = ((uint8_t)0x07)
  123. } ADC_SAMPLETIME_T;
  124. /**
  125. * @brief ADC external trigger sources for injected channels conversion
  126. */
  127. typedef enum
  128. {
  129. /** for ADC1 and ADC2 */
  130. ADC_EXT_TRIG_INJEC_CONV_TMR2_TRGO = ((uint8_t)0x02),
  131. ADC_EXT_TRIG_INJEC_CONV_TMR2_CC1 = ((uint8_t)0x03),
  132. ADC_EXT_TRIG_INJEC_CONV_TMR3_CC4 = ((uint8_t)0x04),
  133. ADC_EXT_TRIG_INJEC_CONV_TMR4_TRGO = ((uint8_t)0x05),
  134. ADC_EXT_TRIG_INJEC_CONV_EINT15_T8_CC4 = ((uint8_t)0x06),
  135. /** for ADC1, ADC2 and ADC3 */
  136. ADC_EXT_TRIG_INJEC_CONV_TMR1_TRGO = ((uint8_t)0x00),
  137. ADC_EXT_TRIG_INJEC_CONV_TMR1_CC4 = ((uint8_t)0x01),
  138. ADC_EXT_TRIG_INJEC_CONV_NONE = ((uint8_t)0x07),
  139. /** for ADC3 only */
  140. ADC_EXT_TRIG_INJEC_CONV_TMR4_CC3 = ((uint8_t)0x02),
  141. ADC_EXT_TRIG_INJEC_CONV_TMR8_CC2 = ((uint8_t)0x03),
  142. ADC_EXT_TRIG_INJEC_CONV_TMR8_CC4 = ((uint8_t)0x04),
  143. ADC_EXT_TRIG_INJEC_CONV_TMR5_TRGO = ((uint8_t)0x05),
  144. ADC_EXT_TRIG_INJEC_CONV_TMR5_CC4 = ((uint8_t)0x06)
  145. } ADC_EXT_TRIG_INJEC_CONV_T;
  146. /**
  147. * @brief ADC Injected channels
  148. */
  149. typedef enum
  150. {
  151. ADC_INJEC_CHANNEL_1 = ((uint8_t)0x14),
  152. ADC_INJEC_CHANNEL_2 = ((uint8_t)0x18),
  153. ADC_INJEC_CHANNEL_3 = ((uint8_t)0x1C),
  154. ADC_INJEC_CHANNEL_4 = ((uint8_t)0x20)
  155. } ADC_INJEC_CHANNEL_T;
  156. /**
  157. * @brief ADC Analog Watchdog Selection
  158. */
  159. typedef enum
  160. {
  161. ADC_ANALOG_WATCHDOG_SINGLE_REG = ((uint32_t)0x00800200),
  162. ADC_ANALOG_WATCHDOG_SINGLE_INJEC = ((uint32_t)0x00400200),
  163. ADC_ANALOG_WATCHDOG_SINGLE_REG_INJEC = ((uint32_t)0x00C00200),
  164. ADC_ANALOG_WATCHDOG_ALL_REG = ((uint32_t)0x00800000),
  165. ADC_ANALOG_WATCHDOG_ALL_INJEC = ((uint32_t)0x00400000),
  166. ADC_ANALOG_WATCHDOG_ALL_REG_ALL_INJEC = ((uint32_t)0x00C00000),
  167. ADC_ANALOG_WATCHDOG_NONE = ((uint32_t)0x00000000)
  168. } ADC_ANALOG_WATCHDOG_T;
  169. /**
  170. * @brief ADC Interrupt definition
  171. */
  172. typedef enum
  173. {
  174. ADC_INT_AWD = ((uint16_t)0x0140), //!< Analog Watchdog interrupt
  175. ADC_INT_EOC = ((uint16_t)0x0220), //!< End Of Conversion interrupt
  176. ADC_INT_INJEOC = ((uint16_t)0x0480) //!< Injected Channel End Of Conversion interrupt
  177. } ADC_INT_T;
  178. /**
  179. * @brief ADC Flag
  180. */
  181. typedef enum
  182. {
  183. ADC_FLAG_AWD = ((uint8_t)0x01), //!< Analog Watchdog event occur flag
  184. ADC_FLAG_EOC = ((uint8_t)0x02), //!< End Of Conversion flag
  185. ADC_FLAG_INJEOC = ((uint8_t)0x04), //!< Injected Channel End Of Conversion flag
  186. ADC_FLAG_INJCS = ((uint8_t)0x08), //!< Injected Channel Conversion Start flag
  187. ADC_FLAG_REGCS = ((uint8_t)0x10) //!< Regular Channel Conversion Start flag
  188. } ADC_FLAG_T;
  189. /**@} end of group ADC_Enumerations*/
  190. /** @addtogroup ADC_Macros Macros
  191. @{
  192. */
  193. /** ADC_IJD Offset */
  194. #define INJDATA_OFFSET ((uint8_t)0x28)
  195. /** ADC_RDG register address */
  196. #define RDG_ADDRESS ((uint32_t)0x4001244C)
  197. /** INJSEQ register config */
  198. #define INJSEQ_SET_INJSEQC ((uint32_t)0x0000001F)
  199. #define INJSEQ_SET_INJSEQLEN ((uint32_t)0x00300000)
  200. /** SMPTIM register SET */
  201. #define SMPCYCCFG_SET_SMPTIM1 ((uint32_t)0x00000007)
  202. #define SMPCYCCFG_SET_SMPTIM2 ((uint32_t)0x00000007)
  203. /** REGSEQ register SET */
  204. #define REGSEQC_SET_REGSEQ3 ((uint32_t)0x0000001F)
  205. #define REGSEQC_SET_REGSEQ2 ((uint32_t)0x0000001F)
  206. #define REGSEQC_SET_REGSEQ1 ((uint32_t)0x0000001F)
  207. /**@} end of group ADC_Macros*/
  208. /** @addtogroup ADC_Structure Data Structure
  209. @{
  210. */
  211. /**
  212. * @brief ADC Config structure definition
  213. */
  214. typedef struct
  215. {
  216. ADC_MODE_T mode;
  217. uint8_t scanConvMode; //!< This parameter can be ENABLE or DISABLE.
  218. uint8_t continuosConvMode; //!< This parameter can be ENABLE or DISABLE.
  219. ADC_EXT_TRIG_CONV_T externalTrigConv;
  220. ADC_DATA_ALIGN_T dataAlign;
  221. uint8_t nbrOfChannel; //!< This parameter must range from 1 to 16.
  222. } ADC_Config_T;
  223. /**@} end of group ADC_Structure*/
  224. /** @addtogroup ADC_Fuctions Fuctions
  225. @{
  226. */
  227. /** ADC reset and common configuration */
  228. void ADC_Reset(ADC_T* adc);
  229. void ADC_Config(ADC_T* adc, ADC_Config_T* adcConfig);
  230. void ADC_ConfigStructInit(ADC_Config_T* adcConfig);
  231. void ADC_ConfigRegularChannel(ADC_T* adc, uint8_t channel,uint8_t rank, uint8_t sampleTime);
  232. void ADC_Enable(ADC_T* adc);
  233. void ADC_Disable(ADC_T* adc);
  234. /** ADC for DMA */
  235. void ADC_EnableDMA(ADC_T* adc);
  236. void ADC_DisableDMA(ADC_T* adc);
  237. /** ADC Calibration */
  238. void ADC_ResetCalibration(ADC_T* adc);
  239. uint8_t ADC_ReadResetCalibrationStatus(ADC_T* adc);
  240. void ADC_StartCalibration(ADC_T* adc);
  241. uint8_t ADC_ReadCalibrationStartFlag(ADC_T* adc);
  242. /** ADC software start conversion */
  243. void ADC_EnableSoftwareStartConv(ADC_T* adc);
  244. void ADC_DisableSoftwareStartConv(ADC_T* adc);
  245. uint8_t ADC_ReadSoftwareStartConvStatus(ADC_T* adc);
  246. /** ADC Discontinuous mode */
  247. void ADC_ConfigDiscMode(ADC_T* adc, uint8_t number);
  248. void ADC_EnableDiscMode(ADC_T* adc);
  249. void ADC_DisableDiscMode(ADC_T* adc);
  250. /** ADC External trigger conversion */
  251. void ADC_EnableExternalTrigConv(ADC_T* adc);
  252. void ADC_DisableExternalTrigConv(ADC_T* adc);
  253. /** ADC Conversion result */
  254. uint16_t ADC_ReadConversionValue(ADC_T* adc);
  255. uint32_t ADC_ReadDualModeConversionValue(ADC_T* adc);
  256. /** ADC Automatic injected group */
  257. void ADC_EnableAutoInjectedConv(ADC_T* adc);
  258. void ADC_DisableAutoInjectedConv(ADC_T* adc);
  259. void ADC_EnableInjectedDiscMode(ADC_T* adc);
  260. void ADC_DisableInjectedDiscMode(ADC_T* adc);
  261. /** ADC External trigger for injected channels conversion */
  262. void ADC_ConfigExternalTrigInjectedConv(ADC_T* adc, ADC_EXT_TRIG_INJEC_CONV_T extTrigInjecConv);
  263. void ADC_EnableExternalTrigInjectedConv(ADC_T* adc);
  264. void ADC_DisableExternalTrigInjectedConv(ADC_T* adc);
  265. /** ADC Start of the injected channels conversion */
  266. void ADC_EnableSoftwareStartInjectedConv(ADC_T* adc);
  267. void ADC_DisableSoftwareStartInjectedConv(ADC_T* adc);
  268. uint8_t ADC_ReadSoftwareStartInjectedConvStatus(ADC_T* adc);
  269. /** ADC injected channel */
  270. void ADC_ConfigInjectedChannel(ADC_T* adc, uint8_t channel, uint8_t rank, uint8_t sampleTime);
  271. void ADC_ConfigInjectedSequencerLength(ADC_T* adc, uint8_t length);
  272. void ADC_ConfigInjectedOffset(ADC_T* adc, ADC_INJEC_CHANNEL_T channel, uint16_t offSet);
  273. uint16_t ADC_ReadInjectedConversionValue(ADC_T* adc, ADC_INJEC_CHANNEL_T channel);
  274. /** ADC analog watchdog */
  275. void ADC_EnableAnalogWatchdog(ADC_T* adc, uint32_t analogWatchdog);
  276. void ADC_DisableAnalogWatchdog(ADC_T* adc);
  277. void ADC_ConfigAnalogWatchdogThresholds(ADC_T* adc, uint16_t highThreshold, uint16_t lowThreshold);
  278. void ADC_ConfigAnalogWatchdogSingleChannel(ADC_T* adc, uint8_t channel);
  279. /** ADC temperature sensor */
  280. void ADC_EnableTempSensorVrefint(ADC_T* adc);
  281. void ADC_DisableTempSensorVrefint(ADC_T* adc);
  282. /** Interrupt and flag */
  283. void ADC_EnableInterrupt(ADC_T* adc, uint16_t interrupt);
  284. void ADC_DisableInterrupt(ADC_T* adc, uint16_t interrupt);
  285. uint8_t ADC_ReadStatusFlag(ADC_T* adc, ADC_FLAG_T flag);
  286. void ADC_ClearStatusFlag(ADC_T* adc, uint8_t flag);
  287. uint8_t ADC_ReadIntFlag(ADC_T* adc, ADC_INT_T flag);
  288. void ADC_ClearIntFlag(ADC_T* adc, uint16_t flag);
  289. /**@} end of group ADC_Fuctions*/
  290. /**@} end of group ADC_Driver*/
  291. /**@} end of group Peripherals_Library*/
  292. #ifdef __cplusplus
  293. }
  294. #endif
  295. #endif /** __APM32F10X_ADC_H */