Problem by GCLK registry read

Hi guys, I have a problem by reading the GCLK registry in my Arduino MKR WiFi 1010.

I've tried to follow the (not so detailed) description from the Atmel-42181-SAM-D21 Datasheet. It says:

14.6.4.1 Indirect Access

...

To read a register, the user must write the ID of the channel, i, in the corresponding register. The value of the register for the corresponding ID is available in the user interface by a read access.

For example, the sequence to read the GENCTRL register of generic clock generator i is:

  1. Do an 8-bit write of the i value to GENCTRL.ID
  2. Read GENCTRL

This is how my code looks like:

setup() {
  int32_t reg;

  while (GCLK->STATUS.bit.SYNCBUSY); // just to be sure

  Serial.print(F("GCLK->GENCTRL[0]: "));

  // variant 1, freezes the processor
  GCLK->GENCTRL.reg = GCLK_GENCTRL_ID(0);
  while (GCLK->STATUS.bit.SYNCBUSY); // optionally tried to wait for syncing before read, same result
  reg = GCLK->GENCTRL.reg;

  // variant 2, freezes the processor
  GCLK->GENCTRL.bit.ID = 0;
  while (GCLK->STATUS.bit.SYNCBUSY); // optionally tried to wait for syncing before read, same result
  reg = GCLK->GENCTRL.reg;

  Serial.println(reg, BIN);
}

Where am I wrong?
Thanks in advance
Zsolt

The issue is you are not following the guide to the letter. Datasheets tend to be very explicit.

  1. Do an 8-bit write of the i value to GENCTRL.ID

The SAMD31 has a 32bit processor and the register is 32bits wide. So, the following will create a 32bit write, overwriting all the other bits in that register with 0.

GCLK->GENCTRL.reg = GCLK_GENCTRL_ID(0);

Luckily we can look how the Arduino guys did it. For instance in the following file.

C:\Users\UserName\AppData\Local\Arduino15\packages\arduino\hardware\samd\1.8.11\bootloaders\sofia\Bootloader_D21_Sofia_V2.1\src\ASF\sam0\drivers\system\clock\clock_samd21_r21\gclk.c

/* Select the appropriate generator */
((uint8_t)&GCLK->GENCTRL.reg) = generator;

So, for your example

((uint8_t)&GCLK->GENCTRL.reg) = GCLK_GENCTRL_ID( 0 );

... aaaand it works. Thank you very much! 8)

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.