1 #include <stdio.h>
2 #include <stdlib.h>
3
4 #include "pca9544.h"
5 #include "pca9544_i2c_drv.h"
6
7 static PCA9544_DEV_CFG_T g_pca9544_dev_cfg;
8 static i2c_dev_t g_pca9544_i2c_cfg;
9
10 /**********************************************************
11 * @fun PCA9544_init
12 * @breif pca9544 initialization
13 * @param i2c:the pointer for i2c configuration
14 * @param dev_cfg: the pointer for dev configuration
15 * @rtn
16 *********************************************************/
pca9544_init(i2c_dev_t * i2c,PCA9544_DEV_CFG_T * dev_cfg)17 int pca9544_init(i2c_dev_t *i2c, PCA9544_DEV_CFG_T *dev_cfg)
18 {
19 int ret = 0;
20
21 if (i2c == NULL) {
22 printf("PCA9544 i2c is null\n");
23 return -1;
24 }
25
26 memset(&g_pca9544_i2c_cfg, 0, sizeof(i2c_dev_t));
27 memcpy(&g_pca9544_i2c_cfg, i2c, sizeof(i2c_dev_t));
28
29 /*ret = hal_i2c_init(&g_pca9544_i2c_cfg);*/
30 ret = pca9544_i2c_init(&g_pca9544_i2c_cfg);
31 if (ret) {
32 printf("Host I2C open failed\n");
33 /*hal_i2c_finalize(&g_pca9544_i2c_cfg);*/
34 pca9544_i2c_finalize(&g_pca9544_i2c_cfg);
35
36 return -1;
37 }
38
39 if (dev_cfg == NULL) {
40 printf("PCA9544 cfg is null\n");
41 return -1;
42 }
43
44 memset(&g_pca9544_dev_cfg, 0, sizeof(PCA9544_DEV_CFG_T));
45 memcpy(&g_pca9544_dev_cfg, dev_cfg, sizeof(PCA9544_DEV_CFG_T));
46
47 printf("PCA9544 cfg is successful\n");
48 return ret;
49 }
50
51 /**********************************************************
52 * @fun pca9544_deinit
53 * @breif pca9544 deinitialization
54 * @param none
55 * @rtn none
56 *********************************************************/
pca9544_deinit()57 void pca9544_deinit()
58 {
59 memset(&g_pca9544_i2c_cfg, 0, sizeof(i2c_dev_t));
60 memset(&g_pca9544_dev_cfg, 0, sizeof(PCA9544_DEV_CFG_T));
61 /*hal_i2c_finalize(&g_pca9544_i2c_cfg);*/
62 pca9544_i2c_finalize(&g_pca9544_i2c_cfg);
63 }
64
65 /**********************************************************
66 * @fun pca9544_set_chan
67 * @breif pca9544 channel set
68 * @param chan: the object channel
69 * @rtn ret
70 *********************************************************/
pca9544_set_chan(PCA9544_CH_E chan)71 int pca9544_set_chan(PCA9544_CH_E chan)
72 {
73
74 int ret = 0;
75 uint16_t dev_addr = g_pca9544_dev_cfg.dev_addr;
76 uint8_t regval = 0;
77
78 if ((chan < PCA9544_CHO) || (chan > PCA9544_CH3)) {
79 printf("the setting channel is not valid\n");
80 return -1;
81 }
82 g_pca9544_dev_cfg.pca9544_ch = chan;
83 regval = g_pca9544_dev_cfg.pca9544_ch;
84
85 ret = pca9544_write_reg(dev_addr, regval);
86 if (ret) {
87 printf("set PCA9544 channel failed\n");
88 return -1;
89 }
90
91 return ret;
92 }
93
94 /**********************************************************
95 * @fun pca9544_read_chan
96 * @breif pca9544 channel reading
97 * @param none
98 * @rtn chan: the current selected channel of pca9544
99 *********************************************************/
pca9544_read_chan()100 PCA9544_CH_E pca9544_read_chan()
101 {
102 PCA9544_CH_E chan = g_pca9544_dev_cfg.pca9544_ch;
103
104 return chan;
105 }
106
107 /**********************************************************
108 * @fun pca9544_clean_chan
109 * @breif pca9544 channel clean
110 * @param none
111 * @rtn ret
112 *********************************************************/
pca9544_clean_chan()113 int pca9544_clean_chan()
114 {
115
116 int ret = 0;
117 uint16_t dev_addr = g_pca9544_dev_cfg.dev_addr;
118 g_pca9544_dev_cfg.pca9544_ch = PCA9544_CHAN_CLEAN;
119 uint8_t regval = g_pca9544_dev_cfg.pca9544_ch;
120
121 ret = pca9544_write_reg(dev_addr, regval);
122 if (ret) {
123 printf("clean PCA9544 channel failed\n");
124 return -1;
125 }
126
127 return ret;
128 }
129
130 /**********************************************************
131 * @fun pca9544_write_reg
132 * @breif pca9544 register writing operation
133 * @param dev_addr: i2c device address
134 * @param regval: register value
135 * @rtn ret
136 *********************************************************/
pca9544_write_reg(uint16_t dev_addr,uint8_t regval)137 int pca9544_write_reg(uint16_t dev_addr, uint8_t regval)
138 {
139
140 int ret = 0;
141 i2c_dev_t *i2c = &g_pca9544_i2c_cfg;
142 uint32_t timeout = 1000;
143
144 /*ret = hal_i2c_master_send(i2c, dev_addr, ®val, sizeof(regval),
145 * timeout);*/
146 ret = pca9544_i2c_master_send(i2c, dev_addr, ®val, sizeof(regval), timeout);
147 if (ret) {
148 printf("write PCA9544 reg failed\n");
149 return -1;
150 }
151
152 return ret;
153 }
154
155 /**********************************************************
156 * @fun pca9544_read_reg
157 * @breif pca9544 register reading operation
158 * @param dev_addr: i2c device address
159 * @param regval: register value
160 * @rtn ret
161 *********************************************************/
pca9544_read_reg(uint16_t dev_addr,uint8_t * data)162 int pca9544_read_reg(uint16_t dev_addr, uint8_t *data)
163 {
164 int ret = 0;
165 i2c_dev_t *i2c = &g_pca9544_i2c_cfg;
166 uint16_t len = PCA9544_CONFIG_REG_LEN;
167 uint32_t timeout = 1000;
168
169 /*ret = hal_i2c_master_recv(i2c, dev_addr, data, len, timeout);*/
170 ret = pca9544_i2c_master_recv(i2c, dev_addr, data, len, timeout);
171 if (ret) {
172 printf("Read PCA9544 reg failed\n");
173 return -1;
174 }
175
176 return ret;
177 }
178
179 /**********************************************************
180 * @fun pca9544_raw_data_write
181 * @breif write raw data to the selected channel
182 * @param sub_addr: the sub dev addr for
183 * the selected pca9544 channel
184 * @param reg_addr: the sub dev register addr for
185 * the selected pca9544 channel
186 * @param size: register size
187 * @param data: the pointer for the writing data
188 * @param len: the data length
189 * @rtn ret
190 *********************************************************/
pca9544_raw_data_write(uint16_t subdev_addr,uint16_t reg_addr,uint16_t size,uint8_t * data,uint16_t len)191 int pca9544_raw_data_write(uint16_t subdev_addr, uint16_t reg_addr, uint16_t size, uint8_t *data, uint16_t len)
192 {
193 int ret = 0;
194 i2c_dev_t *i2c = &g_pca9544_i2c_cfg;
195 g_pca9544_dev_cfg.subdev_addr = subdev_addr;
196 g_pca9544_dev_cfg.reg_addr = reg_addr;
197 uint16_t dev_addr = g_pca9544_dev_cfg.subdev_addr;
198 uint16_t mem_addr = g_pca9544_dev_cfg.reg_addr;
199 uint16_t mem_addr_size = size;
200 uint32_t timeout = 1000;
201
202 /*ret = hal_i2c_mem_write(i2c, dev_addr, mem_addr, mem_addr_size, &data[0],
203 * len, timeout);*/
204 ret = pca9544_i2c_mem_write(i2c, dev_addr, mem_addr, mem_addr_size, &data[0], len, timeout);
205 if (ret) {
206 printf("PCA9544 sub dev raw data writing failed\n");
207 return -1;
208 }
209
210 return ret;
211 }
212
213 /**********************************************************
214 * @fun pca9544_raw_data_read
215 * @breif read raw data to the selected channel
216 * @param sub_addr: the sub dev addr for
217 * the selected pca9544 channel
218 * @param reg_addr: the sub dev register addr for
219 * the selected pca9544 channel
220 * @param size: register size
221 * @param data: the pointer for the reading data
222 * @param len: the data length
223 * @rtn ret
224 *********************************************************/
pca9544_raw_data_read(uint16_t subdev_addr,uint16_t reg_addr,uint16_t size,uint8_t * data,uint16_t len)225 int pca9544_raw_data_read(uint16_t subdev_addr, uint16_t reg_addr, uint16_t size, uint8_t *data, uint16_t len)
226 {
227 int ret = 0;
228 i2c_dev_t *i2c = &g_pca9544_i2c_cfg;
229 g_pca9544_dev_cfg.subdev_addr = subdev_addr;
230 g_pca9544_dev_cfg.reg_addr = reg_addr;
231 uint16_t dev_addr = g_pca9544_dev_cfg.subdev_addr;
232 uint16_t mem_addr = g_pca9544_dev_cfg.reg_addr;
233 uint16_t mem_addr_size = size;
234 uint32_t timeout = 1000;
235
236 /*ret = hal_i2c_mem_read(i2c, dev_addr, mem_addr, mem_addr_size, &data[0],
237 * len, timeout);*/
238 ret = pca9544_i2c_mem_read(i2c, dev_addr, mem_addr, mem_addr_size, &data[0], len, timeout);
239 if (ret) {
240 printf("PCA9544 sub dev raw data reading failed\n");
241 return -1;
242 }
243
244 return ret;
245 }
246
pca954x_irq_handler(uint16_t dev_addr,uint8_t * data)247 int pca954x_irq_handler(uint16_t dev_addr, uint8_t *data)
248 {
249 int ret = 0;
250 uint8_t irq_state = 0;
251
252 ret = pca9544_get_irq_state(dev_addr, data);
253 return ret;
254 }
255
pca9544_get_irq_state(uint16_t dev_addr,uint8_t * regval)256 int pca9544_get_irq_state(uint16_t dev_addr, uint8_t *regval)
257 {
258
259 int ret = 0;
260
261 ret = pca9544_read_reg(dev_addr, ®val);
262 if (ret) {
263 printf("get PCA9544 irq state failed\n");
264 return -1;
265 } else {
266 regval[0] = regval[0] | PCA9544_IRQ_MASK;
267 regval[0] = regval[0] >> 4;
268
269 printf("PCA9544 IRQ STATUS is %d\n", regval[0]);
270 }
271
272 return ret;
273 }
274