• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  * Copyright (c) 2022 Telink Semiconductor (Shanghai) Co., Ltd. ("TELINK")
3  * All rights reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *****************************************************************************/
18 #pragma once
19 
20 #include "analog.h"
21 
22 /**
23  * define input IO.
24  */
25 typedef enum {
26     LPC_INPUT_PB1 = 1,
27     LPC_INPUT_PB2 = 2,
28     LPC_INPUT_PB3 = 3,
29     LPC_INPUT_PB4 = 4,
30     LPC_INPUT_PB5 = 5,
31     LPC_INPUT_PB6 = 6,
32     LPC_INPUT_PB7 = 7,
33 } lpc_input_channel_e;
34 
35 /**
36  * define work mode.
37  */
38 typedef enum {
39     LPC_NORMAL = 0,
40     LPC_LOWPOWER,
41 } lpc_mode_e;
42 
43 /**
44  * define Reference voltage.
45  */
46 typedef enum {
47     LPC_REF_974MV = 1,
48     LPC_REF_923MV = 2,
49     LPC_REF_872MV = 3,
50     LPC_REF_820MV = 4,
51     LPC_REF_PB0 = 5,
52     LPC_REF_PB3 = 6,
53 } lpc_reference_e;
54 
55 /**
56  * define scale.
57  */
58 typedef enum {
59     LPC_SCALING_PER25 = 0,
60     LPC_SCALING_PER50 = 1,
61     LPC_SCALING_PER75 = 2,
62     LPC_SCALING_PER100 = 3,
63 } lpc_scaling_e;
64 
65 /**
66  * @brief		This function servers to powers down low power comparator.
67  * @return		none.
68  */
lpc_power_down(void)69 static inline void lpc_power_down(void)
70 {
71     analog_write_reg8(0x07, (analog_read_reg8(0x07)) | 0x08);
72 }
73 
74 /**
75  * @brief		This function servers to power on low power comparator.
76  * @return		none.
77  */
lpc_power_on(void)78 static inline void lpc_power_on(void)
79 {
80     analog_write_reg8(0x06, analog_read_reg8(0x06) & 0xfd);
81 }
82 
83 /**
84  * @brief		This function selects input channel for low power comparator.
85  * @param[in]	pin		- selected input channel.Input derived from external PortB(PB<1>~PB<7>).
86  * @return		none.
87  */
lpc_set_input_chn(lpc_input_channel_e pin)88 static inline void lpc_set_input_chn(lpc_input_channel_e pin)
89 {
90     analog_write_reg8(0x0d, (analog_read_reg8(0x0d) & 0xf8) | pin);
91 }
92 
93 /**
94  * @brief		This function serves to set scaling_coefficient for low power comparator.
95  * @param[in]	divider	- selected scaling coefficient.(%25,%50,%75,%100)
96  * @return		none.
97  */
lpc_set_scaling_coeff(lpc_scaling_e divider)98 static inline void lpc_set_scaling_coeff(lpc_scaling_e divider)
99 {
100     analog_write_reg8(0x0b, (analog_read_reg8(0x0b) & 0xcf) | (divider << 4));
101 }
102 
103 /**
104  * @brief		This function serves to get the comparison results.if Vin>Vref 0x88[6]=0,else 0x88[6]=1.
105  * @return		comparison results.
106  */
lpc_get_result(void)107 static inline unsigned char lpc_get_result(void)
108 {
109     return ((analog_read_reg8(0x88) & 0x40) >> 6);
110 }
111 
112 /**
113  * @brief		This function selects input reference voltage for low power comparator.
114  * @param[in]	mode	- lower power comparator working mode includes normal mode and low power mode.
115  * @param[in]	ref		- selected input reference voltage.
116  * @return		none.
117  */
118 void lpc_set_input_ref(lpc_mode_e mode, lpc_reference_e ref);
119