summaryrefslogtreecommitdiff
path: root/bsp/mm32f327x/Libraries/MM32F327x/HAL_Lib/Src/hal_comp.c
blob: b55558263c7116cd1b14bc9f784b2b06c9d00ee0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
////////////////////////////////////////////////////////////////////////////////
/// @file     hal_comp.c
/// @author   AE TEAM
/// @brief    THIS FILE PROVIDES ALL THE COMP FIRMWARE FUNCTIONS.
////////////////////////////////////////////////////////////////////////////////
/// @attention
///
/// THE EXISTING FIRMWARE IS ONLY FOR REFERENCE, WHICH IS DESIGNED TO PROVIDE
/// CUSTOMERS WITH CODING INFORMATION ABOUT THEIR PRODUCTS SO THEY CAN SAVE
/// TIME. THEREFORE, MINDMOTION SHALL NOT BE LIABLE FOR ANY DIRECT, INDIRECT OR
/// CONSEQUENTIAL DAMAGES ABOUT ANY CLAIMS ARISING OUT OF THE CONTENT OF SUCH
/// HARDWARE AND/OR THE USE OF THE CODING INFORMATION CONTAINED HEREIN IN
/// CONNECTION WITH PRODUCTS MADE BY CUSTOMERS.
///
/// <H2><CENTER>&COPY; COPYRIGHT MINDMOTION </CENTER></H2>
////////////////////////////////////////////////////////////////////////////////

// Define to prevent recursive inclusion
#define _HAL_COMP_C_

// Files includes
#include "hal_comp.h"

////////////////////////////////////////////////////////////////////////////////
/// @addtogroup MM32_Hardware_Abstract_Layer
/// @{

////////////////////////////////////////////////////////////////////////////////
/// @addtogroup COMP_HAL
/// @{


////////////////////////////////////////////////////////////////////////////////
/// @addtogroup COMP_Exported_Functions
/// @{

////////////////////////////////////////////////////////////////////////////////
/// @brief  Deinitializes COMP peripheral registers to their default reset
///         values.
/// @param  selection: the selected comparator.
///         select the COMP peripheral.
/// @retval None.
////////////////////////////////////////////////////////////////////////////////
void COMP_DeInit(COMP_Selection_TypeDef selection)
{
    *(vu32*)(COMP_BASE + selection) = 0;
}

////////////////////////////////////////////////////////////////////////////////
/// @brief  Initializes the COMP peripheral according to the specified
/// @param  selection: the selected comparator.
///         select the COMP peripheral.
/// @param  init_struct: pointer to an COMP_InitTypeDef structure that
///         contains the configuration information for the specified COMP
///         peripheral.
///         - COMP_InvertingInput specifies the inverting input of COMP
///         - COMP_NonInvertingInput specifies the non inverting input of COMP
///         - COMP_Output connect COMP output to selected timer
///           input (Input capture / Output Compare Reference Clear / Break
///           Input)
///         - COMP_BlankingSrce specifies the blanking source of COMP
///         - COMP_OutputPol select output polarity
///         - COMP_Hysteresis configures COMP hysteresis value
///         - COMP_Mode configures COMP power mode
/// @retval None.
////////////////////////////////////////////////////////////////////////////////
void COMP_Init(COMP_Selection_TypeDef selection, COMP_InitTypeDef* init_struct)
{
    *(vu32*)(COMP_BASE + selection) =    init_struct->Invert     |
                                         init_struct->NonInvert  |
                                         init_struct->Output     |
                                         init_struct->OutputPol  |
                                         init_struct->BlankingSrce   |
                                         init_struct->Hysteresis |
                                         init_struct->Mode       |
                                         init_struct->OFLT;
}

////////////////////////////////////////////////////////////////////////////////
/// @brief  Fills each init_struct member with its default value.
/// @param  init_struct: pointer to an COMP_InitTypeDef structure which will
///         be initialized.
/// @retval None.
////////////////////////////////////////////////////////////////////////////////
void COMP_StructInit(COMP_InitTypeDef* init_struct)
{

    init_struct->Invert         = COMP_InvertingInput_IO1;
    init_struct->NonInvert      = COMP_NonInvertingInput_IO1;
    init_struct->Output         = COMP_Output_None;
    init_struct->BlankingSrce   = COMP_BlankingSrce_None;
    init_struct->OutputPol      = COMP_NonInverted;
    init_struct->Hysteresis     = COMP_Hysteresis_No;
    init_struct->Mode           = COMP_Mode_UltraLowPower;
    init_struct->OFLT           = COMP_Filter_4_Period;                                                               ///< to adjust the speed/consumption.
}

