What programming language is this?

what programming language is this?

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.

It is a C/C++
It looks like a low level code dealing with microcontroller registers, but it is definitely a C/C++ code.

Please be more specific, what kind of syntax from the snippet above looks unusual to you?

Ciao, Ale.

Are they C/C++ codes or these codes are supported by C/C++ Compiler?

and what is the difference?
The code above has nothing specific, only standard language statements.

It also looks like there are some lines missing at the top.

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?");
    }
}

could be the case, i linked a few other snippets in post#7 that may be more complete.

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.

1 Like

thank you! using this link they mention it briefly C structs and Pointers (With Examples) i was used to the * and & pointers

1 Like

some source code references

the Gclk structure

and how the pointer is defined for samd20 for example

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.

// GCLK->GENCTRL.reg

struct S_GENCTRL
{
  uint8_t reg;
};

struct S_GCLK
{
  S_GENCTRL GENCTRL;
};

S_GCLK *GCLK;


void setup()
{
  Serial.println(GCLK->GENCTRL.reg);
}

void loop()
{

}

// Edit; fixed a mistake as the given code is specific for the Arduino; below for a PC

struct S_GENCTRL
{
  uint8_t reg;
};

struct S_GCLK
{
  S_GENCTRL GENCTRL;
};

S_GCLK *GCLK;

int main()
{
  
}
3 Likes

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++'.

1 Like

I suspect accessing the data through either bit or reg but not both is quite legal in C++.

You can Access for reading the last member that was written to in C++

1 Like

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.

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