yahboom
HomeLearn
Solution
Blog
Online Store
About Us
EN/中文
ESP32 Core Board📖
1. ESP32 Introduction
  • 1. ESP32S3 Overview
  • 2. Environment Setup Introduction
  • 3. Dev Board Introduction
2. ESP-IDF Development Tutorial
1. Before Development
  • 1. Environment Setup (Windows)
  • 2. Environment Setup (Linux)
  • 3. Build and Flash
2. Basic Peripherals
  • 1. LED Control
  • 2. Button Detection
  • 3. Peripheral Interrupts
  • 4. ESP_TIMER
  • 5. GPTIMER
  • 6. Watchdog Timer
  • 7. DMA
  • 8. ADC
  • 9. PWM Breathing LED
  • 10. RGB LED Effects
3. Serial Communication and Storage System
  • 1. Serial UART Communication Experiment
  • 2. I2C_OLED Display Experiment
  • 3. SPI Bus Communication Experiment
  • 4. PSRAM External Memory Experiment
  • 5. Flash Read and Write Experiment
  • 6. USB Emulated Mass Storage Experiment
4. FreeRTOS operating system
  • 1.FreeRTOS Core Concepts Theory
  • 2.Task Creation and Management
  • 3.Queue
  • 4.Binary Semaphore
  • 5.Mutex
  • 6.Event Groups
  • 7.Software Timer
  • 8.Task Scheduler
  • 9.Low Power Mode
  • 10.Deadlock
  • 11.Priority Inversion
5. Network Protocols and Wireless Communication
  • 1. WIFI Network Basics
  • 2. WIFI STA Mode
  • 3. WIFI AP Mode
  • 4. UDP Client Experiment
  • 5. UDP Server Experiment
  • 6. TCP Client Experiment
  • 7. TCP Server Experiment
  • 8. Socket Programming Basics
  • 9. HTTP Client Experiment
  • 10. HTTP Server Experiment
  • 11. MQTT IoT Communication Protocol
  • 12. WIFI OTA Upgrade
  • 13. Bluetooth Fundamentals
  • 14. Bluetooth Slave Mode
  • 15. Bluetooth Master Mode
6. Audio Applications
  • 1.I2S Microphone Recording and U-Disk Playback
  • 2.I2S Audio Playback Experiment
  • 3.Repeater Experiment
7. Xiaozhi AI Voice
  • 1. Burn Xiaozhi Firmware
  • 2. Network and Configuration
  • 3. AI Agent Settings
  • 4. Program Analysis
  • 5. Free Chat
3. Arduino Development Course
1. Before Development
  • 1. Environment Setup
  • 2. Build and Flash
2. Basic Peripherals
  • 1. LED Control
  • 2. Button Detection
  • 3. Peripheral Interrupts
  • 4. ESP_TIMER
  • 5. GPTIMER
  • 6. Watchdog Timer
  • 7. ADC
  • 8. PWM Breathing LED
  • 9. RGB LED Effects
3. Serial Communication and Storage System
  • 1. Serial UART Communication
  • 2. I2C OLED Display
  • 3. SPI Bus Communication
  • 4. Flash Read and Write
  • 5. USB Emulated Mass Storage
4. Network Protocols and Wireless Communication
  • 1. WIFI Network Basics
  • 2. WIFI STA Mode
  • 3. WIFI AP Mode
  • 4. UDP Server Experiment
  • 5. UDP Client Experiment
  • 6. TCP Client Experiment
  • 7. TCP Server Experiment
  • 8. HTTP Client Experiment
  • 9. HTTP Server Experiment
  • 10. MQTT IoT Communication Protocol
  • 11. WIFI OTA Upgrade
  • 12. Bluetooth Fundamentals
  • 13. Bluetooth Slave Mode
  • 14. Bluetooth Master Mode
5. Audio Applications
  • 1.I2S Microphone Recording and U-Disk Playback
  • 2.I2S Audio Playback Experiment
  • 3.Repeater Experiment
4. MicroPython Development Course
1. Before Development
  • 1. Environment Setup
  • 2. Build and Flash
  • 3. Thonny IDE
