-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpca9555.cpp
90 lines (77 loc) · 2.34 KB
/
pca9555.cpp
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
/*
* author : Shuichi TAKANO
* since : Mon Aug 29 2022 02:42:51
*/
#include "pca9555.h"
#include "debug.h"
#include <cassert>
namespace device
{
namespace
{
enum class Command
{
INPUT_PORT_0,
INPUT_PORT_1,
OUTPUT_PORT_0,
OUTPUT_PORT_1,
POLARITY_INVERSION_0,
POLARITY_INVERSION_1,
CONFIGURATION_0,
CONFIGURATION_1,
};
}
void
PCA9555::init(i2c_inst_t *i2cInst, int addrSel)
{
addr_ = 0b0100000 | addrSel;
uint8_t rxdata{};
// auto r = i2c_read_timeout_per_char_us(i2cInst, addr_, &rxdata, 1, false, 100);
auto r = i2c_read_blocking(i2cInst, addr_, &rxdata, 1, false);
DPRINT(("PCA9555 init[%02x]: %d\n", addr_, r));
if (r >= 0)
{
i2c_ = i2cInst;
}
{
uint8_t wd[] = {6, 0xaa};
i2c_write_blocking(i2cInst, addr_, wd, 2, false);
}
{
static uint8_t v = 0;
v ^= 255;
uint8_t wd[] = {2, v};
i2c_write_blocking(i2cInst, addr_, wd, 2, false);
}
}
void
PCA9555::setPortDir(uint16_t inputPortBits) const
{
assert(i2c_);
uint8_t data[] = {static_cast<uint8_t>(Command::CONFIGURATION_0), static_cast<uint8_t>(inputPortBits & 0xFF)};
i2c_write_blocking(i2c_, addr_, data, 2, false);
data[0] = static_cast<uint8_t>(Command::CONFIGURATION_1);
data[1] = static_cast<uint8_t>((inputPortBits >> 8) & 0xFF);
i2c_write_blocking(i2c_, addr_, data, 2, false);
}
void
PCA9555::output(uint16_t bits) const
{
assert(i2c_);
uint8_t data[] = {static_cast<uint8_t>(Command::OUTPUT_PORT_0), static_cast<uint8_t>(bits & 0xFF)};
i2c_write_blocking(i2c_, addr_, data, 2, false);
data[0] = static_cast<uint8_t>(Command::OUTPUT_PORT_1);
data[1] = static_cast<uint8_t>((bits >> 8) & 0xFF);
i2c_write_blocking(i2c_, addr_, data, 2, false);
}
int
PCA9555::input() const
{
assert(i2c_);
uint8_t cmd = static_cast<uint8_t>(Command::INPUT_PORT_0);
i2c_write_blocking(i2c_, addr_, &cmd, 1, true);
uint8_t data[2] = {0};
i2c_read_blocking(i2c_, addr_, data, 2, false);
return data[0] | (data[1] << 8);
}
}