Files
foc_ccs/3rd/soft_i2c.c

118 lines
2.2 KiB
C

#include "soft_i2c.h"
#include "delay.h"
#include "ti_msp_dl_config.h"
#define DELAY 1
#define SDA_PIN_SHIFT 9 // if pa.8 shift8 , if pa.9 shift 9
uint32_t SCL_PIN, SDA_PIN;
uint32_t SDA_IOMUX;
GPIO_Regs *PORT;
void Set_Ang_Sensor(int Mot) {
if (Mot == 0) {
PORT = SOFT_I2C_PORT;
SDA_IOMUX = SOFT_I2C_SDA_IOMUX;
SCL_PIN = SOFT_I2C_CLK_PIN;
SDA_PIN = SOFT_I2C_SDA_PIN;
} else if (Mot == 1) {
}
}
void I2C_ENABLE_OUTPUT_SDA(void) {
DL_GPIO_initDigitalOutput(SDA_IOMUX);
DL_GPIO_enableOutput(PORT, SDA_PIN);
}
void I2C_ENABLE_INPUT_SDA(void) {
DL_GPIO_disableOutput(PORT, SDA_PIN);
DL_GPIO_initDigitalInputFeatures(
SDA_IOMUX, DL_GPIO_INVERSION_DISABLE, DL_GPIO_RESISTOR_PULL_UP,
DL_GPIO_HYSTERESIS_DISABLE, DL_GPIO_WAKEUP_DISABLE);
}
void I2C_W_SCL(uint8_t BitValue) {
if (BitValue) {
DL_GPIO_setPins(PORT, SCL_PIN);
} else {
DL_GPIO_clearPins(PORT, SCL_PIN);
}
delay_us(DELAY);
}
void I2C_W_SDA(uint8_t BitValue) {
if (BitValue) {
DL_GPIO_setPins(PORT, SDA_PIN);
} else {
DL_GPIO_clearPins(PORT, SDA_PIN);
}
delay_us(DELAY);
}
// 读取时钟线数据
uint8_t I2C_R_SDA(void) {
uint32_t BitValue;
BitValue = DL_GPIO_readPins(PORT, SDA_PIN);
return (uint8_t)(BitValue >> 9);
}
void I2C_Start(void) {
I2C_W_SCL(1);
I2C_W_SDA(1);
I2C_W_SDA(0);
I2C_W_SCL(0);
}
void I2C_Stop(void) {
I2C_W_SDA(0);
I2C_W_SCL(1);
I2C_W_SDA(1);
}
void I2C_SendByte(uint8_t Byte) {
uint8_t i;
for (i = 0; i < 8; i++) {
I2C_W_SDA(Byte & (0x80 >> i));
I2C_W_SCL(1);
I2C_W_SCL(0);
}
}
uint8_t I2C_RecviveData(void) {
uint8_t i, Byte = 0x00;
I2C_W_SDA(1);
for (i = 0; i < 8; i++) {
I2C_ENABLE_INPUT_SDA();
I2C_W_SCL(1);
if (I2C_R_SDA() == 1) {
Byte |= (0x80 >> i);
}
I2C_W_SCL(0);
I2C_ENABLE_OUTPUT_SDA();
}
return Byte;
}
void I2C_SendAck(uint8_t AckBit) {
I2C_W_SDA(AckBit);
I2C_W_SCL(1);
I2C_W_SCL(0);
}
uint8_t I2C_RecviveAck(void) {
uint8_t AckBit;
I2C_W_SDA(1);
I2C_ENABLE_INPUT_SDA();
I2C_W_SCL(1);
AckBit = I2C_R_SDA();
I2C_W_SCL(0);
I2C_ENABLE_OUTPUT_SDA();
return AckBit;
}