Merge pull request #159 from pimoroni/patch-i2c-micropython-repl

Reset unused I2C pins to default when an instance is initialised
pull/160/head
Philip Howard 2021-05-21 15:27:13 +01:00 zatwierdzone przez GitHub
commit 32277ff8c5
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
2 zmienionych plików z 18 dodań i 2 usunięć

Wyświetl plik

@ -3,14 +3,28 @@
namespace pimoroni {
void I2C::init() {
i2c = ((sda / 2) & 0b1) ? i2c1 : i2c0;
i2c = pin_to_inst(sda);
// TODO call pin_to_inst on sda and scl, and verify they are a valid i2c pin pair
// TODO maybe also fall back to PIO i2c for non-standard pin combinations
// Since it's easy to leave the I2C in a bad state when experimenting in the MicroPython REPL
// this loop will find any I2C pins relevant to the current instance and reset them.
for(auto pin = 0u; pin < 30; pin++) {
if(pin_to_inst(pin) == i2c && gpio_get_function(pin) == GPIO_FUNC_I2C) {
gpio_disable_pulls(pin);
gpio_set_function(pin, GPIO_FUNC_NULL);
}
}
i2c_init(i2c, baudrate);
gpio_set_function(sda, GPIO_FUNC_I2C); gpio_pull_up(sda);
gpio_set_function(scl, GPIO_FUNC_I2C); gpio_pull_up(scl);
}
i2c_inst_t* I2C::pin_to_inst(uint pin) {
return ((pin >> 1) & 0b1) ? i2c1 : i2c0;
}
/* Basic wrappers for devices using i2c functions directly */
int I2C::write_blocking(uint8_t addr, const uint8_t *src, size_t len, bool nostop) {
return i2c_write_blocking(i2c, addr, src, len, nostop);

Wyświetl plik

@ -47,6 +47,8 @@ namespace pimoroni {
gpio_set_function(scl, GPIO_FUNC_NULL);
}
i2c_inst_t* pin_to_inst(uint pin);
void reg_write_uint8(uint8_t address, uint8_t reg, uint8_t value);
uint8_t reg_read_uint8(uint8_t address, uint8_t reg);
uint16_t reg_read_uint16(uint8_t address, uint8_t reg);