Concepts First
- MTimer (machine timer): the system-level "heartbeat" providing periodic interrupts, microsecond timestamps, and delays — the timing base of every example.
- Tick: each timer trigger is a tick; the example prints
tick:Nonce per second. - MTimer vs TIMER: MTimer is the system machine timer; TIMER is the general-purpose timer (multi-channel compare/capture), see TIMER.
Example Overview
This page is based on the mtimer example in the official Bouffalo SDK (examples/peripherals/mtimer), which demonstrates the machine timer (MTimer) interrupt period:
bflb_mtimer_config(1000000, systick_isr)sets the period to 1,000,000 microseconds (1 second) and registers the callback;- The timer fires every second; the callback increments a counter and prints
tick:N; - MTimer is the system-level timer used as the time base for OS ticks, delays, and performance stats.
Operation Steps
No external wiring is needed for this page. Open a terminal and enter the MTimer example directory (prerequisite: set up the environment as in Quick Start (Linux) or Windows):
cd examples/peripherals/mtimerRun the build command. The Ai-M62 (BL616) and Ai-M61 (BL618) belong to the same series, so both use bl616:
make CHIP=bl616 BOARD=bl616dkConnect the board with a USB cable, hold the BOOT button (IO2 on the Ai-M61-32S-Kit), briefly press EN/RST to enter download mode, then flash (replace the serial port with the one on your computer):
make flash CHIP=bl616 COMX=/dev/ttyUSB0Open a serial tool (baud rate 2000000). The example configures the MTimer period to 1 second; the ISR prints one line per trigger: tick:1, tick:2, … incrementing every second.
Code Execution Flow
The complete execution flow from startup to running is shown below (loop arrows mean repeated execution):
APIs Used by the Example
bflb_mtimer_config(period_us, isr)
Configures the machine timer period and registers the ISR. The period is in microseconds; 1000000 = 1 second.
Parameters:
period_us: period in microsecondsisr: callback, e.g.void isr(void)
Return: 0 on success; negative error code on failure
Other capabilities
MTimer also provides bflb_mtimer_delay_ms (millisecond delay) and bflb_mtimer_get_time_us (microsecond timestamp), used by many examples; this example only demonstrates the periodic interrupt.
Complete Code
The complete source below matches the effect described on this page. It is based on the official example (examples/peripherals/mtimer); the LED pins are adapted to the Ai-M61/62-32S-Kit onboard RGB LED. Collapsed by default, click to expand:
📜 Click to expand mtimer/main.c full code
#include "bflb_mtimer.h"
#include "board.h"
void systick_isr()
{
static uint32_t tick = 0;
tick++;
printf("tick:%d\r\n", tick);
}
int main(void)
{
board_init();
bflb_mtimer_config(1000000, systick_isr);
while (1) {
}
}FAQ
The tick interval is not 1 second
bflb_mtimer_config takes microseconds; change 1000000 to adjust the period. If the rate is odd, check that other code is not frequently disabling global interrupts.
Can I do heavy work in the ISR?
The callback runs in interrupt context and should stay short (the example only counts and prints); move heavy work to the main loop or a task using a flag.
MTimer vs TIMER?
MTimer is the system machine timer (periodic interrupt/timestamp); TIMER is the general-purpose timer with multiple compare/capture channels (see TIMER).
Have questions?
For any other questions, visit the unified Q&A and discussion board: Ai-Thinker Discussions

