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 200.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->inited = 1; /* flag finish initialization */
164
165 return 0; /* success return 0 */
166}
167
179{
180 uint8_t res;
181
182 if (handle == NULL) /* check handle */
183 {
184 return 2; /* return error */
185 }
186 if (handle->inited != 1) /* check handle initialization */
187 {
188 return 3; /* return error */
189 }
190
191 res = handle->iic_deinit(); /* iic deinit */
192 if (res != 0) /* check error */
193 {
194 handle->debug_print("pcf8574: iic deinit failed.\n"); /* iic deinit failed */
195
196 return 1; /* return error */
197 }
198 handle->inited = 0; /* flag closed */
199
200 return 0; /* success return 0 */
201}
202
216{
217 uint8_t res;
218 uint8_t data;
219
220 if (handle == NULL) /* check handle */
221 {
222 return 2; /* return error */
223 }
224 if (handle->inited != 1) /* check handle initialization */
225 {
226 return 3; /* return error */
227 }
228
229 res = handle->iic_read_cmd(handle->iic_addr, (uint8_t *)&data, 1); /* read data */
230 if (res != 0) /* check error */
231 {
232 handle->debug_print("pcf8574: iic read failed.\n"); /* iic read failed */
233
234 return 1; /* return error */
235 }
236 *level = (pcf8574_pin_level_t)((data >> pin) & 0x01); /* get level */
237
238 return 0; /* success return 0 */
239}
240
254{
255 uint8_t res;
256 uint8_t data;
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 res = handle->iic_read_cmd(handle->iic_addr, (uint8_t *)&data, 1); /* read data */
268 if (res != 0) /* check error */
269 {
270 handle->debug_print("pcf8574: iic read failed.\n"); /* iic read failed */
271
272 return 1; /* return error */
273 }
274 data &= ~(1 << pin); /* clear 0 */
275 data |= level << pin; /* set data */
276 res = handle->iic_write_cmd(handle->iic_addr, (uint8_t *)&data, 1); /* write data */
277 if (res != 0) /* check error */
278 {
279 handle->debug_print("pcf8574: iic write failed.\n"); /* iic write failed */
280
281 return 1; /* return error */
282 }
283
284 return 0; /* success return 0 */
285}
286
299uint8_t pcf8574_set_reg(pcf8574_handle_t *handle, uint8_t *buf, uint16_t len)
300{
301 if (handle == NULL) /* check handle */
302 {
303 return 2; /* return error */
304 }
305 if (handle->inited != 1) /* check handle initialization */
306 {
307 return 3; /* return error */
308 }
309
310 if (handle->iic_write_cmd(handle->iic_addr, buf, len) != 0) /* write command */
311 {
312 return 1; /* return error */
313 }
314 else
315 {
316 return 0; /* success return 0 */
317 }
318}
319
332uint8_t pcf8574_get_reg(pcf8574_handle_t *handle, uint8_t *buf, uint16_t len)
333{
334 if (handle == NULL) /* check handle */
335 {
336 return 2; /* return error */
337 }
338 if (handle->inited != 1) /* check handle initialization */
339 {
340 return 3; /* return error */
341 }
342
343 if (handle->iic_read_cmd(handle->iic_addr, buf, len) != 0) /* read command */
344 {
345 return 1; /* return error */
346 }
347 else
348 {
349 return 0; /* success return 0 */
350 }
351}
352
362{
363 if (info == NULL) /* check handle */
364 {
365 return 2; /* return error */
366 }
367
368 memset(info, 0, sizeof(pcf8574_info_t)); /* initialize pcf8574 info structure */
369 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
370 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
371 strncpy(info->interface, "IIC", 8); /* copy interface name */
372 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
373 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
374 info->max_current_ma = MAX_CURRENT; /* set maximum current */
375 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
376 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
377 info->driver_version = DRIVER_VERSION; /* set driver version */
378
379 return 0; /* success return 0 */
380}
#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]