Showing posts with label Microcontrollers. Show all posts
Showing posts with label Microcontrollers. Show all posts

16 July 2025

Debugging ESP32 Timer Interrupts: A Tale of API Compatibility

 

The Silent Signal Generator

 

Have you ever spent hours debugging code that should work but doesn't? Recently, I faced this exact challenge with an ESP32-based signal generator project. The symptoms were perplexing: all configuration parameters were correctly set, the web interface was working flawlessly, but the CTCSS tone output was completely silent. The culprit? 

A timer interrupt that refused to fire...

The Setup

The project uses an ESP32 to generate precise CTCSS (Continuous Tone-Coded Squelch System) tones for radio applications with  a GPS to "discipline" the time for 0.5% or less accuracy. 

Yeah! Overkill, I know, but the architecture is straightforward:

  1. A web interface for controlling tone parameters (frequency, level, on/off)

  2. WebSockets for real-time communication

  3. A timer interrupt running at 10 kHz to drive the DAC output

  4. Direct Digital Synthesis (DDS) with a sine lookup table for waveform generation

Everything seemed perfect in theory, but in practice, the DAC remained stubbornly silent.

The Investigation: Hours of Head-Scratching

 

What followed were countless hours of frustrating debugging and head-scratching. I tried everything: different timer configurations, checking hardware connections, simplifying the code, even rewriting the interrupt handler from scratch. I attempted various approaches: minimizing critical sections to reduce conflicts with WebSockets, trying different timer frequencies, toggling GPIO pins to verify hardware functionality, and even considering direct register access to bypass the Arduino API entirely. Nothing worked. The timer stubbornly refused to fire its interrupt, and the oscilloscope showed a flat line where a beautiful sine wave should have been.

The extensive debug logged every aspect of the system and for no reason, the interrupt was silent. 

At one point, my frustration reached such heights that I was ready to abandon the Arduino framework entirely and start learning Assembler or Machine Code just to manipulate the ESP32's DAC at the lowest possible level. 

Anything to get that stubborn signal flowing! A pattern finally emerged: the timer was being initialized correctly (the code was compiling and running without errors), but the interrupt service routine (ISR) counter remained at zero. This meant the timer was configured but never actually firing its interrupt.

[DIRECT DEBUG] Timer status check:
ISR counter value: 0
CTCSS enabled: YES
CTCSS frequency: 100.00
Timer initialized: YES

The code was using the ESP32 Arduino Core 3.2.1, and the timer setup looked correct:

timer = timerBegin(timerFreq);
timerAttachInterrupt(timer, &onTimer);
timerAlarm(timer, alarmValue, autoReload, reloadCount);
timerStart(timer);

 

The Breakthrough: API Version Matters

After multiple failed attempts to fix the issue within the 3.2.1 framework, I discovered something crucial: ESP32 Arduino Core versions have significantly different timer APIs.

The newer 3.2.1 version had simplified the API, removing critical parameters like timer number selection and edge-triggered interrupt options. These simplifications made the API easier to use but less powerful for specialized applications like precise signal generation.

 

 


 

The Solution: Strategic Downgrade

The solution was to downgrade to ESP32 Arduino Core 2.0.9, which offers a more comprehensive timer API. This allowed explicit control over:

1. Timer selection - Using Timer 1 to avoid conflicts with system functions

2. Edge-triggered interrupts - For more responsive and reliable timing

3. Precise prescaler settings - For accurate frequency generation

The updated code looked like this:

// ESP32 Arduino Core 2.0.9 API with explicit timer number
int timerNumber = 1; // Using timer 1 to avoid conflicts
uint16_t prescaler = 80; // 80MHz / 80 = 1MHz timer frequency

timer = timerBegin(timerNumber, prescaler, true); // true = count up
timerAttachInterrupt(timer, &onTimer, true); // true = edge triggered

uint32_t alarmValue = 1000000 / DDS_SAMPLE_RATE; // 100 ticks for 10kHz
timerAlarmWrite(timer, alarmValue, true); // true = auto reload
timerAlarmEnable(timer);

 

 

The Results

The results were immediate and impressive:

[DIRECT DEBUG] Timer status check:
ISR counter value: 2232839
CTCSS enabled: YES
CTCSS frequency: 254.00
Timer initialized: YES

[INFO] ISR count: 2240000, Time since last report: 1003 ms
[INFO] Actual interrupt frequency: 9970.09 Hz (expected: 10000 Hz)
[INFO] DAC output active: yes, CTCSS enabled: yes

The timer was now firing at approximately 9970-9980 Hz (within 0.3% of the target 10 kHz), and the oscilloscope confirmed a clean CTCSS signal output from the DAC.

 


 

Key Takeaways

1. API Compatibility Matters: Always check API compatibility when upgrading frameworks or libraries. What works in one version may not work in another, even if the code compiles without errors.

2. Explicit is Better than Implicit: When working with hardware-level features like timers, explicit control often yields better results than simplified APIs.

3. Effective Debugging: Implement counters and detailed logging to make invisible problems visible. The ISR counter was crucial in diagnosing this issue.

4. Sometimes Downgrading is Upgrading: Don't be afraid to use an older version if it offers functionality that better suits your specific needs.

Conclusion: From Frustration to Triumph

