MPLAB® Harmony v3 Peripheral Libraries on SAM D21: Step 2
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.
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).
Go back to the project graph and configure the RTC PLIB to generate a compare interrupt every 500 milliseconds.
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.
Select the SERCOM 2 Peripheral Library and configure it for the I²C protocol as shown.
Open the Pin Configuration tabs by clicking Project Graph > Plugins > Pin Configuration.
Select the MCC Pin Settings tab and sort the entries by Ports, as shown in the following image.
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)
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.
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.
Verify that the default baud rate is set to 115200 Hz.
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).
Configure DMA PLIB
Open the DMA Configurator tab by clicking Plugins > DMA Configuration.
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.