|
| 1 | +/* |
| 2 | + * Copyright (c) 2006-2018, RT-Thread Development Team |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + * |
| 6 | + * |
| 7 | + * Change Logs: |
| 8 | + * Date Author Notes |
| 9 | + * 2018-11-21 SummerGift first version |
| 10 | + * 2018-11-22 flybreak Make the first version of pcf8574's package |
| 11 | + */ |
| 12 | + |
| 13 | +#include "pcf8574.h" |
| 14 | + |
| 15 | +#define DBG_ENABLE |
| 16 | +#define DBG_SECTION_NAME "pcf8574" |
| 17 | +#define DBG_LEVEL DBG_INFO |
| 18 | +#define DBG_COLOR |
| 19 | +#include <rtdbg.h> |
| 20 | + |
| 21 | +uint8_t pcf8574_port_read(pcf8574_device_t dev) |
| 22 | +{ |
| 23 | + uint8_t value; |
| 24 | + |
| 25 | + rt_device_read(&dev->bus->parent, dev->i2c_addr, &value, 1); |
| 26 | + |
| 27 | + return value; |
| 28 | +} |
| 29 | + |
| 30 | +void pcf8574_port_write(pcf8574_device_t dev, uint8_t value) |
| 31 | +{ |
| 32 | + rt_device_write(&dev->bus->parent, dev->i2c_addr, &value, 1); |
| 33 | +} |
| 34 | + |
| 35 | +uint8_t pcf8574_pin_read(pcf8574_device_t dev, uint8_t bit) |
| 36 | +{ |
| 37 | + uint8_t data; |
| 38 | + data = pcf8574_port_read(dev); |
| 39 | + |
| 40 | + if (data & (1 << bit)) |
| 41 | + return 1; |
| 42 | + else |
| 43 | + return 0; |
| 44 | +} |
| 45 | + |
| 46 | +void pcf8574_pin_write(pcf8574_device_t dev, uint8_t bit, uint8_t value) |
| 47 | +{ |
| 48 | + uint8_t data; |
| 49 | + data = pcf8574_port_read(dev); |
| 50 | + |
| 51 | + if (value == 0) |
| 52 | + data &= ~(1 << bit); |
| 53 | + else |
| 54 | + data |= 1 << bit; |
| 55 | + |
| 56 | + pcf8574_port_write(dev, data); |
| 57 | +} |
| 58 | + |
| 59 | +pcf8574_device_t pcf8574_init(const char *dev_name, rt_uint8_t i2c_addr) |
| 60 | +{ |
| 61 | + uint8_t buffer[] = { 0xFF }; |
| 62 | + pcf8574_device_t dev = RT_NULL; |
| 63 | + |
| 64 | + RT_ASSERT(dev_name); |
| 65 | + |
| 66 | + dev = rt_calloc(1, sizeof(struct pcf8574_device)); |
| 67 | + if (dev == RT_NULL) |
| 68 | + { |
| 69 | + LOG_E("Can't allocate memory for pcf8574 device on '%s' ", dev_name); |
| 70 | + goto __exit; |
| 71 | + } |
| 72 | + |
| 73 | + dev->bus = (struct rt_i2c_bus_device *)rt_device_find(dev_name); |
| 74 | + if (dev->bus == RT_NULL) |
| 75 | + { |
| 76 | + LOG_E("i2c_bus %s for PCF8574 not found!", dev_name); |
| 77 | + goto __exit; |
| 78 | + } |
| 79 | + |
| 80 | + if (i2c_addr != RT_NULL) |
| 81 | + dev->i2c_addr = i2c_addr; |
| 82 | + else |
| 83 | + dev->i2c_addr = PCF8574_ADDR_DEFAULT; |
| 84 | + |
| 85 | + if (rt_device_open(&dev->bus->parent, RT_NULL) != RT_EOK) |
| 86 | + { |
| 87 | + LOG_D("i2c_bus %s for PCF8574 opened failed!", dev_name); |
| 88 | + goto __exit; |
| 89 | + } |
| 90 | + |
| 91 | + rt_device_write(&dev->bus->parent, dev->i2c_addr, &buffer, 1); |
| 92 | + |
| 93 | + LOG_D("pcf8574 init done", dev_name); |
| 94 | + return dev; |
| 95 | + |
| 96 | +__exit: |
| 97 | + if (dev != RT_NULL) |
| 98 | + rt_free(dev); |
| 99 | + |
| 100 | + return RT_NULL; |
| 101 | +} |
| 102 | + |
| 103 | +void pcf8574_deinit(struct pcf8574_device *dev) |
| 104 | +{ |
| 105 | + RT_ASSERT(dev); |
| 106 | + |
| 107 | + rt_free(dev); |
| 108 | +} |
0 commit comments