LibDriver PCF8574
Loading...
Searching...
No Matches
driver_pcf8574.c
Go to the documentation of this file.
1
36
37#include "driver_pcf8574.h"
38
42#define CHIP_NAME "NXP PCF8574"
43#define MANUFACTURER_NAME "NXP"
44#define SUPPLY_VOLTAGE_MIN 2.5f
45#define SUPPLY_VOLTAGE_MAX 6.0f
46#define MAX_CURRENT 80.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 = (pcf8574_address_t)((handle->iic_addr & (~0x40)) >> 1); /*get iic address */
90
91 return 0; /* success return 0 */
92}
93
106{
107 uint8_t data;
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("pcf8574: 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("pcf8574: 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("pcf8574: 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("pcf8574: 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("pcf8574: 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("pcf8574: iic init failed.\n"); /* iic init failed */
151
152 return 1; /* return error */
153 }
154 data = 0xFF; /* set 0xFF */
155 if (handle->iic_write_cmd(handle->iic_addr,
156 (uint8_t *)&data, 1) != 0) /* write all pin high level */
157 {
158 handle->debug_print("pcf8574: iic write failed.\n"); /* iic write failed */
159 (void)handle->iic_deinit(); /* iic deinit */
160
161 return 4; /* return error */
162 }
163 handle->output_shadow = 0xFF; /* set 0xFF */
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("pcf8574: 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 data;
220
221 if (handle == NULL) /* check handle */
222 {
223 return 2; /* return error */
224 }
225 if (handle->inited != 1) /* check handle initialization */
226 {
227 return 3; /* return error */
228 }
229
230 res = handle->iic_read_cmd(handle->iic_addr, (uint8_t *)&data, 1); /* read data */
231 if (res != 0) /* check error */
232 {
233 handle->debug_print("pcf8574: iic read failed.\n"); /* iic read failed */
234
235 return 1; /* return error */
236 }
237 *level = (pcf8574_pin_level_t)((data >> pin) & 0x01); /* get level */
238
239 return 0; /* success return 0 */
240}
241
255{
256 uint8_t res;
257
258 if (handle == NULL) /* check handle */
259 {
260 return 2; /* return error */
261 }
262 if (handle->inited != 1) /* check handle initialization */
263 {
264 return 3; /* return error */
265 }
266
267 handle->output_shadow &= ~(1 << pin); /* clear 0 */
268 handle->output_shadow |= level << pin; /* set data */
269 res = handle->iic_write_cmd(handle->iic_addr, (uint8_t *)&handle->output_shadow, 1); /* write data */
270 if (res != 0) /* check error */
271 {
272 handle->debug_print("pcf8574: iic write failed.\n"); /* iic write failed */
273
274 return 1; /* return error */
275 }
276
277 return 0; /* success return 0 */
278}
279
292uint8_t pcf8574_set_reg(pcf8574_handle_t *handle, uint8_t *buf, uint16_t len)
293{
294 if (handle == NULL) /* check handle */
295 {
296 return 2; /* return error */
297 }
298 if (handle->inited != 1) /* check handle initialization */
299 {
300 return 3; /* return error */
301 }
302
303 if (handle->iic_write_cmd(handle->iic_addr, buf, len) != 0) /* write command */
304 {
305 return 1; /* return error */
306 }
307 else
308 {
309 return 0; /* success return 0 */
310 }
311}
312
325uint8_t pcf8574_get_reg(pcf8574_handle_t *handle, uint8_t *buf, uint16_t len)
326{
327 if (handle == NULL) /* check handle */
328 {
329 return 2; /* return error */
330 }
331 if (handle->inited != 1) /* check handle initialization */
332 {
333 return 3; /* return error */
334 }
335
336 if (handle->iic_read_cmd(handle->iic_addr, buf, len) != 0) /* read command */
337 {
338 return 1; /* return error */
339 }
340 else
341 {
342 return 0; /* success return 0 */
343 }
344}
345
355{
356 if (info == NULL) /* check handle */
357 {
358 return 2; /* return error */
359 }
360
361 memset(info, 0, sizeof(pcf8574_info_t)); /* initialize pcf8574 info structure */
362 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
363 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
364 strncpy(info->interface, "IIC", 8); /* copy interface name */
365 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
366 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
367 info->max_current_ma = MAX_CURRENT; /* set maximum current */
368 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
369 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
370 info->driver_version = DRIVER_VERSION; /* set driver version */
371
372 return 0; /* success return 0 */
373}
#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 pcf8574 header file
uint8_t pcf8574_info(pcf8574_info_t *info)
get chip's information
struct pcf8574_info_s pcf8574_info_t
pcf8574 information structure definition
uint8_t pcf8574_deinit(pcf8574_handle_t *handle)
close the chip
uint8_t pcf8574_get_addr_pin(pcf8574_handle_t *handle, pcf8574_address_t *addr_pin)
get the address pin
uint8_t pcf8574_write(pcf8574_handle_t *handle, pcf8574_pin_t pin, pcf8574_pin_level_t level)
write the pin
struct pcf8574_handle_s pcf8574_handle_t
pcf8574 handle structure definition
pcf8574_pin_level_t
pcf8574 pin enumeration definition
uint8_t pcf8574_read(pcf8574_handle_t *handle, pcf8574_pin_t pin, pcf8574_pin_level_t *level)
read the pin
uint8_t pcf8574_init(pcf8574_handle_t *handle)
initialize the chip
pcf8574_pin_t
pcf8574 pin enumeration definition
pcf8574_address_t
pcf8574 address enumeration definition
uint8_t pcf8574_set_addr_pin(pcf8574_handle_t *handle, pcf8574_address_t addr_pin)
set the address pin
uint8_t pcf8574_get_reg(pcf8574_handle_t *handle, uint8_t *buf, uint16_t len)
get the chip register
uint8_t pcf8574_set_reg(pcf8574_handle_t *handle, uint8_t *buf, uint16_t len)
set 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]