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 
19 /********************************************************************************************************
20  * @file	lpc.h
21  *
22  * @brief	This is the header file for B91
23  *
24  * @author	Driver Group
25  *
26  *******************************************************************************************************/
27 #pragma once
28 
29 #include "analog.h"
30 
31 /**
32  * define input IO.
33  */
34 typedef enum{
35 	LPC_INPUT_PB1 = 1,
36 	LPC_INPUT_PB2 = 2,
37 	LPC_INPUT_PB3 = 3,
38 	LPC_INPUT_PB4 = 4,
39 	LPC_INPUT_PB5 = 5,
40 	LPC_INPUT_PB6 = 6,
41 	LPC_INPUT_PB7 = 7,
42 }lpc_input_channel_e;
43 
44 /**
45  * define work mode.
46  */
47 typedef enum{
48 	LPC_NORMAL = 0,
49 	LPC_LOWPOWER,
50 }lpc_mode_e;
51 
52 /**
53  * define Reference voltage.
54  */
55 typedef enum{
56 	LPC_REF_974MV  = 1,
57 	LPC_REF_923MV  = 2,
58 	LPC_REF_872MV  = 3,
59 	LPC_REF_820MV  = 4,
60 	LPC_REF_PB0    = 5,
61 	LPC_REF_PB3    = 6,
62 }lpc_reference_e;
63 
64 /**
65  * define scale.
66  */
67 typedef enum{
68 	LPC_SCALING_PER25  = 0,
69 	LPC_SCALING_PER50  = 1,
70 	LPC_SCALING_PER75  = 2,
71 	LPC_SCALING_PER100 = 3,
72 }lpc_scaling_e;
73 
74 /**
75  * @brief		This function servers to powers down low power comparator.
76  * @return		none.
77  */
lpc_power_down(void)78 static inline void lpc_power_down(void)
79 {
80 	analog_write_reg8(0x07,(analog_read_reg8(0x07))|0x08);
81 }
82 
83 /**
84  * @brief		This function servers to power on low power comparator.
85  * @return		none.
86  */
lpc_power_on(void)87 static inline void lpc_power_on(void)
88 {
89 	analog_write_reg8(0x06,analog_read_reg8(0x06) & 0xfd);
90 }
91 
92 /**
93  * @brief		This function selects input channel for low power comparator.
94  * @param[in]	pin		- selected input channel.Input derived from external PortB(PB<1>~PB<7>).
95  * @return		none.
96  */
lpc_set_input_chn(lpc_input_channel_e pin)97 static inline void lpc_set_input_chn(lpc_input_channel_e pin)
98 {
99 	analog_write_reg8(0x0d,(analog_read_reg8(0x0d) & 0xf8) | pin);
100 }
101 
102 /**
103  * @brief		This function serves to set scaling_coefficient for low power comparator.
104  * @param[in]	divider	- selected scaling coefficient.(%25,%50,%75,%100)
105  * @return		none.
106  */
lpc_set_scaling_coeff(lpc_scaling_e divider)107 static inline void lpc_set_scaling_coeff(lpc_scaling_e divider)
108 {
109 	analog_write_reg8(0x0b,(analog_read_reg8(0x0b)&0xcf)|(divider<<4));
110 }
111 
112 /**
113  * @brief		This function serves to get the comparison results.if Vin>Vref 0x88[6]=0,else 0x88[6]=1.
114  * @return		comparison results.
115  */
lpc_get_result(void)116 static inline unsigned char lpc_get_result(void)
117 {
118 	return ((analog_read_reg8(0x88)&0x40)>>6);
119 }
120 
121 /**
122  * @brief		This function selects input reference voltage for low power comparator.
123  * @param[in]	mode	- lower power comparator working mode includes normal mode and low power mode.
124  * @param[in]	ref		- selected input reference voltage.
125  * @return		none.
126  */
127 void lpc_set_input_ref(lpc_mode_e mode, lpc_reference_e ref);
128 
129 
130