test soft i2c write and read passed
This commit is contained in:
@@ -1,20 +1,46 @@
|
||||
#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
|
||||
void I2C_ENABLE_OUTPUT_SDA(void) {
|
||||
DL_GPIO_initDigitalOutput(SOFT_I2C_SDA_IOMUX);
|
||||
DL_GPIO_enableOutput(SOFT_I2C_PORT, SOFT_I2C_SDA_PIN);
|
||||
}
|
||||
void I2C_ENABLE_INPUT_SDA(void) {
|
||||
DL_GPIO_disableOutput(SOFT_I2C_PORT, SOFT_I2C_SDA_PIN);
|
||||
DL_GPIO_initDigitalInputFeatures(
|
||||
SOFT_I2C_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) {
|
||||
DL_GPIO_writePinsVal(SOFT_I2C_PORT, SOFT_I2C_CLK_PIN, BitValue);
|
||||
if (BitValue) {
|
||||
DL_GPIO_setPins(SOFT_I2C_PORT, SOFT_I2C_CLK_PIN);
|
||||
|
||||
} else {
|
||||
DL_GPIO_clearPins(SOFT_I2C_PORT, SOFT_I2C_CLK_PIN);
|
||||
}
|
||||
delay_us(DELAY);
|
||||
}
|
||||
|
||||
void I2C_W_SDA(uint8_t BitValue) {
|
||||
DL_GPIO_writePinsVal(SOFT_I2C_PORT, SOFT_I2C_SDA_PIN, BitValue);
|
||||
if (BitValue) {
|
||||
DL_GPIO_setPins(SOFT_I2C_PORT, SOFT_I2C_SDA_PIN);
|
||||
|
||||
} else {
|
||||
DL_GPIO_clearPins(SOFT_I2C_PORT, SOFT_I2C_SDA_PIN);
|
||||
}
|
||||
delay_us(DELAY);
|
||||
}
|
||||
|
||||
// 读取时钟线数据
|
||||
uint8_t I2C_R_SDA(void) {
|
||||
uint8_t BitValue;
|
||||
uint32_t BitValue;
|
||||
BitValue = DL_GPIO_readPins(SOFT_I2C_PORT, SOFT_I2C_SDA_PIN);
|
||||
|
||||
return BitValue;
|
||||
return (uint8_t)(BitValue >> 9);
|
||||
}
|
||||
|
||||
void I2C_Start(void) {
|
||||
@@ -44,11 +70,13 @@ 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;
|
||||
}
|
||||
@@ -64,9 +92,12 @@ 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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user