Skip to content

Commit

Permalink
More tidy up and add doxygen comments
Browse files Browse the repository at this point in the history
  • Loading branch information
zvecr committed Jan 19, 2022
1 parent 52b0a48 commit 1ae7e0e
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 26 deletions.
8 changes: 4 additions & 4 deletions drivers/gpio/mcp23018.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 zvecr<[email protected]>
// Copyright 2022 zvecr<[email protected]>
// SPDX-License-Identifier: GPL-2.0-or-later

#include "mcp23018.h"
Expand Down Expand Up @@ -28,7 +28,7 @@ void mcp23018_init(uint8_t addr) {
}
}

bool mcp23018_set_config(uint8_t slave_addr, uint8_t port, uint8_t conf) {
bool mcp23018_set_config(uint8_t slave_addr, mcp23018_port_t port, uint8_t conf) {
uint8_t addr = SLAVE_TO_ADDR(slave_addr);
uint8_t cmdDirection = port ? CMD_IODIRB : CMD_IODIRA;
uint8_t cmdPullup = port ? CMD_GPPUB : CMD_GPPUA;
Expand All @@ -48,7 +48,7 @@ bool mcp23018_set_config(uint8_t slave_addr, uint8_t port, uint8_t conf) {
return true;
}

bool mcp23018_set_output(uint8_t slave_addr, uint8_t port, uint8_t conf) {
bool mcp23018_set_output(uint8_t slave_addr, mcp23018_port_t port, uint8_t conf) {
uint8_t addr = SLAVE_TO_ADDR(slave_addr);
uint8_t cmd = port ? CMD_GPIOB : CMD_GPIOA;

Expand All @@ -74,7 +74,7 @@ bool mcp23018_set_output_all(uint8_t slave_addr, uint8_t confA, uint8_t confB) {
return true;
}

bool mcp23018_readPins(uint8_t slave_addr, uint8_t port, uint8_t* out) {
bool mcp23018_readPins(uint8_t slave_addr, mcp23018_port_t port, uint8_t* out) {
uint8_t addr = SLAVE_TO_ADDR(slave_addr);
uint8_t cmd = port ? CMD_GPIOB : CMD_GPIOA;

Expand Down
61 changes: 52 additions & 9 deletions drivers/gpio/mcp23018.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,60 @@
#include <stdint.h>
#include <stdbool.h>

#define mcp23018_PORTA 0
#define mcp23018_PORTB 1
/**
* Port ID
*/
typedef enum {
mcp23018_PORTA,
mcp23018_PORTB,
} mcp23018_port_t;

#define ALL_OUTPUT 0
#define ALL_INPUT 0xFF
#define ALL_LOW 0
#define ALL_HIGH 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 mcp23018_init(uint8_t slave_addr);
bool mcp23018_set_config(uint8_t slave_addr, uint8_t port, uint8_t conf);
bool mcp23018_set_output(uint8_t slave_addr, uint8_t port, uint8_t conf);

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

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

/**
* Write high/low to both ports sequentially
*
* - slightly faster than multiple set_output
*/
bool mcp23018_set_output_all(uint8_t slave_addr, uint8_t confA, uint8_t confB);
bool mcp23018_readPins(uint8_t slave_addr, uint8_t port, uint8_t* ret);

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

/**
* Read state of both ports sequentially
*
* - slightly faster than multiple readPins
*/
bool mcp23018_readPins_all(uint8_t slave_addr, uint16_t* ret);
6 changes: 3 additions & 3 deletions drivers/gpio/pca9555.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void pca9555_init(uint8_t slave_addr) {
// i2c_stop();
}

bool pca9555_set_config(uint8_t slave_addr, uint8_t port, uint8_t conf) {
bool pca9555_set_config(uint8_t slave_addr, pca9555_port_t port, uint8_t conf) {
uint8_t addr = SLAVE_TO_ADDR(slave_addr);
uint8_t cmd = port ? CMD_CONFIG_1 : CMD_CONFIG_0;

Expand All @@ -46,7 +46,7 @@ bool pca9555_set_config(uint8_t slave_addr, uint8_t port, uint8_t conf) {
return true;
}

bool pca9555_set_output(uint8_t slave_addr, uint8_t port, uint8_t conf) {
bool pca9555_set_output(uint8_t slave_addr, pca9555_port_t port, uint8_t conf) {
uint8_t addr = SLAVE_TO_ADDR(slave_addr);
uint8_t cmd = port ? CMD_OUTPUT_1 : CMD_OUTPUT_0;

Expand All @@ -72,7 +72,7 @@ bool pca9555_set_output_all(uint8_t slave_addr, uint8_t confA, uint8_t confB) {
return true;
}

bool pca9555_readPins(uint8_t slave_addr, uint8_t port, uint8_t* out) {
bool pca9555_readPins(uint8_t slave_addr, pca9555_port_t port, uint8_t* out) {
uint8_t addr = SLAVE_TO_ADDR(slave_addr);
uint8_t cmd = port ? CMD_INPUT_1 : CMD_INPUT_0;

Expand Down
58 changes: 48 additions & 10 deletions drivers/gpio/pca9555.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,60 @@
`----------'
*/

#define PCA9555_PORT0 0
#define PCA9555_PORT1 1
/**
* Port ID
*/
typedef enum {
PCA9555_PORT0,
PCA9555_PORT1,
} pca9555_port_t;

#define ALL_OUTPUT 0
#define ALL_INPUT 0xFF
#define ALL_LOW 0
#define ALL_HIGH 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 pca9555_init(uint8_t slave_addr);

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

bool pca9555_set_output_all(uint8_t slave_addr, uint8_t confA, uint8_t confB);
/**
* Write high/low to a given port
*/
bool pca9555_set_output(uint8_t slave_addr, pca9555_port_t port, uint8_t conf);

bool pca9555_set_output(uint8_t slave_addr, uint8_t port, uint8_t conf);
/**
* Write high/low to both ports sequentially
*
* - slightly faster than multiple set_output
*/
bool pca9555_set_output_all(uint8_t slave_addr, uint8_t confA, uint8_t confB);

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

/**
* Read state of both ports sequentially
*
* - slightly faster than multiple readPins
*/
bool pca9555_readPins_all(uint8_t slave_addr, uint16_t* ret);

0 comments on commit 1ae7e0e

Please sign in to comment.