void setup()
// Setup the program
{
GCLK->GENCTRL.reg = GCLK_GENCTRL_OE | // Enable GCLK5 output
GCLK_GENCTRL_IDC | // Improve duty-cycle to 50%
GCLK_GENCTRL_GENEN | // Enable generic clock
GCLK_GENCTRL_SRC_OSC8M | // Select 8 MHz as source \
GCLK_GENDIV_DIV(16) // Divide by 16 to produce 500 kHz
GCLK_GENCTRL_ID(5); // Set the GCLK ID to GCLK5
while (GCLK->STATUS.bit.SYNCBUSY); // Wait for synchronization
PORT->Group[PORTA].PINCFG[21].bit.PMUXEN = 1; // Switch on port pin PA21's multiplexer
PORT->Group[PORTA].PMUX[21 >> 1].reg |= PORT_PMUX_PMUXE_H; // Switch the PA21's port multiplexer to GCLK IO
}
void loop()
{
}
I found in this topic: SAMD21 with 96MHz counter?
but also other topics concerning the SAMD21 and Atmega processors. Chatgpt tells me it is C, but i've programmed in c and never encountered anything like this. The syntax is also unfamiliar to me.
Also, if someone has any tips to make this topic easier to find i would appreciate it as i am still quite new to using this forum and would like to make my topics easier to find for people with the same problem or question.
for example this part: GCLK->GENCTRL.reg
What does the arrow pointing mean? is GENCTRL.reg a predefined registry? here are some links to other code snippets:
when i think of C and programming in C, i think of code structured like this with if, else, do, while, case and the whole shebang:
#include <stdio.h>
int main(){
int y = 15;
if (y == 15){
printf("%d", y);
}
else{
printf("How did we get here?");
}
}
The arrow (->) indicates that GCLK is a pointer to a struct or class. That struct has a field called GENCTRL which is an instance of another struct or class. And that other struct or class has a field called reg.
It's just C/C++. The fields are specific to the used core. But nothing stops you from defining these yourself.
The below will happily compile on a PC or an Arduino; be careful, the pointer does not point anywhere so attempting to do something with it can result in crashes.
I will opine that code using SAMD register definitions like that should only be compiled as 'C'. That's because it's full of union types that are used to type pun between bitfields and entire bytes / words. For example, from gclk.h:
typedef union {
struct {
uint8_t SWRST:1; /*!< bit: 0 Software Reset */
uint8_t :7; /*!< bit: 1.. 7 Reserved */
} bit; /*!< Structure used for bit access */
uint8_t reg; /*!< Type used for register access */
} GCLK_CTRL_Type;
Using unions to type pun is legal in 'C', but not 'C++'.
Agree. I do remember seeing code that flopped back and in accessing the union's datatypes. It was in the SAMD core functions and compiled a 'C'. The core treats all SAMD memory mapped peripherals in this way.