LibDriver PCF8575
Loading...
Searching...
No Matches
driver_pcf8575.c
Go to the documentation of this file.
1
36
37#include "driver_pcf8575.h"
38
42#define CHIP_NAME "Texas Instruments PCF8575"
43#define MANUFACTURER_NAME "Texas Instruments"
44#define SUPPLY_VOLTAGE_MIN 2.5f
45#define SUPPLY_VOLTAGE_MAX 5.5f
46#define MAX_CURRENT 100.0f
47#define TEMPERATURE_MIN -40.0f
48#define TEMPERATURE_MAX 85.0f
49#define DRIVER_VERSION 1000
50
61{
62 if (handle == NULL) /* check handle */
63 {
64 return 2; /* return error */
65 }
66
67 handle->iic_addr = 0x40; /* set iic addr */
68 handle->iic_addr |= (uint8_t)(addr_pin << 1); /* set iic address */
69
70 return 0; /* success return 0 */
71}
72
83{
84 if (handle == NULL) /* check handle */
85 {
86 return 2; /* return error */
87 }
88
89 *addr_pin = (pcf8575_address_t)((handle->iic_addr & (~0x40)) >> 1); /*get iic address */
90
91 return 0; /* success return 0 */
92}
93
106{
107 uint8_t buf[2];
108
109 if (handle == NULL) /* check handle */
110 {
111 return 2; /* return error */
112 }
113 if (handle->debug_print == NULL) /* check debug_print */
114 {
115 return 3; /* return error */
116 }
117 if (handle->iic_init == NULL) /* check iic_init */
118 {
119 handle->debug_print("pcf8575: iic_init is null.\n"); /* iic_init is null */
120
121 return 3; /* return error */
122 }
123 if (handle->iic_deinit == NULL) /* check iic_deinit */
124 {
125 handle->debug_print("pcf8575: iic_deinit is null.\n"); /* iic_deinit is null */
126
127 return 3; /* return error */
128 }
129 if (handle->iic_read_cmd == NULL) /* check iic_read_cmd */
130 {
131 handle->debug_print("pcf8575: iic_read_cmd is null.\n"); /* iic_read_cmd is null */
132
133 return 3; /* return error */
134 }
135 if (handle->iic_write_cmd == NULL) /* check iic_write_cmd */
136 {
137 handle->debug_print("pcf8575: iic_write_cmd is null.\n"); /* iic_write_cmd is null */
138
139 return 3; /* return error */
140 }
141 if (handle->delay_ms == NULL) /* check delay_ms */
142 {
143 handle->debug_print("pcf8575: delay_ms is null.\n"); /* delay_ms is null */
144
145 return 3; /* return error */
146 }
147
148 if (handle->iic_init() != 0) /* iic init */
149 {
150 handle->debug_print("pcf8575: iic init failed.\n"); /* iic init failed */
151
152 return 1; /* return error */
153 }
154 buf[0] = 0xFF; /* set 0xFF */
155 buf[1] = 0xFF; /* set 0xFF */
156 if (handle->iic_write_cmd(handle->iic_addr,
157 (uint8_t *)buf, 2) != 0) /* write all pin high level */
158 {
159 handle->debug_print("pcf8575: iic write failed.\n"); /* iic write failed */
160 (void)handle->iic_deinit(); /* iic deinit */
161
162 return 4; /* return error */
163 }
164 handle->inited = 1; /* flag finish initialization */
165
166 return 0; /* success return 0 */
167}
168
180{
181 uint8_t res;
182
183 if (handle == NULL) /* check handle */
184 {
185 return 2; /* return error */
186 }
187 if (handle->inited != 1) /* check handle initialization */
188 {
189 return 3; /* return error */
190 }
191
192 res = handle->iic_deinit(); /* iic deinit */
193 if (res != 0) /* check error */
194 {
195 handle->debug_print("pcf8575: iic deinit failed.\n"); /* iic deinit failed */
196
197 return 1; /* return error */
198 }
199 handle->inited = 0; /* flag closed */
200
201 return 0; /* success return 0 */
202}
203
217{
218 uint8_t res;
219 uint8_t buf[2];
220 uint16_t prev;
221
222 if (handle == NULL) /* check handle */
223 {
224 return 2; /* return error */
225 }
226 if (handle->inited != 1) /* check handle initialization */
227 {
228 return 3; /* return error */
229 }
230
231 res = handle->iic_read_cmd(handle->iic_addr, (uint8_t *)buf, 2); /* read data */
232 if (res != 0) /* check error */
233 {
234 handle->debug_print("pcf8575: iic read failed.\n"); /* iic read failed */
235
236 return 1; /* return error */
237 }
238 prev = (((uint16_t)buf[0]) << 8) | buf[1]; /* set buffer */
239 *level = (pcf8575_pin_level_t)((prev >> pin) & 0x01); /* get level */
240
241 return 0; /* success return 0 */
242}
243
257{
258 uint8_t res;
259 uint8_t buf[2];
260 uint16_t prev;
261
262 if (handle == NULL) /* check handle */
263 {
264 return 2; /* return error */
265 }
266 if (handle->inited != 1) /* check handle initialization */
267 {
268 return 3; /* return error */
269 }
270
271 res = handle->iic_read_cmd(handle->iic_addr, (uint8_t *)buf, 2); /* read data */
272 if (res != 0) /* check error */
273 {
274 handle->debug_print("pcf8575: iic read failed.\n"); /* iic read failed */
275
276 return 1; /* return error */
277 }
278 prev = (((uint16_t)buf[0]) << 8) | buf[1]; /* set buffer */
279 prev &= ~(1 << pin); /* clear 0 */
280 prev |= level << pin; /* set data */
281 buf[0] = (prev >> 8) & 0xFF; /* set buffer 0 */
282 buf[1] = (prev >> 0) & 0xFF; /* set buffer 1 */
283 res = handle->iic_write_cmd(handle->iic_addr, (uint8_t *)buf, 2); /* write data */
284 if (res != 0) /* check error */
285 {
286 handle->debug_print("pcf8575: iic write failed.\n"); /* iic write failed */
287
288 return 1; /* return error */
289 }
290
291 return 0; /* success return 0 */
292}
293
306uint8_t pcf8575_set_reg(pcf8575_handle_t *handle, uint8_t *buf, uint16_t len)
307{
308 if (handle == NULL) /* check handle */
309 {
310 return 2; /* return error */
311 }
312 if (handle->inited != 1) /* check handle initialization */
313 {
314 return 3; /* return error */
315 }
316
317 if (handle->iic_write_cmd(handle->iic_addr, buf, len) != 0) /* write command */
318 {
319 return 1; /* return error */
320 }
321 else
322 {
323 return 0; /* success return 0 */
324 }
325}
326
339uint8_t pcf8575_get_reg(pcf8575_handle_t *handle, uint8_t *buf, uint16_t len)
340{
341 if (handle == NULL) /* check handle */
342 {
343 return 2; /* return error */
344 }
345 if (handle->inited != 1) /* check handle initialization */
346 {
347 return 3; /* return error */
348 }
349
350 if (handle->iic_read_cmd(handle->iic_addr, buf, len) != 0) /* read command */
351 {
352 return 1; /* return error */
353 }
354 else
355 {
356 return 0; /* success return 0 */
357 }
358}
359
369{
370 if (info == NULL) /* check handle */
371 {
372 return 2; /* return error */
373 }
374
375 memset(info, 0, sizeof(pcf8575_info_t)); /* initialize pcf8575 info structure */
376 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
377 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
378 strncpy(info->interface, "IIC", 8); /* copy interface name */
379 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
380 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
381 info->max_current_ma = MAX_CURRENT; /* set maximum current */
382 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
383 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
384 info->driver_version = DRIVER_VERSION; /* set driver version */
385
386 return 0; /* success return 0 */
387}
#define MAX_CURRENT
#define SUPPLY_VOLTAGE_MAX
#define TEMPERATURE_MAX
#define MANUFACTURER_NAME
#define TEMPERATURE_MIN
#define SUPPLY_VOLTAGE_MIN
#define CHIP_NAME
chip information definition
#define DRIVER_VERSION
driver pcf8575 header file
uint8_t pcf8575_deinit(pcf8575_handle_t *handle)
close the chip
struct pcf8575_handle_s pcf8575_handle_t
pcf8575 handle structure definition
uint8_t pcf8575_set_addr_pin(pcf8575_handle_t *handle, pcf8575_address_t addr_pin)
set the address pin
uint8_t pcf8575_write(pcf8575_handle_t *handle, pcf8575_pin_t pin, pcf8575_pin_level_t level)
write the pin
uint8_t pcf8575_read(pcf8575_handle_t *handle, pcf8575_pin_t pin, pcf8575_pin_level_t *level)
read the pin
uint8_t pcf8575_init(pcf8575_handle_t *handle)
initialize the chip
pcf8575_address_t
pcf8575 address enumeration definition
pcf8575_pin_t
pcf8575 pin enumeration definition
pcf8575_pin_level_t
pcf8575 pin enumeration definition
struct pcf8575_info_s pcf8575_info_t
pcf8575 information structure definition
uint8_t pcf8575_info(pcf8575_info_t *info)
get chip's information
uint8_t pcf8575_get_addr_pin(pcf8575_handle_t *handle, pcf8575_address_t *addr_pin)
get the address pin
uint8_t pcf8575_set_reg(pcf8575_handle_t *handle, uint8_t *buf, uint16_t len)
set the chip register
uint8_t pcf8575_get_reg(pcf8575_handle_t *handle, uint8_t *buf, uint16_t len)
get the chip register
void(* delay_ms)(uint32_t ms)
void(* debug_print)(const char *const fmt,...)
uint8_t(* iic_init)(void)
uint8_t(* iic_read_cmd)(uint8_t addr, uint8_t *buf, uint16_t len)
uint8_t(* iic_deinit)(void)
uint8_t(* iic_write_cmd)(uint8_t addr, uint8_t *buf, uint16_t len)
uint32_t driver_version
char manufacturer_name[32]