1 /* 2 * Copyright 2021 MindMotion Microelectronics Co., Ltd. 3 * All rights reserved. 4 * 5 * SPDX-License-Identifier: BSD-3-Clause 6 */ 7 8 #ifndef __HAL_ADC_H__ 9 #define __HAL_ADC_H__ 10 11 #include "hal_common.h" 12 13 /*! 14 * @addtogroup ADC 15 * @{ 16 */ 17 18 /*! 19 * @brief ADC driver version number. 20 */ 21 #define ADC_DRIVER_VERSION 0u /*!< adc_0. */ 22 23 /*! 24 * @addtogroup ADC_STATUS 25 * Define ADC status. 26 * @{ 27 */ 28 29 #define ADC_STATUS_CONV_SLOT_DONE (1u << 0u) /*!< Status flag when ADC slot conversion done. */ 30 #define ADC_STATUS_CONV_SAMPLE_DONE (1u << 1u) /*!< Status flag when ADC sample done. */ 31 #define ADC_STATUS_CONV_SEQ_DONE (1u << 2u) /*!< Status flag when ADC sequence conversion done. */ 32 #define ADC_STATUS_COMPARE_DONE (1u << 3u) /*!< Status flag when ADC compare done. */ 33 34 /*! 35 * @} 36 */ 37 38 /*! 39 * @addtogroup ADC_INT 40 * ADC interrupt define. 41 * @{ 42 */ 43 44 #define ADC_INT_CONV_SLOT_DONE (1u << 0u) /*!< Interrupt enable when when ADC slot conversion done. */ 45 #define ADC_INT_CONV_SAMPLE_DONE (1u << 1u) /*!< Interrupt enable when when ADC sample done. */ 46 #define ADC_INT_CONV_SEQ_DONE (1u << 2u) /*!< Interrupt enable when when ADC sequence conversion done. */ 47 #define ADC_INT_COMPARE_DONE (1u << 3u) /*!< Interrupt enable when when ADC compare done. */ 48 49 /*! 50 * @} 51 */ 52 53 /*! 54 * @addtogroup ADC_RESULT_FLAGS 55 * Define ADC Convert result flags. 56 * @{ 57 */ 58 59 #define ADC_CONV_RESULT_FLAG_OVERRUN (1u << 0u) /*!< Result flag when adc conversion result is overrun. */ 60 #define ADC_CONV_RESULT_FLAG_VALID (1u << 1u) /*!< Result flag when adc conversion result valid. */ 61 62 /*! 63 * @} 64 */ 65 66 /*! 67 * @brief ADC Resolution type. 68 * 69 * Select ADC conversion valid data bit. 70 */ 71 typedef enum 72 { 73 ADC_Resolution_12b = 0u, /*!< Resolution select 12 bit. */ 74 ADC_Resolution_11b = 1u, /*!< Resolution select 11 bit. */ 75 ADC_Resolution_10b = 2u, /*!< Resolution select 10 bit. */ 76 ADC_Resolution_9b = 3u, /*!< Resolution select 9 bit. */ 77 ADC_Resolution_8b = 4u, /*!< Resolution select 8 bit. */ 78 } ADC_Resolution_Type; 79 80 /*! 81 * @brief ADC Prescaler type. 82 * 83 * Select the prescaler of the bus as the adc clock. 84 */ 85 typedef enum 86 { 87 ADC_ClockDiv_2 = 0u, /*!< ADC clock divided by 2. */ 88 ADC_ClockDiv_3 = 1u, /*!< ADC clock divided by 3. */ 89 ADC_ClockDiv_4 = 2u, /*!< ADC clock divided by 4. */ 90 ADC_ClockDiv_5 = 3u, /*!< ADC clock divided by 5. */ 91 ADC_ClockDiv_6 = 4u, /*!< ADC clock divided by 6. */ 92 ADC_ClockDiv_7 = 5u, /*!< ADC clock divided by 7. */ 93 ADC_ClockDiv_8 = 6u, /*!< ADC clock divided by 8. */ 94 ADC_ClockDiv_9 = 7u, /*!< ADC clock divided by 9. */ 95 ADC_ClockDiv_10 = 8u, /*!< ADC clock divided by 10. */ 96 ADC_ClockDiv_11 = 9u, /*!< ADC clock divided by 11. */ 97 ADC_ClockDiv_12 = 10u, /*!< ADC clock divided by 12. */ 98 ADC_ClockDiv_13 = 11u, /*!< ADC clock divided by 13. */ 99 ADC_ClockDiv_14 = 12u, /*!< ADC clock divided by 14. */ 100 ADC_ClockDiv_15 = 13u, /*!< ADC clock divided by 15. */ 101 ADC_ClockDiv_16 = 14u, /*!< ADC clock divided by 16. */ 102 ADC_ClockDiv_17 = 15u, /*!< ADC clock divided by 17. */ 103 } ADC_ClockDiv_Type; 104 105 /*! 106 * @brief ADC HwTrgEdge type. 107 * 108 * Use hardware trigger in ADC, select the trigger edge to trigger adc. 109 */ 110 typedef enum 111 { 112 ADC_HwTrgEdge_Both = 0u, /*!< Both edge trigger. */ 113 ADC_HwTrgEdge_Falling = 1u, /*!< Falling edge trigger. */ 114 ADC_HwTrgEdge_Rising = 2u, /*!< Rising edge trigger. */ 115 ADC_HwTrgEdge_Disabled = 3u, /*!< Edge trigger is disabled. */ 116 } ADC_HwTrgEdge_Type; 117 118 /*! 119 * @brief ADC HwTrgDelayCycle type. 120 * 121 * After the hardware trigger signal is generated, delay N PCLK2 clock cycles before starting the first sample. 122 */ 123 typedef enum 124 { 125 ADC_HwTrgDelayCycle_0 = 0u, /*!< Delay 0 cycle. */ 126 ADC_HwTrgDelayCycle_4 = 1u, /*!< Delay 4 cycle. */ 127 ADC_HwTrgDelayCycle_16 = 2u, /*!< Delay 16 cycle. */ 128 ADC_HwTrgDelayCycle_32 = 3u, /*!< Delay 32 cycle. */ 129 ADC_HwTrgDelayCycle_64 = 4u, /*!< Delay 64 cycle. */ 130 ADC_HwTrgDelayCycle_128 = 5u, /*!< Delay 128 cycle. */ 131 ADC_HwTrgDelayCycle_256 = 6u, /*!< Delay 256 cycle. */ 132 ADC_HwTrgDelayCycle_512 = 7u, /*!< Delay 512 cycle. */ 133 } ADC_HwTrgDelayCycle_Type; 134 135 /*! 136 * @brief ADC Align type. 137 * 138 * Choose to store the converted data as left-aligned or right-aligned. 139 */ 140 typedef enum 141 { 142 ADC_Align_Right = 0u, /*!< Data right align. */ 143 ADC_Align_Left = 1u, /*!< Data left align. */ 144 } ADC_Align_Type; 145 146 /*! 147 * @brief ADC RegularSeqDirection type. 148 * 149 * In single-cycle scan or continuous scan mode, set the order of scan channels. 150 */ 151 typedef enum 152 { 153 ADC_RegSeqDirection_LowFirst = 0u, /*!< ADC scan direction from low channel to high channel. */ 154 ADC_RegSeqDirection_HighFirst = 1u, /*!< ADC scan direction from high channel to low channel. */ 155 } ADC_RegSeqDirection_Type; 156 157 /*! 158 * @brief ADC ConvMode type. 159 * 160 * Select the adc channel or sequence convert mode. 161 */ 162 typedef enum 163 { 164 ADC_ConvMode_SingleSlot = 0u, /*!< Run the conversion by channel. */ 165 ADC_ConvMode_Seq = 1u, /*!< Run the conversion by sequence. */ 166 ADC_ConvMode_SeqContinues = 2u, /*!< Run the conversion by sequence again and again. */ 167 } ADC_ConvMode_Type; 168 169 /*! 170 * @brief ADC HwTrgSel type. 171 * 172 * Select ADC hardware trigger source. 173 */ 174 typedef enum 175 { 176 ADC_HwTrgSource_Alt0 = 0u, /*!< source 0 as an external trigger source for ADC . */ 177 ADC_HwTrgSource_Alt1 = 1u, /*!< source 1 as an external trigger source for ADC . */ 178 ADC_HwTrgSource_Alt2 = 2u, /*!< source 2 as an external trigger source for ADC . */ 179 ADC_HwTrgSource_Alt3 = 3u, /*!< source 3 as an external trigger source for ADC . */ 180 ADC_HwTrgSource_Alt4 = 4u, /*!< source 4 as an external trigger source for ADC . */ 181 ADC_HwTrgSource_Alt5 = 5u, /*!< source 5 as an external trigger source for ADC . */ 182 ADC_HwTrgSource_Alt6 = 6u, /*!< source 6 as an external trigger source for ADC . */ 183 ADC_HwTrgSource_Alt7 = 7u, /*!< source 7 as an external trigger source for ADC . */ 184 ADC_HwTrgSource_Alt8 = 8u, /*!< source 8 as an external trigger source for ADC . */ 185 ADC_HwTrgSource_Alt9 = 9u, /*!< source 9 as an external trigger source for ADC . */ 186 ADC_HwTrgSource_Alt10 = 10u, /*!< source 10 as an external trigger source for ADC . */ 187 ADC_HwTrgSource_Alt11 = 11u, /*!< source 11 as an external trigger source for ADC . */ 188 ADC_HwTrgSource_Alt12 = 12u, /*!< source 12 as an external trigger source for ADC . */ 189 ADC_HwTrgSource_Alt13 = 13u, /*!< source 13 as an external trigger source for ADC . */ 190 ADC_HwTrgSource_Alt14 = 14u, /*!< source 14 as an external trigger source for ADC . */ 191 ADC_HwTrgSource_Alt15 = 15u, /*!< source 15 as an external trigger source for ADC . */ 192 ADC_HwTrgSource_Alt16 = 16u, /*!< source 16 as an external trigger source for ADC . */ 193 ADC_HwTrgSource_Alt17 = 17u, /*!< source 17 as an external trigger source for ADC . */ 194 ADC_HwTrgSource_Alt18 = 18u, /*!< source 18 as an external trigger source for ADC . */ 195 ADC_HwTrgSource_Alt19 = 19u, /*!< source 19 as an external trigger source for ADC . */ 196 ADC_HwTrgSource_Alt20 = 20u, /*!< source 20 as an external trigger source for ADC . */ 197 ADC_HwTrgSource_Alt21 = 21u, /*!< source 21 as an external trigger source for ADC . */ 198 ADC_HwTrgSource_Alt22 = 22u, /*!< source 22 as an external trigger source for ADC . */ 199 ADC_HwTrgSource_Alt23 = 23u, /*!< source 23 as an external trigger source for ADC . */ 200 ADC_HwTrgSource_Alt24 = 24u, /*!< source 24 as an external trigger source for ADC . */ 201 ADC_HwTrgSource_Alt25 = 25u, /*!< source 25 as an external trigger source for ADC . */ 202 ADC_HwTrgSource_Alt26 = 26u, /*!< source 26 as an external trigger source for ADC . */ 203 ADC_HwTrgSource_Alt27 = 27u, /*!< source 27 as an external trigger source for ADC . */ 204 ADC_HwTrgSource_Alt28 = 28u, /*!< source 28 as an external trigger source for ADC . */ 205 ADC_HwTrgSource_Alt29 = 29u, /*!< source 29 as an external trigger source for ADC . */ 206 ADC_HwTrgSource_Alt30 = 30u, /*!< source 30 as an external trigger source for ADC . */ 207 ADC_HwTrgSource_Alt31 = 31u, /*!< source 31 as an external trigger source for ADC . */ 208 } ADC_HwTrgSource_Type; 209 210 /*! 211 * @brief ADC SampleTime type. 212 * 213 * Select channel sample time. 214 */ 215 typedef enum 216 { 217 ADC_SampleTime_Alt0 = 0u, /*!< Channel sample time mode 0 . */ 218 ADC_SampleTime_Alt1 = 1u, /*!< Channel sample time mode 1 . */ 219 ADC_SampleTime_Alt2 = 2u, /*!< Channel sample time mode 2 . */ 220 ADC_SampleTime_Alt3 = 3u, /*!< Channel sample time mode 3 . */ 221 ADC_SampleTime_Alt4 = 4u, /*!< Channel sample time mode 4 . */ 222 ADC_SampleTime_Alt5 = 5u, /*!< Channel sample time mode 5 . */ 223 ADC_SampleTime_Alt6 = 6u, /*!< Channel sample time mode 6 . */ 224 ADC_SampleTime_Alt7 = 7u, /*!< Channel sample time mode 7 . */ 225 ADC_SampleTime_Alt8 = 8u, /*!< Channel sample time mode 8 . */ 226 ADC_SampleTime_Alt9 = 9u, /*!< Channel sample time mode 9 . */ 227 ADC_SampleTime_Alt10 = 10u, /*!< Channel sample time mode 10. */ 228 ADC_SampleTime_Alt11 = 11u, /*!< Channel sample time mode 11. */ 229 ADC_SampleTime_Alt12 = 12u, /*!< Channel sample time mode 12. */ 230 ADC_SampleTime_Alt13 = 13u, /*!< Channel sample time mode 13. */ 231 ADC_SampleTime_Alt14 = 14u, /*!< Channel sample time mode 14. */ 232 ADC_SampleTime_Alt15 = 15u, /*!< Channel sample time mode 15. */ 233 } ADC_SampleTime_Type; 234 235 /*! 236 * @brief This type of structure instance is used to keep the settings 237 * when calling the @ref ADC_Init() to initialize the ADC module. 238 */ 239 typedef struct 240 { 241 ADC_Resolution_Type Resolution; /*!< Specify the available bits for the conversion result data. */ 242 ADC_ClockDiv_Type ClockDiv; /*!< Specify the adc clock divison. */ 243 ADC_Align_Type Align; /*!< Specify the data alignment. */ 244 ADC_ConvMode_Type ConvMode; /*!< Specify the adc conversion mode. */ 245 } ADC_Init_Type; 246 247 /*! 248 * @brief This type of structure instance is used to keep the settings 249 * when calling the @ref ADC_EnableHwTrigger() to initialize the ADC hardware trigger module. 250 */ 251 typedef struct 252 { 253 ADC_HwTrgSource_Type Source; /*!< Select the ADC hardware trigger source. */ 254 ADC_HwTrgEdge_Type Edge; /*!< Select the trigger edge. */ 255 ADC_HwTrgDelayCycle_Type DelayCycle; /*!< Select the hardware trigger shift sample. */ 256 } ADC_HwTrgConf_Type; 257 258 /*! 259 * @brief This type of structure instance is used to keep the settings 260 * when calling the @ref ADC_EnableRegSeq() to initialize the ADC regular sequence module. 261 */ 262 typedef struct 263 { 264 uint32_t SeqSlots; /*!< Select the slots length. */ 265 ADC_RegSeqDirection_Type SeqDirection; /*!< Select the regular sequence sacn direction. */ 266 } ADC_RegSeqConf_Type; 267 268 /*! 269 * @brief This type of structure instance is used to keep the settings 270 * when calling the @ref ADC_EnableAnySeq() to initialize the ADC Any sequence mode. 271 */ 272 typedef struct 273 { 274 uint32_t SeqLen; /*!< Select the slots length. */ 275 uint32_t * SeqChannels; /*!< Select the channel, channel can be disorder. */ 276 } ADC_AnySeqConf_Type; 277 278 279 /*! 280 * @brief This type of structure instance is used to keep the settings 281 * when calling the @ref ADC_EnableHwCompare() to enable the ADC windows compare mode. 282 */ 283 typedef struct 284 { 285 uint32_t ChnNum; /*!< Select the channel number binding to the compare. */ 286 uint32_t HighLimit; /*!< The comparator high limit. */ 287 uint32_t LowLimit; /*!< The comparator low limit. */ 288 } ADC_HwCompConf_Type; 289 290 /*! 291 * @brief Initialize the ADC module. 292 * 293 * @param ADCx ADC instance. 294 * @param init Pointer to the initialization structure. See to @ref ADC_Init_Type. 295 * @return None. 296 */ 297 void ADC_Init(ADC_Type * ADCx, ADC_Init_Type * init); 298 299 /*! 300 * @brief Enable the ADC module. 301 * 302 * The ADC module should be enabled before conversion data. 303 * 304 * @param ADCx ADC instance. 305 * @param enable 'true' to enable the module, 'false' to disable the module. 306 * @return None. 307 */ 308 void ADC_Enable(ADC_Type * ADCx, bool enable); 309 310 /*! 311 * @brief Enable Temperature sensor from the ADC module. 312 * 313 * The module should be enabled when Using the built-in temperature sensor to 314 * detect temperature changes inside the device. 315 * 316 * @param ADCx ADC instance. 317 * @param enable 'true' to enable the module, 'false' to disable the module. 318 * @return None. 319 */ 320 void ADC_EnableTempSensor(ADC_Type * ADCx, bool enable); 321 322 /*! 323 * @brief Enable Voltage sensor from the ADC module. 324 * 325 * The module should be enabled when using internal reference voltage . 326 * 327 * @param ADCx ADC instance. 328 * @param enable 'true' to enable the module, 'false' to disable the module. 329 * @return None. 330 */ 331 void ADC_EnableVoltSensor(ADC_Type * ADCx, bool enable); 332 333 /*! 334 * @brief Enable the DMA from the ADC module. 335 * 336 * @param ADCx ADC instance. 337 * @param enable 'true' to enable the DMA, 'false' to disable the DMA. 338 * @return None. 339 */ 340 void ADC_EnableDMA(ADC_Type * ADCx, bool enable); 341 342 /*! 343 * @brief Enable interrupts of the ADC module. 344 * 345 * @param ADCx ADC instance. 346 * @param interrupts Interrupt code masks. 347 * @param enable 'true' to enable the indicated interrupts, 'false' to disable the indicated interrupts. 348 * @return None. 349 */ 350 void ADC_EnableInterrupts(ADC_Type * ADCx, uint32_t interrupts, bool enable); 351 352 /*! 353 * @brief Get the current status flags of the ADC module. 354 * 355 * @param ADCx ADC instance. 356 * @return Status flags. 357 */ 358 uint32_t ADC_GetStatus(ADC_Type * ADCx); 359 360 /*! 361 * @brief Clear the status flags of the ADC module. 362 * 363 * @param ADCx ADC instance. 364 * @param flags The mask codes of the indicated interrupt flags to be cleared. 365 * @return None. 366 */ 367 void ADC_ClearStatus(ADC_Type * ADCx, uint32_t flags); 368 369 /*! 370 * @brief Get the channel convert data from the ADC module. 371 * 372 * @param ADCx ADC instance. 373 * @param channel The convert channel. 374 * @param flags The ADC convert result flags. See to @ref ADC_RESULT_FLAGS. 375 * @return The data value. 376 */ 377 uint32_t ADC_GetConvResult(ADC_Type * ADCx, uint32_t * channel, uint32_t * flags); 378 379 /*! 380 * @brief Get the indication channel current data from the ADC module. 381 * 382 * @param ADCx ADC instance. 383 * @param channel the adc channel. 384 * @param flags The ADC convert result flags. See to @ref ADC_RESULT_FLAGS. 385 * @return The data value. 386 */ 387 uint32_t ADC_GetChnConvResult(ADC_Type * ADCx, uint32_t channel, uint32_t * flags); 388 389 390 /*! 391 * @brief Configuration channel sample time of the ADC module. 392 * 393 * @param ADCx ADC instance. 394 * @param channel the adc convert channel. 395 * @param sampletime the adc channel sample time 396 * @return None. 397 */ 398 void ADC_SetChnSampleTime(ADC_Type * ADCx, uint32_t channel, ADC_SampleTime_Type sampletime); 399 400 /*! 401 * @brief Enable regular sequence from the ADC module. 402 * 403 * The ADC module should be enabled before regular sequence function. 404 * it also have interrupt. 405 * 406 * @param ADCx ADC instance. 407 * @param conf Pointer to the initialization structure. See to @ref ADC_RegSeqConf_Type. 408 * @return None. 409 */ 410 void ADC_EnableRegSeq(ADC_Type * ADCx, ADC_RegSeqConf_Type * conf); 411 412 /*! 413 * @brief Enable hardware trigger from the ADC module. 414 * 415 * The ADC module should be enabled before using hardware trrigger function. 416 * it also have interrupt. 417 * 418 * @param ADCx ADC instance. 419 * @param conf Select the hardware trigger source. See to @ref ADC_HwTrgConf_Type. 420 * @return None. 421 */ 422 void ADC_EnableHwTrigger(ADC_Type * ADCx, ADC_HwTrgConf_Type * conf); 423 424 /*! 425 * @brief Enable Any sequence from the ADC module. 426 * 427 * The ADC module should be enabled before Any sequence function. 428 * it also have interrupt. 429 * 430 * @param ADCx ADC instance. 431 * @param conf Pointer to the initialization structure. See to @ref ADC_AnySeqConf_Type. 432 * @return None. 433 */ 434 void ADC_EnableAnySeq(ADC_Type * ADCx, ADC_AnySeqConf_Type * conf); 435 436 /*! 437 * @brief Enable window comparison from the ADC module. 438 * 439 * The ADC module should be enabled before using analog watchdog function. 440 * it also have interrupt. 441 * 442 * @param ADCx ADC instance. 443 * @param conf Pointer to the initialization structure. See to @ref ADC_HwCompareConf_Type. 444 * @return None. 445 */ 446 void ADC_EnableHwComp(ADC_Type * ADCx, ADC_HwCompConf_Type * conf); 447 448 /*! 449 * @brief Do soft trigger. 450 * 451 * @param ADCx ADC instance. 452 * @param enable 'true' to enable the ADC start convert, 'false' to disable. 453 * @return None. 454 */ 455 void ADC_DoSwTrigger(ADC_Type * ADCx, bool enable); 456 457 /*! 458 *@} 459 */ 460 461 #endif /*__HAL_ADC_H__. */ 462 463