1 /**
2 ******************************************************************************
3 * @file rtl8721d_comparator.c
4 * @author
5 * @version V1.0.0
6 * @date 2016-05-17
7 * @brief This file provides firmware functions to manage the following
8 * functionalities of the Comparator peripheral:
9 * - Initialization and Configuration
10 * - Analog configuration
11 * - Mode configuration
12 * - Interrupts and flags management
13 *
14 ******************************************************************************
15 * @attention
16 *
17 * This module is a confidential and proprietary property of RealTek and
18 * possession or use of this module requires written permission of RealTek.
19 *
20 * Copyright(c) 2016, Realtek Semiconductor Corporation. All rights reserved.
21 ******************************************************************************
22 */
23
24 #include "ameba_soc.h"
25
26 /**
27 * @brief Initializes the parameters in the CMP_InitStruct with default value.
28 * @param CMP_InitStruct: pointer to a CMP_InitTypeDef structure that contains
29 * the configuration information for the Comparator peripheral.
30 * @retval None
31 */
CMP_StructInit(CMP_InitTypeDef * CMP_InitStruct)32 void CMP_StructInit(CMP_InitTypeDef *CMP_InitStruct)
33 {
34 u8 i;
35
36 for(i = 0; i < COMP_CH_NUM; i++) {
37 CMP_InitStruct->CMP_ChanCtrl[i].CMP_ChIndex = i;
38 CMP_InitStruct->CMP_ChanCtrl[i].CMP_Ref0 = 0; // Vref0 = 0.1*0=0v
39 CMP_InitStruct->CMP_ChanCtrl[i].CMP_Ref1 = 0x1F; // Vref1 = 0.1*31=3.1v
40 CMP_InitStruct->CMP_ChanCtrl[i].CMP_WakeType = COMP_WK_NONE;
41 CMP_InitStruct->CMP_ChanCtrl[i].CMP_WakeSysCtrl = COMP_WITHIN_REF0_AND_REF1;
42 CMP_InitStruct->CMP_ChanCtrl[i].CMP_WakeADCCtrl = COMP_WITHIN_REF0_AND_REF1;
43 }
44 }
45
46 /**
47 * @brief Initializes the Comparator according to the specified parameters in CMP_InitStruct.
48 * @param CMP_InitStruct: pointer to a CMP_InitTypeDef structure that contains
49 * the configuration for the Comparator peripheral.
50 * @retval None
51 */
CMP_Init(CMP_InitTypeDef * CMP_InitStruct)52 void CMP_Init(CMP_InitTypeDef* CMP_InitStruct)
53 {
54 u8 i, index;
55 u32 tmp, val_ch = 0;
56 CMP_TypeDef *comparator = COMPARATOR;
57 CMP_CHTypeDef *compChan = CMP_InitStruct->CMP_ChanCtrl;
58
59 CMP_Cmd(DISABLE);
60
61 tmp = comparator->COMP_INTR_CTRL;
62
63 for(i = 0; i < COMP_CH_NUM; i++) {
64 index = compChan[i].CMP_ChIndex;
65
66 val_ch |= (u32)(index << BIT_SHIFT_COMP_CHSW(i));
67
68 comparator->COMP_REF_CH[index] = (u32)((compChan[i].CMP_Ref0 << BIT_SHIFT_COMP_REF0) | \
69 (compChan[i].CMP_Ref1 << BIT_SHIFT_COMP_REF1));
70
71 if(compChan[i].CMP_WakeType & COMP_WK_ADC) {
72 tmp &= ~ BIT_MASK_COMP_WK_ADC_CTRL(index);
73 tmp |= (compChan[i].CMP_WakeADCCtrl << BIT_SHIFT_COMP_WK_ADC_CTRL(index)) | \
74 BIT_COMP_WK_ADC_EN(index);
75 }
76
77 if(compChan[i].CMP_WakeType & COMP_WK_SYS) {
78 tmp &= ~ BIT_MASK_COMP_WK_SYS_CTRL(index);
79 tmp |= (compChan[i].CMP_WakeSysCtrl << BIT_SHIFT_COMP_WK_SYS_CTRL(index)) | \
80 BIT_COMP_WK_SYS_EN(index);
81 }
82 }
83
84 comparator->COMP_INTR_CTRL = tmp;
85 comparator->COMP_CHSW_LIST = val_ch;
86
87 comparator->COMP_ANALOG |= BIT_SD_POSEDGE;
88 comparator->COMP_AUTO_SHUT = BIT_COMP_AUTO_SHUT;
89 }
90
91 /**
92 * @brief Enable or Disable the Comparator peripheral.
93 * @param NewState: new state of the Comparator peripheral.
94 * This parameter can be: ENABLE or DISABLE.
95 * @retval None
96 */
CMP_Cmd(u32 NewState)97 void CMP_Cmd(u32 NewState)
98 {
99 CMP_TypeDef *comparator = COMPARATOR;
100
101 if(NewState != DISABLE)
102 comparator->COMP_EN_TRIG |= BIT_COMP_ENABLE | BIT_COMP_EN_TRIG;
103 else
104 comparator->COMP_EN_TRIG &= ~(BIT_COMP_ENABLE | BIT_COMP_EN_TRIG);
105 }
106
107 /**
108 * @brief Indicate the Comparator is busy or not .
109 * @param None
110 * @retval busy status value:
111 * - 1: Busy
112 * - 0: Not Busy.
113 */
CMP_Busy(void)114 u32 CMP_Busy(void)
115 {
116 CMP_TypeDef *comparator = COMPARATOR;
117
118 if(comparator->COMP_BUSY_STS & BIT_COMP_BUSY_STS)
119 return 1;
120 else
121 return 0;
122 }
123 /**
124 * @brief Get Comparator Wakeup ADC/SYS Status Register .
125 * @param None
126 * @retval Current Comparator Wakeup ADC/SYS Status Register
127 */
CMP_GetISR(void)128 u32 CMP_GetISR(void)
129 {
130 CMP_TypeDef *comparator = COMPARATOR;
131
132 return comparator->COMP_WK_STS;
133 }
134
135 /**
136 * @brief Clear pending bit in Comparator Wakeup ADC/SYS Status Register.
137 * @param Comparator Wakeup ADC/SYS Status Register
138 * @retval None
139 */
CMP_INTClearPendingBit(u32 Cmp_IT)140 void CMP_INTClearPendingBit(u32 Cmp_IT)
141 {
142 CMP_TypeDef *comparator = COMPARATOR;
143
144 comparator->COMP_WK_STS = Cmp_IT;
145 }
146
147 /**
148 * @brief Get the comparison result .
149 * @param channel : the Comparator channel index
150 * @retval The comparison result of specified channel index. The return value can be:
151 * -0: when Vin < Vref0
152 * -2: when Vin > Vref0 & Vin < Vref1
153 * -3: when Vin > Vref1
154 */
CMP_GetCompStatus(u8 channel)155 u32 CMP_GetCompStatus(u8 channel)
156 {
157 CMP_TypeDef *comparator = COMPARATOR;
158 u32 value = (comparator->COMP_CH_STS & BIT_COMP_CH_STS(channel)) >> \
159 BIT_SHIFT_COMP_CH_STS(channel);
160
161 return value;
162 }
163
164 /**
165 * @brief Get the channel list index of the last used channel.
166 * @param None.
167 * @retval The channel list index of the last used channel. Not channel ID.
168 */
CMP_GetLastChan(void)169 u32 CMP_GetLastChan(void)
170 {
171 CMP_TypeDef *comparator = COMPARATOR;
172
173 return comparator->COMP_LAST_CH;
174 }
175
176 /**
177 * @brief Reset the channel switch to default state.
178 * @param None
179 * @retval None
180 */
CMP_ResetCSwList(void)181 void CMP_ResetCSwList(void)
182 {
183 CMP_TypeDef *comparator = COMPARATOR;
184
185 comparator->COMP_RST_LIST = BIT_COMP_RST_LIST;
186 comparator->COMP_RST_LIST = 0;
187 }
188
189 /**
190 * @brief Controls the automatic channel swtich enabled or disabled in Comparator Automatic Mode.
191 * @param NewState: can be one of the following value:
192 * @arg ENABLE: Enable the automatic channel switch.
193 * When setting this bit, an automatic channel switch starts from the first channel in the channel switch list.
194 * @arg DISABLE: Disable the automatic channel switch.
195 * If an automatic channel switch is in progess, writing 0 will terminate the automatic channel switch.
196 * @retval None.
197 */
CMP_AutoCSwCmd(u32 NewState)198 void CMP_AutoCSwCmd(u32 NewState)
199 {
200 CMP_TypeDef *comparator = COMPARATOR;
201
202 if(NewState != DISABLE) {
203 comparator->COMP_AUTOSW_EN = BIT_COMP_AUTOSW_EN;
204 comparator->COMP_EN_TRIG |= BIT_COMP_EN_TRIG;
205 } else {
206 comparator->COMP_AUTOSW_EN = 0;
207 comparator->COMP_EN_TRIG &= ~BIT_COMP_EN_TRIG;
208 }
209 }
210
211 /**
212 * @brief Initialize the trigger timer when in Comparator Timer-Trigger Mode.
213 * @param Tim_Idx: The timer index would be used to make the Comparator module do a comparison.
214 * @param PeriodMs: Indicate the period of trigger timer.
215 * @param NewState: can be one of the following value:
216 * @arg ENABLE: Enable the Comparator timer trigger mode.
217 * @arg DISABLE: Disable the Comparator timer trigger mode.
218 * @retval None.
219 * @note Used in Comparator Timer-Trigger Mode
220 */
CMP_TimerTrigCmd(u8 Tim_Idx,u32 PeriodMs,u32 NewState)221 void CMP_TimerTrigCmd(u8 Tim_Idx, u32 PeriodMs, u32 NewState)
222 {
223 CMP_TypeDef *comparator = COMPARATOR;
224 RTIM_TimeBaseInitTypeDef TIM_InitStruct;
225
226 comparator->COMP_EXT_TRIG_TIMER_SEL = Tim_Idx;
227
228 if(NewState != DISABLE) {
229 RTIM_TimeBaseStructInit(&TIM_InitStruct);
230 TIM_InitStruct.TIM_Idx = Tim_Idx;
231 TIM_InitStruct.TIM_Period = (PeriodMs *32768)/1000/2;//ms to tick
232
233 RTIM_TimeBaseInit(TIMx_LP[Tim_Idx], &TIM_InitStruct, TIMx_irq_LP[Tim_Idx], (IRQ_FUN)NULL, (u32)NULL);
234 RTIM_Cmd(TIMx_LP[Tim_Idx], ENABLE);
235
236 comparator->COMP_EXT_TRIG_CTRL = BIT_COMP_EXT_WK_TIMER;
237 } else {
238 RTIM_Cmd(TIMx_LP[Tim_Idx], DISABLE);
239 comparator->COMP_EXT_TRIG_CTRL = 0;
240 }
241 }
242
243 /******************* (C) COPYRIGHT 2016 Realtek Semiconductor *****END OF FILE****/
244