Cant find what are the pre scales possible?

The data sheet is horrible, if you search for the words: "pre scale,pll, GCLK_GENDIV_DIV" you find nothing.
Anyway, what are the possible pre scales for the timer, or what pre scales you can put here :

  // Set up the generic clock (GCLK4) used to clock timers
            REG_GCLK_GENDIV = GCLK_GENDIV_DIV(prescale) |          // Divide the 48MHz clock source by divisor 3: 48MHz/3=16MHz
                              GCLK_GENDIV_ID(4);            // Select Generic Clock (GCLK) 4
            while (GCLK->STATUS.bit.SYNCBUSY);              // Wait for synchronization

Tnx.

Hi BenStlr,

That's the generic clock divisor. The generic clock source is divided by whatever number you put in the brackets. For instance GCLK_GENDIV_DIV(3) will divide the GCLK (48MHz) by 3, giving 16MHz. This 16MHz clock is then routed to the timer further down stream. In the timer peripheral itself there's the prescaler that divides the 16MHz clock down further, this is defined in the timer's CTRLA register.

REG_TC4_CTRLA |= TC_CTRLA_PRESCALER_DIV64 |     // Set prescaler to 64, 16MHz/64 = 256kHz
                   TC_CTRLA_MODE_COUNT8 |         // Set the counter to 8-bit mode
                   TC_CTRLA_ENABLE;               // Enable TC4
  while (TC4->COUNT8.STATUS.bit.SYNCBUSY);        // Wait for synchronization

The available prescalers are on page 628 of the datasheet and are 1, 2, 4, 8, 16, 64, 256 and 1024.

Thanks a lot, thats i already know, I am looking for the specific numbers that you can put in this place.
For example can i put 5 ? 48/5 is a float, and its not a square of 2. (the routed clock usually can be 16,8,4,2...)

So can i put there 1,2,3,4,5,6,7,8,9 ?? the output clock would be a strange number (not a square of 2).

The division factor for GCLKs 3 - 7 is an 8-bit number between 0 and 255. This will be integer division (not floating point). The clock the signal is then returned to a 50% duty cycle, by setting the GCLK_GENCTRL_IDC bit in the GCLK Generator Control Register (REG_GCLK_GENCTRL).

I am not sure I get what you are saying here at all.

To feed a timer , these options are possible ?

48Mhz/1=48Mhz ticking
48Mhz/2=24Mhz ticking
48Mhz/3=16Mhz ticking
48Mhz/4=12Mhz ticking
48Mhz/5=10Mhz ticking (9.6->10)
48Mhz/6=8Mhz ticking
48Mhz/7=7Mhz ticking (6.8->7)
..

1.Is it true?
2.If the timers are 16bits, I am not get why the CC are 0-255 ? (255 is 8 bits) ?

Regarding the 16bits timer, ok i get it , you set it to be 8bits thats why its 0-255.

Martin, I would like to modify your code to a timer(not PWM), with TCC0, on 24Bits resolution, can you explain how to do that ?

  // Set up the generic clock (GCLK4) used to clock timers
            REG_GCLK_GENDIV = GCLK_GENDIV_DIV(prescale) |          // Divide the 48MHz clock source by divisor 3: 48MHz/3=16MHz
                              GCLK_GENDIV_ID(4);            // Select Generic Clock (GCLK) 4
            while (GCLK->STATUS.bit.SYNCBUSY);              // Wait for synchronization
          
            REG_GCLK_GENCTRL = GCLK_GENCTRL_IDC |           // Set the duty cycle to 50/50 HIGH/LOW
                               GCLK_GENCTRL_GENEN |         // Enable GCLK4
                               GCLK_GENCTRL_SRC_DFLL48M |   // Set the 48MHz clock source
                               GCLK_GENCTRL_ID(4);          // Select GCLK4
            while (GCLK->STATUS.bit.SYNCBUSY);              // Wait for synchronization
          
            // Feed GCLK4 to TC4 and TC5
            REG_GCLK_CLKCTRL = GCLK_CLKCTRL_CLKEN |         // Enable GCLK4 to TC4 and TC5
                               GCLK_CLKCTRL_GEN_GCLK4 |     // Select GCLK4
                               GCLK_CLKCTRL_ID_TC4_TC5;     // Feed the GCLK4 to TC4 and TC5
            while (GCLK->STATUS.bit.SYNCBUSY);              // Wait for synchronization
          
            REG_TC4_COUNT8_CC0 = 0x55;                      // Set the TC4 CC0 register to some arbitary value
            while (TC4->COUNT8.STATUS.bit.SYNCBUSY);        // Wait for synchronization
            REG_TC4_COUNT8_CC1 = 0xAA;                      // Set the TC4 CC1 register to some arbitary value
            while (TC4->COUNT8.STATUS.bit.SYNCBUSY);        // Wait for synchronization
            REG_TC4_COUNT8_PER = 0xFF;                      // Set the PER (period) register to its maximum value
            while (TC4->COUNT8.STATUS.bit.SYNCBUSY);        // Wait for synchronization
          
            //NVIC_DisableIRQ(TC4_IRQn);
            //NVIC_ClearPendingIRQ(TC4_IRQn);
            NVIC_SetPriority(TC4_IRQn, 0);    // Set the Nested Vector Interrupt Controller (NVIC) priority for TC4 to 0 (highest)
            NVIC_EnableIRQ(TC4_IRQn);         // Connect TC4 to Nested Vector Interrupt Controller (NVIC)
          
            REG_TC4_INTFLAG |= TC_INTFLAG_MC1 | TC_INTFLAG_MC0 | TC_INTFLAG_OVF;        // Clear the interrupt flags
            REG_TC4_INTENSET = TC_INTENSET_MC1 | TC_INTENSET_MC0 | TC_INTENSET_OVF;     // Enable TC4 interrupts
            // REG_TC4_INTENCLR = TC_INTENCLR_MC1 | TC_INTENCLR_MC0 | TC_INTENCLR_OVF;     // Disable TC4 interrupts
           
            REG_TC4_CTRLA |= TC_CTRLA_PRESCALER_DIV64 |     // Set prescaler to 64, 16MHz/64 = 256kHz
                             TC_CTRLA_MODE_COUNT8 |         // Set the counter to 8-bit mode
                             TC_CTRLA_ENABLE;               // Enable TC4
            while (TC4->COUNT8.STATUS.bit.SYNCBUSY);        // Wait for synchronization

