Added possibility to enable internal pull-up resistors with i2c_pullups(boolean)
This commit is contained in:
parent
e12077513f
commit
0ee04e985a
2 changed files with 13 additions and 2 deletions
|
@ -36,16 +36,23 @@
|
||||||
#define CLK_STRETCH (10)
|
#define CLK_STRETCH (10)
|
||||||
|
|
||||||
static bool started;
|
static bool started;
|
||||||
|
static bool pullups;
|
||||||
static uint8_t g_scl_pin;
|
static uint8_t g_scl_pin;
|
||||||
static uint8_t g_sda_pin;
|
static uint8_t g_sda_pin;
|
||||||
|
|
||||||
void i2c_init(uint8_t scl_pin, uint8_t sda_pin)
|
void i2c_init(uint8_t scl_pin, uint8_t sda_pin)
|
||||||
{
|
{
|
||||||
started = false;
|
started = false;
|
||||||
|
pullups = false;
|
||||||
g_scl_pin = scl_pin;
|
g_scl_pin = scl_pin;
|
||||||
g_sda_pin = sda_pin;
|
g_sda_pin = sda_pin;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void i2c_pullups(bool enable)
|
||||||
|
{
|
||||||
|
pullups = enable;
|
||||||
|
}
|
||||||
|
|
||||||
static void i2c_delay(void)
|
static void i2c_delay(void)
|
||||||
{
|
{
|
||||||
sdk_os_delay_us(CLK_HALF_PERIOD_US);
|
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
|
// Set SCL as input 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, pullups ? GPIO_INPUT_PULLUP : GPIO_INPUT);
|
||||||
return gpio_read(g_scl_pin); // Clock high, valid ACK
|
return gpio_read(g_scl_pin); // Clock high, valid ACK
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set SDA as input and return current level of line, 0 or 1
|
// Set SDA as input and return current level of line, 0 or 1
|
||||||
static bool read_sda(void)
|
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
|
// TODO: Without this delay we get arbitration lost in i2c_stop
|
||||||
i2c_delay();
|
i2c_delay();
|
||||||
return gpio_read(g_sda_pin); // Clock high, valid ACK
|
return gpio_read(g_sda_pin); // Clock high, valid ACK
|
||||||
|
|
|
@ -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.
|
// devices where the i2c_slave_[read|write] functions above are of no use.
|
||||||
void i2c_start(void);
|
void i2c_start(void);
|
||||||
void i2c_stop(void);
|
void i2c_stop(void);
|
||||||
|
|
||||||
|
// Use internal pull-up resistors instead of external ones,
|
||||||
|
// by default they are disabled.
|
||||||
|
void i2c_pullups(bool enable);
|
||||||
|
|
Loading…
Reference in a new issue