I2C clock streching on esp8266.

Posted on
  • I have made clock streching for HTU21D.
    I need to get JsError to work. I've tried different header file, but I get compile errors.
    Any help is welcome.

    I made a git clone from @TVE and made some changes to the file: Espruino / targets / esp8266 / i2c_master.c.

    Here are the file:

    [#define](https://forum.espruino.com/sea­rch/?q=%23define) i2c_master_getDD(void) ((GPIO_REG_READ(GPIO_IN_ADDRESS) >> pinSCL) & 1) // PB
    
    /***************************************­*************************************** PB
     * FunctionName : i2c_master_getDD
     * Description  : Internal used function -
     *                    get i2c SCL bit value
     * Parameters   : NONE
     * Returns      : uint8 - SCL bit value
    ****************************************­***************************************/­
    [#if](https://forum.espruino.com/search/­?q=%23if) 0
    LOCAL uint8 CALLED_FROM_INTERRUPT
    i2c_master_getDD(void)
    {
        return (GPIO_REG_READ(GPIO_IN_ADDRESS) >> pinSCL) & 1;
    }
    [#endif](https://forum.espruino.com/sear­ch/?q=%23endif)
    
    /***************************************­***************************************
    
    /***************************************­***************************************
     * FunctionName : i2c_master_readByte
     * Description  : read Byte from i2c bus
     * Parameters   : NONE
     * Returns      : uint8 - readed value
    ****************************************­***************************************/­
    uint8 ICACHE_FLASH_ATTR
    i2c_master_readByte(void)
    {
        uint8 retVal = 0;
        uint8 i;
    
        i2c_master_wait(2);
        i2c_master_setDC(m_nLastSDA, 0);
        i2c_master_wait(4);	// sda 1, scl 0
    
        for (i = 0; i < 8; i++) {
            i2c_master_setDC(1, 0);
            i2c_master_wait(4);	// sda 1, scl 0
            i2c_master_setDC(1, 1);
            i2c_master_wait(3);	// sda 1, scl 1
            uint8 count = 200; // max 50ms/14bit // PB 50000/1000*4=200
            while (i2c_master_getDD()==0 && count--) { // PB 36000/1000*4=144
              i2c_master_wait(1000); // wait 1000/4=250 usec // PB
            };
    		if(count==0) { // PB
              goto error;
            };
            //retVal = (retVal<<1) | (i2c_master_getDC()&1);
            retVal = (retVal<<1) | (i2c_master_getDC()); // PB
        }
        i2c_master_setDC(1, 0);
        i2c_master_wait(2);	// sda 1, scl 0
    
        return retVal;
    error:
      i2c_master_stop();
    //  jsError("> I2C streching too long\n");
      return -1;
    }
    

    Should I mention it is on a esp8266-01 from my Pico kickstarter set.


    1 Attachment

  • please try

    #include "jsutils.h"

    ./src/jsutils.h
    void jsError(const char *fmt, ...);

    NO - lots of compile errors ......

  • using printf - remove \ infront of #define - line was not displayed correct without

    // insert after last [#include](https://forum.espruino.com/se­arch/?q=%23include)
    extern int os_printf_plus(const char *format, ...)  __attribute__((format(printf, 1, 2)));
    \#define printf os_printf_plus
    
    .......
    
    error:
      i2c_master_stop();
      //jsError("> I2C streching too long\n");
      printf("> I2C streching too long\n");
      return -1; 
    }
    

    compile output:

    targets/esp8266/i2c_master.c: In function 'i2c_master_readByte':
    targets/esp8266/i2c_master.c:330:9: error: implicit declaration of function 'i2c_master_getDD' [-Werror=implicit-function-declaration]
             while ( i2c_master_getDD()==0 && count--) { // PB 36000/1000*4=144
             ^
    targets/esp8266/i2c_master.c: At top level:
    
    
    
  • typo : i2c_master_getDD -> i2c_master_getDC ?

    compiles ;-)

  • Don't change "i2c_master_getDD" it's read the scl line.
    and "i2c_master_getDC" read the sda line.

    Try the Attachment and see if it compiles ok.

  • ups - ok - adding that missing #define and C back to D

    Now it compiles without errors

    attached the version including your changes and printf()

    Frida - Thanks for sharing your work !


    1 Attachment

  • I was hoping I could use JsError as in the file jshardware.c, where the call come from, maybe someone has a solution.

    As you may see, I have tried with a lower value, to test that, so I could see that it worked.

    I don't now if it work with different clockfreq, but try it, and let me know.

  • Did you adjust the timing constants to maintain the overall clock rate? It seems that the addition of your code must have affected the performance...

    What does the 'DD' in i2c_master_getDD stand for? For reference, the DC in i2c_master_setDC stands for Data/Clock.

  • I am new to all this stof. There are lots I do not understand. But it is a wonderful world to dive into.

    Did you adjust the timing constants to maintain the overall clock rate? It seems that the addition of your code must have affected the performance...

    No my only wish was to make clock streching working for my HTU21D.
    Yes I know it take longer with every read, because of the extra instructions.

    What does the 'DD' in i2c_master_getDD stand for? For reference, the DC in i2c_master_setDC stands for Data/Clock.

    I had to find a name and saw this, and it only reads SDA

    \#define i2c_master_getDC(void) ((GPIO_REG_READ(GPIO_IN_ADDRESS) >> pinSDA) & 1)
    

    so I made one for SCL

    \#define i2c_master_getDD(void) ((GPIO_REG_READ(GPIO_IN_ADDRESS) >> pinSCL) & 1) // PB
    
  • I was hoping I could use JsError as in the file jshardware.c, where the call come from, maybe someone has a solution.

    you can't use JsError in i2c_master.c

    use os_printf() or os_printf_plus(), both write to debug log

    than use ESP8266.setLog(2); in the js code to switch debug log to uart0 (console)

  • you can't use JsError in i2c_master.c

    Why? Surely it's just a matter of including jsutils.h?

  • Hey, I have tryed that, but I get a lot of compiler fault.
    So it is over my head at the moment.
    I only seems that could be nice, with a message if it took too long.

    
    In file included from /home/poul/Programmer/ESP8266_NONOS_SDK_­V1.5.3_16_04_18/ESP8266_NONOS_SDK/includ­e/ets_sys.h:11:0,
                     from targets/esp8266/i2c_master.c:35:
    /home/poul/Programmer/ESP8266_NONOS_SDK_­V1.5.3_16_04_18/ESP8266_NONOS_SDK/includ­e/c_types.h:15:29: error: conflicting types for 'uint32_t'
     typedef unsigned long       uint32_t;
                                 ^
    In file included from /home/poul/Programmer/uPython/esp-open-s­dk/xtensa-lx106-elf/lib/gcc/xtensa-lx106­-elf/4.8.2/include/stdint.h:9:0,
                     from /home/poul/Programmer/Espruino/tve/Espru­ino/src/jsutils.h:23,
                     from targets/esp8266/i2c_master.c:34:
    /home/poul/Programmer/uPython/esp-open-s­dk/xtensa-lx106-elf/xtensa-lx106-elf/inc­lude/stdint.h:81:22: note: previous declaration of 'uint32_t' was here
     typedef unsigned int uint32_t;
                          ^
    In file included from /home/poul/Programmer/ESP8266_NONOS_SDK_­V1.5.3_16_04_18/ESP8266_NONOS_SDK/includ­e/ets_sys.h:11:0,
                     from targets/esp8266/i2c_master.c:35:
    /home/poul/Programmer/ESP8266_NONOS_SDK_­V1.5.3_16_04_18/ESP8266_NONOS_SDK/includ­e/c_types.h:17:29: error: conflicting types for 'int32_t'
     typedef signed long         int32_t;
                                 ^
    In file included from /home/poul/Programmer/uPython/esp-open-s­dk/xtensa-lx106-elf/lib/gcc/xtensa-lx106­-elf/4.8.2/include/stdint.h:9:0,
                     from /home/poul/Programmer/Espruino/tve/Espru­ino/src/jsutils.h:23,
                     from targets/esp8266/i2c_master.c:34:
    /home/poul/Programmer/uPython/esp-open-s­dk/xtensa-lx106-elf/xtensa-lx106-elf/inc­lude/stdint.h:80:20: note: previous declaration of 'int32_t' was here
     typedef signed int int32_t;
                        ^
    In file included from /home/poul/Programmer/Espruino/tve/Espru­ino/src/jsutils.h:24:0,
                     from targets/esp8266/i2c_master.c:34:
    /home/poul/Programmer/ESP8266_NONOS_SDK_­V1.5.3_16_04_18/ESP8266_NONOS_SDK/includ­e/c_types.h:85:25: error: two or more data types in declaration specifiers
     typedef unsigned char   bool;
                             ^
    In file included from /home/poul/Programmer/ESP8266_NONOS_SDK_­V1.5.3_16_04_18/ESP8266_NONOS_SDK/includ­e/ets_sys.h:11:0,
                     from targets/esp8266/i2c_master.c:35:
    /home/poul/Programmer/ESP8266_NONOS_SDK_­V1.5.3_16_04_18/ESP8266_NONOS_SDK/includ­e/c_types.h:85:1: warning: useless type name in empty declaration [enabled by default]
     typedef unsigned char   bool;
     ^
    /home/poul/Programmer/ESP8266_NONOS_SDK_­V1.5.3_16_04_18/ESP8266_NONOS_SDK/includ­e/c_types.h:87:0: warning: "true" redefined [enabled by default]
     [#define](https://forum.espruino.com/sea­rch/?q=%23define) true            (1)
     ^
    In file included from /home/poul/Programmer/Espruino/tve/Espru­ino/src/jsutils.h:24:0,
                     from targets/esp8266/i2c_master.c:34:
    /home/poul/Programmer/uPython/esp-open-s­dk/xtensa-lx106-elf/lib/gcc/xtensa-lx106­-elf/4.8.2/include/stdbool.h:34:0: note: this is the location of the previous definition
     [#define](https://forum.espruino.com/sea­rch/?q=%23define) true 1
     ^
    In file included from /home/poul/Programmer/ESP8266_NONOS_SDK_­V1.5.3_16_04_18/ESP8266_NONOS_SDK/includ­e/ets_sys.h:11:0,
                     from targets/esp8266/i2c_master.c:35:
    /home/poul/Programmer/ESP8266_NONOS_SDK_­V1.5.3_16_04_18/ESP8266_NONOS_SDK/includ­e/c_types.h:88:0: warning: "false" redefined [enabled by default]
     [#define](https://forum.espruino.com/sea­rch/?q=%23define) false           (0)
     ^
    In file included from /home/poul/Programmer/Espruino/tve/Espru­ino/src/jsutils.h:24:0,
                     from targets/esp8266/i2c_master.c:34:
    /home/poul/Programmer/uPython/esp-open-s­dk/xtensa-lx106-elf/lib/gcc/xtensa-lx106­-elf/4.8.2/include/stdbool.h:35:0: note: this is the location of the previous definition
     [#define](https://forum.espruino.com/sea­rch/?q=%23define) false 0
     ^
    In file included from /home/poul/Programmer/ESP8266_NONOS_SDK_­V1.5.3_16_04_18/ESP8266_NONOS_SDK/includ­e/ets_sys.h:11:0,
                     from targets/esp8266/i2c_master.c:35:
    /home/poul/Programmer/ESP8266_NONOS_SDK_­V1.5.3_16_04_18/ESP8266_NONOS_SDK/includ­e/c_types.h:89:0: warning: "TRUE" redefined [enabled by default]
     [#define](https://forum.espruino.com/sea­rch/?q=%23define) TRUE            true
     ^
    In file included from targets/esp8266/i2c_master.c:34:0:
    /home/poul/Programmer/Espruino/tve/Espru­ino/src/jsutils.h:116:0: note: this is the location of the previous definition
     [#define](https://forum.espruino.com/sea­rch/?q=%23define) TRUE (1)
     ^
    In file included from /home/poul/Programmer/ESP8266_NONOS_SDK_­V1.5.3_16_04_18/ESP8266_NONOS_SDK/includ­e/ets_sys.h:11:0,
                     from targets/esp8266/i2c_master.c:35:
    /home/poul/Programmer/ESP8266_NONOS_SDK_­V1.5.3_16_04_18/ESP8266_NONOS_SDK/includ­e/c_types.h:90:0: warning: "FALSE" redefined [enabled by default]
     [#define](https://forum.espruino.com/sea­rch/?q=%23define) FALSE           false
     ^
    In file included from targets/esp8266/i2c_master.c:34:0:
    /home/poul/Programmer/Espruino/tve/Espru­ino/src/jsutils.h:115:0: note: this is the location of the previous definition
     [#define](https://forum.espruino.com/sea­rch/?q=%23define) FALSE (0)
     ^
    cc1: warning: unrecognized command line option "-Wno-float-conversion" [enabled by default]
    cc1: warning: unrecognized command line option "-Wno-discarded-qualifiers" [enabled by default]
    make: *** [targets/esp8266/i2c_master.o] Fejl 1
    make: *** Venter på uafsluttede job....
    make: *** wait: Ingen børneprocesser. Stop.
    
  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

I2C clock streching on esp8266.

Posted by Avatar for Frida @Frida

Actions