110 lines
5.2 KiB
C
110 lines
5.2 KiB
C
#ifndef __TCA6416_H
|
||
#define __TCA6416_H
|
||
|
||
#include "main.h"
|
||
#include <stdint.h>
|
||
|
||
/* GPIO定义 */
|
||
//第一条总线
|
||
#define TCA6416_SCL_PIN GPIO_PIN_2
|
||
#define TCA6416_SCL_PORT GPIOE
|
||
#define TCA6416_SDA_PIN GPIO_PIN_3
|
||
#define TCA6416_SDA_PORT GPIOE
|
||
|
||
//第二条总线
|
||
#define TCA6416_SCL_PIN2 GPIO_PIN_5
|
||
#define TCA6416_SCL_PORT2 GPIOE
|
||
#define TCA6416_SDA_PIN2 GPIO_PIN_4
|
||
#define TCA6416_SDA_PORT2 GPIOE
|
||
|
||
/* TCA6416 I2C地址 */
|
||
#define TCA6416_ADDR 0x20 // 第一个芯片地址 (A0=A1=A2=GND)单独总线,SDA PE3 SCL PE2
|
||
#define TCA6416_ADDR2 0x21 // 第二个芯片地址 (A0=1, A1=A2=GND)与TCA6416_ADDR3共用总线,SDA PE4 SCL PE5
|
||
#define TCA6416_ADDR3 0x20 // 第三个芯片地址 (与第二个芯片共用总线,地址0x20)
|
||
|
||
|
||
/* 寄存器地址定义 */
|
||
#define TCA6416_INPUT_PORT0 0x00 // 输入端口0
|
||
#define TCA6416_INPUT_PORT1 0x01 // 输入端口1
|
||
#define TCA6416_OUTPUT_PORT0 0x02 // 输出端口0
|
||
#define TCA6416_OUTPUT_PORT1 0x03 // 输出端口1
|
||
#define TCA6416_CONFIG_PORT0 0x06 // 配置端口0
|
||
#define TCA6416_CONFIG_PORT1 0x07 // 配置端口1
|
||
#define TCA6416_POL_INV_PORT0 0x04 // 极性反转端口0
|
||
#define TCA6416_POL_INV_PORT1 0x05 // 极性反转端口1
|
||
|
||
/* 软件I2C延时定义 */
|
||
#define I2C_DELAY() HAL_Delay(5) // 增加延时时间到5ms
|
||
|
||
/* 第一个芯片的软件I2C基本操作函数 */
|
||
#define SCL_HIGH() HAL_GPIO_WritePin(TCA6416_SCL_PORT, TCA6416_SCL_PIN, GPIO_PIN_SET)
|
||
#define SCL_LOW() HAL_GPIO_WritePin(TCA6416_SCL_PORT, TCA6416_SCL_PIN, GPIO_PIN_RESET)
|
||
#define SDA_HIGH() HAL_GPIO_WritePin(TCA6416_SDA_PORT, TCA6416_SDA_PIN, GPIO_PIN_SET)
|
||
#define SDA_LOW() HAL_GPIO_WritePin(TCA6416_SDA_PORT, TCA6416_SDA_PIN, GPIO_PIN_RESET)
|
||
#define SDA_READ() HAL_GPIO_ReadPin(TCA6416_SDA_PORT, TCA6416_SDA_PIN)
|
||
|
||
/* SDA输入/输出模式切换宏 */
|
||
#define SDA_INPUT_MODE() do { \
|
||
GPIO_InitTypeDef GPIO_InitStruct = {0}; \
|
||
GPIO_InitStruct.Pin = TCA6416_SDA_PIN; \
|
||
GPIO_InitStruct.Mode = GPIO_MODE_INPUT; \
|
||
GPIO_InitStruct.Pull = GPIO_NOPULL; \
|
||
HAL_GPIO_Init(TCA6416_SDA_PORT, &GPIO_InitStruct); \
|
||
} while(0)
|
||
|
||
#define SDA_OUTPUT_MODE() do { \
|
||
GPIO_InitTypeDef GPIO_InitStruct = {0}; \
|
||
GPIO_InitStruct.Pin = TCA6416_SDA_PIN; \
|
||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD; \
|
||
GPIO_InitStruct.Pull = GPIO_NOPULL; \
|
||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM; \
|
||
HAL_GPIO_Init(TCA6416_SDA_PORT, &GPIO_InitStruct); \
|
||
} while(0)
|
||
|
||
/* 第二个芯片的软件I2C基本操作函数 */
|
||
#define SCL_HIGH2() HAL_GPIO_WritePin(TCA6416_SCL_PORT2, TCA6416_SCL_PIN2, GPIO_PIN_SET)
|
||
#define SCL_LOW2() HAL_GPIO_WritePin(TCA6416_SCL_PORT2, TCA6416_SCL_PIN2, GPIO_PIN_RESET)
|
||
#define SDA_HIGH2() HAL_GPIO_WritePin(TCA6416_SDA_PORT2, TCA6416_SDA_PIN2, GPIO_PIN_SET)
|
||
#define SDA_LOW2() HAL_GPIO_WritePin(TCA6416_SDA_PORT2, TCA6416_SDA_PIN2, GPIO_PIN_RESET)
|
||
#define SDA_READ2() HAL_GPIO_ReadPin(TCA6416_SDA_PORT2, TCA6416_SDA_PIN2)
|
||
|
||
/* 函数声明 */
|
||
|
||
uint8_t TCA6416_Init(void);
|
||
uint8_t TCA6416_Init2(void); // 第二个芯片的初始化函数
|
||
uint8_t TCA6416_Init3(void); // 第三个芯片的初始化函数
|
||
uint8_t TCA6416_WritePort(uint8_t port, uint8_t data);
|
||
uint8_t TCA6416_WritePort2(uint8_t port, uint8_t data); // 第二个芯片的写端口函数
|
||
uint8_t TCA6416_WritePort3(uint8_t port, uint8_t data); // 第三个芯片的写端口函数
|
||
uint8_t TCA6416_ReadPort(uint8_t port, uint8_t *data);
|
||
uint8_t TCA6416_ReadPort2(uint8_t port, uint8_t *data); // 第二个芯片的读端口函数
|
||
uint8_t TCA6416_ReadPort3(uint8_t port, uint8_t *data); // 第三个芯片的读端口函数
|
||
uint8_t TCA6416_SetPortDirection(uint8_t port, uint8_t direction);
|
||
uint8_t TCA6416_SetPortDirection2(uint8_t port, uint8_t direction); // 第二个芯片的设置方向函数
|
||
uint8_t TCA6416_SetPortDirection3(uint8_t port, uint8_t direction); // 第三个芯片的设置方向函数
|
||
uint8_t TCA6416_SetPortPolarity(uint8_t port, uint8_t polarity);
|
||
uint8_t TCA6416_SetPortPolarity2(uint8_t port, uint8_t polarity); // 第二个芯片的设置极性函数
|
||
uint8_t TCA6416_SetPortPolarity3(uint8_t port, uint8_t polarity); // 第三个芯片的设置极性函数
|
||
uint8_t TCA6416_WritePin(uint8_t port, uint8_t pin, uint8_t state);
|
||
uint8_t TCA6416_ReadPin(uint8_t port, uint8_t pin, uint8_t *state);
|
||
|
||
/* 软件I2C底层函数 */
|
||
void I2C_Start(void);
|
||
void I2C_Start2(void); // 第二个芯片的I2C起始信号
|
||
void I2C_Stop(void);
|
||
void I2C_Stop2(void); // 第二个芯片的I2C停止信号
|
||
void I2C_Ack(void);
|
||
void I2C_Ack2(void); // 第二个芯片的I2C应答
|
||
void I2C_NAck(void);
|
||
void I2C_NAck2(void); // 第二个芯片的I2C非应答
|
||
uint8_t I2C_WaitAck(void);
|
||
uint8_t I2C_WaitAck2(void); // 第二个芯片的等待应答
|
||
uint8_t I2C_SendByte(uint8_t byte); // 修正返回值类型
|
||
void I2C_SendByte2(uint8_t byte); // 第二个芯片的发送字节
|
||
uint8_t I2C_ReadByte(void);
|
||
uint8_t I2C_ReadByte2(void); // 第二个芯片的读取字节
|
||
void TCA6416_GPIO_Init(void);
|
||
void TCA6416_GPIO_Init2(void); // 第二个芯片的I2C初始化
|
||
|
||
#endif /* __TCA6416_H */
|