1 /*
2   ******************************************************************************
3   * @file    HAL_COMP.h
4   * @version V1.0.0
5   * @date    2020
6   * @brief   Header file of COMP HAL module.
7   ******************************************************************************
8 */
9 #ifndef __HAL_COMP_H__
10 #define __HAL_COMP_H__
11 
12 #include "ACM32Fxx_HAL.h"
13 
14 #define COMP1        (0x01)
15 #define COMP2        (0x02)
16 
17 /****************  Bit definition for COMP_CR1 and COMP_CR2 register  **************************/
18 #define COMP_CR_LOCK                            (BIT31)
19 #define COMP_CR_BLANKTIME_POS                   (29U)
20 #define COMP_CR_BLANKTIME_MASK                  (BIT30|BIT29)
21 #define COMP_CR_CRV_CFG_POS                     (25U)
22 #define COMP_CR_CRV_CFG_MASK                    (BIT28|BIT27|BIT26|BIT25)
23 #define COMP_CR_CRV_SEL                         (BIT24)
24 #define COMP_CR_CRV_EN                          (BIT23)
25 #define COMP_CR_WINMODE                         (BIT22)
26 #define COMP_CR_WINOUT                          (BIT21)
27 #define COMP_CR_POLARITY                        (BIT20)
28 #define COMP_CR_FLTEN                           (BIT19)
29 #define COMP_CR_FLTTIME_POS                     (16U)
30 #define COMP_CR_FLTTIME_MASK                    (BIT18|BIT17|BIT16)
31 #define COMP_CR_BLANKSEL_POS                    (12U)
32 #define COMP_CR_BLANKSEL_MASK                   (BIT15|BIT14|BIT13|BIT12)
33 #define COMP_CR_INPSEL_POS                      (8U)
34 #define COMP_CR_INPSEL_MASK                     (BIT11|BIT10|BIT9|BIT8)
35 #define COMP_CR_INMSEL_POS                      (4U)
36 #define COMP_CR_INMSEL_MASK                     (BIT7|BIT6|BIT5|BIT4)
37 #define COMP_CR_HYS_POS                         (1U)
38 #define COMP_CR_HYS_MASK                        (BIT3|BIT2|BIT1)
39 #define COMP_CR_EN                              (BIT0)
40 
41 /****************  Bit definition for COMP_SR register  **************************/
42 #define COMP_SR_VCOUT2_ORG                      (BIT3)
43 #define COMP_SR_VCOUT1_ORG                      (BIT2)
44 #define COMP_SR_VCOUT2                          (BIT1)
45 #define COMP_SR_VCOUT1                          (BIT0)
46 
47 /**
48   * @brief  COMP Configuration Structure definition
49   */
50 typedef struct
51 {
52     uint8_t             Comparator;        /*!< Specify witch comparator be selected */
53     uint32_t            Crv_En;
54     uint32_t            BlankTime;
55     uint32_t            Crv_Sel;
56     uint32_t            Crv_Cfg;
57     uint32_t            WinMode;
58     uint32_t            WinOut;
59     uint32_t            Polarity;
60     uint32_t            FltEn;
61     uint32_t            FltTime;
62     uint32_t            BlankSel;
63     uint32_t            InPSel;
64     uint32_t            InMSel;
65     uint32_t            HYS;
66 }COMP_InitTypeDef;
67 
68 /**
69   * @brief  COMP handle Structure definition
70   */
71 typedef struct
72 {
73   COMP_TypeDef       *Instance;       /*!< Register base address    */
74   COMP_InitTypeDef   Init;            /*!< COMP required parameters */
75   uint8_t            OutputLevel_Org; /*!< COMP OutputLevel original */
76   uint8_t            OutputLevel;     /*!< COMP OutputLevel with filter */
77 } COMP_HandleTypeDef;
78 
79 #define COMP_CR_CRV_EN_DISABLE           (0U)
80 #define COMP_CR_CRV_EN_ENABLE            (1U)
81 
82 #define COMP_CR_CRV_SEL_AVDD             (0U)
83 #define COMP_CR_CRV_SEL_VREF             (1U)
84 
85 #define COMP_CR1_WINMODE_COMP1_INPSEL           (0U)
86 #define COMP_CR1_WINMODE_COMP2_INPSEL           (1U)
87 
88 #define COMP_CR2_WINMODE_COMP2_INPSEL           (0U)
89 #define COMP_CR2_WINMODE_COMP1_INPSEL           (1U)
90 
91 #define COMP_CR1_WINOUT_VCOUT1                 (0U)
92 #define COMP_CR2_WINOUT_VCOUT2                 (0U)
93 #define COMP_CR_WINOUT_VCOUT12                 (1U)
94 
95 #define COMP_CR_POLARITY_P                     (0U)
96 #define COMP_CR_POLARITY_N                     (1U)
97 
98 #define COMP_CR_FLTEN_DISABLE                  (0U)
99 #define COMP_CR_FLTEN_ENABLE                   (1U)
100 
101 #define COMP_CR_FLTTIME_1_CLK                  (0U)
102 #define COMP_CR_FLTTIME_2_CLK                  (1U)
103 #define COMP_CR_FLTTIME_4_CLK                  (2U)
104 #define COMP_CR_FLTTIME_16_CLK                 (3U)
105 #define COMP_CR_FLTTIME_64_CLK                 (4U)
106 #define COMP_CR_FLTTIME_256_CLK                (5U)
107 #define COMP_CR_FLTTIME_1024_CLK               (6U)
108 #define COMP_CR_FLTTIME_4095_CLK               (7U)
109 
110 #define COMP_CR_BLANKTIME_32_CLK               (0U)
111 #define COMP_CR_BLANKTIME_64_CLK               (1U)
112 #define COMP_CR_BLANKTIME_128_CLK              (2U)
113 #define COMP_CR_BLANKTIME_256_CLK              (3U)
114 
115 #define COMP_CR_BLANKSEL_NONE                  (0U)
116 #define COMP_CR_BLANKSEL_1                     (1U)
117 #define COMP_CR_BLANKSEL_2                     (2U)
118 #define COMP_CR_BLANKSEL_3                     (4U)
119 #define COMP_CR_BLANKSEL_4                     (8U)
120 
121 #define COMP_CR_INPSEL_0                      (0U)
122 #define COMP_CR_INPSEL_1                      (1U)
123 #define COMP_CR_INPSEL_2                      (2U)
124 
125 #define COMP_CR_INMSEL_0                      (0U)
126 #define COMP_CR_INMSEL_1                      (1U)
127 #define COMP_CR_INMSEL_2                      (2U)
128 #define COMP_CR_INMSEL_3                      (3U)
129 
130 #define COMP_CR_HYS_DISABLE                   (0U)
131 #define COMP_CR_HYS_1                         (4U)
132 #define COMP_CR_HYS_2                         (5U)
133 #define COMP_CR_HYS_3                         (6U)
134 #define COMP_CR_HYS_4                         (7U)
135 
136 /******************************** COMP Instances *******************************/
137 #define IS_COMP_ALL_INSTANCE(INSTANCE)    ((INSTANCE) == COMP)
138 #define IS_COMP_ALL_COMP(_COMP)     (((_COMP) == COMP1) || \
139                                   ((_COMP) == COMP2))
140 
141 #define IS_COMP_ALL_CRV_EN(_CRV_EN)        (((_CRV_EN) == COMP_CR_CRV_EN_DISABLE) || \
142                                            ((_CRV_EN) == COMP_CR_CRV_EN_ENABLE))
143 
144 #define IS_COMP_ALL_CRV_SEL(_CRV_SEL)      (((_CRV_SEL) == COMP_CR_CRV_SEL_AVDD) || \
145                                            ((_CRV_SEL) == COMP_CR_CRV_SEL_VREF))
146 
147 #define IS_COMP_ALL_CRV_CFG(_CRV_CFG)      ((_CRV_CFG) <= 15U)
148 
149 #define IS_COMP_ALL_WINMODE(WINMODE)       (((WINMODE) == COMP_CR1_WINMODE_COMP1_INPSEL) || \
150                                            ((WINMODE) == COMP_CR1_WINMODE_COMP2_INPSEL) || \
151                                            ((WINMODE) == COMP_CR2_WINMODE_COMP2_INPSEL) || \
152                                            ((WINMODE) == COMP_CR2_WINMODE_COMP1_INPSEL))
153 
154 #define IS_COMP_ALL_WINOUT(_WINOUT)        (((_WINOUT) == COMP_CR1_WINOUT_VCOUT1) || \
155                                            ((_WINOUT) == COMP_CR2_WINOUT_VCOUT2) || \
156                                            ((_WINOUT) == COMP_CR_WINOUT_VCOUT12))
157 
158 #define IS_COMP_ALL_POLARITY(POLARITY)     (((POLARITY) == COMP_CR_POLARITY_N) || \
159                                            ((POLARITY) == COMP_CR_POLARITY_P))
160 
161 #define IS_COMP_ALL_FLTEN(FLTEN)           (((FLTEN) == COMP_CR_FLTEN_DISABLE) || \
162                                            ((FLTEN) == COMP_CR_FLTEN_ENABLE))
163 
164 #define IS_COMP_ALL_FLTTIME(FLTTIME)       (((FLTTIME) == COMP_CR_FLTTIME_1_CLK) || \
165                                            ((FLTTIME) == COMP_CR_FLTTIME_2_CLK) || \
166                                            ((FLTTIME) == COMP_CR_FLTTIME_4_CLK) || \
167                                            ((FLTTIME) == COMP_CR_FLTTIME_16_CLK) || \
168                                            ((FLTTIME) == COMP_CR_FLTTIME_64_CLK) || \
169                                            ((FLTTIME) == COMP_CR_FLTTIME_256_CLK) || \
170                                            ((FLTTIME) == COMP_CR_FLTTIME_1024_CLK) || \
171                                            ((FLTTIME) == COMP_CR_FLTTIME_4095_CLK))
172 
173 #define IS_COMP_ALL_BLANKTIME(BLANKTIME)   (((BLANKTIME) == COMP_CR_BLANKTIME_32_CLK) || \
174                                            ((BLANKTIME) == COMP_CR_BLANKTIME_64_CLK) || \
175                                            ((BLANKTIME) == COMP_CR_BLANKTIME_128_CLK) || \
176                                            ((BLANKTIME) == COMP_CR_BLANKTIME_256_CLK))
177 
178 #define IS_COMP_ALL_BLANKSEL(BLANKSEL)     (((BLANKSEL) == COMP_CR_BLANKSEL_NONE) || \
179                                            ((BLANKSEL) == COMP_CR_BLANKSEL_1) || \
180                                            ((BLANKSEL) == COMP_CR_BLANKSEL_2) || \
181                                            ((BLANKSEL) == COMP_CR_BLANKSEL_3) || \
182                                            ((BLANKSEL) == COMP_CR_BLANKSEL_4))
183 
184 #define IS_COMP_ALL_INPSEL(INPSEL)         (((INPSEL) == COMP_CR_INPSEL_0) || \
185                                            ((INPSEL) == COMP_CR_INPSEL_1) || \
186                                            ((INPSEL) == COMP_CR_INPSEL_2))
187 
188 #define IS_COMP_ALL_INMSEL(INMSEL)         (((INMSEL) == COMP_CR_INMSEL_0 ) || \
189                                            ((INMSEL) == COMP_CR_INMSEL_1 ) || \
190                                            ((INMSEL) == COMP_CR_INMSEL_2 ) || \
191                                            ((INMSEL) == COMP_CR_INMSEL_3 ))
192 
193 #define IS_COMP_ALL_HYS(_HYS)              (((_HYS) == COMP_CR_HYS_DISABLE) || \
194                                            ((_HYS) == COMP_CR_HYS_1) || \
195                                            ((_HYS) == COMP_CR_HYS_2) || \
196                                            ((_HYS) == COMP_CR_HYS_3) || \
197                                            ((_HYS) == COMP_CR_HYS_4))
198 
199 /* Function */
200 void HAL_COMP_MspInit(COMP_HandleTypeDef* hcomp);
201 void HAL_COMP_MspDeInit(COMP_HandleTypeDef* hcomp);
202 HAL_StatusTypeDef HAL_COMP_Init(COMP_HandleTypeDef* hcomp);
203 HAL_StatusTypeDef HAL_COMP_DeInit(COMP_HandleTypeDef* hcomp);
204 HAL_StatusTypeDef HAL_COMP_Enable(COMP_HandleTypeDef* hcomp);
205 HAL_StatusTypeDef HAL_COMP_Disable(COMP_HandleTypeDef* hcomp);
206 HAL_StatusTypeDef HAL_COMP_GetOutputLevel(COMP_HandleTypeDef* hcomp);
207 HAL_StatusTypeDef HAL_COMP_Lock(COMP_HandleTypeDef* hcomp);
208 HAL_StatusTypeDef HAL_COMP_Start(COMP_HandleTypeDef *hcomp);
209 HAL_StatusTypeDef HAL_COMP_Stop(COMP_HandleTypeDef *hcomp);
210 
211 #endif
212 
213 
214 
215 
216 
217