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