Really fast timer ISR for GPIO at 1 MHz or faster

tasha6
Posts: 4
Joined: Wed Mar 13, 2024 12:15 am

Really fast timer ISR for GPIO at 1 MHz or faster

Postby tasha6 » Wed Mar 13, 2024 12:31 am

Hi, I want to record a whole bunch of UARTs, I'm thinking at least 8. I only care about RX. I will probably use the hardware UART to transmit all the data from the 8 or more input UARTs. I need to be able to bitbang them because as you know, there aren't enough UARTs. The baud is 115,200. My goal is to set up a timer with an ISR, which reads the pins and does a minimum of processing to see if there is a new byte ready, put that in a buffer and return. I would like for this to happen at a rate of 1 MHz but I'd prefer faster, like 4 MHz. My question isn't about the logic to implement this. I've bitbanged UARTs before and other stuff. My question is, is this even possible? At 1 MHz the ISR would have to happen once every 1 us, at 4 MHz it would have to be every 250 ns. I've never had issues with stuff like this before but this is the first time I'm using FreeRTOS and in the past I've always done everything bare metal. Is it hopeless to even try this in FreeRTOS? I couldn't find any documentation on what the limits are. I'm using ESP-WROOM-32, I guess it can go up to 240 MHz, so with that in mind, at least 1 MHz should be not a problem. I can keep the code light, I don't need WiFi or anything like that, just multiplex a bunch of UARTs into one output. Any advice would be greatly appreciated.

ESP_Sprite
Posts: 9766
Joined: Thu Nov 26, 2015 4:08 am

Re: Really fast timer ISR for GPIO at 1 MHz or faster

Postby ESP_Sprite » Wed Mar 13, 2024 2:05 am

Accessing the GPIO is pretty slow on the 'classic' ESP32, sorry. Newer ESP32 chips (ESP32-Cx, ESP32-Sx) have 'fast GPIO' that may help. Alternatively, you can use any peripheral that can do parallel data input and has DMA to simply read the data to memory and then process it later; look for the I2S parallel or camera peripherals for that.

MicroController
Posts: 1735
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: Really fast timer ISR for GPIO at 1 MHz or faster

Postby MicroController » Wed Mar 13, 2024 2:32 pm

I guess it can go up to 240 MHz, so with that in mind, at least 1 MHz should be not a problem
When entering/exiting an ISR, there's more stuff going on than one might think; switching context from task to ISR and back is not instantaneous. I have no numbers at hand, but I figure 100-200 CPU cycles of 'overhead' for an ISR call/return might be on the optimistic side.

tasha6
Posts: 4
Joined: Wed Mar 13, 2024 12:15 am

Re: Really fast timer ISR for GPIO at 1 MHz or faster

Postby tasha6 » Thu Mar 14, 2024 4:14 pm

Thanks for the replies.
For now I'm giving up on this project with ESP32, it's just way too slow, despite running at 240 MHz. The best I could achieve was about 100 kHz and the jitter was really bad, and that's even without ESP-IDF and just running basic arduino code. I'm sure I can still improve upon this but seeing how I'm already struggling getting to 100 kHz I don't think I'll even have a chance to get to 1 MHz.
Below is the code for reference. I like the ESP32 but for this project I will be moving onto something else. I have an old Teensy 3.2 laying around so I will give that one a shot. If I ever get this to work I may report back with what I ended up using.

Code: Select all

#include "ESP32TimerInterrupt.h"

#define LED_GRN1  12

bool IRAM_ATTR TimerHandler0(void * timerNo)
{
  static bool toggle0 = false;
  digitalWrite(LED_GRN1, toggle0);
  toggle0 = !toggle0;
  return true;
}

ESP32Timer ITimer0(0);

void setup() {
  pinMode(LED_GRN1, OUTPUT);
  ITimer0.attachInterruptInterval(6, TimerHandler0);
}

void loop() {
  while(1);
}

tasha6
Posts: 4
Joined: Wed Mar 13, 2024 12:15 am

Re: Really fast timer ISR for GPIO at 1 MHz or faster

Postby tasha6 » Thu Mar 14, 2024 6:32 pm

I forgot to say that when I was close to 100 kHz, that was measured as the frequency of the pin toggle so the ISR was executed closer to 200 kHz. Also some progress with the Teensy 3.2, I maxed it out with the ISR getting executed at 1.26 MHz toggling the pin. I already ordered the newest Teensy 4.2 so looking forward to how much faster that is.

Who is online

Users browsing this forum: No registered users and 120 guests