33 #include "gpio-pcal9535a.h"
37 #define REG_INPUT_PORT0 0x00
38 #define REG_INPUT_PORT1 0x01
39 #define REG_OUTPUT_PORT0 0x02
40 #define REG_OUTPUT_PORT1 0x03
41 #define REG_POL_INV_PORT0 0x04
42 #define REG_POL_INV_PORT1 0x05
43 #define REG_CONF_PORT0 0x06
44 #define REG_CONG_PORT1 0x07
45 #define REG_OUT_DRV_STRENGTH_PORT0_L 0x40
46 #define REG_OUT_DRV_STRENGTH_PORT0_H 0x41
47 #define REG_OUT_DRV_STRENGTH_PORT1_L 0x42
48 #define REG_OUT_DRV_STRENGTH_PORT1_H 0x43
49 #define REG_INPUT_LATCH_PORT0 0x44
50 #define REG_INPUT_LATCH_PORT1 0x45
51 #define REG_PUD_EN_PORT0 0x46
52 #define REG_PUD_EN_PORT1 0x47
53 #define REG_PUD_SEL_PORT0 0x48
54 #define REG_PUD_SEL_PORT1 0x49
55 #define REG_INT_MASK_PORT0 0x4A
56 #define REG_INT_MASK_PORT1 0x4B
57 #define REG_INT_STATUS_PORT0 0x4C
58 #define REG_INT_STATUS_PORT1 0x4D
59 #define REG_OUTPUT_PORT_CONF 0x4F
61 #define READ_PORT_TIMEOUT (CLOCK_SECOND / 100)
62 #define READ_PORT_TRIES 5
65 read_port_regs(
struct gpio_pcal9535a_data *data, uint8_t reg,
union gpio_pcal9535a_port_data *buf)
68 uint8_t tries = READ_PORT_TRIES;
73 if(quarkX1000_i2c_write(buf->byte, 1, data->i2c_slave_addr) < 0) {
80 r = quarkX1000_i2c_read(buf->byte, 2, data->i2c_slave_addr);
93 write_port_regs(
struct gpio_pcal9535a_data *data, uint8_t reg,
union gpio_pcal9535a_port_data *buf)
95 uint8_t cmd[] = { reg, buf->byte[0], buf->byte[1] };
97 if(quarkX1000_i2c_polling_write(cmd,
sizeof(cmd), data->i2c_slave_addr) < 0) {
104 setup_pin_dir(
struct gpio_pcal9535a_data *data, uint32_t pin,
int flags)
106 union gpio_pcal9535a_port_data *port = &data->reg_cache.dir;
107 uint16_t bit_mask, new_value = 0;
111 if((flags & QUARKX1000_GPIO_DIR_MASK) == QUARKX1000_GPIO_IN) {
112 new_value = 1 << pin;
115 port->all &= ~bit_mask;
116 port->all |= new_value;
118 return write_port_regs(data, REG_CONF_PORT0, port);
121 setup_pin_pullupdown(
struct gpio_pcal9535a_data *data, uint32_t pin,
int flags)
123 union gpio_pcal9535a_port_data *port;
124 uint16_t bit_mask, new_value = 0;
126 if((flags & QUARKX1000_GPIO_PUD_MASK) != QUARKX1000_GPIO_PUD_NORMAL) {
127 port = &data->reg_cache.pud_sel;
130 if((flags & QUARKX1000_GPIO_PUD_MASK) == QUARKX1000_GPIO_PUD_PULL_UP) {
131 new_value = 1 << pin;
134 port->all &= ~bit_mask;
135 port->all |= new_value;
137 if(write_port_regs(data, REG_PUD_SEL_PORT0, port) < 0) {
142 port = &data->reg_cache.pud_en;
145 if((flags & QUARKX1000_GPIO_PUD_MASK) != QUARKX1000_GPIO_PUD_NORMAL) {
146 new_value = 1 << pin;
149 port->all &= ~bit_mask;
150 port->all |= new_value;
152 return write_port_regs(data, REG_PUD_EN_PORT0, port);
155 setup_pin_polarity(
struct gpio_pcal9535a_data *data, uint32_t pin,
int flags)
157 union gpio_pcal9535a_port_data *port = &data->reg_cache.pol_inv;
158 uint16_t bit_mask, new_value = 0;
162 if((flags & QUARKX1000_GPIO_POL_MASK) == QUARKX1000_GPIO_POL_INV) {
163 new_value = 1 << pin;
166 port->all &= ~bit_mask;
167 port->all |= new_value;
169 if(write_port_regs(data, REG_POL_INV_PORT0, port) < 0) {
173 data->out_pol_inv = port->all;
178 gpio_pcal9535a_write(
struct gpio_pcal9535a_data *data, uint32_t pin, uint32_t value)
180 union gpio_pcal9535a_port_data *port = &data->reg_cache.output;
181 uint16_t bit_mask, new_value;
183 if(!quarkX1000_i2c_is_available()) {
189 new_value = (value << pin) & bit_mask;
190 new_value ^= (data->out_pol_inv & bit_mask);
191 new_value &= bit_mask;
193 port->all &= ~bit_mask;
194 port->all |= new_value;
196 return write_port_regs(data, REG_OUTPUT_PORT0, port);
199 gpio_pcal9535a_read(
struct gpio_pcal9535a_data *data, uint32_t pin, uint32_t *value)
201 union gpio_pcal9535a_port_data buf;
203 if(!quarkX1000_i2c_is_available()) {
207 if(read_port_regs(data, REG_INPUT_PORT0, &buf) < 0) {
211 *value = (buf.all >> pin) & 0x01;
216 gpio_pcal9535a_config(
struct gpio_pcal9535a_data *data, uint32_t pin,
int flags)
218 if(!quarkX1000_i2c_is_available()) {
222 if(setup_pin_dir(data, pin, flags) < 0) {
226 if(setup_pin_polarity(data, pin, flags) < 0) {
230 if(setup_pin_pullupdown(data, pin, flags) < 0) {
237 setup_port_dir(
struct gpio_pcal9535a_data *data, uint32_t pin,
int flags)
239 union gpio_pcal9535a_port_data *port = &data->reg_cache.dir;
241 port->all = ((flags & QUARKX1000_GPIO_DIR_MASK) == QUARKX1000_GPIO_IN) ? 0xFFFF : 0x0;
243 return write_port_regs(data, REG_CONF_PORT0, port);
246 setup_port_pullupdown(
struct gpio_pcal9535a_data *data, uint32_t pin,
int flags)
248 union gpio_pcal9535a_port_data *port;
250 if((flags & QUARKX1000_GPIO_PUD_MASK) != QUARKX1000_GPIO_PUD_NORMAL) {
251 port = &data->reg_cache.pud_sel;
252 port->all = ((flags & QUARKX1000_GPIO_PUD_MASK) == QUARKX1000_GPIO_PUD_PULL_UP) ? 0xFFFF : 0x0;
254 if(write_port_regs(data, REG_PUD_SEL_PORT0, port) < 0) {
259 port = &data->reg_cache.pud_en;
260 port->all = ((flags & QUARKX1000_GPIO_PUD_MASK) != QUARKX1000_GPIO_PUD_NORMAL) ? 0xFFFF : 0x0;
262 return write_port_regs(data, REG_PUD_EN_PORT0, port);
265 setup_port_polarity(
struct gpio_pcal9535a_data *data, uint32_t pin,
int flags)
267 union gpio_pcal9535a_port_data *port = &data->reg_cache.pol_inv;
269 port->all = ((flags & QUARKX1000_GPIO_POL_MASK) == QUARKX1000_GPIO_POL_INV) ? 0xFFFF : 0x0;
271 if(write_port_regs(data, REG_POL_INV_PORT0, port) < 0) {
275 data->out_pol_inv = port->all;
280 gpio_pcal9535a_write_port(
struct gpio_pcal9535a_data *data, uint32_t pin, uint32_t value)
282 union gpio_pcal9535a_port_data *port = &data->reg_cache.output;
283 uint16_t bit_mask, new_value;
285 if(!quarkX1000_i2c_is_available()) {
290 bit_mask = data->out_pol_inv;
292 new_value = value & bit_mask;
293 new_value ^= data->out_pol_inv;
294 new_value &= bit_mask;
296 port->all &= ~bit_mask;
297 port->all |= new_value;
299 return write_port_regs(data, REG_OUTPUT_PORT0, port);
302 gpio_pcal9535a_read_port(
struct gpio_pcal9535a_data *data, uint32_t pin, uint32_t *value)
304 union gpio_pcal9535a_port_data buf;
306 if(!quarkX1000_i2c_is_available()) {
310 if(read_port_regs(data, REG_INPUT_PORT0, &buf) < 0) {
319 gpio_pcal9535a_config_port(
struct gpio_pcal9535a_data *data, uint32_t pin,
int flags)
321 if(!quarkX1000_i2c_is_available()) {
325 if(setup_port_dir(data, pin, flags) < 0) {
329 if(setup_port_polarity(data, pin, flags) < 0) {
333 if(setup_port_pullupdown(data, pin, flags) < 0) {
340 gpio_pcal9535a_init(
struct gpio_pcal9535a_data *data, uint16_t i2c_slave_addr)
343 if(!quarkX1000_i2c_is_available()) {
347 data->i2c_slave_addr = i2c_slave_addr;
350 data->reg_cache.output.all = 0xFFFF;
351 data->reg_cache.pol_inv.all = 0x0;
352 data->reg_cache.dir.all = 0xFFFF;
353 data->reg_cache.pud_en.all = 0x0;
354 data->reg_cache.pud_sel.all = 0xFFFF;
void clock_wait(clock_time_t t)
Wait for a given number of ticks.