HPM SDK
HPMicro Software Development Kit
hpm_acmp_drv.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2021-2025 HPMicro
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 
8 #ifndef HPM_ACMP_DRV_H
9 #define HPM_ACMP_DRV_H
10 
11 #include "hpm_common.h"
12 #include "hpm_acmp_regs.h"
13 #include "hpm_soc_ip_feature.h"
14 
23 /***********************************************************************************************************************
24  *
25  * Definitions
26  *
27  **********************************************************************************************************************/
28 
32 #define ACMP_HYST_LEVEL_0 (0U)
33 #define ACMP_HYST_LEVEL_1 (1U)
34 #define ACMP_HYST_LEVEL_2 (2U)
35 #define ACMP_HYST_LEVEL_3 (3U)
36 
40 #define ACMP_INPUT_DAC_OUT (0U)
41 #define ACMP_INPUT_ANALOG_1 (1U)
42 #define ACMP_INPUT_ANALOG_2 (2U)
43 #define ACMP_INPUT_ANALOG_3 (3U)
44 #define ACMP_INPUT_ANALOG_4 (4U)
45 #define ACMP_INPUT_ANALOG_5 (5U)
46 #define ACMP_INPUT_ANALOG_6 (6U)
47 #define ACMP_INPUT_ANALOG_7 (7U)
48 
52 #define ACMP_FILTER_MODE_BYPASS (0U)
53 #define ACMP_FILTER_MODE_CHANGE_IMMEDIATELY (4U)
54 #define ACMP_FILTER_MODE_CHANGE_AFTER_FILTER (5U)
55 #define ACMP_FILTER_MODE_STABLE_LOW (6U)
56 #define ACMP_FILTER_MODE_STABLE_HIGH (7U)
57 
61 #define ACMP_EVENT_RISING_EDGE (1U)
62 #define ACMP_EVENT_FALLING_EDGE (2U)
63 
67 #define ACMP_CAP_SEL_LEVEL_0 (0U)
68 #define ACMP_CAP_SEL_LEVEL_1 (1U)
69 #define ACMP_CAP_SEL_LEVEL_2 (2U)
70 #define ACMP_CAP_SEL_LEVEL_3 (3U)
71 
76 typedef struct acmp_channel_config {
77  uint8_t plus_input;
78  uint8_t minus_input;
79  uint8_t filter_mode;
80  uint8_t hyst_level;
85  bool enable_dac;
87  uint16_t filter_length; /* ACMP output digital filter length in ACMP clock cycle */
88 #if defined(HPM_IP_FEATURE_ACMP_FILT_LEN_EXTEND) && HPM_IP_FEATURE_ACMP_FILT_LEN_EXTEND
89  uint8_t filter_length_shift;
90 #endif
92 
93 #ifdef __cplusplus
94 extern "C" {
95 #endif
96 
104 static inline void acmp_channel_config_dac(ACMP_Type *ptr, uint8_t ch, uint32_t value)
105 {
107 }
108 
118 static inline void acmp_channel_clear_status(ACMP_Type *ptr, uint8_t ch, uint32_t mask)
119 {
120  ptr->CHANNEL[ch].SR = mask;
121 }
122 
130 static inline uint32_t acmp_channel_get_status(ACMP_Type *ptr, uint8_t ch)
131 {
132  return ptr->CHANNEL[ch].SR;
133 }
134 
147 static inline void acmp_channel_dma_request_enable(ACMP_Type *ptr, uint8_t ch,
148  uint32_t mask, bool enable)
149 {
150  ptr->CHANNEL[ch].DMAEN = (ptr->CHANNEL[ch].DMAEN & ~mask)
151  | (enable ? mask : 0);
152 }
153 
166 static inline void acmp_channel_enable_irq(ACMP_Type *ptr, uint8_t ch,
167  uint32_t mask, bool enable)
168 {
169  ptr->CHANNEL[ch].IRQEN = (ptr->CHANNEL[ch].IRQEN & ~mask)
170  | (enable ? mask : 0);
171 }
172 
182 static inline void acmp_channel_enable_dac(ACMP_Type *ptr, uint8_t ch, bool enable)
183 {
184  ptr->CHANNEL[ch].CFG = (ptr->CHANNEL[ch].CFG & ~ACMP_CHANNEL_CFG_DACEN_MASK)
185  | ACMP_CHANNEL_CFG_DACEN_SET(enable);
186 }
187 
197 static inline void acmp_channel_enable_hpmode(ACMP_Type *ptr, uint8_t ch, bool enable)
198 {
199  ptr->CHANNEL[ch].CFG = (ptr->CHANNEL[ch].CFG & ~ACMP_CHANNEL_CFG_HPMODE_MASK)
200  | ACMP_CHANNEL_CFG_HPMODE_SET(enable);
201 }
202 
210 static inline void acmp_channel_set_hyst(ACMP_Type *ptr, uint8_t ch, uint8_t level)
211 {
212  ptr->CHANNEL[ch].CFG = (ptr->CHANNEL[ch].CFG & ~ACMP_CHANNEL_CFG_HYST_MASK)
213  | ACMP_CHANNEL_CFG_HYST_SET(level);
214 }
215 
225 static inline void acmp_channel_enable_cmp(ACMP_Type *ptr, uint8_t ch, bool enable)
226 {
227  ptr->CHANNEL[ch].CFG = (ptr->CHANNEL[ch].CFG & ~ACMP_CHANNEL_CFG_CMPEN_MASK)
228  | ACMP_CHANNEL_CFG_CMPEN_SET(enable);
229 }
230 
240 static inline void acmp_channel_enable_cmp_output(ACMP_Type *ptr, uint8_t ch, bool enable)
241 {
242  ptr->CHANNEL[ch].CFG = (ptr->CHANNEL[ch].CFG & ~ACMP_CHANNEL_CFG_CMPOEN_MASK)
243  | ACMP_CHANNEL_CFG_CMPOEN_SET(enable);
244 }
245 
255 static inline void acmp_channel_cmp_output_bypass_filter(ACMP_Type *ptr, uint8_t ch, bool enable)
256 {
257  ptr->CHANNEL[ch].CFG = (ptr->CHANNEL[ch].CFG & ~ACMP_CHANNEL_CFG_FLTBYPS_MASK)
258  | ACMP_CHANNEL_CFG_FLTBYPS_SET(!enable);
259 }
260 
270 static inline void acmp_channel_enable_cmp_window_mode(ACMP_Type *ptr, uint8_t ch, bool enable)
271 {
272  ptr->CHANNEL[ch].CFG = (ptr->CHANNEL[ch].CFG & ~ACMP_CHANNEL_CFG_WINEN_MASK)
273  | ACMP_CHANNEL_CFG_WINEN_SET(enable);
274 }
275 
285 static inline void acmp_channel_invert_output(ACMP_Type *ptr, uint8_t ch, bool enable)
286 {
287  ptr->CHANNEL[ch].CFG = (ptr->CHANNEL[ch].CFG & ~ACMP_CHANNEL_CFG_OPOL_MASK)
288  | ACMP_CHANNEL_CFG_OPOL_SET(enable);
289 }
290 
298 static inline void acmp_channel_set_filter_mode(ACMP_Type *ptr, uint8_t ch, uint8_t filter)
299 {
300  ptr->CHANNEL[ch].CFG = (ptr->CHANNEL[ch].CFG & ~ACMP_CHANNEL_CFG_FLTMODE_MASK)
302 }
303 
311 static inline void acmp_channel_set_filter_length(ACMP_Type *ptr, uint8_t ch, uint16_t filter_length)
312 {
313  ptr->CHANNEL[ch].CFG = (ptr->CHANNEL[ch].CFG & ~ACMP_CHANNEL_CFG_FLTLEN_MASK)
314  | ACMP_CHANNEL_CFG_FLTLEN_SET(filter_length);
315 }
316 
324 #if defined(HPM_IP_FEATURE_ACMP_FILT_LEN_EXTEND) && HPM_IP_FEATURE_ACMP_FILT_LEN_EXTEND
325 static inline void acmp_channel_set_filter_length_extend(ACMP_Type *ptr, uint8_t ch, uint16_t filter_length_shift)
326 {
327  ptr->CHANNEL[ch].CFG = (ptr->CHANNEL[ch].CFG & ~ACMP_CHANNEL_CFG_FLTLEN_SHIFT_MASK)
328  | ACMP_CHANNEL_CFG_FLTLEN_SHIFT_SET(filter_length_shift);
329 }
330 #endif
331 
344 hpm_stat_t acmp_channel_config(ACMP_Type *ptr, uint8_t ch, acmp_channel_config_t *config, bool enable);
345 
353 
359 #ifdef __cplusplus
360 }
361 #endif
362 
363 #endif /* HPM_ACMP_DRV_H */
#define ACMP_CHANNEL_CFG_FLTLEN_SHIFT_SET(x)
Definition: hpm_acmp_regs.h:182
#define ACMP_CHANNEL_CFG_CMPEN_MASK
Definition: hpm_acmp_regs.h:71
#define ACMP_CHANNEL_CFG_FLTLEN_SET(x)
Definition: hpm_acmp_regs.h:192
#define ACMP_CHANNEL_CFG_FLTBYPS_MASK
Definition: hpm_acmp_regs.h:126
#define ACMP_CHANNEL_CFG_FLTMODE_MASK
Definition: hpm_acmp_regs.h:165
#define ACMP_CHANNEL_CFG_FLTMODE_SET(x)
Definition: hpm_acmp_regs.h:167
#define ACMP_CHANNEL_CFG_CMPOEN_MASK
Definition: hpm_acmp_regs.h:114
#define ACMP_CHANNEL_CFG_WINEN_SET(x)
Definition: hpm_acmp_regs.h:140
#define ACMP_CHANNEL_DACCFG_DACCFG_SET(x)
Definition: hpm_acmp_regs.h:203
#define ACMP_CHANNEL_CFG_DACEN_MASK
Definition: hpm_acmp_regs.h:47
#define ACMP_CHANNEL_CFG_HPMODE_SET(x)
Definition: hpm_acmp_regs.h:61
#define ACMP_CHANNEL_CFG_CMPEN_SET(x)
Definition: hpm_acmp_regs.h:73
#define ACMP_CHANNEL_CFG_OPOL_MASK
Definition: hpm_acmp_regs.h:150
#define ACMP_CHANNEL_CFG_DACEN_SET(x)
Definition: hpm_acmp_regs.h:49
#define ACMP_CHANNEL_CFG_HYST_MASK
Definition: hpm_acmp_regs.h:35
#define ACMP_CHANNEL_CFG_HPMODE_MASK
Definition: hpm_acmp_regs.h:59
#define ACMP_CHANNEL_CFG_OPOL_SET(x)
Definition: hpm_acmp_regs.h:152
#define ACMP_CHANNEL_CFG_FLTBYPS_SET(x)
Definition: hpm_acmp_regs.h:128
#define ACMP_CHANNEL_CFG_CMPOEN_SET(x)
Definition: hpm_acmp_regs.h:116
#define ACMP_CHANNEL_CFG_WINEN_MASK
Definition: hpm_acmp_regs.h:138
#define ACMP_CHANNEL_CFG_FLTLEN_MASK
Definition: hpm_acmp_regs.h:190
#define ACMP_CHANNEL_DACCFG_DACCFG_MASK
Definition: hpm_acmp_regs.h:201
#define ACMP_CHANNEL_CFG_FLTLEN_SHIFT_MASK
Definition: hpm_acmp_regs.h:180
#define ACMP_CHANNEL_CFG_HYST_SET(x)
Definition: hpm_acmp_regs.h:37
static uint32_t acmp_channel_get_status(ACMP_Type *ptr, uint8_t ch)
ACMP channel get status.
Definition: hpm_acmp_drv.h:130
static void acmp_channel_enable_cmp_window_mode(ACMP_Type *ptr, uint8_t ch, bool enable)
ACMP channel enable comparator window mode.
Definition: hpm_acmp_drv.h:270
struct acmp_channel_config acmp_channel_config_t
ACMP channel config.
static void acmp_channel_clear_status(ACMP_Type *ptr, uint8_t ch, uint32_t mask)
ACMP channel clear status.
Definition: hpm_acmp_drv.h:118
static void acmp_channel_set_hyst(ACMP_Type *ptr, uint8_t ch, uint8_t level)
ACMP channel enable hysteresis level.
Definition: hpm_acmp_drv.h:210
static void acmp_channel_set_filter_mode(ACMP_Type *ptr, uint8_t ch, uint8_t filter)
ACMP channel set comparator output filter mode.
Definition: hpm_acmp_drv.h:298
static void acmp_channel_enable_cmp(ACMP_Type *ptr, uint8_t ch, bool enable)
ACMP channel enable comparator.
Definition: hpm_acmp_drv.h:225
static void acmp_channel_config_dac(ACMP_Type *ptr, uint8_t ch, uint32_t value)
ACMP channel config DAC output value.
Definition: hpm_acmp_drv.h:104
static void acmp_channel_enable_irq(ACMP_Type *ptr, uint8_t ch, uint32_t mask, bool enable)
ACMP channel enable IRQ.
Definition: hpm_acmp_drv.h:166
static void acmp_channel_enable_hpmode(ACMP_Type *ptr, uint8_t ch, bool enable)
ACMP channel enable high performance mode.
Definition: hpm_acmp_drv.h:197
static void acmp_channel_dma_request_enable(ACMP_Type *ptr, uint8_t ch, uint32_t mask, bool enable)
ACMP channel enable DMA request.
Definition: hpm_acmp_drv.h:147
static void acmp_channel_cmp_output_bypass_filter(ACMP_Type *ptr, uint8_t ch, bool enable)
ACMP channel bypass comparator output filter.
Definition: hpm_acmp_drv.h:255
hpm_stat_t acmp_channel_config(ACMP_Type *ptr, uint8_t ch, acmp_channel_config_t *config, bool enable)
ACMP channel set comparator output filter extended length.
Definition: hpm_acmp_drv.c:10
static void acmp_channel_set_filter_length(ACMP_Type *ptr, uint8_t ch, uint16_t filter_length)
ACMP channel set comparator output filter length.
Definition: hpm_acmp_drv.h:311
static void acmp_channel_enable_cmp_output(ACMP_Type *ptr, uint8_t ch, bool enable)
ACMP channel enable comparator output.
Definition: hpm_acmp_drv.h:240
static void acmp_channel_enable_dac(ACMP_Type *ptr, uint8_t ch, bool enable)
ACMP channel enable DAC.
Definition: hpm_acmp_drv.h:182
static void acmp_channel_invert_output(ACMP_Type *ptr, uint8_t ch, bool enable)
ACMP channel invert comparator output.
Definition: hpm_acmp_drv.h:285
void acmp_channel_get_default_config(ACMP_Type *ptr, acmp_channel_config_t *config)
ADC channel get default config setting.
Definition: hpm_acmp_drv.c:34
uint32_t hpm_stat_t
Definition: hpm_common.h:126
Definition: hpm_acmp_regs.h:12
__RW uint32_t DACCFG
Definition: hpm_acmp_regs.h:15
__RW uint32_t IRQEN
Definition: hpm_acmp_regs.h:18
__RW uint32_t SR
Definition: hpm_acmp_regs.h:17
__RW uint32_t CFG
Definition: hpm_acmp_regs.h:14
struct ACMP_Type::@289 CHANNEL[2]
__RW uint32_t DMAEN
Definition: hpm_acmp_regs.h:19
ACMP channel config.
Definition: hpm_acmp_drv.h:76
bool invert_output
Definition: hpm_acmp_drv.h:83
uint8_t minus_input
Definition: hpm_acmp_drv.h:78
bool enable_dac
Definition: hpm_acmp_drv.h:85
uint8_t filter_mode
Definition: hpm_acmp_drv.h:79
uint8_t hyst_level
Definition: hpm_acmp_drv.h:80
uint16_t filter_length
Definition: hpm_acmp_drv.h:87
bool enable_hpmode
Definition: hpm_acmp_drv.h:86
bool enable_cmp_output
Definition: hpm_acmp_drv.h:81
uint8_t plus_input
Definition: hpm_acmp_drv.h:77
bool enable_window_mode
Definition: hpm_acmp_drv.h:82
bool bypass_filter
Definition: hpm_acmp_drv.h:84