Timer register definition doesn't match datasheet?

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?

smdh

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.

I think that this is correct.

b26 to b24 TPCS[2:0] Timer Prescaler Select b26 b24
0 0 0: PCLKD/1
0 0 1: PCLKD/4
0 1 0: PCLKD/16
0 1 1: PCLKD/64
1 0 0: PCLKD/256
1 0 1: PCLKD/1024.

The data sheet then goes on to say

b23 to b19 — Reserved These bits are read as 0. The write value should be 0. R/W

It is not clear that an value written to b23 by the union you refer to in the header file path will change the value in the register.

Have you tried to write a value to b23 and read it? Is it anything other than 0?

I'm not clear about the path you posted for the header file. Where does that come from and how that is used by the Arduino Renesas Core.

https://github.com/arduino/ArduinoCore-renesas

I suspect there may be an error in either the datasheet published by Renesas or the header file provided by ARM.

I had previously contacted Renesas support about an error in their documentation, and I've also submitted a pull request for an error in the R7FA4M1AB.svd provided in ARM's CMSIS pack.

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.

Good luck.

Running this code:

  R_GPT0->GTCR_b.TPCS = 0b111; //prescale

  Serial.print("R_GPT0->GTCR_b.TPCS: ");

  Serial.println(R_GPT0->GTCR_b.TPCS, BIN);

Yields this in the console:

R_GPT0->GTCR_b.TPCS: 110

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)

I agree with your finding of a bug in the Arduino core. I suggest that you log it as a new issue in git hub.
https://github.com/arduino/ArduinoCore-renesas/issues

I think that to set prescaler values you will need to write the bits of the GTCR register directly without the group syntax.

For a prescaler of 64

R_GPT0->GTCR = 0x03000000;
//R_GPT0->GTCR = 0b11000000000000000000000000;

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.

Thanks, everyone. I opened an issue here.
Hopefully that gets fixed. I’ll just work around it in the mean time. :folded_hands:

Yes. Thank you for this.

The Arduino Core is using V4.0.0 of the Flexible Software Package (FSP) for Renesas RA MCU Family.

The current version is V6.3.1.

The prescaler bit selection was fixed in V4.1.0

Fixes and Improvements

  • GPT TPCS bitfield width adjusted for some MCUs
union
    {
        __IOM uint32_t GTCR;           /*!< (@ 0x0000002C) General PWM Timer Control Register                         */

        struct
        {
            __IOM uint32_t CST  : 1;   /*!< [0..0] Count Start                                                        */
            uint32_t            : 15;
            __IOM uint32_t MD   : 3;   /*!< [18..16] Mode Select                                                      */
            uint32_t            : 5;
            __IOM uint32_t TPCS : 3;   /*!< [26..24] Timer Prescaler Select                                           */
            uint32_t            : 5;
        } GTCR_b;
    };


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: