Low-Power Application on SAM D21 Using MPLAB® Harmony v3 Peripheral Libraries: Step 2

Last modified by Microchip on 2024/09/13 14:11

Configure Real-Time Clock (RTC) Peripheral Library

Under the bottom left Device Resources tab, expand Harmony > Peripherals > RTC.

Double-click or drag and drop RTC to add the RTC Peripheral Library (PLIB) to the project graph.

Adding RTC

Figure 1

 

In the Clock Easy View, open the Peripheral Clock Configuration window by clicking on the Peripheral Clock Configuration button.

Peripheral Clock Configuration window

Figure 2

Once the window is opened, scroll down to the RTC peripheral and select GCLK1 (1 kHz) as the source clock to generate the peripheral clock frequency.

Scrolling down to the RTC peripheral and selecting GCLK1 (1 kHz) as the source clock

Figure 3

When a peripheral is added to the project, the peripheral clock is automatically fed by the GCLK0. However, you must configure the peripheral clocks according to your needs (power consumption, performance, etc.).

Go back to the Project Graph, select the RTC module, and in Configuration Options, configure the RTC PLIB to generate a compare interrupt every 500 milliseconds.

RTC configuration setup

Figure 4

 The Compare Value is set as 0x200. This compare value generates an RTC compare interrupt every 500 milliseconds.

  • RTC clock = 1024 Hz
  • RTC Prescaler = 1
  • Required Interrupt rate = 500 ms

Hence, Compare Value = (500/1000) x 1024 = 512 (i.e., 0x200).

Back to Top


Configure Inter-Integrated Circuit (I²C) Peripheral Library, I²C Pins, and Verify I²C Clock

Under the Device Resources tab, expand Harmony > Peripherals > SERCOM.

Expand SERCOM and double-click on SERCOM2 to add the SERCOM instance 2 to the project.

SERCOM selection

Figure 5

Select the SERCOM 2 Peripheral Library and configure it for the I²C protocol.

SERCOM setup

Figure 6

  • SERCOM2 (as I²C) retains the default 100 kHz speed because the temperature sensor chip on the I/O1 Xplained Pro Extension Kit can operate at 100 kHz I²C speed.
  • SERCOM2 (as I²C) retains the default 50-100 nanoseconds hold time for Serial Data (SDA) Hold Time because it aligns with the minimum (50 nanoseconds) start to hold time in the specification of the temperature sensor chip (AT30TSE758).
  • SERCOM2 (as I²C) retains the default 100 nanoseconds for I²C Trise time because it aligns with the maximum (300 nanoseconds) input rise time in the specification of the temperature sensor chip (AT30TSE758).

Open the Pin Configuration tabs by clicking Project Graph > Plugins > Pin Configuration.

Open pin configuration

Figure 7

 

Select the Pin Settings tab and sort the entries by Ports, as shown in Figure 8.

SERCOM pins setup

Figure 8

Now, In the Pin Settings tab scroll down to the PA08 and PA09 pins as follows.

  • Enable I²C Clock (TWI_SCL)(SERCOM2_PAD1) on PA09 (Pin #18)
  • Enable I²C Data (TWI_SDA)(SERCOM2_PAD0) on PA08 (Pin #17)
SERCOM pins setup

Figure 9

In Clock Easy View, open the Peripheral Clock Configuration by clicking on the button Peripheral Clock Configuration.

Opening the Peripheral Clock Configuration

Figure 10

Once the window is opened, scroll down to the SERCOM2_CORE peripheral and verify GCLK0 (48 MHz) as the source clock to generate the peripheral clock frequency.

Scrolling down to the SERCOM2_CORE peripheral

Figure 11

This completes the configuration of the I²C peripheral library. The application code will use the I²C PLIB Application Programming Interfaces (APIs) to read the temperature from the temperature sensor. 

Back to Top


Configure Universal Synchronous Asynchronous Receiver Transmitter (USART) Peripheral Library, USART Pins, and Verify USART Clock

Under the Device Resources tab, expand Harmony > Peripherals > SERCOM.

Expand SERCOM and double-click on SERCOM3 to add the SERCOM instance 3 to the project.

SERCOM selection for UART

Figure 12

Associate the STDIO tool with SERCOM instance 3 (USART) peripheral by right-clicking on the yellow diamond.

Right-clicking on the yellow diamond

Figure 13

Right-clicking on the yellow diamond

Figure 14

In Clock Easy View, open the Peripheral Clock Configuration by clicking on the Peripheral Clock Configuration button.

Opening the Peripheral Clock Configuration

Figure 15

Once the window is opened, scroll down to the SERCOM3_CORE peripheral and set GCLK2 (32 kHz) as the source clock to generate the peripheral clock frequency.

Scrolling down to the SERCOM3_CORE peripheral and setting GCLK2 (32 kHz) as the source clock

Figure 16

Note: The default main clock source will take time to settle once after wakeup from Standby Sleep mode. So, the USART clock source is changed to GLCK2, as GCLK2 sourcing from the XOSC32K oscillator and this XOSC32K will run even in Standby Sleep mode.

Select the SERCOM3 Peripheral Library in the Project Graph, verify the default SERCOM Operation Mode configuration is set as USART, and configure it.

UART SERCOM setup

Figure 17

 The default Baud rate is set to 4800 Hz, as the peripheral is set at a low-frequency clock of 32 kHz.

Select Pin Settings tab and scroll down to the PA22 and PA23 pins as follows.

  • Enable USART_TX (SERCOM3_PAD0) on PA22 (Pin #43)
  • Enable USART_RX (SERCOM3_PAD1) on PA23 (Pin #44)
Selecting the Pin Table tab

Figure 18

Back to Top