1 ////////////////////////////////////////////////////////////////////////////////
2 /// @file     hal_comp.c
3 /// @author   AE TEAM
4 /// @brief    THIS FILE PROVIDES ALL THE COMP FIRMWARE FUNCTIONS.
5 ////////////////////////////////////////////////////////////////////////////////
6 /// @attention
7 ///
8 /// THE EXISTING FIRMWARE IS ONLY FOR REFERENCE, WHICH IS DESIGNED TO PROVIDE
9 /// CUSTOMERS WITH CODING INFORMATION ABOUT THEIR PRODUCTS SO THEY CAN SAVE
10 /// TIME. THEREFORE, MINDMOTION SHALL NOT BE LIABLE FOR ANY DIRECT, INDIRECT OR
11 /// CONSEQUENTIAL DAMAGES ABOUT ANY CLAIMS ARISING OUT OF THE CONTENT OF SUCH
12 /// HARDWARE AND/OR THE USE OF THE CODING INFORMATION CONTAINED HEREIN IN
13 /// CONNECTION WITH PRODUCTS MADE BY CUSTOMERS.
14 ///
15 /// <H2><CENTER>&COPY; COPYRIGHT MINDMOTION </CENTER></H2>
16 ////////////////////////////////////////////////////////////////////////////////
17 
18 // Define to prevent recursive inclusion
19 #define _HAL_COMP_C_
20 
21 // Files includes
22 #include "hal_comp.h"
23 
24 ////////////////////////////////////////////////////////////////////////////////
25 /// @addtogroup MM32_Hardware_Abstract_Layer
26 /// @{
27 
28 ////////////////////////////////////////////////////////////////////////////////
29 /// @addtogroup COMP_HAL
30 /// @{
31 
32 
33 ////////////////////////////////////////////////////////////////////////////////
34 /// @addtogroup COMP_Exported_Functions
35 /// @{
36 
37 ////////////////////////////////////////////////////////////////////////////////
38 /// @brief  Deinitializes COMP peripheral registers to their default reset
39 ///         values.
40 /// @param  selection: the selected comparator.
41 ///         select the COMP peripheral.
42 /// @retval None.
43 ////////////////////////////////////////////////////////////////////////////////
COMP_DeInit(COMP_Selection_TypeDef selection)44 void COMP_DeInit(COMP_Selection_TypeDef selection)
45 {
46     *(vu32*)(COMP_BASE + selection) = 0;
47 }
48 
49 ////////////////////////////////////////////////////////////////////////////////
50 /// @brief  Initializes the COMP peripheral according to the specified
51 /// @param  selection: the selected comparator.
52 ///         select the COMP peripheral.
53 /// @param  init_struct: pointer to an COMP_InitTypeDef structure that
54 ///         contains the configuration information for the specified COMP
55 ///         peripheral.
56 ///         - COMP_InvertingInput specifies the inverting input of COMP
57 ///         - COMP_NonInvertingInput specifies the non inverting input of COMP
58 ///         - COMP_Output connect COMP output to selected timer
59 ///           input (Input capture / Output Compare Reference Clear / Break
60 ///           Input)
61 ///         - COMP_BlankingSrce specifies the blanking source of COMP
62 ///         - COMP_OutputPol select output polarity
63 ///         - COMP_Hysteresis configures COMP hysteresis value
64 ///         - COMP_Mode configures COMP power mode
65 /// @retval None.
66 ////////////////////////////////////////////////////////////////////////////////
COMP_Init(COMP_Selection_TypeDef selection,COMP_InitTypeDef * init_struct)67 void COMP_Init(COMP_Selection_TypeDef selection, COMP_InitTypeDef* init_struct)
68 {
69     *(vu32*)(COMP_BASE + selection) =    init_struct->Invert     |
70                                          init_struct->NonInvert  |
71                                          init_struct->Output     |
72                                          init_struct->OutputPol  |
73                                          init_struct->BlankingSrce   |
74                                          init_struct->Hysteresis |
75                                          init_struct->Mode       |
76                                          init_struct->OFLT;
77 }
78 
79 ////////////////////////////////////////////////////////////////////////////////
80 /// @brief  Fills each init_struct member with its default value.
81 /// @param  init_struct: pointer to an COMP_InitTypeDef structure which will
82 ///         be initialized.
83 /// @retval None.
84 ////////////////////////////////////////////////////////////////////////////////
COMP_StructInit(COMP_InitTypeDef * init_struct)85 void COMP_StructInit(COMP_InitTypeDef* init_struct)
86 {
87 
88     init_struct->Invert         = COMP_InvertingInput_IO1;
89     init_struct->NonInvert      = COMP_NonInvertingInput_IO1;
90     init_struct->Output         = COMP_Output_None;
91     init_struct->BlankingSrce   = COMP_BlankingSrce_None;
92     init_struct->OutputPol      = COMP_NonInverted;
93     init_struct->Hysteresis     = COMP_Hysteresis_No;
94     init_struct->Mode           = COMP_Mode_UltraLowPower;
95     init_struct->OFLT           = COMP_Filter_4_Period;                                                               ///< to adjust the speed/consumption.
96 }
97 
98 ////////////////////////////////////////////////////////////////////////////////
99 /// @brief  Enable or disable the COMP peripheral.
100 /// @param  selection: the selected comparator.
101 ///         select the COMP peripheral.
102 /// @param  NewState: new state of the COMP peripheral.
103 ///         This parameter can be: ENABLE or DISABLE.
104 ///         When enabled, the comparator compares the non inverting input with
105 ///         the inverting input and the comparison result is available on
106 ///         comparator output.
107 ///         When disabled, the comparator doesn't perform comparison and the
108 ///         output level is low.
109 /// @retval None.
110 ////////////////////////////////////////////////////////////////////////////////
COMP_Cmd(COMP_Selection_TypeDef selection,FunctionalState state)111 void COMP_Cmd(COMP_Selection_TypeDef selection, FunctionalState state)
112 {
113     (state) ? (*(vu32*)(COMP_BASE + selection) |=  COMP_CSR_EN) :
114     (*(vu32*)(COMP_BASE + selection) &= ~COMP_CSR_EN);
115 }
116 ////////////////////////////////////////////////////////////////////////////////
117 /// @brief  Select CRV param.
118 /// @param  crv_select: Select source for CRV.
119 /// @param  crv_level: Set level for CRV.
120 /// @retval None.
121 ////////////////////////////////////////////////////////////////////////////////
COMP_SetCrv(u8 crv_select,u8 crv_level)122 void COMP_SetCrv(u8 crv_select, u8 crv_level)
123 {
124     u32 temreg = 0;
125     temreg = COMP->CRV;
126     temreg &= ~COMP_CRV_MASK;
127     // Load config to CRV and enable
128     temreg |= crv_select | crv_level | (1 << 4);
129     COMP->CRV = temreg;
130 }
131 ////////////////////////////////////////////////////////////////////////////////
132 /// @brief  Close or Open the SW1 switch.
133 /// @param  selection: the selected comparator.
134 ///         select the COMP peripheral.
135 /// @param  state: new state of the COMP peripheral.
136 ///         This parameter can be: ENABLE or DISABLE.
137 ///         When enabled, the comparator compares the non inverting input with
138 ///         the inverting input and the comparison result is available on
139 ///         comparator output.
140 ///         When disabled, the comparator doesn't perform comparison and the
141 ///         output level is low.
142 /// @retval None.
143 ////////////////////////////////////////////////////////////////////////////////
COMP_SwitchCmd(COMP_Selection_TypeDef selection,FunctionalState state)144 void COMP_SwitchCmd(COMP_Selection_TypeDef selection, FunctionalState state)
145 {
146     (state) ?
147     (*(vu32*)(COMP_BASE + selection) |=  COMP_CSR_COMPSW1) :
148     (*(vu32*)(COMP_BASE + selection) &= ~COMP_CSR_COMPSW1);
149 }
150 
151 ////////////////////////////////////////////////////////////////////////////////
152 /// @brief  Return the output level (high or low) of the selected comparator.
153 ///         The output level depends on the selected polarity.
154 ///         If the polarity is not inverted:
155 ///           - Comparator output is low when the non-inverting input is at a
156 ///           lower voltage than the inverting input
157 ///           - Comparator output is high when the non-inverting input is at a
158 ///           higher voltage than the inverting input
159 ///         If the polarity is inverted:
160 ///           - Comparator output is high when the non-inverting input is at a
161 ///           lower voltage than the inverting input
162 ///           - Comparator output is low when the non-inverting input is at a
163 ///           higher voltage than the inverting input
164 /// @param  comp: the selected comparator.
165 ///         select the COMP peripheral.
166 /// @retval  The selected comparator output level: low or high.
167 ////////////////////////////////////////////////////////////////////////////////
COMP_GetOutputLevel(COMP_Selection_TypeDef selection)168 u32 COMP_GetOutputLevel(COMP_Selection_TypeDef selection)
169 {
170     return (((*(vu32*)(COMP_BASE + selection) & COMP_CSR_STA) != 0) ?
171             COMP_OutputLevel_High :
172             COMP_OutputLevel_Low );
173 }
174 
175 ////////////////////////////////////////////////////////////////////////////////
176 /// @brief  Lock the selected comparator (COMP1/COMP2) configuration.
177 /// @param  selection: the selected comparator.
178 ///         select the COMP peripheral.
179 /// @retval None.
180 ////////////////////////////////////////////////////////////////////////////////
COMP_LockConfig(COMP_Selection_TypeDef selection)181 void COMP_LockConfig(COMP_Selection_TypeDef selection)
182 {
183     *(vu32*)(COMP_BASE + selection) |= COMP_CSR_LOCK;
184 }
185 
186 ////////////////////////////////////////////////////////////////////////////////
187 /// @brief  Enable or disable the COMP register.
188 /// @param  state: new state of the COMP peripheral.
189 ///         This parameter can be: ENABLE or DISABLE.
190 /// @retval None.
191 ////////////////////////////////////////////////////////////////////////////////
exCOMP_CrvCmd(FunctionalState state)192 void exCOMP_CrvCmd(FunctionalState state)
193 {
194     (state) ? (COMP->CRV |= COMP_CRV_EN_ENABLE) : (COMP->CRV &= ~COMP_CRV_EN_ENABLE);
195 }
196 
197 ////////////////////////////////////////////////////////////////////////////////
198 /// @brief  Select comparator external reference voltage.
199 /// @param  selection: the selected external reference voltage.
200 /// @retval None.
201 ////////////////////////////////////////////////////////////////////////////////
exCOMP_SwitchCrv(u32 crv)202 void exCOMP_SwitchCrv(u32 crv)
203 {
204     COMP->CRV |= crv;
205 }
206 
207 ////////////////////////////////////////////////////////////////////////////////
208 /// @brief  Select comparator external reference voltage source.
209 /// @param  selection: the selected external reference voltage source.
210 ///         This parameter can be: COMP_CRV_SRC_AVDD or COMP_CRV_SRC_VREF.
211 /// @retval None.
212 ////////////////////////////////////////////////////////////////////////////////
exCOMP_CrvSrc(u32 src)213 void exCOMP_CrvSrc(u32 src)
214 {
215     COMP->CRV |= src;
216 }
217 
218 
219 
220 
221 
222 /// @}
223 
224 /// @}
225 
226 /// @}
227