Thank you.

The GCLK division factor is part of the generic clock. The generic clocks are completely separate and are up stream from the timer, or for that matter any other of the SAMD21's peripherals.

What the geneic clock registers allow you to do is to specify the generic clock source, divide it down (if required), and connect it to your peripherals of choice that are down stream.

The 8-bits size the GCLK division factor (0-255) and has nothing to do with the timer peripheral itself, it divides down the GCLK. When you connect the GCLK to the timer, this clock is connected to the timer. The timer perhipheral can then divide the clock down further using its prescaler (if required).

Thank you Martin, so again, your explanation is exactly what I wrote, my question was, how you divided the GCLK, because for some numbers you get odd frequencies to feed into the timer, so as I'v showed for example:

48Mhz/5=10Mhz ticking (9.6 ->10)

Setting a value of 5, will divided 48Mhz it to be 9.6Mhz, or 10Mhz clock to be feed into the timer.

Is that right ?

Also, could you show how to modify your own code, to get a timer with 24bits resolution ?
(I could find many PWM examples you show, not a TCC 24bits timer)

Thank you for your kind help !

Martin, I would like to modify your code to a timer(not PWM), with TCC0, on 24Bits resolution, can you explain how to do that ?

I posted some code here: Create a timer function with frequency - Arduino Zero - Arduino Forum

I'm getting confused with all these multiple threads, can we just stick to one?

Oh sorry for confusing you ! :slight_smile: you are so nice that you really help everyone like that .

Your code works, but works strange, if i set this :

REG_TCC0_CC0 = 0x0000FF; // Set counter compare 0

and then I check the interrupt on condition:

if (TCC0->INTFLAG.bit.MC0 && TCC0->INTENSET.bit.MC0)
//print here !

It will print in a frequency of once per second, but the calculation show that the CC0 is FF which is 255, so:
48Mhz/255 = 188Khz...

and I get 2 Hz...(2 prints per second)

Am I missing the point here ?

Thank you again for everything....

and I get 2 Hz...(2 prints per second)

With TCC0 we're back to the PER register setting timer's TOP value, so you can control the timer's frequency by setting the PER register to the appropriate value.

The formula for the timer frequency is:

Timer frequency = GCLK frequency / (N * PER + 1)

where
PER = period register value
N = timer's prescaler value

So, the in the code example:

GCLK frequency = 48MHz
PER = 2^24 = 16777216 = 0xFFFFFF (hex)
N = 1

Timer frequency = 48MHz / (1 * 16777216 + 1) = 2.86Hz (almost 3Hz)

As were cycling around the timer every 0.3s (1/2.86), the value of CC0 determines when in the 0.3s period that the interrupt handler is called. CC0 = 0x000000: it's called at the beginning, CC0 = 0xFFFFFF: it's called at the end. CC0 should be set between 0 and the PER register value.

Yea stupid me I'v figured this out yesterday. thank you.