HPM SDK
HPMicro Software Development Kit
hpm_acmp_drv.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2021 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 
22 /***********************************************************************************************************************
23  *
24  * Definitions
25  *
26  **********************************************************************************************************************/
27 
31 #define ACMP_HYST_LEVEL_0 (0U)
32 #define ACMP_HYST_LEVEL_1 (1U)
33 #define ACMP_HYST_LEVEL_2 (2U)
34 #define ACMP_HYST_LEVEL_3 (3U)
35 
39 #define ACMP_INPUT_DAC_OUT (0U)
40 #define ACMP_INPUT_ANALOG_1 (1U)
41 #define ACMP_INPUT_ANALOG_2 (2U)
42 #define ACMP_INPUT_ANALOG_3 (3U)
43 #define ACMP_INPUT_ANALOG_4 (4U)
44 #define ACMP_INPUT_ANALOG_5 (5U)
45 #define ACMP_INPUT_ANALOG_6 (6U)
46 #define ACMP_INPUT_ANALOG_7 (7U)
47 
51 #define ACMP_FILTER_MODE_BYPASS (0U)
52 #define ACMP_FILTER_MODE_CHANGE_IMMEDIATELY (4U)
53 #define ACMP_FILTER_MODE_CHANGE_AFTER_FILTER (5U)
54 #define ACMP_FILTER_MODE_STABLE_LOW (6U)
55 #define ACMP_FILTER_MODE_STABLE_HIGH (7U)
56 
60 #define ACMP_EVENT_RISING_EDGE (1U)
61 #define ACMP_EVENT_FALLING_EDGE (2U)
62 
67 typedef struct acmp_channel_config {
68  uint8_t plus_input;
69  uint8_t minus_input;
70  uint8_t filter_mode;
71  uint8_t hyst_level;
77  bool enable_dac;
79  uint16_t filter_length; /* ACMP output digital filter length in ACMP clock cycle */
81 
82 #ifdef __cplusplus
83 extern "C" {
84 #endif
85 
93 static inline void acmp_channel_config_dac(ACMP_Type *ptr, uint8_t ch, uint32_t value)
94 {
96 }
97 
107 static inline void acmp_channel_clear_status(ACMP_Type *ptr, uint8_t ch, uint32_t mask)
108 {
109  ptr->CHANNEL[ch].SR = mask;
110 }
111 
119 static inline uint32_t acmp_channel_get_status(ACMP_Type *ptr, uint8_t ch)
120 {
121  return ptr->CHANNEL[ch].SR;
122 }
123 
136 static inline void acmp_channel_dma_request_enable(ACMP_Type *ptr, uint8_t ch,
137  uint32_t mask, bool enable)
138 {
139  ptr->CHANNEL[ch].DMAEN = (ptr->CHANNEL[ch].DMAEN & ~mask)
140  | (enable ? mask : 0);
141 }
142 
155 static inline void acmp_channel_enable_irq(ACMP_Type *ptr, uint8_t ch,
156  uint32_t mask, bool enable)
157 {
158  ptr->CHANNEL[ch].IRQEN = (ptr->CHANNEL[ch].IRQEN & ~mask)
159  | (enable ? mask : 0);
160 }
161 
171 static inline void acmp_channel_enable_dac(ACMP_Type *ptr, uint8_t ch, bool enable)
172 {
173  ptr->CHANNEL[ch].CFG = (ptr->CHANNEL[ch].CFG & ~ACMP_CHANNEL_CFG_DACEN_MASK)
174  | ACMP_CHANNEL_CFG_DACEN_SET(enable);
175 }
176 
186 static inline void acmp_channel_enable_hpmode(ACMP_Type *ptr, uint8_t ch, bool enable)
187 {
188  ptr->CHANNEL[ch].CFG = (ptr->CHANNEL[ch].CFG & ~ACMP_CHANNEL_CFG_HPMODE_MASK)
189  | ACMP_CHANNEL_CFG_HPMODE_SET(enable);
190 }
191 
199 static inline void acmp_channel_set_hyst(ACMP_Type *ptr, uint8_t ch, uint8_t level)
200 {
201  ptr->CHANNEL[ch].CFG = (ptr->CHANNEL[ch].CFG & ~ACMP_CHANNEL_CFG_HYST_MASK)
202  | ACMP_CHANNEL_CFG_HYST_SET(level);
203 }
204 
214 static inline void acmp_channel_enable_cmp(ACMP_Type *ptr, uint8_t ch, bool enable)
215 {
216  ptr->CHANNEL[ch].CFG = (ptr->CHANNEL[ch].CFG & ~ACMP_CHANNEL_CFG_CMPEN_MASK)
217  | ACMP_CHANNEL_CFG_CMPEN_SET(enable);
218 }
219 
229 static inline void acmp_channel_enable_cmp_output(ACMP_Type *ptr, uint8_t ch, bool enable)
230 {
231  ptr->CHANNEL[ch].CFG = (ptr->CHANNEL[ch].CFG & ~ACMP_CHANNEL_CFG_CMPOEN_MASK)
232  | ACMP_CHANNEL_CFG_CMPOEN_SET(enable);
233 }
234 
244 static inline void acmp_channel_cmp_output_bypass_filter(ACMP_Type *ptr, uint8_t ch, bool enable)
245 {
246  ptr->CHANNEL[ch].CFG = (ptr->CHANNEL[ch].CFG & ~ACMP_CHANNEL_CFG_FLTBYPS_MASK)
247  | ACMP_CHANNEL_CFG_FLTBYPS_SET(!enable);
248 }
249 
259 static inline void acmp_channel_enable_cmp_window_mode(ACMP_Type *ptr, uint8_t ch, bool enable)
260 {
261  ptr->CHANNEL[ch].CFG = (ptr->CHANNEL[ch].CFG & ~ACMP_CHANNEL_CFG_WINEN_MASK)
262  | ACMP_CHANNEL_CFG_WINEN_SET(enable);
263 }
264 
274 static inline void acmp_channel_invert_output(ACMP_Type *ptr, uint8_t ch, bool enable)
275 {
276  ptr->CHANNEL[ch].CFG = (ptr->CHANNEL[ch].CFG & ~ACMP_CHANNEL_CFG_OPOL_MASK)
277  | ACMP_CHANNEL_CFG_OPOL_SET(enable);
278 }
279 
287 static inline void acmp_channel_set_filter_mode(ACMP_Type *ptr, uint8_t ch, uint8_t filter)
288 {
289  ptr->CHANNEL[ch].CFG = (ptr->CHANNEL[ch].CFG & ~ACMP_CHANNEL_CFG_FLTMODE_MASK)
291 }
292 
302 static inline void acmp_channel_enable_sync(ACMP_Type *ptr, uint8_t ch, bool enable)
303 {
304  ptr->CHANNEL[ch].CFG = (ptr->CHANNEL[ch].CFG & ~ACMP_CHANNEL_CFG_SYNCEN_MASK)
305  | ACMP_CHANNEL_CFG_SYNCEN_SET(enable);
306 }
307 
315 static inline void acmp_channel_set_filter_length(ACMP_Type *ptr, uint8_t ch, uint16_t filter_length)
316 {
317  ptr->CHANNEL[ch].CFG = (ptr->CHANNEL[ch].CFG & ~ACMP_CHANNEL_CFG_FLTLEN_MASK)
318  | ACMP_CHANNEL_CFG_FLTLEN_SET(filter_length);
319 }
320 
333 hpm_stat_t acmp_channel_config(ACMP_Type *ptr, uint8_t ch, acmp_channel_config_t *config, bool enable);
334 
342 
348 #ifdef __cplusplus
349 }
350 #endif
351 
352 #endif /* HPM_ACMP_DRV_H */
static uint32_t acmp_channel_get_status(ACMP_Type *ptr, uint8_t ch)
ACMP channel get status.
Definition: hpm_acmp_drv.h:119
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:259
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:107
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:199
static void acmp_channel_enable_sync(ACMP_Type *ptr, uint8_t ch, bool enable)
ACMP channel enable comparator output sync with clock.
Definition: hpm_acmp_drv.h:302
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:287
static void acmp_channel_enable_cmp(ACMP_Type *ptr, uint8_t ch, bool enable)
ACMP channel enable comparator.
Definition: hpm_acmp_drv.h:214
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:93
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:155
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:186
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:136
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:244
hpm_stat_t acmp_channel_config(ACMP_Type *ptr, uint8_t ch, acmp_channel_config_t *config, bool enable)
ADC channel config.
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:315
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:229
static void acmp_channel_enable_dac(ACMP_Type *ptr, uint8_t ch, bool enable)
ACMP channel enable DAC.
Definition: hpm_acmp_drv.h:171
static void acmp_channel_invert_output(ACMP_Type *ptr, uint8_t ch, bool enable)
ACMP channel invert comparator output.
Definition: hpm_acmp_drv.h:274
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:32
uint32_t hpm_stat_t
Definition: hpm_common.h:119
#define ACMP_CHANNEL_CFG_CMPEN_MASK
Definition: hpm_acmp_regs.h:71
#define ACMP_CHANNEL_CFG_FLTLEN_SET(x)
Definition: hpm_acmp_regs.h:178
#define ACMP_CHANNEL_CFG_SYNCEN_MASK
Definition: hpm_acmp_regs.h:166
#define ACMP_CHANNEL_CFG_FLTBYPS_MASK
Definition: hpm_acmp_regs.h:115
#define ACMP_CHANNEL_CFG_FLTMODE_MASK
Definition: hpm_acmp_regs.h:154
#define ACMP_CHANNEL_CFG_FLTMODE_SET(x)
Definition: hpm_acmp_regs.h:156
#define ACMP_CHANNEL_CFG_CMPOEN_MASK
Definition: hpm_acmp_regs.h:103
#define ACMP_CHANNEL_CFG_WINEN_SET(x)
Definition: hpm_acmp_regs.h:129
#define ACMP_CHANNEL_DACCFG_DACCFG_SET(x)
Definition: hpm_acmp_regs.h:189
#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_SYNCEN_SET(x)
Definition: hpm_acmp_regs.h:168
#define ACMP_CHANNEL_CFG_OPOL_MASK
Definition: hpm_acmp_regs.h:139
#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:141
#define ACMP_CHANNEL_CFG_FLTBYPS_SET(x)
Definition: hpm_acmp_regs.h:117
#define ACMP_CHANNEL_CFG_CMPOEN_SET(x)
Definition: hpm_acmp_regs.h:105
#define ACMP_CHANNEL_CFG_WINEN_MASK
Definition: hpm_acmp_regs.h:127
#define ACMP_CHANNEL_CFG_FLTLEN_MASK
Definition: hpm_acmp_regs.h:176
#define ACMP_CHANNEL_CFG_HYST_SET(x)
Definition: hpm_acmp_regs.h:37
Definition: hpm_acmp_regs.h:12
struct ACMP_Type::@366 CHANNEL[4]
__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
__RW uint32_t DMAEN
Definition: hpm_acmp_regs.h:19
ACMP channel config.
Definition: hpm_acmp_drv.h:67
bool invert_output
Definition: hpm_acmp_drv.h:74
bool enable_clock_sync
Definition: hpm_acmp_drv.h:75
uint8_t minus_input
Definition: hpm_acmp_drv.h:69
bool enable_dac
Definition: hpm_acmp_drv.h:77
uint8_t filter_mode
Definition: hpm_acmp_drv.h:70
uint8_t hyst_level
Definition: hpm_acmp_drv.h:71
uint16_t filter_length
Definition: hpm_acmp_drv.h:79
bool enable_hpmode
Definition: hpm_acmp_drv.h:78
bool enable_cmp_output
Definition: hpm_acmp_drv.h:72
uint8_t plus_input
Definition: hpm_acmp_drv.h:68
bool enable_window_mode
Definition: hpm_acmp_drv.h:73
bool bypass_filter
Definition: hpm_acmp_drv.h:76