1 /*
2  * Copyright (C) 2015-2020 Alibaba Group Holding Limited
3  */
4 
5 #ifndef __DRV_AP3216C_H__
6 #define __DRV_AP3216C_H__
7 
8 #include "aos/hal/i2c.h"
9 #include "hal_iomux_haas1000.h"
10 
11 // 中断引脚定义
12 #define AP_INT_GPIO_PIN HAL_IOMUX_PIN_P3_7
13 
14 #define AP_INT_Read()                                                          \
15     HAL_GPIO_ReadPin(AP_INT_GPIO_PORT, AP_INT_GPIO_PIN) // 读中断引脚的状态
16 
17 void AP_INT_Config(void);
18 
19 enum ap3216c_mode_value {
20     AP3216C_MODE_POWER_DOWN,      // Power down (Default)
21     AP3216C_MODE_ALS,             // ALS function active
22     AP3216C_MODE_PS,              // PS+IR function active
23     AP3216C_MODE_ALS_AND_PS,      // ALS and PS+IR functions active
24     AP3216C_MODE_SW_RESET,        // SW reset
25     AP3216C_MODE_ALS_ONCE,        // ALS function once
26     AP3216C_MODE_PS_ONCE,         // PS+IR function once
27     AP3216C_MODE_ALS_AND_PS_ONCE, // ALS and PS+IR functions once
28 };
29 
30 enum ap3216c_int_clear_manner {
31     AP3216C_INT_CLEAR_MANNER_BY_READING, // INT is automatically cleared by
32     // reading data registers(Default)
33     AP3216C_ALS_CLEAR_MANNER_BY_SOFTWARE, // Software clear after writing 1 into
34     // address 0x01 each bit
35 };
36 
37 enum als_range {
38     AP3216C_ALS_RANGE_20661, // Resolution = 0.35 lux/count(default).
39     AP3216C_ALS_RANGE_5162,  // Resolution = 0.0788 lux/count.
40     AP3216C_ALS_RANGE_1291,  // Resolution = 0.0197 lux/count.
41     AP3216C_ALS_RANGE_323,   // Resolution = 0.0049 lux/count
42 };
43 typedef enum als_range als_range_t;
44 
45 enum ps_gain {
46     AP3216C_PS_GAIN1, // detection distance *1.
47     AP3216C_PS_GAIN2, // detection distance *2 (default).
48     AP3216C_PS_GAIN4, // detection distance *4.
49     AP3216C_PS_GAIN8, // detection distance *8.
50 };
51 typedef enum als_gain ps_gain_t;
52 
53 enum ap3216c_cmd {
54     AP3216C_SYSTEM_MODE,         // system  Configuration(Default : 000)
55     AP3216C_INT_PARAM,           // INT Clear Manner(Default : 0)
56     AP3216C_ALS_RANGE,           // ALS dynamic range(Default : 00)
57     AP3216C_ALS_PERSIST,         // ALS persist(Default : 0000)
58     AP3216C_ALS_CALIBRATION,     // ALS window loss calibration(Default : 0x40)
59     AP3216C_ALS_LOW_THRESHOLD_L, // Lower byte of low interrupt threshold for
60     // ALS(Default : 0x00)
61     AP3216C_ALS_LOW_THRESHOLD_H, // Higher byte of low interrupt threshold for
62     // ALS(Default : 0x00)
63     AP3216C_ALS_HIGH_THRESHOLD_L, // Lower byte of high interrupt threshold for
64     // ALS (Default : 0xFF)
65     AP3216C_ALS_HIGH_THRESHOLD_H, // Higher byte of high interrupt threshold for
66     // ALS(Default : 0xFF)
67     AP3216C_PS_INTEGRATED_TIME, // PS or IR Integrated time select(Default :
68     // 0000)
69     AP3216C_PS_GAIN,             // PS gain (Default : 01)
70     AP3216C_PS_PERSIST,          // Interrupt filter(Default : 01)
71     AP3216C_PS_LED_CONTROL,      // LED pulse(Default : 01)
72     AP3216C_PS_LED_DRIVER_RATIO, // LED driver ratio(Default : 11)
73     AP3216C_PS_INT_MODE,         // PS INT Mode(Default : 0x01)
74     AP3216C_PS_MEAN_TIME,        // PS mean time(Default : 0x00)
75     AP3216C_PS_WAITING_TIME,     // PS LED Waiting(Default : 0x00)
76     AP3216C_PS_CALIBRATION_L,    // PS Calibration L(Default : 0x00)
77     AP3216C_PS_CALIBRATION_H,    // PS Calibration H(Default : 0x00)
78     AP3216C_PS_LOW_THRESHOLD_L,  // PS Low Threshold L(Default :0x00)
79     AP3216C_PS_LOW_THRESHOLD_H,  // PS Low Threshold H(Default :0x00)
80     AP3216C_PS_HIGH_THRESHOLD_L, // PS high Threshold L(Default :0xff)
81     AP3216C_PS_HIGH_THRESHOLD_H, // PS high Threshold H(Default :0xff)
82 };
83 typedef enum ap3216c_cmd ap3216c_cmd_t;
84 
85 /* intrrupt parameters of ap3216c on ps or als */
86 struct ap3216c_threshold {
87     uint16_t min; /* als 16 bits, ps 10 bits available(low byte :0-1 bit and
88                      High Byte :8-15 bit ) */
89     uint16_t max; /* als 16 bits, ps 10 bits available(low byte :0-1 bit and
90                      High Byte :8-15 bit ) */
91     uint8_t noises_time; /* filter special noises trigger interrupt */
92 };
93 typedef struct ap3216c_threshold ap3216c_threshold_t;
94 
95 uint8_t ap3216c_get_IntStatus(void);
96 
97 /**
98  * This function initializes ap3216c registered device driver
99  *
100  * @param no
101  *
102  * @return no
103  */
104 void ap3216c_init(void);
105 
106 /**
107  * This function reads temperature by ap3216c sensor measurement
108  *
109  * @param no
110  *
111  * @return the ambient light converted to float data.
112  */
113 uint16_t ap3216c_read_ambient_light(void);
114 
115 /**
116  * This function reads temperature by ap3216c sensor measurement
117  *
118  * @param no
119  *
120  * @return the proximity data.
121  */
122 uint16_t ap3216c_read_ps_data(void);
123 
124 uint16_t ap3216c_read_ir_data(void);
125 
126 /**
127  * This function sets parameter of ap3216c sensor
128  *
129  * @param cmd the parameter cmd of device
130  * @param value for setting value in cmd register
131  *
132  * @return the setting parameter status,RT_EOK reprensents setting successfully.
133  */
134 void ap3216c_set_param(ap3216c_cmd_t cmd, uint8_t value);
135 
136 /**
137  * This function gets parameter of ap3216c sensor
138  *
139  * @param cmd the parameter cmd of device
140  * @param value to get value in cmd register
141  *
142  * @return the getting parameter status,RT_EOK reprensents getting successfully.
143  */
144 void ap3216c_get_param(ap3216c_cmd_t cmd, uint8_t *value);
145 
146 #endif /*__DRV_AP3216C_H__ */
147