Merge pull request #164 from ourairquality/i2c-open-drain

i2c: use open drain outputs.
This commit is contained in:
Johan Kanflo 2016-07-18 22:01:34 +02:00 committed by GitHub
commit 38375e218a

View file

@ -44,6 +44,19 @@ void i2c_init(uint8_t scl_pin, uint8_t sda_pin)
started = false; started = false;
g_scl_pin = scl_pin; g_scl_pin = scl_pin;
g_sda_pin = sda_pin; g_sda_pin = sda_pin;
// Just to prevent these pins floating too much if not connected.
gpio_set_pullup(g_scl_pin, 1, 1);
gpio_set_pullup(g_sda_pin, 1, 1);
// I2C bus idle state.
gpio_enable(g_scl_pin, GPIO_INPUT);
gpio_enable(g_scl_pin, GPIO_INPUT);
// Set the pins to a low output state for when they are configured
// as outputs.
gpio_write(g_scl_pin, 0);
gpio_write(g_sda_pin, 0);
} }
static void i2c_delay(void) static void i2c_delay(void)
@ -51,7 +64,8 @@ static void i2c_delay(void)
sdk_os_delay_us(CLK_HALF_PERIOD_US); sdk_os_delay_us(CLK_HALF_PERIOD_US);
} }
// Set SCL as input and return current level of line, 0 or 1 // Set SCL as input, allowing it to float high, and return current
// level of line, 0 or 1
static bool read_scl(void) static bool read_scl(void)
{ {
gpio_enable(g_scl_pin, GPIO_INPUT); gpio_enable(g_scl_pin, GPIO_INPUT);
@ -70,15 +84,13 @@ static bool read_sda(void)
// Actively drive SCL signal low // Actively drive SCL signal low
static void clear_scl(void) static void clear_scl(void)
{ {
gpio_enable(g_scl_pin, GPIO_OUTPUT); gpio_enable(g_scl_pin, GPIO_OUT_OPEN_DRAIN);
gpio_write(g_scl_pin, 0);
} }
// Actively drive SDA signal low // Actively drive SDA signal low
static void clear_sda(void) static void clear_sda(void)
{ {
gpio_enable(g_sda_pin, GPIO_OUTPUT); gpio_enable(g_sda_pin, GPIO_OUT_OPEN_DRAIN);
gpio_write(g_sda_pin, 0);
} }
// Output start condition // Output start condition