>>> i2c.scan()
[64]
Smallest scan freq is ca. 800 hz, or esp8266 resets.
To get the scan to work, I changed to write to address instead of read from address.
Code: Select all
STATIC mp_obj_t machine_i2c_scan(mp_obj_t self_in) {
machine_i2c_obj_t *self = MP_OBJ_TO_PTR(self_in);
mp_obj_t list = mp_obj_new_list(0, NULL);
// 7-bit addresses 0b0000xxx and 0b1111xxx are reserved
for (int addr = 0x08; addr < 0x78; ++addr) {
mp_hal_i2c_start(self);
//int ack = mp_hal_i2c_write_byte(self, (addr << 1) | 1);
int ack = mp_hal_i2c_write_byte(self, (addr << 1)); // PB
if (ack) {
mp_obj_list_append(list, MP_OBJ_NEW_SMALL_INT(addr));
}
mp_hal_i2c_stop(self);
}
return list;
}
>>> i2c.writeto(0x40, '231')
>>> i2c.readfrom(0x40, 1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OSError: I2C bus error
>>> i2c.writeto(0x40, b'\xE7') # 231
>>> i2c.readfrom(0x40, 1)
b'\x02'
To get clock stretching to work, I made these changes.
Code: Select all
STATIC int mp_hal_i2c_sda_read(machine_i2c_obj_t *self) {
return mp_hal_pin_read(self->sda);
}
STATIC int mp_hal_i2c_scl_read(machine_i2c_obj_t *self) { // PB
return mp_hal_pin_read(self->scl);
}
STATIC int mp_hal_i2c_read_byte(machine_i2c_obj_t *self, uint8_t *val, int nack) {
mp_hal_i2c_delay(self);
mp_hal_i2c_scl_low(self);
mp_hal_i2c_delay(self);
uint8_t data = 0;
for (int i = 7; i >= 0; i--) {
mp_hal_i2c_scl_release(self);
while (mp_hal_i2c_scl_read(self) == 0) { // PB
// clock stretching
}
mp_hal_i2c_delay(self);
data = (data << 1) | mp_hal_i2c_sda_read(self);
mp_hal_i2c_scl_low(self);
mp_hal_i2c_delay(self);
}
*val = data;
// send ack/nack bit
if (!nack) {
mp_hal_i2c_sda_low(self);
}
mp_hal_i2c_delay(self);
mp_hal_i2c_scl_release(self);
mp_hal_i2c_delay(self);
mp_hal_i2c_scl_low(self);
mp_hal_i2c_sda_release(self);
return 1; // success
}
[edit]
And now temp and humidy and dewpoint comes in.
temp: 22.08019 relH: 32.7497 dew: 5.016602
temp: 22.08019 relH: 32.7497 dew: 5.016602
temp: 22.05873 relH: 32.71918 dew: 4.984558
temp: 22.06947 relH: 32.69629 dew: 4.983948
temp: 22.08019 relH: 32.69629 dew: 4.993347