Page 1 of 1

[ULP] How to read with adc ULP assembler instruction

Posted: Mon May 15, 2017 7:30 pm
by hamele
Hi!

I’m working on a ULP program that reads from ADC and wakes up the main cores if the result is greater than a threshold.
The analog signal modulated with a potentiometer is connected to the pin (ADC2:3, GPIO15), but I’m not sure how I should use the ADC assembler instruction. Besides, the documentation is quite confusing.
I ‘m using the following code:

Code: Select all

	.bss

	.global adc_value
adc_value:
	.long 0

	.global adc_threshold
adc_threshold:
	.long 0

	.text

  // ULP code starts here
	.global entry
entry:
  move r3, adc_threshold      // R3 <- @adc_threshold
  ld r3, r3, 0                // R3 <- adc_threshold
  move r2, adc_value          // R2 <- @adc_value
  /* Measure value using ADC1 pad 2 for 100 cycles and move result to R0 */
  adc r1, 1, 2, 1           // R0 <- ADC1
  st r1, r2, 0                // @adc_value <- R0 (ADC READ VALUE)
  sub r1, r3, r1              // R0 <- R3 - R0 (Threshold - ADC)
  jump wake_up, ov            // Jump if last ALU has set overflow flag (ADC > Threshold)
  halt

	.global wake_up
wake_up:
	/* Wake up the SoC, end program */
	wake
	halt
The output is something like this:

Code: Select all

ULP wakeup
ADC read value: 4095, overpass the threshold: 100.
Entering deep sleep

ULP wakeup
ADC read value: 4095, overpass the threshold: 100.
Entering deep sleep

ULP wakeup
ADC read value: 4095, overpass the threshold: 100.
Entering deep sleep
It doesn’t make sense at all. I tried different operand values on the ADC assembler instruction, but all of them returned different constant numbers, or a low range of numbers without any type of connection with my input.

Re: [ULP] How to read with adc ULP assembler instruction

Posted: Tue May 16, 2017 4:46 am
by ESP_igrr
For ADC1, the instruction should look like

Code: Select all

adc r1, 0, channel + 1, 0
but there is also some amount of ADC configuration which needs to happen before the ADC is usable by ULP. We have an example ready for this; hopefully it will be in master in a day or so.

Re: [ULP] How to read with adc ULP assembler instruction

Posted: Tue May 16, 2017 1:14 pm
by ESP_igrr

Re: [ULP] How to read with adc ULP assembler instruction

Posted: Tue May 16, 2017 1:25 pm
by WiFive
How does cycle count parameter relate to function of SAR?

Re: [ULP] How to read with adc ULP assembler instruction

Posted: Tue May 16, 2017 2:37 pm
by ESP_igrr
I think originally it was supposed to have a function similar to "wait" instruction (i.e. do ADC measurement and wait given number of cycles). But that feature doesn't work properly, so this field should always be zero. We forgot to remove it from the instruction set manual and the assembler; thanks for pointing this out, will fix...

Re: [ULP] How to read with adc ULP assembler instruction

Posted: Tue May 16, 2017 3:10 pm
by hamele
Thanks, the example is very useful.

The example and my program shows this weird message on wake up:

Code: Select all

rst:0x5 (DEEPSLEEP_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0x00
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0008,len:8
load:0x3fff0010,len:3420
ho 0 tail 12 room 4
load:0x40078000,len:10388
load:0x40080000,len:252
entry 0x40080034
I have read the following post. Any known final solution?
viewtopic.php?f=2&t=1662&hilit=DEEPSLEEP_RESET

Re: [ULP] How to read with adc ULP assembler instruction

Posted: Tue May 16, 2017 3:26 pm
by ESP_igrr
This looks like perfectly normal bootloader output following deep sleep reset.
If you want to disable it, pull GPIO15 down to ground before power-on reset.