-
Notifications
You must be signed in to change notification settings - Fork 2
/
mcp23017.c
177 lines (156 loc) · 6.17 KB
/
mcp23017.c
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
165
166
167
168
169
170
171
172
173
174
175
176
#include "mcp23017.h"
#include <driver/gpio.h>
#include <driver/i2c.h>
#include "esp_log.h"
static const char* TAG = "MCP23017";
// disable buffers
static const size_t I2C_MASTER_TX_BUF_DISABLE = 0;
static const size_t I2C_MASTER_RX_BUF_DISABLE = 0;
static const int INTR_FLAGS = 0;
/**
* Converts generic register and group (A/B) to register address
* @param reg the generic register index
* @param group the group (A/B) to compute offset
* @return The register address specified by the parameters
*/
uint8_t mcp23017_register(mcp23017_reg_t reg, mcp23017_gpio_t group) {
return (group == GPIOA)?(reg << 1):(reg << 1) | 1;
}
/**
* Initializes the MCP23017
* @param mcp the MCP23017 interface structure
* @return an error code or MCP23017_ERR_OK if no error encountered
*/
mcp23017_err_t mcp23017_init(mcp23017_t *mcp) {
esp_err_t ret;
// setup i2c controller
i2c_config_t conf = {
.mode = I2C_MODE_MASTER,
.sda_io_num = mcp->sda_pin,
.scl_io_num = mcp->scl_pin,
.sda_pullup_en = mcp->sda_pullup_en,
.scl_pullup_en = mcp->scl_pullup_en,
.master.clk_speed = 100000
};
ret = i2c_param_config(mcp->port, &conf);
if( ret != ESP_OK ) {
ESP_LOGE(TAG,"PARAM CONFIG FAILED");
return MCP23017_ERR_CONFIG;
}
ESP_LOGV(TAG,"PARAM CONFIG done");
// install the driver
ret = i2c_driver_install(mcp->port, I2C_MODE_MASTER, I2C_MASTER_TX_BUF_DISABLE, I2C_MASTER_RX_BUF_DISABLE, INTR_FLAGS);
if(ret != ESP_OK) {
ESP_LOGE(TAG,"I2C driver install failed");
return MCP23017_ERR_INSTALL;
}
ESP_LOGV(TAG,"I2C DRIVER INSTALLED");
// make all I/O's output
mcp23017_write_register(mcp, MCP23017_IODIR, GPIOA, 0x00);
mcp23017_write_register(mcp, MCP23017_IODIR, GPIOB, 0x00);
return MCP23017_ERR_OK;
}
/**
* Writes a value to an MCP23017 register
* @param mcp the MCP23017 interface structure
* @param reg A generic register index
* @param group the group (A/B) to compute register address offset
* @param v the value to write to the register
* @return an error code or MCP23017_ERR_OK if no error encountered
*/
mcp23017_err_t mcp23017_write_register(mcp23017_t *mcp, mcp23017_reg_t reg, mcp23017_gpio_t group, uint8_t v) {
uint8_t r = mcp23017_register(reg, group);
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, mcp->i2c_addr << 1 | WRITE_BIT, ACK_CHECK_EN);
i2c_master_write_byte(cmd, r, ACK_CHECK_EN);
i2c_master_write_byte(cmd, v, ACK_CHECK_EN);
i2c_master_stop(cmd);
esp_err_t ret = i2c_master_cmd_begin(mcp->port, cmd, 1000 / portTICK_RATE_MS);
i2c_cmd_link_delete(cmd);
if (ret == ESP_FAIL) {
ESP_LOGE(TAG,"ERROR: unable to write to register");
return MCP23017_ERR_FAIL;
}
return MCP23017_ERR_OK;
}
/**
* Reads a value to an MCP23017 register
* @param mcp the MCP23017 interface structure
* @param reg A generic register index
* @param group the group (A/B) to compute register address offset
* @param data a pointer to an 8 bit value to be read from the device
* @return an error code or MCP23017_ERR_OK if no error encountered
*/
mcp23017_err_t mcp23017_read_register(mcp23017_t *mcp, mcp23017_reg_t reg, mcp23017_gpio_t group, uint8_t *data) {
// from the generic register and group, derive register address
uint8_t r = mcp23017_register(reg, group);
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (mcp->i2c_addr << 1) | I2C_MASTER_WRITE, ACK_CHECK_EN);
i2c_master_write_byte(cmd, r, 1);
i2c_master_stop(cmd);
esp_err_t ret =i2c_master_cmd_begin(mcp->port, cmd, 1000 / portTICK_RATE_MS);
i2c_cmd_link_delete(cmd);
if( ret == ESP_FAIL ) {
ESP_LOGE(TAG,"ERROR: unable to write address %02x to read reg %02x",mcp->i2c_addr,r);
return MCP23017_ERR_FAIL;
}
cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (mcp->i2c_addr << 1) | I2C_MASTER_READ, ACK_CHECK_EN);
i2c_master_read_byte(cmd, data, 1);
ret =i2c_master_cmd_begin(mcp->port, cmd, 1000 / portTICK_RATE_MS);
i2c_cmd_link_delete(cmd);
if( ret == ESP_FAIL ) {
ESP_LOGE(TAG,"ERROR: unable to read reg %02x from address %02x",r,mcp->i2c_addr);
return MCP23017_ERR_FAIL;
}
return MCP23017_ERR_OK;
}
/**
* Clears a bit from a current register value
* @param mcp address of the MCP23017 data structure
* @param bit The number of the bit to set
* @param reg A generic register index
* @param group the group (A/B) to compute register address offset
* @return an error code or MCP23017_ERR_OK if no error encountered
*/
mcp23017_err_t mcp23017_set_bit(mcp23017_t *mcp, uint8_t bit, mcp23017_reg_t reg, mcp23017_gpio_t group) {
uint8_t current_value;
if( mcp23017_read_register(mcp, reg, group, ¤t_value) != MCP23017_ERR_OK ) {
uint8_t r = mcp23017_register(reg, group);
ESP_LOGE(TAG, "ERROR: unable to read current value of register %02x",r);
return MCP23017_ERR_FAIL;
}
current_value |= 1 << bit;
if( mcp23017_write_register(mcp, reg, group, current_value) != MCP23017_ERR_OK ) {
uint8_t r = mcp23017_register(reg, group);
ESP_LOGE(TAG, "ERROR: unable to write new value %02X to register %02x",current_value, r)
return MCP23017_ERR_FAIL;
}
return MCP23017_ERR_OK;
}
/**
* Clears a bit from a current register value
* @param mcp address of the MCP23017 data structure
* @param bit The number of the bit to set
* @param reg A generic register index
* @param group the group (A/B) to compute register address offset
* @return an error code or MCP23017_ERR_OK if no error encountered
*/
mcp23017_err_t mcp23017_clear_bit(mcp23017_t *mcp, uint8_t bit, mcp23017_reg_t reg, mcp23017_gpio_t group) {
uint8_t current_value;
if( mcp23017_read_register(mcp, reg, group, ¤t_value) != MCP23017_ERR_OK ) {
uint8_t r = mcp23017_register(reg, group);
ESP_LOGE(TAG, "ERROR: unable to read current value of register %02x",r);
return MCP23017_ERR_FAIL;
}
current_value &= ~(1 << bit);
if( mcp23017_write_register(mcp, reg, group, current_value) != MCP23017_ERR_OK ) {
uint8_t r = mcp23017_register(reg, group);
ESP_LOGE(TAG, "ERROR: unable to write new value %02X to register %02x",current_value, r)
return MCP23017_ERR_FAIL;
}
return MCP23017_ERR_OK;
}