Skip to content

Commit

Permalink
get rid of super bad enums lol
Browse files Browse the repository at this point in the history
  • Loading branch information
jr1221 committed May 25, 2024
1 parent f35aa5a commit deeb684
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 62 deletions.
93 changes: 39 additions & 54 deletions general/include/pca9539.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,84 +3,69 @@

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

/// Possible I2C addresses, see comment below
/// A0 A1
/// PCA_I2C_ADDR_0 0x74, L L
/// PCA_I2C_ADDR_1 0x75, L H
/// PCA_I2C_ADDR_2 0x76, H L
/// PCA_I2C_ADDR_3 0x77, H H
typedef enum
{
PCA_I2C_ADDR_0 = 0x74,
PCA_I2C_ADDR_1 = 0x75,
PCA_I2C_ADDR_2 = 0x76,
PCA_I2C_ADDR_3 = 0x77
} pca9539_addr_t;
#define PCA_I2C_ADDR_0 0x74
#define PCA_I2C_ADDR_1 0x75
#define PCA_I2C_ADDR_2 0x76
#define PCA_I2C_ADDR_3 0x77

/// @brief Pin #, (0 through 7 on each bus, ex. bus 1 pin 3 is 13)
typedef enum
{
PCA_P0,
PCA_P1,
PCA_P2,
PCA_P3,
PCA_P4,
PCA_P5,
PCA_P6,
PCA_P7,
typedef enum {
PCA_P0,
PCA_P1,
PCA_P2,
PCA_P3,
PCA_P4,
PCA_P5,
PCA_P6,
PCA_P7,
} pca9539_pins_t;

/// @brief Bus #, Ex. P0 is B0 P0 and P12 is B1 P2
typedef enum
{
PCA_B0,
PCA_B1
} pca9539_port_t;

/// Whether a pin is an input or output (direction) or inverted or univerted (polarity), or logic high or low
typedef enum
{
PCA_INPUT_MODE = 1,
PCA_OUTPUT_MODE = 0,
PCA_INVERTED = 1,
PCA_UNINVERTED = 0,
PCA_HIGH = 1,
PCA_LOW = 1
} pca9539_pin_mode_t;
typedef enum { PCA_B0, PCA_B1 } pca9539_port_t;

/// What to write/read to/from
///
/// INPUT: The incoming logic level (read only)
/// OUTPUT: The outgoing logic level (write only)
/// POLARITY: Inversion state
/// DIRECTION: Input/Output selection
typedef enum
{
PCA_INPUT = 0,
PCA_OUTPUT = 2,
PCA_POLARITY = 4,
PCA_DIRECTION = 6,
/// INPUT: The incoming logic level (read only) Result: 1=H 0=L
/// OUTPUT: The outgoing logic level (write only) Set: 1=H 0=L
/// POLARITY: Inversion state, 1=Inverted 0=Uninverted
/// DIRECTION: Input/Output selection 1=Input 0=Output
typedef enum {
PCA_INPUT = 0,
PCA_OUTPUT = 2,
PCA_POLARITY = 4,
PCA_DIRECTION = 6,
} pca9539_reg_type_t;

typedef struct
{
I2C_HandleTypeDef *i2c_handle;
uint16_t dev_addr;
typedef struct {
I2C_HandleTypeDef* i2c_handle;
uint16_t dev_addr;
} pca9539_t;

/// Init PCA9539, a 16 bit I2C GPIO expander
void pca9539_init(pca9539_t *pca, I2C_HandleTypeDef *i2c_handle, pca9539_addr_t *dev_addr);

void pca9539_init(pca9539_t* pca, I2C_HandleTypeDef* i2c_handle, uint8_t dev_addr);

/// @brief Read all pins on a bus, using reg_type input to get incoming logic level
HAL_StatusTypeDef read_pins(pca9539_t *pca, pca9539_reg_type_t reg_type, pca9539_port_t port, pca9539_pin_mode_t config[8]);
HAL_StatusTypeDef pca9539_read_pins(pca9539_t* pca, pca9539_reg_type_t reg_type,
pca9539_port_t port, bool config[8]);
/// @brief Read a specific pin on a bus, do not iterate over this, use read_pins instead
HAL_StatusTypeDef read_pin(pca9539_t *pca, pca9539_reg_type_t reg_type, pca9539_port_t port, pca9539_pins_t pin, pca9539_pin_mode_t *config);
HAL_StatusTypeDef pca9539_read_pin(pca9539_t* pca, pca9539_reg_type_t reg_type, pca9539_port_t port,
pca9539_pins_t pin, bool* config);

/// @brief Write all pins on a bus, using reg_type OUTPUT to set logic level or DIRECTION to set as output
HAL_StatusTypeDef write_pins(pca9539_t *pca, pca9539_reg_type_t reg_type, pca9539_port_t port, pca9539_pin_mode_t config[8]);
/// @brief Write all pins on a bus, using reg_type OUTPUT to set logic level or DIRECTION to set as
/// output
HAL_StatusTypeDef pca9539_write_pins(pca9539_t* pca, pca9539_reg_type_t reg_type,
pca9539_port_t port, bool config[8]);
/// @brief Write a specific pin on a bus, do not iterate over this, use write_pins instead
HAL_StatusTypeDef write_pin(pca9539_t *pca, pca9539_reg_type_t reg_type, pca9539_port_t port, pca9539_pins_t pin, pca9539_pin_mode_t config);
HAL_StatusTypeDef pca9539_write_pin(pca9539_t* pca, pca9539_reg_type_t reg_type,
pca9539_port_t port, pca9539_pins_t pin,
bool config);

#endif
16 changes: 8 additions & 8 deletions general/src/pca9539.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ HAL_StatusTypeDef pca_read_reg(pca9539_t *pca, uint16_t address, pca9539_port_t
return HAL_I2C_Mem_Read(pca->i2c_handle, pca->dev_addr << 1, address, I2C_MEMADD_SIZE_8BIT, data, sizeof(data), TIMEOUT);
}

static uint8_t create_buf(pca9539_pin_mode_t config[8])
static uint8_t create_buf(bool config[8])
{
uint8_t data = 0;
for (uint8_t i = 0; i < 8; i++)
Expand All @@ -41,21 +41,21 @@ static uint8_t create_buf(pca9539_pin_mode_t config[8])
return data;
}

static void deconstruct_buf(uint8_t *data, pca9539_pin_mode_t *config)
static void deconstruct_buf(uint8_t *data, bool *config)
{
for (uint8_t i = 0; i < 8; i++)
{
config[i] = (*data >> i) & 1;
}
}

void pca9539_init(pca9539_t *pca, I2C_HandleTypeDef *i2c_handle, pca9539_addr_t *dev_addr)
void pca9539_init(pca9539_t *pca, I2C_HandleTypeDef *i2c_handle, uint8_t dev_addr)
{
pca->i2c_handle = i2c_handle;
pca->dev_addr = pca->dev_addr << 1u; /* shifted one to the left cuz STM says so */
pca->dev_addr = dev_addr << 1u; /* shifted one to the left cuz STM says so */
}

HAL_StatusTypeDef read_pins(pca9539_t *pca, pca9539_reg_type_t reg_type, pca9539_port_t port, pca9539_pin_mode_t config[8])
HAL_StatusTypeDef pca9539_read_pins(pca9539_t *pca, pca9539_reg_type_t reg_type, pca9539_port_t port, bool config[8])
{

uint8_t data = create_buf(config);
Expand All @@ -70,7 +70,7 @@ HAL_StatusTypeDef read_pins(pca9539_t *pca, pca9539_reg_type_t reg_type, pca9539
return status;
}

HAL_StatusTypeDef read_pin(pca9539_t *pca, pca9539_reg_type_t reg_type, pca9539_port_t port, pca9539_pins_t pin, pca9539_pin_mode_t *config)
HAL_StatusTypeDef pca9539_read_pin(pca9539_t *pca, pca9539_reg_type_t reg_type, pca9539_port_t port, pca9539_pins_t pin, bool *config)
{
uint8_t data;
HAL_StatusTypeDef status = pca_read_reg(pca, reg_type, port, &data);
Expand All @@ -84,14 +84,14 @@ HAL_StatusTypeDef read_pin(pca9539_t *pca, pca9539_reg_type_t reg_type, pca9539_
return status;
}

HAL_StatusTypeDef write_pins(pca9539_t *pca, pca9539_reg_type_t reg_type, pca9539_port_t port, pca9539_pin_mode_t config[8])
HAL_StatusTypeDef pca9539_write_pins(pca9539_t *pca, pca9539_reg_type_t reg_type, pca9539_port_t port, bool config[8])
{

uint8_t buf = create_buf(config);
return pca_write_reg(pca, reg_type, port, &buf);
}

HAL_StatusTypeDef write_pin(pca9539_t *pca, pca9539_reg_type_t reg_type, pca9539_port_t port, pca9539_pins_t pin, pca9539_pin_mode_t config)
HAL_StatusTypeDef pca9539_write_pin(pca9539_t *pca, pca9539_reg_type_t reg_type, pca9539_port_t port, pca9539_pins_t pin, bool config)
{

uint8_t data;
Expand Down

0 comments on commit deeb684

Please sign in to comment.