apm32s10x_adc.h 11 KB

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