I2C changes to get the HTU21D working.

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
User avatar
Frida
Posts: 34
Joined: Sat Jan 30, 2016 2:20 pm
Location: Middelfart, Denmark

I2C changes to get the HTU21D working.

Post by Frida » Tue Apr 12, 2016 7:21 pm

In the latest commit eec8a94 I have made some changes.

>>> 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') dosent work, but that does i2c.writeto(0x40, b'\xE7') # 231.

>>> 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
}
All done in file machine_i2c.c in /micropython/extmod


[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
Yes Frida is my watchdog!

User avatar
Frida
Posts: 34
Joined: Sat Jan 30, 2016 2:20 pm
Location: Middelfart, Denmark

Re: I2C changes to get the HTU21D working.

Post by Frida » Fri Apr 15, 2016 1:21 pm

And now with timeout

Code: Select all

        uint32_t start = mp_hal_ticks_ms(); // PB
        while (mp_hal_i2c_scl_read(self) == 0) { // PB
            // clock stretching
            if(mp_hal_ticks_ms() - start > 55) { // max 50ms/14bit/temp
              mp_hal_i2c_stop(self);
              nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "I2C bus error clock stretching"));
            }
        }
Yes Frida is my watchdog!

Lornioiz
Posts: 36
Joined: Wed Aug 03, 2016 11:39 am
Location: Florence, Italy

Re: I2C changes to get the HTU21D working.

Post by Lornioiz » Fri Jan 06, 2017 12:10 pm

Hi there!

I was trying to make a simple HTU21D driver but I got stuck very early because it does seems that I can't find the peripheral address.
When I execute i2c.scan() this is what I get back:

[8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119]

I then found your post and I'm wondering if this is the problem that made you create the fix.
If so, can you explain me how to implement it? where should put this code?

thanks an regards

User avatar
Frida
Posts: 34
Joined: Sat Jan 30, 2016 2:20 pm
Location: Middelfart, Denmark

Re: I2C changes to get the HTU21D working.

Post by Frida » Fri Jan 06, 2017 1:06 pm

I haven't looked at it for a long time.
I put the code in: 'machine_i2c.c in /micropython/extmod'
But are you using pullup on SCL and SDA?

And the HTU21D was part of stretch goal, but I don't know of it is implementet yet?
Yes Frida is my watchdog!

Lornioiz
Posts: 36
Joined: Wed Aug 03, 2016 11:39 am
Location: Florence, Italy

Re: I2C changes to get the HTU21D working.

Post by Lornioiz » Fri Jan 06, 2017 3:52 pm

Frida wrote: But are you using pullup on SCL and SDA?

And the HTU21D was part of stretch goal, but I don't know of it is implementet yet?
The breakout I'm using as already pullups so I don't think that's the problem.
I Think I'm gonna try the fixes you wrote, thanks alot!

P.S.

I don't think the driver is implemented yet (but I'm not sure).

joeheart
Posts: 1
Joined: Sat Sep 30, 2017 3:28 pm

Re: I2C changes to get the HTU21D working.

Post by joeheart » Sat Sep 30, 2017 3:37 pm

in the neweast esp8266 firmware , i2c.scan() still can't found the address

>>>i2c.scan()
>>>[]

same htu21d connect in esp32 board ,use micropython,works good.

please someone help me.thanks a lot

dentex
Posts: 6
Joined: Wed Oct 25, 2017 4:49 pm
Contact:

Re: I2C changes to get the HTU21D working.

Post by dentex » Mon Jan 14, 2019 10:02 am

Hello,
I'm having the same problem with no results from I2C scan. Do you have now a solution? Thanks.

Running esp8266-20180511-v1.9.4.bin (elf, map) (latest)

port_diag:

Code: Select all

FlashROM:
Flash ID: 1640ef (Vendor: ef Device: 4016)
Flash bootloader data:
Byte @2: 02
Byte @3: 40 (Flash size: 4MB Flash freq: 40MHZ)
Firmware checksum:
size: 604856
md5: 53b9852a38ac9f9e27694b3123fd1420
True

Networking:
STA ifconfig: ('192.168.100.55', '255.255.255.0', '192.168.100.1', '192.168.100.101')
AP ifconfig: ('192.168.4.1', '255.255.255.0', '192.168.4.1', '192.168.100.101')
Free WiFi driver buffers of type:
0: 8 (1,2 TX)
1: 0 (4 Mngmt TX(len: 0x41-0x100))
2: 8 (5 Mngmt TX (len: 0-0x40))
3: 4 (7)
4: 7 (8 RX)
lwIP PCBs:
Active PCB states:
Listen PCB states:
Local port 8266, foreign port 58494 snd_nxt 272 rcv_nxt 1073717000 State: LISTEN
TIME-WAIT PCB states:

Post Reply