Where is the definition of uint16 system_adc_read(void) in the esp8266 Arduino code?

I can see it being declared in Arduino/tools/sdk/include/user_interface.h, however there seems to be no definition of this. system_adc_read() gets called in analogRead() in Arduino/cores/esp8266/core_esp8266_wiring_analog.c

I suspect system_adc_read() calls system_adc_read_fast() to read the ADC, but can't actually find it in the code! Could somebody please point me to this? I've been struggling for quite some time.

Purpose: To manipulate the system_adc_read() in ESP8266 to get higher ADC sampling rates. Right now ADC sampling rate is 10ksps. I want it to be more than 30-40ksps.

  • 1
    esp8266 sdk is not open source
    – Juraj
    Dec 28 '17 at 17:06
  • Oh my god! Thanks for telling me this. So, what are my alternatives here? I've searched for ways to improve the sampling rate, but couldn't get to the desired (30-40 ksps) level. Dec 28 '17 at 17:14
  • try ESP8266 Non-OS SDK API Reference here espressif.com/en/support/download/…
    – Juraj
    Dec 28 '17 at 17:24

The "definition" of uint16 system_adc_read(void) is inside the compiled archive/object file libmain.a inside one of the NONOSDK folders which is located in /tools/sdk/lib/ . The version is selected in platform.txt. For example in my platform.txt:

# default SDK for all boards
# (generic board overrides this variable)

Running the xtensa-lx106-elf-objdump.exe -d on this file I can find this section inside:

00000258 <system_adc_read>:
     258:   020c                    movi.n  a2, 0
     25a:   f0c112                  addi    a1, a1, -16
     25d:   0109                    s32i.n  a0, a1, 0
     25f:   fffc01                  l32r    a0, 250 <system_pp_recycle_rx_pkt+0x18>
     262:   0000c0                  callx0  a0
     265:   fff901                  l32r    a0, 24c <system_pp_recycle_rx_pkt+0x14>
     268:   f42020                  extui   a2, a2, 0, 16
     26b:   1b1207                  beq a2, a0, 28a <system_adc_read+0x32>
     26e:   b30c                    movi.n  a3, 11
     270:   902220                  addx2   a2, a2, a2
     273:   1122e0                  slli    a2, a2, 2
     276:   fff701                  l32r    a0, 254 <system_pp_recycle_rx_pkt+0x1c>
     279:   0000c0                  callx0  a0
     27c:   f42020                  extui   a2, a2, 0, 16
     27f:   00a402                  movi    a0, 0x400
     282:   013027                  bltu    a0, a2, 287 <system_adc_read+0x2f>
     285:   020d                    mov.n   a0, a2
     287:   f42000                  extui   a2, a0, 0, 16
     28a:   0108                    l32i.n  a0, a1, 0
     28c:   10c112                  addi    a1, a1, 16
     28f:   f00d                    ret.n
     291:   00                          .byte 00
     292:   00                          .byte 00
     293:   00                          .byte 00
     294:   ff ff 00 00                 

000002a0 <system_adc_read_fast>:
     2a0:   e0c112                  addi    a1, a1, -32
     2a3:   11c9                    s32i.n  a12, a1, 4
     2a5:   21d9                    s32i.n  a13, a1, 8
     2a7:   31e9                    s32i.n  a14, a1, 12
     2a9:   5129                    s32i.n  a2, a1, 20
     2ab:   41f9                    s32i.n  a15, a1, 16
     2ad:   0109                    s32i.n  a0, a1, 0
     2af:   03fd                    mov.n   a15, a3
     2b1:   fff901                  l32r    a0, 298 <system_adc_read+0x40>
     2b4:   0000c0                  callx0  a0
     2b7:   4fbc                    beqz.n  a15, 2ef <system_adc_read_fast+0x4f>
     2b9:   fff6e1                  l32r    a14, 294 <system_adc_read+0x3c>
     2bc:   0c0c                    movi.n  a12, 0
     2be:   51d8                    l32i.n  a13, a1, 20
     2c0:   90dcd0                  addx2   a13, a12, a13
     2c3:   001d32                  l16ui   a3, a13, 0
     2c6:   1a13e7                  beq a3, a14, 2e4 <system_adc_read_fast+0x44>
     2c9:   902330                  addx2   a2, a3, a3
     2cc:   1122e0                  slli    a2, a2, 2
     2cf:   b30c                    movi.n  a3, 11
     2d1:   fff201                  l32r    a0, 29c <system_adc_read+0x44>
     2d4:   0000c0                  callx0  a0
     2d7:   f44020                  extui   a4, a2, 0, 16
     2da:   00a402                  movi    a0, 0x400
     2dd:   003d                    mov.n   a3, a0
     2df:   013047                  bltu    a0, a4, 2e4 <system_adc_read_fast+0x44>
     2e2:   043d                    mov.n   a3, a4
     2e4:   005d32                  s16i    a3, a13, 0
     2e7:   cc1b                    addi.n  a12, a12, 1
     2e9:   f4c0c0                  extui   a12, a12, 0, 16
     2ec:   ce9fc7                  bne a15, a12, 2be <system_adc_read_fast+0x1e>
     2ef:   11c8                    l32i.n  a12, a1, 4
     2f1:   21d8                    l32i.n  a13, a1, 8
     2f3:   31e8                    l32i.n  a14, a1, 12
     2f5:   41f8                    l32i.n  a15, a1, 16
     2f7:   0108                    l32i.n  a0, a1, 0
     2f9:   20c112                  addi    a1, a1, 32
     2fc:   f00d                    ret.n
     2fe:   00                          .byte 00
     2ff:   00                          .byte 00
     300:   ff ff 00 00                 
  • Cool detective work. Thanks! I was able to achieve my use case using this: arduino.stackexchange.com/a/52854/25931 Jan 12 '20 at 6:17
  • @loadingnow can anyone clarify whether system_adc_read returns 0-1023 or whether it returns 1-1024?
    – S. Imp
    Jul 1 '20 at 16:37
  • @S.Imp I am not sure, but you should ask a separate question to clarify. It would be much more helpful for others who have the same question.
    – loadingnow
    Jul 20 '20 at 1:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.