Hi, I'm the guy who posted the question to which you were pointed above.
I've got HX711 code working along with code to test for threshold crossings.
1. If you want the HX711 to be read by both the ULP and the system, set up your GPIOs as RTC GPIOs, which can be controlled/read in both environments. A routine to read the HX711 in C is much simpler than assembly. I'll leave that to you, but the setting up of the GPIOs as RTC GPIOs while still on the main processor(s) is a neat trick I learned on this board. It makes life easy. Set the specific RTC GPIOs to use with #define statements. Here's some code:
/* define clock and data pins, channel, and gain factor. HX711 Channel selection is made...
** by setting the appropriate gain: 128 or 64 for channel A, 32 for channel B.
** To set HX711 gain to default 128, do one throw-away read cycle at the end of each...
** read to set the gain for the next read, and never power down the HX711. We power...
** down the HX711 when we can so the first read is at gain 128. Power down the...
** the HX711 by setting SCLK high for > 60µS.
** Drive HX711 through ESP32 RTC_GPIO so it will work in ULP and SoC.
** See HX711 data sheet for details
*/
void HX711_init(void)
{
// Initialze RTC_GPIO pin for HX711 SCLK
gpio_reset_pin(GPIO_SCLK);
rtc_gpio_init(GPIO_SCLK);
rtc_gpio_set_direction(GPIO_SCLK, RTC_GPIO_MODE_OUTPUT_ONLY);
rtc_gpio_set_level(GPIO_SCLK, HIGH);
// Initialze RTC_GPIO pin for HX711 DOUT
gpio_reset_pin(GPIO_DOUT);
rtc_gpio_init(GPIO_DOUT);
rtc_gpio_set_direction(GPIO_DOUT, RTC_GPIO_MODE_INPUT_ONLY);
gpio_set_level(GPIO_SCLK, HIGH); // Leave HX711 in a power-down state
}
Here's a ULP assembly snippet that reads both the low 16 and high 8 bits from the HX711. I defined 2 macros, SCLK_high and SCLK_low that manipulate the system clock lines of the HX711. DOUT is the data out line from the HX711.
Here are the macros:
// Some macros
.macro DOUT_read
READ_RTC_REG(RTC_GPIO_IN_REG, RTC_GPIO_IN_NEXT_S + DOUT, 1)
.endm
/* These two marco for set bus high and set low when GPIO_L is called, enable W1TS. */
.macro SCLK_high
WRITE_RTC_REG(RTC_GPIO_OUT_REG, RTC_GPIO_OUT_DATA_S + SCLK, 1, 1)
.endm
.macro SCLK_low
WRITE_RTC_REG(RTC_GPIO_OUT_REG, RTC_GPIO_OUT_DATA_S + SCLK, 1, 0)
.endm
And here's the code segment to read the HX711, both the low 16 bits into a location accessible by both the ULP and the system, and the high 8 bits, similarly accessible. There are just enough registers in the ULP to make register allocation not too painful.
ReadHX711:
move r1, 0 // Initialzation HX711 read storage to 0
move r3, HX711HiWord
st r1, r3, 0 // Offset points to HX711HiWord
st r1, r3, 4 // Offset points to HX711LoWord
move r2, 2 // count of passes through bit clocking code
stage_rst
stage_inc 8 // Setup to read hi 8 bits HX711 output
ReadCycleBegin:
SCLK_low
CheckReady:
DOUT_read
jumpr CheckReady, 1, ge // Ready when DOUT goes low
RdBit:
SCLK_high
SCLK_low
DOUT_read
lsh r1, r1, 1
jumpr GotAZeroBit, 1, lt
add r1, r1, 1 // If last read was a 1, OR it with accumulator
GotAZeroBit:
stage_dec 1
jumps RdBit, 0, gt // if bit count down not 0, go read another bit
st r1, r3, 0 // store accumulated read, 8 or 16 bits
sub r2, r2, 1 // Have we read two words?
jump ReadDone, eq
stage_inc 16 // else setup to read 16 bits of low HX711 output
move r3, HX711LoWord // point r3 for the low word read
move r1, 0 // init r1 for low word read (Is this needed? Be safe)
jump RdBit
ReadDone: // must cycle SCLK one more time to set gain to
SCLK_high // 128 on next HX711 read, then leave SCLK hi for
SCLK_low // for > 60 uS to power down HX711, ~10 clocks
I'll leave the rest to you. With some "jiggery-pokery" you can do the threshold arithmetic in the ULP too, but without an XOR instruction, its not as easy as it should be.
Can you tell me what kind of a project you're working on? Tell me by PM if you can.
wevets