2. Basic Peripherals
  • 1. LED Control
  • 2. Button Detection
  • 3. Peripheral Interrupts
  • 4. TIMER_IT
  • 5. Watchdog Timer
  • 6. ADC
  • 7. PWM Breathing LED
  • 8. RGB LED Effects
3. Serial Communication and Storage System
  • 1. Serial UART Communication Experiment
  • 2. I2C_OLED Display Experiment
  • 3. SPI Bus Communication Experiment
  • 4. Flash Read and Write Experiment
  • 5. Print Storage
4. Network Protocols and Wireless Communication
  • 1. WIFI Network Basics
  • 2. WIFI STA Mode
  • 3. WIFI AP Mode
  • 4. UDP Client Experiment
  • 5. UDP Server Experiment
  • 6. TCP Client Experiment
  • 7. TCP Server Experiment
  • 8. HTTP Client Experiment
  • 9. HTTP Server Experiment
  • 10. MQTT
  • 11. Bluetooth Fundamentals
  • 12. Bluetooth Slave Mode
  • 13. Bluetooth Master Mode
5. Audio Applications
  • 1. I2S Audio Playback Experiment
  • 2. Repeater Experiment
Download
  • Code
  • Hardware_Info
  • 3D_Model_File
  • Software_Tools
  • VMware
  • VM_File
Download ZIP

Welcome to ESP32 Core Board repository

5. GPTIMER

GPTIMER


GPTIMER1. GPTIMER Timer Overview2. Core Concepts2.1 Hardware Timer Architecture2.2 IRAM_ATTR Attribute2.3 esp_timer vs GPTIMER Comparison3. Hardware Dependencies4. Project Architecture and Flow5. Source Code Details5.1 Global Variables and Timer Handle5.2 Timer ISR5.3 setup() — Four Steps of Timer Initialization6. Operation Flow6.1 Build and Flash6.2 Normal Behavior6.3 Verification Method7. Common Issues


1. GPTIMER Timer Overview

GPTIMER directly operates ESP32 hardware timers, executing callbacks in ISR with deterministic delay and no RTOS scheduling jitter. This experiment configures a 1-second period hardware timer and prints the trigger count in the ISR. Compared to esp_timer in the previous section, GPTIMER trades convenience (ISR cannot use Serial, this experiment is for teaching only) for nanosecond-level timing determinism.

Typical Application Scenarios:

Application TypeExample
Precision TimingMotor PWM deadband control, stepper motor pulse sequences
Frequency MeasurementInput capture frequency measurement, tachometer signal processing
Pulse GenerationPrecision delay triggering, laser/ultrasonic ranging trigger signals
High-speed CountingEncoder pulse counting, production line photoelectric counting

2. Core Concepts

2.1 Hardware Timer Architecture

ESP32 has 4 groups of 64-bit hardware timers (TIMG0/1, 2 per group). The clock source is APB_CLK = 80MHz, divided to obtain the counting frequency.

​x
timerBegin(divider) Parameter Description:
  APB_CLK = 80,000,000 Hz
  Post-divider frequency = 80,000,000 / divider
  If divider = 80 → post-divider = 1MHz → each tick = 1μs

timerAlarm(timer, 1000000, true, 0) Parameter Description:
  1000000 = alarm threshold (tick count)
  At 1MHz, 1,000,000 ticks = 1,000,000 μs = 1 second
  true = auto-reload, counter resets to zero after trigger and restarts counting

2.2 IRAM_ATTR Attribute

xxxxxxxxxx
void IRAM_ATTR Timer_ISR(void) { ... }

IRAM_ATTR places function code in SRAM instead of Flash. Flash reads have the risk of cache misses, and a cache miss in ISR can cause unpredictable delays or even watchdog reset. IRAM_ATTR ensures ISR code is always in RAM with deterministic execution time.

This experiment calls Serial.printf in the ISR - this is strictly prohibited in production code. It is only used here for teaching convenience to observe timer behavior.

2.3 esp_timer vs GPTIMER Comparison

Featureesp_timerGPTIMER
API Styleesp_timer_create + start_periodictimerBegin + timerAttachInterrupt + timerAlarm + timerStart
Callback ContextTimer Task (Serial available)ISR (Serial unavailable)
Timing JitterHas RTOS scheduling jitterNanosecond-level determinism
volatile RequiredYes (cross-task sharing)Yes (ISR and main loop sharing)

