I am trying to port a program that I have working on an Arduino nano over to an ESP32.
One of the things I do in my program is sample 4 ADC channels in an interrupt running at 1kHz.  Ideally, I would like to run my interrupt at 2kHz, but the nano can't do it, so I wanted to see if the ESP32 could do it faster.
I wrote a simple test program on the ESP32 and got the interrupt running at 500 microseconds.  All I do in the ISR is toggle an output so that I can measure the timing on a scope.  
I can read one ADC channel outside the interrupt and it takes 140 microseconds to read the ADC.  As soon as I put the read ADC command inside the ISR macro, the interrupt stops running and my program keeps restarting.
I attached my program.  Please let me know if you have any ideas.
			
							ESP32 Can't sample ADC inside Interrupt Macro
- 
				pmgreene01
 - Posts: 35
 - http://meble-kuchenne.info.pl
 - Joined: Sat Jul 08, 2023 7:39 pm
 - Has thanked: 3 times
 - Been thanked: 4 times
 
ESP32 Can't sample ADC inside Interrupt Macro
- Attachments
 - 
			
		
		
				
- Interrupt_test_ESP32-01.fcfx
 - (26.2 KiB) Downloaded 508 times
 
 
- 
				pmgreene01
 - Posts: 35
 - Joined: Sat Jul 08, 2023 7:39 pm
 - Has thanked: 3 times
 - Been thanked: 4 times
 
Re: ESP32 Can't sample ADC inside Interrupt Macro
I have continued investigation and found these additional symptoms.
- I can only create an interrupt based on Timer0, timers 1,2 and 3 fail.
- I can do integer math in the interrupt macro, if I try anything with a floating point value, the interrupt won't run.
 
If anyone has any ideas, I would appreciate it. Maybe ESP32 is not the right choice for me. Just need something that runs 30% or more faster than the Arduino nano that I am currently using.
			
			
									
						- I can only create an interrupt based on Timer0, timers 1,2 and 3 fail.
- I can do integer math in the interrupt macro, if I try anything with a floating point value, the interrupt won't run.
If anyone has any ideas, I would appreciate it. Maybe ESP32 is not the right choice for me. Just need something that runs 30% or more faster than the Arduino nano that I am currently using.
- 
				mnfisher
 - Valued Contributor
 - Posts: 1694
 - Joined: Wed Dec 09, 2020 9:37 pm
 - Has thanked: 146 times
 - Been thanked: 789 times
 
Re: ESP32 Can't sample ADC inside Interrupt Macro
The esp32 has an adc_continuous mode which might do what you need.
It would take a little coding to use the api - but have a look at
https://www.google.com/url?sa=t&source= ... F6MU6fE4-7
Martin
			
			
									
						It would take a little coding to use the api - but have a look at
https://www.google.com/url?sa=t&source= ... F6MU6fE4-7
Martin
- 
				stefan.erni
 - Valued Contributor
 - Posts: 1106
 - Joined: Wed Dec 02, 2020 10:53 am
 - Has thanked: 209 times
 - Been thanked: 229 times
 
Re: ESP32 Can't sample ADC inside Interrupt Macro
Hi pmgreene01, Hi Martin
I know the IRQ of the ESP32 is not practical because almost all commands in the macro crash.
One possibility, i use at the moment is the following:
I set only one variable(t1_int) to 1 with the interrupt-macro and in the main program I check if it is 1.
If yes i sample the channel and set the variable back to 0.
So I have a more or less a constant sample rate.
For another project I used a PIC32MZ from Microchip. There the interrupt did not cause any problems with flowcode.
regards
Stefan
Info about main
			
			
									
						I know the IRQ of the ESP32 is not practical because almost all commands in the macro crash.
One possibility, i use at the moment is the following:
I set only one variable(t1_int) to 1 with the interrupt-macro and in the main program I check if it is 1.
If yes i sample the channel and set the variable back to 0.
So I have a more or less a constant sample rate.
For another project I used a PIC32MZ from Microchip. There the interrupt did not cause any problems with flowcode.
regards
Stefan
Info about main
- 
				pmgreene01
 - Posts: 35
 - Joined: Sat Jul 08, 2023 7:39 pm
 - Has thanked: 3 times
 - Been thanked: 4 times
 
Re: ESP32 Can't sample ADC inside Interrupt Macro
Thank you for the response.  I will play with that approach.