How do I set up and use the R4 ADC continuous scan mode?

I have a UNO R4 and would like to use the ADC continuous scan mode described in the R4M1 chip manual. To do that I also need to be able to set the sampling rate, for which I need to address the device registers, and I need to install an interrupt service routine for the scan-complete interrupt.

I've done this with the UNO M3. It's not hard since there are macros that access the device registers and have names that are the same as the register names published in the chip manual, and the ISR is installed simply by declaring the function ISR(ADC_vect).

I have found nothing in this Forum but the post Faster ADC on Arduino R4 Wifi which refers to a post written by Susan Parker with a couple of lines that appear to use pointers that refer to registers (e.g. ADC140_ADCSR). The post includes a reply by Parker which includs a sketch, but it is M3 code. I cannot find the Parker post with the M4R1 code.

How do I do this? Is there a header I can #include to define register macros for the M4R1 chip?

Where you see "M3" in the above, read "R3". Sorry about the typo.

Solved. I finally found a link to the @susan-parker post on Github:
(GitHub - TriodeGirl/Arduino-UNO-R4-code-DAC-ADC-Ints-Fast_Pins: Direct register setup of ADC and DAC, with fast non-blocking ADC with ADC_Complete Interrupt)
It is what I am looking for.

Thank you, Susan.

1 Like

I can't get the interrupt to work. I wrote a sketch based on the @susan-parker sketch (above) to read in single mode. I put these 2 lines in Setup():

  attachInterrupt(2, ADC_ISR, FALLING);  // This IRQ will be assigned to Slot 05...
  *ICU_IELSR05 = 0x029;                  // HiJack Slot 05 IELSR05 for ADC140_ADI

My ISR is void ADC_ISR(), and my ADC setup is the same as Susan's, although I don't really want to use averaging mode. I did not set up or use the DAC, and I dis not manipulate the digital pins. The ISR is never called, but I can read ADC140_ADDR00 and get correct values, like in Susan's code.

I put this variable in my sketch:
volatile bool bInterrupted = false;
and set it to true in the ISR. The variable never becomes true.

I decided to run Susan's ino. It compiled and uploaded OK, but the output sent to Serial was bizarre:

  0 - 1E 

Firmware name: "C:\Users\jshor\AppData\Local\arduino\sketches\04EBAC79990481F95B99CFEFB25CA7A4/R4_LED_Matrix.ino", compiled on: Dec 30 2024
Fault on interrupt or bare metal(no OS) environment
===== Thread stack information =====
  addr: 20007e10    data: 00008c8a
  addr: 20007e14    data: 200003e4

  ... more address dumps, 60 in all

  addr: 20007ef8    data: 00007895
  addr: 20007efc    data: 00002599
====================================
=================== Registers information ====================
  R0 : ffffffff  R1 : 000000c2  R2 : 40006000  R3 : 0006e900
  R12: 00000001  LR : ffffffe9  PC : 00004fe8  PSR: 21000212
==============================================================
Bus fault is caused by precise data access violation
The bus fault occurred address is ffffffff
Show more call stack info by run: addr2line -e "C:\Users\jshor\AppData\Local\arduino\sketches\04EBAC79990481F95B99CFEFB25CA7A4/R4_LED_Matrix.ino".elf -a -f 00004fe8 00008c8a 000097ce 00008c8a 0000ff4c 000097ce 00009806 00004276 0000a194 0000a1d6 00004302 0000789e 00007894

R4_LED_Matrix.ino is a sketch I wrote to learn the LED matrix; it's a complete mystery to me why it is referred to. The path given is not my sketchbook, but there actually was a copy of R4_LED_Matrix.ino in that folder with the 32-digit hexadecimal name.

That went away when I commented out the call to print_icu_event_links(). I now got correct readings, but still no interrupt. I added the bInterrupted variable and used it like before, and it never went to true.

There is an undocumented function named analogAttachIrq() in the analog.h core file. I called it like this:
analogAttachIrq((ADCIrqCbk_f)ADC_ISR, ADC_IRQ_SCAN_END, 12);
and got this when I clicked the Verify button:

C:\Users\jshor\AppData\Local\arduino\sketches\02054CCE147800E620F3EE18C110BA20\sketch\R4_Tests.ino.cpp.o: In function `setup':
D:\Software\Arduino\_SketchBook\R4_Tests/R4_Tests.ino:18: undefined reference to `analogAttachIrq(void (*)(unsigned char), ADCIrqType_t, unsigned char)'
collect2.exe: error: ld returned 1 exit status

exit status 1

Compilation error: exit status 1

I added an unsigned char parameter to my ISR and got the same result. There is another reference to an unrelated sketch, and I have no idea what an "undefined reference" is.

Does anyone know what any of this means?

I really would like to try the continuous mode of the ADC, but I can't do it without the interrupt. Does anyone know how to set the ISR and make it work?

I apologize for the length of this, but I wanted to explain fully so someone might be able to figure it out.