3. Hardware Dependencies

This project is a pure software experiment with no external hardware required. Only USB cable needed to connect serial port for output viewing.


4. Project Architecture and Flow

xxxxxxxxxx
setup()
  ├─ Serial.begin(115200)              // Initialize serial port
  ├─ timer = timerBegin(1000000)       // Initialize timer, divide to 1MHz
  ├─ timerAttachInterrupt(timer, ISR)  // Bind ISR function
  ├─ timerAlarm(timer, 1000000, true, 0) // Alarm threshold = 1s, auto-reload
  └─ timerStart(timer)                 // Start counting

loop()                                 // Empty loop

[Hardware Interrupt]
  └─ Timer_ISR()                       // Triggers once per second
       ├─ trigger_count++              // Increment counter
       └─ Serial.printf(...)           // Print (teaching only! Forbidden in production)

5. Source Code Details

Full source code: Source Code → Arduino → 1.Basic Peripherals → 5.GP_TIMER → 5.GP_TIMER.ino

5.1 Global Variables and Timer Handle

hw_timer_t* is a pointer to the hardware timer control block, volatile variables are used for sharing between ISR and main loop.

xxxxxxxxxx
hw_timer_t *timer = NULL;
volatile uint32_t trigger_count = 0;

timer does not need volatile - it is initialized in setup() and never changes afterward, ISR does not write to it.

5.2 Timer ISR

IRAM_ATTR places the function in RAM ensuring deterministic ISR execution time.

xxxxxxxxxx
void IRAM_ATTR Timer_ISR(void) {
  trigger_count++;
  Serial.printf("Trigger Count: %u | Current Time: %lu ms\n", trigger_count, millis());
}

Warning: Serial.printf is unsafe in ISR, this experiment uses it only for teaching convenience. In real projects, ISR should only set flags, printing should be done in loop().

5.3 setup() — Four Steps of Timer Initialization

Hardware timer initialization has more steps than esp_timer, each step corresponds to an independent API.

xxxxxxxxxx
void setup() {
  Serial.begin(115200);
  Serial.println("GPTIMER Pure Timer Test Started");

  timer = timerBegin(1000000);                     // ① Initialize, divide to 1MHz
  timerAttachInterrupt(timer, Timer_ISR);          // ② Bind ISR
  timerAlarm(timer, 1000000, true, 0);             // ③ Alarm threshold 1s, auto-reload, infinite
  timerStart(timer);                               // ④ Start
}

void loop() {}  // Empty loop - all work done by timer ISR

The four steps must not be reordered: timer must be created first, then bind ISR, set alarm threshold, finally start.


6. Operation Flow

6.1 Build and Flash

Arduino flash process: See Arduino → 1. Before Development → 2. Build and Flash.

6.2 Normal Behavior

Open Serial Monitor (115200 bps), print one line of trigger count every second.

Serial Print

6.3 Verification Method

OperationExpected Result
Power on and observe serialPrint one line per second, count increments
Stopwatch comparison10 triggers ≈ 10 seconds

7. Common Issues

SymptomCauseSolution
Serial hangs in ISRSerial operations forbidden in ISRChange to only set flags, print in loop()
Timing inaccurateDivider configuration errorConfirm APB_CLK / divider = 1MHz
Compilation error hw_timer_t undefinedMissing ESP32 Arduino Core headersConfirm board selected is ESP32S3
ESP_TIMER
Watchdog Timer

COMPANY

  • About us
  • Team Style
  • Blog

LEARNING & EXPLORATION

  • Tutorials
  • Edge AI hardware
  • Educational robots
  • Embedded intelligent hardware

WHERE TO BUY

  • Yahboom Online Store
  • Amazon
  • Alibaba
  • AliExpress
  • Mercado

TECHNOLOGY SUPPORT

  • Email: support@yahboom.com

BE A DISTRIBUTOR

  • Email: sales@yahboom.com
QR Code
QR Code

Copyright © 2017-2026 Yahboom. All Rights Reserved.

EN/中文