After hours of pulling my hair out (not quite) and questioning my sanity (and programming abilities - truly!), the solution turned out to be something I never would have guessed initially. 

Those moments when you stare at perfectly valid code that simply won't work are some of the most frustrating in an amateur developer's life. Yet they're also the moments that teach us the most.

This experience reinforced an important lesson in embedded systems development: understanding the underlying hardware and API capabilities is just as important as writing correct code. Sometimes the most elegant solution isn't using the latest version, but rather the version that gives you the control you need.

The relief when seeing that ISR counter finally incrementing was indescribable. The ESP32 Signal Generator now produces perfect CTCSS tones with rock-solid timing, proving that with the right approach, even the most stubborn bugs can be conquered. The countless hours of head-scratching were ultimately worth it, not just for the working project, but for the deeper understanding gained along the way.

Have you encountered similar API compatibility issues in your projects? Share your experiences in the comments below!

18 December 2011

My "HELLO WORLD"

In these days seems that you cannot light a simple LED without a very complicated electronic hardware (microcontroller with some Quartz Crystal around it etc).
So, I started to become interested in these "chips" becuse I do not want to be so "obsolete"...
First, I started by searching a development board...
I soon received from a fellow ham a EasyPIC 2 Development Board fitted with everything you want.
After that, I stated to search for a suitable PIC to start with. A 16F877A seems to be the best for a beginner because has A/D converters and a lot of I/O ports.
The interest in PIC was determined by the need for some repeater automation, including battery management.
After a lot of struggling, I manage to read and write the PIC using the onboard USB programmer. This was by far the most time consuming phase (a few weekes of study). Don't laugh! I have no one to help me but some internet related posts!
To make the long short, after a successfull R/W operation, I manage to blink a LED but this don't count as a "Hellor World" project because was made with a downloaded .hex file!
I read (and trying to understand) various things about uC.
I soon found that I need a Programming tool and a compiler.
Everyone pointed me to MikroE solutions: MikroBasic and MikroC. Also I read Mr's Surducan book about microcontrollers and JAL (Just Another Language) as reccomended by some uC specialist.
After a lot of reading (and sometime "lot-of-no-understandings") I stayed on BASIC.
This is my first, true, "Hello World" project:

;Chip Settings
#chip 16F877A,10
#config OSC=HS

;Defines (Constants)
#define DISP_COUNT 4
#define DISP_SEG_A PORTB.0
#define DISP_SEG_B PORTB.1
#define DISP_SEG_C PORTB.2
#define DISP_SEG_D PORTB.3
#define DISP_SEG_E PORTB.4
#define DISP_SEG_F PORTB.5
#define DISP_SEG_G PORTB.6
#define DISP_SEG_DOT PORTB.7
#define DISP_SEL_1 PORTA.0
#define DISP_SEL_2 PORTA.1
#define DISP_SEL_3 PORTA.2
#define DISP_SEL_4 PORTA.3

afis:
DisplayValue 1, 0
Wait 1 s
DisplayValue 2, 1
Wait 1 s
DisplayValue 3, 2
Wait 1 s
DisplayValue 4, 3
Wait 1 s
DisplayValue 1, 4
Wait 1 s
DisplayValue 2, 5
Wait 1 s
DisplayValue 3, 6
Wait 1 s
DisplayValue 4, 7
Wait 1 s
DisplayValue 1, 8
Wait 1 s
DisplayValue 2, 9
Wait 1 s
Goto afis



This program is intended to be used with a 16F877A Microchip PIC,
a 10 MHz crystal and a 4 digit, 7 segment LED display.
It count from 0 to 9 but the number is displayd alternatively from left to right.
As I cannot attach the .hex file, use Copy/Paste and a txt editor to make your own hex file
to upload into the uC:

:020000040000FA
:020000000528D1
:100008000900B0209F200130A100A0015920013033
:10001800F40097200230A1000130A00059200130DF
:10002800F40097200330A1000230A00059200130CD
:10003800F40097200430A1000330A00059200130BB
:10004800F40097200130A1000430A00059200130AD
:10005800F40097200230A1000530A000592001309B
:10006800F40097200330A1000630A0005920013089
:10007800F40097200430A1000730A0005920013077
:10008800F40097200130A1000830A0005920013069
:10009800F40097200230A1000930A0005920013057
:1000A800F4009720072863005828200AA300C220DC
:1000B800A200061086100611861106128612061373
:1000C80086130510851005118511210303190514E0
:1000D8000230210203198514033021020319051582
:1000E800043021020319851522180614A218861453
:1000F80022190615A2198615221A0616A21A86169C
:10010800221B0617A21B86170800F30AC030F1004D
:100118000330F000F00B8E28F10B8C28F20B8A28A4
:10012800F30B8A280800E830F2000330F300892036
:10013800F40B97280800831603130610861006117F
:1001480086110612861206138613051085100511EE
:1001580085118312031308001F10831603139F13BE
:100168009F111F159F141F1007309C008312031343
:100178008501860187018801890108000B30230267
:10018800031800342308013E0310D03EA3000030BA
:100198000318013E8A00230882000A343F340634DB
:1001A8005B344F3466346D347D3407347F346F34B8
:02400E007A3FF7
:00000001FF

Most viewed posts in last 30 days