Library for HX711 amplifier and 24 bit ADC

Hello there,
I’m trying to write a library for the HX711, for a weight scale usage. It has a non-standard serial protocol and it is relevant to control at microseconds precision the time that a digital output (used as HX711 serial clock) is high or low.

My starting point is the well-known bogde library for the Arduino platform (https://github.com/bogde/HX711) and I’m trying to write an equivalent in python in Zerynth, but I’ve encountered the following blocking problems:

  • I need a way to delay with microsecond precision but the sleep(…, MICROS) built-in function doesn’t work with my ESP32 and neither hwtimers’ sleep_micros(…) works with it.
  • I need a way to define a sequence of operations as an atomic block: no context switching is possible since I have to avoid a HIGH serial clock time longer than 60microseconds, otherwise the HX711 will go to sleep mode, and I can do this only if I can assert that no other thread will take control (possibly I need also to disable and re-enable interrupts).

The bodge library do it using some board-specific defined macros in C that allows him to recognize the hardware and call the appropriate functions. How can I do that in Zerynth? I don’t think that the usual threading module functionalities will help, since it is like I have to acquire a “global” lock, that stops every other thread, including the interrupts thread.

Hi @linoalfieri

Since the MICROS are not implemented for all boards, a quick fix would be implementing something like this:

static void poll_delay(uint32_t ticks) {
    // polled delay
    volatile uint32_t *now = vosTicks();
    uint32_t start = *now;
    uint32_t end = start+ticks;
    if(end>start) {
    	while(*now>=start && *now<end);
    } else {
    	while(*now>=start || *now<end);
    }
}

you could use this polling sleep by means of the vosTicks() function which returns a pointer to the number of clock ticks.
In a c file, Make a function that call this Macro or implement a similar function then call this function from your python code. (as in C language interface example)
But you should make an evaluation of the right ticks value, check it with an oscilloscope for better timing.

Please check the vosSysLock(), this makes a code executed atomically by disabling interrupts and Thread scheduling.

To have the most efficient handling of timing, Implement the critical timing part in C.
Let me know the updates :smiley: