apm32f10x_adc.h 12 KB

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