summaryrefslogtreecommitdiff
path: root/bsp/imxrt/Libraries/imxrt1021/drivers/drv_pcf8574.c
blob: 398013ea6a33b7aa1168305eae7fb8b16c5ab99a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
 * Copyright (c) 2006-2018, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2018-06-12     zylx     the first version.
 */
#include "drv_pcf8574.h"

#define PCF8574_I2CBUS_NAME  "i2c1"
static struct rt_i2c_bus_device *pcf8574_i2c_bus;

int pcf8574_Init(void)
{
    rt_uint8_t value = 0x01;

    pcf8574_i2c_bus = rt_i2c_bus_device_find(PCF8574_I2CBUS_NAME);

    if (pcf8574_i2c_bus == RT_NULL)
    {
        rt_kprintf("can't find i2c device\r\n");
        return -RT_ERROR;
    }

    if (!rt_i2c_master_send(pcf8574_i2c_bus, PCF8574_ADDR, 0, &value, 1))
    {
        rt_kprintf("can't find pcf8574\r\n");
        return -RT_ERROR;
    }

    return 0;
}
INIT_COMPONENT_EXPORT(pcf8574_Init);

rt_uint8_t pcf8574_read_byte(void)
{
    rt_uint8_t temp = 0;
    rt_i2c_master_recv(pcf8574_i2c_bus, PCF8574_ADDR, 0, &temp, 1);
    return temp;
}

void pcf8574_write_byte(rt_uint8_t data)
{
    rt_i2c_master_send(pcf8574_i2c_bus, PCF8574_ADDR, 0, &data, 1);
}

void pcf8574_write_bit(rt_uint8_t bit, rt_uint8_t sta)
{
    rt_uint8_t data;
    data = pcf8574_read_byte();
    if (sta == 0)data &= ~(1 << bit);
    else data |= 1 << bit;
    pcf8574_write_byte(data);
}

rt_uint8_t pcf8574_read_bit(rt_uint8_t bit)
{
    rt_uint8_t data;
    data = pcf8574_read_byte();
    if (data & (1 << bit))return 1;
    else return 0;
}