esp32-CAM need for 10khz interrupt

brebisson
Posts: 13
Joined: Fri Dec 22, 2023 9:44 am

esp32-CAM need for 10khz interrupt

Postby brebisson » Fri Jan 26, 2024 1:13 pm

Hello,

I am working on an esp32-cam board using the arduino IDE...

Can someone give me some sample code to setup a function that would be called 10000 times per second (on a 100micro second basis)?

The function will be very simple and short so it can run under interupt..

A task based sleep however would not work as it would be too slow and "heavy".

Thanks for your help,
Cyrille

lbernstone
Posts: 828
Joined: Mon Jul 22, 2019 3:20 pm

Re: esp32-CAM need for 10khz interrupt

Postby lbernstone » Fri Jan 26, 2024 1:46 pm

https://github.com/espressif/arduino-es ... tTimer.ino
Note that what you think is simple and short may not be what the esp32 thinks. You should not interact with hardware in an interrupt.

brebisson
Posts: 13
Joined: Fri Dec 22, 2023 9:44 am

Re: esp32-CAM need for 10khz interrupt

Postby brebisson » Fri Jan 26, 2024 2:17 pm

Hello,

Thanks for the pointer to the code.

When you say that I should not interract with HW in interrupt, do you mean that I can not set/clear GPIO pins in interrupt (which is what I need to do) here?

Thanks,
Cyrille

lbernstone
Posts: 828
Joined: Mon Jul 22, 2019 3:20 pm

Re: esp32-CAM need for 10khz interrupt

Postby lbernstone » Fri Jan 26, 2024 7:04 pm

That's what I mean. You can use Ticker if you need a timer that runs in the FreeRTOS space. 100% safe to touch gpios with that. Note that turning on/off a gpio can take up to 50-60 ms, possibly even more if it is attached to a mutex protected driver. There are lots of peripherals to help you if you need patterns/signals at your 10KHz frequency.

brebisson
Posts: 13
Joined: Fri Dec 22, 2023 9:44 am

Re: esp32-CAM need for 10khz interrupt

Postby brebisson » Mon Jan 29, 2024 6:52 am

Hello,

> Use Ticker
But Ticker seems to run as a FreeRTOS task, this means that the timing is limited by the RTOS main timer (is it the standard 1Khz?) and that timing is only aproximative...

> Note that turning on/off a gpio can take up to 50-60 ms, possibly even more if it is attached to a mutex protected driver.
This is scary! 50 mili-seconds?
Is there no way to access the IO ports directly like on arduinos in order to bypass OS protections?
Can I not bit-Bang the IO port?
I am interested in GPIO 12, 13, 14 and 15

> There are lots of peripherals to help you if you need patterns/signals at your 10KHz frequency.
Ok, here is my use case, tell me which periferial is the most adaped...

I have 3 Tasks which can, asynchronously add or remove pulses requests from a "counter". There are in the form of:

Code: Select all

int target= 0;
void arrOrRemoveFromTarget(int value)
{
  xSemaphoreTake(mutex, portMAX_DELAY);
  target= target+value;
  xSemaphoreGive(mutex, portMAX_DELAY);
}
And I have my 10Khz ISR of the form:

Code: Select all

int value= 0; bool pinState= false;
void isr10k()
{
  if (value<target)
  {
    setPin(12, pinState= !pinState);
    value++;
  }
}
So, at 10khz, value will try to "reah" target which will be a moving target (sometimes going up, sometimes going back, but it's ok if value temporary passes target).

Although I have done a lot of embeded work, including porting FreeRTOS to a (then) new ARM chip, I am completely new to the ESP32, it's ecosystem and innerworkings, so I still have a lot to learn.

Cyrille

lbernstone
Posts: 828
Joined: Mon Jul 22, 2019 3:20 pm

Re: esp32-CAM need for 10khz interrupt

Postby lbernstone » Mon Jan 29, 2024 9:24 am

I say it "may" take 50-60ms depending on what else is going on, not that it necessarily will. However, bit-banging blocks the cpu, and at 10KHz, you are likely to starve the system processes and the OS will crash.
The RMT peripheral is most likely what you want. You can give it an arbitrary pattern (which you define), and it will repeat that pattern until you stop it.
What you are describing sounds a lot like a PWM cycle, and if it really is that, changing a LEDC duty cycle (which will add or remove pulses spread evenly throughout your frequency) is certainly a lot easier and more responsive than RMT.
Both these methods offload the function from the CPU, so you only to need to set them when your cycle changes. This means you can use the cpu to do your real work, rather than micro-managing the signal processing.

brebisson
Posts: 13
Joined: Fri Dec 22, 2023 9:44 am

Re: esp32-CAM need for 10khz interrupt

Postby brebisson » Mon Jan 29, 2024 10:40 am

Hello,

I am kind of in an odball case.

Both PWM and LDEC are close, but not really what I need.

The main issue is that I do not know in advance how many pulses will ultimately need to be generated as request can be to add or remove pulses...

So any library or system that I use will need to have some sort of "cancel" or "modify" API for me to use it... which does not seem to be the case...

Since my system will be far from being overloaded, and since I thought that chaniging an IO pin was "just a write in a register", a 10khz ISR seemed like a good solution...

BTW, once my pins are set properly as GPIO output, can I not just write in the GPIO_OUT_REG register to change the value? do I have to go through the digitalWrite() API?


Thanks for your help,
Cyrille

lbernstone
Posts: 828
Joined: Mon Jul 22, 2019 3:20 pm

Re: esp32-CAM need for 10khz interrupt

Postby lbernstone » Mon Jan 29, 2024 1:21 pm

RMT will do what you want. I'm not sure why you think it doesn't have a modify or cancel. You can keep your pattern in a buffer, modify the buffer, and reload it into the peripheral.
You can poke the registers, as long as you are thread safe about it, and are not using them with any other iomux methods.

Who is online

Users browsing this forum: Google [Bot] and 152 guests