Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pca9505/6 driver #17333

Merged
merged 10 commits into from
Jul 2, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
166 changes: 166 additions & 0 deletions drivers/gpio/pca9505.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
// 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
// i2c_start(SLAVE_TO_ADDR(slave) | I2C_WRITE);
// i2c_stop();
}

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_writeReg(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_writeReg(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_writeReg(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_readReg(addr, cmd, out, sizeof(uint8_t), TIMEOUT);
if (ret != I2C_STATUS_SUCCESS) {
print("pca9505_readPins::FAILED\n");
return false;
}

return true;
}
67 changes: 67 additions & 0 deletions drivers/gpio/pca9505.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright 2022 nirim000
// SPDX-License-Identifier: GPL-2.0-or-later

#pragma once

#include <stdint.h>
#include <stdbool.h>

/**
* Port ID
*/
typedef enum {
PCA9505_PORT0,
PCA9505_PORT1,
PCA9505_PORT2,
PCA9505_PORT3,
PCA9505_PORT4,
} pca9505_port_t;

/**
* Helpers for set_config
*/
enum {
ALL_NORMAL = 0,
ALL_INVERTED = 0xFF,
};

/**
* Helpers for set_config
*/
enum {
ALL_OUTPUT = 0,
ALL_INPUT = 0xFF,
};

/**
* Helpers for set_output
*/
enum {
ALL_LOW = 0,
ALL_HIGH = 0xFF,
};

/**
* Init expander and any other dependent drivers
*/
void pca9505_init(uint8_t slave_addr);

/**
* Configure input/output to a given port
*/
bool pca9505_set_config(uint8_t slave_addr, pca9505_port_t port, uint8_t conf);

/**
* Configure polarity to a given port
*/
bool pca9505_set_polarity(uint8_t slave_addr, pca9505_port_t port, uint8_t conf);

/**
* Write high/low to a given port
*/
bool pca9505_set_output(uint8_t slave_addr, pca9505_port_t port, uint8_t conf);

/**
* Read state of a given port
*/
bool pca9505_readPins(uint8_t slave_addr, pca9505_port_t port, uint8_t* ret);