Accessing SAM MCU Registers in C

This page will show you how to access SAM MCU Peripheral registers and bit fields in C, without the use of any framework, such as Advanced Software Framework (ASF3) or START. Additionally, you will learn how to access the SAM CPU-based peripherals using the Arm® Cortex® Microcontroller Software Interface Standard (CMSIS) core APIs, which are also included as part of the standard SAM MCU compiler toolchain in Atmel® Studio.


For standard MCU peripherals, such as UART, SPI, ADC etc, the compiler toolchain contains specially developed Device-Specific, Instance and Component header files that simplify coding for direct and indirect access of the registers and bitfields in these hardware modules.

For CPU-based peripherals, such as the Nested Vectored Interrupt Controller (NVIC) or the System Timer, APIs from the Cortex CMSIS are used for configuration. The APIs are found in the following header files:

  • core_cm0.h - contains processor register definitions
  • core_cm0plus.h - contains definitions for interrupt controller, system tick timer, and others
  • core_cmFunc.h - contains access functions for access to core registers (PSP, MSP, etc…).

The header files mentioned above will appear in your projects' Dependencies folder in Atmel Studio upon a successful project build:


Use of C Pointers for Hardware Mapped Registers

Pointers are a critical facility provided by the C language that makes it one of the most preferred languages in embedded programming. Technically, C is known as a middle-level language since it provides many of the higher language control structures but also allows simple and direct control of the hardware. It does this through the concept of the hardware pointer. Basic control of not just the core process but of all the peripheral blocks in a microcontroller is presented in sets of registers that are realized as special memory locations mapped directly into the address space of the processor being used.

If you want or need a refresher on pointers, structures, bit-fields and unions, check out the Fundamentals of C-Programming self-paced learning tutorial.

Direct Peripheral Register Access

The PORT Group 0 GPIO controller for the SAM D21 microcontroller is located at address 0x41004400. Starting from this memory address there are registers that are specifically designed to control the PORT Group 0 functions. The following extract from the SAM D21 datasheet documents the first three registers of the PORT Group 0 peripheral:


For reasons partially related to the fact that the SAMD21 is a 32-bit microcontroller, many of the registers in this peripheral are 32-bits wide. Thus the first register in the set, DIR, spans the address range 0x41004400 - 0x41004403. The SAM D21 is a little endian processor, so the LSB is located at address 0x41004400.

To define a pointer to this specific memory address we can do the following:

PORT0_DIR_ptr = (unsigned int*)(0x41004400);

A value can now be written or read from this memory location:
// read a value from the PORT0_DIR register
port0_value = *PORT0_DIR_ptr;

// write Bit 23, Bit 13 and Bit 4 as 1 in the PORT0_DIR
*PORT_DIR_ptr = (1 << 23) | (1 << 13) | (1 <<4);

The entire peripheral register set can now be so mapped in the header files using symbolic names for the addresses to make the code more readable:
#define REG_PORT_DIR0 (0x41004400U) /* (PORT) Data Direction Register 0 */
#define REG_PORT_DIRCLR0 (0x41004404U) /* (PORT) Data Direction Clear Register 0 */
#define REG_PORT_DIRSET0 (0x41004408U) /* (PORT) Data Direction Set Register 0 */
#define REG_PORT_DIRTGL0 (0x4100440CU) /* (PORT) Data Direction Toggle Register 0 */
<and so on>

The following header file type is used to define SAM D21 peripheral registers in this way.

Instance Header Files

These header files are used to associate register name identifiers to SAM D21 memory addresses to allow convenient program access.

Here is an example snippet from a ports.h SAM D21 instance header file:

  • RwReg signifies a read and write 32-bit register.
  • RoReg signifies a read-only 32-bit register.
  • WoReg signifies a write-only 32-bit register.

Suffixes may be added to indicate a smaller register size, i.e.:

  • RwReg8 signifies a read and write 8-bit register.
  • RwReg16 signifies a read and write 16-bit register.


Using a port register macro from the ports.h instance header file, the following code example sets the PORT group 0, bit 30 pin high (port pin PA30):

REG_PORT_OUTSET0 = (1 << 30);

Or using the available pin mask macros in the samd21j18a.h pio header file, the same code can be written as:

Indirect Peripheral Register/Bit-Field Access

There is another more elegant way of defining register sets that encapsulates the definition of the registers as a more complete object. A structure can be defined that clones the structure of the register set and then the head of the structure can then be assigned the starting (or base) address of the peripheral set. Since microcontrollers generally have more than one of a specific peripheral available, this allows very easy access to multiple peripherals sets with only the change in the starting address for each instance that is created.

Device-Specific Header Files

The device-specific header file (i.e. samd21j18a.h) contains the base addresses for all peripherals and components. The base address is used as a pointer to each set of peripheral and component register groups.

The following excerpt shows the base address definition of the Generic Clock Controller Module (GCLK) in the ATSAMD21J18A MCU:


A port IO (pio) header file with the same name (samd21j18a.h, located in a different directory) contains useful port pin macro definitions:

#define PIN_PA30                          30  /**< \brief Pin Number for PA30 */
#define PORT_PA30              (_UL(1) << 30) /**< \brief PORT Mask  for PA30 */

Component Header Files

Component header files are used to define the structure of a register set associated with a peripheral. For example the GCLK register set is defined by the following structure type in the gclk.h component header file:


Individual peripheral register types and bitfields are also defined using unions and structures of members. The following union type defines register and bit-field level access for the the GCLK CLKCTRL register:


There are now several ways to access the CLKCTRL register using these definitions, both indirectly as well as directly:

// Enable GCLK for TCC0 (timer counter input clock) on Clock Generator 0
// Enabled GCLK for TC3 on clock generator 0 and enable the clock to TC3

The macros GCLK_CLKCTRL_CLKEN etc., are also defined in the component header file:

CPU Core Register Access (CMSIS)

The CMSIS provides standardized access functions/APIs for accessing the processor's internal peripherals (i.e. NVIC, System Control Block (SCB) and SystemTick timer (SysTick)) for interrupt control and SysTick initialization. The following example uses CMSIS-Core NVIC APIs to enable the SERCOM3 interrupt and set its priority:

#include "sam.h"

void main(void){
// NVIC setup by CMSIS-Core functions
NVIC_SetPriority(SERCOM3_IRQn, 0x0) ;  /* set Priority */
void SERCOM3_Handler(void){

  • Interrupt numbers (i.e. SERCOM3_IRQn) are defined in the device-specific header file samd21j18a.h
  • Peripheral & System interrupt handler names (i.e. SERCOM3_Handler, SysTick_Handler) are also defined in the device-specific header file samd21j18a.h
© 2018 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.