Added possibility to enable internal pull-up resistors with i2c_pullups(boolean)

This commit is contained in:
Karl Kangur 2016-02-20 16:40:25 +01:00
parent e12077513f
commit 0ee04e985a
2 changed files with 13 additions and 2 deletions

View file

@ -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