////////////////////////////////////////////////////////////////////////////////
/// @brief  Enable or disable the COMP peripheral.
/// @param  selection: the selected comparator.
///         select the COMP peripheral.
/// @param  NewState: new state of the COMP peripheral.
///         This parameter can be: ENABLE or DISABLE.
///         When enabled, the comparator compares the non inverting input with
///         the inverting input and the comparison result is available on
///         comparator output.
///         When disabled, the comparator doesn't perform comparison and the
///         output level is low.
/// @retval None.
////////////////////////////////////////////////////////////////////////////////
void COMP_Cmd(COMP_Selection_TypeDef selection, FunctionalState state)
{
    (state) ? (*(vu32*)(COMP_BASE + selection) |=  COMP_CSR_EN) :
    (*(vu32*)(COMP_BASE + selection) &= ~COMP_CSR_EN);
}
////////////////////////////////////////////////////////////////////////////////
/// @brief  Select CRV param.
/// @param  crv_select: Select source for CRV.
/// @param  crv_level: Set level for CRV.
/// @retval None.
////////////////////////////////////////////////////////////////////////////////
void COMP_SetCrv(u8 crv_select, u8 crv_level)
{
    u32 temreg = 0;
    temreg = COMP->CRV;
    temreg &= ~COMP_CRV_MASK;
    // Load config to CRV and enable
    temreg |= crv_select | crv_level | (1 << 4);
    COMP->CRV = temreg;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief  Close or Open the SW1 switch.
/// @param  selection: the selected comparator.
///         select the COMP peripheral.
/// @param  state: new state of the COMP peripheral.
///         This parameter can be: ENABLE or DISABLE.
///         When enabled, the comparator compares the non inverting input with
///         the inverting input and the comparison result is available on
///         comparator output.
///         When disabled, the comparator doesn't perform comparison and the
///         output level is low.
/// @retval None.
////////////////////////////////////////////////////////////////////////////////
void COMP_SwitchCmd(COMP_Selection_TypeDef selection, FunctionalState state)
{
    (state) ?
    (*(vu32*)(COMP_BASE + selection) |=  COMP_CSR_COMPSW1) :
    (*(vu32*)(COMP_BASE + selection) &= ~COMP_CSR_COMPSW1);
}

////////////////////////////////////////////////////////////////////////////////
/// @brief  Return the output level (high or low) of the selected comparator.
///         The output level depends on the selected polarity.
///         If the polarity is not inverted:
///           - Comparator output is low when the non-inverting input is at a
///           lower voltage than the inverting input
///           - Comparator output is high when the non-inverting input is at a
///           higher voltage than the inverting input
///         If the polarity is inverted:
///           - Comparator output is high when the non-inverting input is at a
///           lower voltage than the inverting input
///           - Comparator output is low when the non-inverting input is at a
///           higher voltage than the inverting input
/// @param  comp: the selected comparator.
///         select the COMP peripheral.
/// @retval  The selected comparator output level: low or high.
////////////////////////////////////////////////////////////////////////////////
u32 COMP_GetOutputLevel(COMP_Selection_TypeDef selection)
{
    return (((*(vu32*)(COMP_BASE + selection) & COMP_CSR_STA) != 0) ?
            COMP_OutputLevel_High :
            COMP_OutputLevel_Low );
}

////////////////////////////////////////////////////////////////////////////////
/// @brief  Lock the selected comparator (COMP1/COMP2) configuration.
/// @param  selection: the selected comparator.
///         select the COMP peripheral.
/// @retval None.
////////////////////////////////////////////////////////////////////////////////
void COMP_LockConfig(COMP_Selection_TypeDef selection)
{
    *(vu32*)(COMP_BASE + selection) |= COMP_CSR_LOCK;
}

////////////////////////////////////////////////////////////////////////////////
/// @brief  Enable or disable the COMP register.
/// @param  state: new state of the COMP peripheral.
///         This parameter can be: ENABLE or DISABLE.
/// @retval None.
////////////////////////////////////////////////////////////////////////////////
void exCOMP_CrvCmd(FunctionalState state)
{
    (state) ? (COMP->CRV |= COMP_CRV_EN_ENABLE) : (COMP->CRV &= ~COMP_CRV_EN_ENABLE);
}

////////////////////////////////////////////////////////////////////////////////
/// @brief  Select comparator external reference voltage.
/// @param  selection: the selected external reference voltage.
/// @retval None.
////////////////////////////////////////////////////////////////////////////////
void exCOMP_SwitchCrv(u32 crv)
{
    COMP->CRV |= crv;
}

////////////////////////////////////////////////////////////////////////////////
/// @brief  Select comparator external reference voltage source.
/// @param  selection: the selected external reference voltage source.
///         This parameter can be: COMP_CRV_SRC_AVDD or COMP_CRV_SRC_VREF.
/// @retval None.
////////////////////////////////////////////////////////////////////////////////
void exCOMP_CrvSrc(u32 src)
{
    COMP->CRV |= src;
}





/// @}

/// @}

/// @}