It seems to have caused confusion. I just didn't want it to look like an indefinite value, and -1 is completely unnecessary. Simply uint8_t type; is fine.
Also, when I looked at the RA4M1 manual again, I found that there was inappropriate code.
Renesas RA4M1 Group User’s Manual: Hardware
The division ratios of AGT_TIMER and GPT_TIMER to the respective source clocks have the following constraints:
I will post the retested code:
#include <Arduino.h>
#include <FspTimer.h>
#include <math.h> // for round()
FspTimer fsp_timer;
volatile uint32_t lap_time;
volatile bool irq_fired = false;
static void IrqCallback(timer_callback_args_t* p_args) {
lap_time = millis() - lap_time;
irq_fired = true;
}
void setup() {
Serial.begin(115200);
while (!Serial);
pinMode(LED_BUILTIN, OUTPUT);
// Get available timer from the timer pool (GPT32 x 2, GPT16 x 6 and AGT x 2).
// Note: Two of the GPT32 timers are already in use, so we can only get 16-bit timers.
uint8_t type;
int channel = FspTimer::get_available_timer(type); // Get GPT_TIMER as a priority
if (channel == -1) {
Serial.println("Cannot get timer.");
return;
}
#if 0
// Generate an interrupt after 10 milliseconds.
fsp_timer.begin(TIMER_MODE_ONE_SHOT, type, channel, 100.0 /* Hz */, 50.0 /* % */, IrqCallback, nullptr);
#else
/* PCLK divisors
https://github.com/arduino/ArduinoCore-renesas/blob/main/variants/MINIMA/includes/ra/fsp/inc/api/r_timer_api.h#L130-L144
typedef enum e_timer_source_div
{
TIMER_SOURCE_DIV_1 = 0, ///< Timer clock source divided by 1
TIMER_SOURCE_DIV_2 = 1, ///< Timer clock source divided by 2
TIMER_SOURCE_DIV_4 = 2, ///< Timer clock source divided by 4
TIMER_SOURCE_DIV_8 = 3, ///< Timer clock source divided by 8
TIMER_SOURCE_DIV_16 = 4, ///< Timer clock source divided by 16
TIMER_SOURCE_DIV_32 = 5, ///< Timer clock source divided by 32
TIMER_SOURCE_DIV_64 = 6, ///< Timer clock source divided by 64
TIMER_SOURCE_DIV_128 = 7, ///< Timer clock source divided by 128
TIMER_SOURCE_DIV_256 = 8, ///< Timer clock source divided by 256
TIMER_SOURCE_DIV_512 = 9, ///< Timer clock source divided by 512
TIMER_SOURCE_DIV_1024 = 10, ///< Timer clock source divided by 1024
} timer_source_div_t;
*/
// Get source clock frequency of the acquired timer.
uint32_t clock_hz;
timer_source_div_t src_div;
if (type == GPT_TIMER) {
clock_hz = R_FSP_SystemClockHzGet(FSP_PRIV_CLOCK_PCLKD);
src_div = TIMER_SOURCE_DIV_1024; // 1, 4, 16, 64, 256, 1024
Serial.print("GPT_TIMER ");
} else {
clock_hz = R_FSP_SystemClockHzGet(FSP_PRIV_CLOCK_PCLKB);
src_div = TIMER_SOURCE_DIV_8; // 1, 2, 8
Serial.print("AGT_TIMER ");
}
// Calculate the number of counts for 10 msec
uint32_t counts = round((10.0f /* msec */ / 1000.0f) * clock_hz / (1 << src_div));
Serial.print("channel:" + String(channel));
Serial.print(", clock:" + String(clock_hz));
Serial.print(", div:" + String(1 << src_div));
Serial.print(", counts:" + String(counts) + "\n");
// Generate an interrupt after 10 milliseconds. (The count starts from 0)
fsp_timer.begin(TIMER_MODE_ONE_SHOT, type, channel, counts - 1, 1, src_div, IrqCallback, nullptr);
#endif
// Start the timer
fsp_timer.setup_overflow_irq();
fsp_timer.open();
fsp_timer.start();
lap_time = millis();
Serial.println("Start.");
}
void loop() {
if (irq_fired) {
irq_fired = false;
digitalWrite(LED_BUILTIN, HIGH);
Serial.println("Fired after " + String(lap_time) + " msec");
}
}
If the divide ratio is not set correctly it will not work as expected, however specifying the frequency with begin() will set the appropriate divide ratio and is recommended.

