🛠 Temps réel : timers matériels et interruptions sur le GIGA R1

Arduino GIGA R1 · avance · Schéma : IMG-giga-r1-a2-1-timer-interruptions.svg — Les timers matériels du STM32H747XI pilotant une sortie DAC du GIGA R1 à cadence fixe pendant que la boucle loop reste libre pour le reste du programme.

Objectives

Move beyond the "delay everywhere" model to generate an audio signal at an exact rate. The GIGA R1's 480 MHz Cortex-M7 features hardware timers and interrupts; we will use a hardware ticker (mbed::Ticker, exposed by the Arduino Mbed core) to sample a waveform on the 12-bit DAC while loop remains available. You will also learn about interrupt precautions related to documented bugs (#789 regarding attachInterrupt).

Hardware

Complete Code

// GIGA R1 WiFi — ton precis genere par ticker materiel, loop reste libre
#include <Arduino.h>
#include "mbed.h"

const int DAC_PIN = DAC0;          // jack audio embarque
mbed::Ticker ticker;
uint32_t phase = 0;
const uint32_t PAS = 32;           // pas de phase -> frequence du ton
volatile uint32_t compteur = 0;    // partage ISR <-> loop : volatile obligatoire

void echantillonner {            // s'execute en contexte d'interruption
  phase = (phase + PAS) & 0xFFF;   // onde 12 bits
  analogWrite(DAC_PIN, phase);
  compteur++;
}

void setup {
  Serial.begin(115200);
  analogWriteResolution(12);
  ticker.attach(echantillonner, 0.0001);  // 10 kHz constants
}

void loop {
  static uint32_t dernier = 0;
  if (compteur - dernier >= 10000) {      // ~1 s d'echantillons a 10 kHz
    dernier = compteur;
    Serial.print("echantillons 10 kHz : ");
    Serial.println(compteur);
    // loop peut faire autre chose : capteurs, reseau, I2C...
  }
}

Expected Verification

A continuous and stable sound comes out of the jack, without crackling even if loop performs long processing tasks. The Serial Monitor displays a counter that increases by exactly 10,000 per second, proving that the rate is maintained by the hardware timer and not by loop.

Common Errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials