MPLAB® Harmony v3 Peripheral Libraries on SAM D21: Step 2

Last modified by Microchip on 2023/11/09 09:09

Configure I²C, USART and RTC Peripheral Libraries

Configure Real-Time Clock (RTC) Peripheral Library

Click on the Resource Management [MCC] tab, In the Device Resources, expand Peripherals > RTC.
Double-click or drag and drop RTC to add the Real-Time Clock (RTC) Peripheral Library (PLIB) to the project graph.

Resource Management and Project Graph pane

In the clock manager, set the RTC to run at 1000 Hz low-speed clock.

When a module is added to the project graph, MPLAB® Code Configurator (MCC) automatically enables the clock to the module. The default RTC clock source is a 48 MHz Generic Clock Generator 0 (GCLK0).

RTC clock setup

Information

On the SAM D21 device, the RTC can be clocked through several clock sources of 32 kHz to 48 MHz as shown above. The RTC can be sourced through a low-speed clock (OSCULP32K) using Generic Clock Controller (GCLK), clock divisor, and Peripheral Clock Configuration. In the above screenshot, RTC is a sourced 1000 Hz low-speed clock source. The 1000 Hz low-speed clock is enough to generate periods at 500 milliseconds, 1 second, 2 seconds, and 4 seconds.

Go back to the project graph and configure the RTC PLIB to generate a compare interrupt every 500 milliseconds.

Project Graph and Configuration Options tab

Information

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 I²C PLIB and I²C Pins

Under the left tab, Resource Management (MCC), go to Device Resources and expand Libraries > Harmony > Peripherals > SERCOM.
Double-click on SERCOM2 to add the SERCOM instance 3 to the project.

Resource Management and Project Graph pane

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

Configuration Options pane

Information
  • The 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.
  • The 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 hold time stated in the specification of the temperature sensor chip (AT30TSE758).
  • The 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 stated in the specification of the temperature sensor chip (AT30TSE758).

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

Project Graph pane

Select the MCC Pin Settings tab and sort the entries by Ports, as shown in the following image.

Pin Settings tab

Now, select the MCC Pin Table tab and then scroll down to the SERCOM2 module as shown in the following image.

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

Pin Table tab

Information

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

Back to Top


Configure Universal Synchronous Asynchronous Receiver Transmitter (USART) PLIB and USART Pins

Under the left tab, Resource Management (MCC), go to Device Resources and expand Libraries > Harmony > Peripherals > SERCOM.

Double-click on SERCOM3 to add the SERCOM instance 3 to the project.

Resource Management and Project Graph pane

Select the SERCOM3 Peripheral Library in the project graph. Verify that the default SERCOM Operation Mode configuration is set as USART and configure it as shown in the following image.

Configuration Options pane

Verify that the default baud rate is set to 115200 Hz.

Information
  • SERCOM3 (as USART) interrupt is disabled because Direct Memory Access (DMA) will be used (configured in the following steps) to transfer the application buffer to the USART TX register.
  • In MCC, by default, Receive Pinout and Transmit Pinout, both are configured on SERCOM3 PAD0. However, as per the SAM D21 Xplained Pro Evaluation Kit design, SERCOM3 PAD0 is used for SERCOM3 (as USART) data transmit pinout. Even though the SERCOM3 (as USART) receive functionality is unused in this tutorial, the receive pinout is changed to SERCOM3 PAD1 to avoid confusion.
  • The application will use the SERCOM3 (as USART) PLIB for printing messages on the serial terminal. Hence, only the transmit functionality is enabled and the receive functionality is disabled.

Select the Pin Table tab and then scroll down to the SERCOM3 module as shown in the following image.

Enable USART_TX on PA22 (Pin #43).

Pin Table tab

Information

In the SERCOM3 (as USART) configuration, USART is enabled for TX functionality, and no USART Rx functionality is enabled.

Back to Top


Configure DMA PLIB

Open the DMA Configurator tab by clicking Plugins > DMA Configuration.

Project Graph pane

In the DMA Settings window, configure DMA Channel 0 to transfer the application buffer to the USART TX register. The DMA transfers one byte from the user buffer to the USART transmit buffer on each trigger.

Based on the trigger source, the DMA channel configuration is automatically set by MCC.

  • Trigger Action: Action taken by DMA on receiving a trigger.
    • One beat transfer: Generally used during a memory-to-peripheral or peripheral-to-memory transfer.
    • One block transfer: Generally used during the memory-to-memory transfer on a software trigger.
  • Source Address mode, Destination Address mode: Select whether to increment the Source/Destination Address after every transfer. Automatically set by MCC based on the trigger type. For example:
    • If the trigger source is USART transmit, then the Source Address is incremented, and the Destination Address is fixed.
    • If the trigger source is USART receive, then the Source Address is fixed, and the Destination Address is incremented.
  • Beat Size: Size of one beat. The default value is 8 bits. For example:
    • If the Serial Peripheral Interface (SPI) peripheral is configured for 16-bit/32-bit mode, then the beat size must be set to 16-bits/32-bits respectively.

Click on Add Channel to add the DMA channel and configure the DMA channel as shown in the following image.

  • Enable Interrupts: In this tab, SERCOM4 (as USART) Interrupt is enabled.

DMA Configuration window

USART transmit buffer empty event triggers the DMA to transfer one byte of data from the source (user buffer) to the destination (USART Tx register). When all the requested bytes are transmitted, the DMA PLIB notifies the application by calling the registered DMA callback event handler.

Back to Top