summaryrefslogtreecommitdiff
path: root/drivers/gpio/pca9505.c
blob: 5617a14a8b13c1e1d948f4ba7df704ec5fd7bd84 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Copyright 2022 nirim000
// SPDX-License-Identifier: GPL-2.0-or-later

#include "i2c_master.h"
#include "pca9505.h"

#include "debug.h"

#define SLAVE_TO_ADDR(n) (n << 1)
#define TIMEOUT 100

enum {
    CMD_INPUT_0 = 0,
    CMD_INPUT_1,
    CMD_INPUT_2,
    CMD_INPUT_3,
    CMD_INPUT_4,
    CMD_OUTPUT_0 = 8,
    CMD_OUTPUT_1,
    CMD_OUTPUT_2,
    CMD_OUTPUT_3,
    CMD_OUTPUT_4,
    CMD_INVERSION_0 = 16,
    CMD_INVERSION_1,
    CMD_INVERSION_2,
    CMD_INVERSION_3,
    CMD_INVERSION_4,
    CMD_CONFIG_0 = 24,
    CMD_CONFIG_1,
    CMD_CONFIG_2,
    CMD_CONFIG_3,
    CMD_CONFIG_4,
};

void pca9505_init(uint8_t slave_addr) {
    static uint8_t s_init = 0;
    if (!s_init) {
        i2c_init();

        s_init = 1;
    }

    // TODO: could check device connected
}

bool pca9505_set_config(uint8_t slave_addr, pca9505_port_t port, uint8_t conf) {
    uint8_t addr = SLAVE_TO_ADDR(slave_addr);
    uint8_t cmd  = 0;
    switch (port) {
        case 0:
            cmd = CMD_CONFIG_0;
            break;
        case 1:
            cmd = CMD_CONFIG_1;
            break;
        case 2:
            cmd = CMD_CONFIG_2;
            break;
        case 3:
            cmd = CMD_CONFIG_3;
            break;
        case 4:
            cmd = CMD_CONFIG_4;
            break;
    }

    i2c_status_t ret = i2c_write_register(addr, cmd, &conf, sizeof(conf), TIMEOUT);
    if (ret != I2C_STATUS_SUCCESS) {
        print("pca9505_set_config::FAILED\n");
        return false;
    }

    return true;
}

bool pca9505_set_polarity(uint8_t slave_addr, pca9505_port_t port, uint8_t conf) {
    uint8_t addr = SLAVE_TO_ADDR(slave_addr);
    uint8_t cmd  = 0;
    switch (port) {
        case 0:
            cmd = CMD_INVERSION_0;
            break;
        case 1:
            cmd = CMD_INVERSION_1;
            break;
        case 2:
            cmd = CMD_INVERSION_2;
            break;
        case 3:
            cmd = CMD_INVERSION_3;
            break;
        case 4:
            cmd = CMD_INVERSION_4;
            break;
    }

    i2c_status_t ret = i2c_write_register(addr, cmd, &conf, sizeof(conf), TIMEOUT);
    if (ret != I2C_STATUS_SUCCESS) {
        print("pca9505_set_polarity::FAILED\n");
        return false;
    }

    return true;
}

bool pca9505_set_output(uint8_t slave_addr, pca9505_port_t port, uint8_t conf) {
    uint8_t addr = SLAVE_TO_ADDR(slave_addr);
    uint8_t cmd  = 0;
    switch (port) {
        case 0:
            cmd = CMD_OUTPUT_0;
            break;
        case 1:
            cmd = CMD_OUTPUT_1;
            break;
        case 2:
            cmd = CMD_OUTPUT_2;
            break;
        case 3:
            cmd = CMD_OUTPUT_3;
            break;
        case 4:
            cmd = CMD_OUTPUT_4;
            break;
    }

    i2c_status_t ret = i2c_write_register(addr, cmd, &conf, sizeof(conf), TIMEOUT);
    if (ret != I2C_STATUS_SUCCESS) {
        print("pca9505_set_output::FAILED\n");
        return false;
    }

    return true;
}

bool pca9505_readPins(uint8_t slave_addr, pca9505_port_t port, uint8_t* out) {
    uint8_t addr = SLAVE_TO_ADDR(slave_addr);
    uint8_t cmd  = 0;
    switch (port) {
        case 0:
            cmd = CMD_INPUT_0;
            break;
        case 1:
            cmd = CMD_INPUT_1;
            break;
        case 2:
            cmd = CMD_INPUT_2;
            break;
        case 3:
            cmd = CMD_INPUT_3;
            break;
        case 4:
            cmd = CMD_INPUT_4;
            break;
    }

    i2c_status_t ret = i2c_read_register(addr, cmd, out, sizeof(uint8_t), TIMEOUT);
    if (ret != I2C_STATUS_SUCCESS) {
        print("pca9505_readPins::FAILED\n");
        return false;
    }

    return true;
}