1 /** 2 ********************************************************************************* 3 * 4 * @file ald_acmp.h 5 * @brief Header file of ACMP module driver. 6 * 7 * @version V1.0 8 * @date 13 Dec 2017 9 * @author AE Team 10 * @note 11 * 12 * Copyright (C) Shanghai Eastsoft Microelectronics Co. Ltd. All rights reserved. 13 * 14 * SPDX-License-Identifier: Apache-2.0 15 * 16 * Licensed under the Apache License, Version 2.0 (the License); you may 17 * not use this file except in compliance with the License. 18 * You may obtain a copy of the License at 19 * 20 * www.apache.org/licenses/LICENSE-2.0 21 * 22 * Unless required by applicable law or agreed to in writing, software 23 * distributed under the License is distributed on an AS IS BASIS, WITHOUT 24 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 25 * See the License for the specific language governing permissions and 26 * limitations under the License. 27 * 28 ********************************************************************************* 29 */ 30 31 #ifndef __ALD_ACMP_H__ 32 #define __ALD_ACMP_H__ 33 34 #ifdef __cplusplus 35 extern "C" { 36 #endif 37 38 #include "utils.h" 39 40 /** @addtogroup ES32FXXX_ALD 41 * @{ 42 */ 43 44 /** @addtogroup ACMP 45 * @{ 46 */ 47 48 /** @defgroup ACMP_Public_Types ACMP Public Types 49 * @{ 50 */ 51 52 /** 53 * @brief Acmp interrupt 54 */ 55 typedef enum { 56 ACMP_IT_EDGE = (1U << 0), /**< Edge interrupt bit */ 57 ACMP_IT_WARMUP = (1U << 1), /**< Warm up interrupt bit */ 58 } acmp_it_t; 59 60 /** 61 * @brief Acmp interrupt 62 */ 63 typedef enum { 64 ACMP_FLAG_EDGE = (1U << 0), /**< Edge interrupt flag */ 65 ACMP_FLAG_WARMUP = (1U << 1), /**< Warm up interrupt flag */ 66 } acmp_flag_t; 67 68 /** 69 * @brief Acmp interrupt flag 70 */ 71 typedef enum { 72 ACMP_STATUS_EDGE = (1U << 0), /**< Edge interrupt flag */ 73 ACMP_STATUS_WARMUP = (1U << 1), /**< Warm up interrupt flag */ 74 } acmp_status_t; 75 76 /** 77 * @brief Acmp positive input 78 */ 79 typedef enum { 80 ACMP_POS_CH0 = 0U, /**< Channel 0 as positive input */ 81 ACMP_POS_CH1 = 1U, /**< Channel 1 as positive input */ 82 ACMP_POS_CH2 = 2U, /**< Channel 2 as positive input */ 83 ACMP_POS_CH3 = 3U, /**< Channel 3 as positive input */ 84 ACMP_POS_CH4 = 4U, /**< Channel 4 as positive input */ 85 ACMP_POS_CH5 = 5U, /**< Channel 5 as positive input */ 86 ACMP_POS_CH6 = 6U, /**< Channel 6 as positive input */ 87 ACMP_POS_CH7 = 7U, /**< Channel 7 as positive input */ 88 } acmp_pos_input_t; 89 90 /** 91 * @brief Acmp negative input 92 */ 93 typedef enum { 94 ACMP_NEG_CH0 = 0U, /**< Channel 0 as negative input */ 95 ACMP_NEG_CH1 = 1U, /**< Channel 1 as negative input */ 96 ACMP_NEG_CH2 = 2U, /**< Channel 2 as negative input */ 97 ACMP_NEG_CH3 = 3U, /**< Channel 3 as negative input */ 98 ACMP_NEG_CH4 = 4U, /**< Channel 4 as negative input */ 99 ACMP_NEG_CH5 = 5U, /**< Channel 5 as negative input */ 100 ACMP_NEG_CH6 = 6U, /**< Channel 6 as negative input */ 101 ACMP_NEG_CH7 = 7U, /**< Channel 7 as negative input */ 102 ACMP_NEG_1V25 = 8U, /**< 1.25v as negative input */ 103 ACMP_NEG_2V5 = 9U, /**< 2.5v as negative input */ 104 ACMP_NEG_VDD = 10U, /**< VDD as negative input */ 105 } acmp_neg_input_t; 106 107 /** 108 * @brief Acmp mode 109 */ 110 typedef enum { 111 ACMP_ULTRA_LOW_POWER = 0U, /**< Ultra low power mode */ 112 ACMP_LOW_POWER = 1U, /**< Low power mode */ 113 ACMP_MIDDLE_POWER = 2U, /**< Middle power mode */ 114 ACMP_HIGH_POWER = 3U, /**< High power mode */ 115 } acmp_mode_t; 116 117 /** 118 * @brief Acmp warm-up time 119 */ 120 typedef enum { 121 ACMP_4_PCLK = 0U, /**< 4 hfperclk cycles */ 122 ACMP_8_PCLK = 1U, /**< 4 hfperclk cycles */ 123 ACMP_16_PCLK = 2U, /**< 4 hfperclk cycles */ 124 ACMP_32_PCLK = 3U, /**< 4 hfperclk cycles */ 125 ACMP_64_PCLK = 4U, /**< 4 hfperclk cycles */ 126 ACMP_128_PCLK = 5U, /**< 4 hfperclk cycles */ 127 ACMP_256_PCLK = 6U, /**< 4 hfperclk cycles */ 128 ACMP_512_PCLK = 7U, /**< 4 hfperclk cycles */ 129 } acmp_warm_time_t; 130 131 /** 132 * @brief Acmp hysteresis level 133 */ 134 typedef enum { 135 ACMP_HYST_0 = 0U, /**< No hysteresis */ 136 ACMP_HYST_15 = 1U, /**< 15mV hysteresis */ 137 ACMP_HYST_22 = 2U, /**< 22mV hysteresis */ 138 ACMP_HYST_29 = 3U, /**< 29mV hysteresis */ 139 ACMP_HYST_36 = 4U, /**< 36mV hysteresis */ 140 ACMP_HYST_43 = 5U, /**< 43mV hysteresis */ 141 ACMP_HYST_50 = 6U, /**< 50mV hysteresis */ 142 ACMP_HYST_57 = 7U, /**< 57mV hysteresis */ 143 } acmp_hystsel_t; 144 145 /** 146 * @brief Acmp inactive state 147 */ 148 typedef enum { 149 ACMP_INACTVAL_LOW = 0U, /**< The inactive value is 0 */ 150 ACMP_INACTVAL_HIGH = 1U, /**< The inactive value is 1 */ 151 } acmp_inactval_t; 152 153 /** 154 * @brief which edges set up interrupt 155 */ 156 typedef enum { 157 ACMP_EDGE_NONE = 0U, /**< Disable EDGE interrupt */ 158 ACMP_EDGE_FALL = 1U, /**< Falling edges set EDGE interrupt */ 159 ACMP_EDGE_RISE = 2U, /**< rise edges set EDGE interrupt */ 160 ACMP_EDGE_ALL = 3U, /**< Falling edges and rise edges set EDGE interrupt */ 161 } acmp_edge_t; 162 163 /** 164 * @brief Acmp output function 165 */ 166 typedef enum { 167 ACMP_OUT_DISABLE = 0U, /**< Disable acmp output */ 168 ACMP_OUT_ENABLE = 1U, /**< Enable acmp output */ 169 } acmp_out_func_t; 170 171 /** 172 * @brief Acmp warm-up interrupt function 173 */ 174 typedef enum { 175 ACMP_WARM_DISABLE = 0U, /**< Disable acmp warm-up interrupt */ 176 ACMP_WARM_ENABLE = 1U, /**< Enable acmp warm-up interrupt */ 177 } acmp_warm_it_func; 178 179 /** 180 * @brief Acmp gpio output invert 181 */ 182 typedef enum { 183 ACMP_GPIO_NO_INV = 0U, /**< Acmp output to gpio is not inverted */ 184 ACMP_GPIO_INV = 1U, /**< Acmp output to gpio is inverted */ 185 } acmp_invert_t; 186 187 /** 188 * @brief Acmp output config structure definition 189 */ 190 typedef struct { 191 acmp_out_func_t out_func; /**< Acmp output function */ 192 acmp_invert_t gpio_inv; /**< If invert gpio output */ 193 } acmp_output_config_t; 194 195 /** 196 * @brief Acmp init structure definition 197 */ 198 typedef struct { 199 acmp_mode_t mode; /**< Acmp operation mode */ 200 acmp_warm_time_t warm_time; /**< Acmp warm up time */ 201 acmp_hystsel_t hystsel; /**< Acmp hysteresis level */ 202 acmp_warm_it_func warm_func; /**< Acmp warm-up interrupt enable/disable */ 203 acmp_pos_input_t pos_port; /**< Acmp positive port select */ 204 acmp_neg_input_t neg_port; /**< Acmp negative port select */ 205 acmp_inactval_t inactval; /**< Acmp inavtive output value */ 206 acmp_edge_t edge; /** Select edges to set interrupt flag */ 207 uint8_t vdd_level; /** Select scaling factor for CDD reference level, MAX is 63 */ 208 } acmp_init_t; 209 210 /** 211 * @brief ACMP Handle Structure definition 212 */ 213 typedef struct acmp_handle_s { 214 ACMP_TypeDef *perh; /**< Register base address */ 215 acmp_init_t init; /**< ACMP required parameters */ 216 lock_state_t lock; /**< Locking object */ 217 218 void (*acmp_warmup_cplt_cbk)(struct acmp_handle_s *arg); /**< Acmp warm-up complete callback */ 219 void (*acmp_edge_cplt_cbk)(struct acmp_handle_s *arg); /**< Acmp edge trigger callback */ 220 } acmp_handle_t; 221 /** 222 * @} 223 */ 224 225 /** @defgroup ACMP_Public_Macros ACMP Public Macros 226 * @{ 227 */ 228 #define ACMP_ENABLE(handle) (SET_BIT((handle)->perh->CON, ACMP_CON_EN_MSK)) 229 #define ACMP_DISABLE(handle) (CLEAR_BIT((handle)->perh->CON, ACMP_CON_EN_MSK)) 230 /** 231 * @} 232 */ 233 234 /** @defgroup ACMP_Private_Macros ACMP Private Macros 235 * @{ 236 */ 237 #define IS_ACMP_TYPE(x) (((x) == ACMP0) || \ 238 ((x) == ACMP1)) 239 #define IS_ACMP_MODE_TYPE(x) (((x) == ACMP_ULTRA_LOW_POWER) || \ 240 ((x) == ACMP_LOW_POWER) || \ 241 ((x) == ACMP_MIDDLE_POWER) || \ 242 ((x) == ACMP_HIGH_POWER)) 243 #define IS_ACMP_IT_TYPE(x) (((x) == ACMP_IT_EDGE) || \ 244 ((x) == ACMP_IT_WARMUP)) 245 #define IS_ACMP_FLAG_TYPE(x) (((x) == ACMP_FLAG_EDGE) || \ 246 ((x) == ACMP_FLAG_WARMUP)) 247 #define IS_ACMP_STATUS_TYPE(x) (((x) == ACMP_STATUS_EDGE) || \ 248 ((x) == ACMP_STATUS_WARMUP)) 249 #define IS_ACMP_POS_INPUT_TYPE(x) (((x) == ACMP_POS_CH0) || \ 250 ((x) == ACMP_POS_CH1) || \ 251 ((x) == ACMP_POS_CH2) || \ 252 ((x) == ACMP_POS_CH3) || \ 253 ((x) == ACMP_POS_CH4) || \ 254 ((x) == ACMP_POS_CH5) || \ 255 ((x) == ACMP_POS_CH6) || \ 256 ((x) == ACMP_POS_CH7)) 257 #define IS_ACMP_NEG_INPUT_TYPE(x) (((x) == ACMP_NEG_CH0) || \ 258 ((x) == ACMP_NEG_CH1) || \ 259 ((x) == ACMP_NEG_CH2) || \ 260 ((x) == ACMP_NEG_CH3) || \ 261 ((x) == ACMP_NEG_CH4) || \ 262 ((x) == ACMP_NEG_CH5) || \ 263 ((x) == ACMP_NEG_CH6) || \ 264 ((x) == ACMP_NEG_CH7) || \ 265 ((x) == ACMP_NEG_1V25) || \ 266 ((x) == ACMP_NEG_2V5) || \ 267 ((x) == ACMP_NEG_VDD)) 268 #define IS_ACMP_WARM_UP_TIME_TYPE(x) (((x) == ACMP_4_PCLK) || \ 269 ((x) == ACMP_8_PCLK) || \ 270 ((x) == ACMP_16_PCLK) || \ 271 ((x) == ACMP_32_PCLK) || \ 272 ((x) == ACMP_64_PCLK) || \ 273 ((x) == ACMP_128_PCLK) || \ 274 ((x) == ACMP_256_PCLK) || \ 275 ((x) == ACMP_512_PCLK)) 276 #define IS_ACMP_HYSTSEL_TYPE(x) (((x) == ACMP_HYST_0) || \ 277 ((x) == ACMP_HYST_15) || \ 278 ((x) == ACMP_HYST_22) || \ 279 ((x) == ACMP_HYST_29) || \ 280 ((x) == ACMP_HYST_36) || \ 281 ((x) == ACMP_HYST_43) || \ 282 ((x) == ACMP_HYST_50) || \ 283 ((x) == ACMP_HYST_57)) 284 #define IS_ACMP_INACTVAL_TYPE(x) (((x) == ACMP_INACTVAL_LOW) || \ 285 ((x) == ACMP_INACTVAL_HIGH)) 286 #define IS_ACMP_EDGE_TYPE(x) (((x) == ACMP_EDGE_NONE) || \ 287 ((x) == ACMP_EDGE_FALL) || \ 288 ((x) == ACMP_EDGE_RISE) || \ 289 ((x) == ACMP_EDGE_ALL)) 290 #define IS_ACMP_OUT_FUNC_TYPE(x) (((x) == ACMP_OUT_DISABLE) || \ 291 ((x) == ACMP_OUT_ENABLE)) 292 #define IS_ACMP_INVERT_TYPE(x) (((x) == ACMP_GPIO_NO_INV) || \ 293 ((x) == ACMP_GPIO_INV)) 294 #define IS_ACMP_WARM_FUNC_TYPE(x) (((x) == ACMP_WARM_DISABLE) || \ 295 ((x) == ACMP_WARM_ENABLE)) 296 /** 297 * @} 298 */ 299 300 /** @addtogroup ACMP_Public_Functions 301 * @{ 302 */ 303 304 /** @addtogroup ACMP_Public_Functions_Group1 305 * @{ 306 */ 307 ald_status_t ald_acmp_init(acmp_handle_t *hperh); 308 309 /** 310 * @} 311 */ 312 313 /** @addtogroup ACMP_Public_Functions_Group2 314 * @{ 315 */ 316 ald_status_t ald_acmp_interrupt_config(acmp_handle_t *hperh, acmp_it_t it, type_func_t state); 317 ald_status_t ald_acmp_set_interrupt_mask(acmp_handle_t *hperh, acmp_it_t it); 318 it_status_t ald_acmp_get_it_status(acmp_handle_t *hperh, acmp_it_t it); 319 it_status_t ald_acmp_get_flag_status(acmp_handle_t *hperh, acmp_flag_t it); 320 ald_status_t ald_acmp_clear_flag_status(acmp_handle_t *hperh, acmp_flag_t it); 321 flag_status_t ald_acmp_get_status(acmp_handle_t *hperh, acmp_status_t flag); 322 323 /** 324 * @} 325 */ 326 327 /** @addtogroup ACMP_Public_Functions_Group3 328 * @{ 329 */ 330 void ald_acmp_irq_handler(acmp_handle_t *hperh); 331 ald_status_t ald_acmp_out_config(acmp_handle_t *hperh, acmp_output_config_t *config); 332 uint8_t ald_acmp_out_result(acmp_handle_t *hperh); 333 /** 334 * @} 335 */ 336 337 /** 338 * @} 339 */ 340 341 /** 342 * @} 343 */ 344 345 /** 346 * @} 347 */ 348 #ifdef __cplusplus 349 extern "C" } 350 #endif 351 352 #endif 353