130 lines
4.3 KiB
C
130 lines
4.3 KiB
C
#include "tca9555.h"
|
||
#include "i2cs.h"
|
||
#define I2C_MASTER_SCL_PORT I2C1_SCL_GPIO_Port
|
||
#define I2C_MASTER_SCL_PIN I2C1_SCL_Pin
|
||
#define I2C_MASTER_SDA_PORT I2C1_SDA_GPIO_Port
|
||
#define I2C_MASTER_SDA_PIN I2C1_SDA_Pin
|
||
static i2c_t *tca9555_i2c;
|
||
|
||
void tca9555_init(void)
|
||
{
|
||
i2c_gpio_group_t gpios;
|
||
gpios.scl = gpio_create(I2C_MASTER_SCL_PORT, I2C_MASTER_SCL_PIN);
|
||
gpios.sda = gpio_create(I2C_MASTER_SDA_PORT, I2C_MASTER_SDA_PIN);
|
||
|
||
tca9555_i2c = i2c_create(gpios, 0);
|
||
|
||
tca9555_register reg;
|
||
osel_memset((uint8_t *)®, 0, sizeof(tca9555_register));
|
||
tca9555_write_config(®);
|
||
|
||
osel_memset((uint8_t *)®, 0, sizeof(tca9555_register));
|
||
reg.port.p0.asint = 0;
|
||
reg.port.p1.asint = 0;
|
||
tca9555_write_output(®);
|
||
}
|
||
|
||
void tca9555_configure_input(tca9555_register *reg, uint8_t port, uint8_t pin)
|
||
{
|
||
if (port == 0)
|
||
{
|
||
reg->port.p0.asint |= (1 << pin); // 设置对应的引脚为输入
|
||
}
|
||
else if (port == 1)
|
||
{
|
||
reg->port.p1.asint |= (1 << pin); // 设置对应的引脚为输入
|
||
}
|
||
tca9555_write_config(reg);
|
||
}
|
||
|
||
uint8_t tca9555_read_single_register(tca9555_reg_t address)
|
||
{
|
||
uint8_t reg_data = 0; // 定义一个8位无符号字符型变量reg_data,用于存储读取到的寄存器值
|
||
tca9555_i2c->interface.start(tca9555_i2c);
|
||
tca9555_i2c->interface.write_byte(tca9555_i2c, WRITE_ADDRESS);
|
||
tca9555_i2c->interface.wait_ack(tca9555_i2c);
|
||
tca9555_i2c->interface.write_byte(tca9555_i2c, address);
|
||
tca9555_i2c->interface.wait_ack(tca9555_i2c);
|
||
|
||
tca9555_i2c->interface.start(tca9555_i2c);
|
||
tca9555_i2c->interface.write_byte(tca9555_i2c, READ_ADDRESS);
|
||
tca9555_i2c->interface.wait_ack(tca9555_i2c);
|
||
reg_data = tca9555_i2c->interface.read_byte(tca9555_i2c, FALSE);
|
||
tca9555_i2c->interface.stop(tca9555_i2c);
|
||
return reg_data;
|
||
}
|
||
|
||
void tca9555_write_single_register(tca9555_reg_t address, uint8_t reg_val)
|
||
{
|
||
tca9555_i2c->interface.start(tca9555_i2c);
|
||
tca9555_i2c->interface.write_byte(tca9555_i2c, WRITE_ADDRESS);
|
||
tca9555_i2c->interface.wait_ack(tca9555_i2c);
|
||
tca9555_i2c->interface.write_byte(tca9555_i2c, address);
|
||
tca9555_i2c->interface.wait_ack(tca9555_i2c);
|
||
tca9555_i2c->interface.write_byte(tca9555_i2c, reg_val);
|
||
tca9555_i2c->interface.wait_ack(tca9555_i2c);
|
||
tca9555_i2c->interface.stop(tca9555_i2c);
|
||
}
|
||
|
||
void tca9555_read_struct(tca9555_register *reg, tca9555_reg_t reg_num)
|
||
{
|
||
tca9555_i2c->interface.start(tca9555_i2c);
|
||
tca9555_i2c->interface.write_byte(tca9555_i2c, WRITE_ADDRESS);
|
||
tca9555_i2c->interface.wait_ack(tca9555_i2c);
|
||
|
||
tca9555_i2c->interface.start(tca9555_i2c);
|
||
tca9555_i2c->interface.write_byte(tca9555_i2c, READ_ADDRESS);
|
||
tca9555_i2c->interface.wait_ack(tca9555_i2c);
|
||
|
||
reg->port.p0.asint = tca9555_i2c->interface.read_byte(tca9555_i2c, TRUE);
|
||
reg->port.p1.asint = tca9555_i2c->interface.read_byte(tca9555_i2c, FALSE);
|
||
|
||
tca9555_i2c->interface.stop(tca9555_i2c);
|
||
}
|
||
void tca9555_write_struct(tca9555_register *reg, tca9555_reg_t reg_num)
|
||
{
|
||
uint8_t reg_data[2];
|
||
osel_memcpy(reg_data, (uint8_t *)reg, sizeof(tca9555_register));
|
||
tca9555_i2c->interface.start(tca9555_i2c);
|
||
tca9555_i2c->interface.write_byte(tca9555_i2c, WRITE_ADDRESS);
|
||
tca9555_i2c->interface.wait_ack(tca9555_i2c);
|
||
|
||
tca9555_i2c->interface.write_byte(tca9555_i2c, reg_num);
|
||
tca9555_i2c->interface.wait_ack(tca9555_i2c);
|
||
tca9555_i2c->interface.write_byte(tca9555_i2c, reg_data[0]);
|
||
tca9555_i2c->interface.wait_ack(tca9555_i2c);
|
||
tca9555_i2c->interface.write_byte(tca9555_i2c, reg_data[1]);
|
||
tca9555_i2c->interface.wait_ack(tca9555_i2c);
|
||
tca9555_i2c->interface.stop(tca9555_i2c);
|
||
}
|
||
|
||
void tca9555_write_output(tca9555_register *reg)
|
||
{
|
||
tca9555_write_struct(reg, TCA9555_OUTPUT_REG0);
|
||
}
|
||
void tca9555_write_polarity(tca9555_register *reg)
|
||
{
|
||
tca9555_write_struct(reg, TCA9555_POLARITY_REG0);
|
||
}
|
||
void tca9555_write_config(tca9555_register *reg)
|
||
{
|
||
tca9555_write_struct(reg, TCA9555_CONFIG_REG0);
|
||
}
|
||
|
||
void tca9555_read_input(tca9555_register *reg)
|
||
{
|
||
tca9555_write_struct(reg, TCA9555_INPUT_REG0);
|
||
}
|
||
void tca9555_read_output(tca9555_register *reg)
|
||
{
|
||
tca9555_write_struct(reg, TCA9555_OUTPUT_REG0);
|
||
}
|
||
void tca9555_read_polarity(tca9555_register *reg)
|
||
{
|
||
tca9555_write_struct(reg, TCA9555_POLARITY_REG0);
|
||
}
|
||
void tca9555_read_config(tca9555_register *reg)
|
||
{
|
||
tca9555_write_struct(reg, TCA9555_CONFIG_REG0);
|
||
}
|