diff --git a/extras/i2c/i2c.c b/extras/i2c/i2c.c index ef19c45..7a44c4b 100644 --- a/extras/i2c/i2c.c +++ b/extras/i2c/i2c.c @@ -36,16 +36,23 @@ #define CLK_STRETCH (10) static bool started; +static bool pullups; static uint8_t g_scl_pin; static uint8_t g_sda_pin; void i2c_init(uint8_t scl_pin, uint8_t sda_pin) { started = false; + pullups = false; g_scl_pin = scl_pin; g_sda_pin = sda_pin; } +void i2c_pullups(bool enable) +{ + pullups = enable; +} + static void i2c_delay(void) { sdk_os_delay_us(CLK_HALF_PERIOD_US); @@ -54,14 +61,14 @@ static void i2c_delay(void) // Set SCL as input and return current level of line, 0 or 1 static bool read_scl(void) { - gpio_enable(g_scl_pin, GPIO_INPUT); + gpio_enable(g_scl_pin, pullups ? GPIO_INPUT_PULLUP : GPIO_INPUT); return gpio_read(g_scl_pin); // Clock high, valid ACK } // Set SDA as input and return current level of line, 0 or 1 static bool read_sda(void) { - gpio_enable(g_sda_pin, GPIO_INPUT); + gpio_enable(g_sda_pin, pullups ? GPIO_INPUT_PULLUP : GPIO_INPUT); // TODO: Without this delay we get arbitration lost in i2c_stop i2c_delay(); return gpio_read(g_sda_pin); // Clock high, valid ACK diff --git a/extras/i2c/i2c.h b/extras/i2c/i2c.h index ac3163f..5d709b8 100644 --- a/extras/i2c/i2c.h +++ b/extras/i2c/i2c.h @@ -49,3 +49,7 @@ bool i2c_slave_read(uint8_t slave_addr, uint8_t data, uint8_t *buf, uint32_t len // devices where the i2c_slave_[read|write] functions above are of no use. void i2c_start(void); void i2c_stop(void); + +// Use internal pull-up resistors instead of external ones, +// by default they are disabled. +void i2c_pullups(bool enable);