I setup one of the timers to do input capture by setting the timer registers directly, like this: R_GPT0->GTCR_b.MD = xxx;
It works, but I found that the prescaler wasn't set how I expected. I looked at the register definition in the header file (R7FA4M1AB.h) and it doesn't match the datasheet.
The RA4M1 group datasheet (page 417) says GTCR[26..24] are TPCS (Timer Prescaler Select).
The R7FA4M1AB.h header (line 5219) says GTCR[26..23] are TPCS (Timer Prescaler Select). It specifies 4 bits for it in the union.
Am I reading these wrong? Is there a better way to setup the timer?
The current version of FSP shows 24..=26 in the header and SVD. ArdunioCore uses an old version of FSP which shows 23..=26 in the C header and 24..=26 in the SVD. I'm not using the FSP or ArduinoCore but using bits 24..=26 per the SVD scales the clock about how I'd expect.
Honestly, it's disappointing to see so much unrealized potential with the Uno R4 models.
However, in this case, it's unlikely that "Timer Prescaler Select" is a 4-bit setting, so wouldn't it be better to write zero to the 23th bit?
Does this mean that the prescaler isn't functioning as expected, even though it works?
If you could post the full sketch and a detailed description of your issue, someone might be able to help.
(Unfortunately I only have experience programming with FSP and no experience register programming, so I don't think I can be of much help.)
You could also contact Renesas support or ask a question on the Renesas forum.
Sure seems like the register is off by one bit position, and b23 doesn’t stick.
The header file is in this folder and was installed by the Arduino IDE somehow:
.\AppData\Local\Arduino15\packages\arduino\hardware\renesas_uno\1.5.2\variants\MINIMA\includes\ra\fsp\src\bsp\cmsis\Device\RENESAS\Include
I meant that timer was working, but the prescaler was wrong. Writing to the register does change the prescale, but everything is off by one bit position.
There’s very little to the sketch outside of setting up the timer. I poll a flag to see if an input capture happened, and read the count out of the timer.
I played with the FSP tool inside of e2, but it started pulling in HAL stuff and it didn’t seem like it would generate something I could easily paste back into an Arduino environment.
Anyway, here’s the full setup code:
R_MSTP->MSTPCRD_b.MSTPD5 = 0; //enable GPT0 module clock (General PWM Timer 321 to 320 Module Stop)
delayMicroseconds(10);
R_GPT0->GTCR_b.CST = 0; //stop timer
R_GPT0->GTCR_b.MD = 0; //saw-wave PWM (000b)
R_GPT0->GTUDDTYC = 0b11; //set 11b first (?)
R_GPT0->GTUDDTYC = 0b01; //01b for up-counting
R_GPT0->GTCR_b.TPCS = 0b011; //PCLKD/64 prescale
R_GPT0->GTPR = 0xFFFFFFFF; //max cycle
R_GPT0->GTCNT = 0; //0 initial count
//GTCCRA input capture enabled on the...
R_GPT0->GTICASR_b.ASCARBL = 1; //rising edge of GTIOCA input when GTIOCB input is 0
R_GPT0->GTICASR_b.ASCARBH = 1; //rising edge of GTIOCA input when GTIOCB input is 1
//set up Pin D6 (P106)
R_PFS->PORT[1].PIN[6].PmnPFS_b.PMR = 1; //Used as an I/O port for peripheral functions
R_PFS->PORT[1].PIN[6].PmnPFS_b.PSEL = 0b11; //GTIOC0B (GPT peripheral function)
That header comes from FSP and ArduinoCore uses an outdated version of FSP. Renesas already fixed it upstream. You'll either need to use the corrected structure or set the bits manually.
If you're not already you'll also need to disable write protection to change any PFS registers.
In order to make relevant information available to any who are interested in this subject, I'll share a link to the formal report @skidlz submitted to the Arduino developers: