SAMA5D29 Curiosity – MPLAB® Harmony-Based Complementary PWM Application with Dead-Time
Introduction
In this training, you will explore the process of configuring, building, and debugging a complementary Pulse-Width Modulation (PWM) application on the SAMA5D29 Curiosity development board (P/N: EV07R15A) using MPLAB® X Integrated Development Environment (IDE). Complementary PWM is essential in applications such as half-bridge converter, motor control, power inverters, and DC-DC converters, where precise control of switching devices is required to ensure efficient operation and minimize power losses. Dead-time is a critical feature in complementary PWM, as it prevents simultaneous conduction of high-side and low-side switches, which could lead to short circuits and damage to the hardware.
This training also covers two key PWM Push-Pull modes, Left-aligned mode and Center-aligned mode. In Left aligned mode, the PWM signal starts at the beginning of the period and adjusts its duty cycle accordingly, making it suitable for simpler timing requirements. In contrast, center-aligned mode generates a symmetric waveform by aligning the PWM signal around the center of the period, which helps reduce harmonics and is often preferred in motor control applications.
You will learn how to configure these modes, implement dead-time, build a PWM application, and debug it using the MPLAB Harmony v3 Software Framework. This hands-on training provides a comprehensive understanding of the importance of complementary PWM, its practical applications, and the process of developing and debugging PWM applications for the SAMA5D29 Curiosity development board.
Prerequisites
- Install MPLAB X IDE
- Install MPLAB XC32/32++ Compiler
Hardware
For this training, you will use the SAMA5D29 Curiosity development board.
SAMA5D29 Curiosity Development Board
- See the "SAMA5D29-Curiosity – Features" page.
- Set up the SAMA5D29 Curiosity development board to the host computer running MPLAB X IDE as listed below.
Jumpers
- Open Jumper J10 to disable QSPI boot.
- Refer to Section 2.6 of the " SAMA5D29-Curiosity User Guide" for more details about jumper settings.
SD Memory Cards
- Ensure no SD memory cards are plugged into the SD card (J6) connector.
Power
- Apply power as shown in the "Power Options" section of the "SAMA5D29-Curiosity – Features" page.
Debug Communications
- The SAMA5D29 Curiosity development board contains a 20-pin JTAG debug port. Connect a MPLAB PICkit™ 5 in-circuit debugger to the 20-pin JTAG debug port (J27) and the host computer running MPLAB X IDE.
Console Serial Communications
- To view the target console, connect the host computer running a terminal emulation program in accordance with “SAMA5D29-Curiosity – Console Serial Communications”.
Others
- Three male-to-male jumper wires
- Oscilloscope with two probes
Hardware Modification
To generate a complementary PWM signal, we need both the PWMH and PWML pins of the same PWM channel.
In this example, we will generate the PWM signal using Channel 1. However, some modifications are required on the SAMA5D29 Curiosity development board to enable this configuration:
- Channel 1 PWMH1 is mapped to the mikroBUS2 PWM pin.
- Channel 1 PWML1 is mapped to the mikroBUS2 INT pin.
These mappings ensure proper functionality for generating the complementary PWM signal.
The mikroBUS2 INT pin is pulled up with a 100 kΩ resistor. To enable proper functionality, remove the R187 resistor located on the bottom side of the SAMA5D29 Curiosity development board.
Hardware Setup
Power up the board by connecting the USB Type-C® cable to the USB_A port J1 on the SAMA5D29 Curiosity development board.
Connect the MPLAB PICkit 5 debugger to the 20-pin JTAG debug port (J27).
Connect FTDI cable to the Serial Debug Port (J28) on the SAMA5D29 Curiosity development board.
Connect one end of the male-to-male jumper wires to PWMH1, PWML1, and GND pins on the mikroBUS™ 2 connector.
Connect the other end of the male-to-male jumper wires to the oscilloscope.
The complete setup is shown in the accompanying image.
Developing a PWM Demo Application
Create an application project and add peripheral blocks in the project graph.
Open MPLAB X IDE from the main menu.
Create a new project by clicking the New Project icon or by selecting File > New Project.
In the New Project window, under Projects, choose Application Project(s) and click Next.
In the Select Device dialog window, fill in or select the following information:
- Family: 32-bit MCUs (PIC32C/SAM)
- Device: SAMA2D29
In the Select Compiler window, for Compiler Toolchains select XC32, and click Next.
Enter Project Location, Project Folder, and Project Name. Click Finish.
This creates an empty project and set this project as the main project. If there are other projects open in the project explorer window, set this project as the main project by right clicking on the project, choose Set as Main Project.
After the project is created, MPLAB Code Configurator (MCC) will be automatically launched. (To launch MCC manually from main menu, click on Tools > Embedded > MPLAB Code Configurator or click the MCC button in the MPLAB X IDE toolbar.) It will launch Content Manger Wizard. Then click Select MPLAB Harmony.
In addition to the required packages (csp, dev_packs), download the optional packages bsp and then click Finish. The content download will take some time. Wait for all the content to download.
A project graph will be displayed. From Device Resource add Board Support Packages for SAM A5D29 Curiosity BSP to the Project Graph.
From Device Resource, Peripherals > UART, add UART0 to the Project Graph. This helps to print the debug messages (console serial communication messages) via comm port.
In the Project Graph, right click on UART in UART0 and then add Consumers > STDIO.
From the Project Graph window, click System and in the Configuration Options pane, select Clock (PMC) > Generic Clock Menu > Generic Clock for Peripheral UART0. Check the Enable Generic Clock box. Select MCK_CLK for Generic Clock Source Selection.
From the Project Graph window, click the UART0 and in the Configuration Options pane, apply the following settings:
- Select Clock Source: MCK
- Baud Rate: 115,200
From Device Resources, expand Peripherals > PWM, and add PWM to the Project Graph.
PWM Configuration in Project graph
This demo demonstrates the basic configuration of Left-aligned and Center-aligned PWM modes. You can select the PWM mode that best suits your application's requirements.
Left-Aligned Mode Configuration
From the Project Graph window, click the PWM and in the Configuration Options > PWM > Channel Configuration > Channel 1.
- Enable: Channel 1
- Select Alignment: Left-aligned
- Select Channel Clock: Peripheral clock/2.
- Output Polarity: Waveform starts at low level
- Period Value: 41,500 for 1 KHz PWM Frequency
- Duty Value: 7,000 for ~17 percent duty cycle
- Dead Time for low-Side Output: 100
- Dead Time for high-Side Output: 100
- Not enabled: Enable Fault
- Not enabled: Enable Counter Period Interrupt
Where;
Peripheral clock = 166 MHz.
Prescaler value = 2 (peripheral clock/2).
Desired PWM Frequency = 1 KHz.
Center-Aligned Mode Configuration
From the Project Graph window, click the PWM and in the Configuration Options > PWM > Channel Configuration > Channel 1.
- Enable: Channel 1
- Select Channel Clock: Peripheral clock/2
- Select Alignment: Center-aligned
- Select Duty-Cycle Update Trigger: At the next end of PWM period
- Select Counter Event Occurrence: At the end of PWM period
- Output Polarity: Waveform starts at low level
- Period Value: 20,750 for 1 KHz PWM Frequency
- Duty Value: 7,000 for ~17 percent duty cycle
- Dead Time for low-Side Output: 100
- Dead time for high-Side Output: 100
- Not enabled: Enable Fault
- Not enabled: Enable Counter Period Interrupt
Where;
Peripheral clock = 166 MHz.
Prescaler value = 2 (peripheral clock/2).
Desired PWM Frequency = 1 KHz.
Add Pin Configuration and Generate the Code for the Added Peripherals
From the Project Graph window, select the Pin Configuration from the Plugins drop-down. The Pin Setting window will open.
- Ensure all the pins are configured as follows:
Pin ID Function PA7 LED_AH PA8 LED_AH PA9 LED_AH PB26 UART0_URXD0 PC27 UART0_UTXD0 PB0 PWM_PWMH1 PB1
PWM_PWML1
Save all and then click the Generate button. This will generate code for all the device resource that have been added in the project graph.
PWM Application Code
Functions to be called from PWM application as follows:
PWM_ChannelPeriodGet() : Read the period.
PWM_ChannelsStart() : Start all synchronous channels by starting channel 1.
PWM_ChannelDutySet() : To set the PWM duty cycle .
PWM_ChannelDeadTimeSet() : To set the PWM dead-time.Refer to the main.c for demo code.
- PWM demo application is now ready.
Debugging the PWM Application on the Target
To debug the project on MPLAB, make sure the board is powered, and the external debugger is connected. Refer to the Hardware setup section in this document for more details.
Right click on the project and click Properties. For Compiler Toolchain, select XC32. Under Packs, expand SAMA5D2_DFP and select 1.12.233 or the latest and click Apply.
Configure the debug interface of PICKit 5 as shown in the accompanying:
By default, MPLAB X IDE only generates ELF and HEX format output files. To generate application output in binary format, a post build step needs to be added to the project properties. To do this, right click on project and select Properties, select Building, click the Execute this line after build check box, then enter the following command:
${MP_CC_DIR}/xc32-objcopy -O binary ${DISTDIR}/${PROJECTNAME}.${IMAGE_TYPE}.elf ${DISTDIR}/harmony.bin
In the Categories pane, select Bootstrap.
Ensure that Use bootstrap is enabled and the pre-complied at91bootstrap.elf is selected.
While in Debug mode, the second-stage bootloader, at91bootstrap, is used by MPLAB X IDE to initialize DRAM so that it can continue loading the MPLAB Harmony v3 project binary (harmony.bin). Once the project has been loaded onto the SAMA5D29 Curiosity development board, MPLAB X IDE can be used to run and debug the application.
Clean and build the project. You should see a message on the output console that the project was successfully built. This completes the building of the MPLAB Harmony application.
The harmony.bin binaries will be available at ./project_directory/sama5d29_curiosity.X/dist/default/production.
Now start debugging the project by step in, step out, halt or run.
Now observe the prints in Tera Term or any serial terminal software and oscilloscope. Enter the Duty-cycle and Dead-Time in serial terminal software and press Enter.
Running the Pre-built MPLAB Harmony Application From the SD Card
The pre-built application BIN file can be programmed by following these steps.
Steps to Program the BIN File on an SD Card
Take a microSD card formatted with FAT32 file system. See the "How to Format and Partition an SD Card Larger Than 32GB Using DiskPart in Windows® for MPU" page.
Copy the boot.bin files from this location to the microSD card using your PC.
Copy harmony.bin files (available in the ./project_directory/sama5d29_curiosity.X/dist/default/production) to the microSD card using your PC.
Insert the SD card to J6 on the SAMA5D29 Curiosity development board.
Steps to Run the BIN File From an SD Card
Press the reset button.
It will display the automotive dashboard graphics application on the display connected with SAMA5D29 Curiosity development board.
Now observe the prints in Tera Term or any serial terminal software and oscilloscope. Enter the Duty-cycle and Dead-Time in serial terminal software and press Enter.
Summary
In this training, you learned how to perform hardware modifications and set up. You also learned how to configure various complementary PWM modes with dead-time, build a PWM application, and debug it using the MPLAB Harmony v3 Software Framework within the MPLAB X IDE. The project was specifically developed for the SAMA5D29 Curiosity development board.
Learn More
- SAMA5D29-Curiosity – Features
- SAMA5D29-Curiosity – Console Serial Communications
- SAMA5D29-Curiosity – Booting Demo Linux® Image
- SAMA5D29-Curiosity – Read and Write Boot Configuration Word using SAM-BA® ISP
- Developing with Embedded Linux
- SAMA5D29-Curiosity – Configure and Build at91bootstrap for a MPLAB Harmony v3 Project with MPLAB X IDE
- Developing Applications with MPLAB Harmony v3 Software Framework for MPUs
- SAMA5D29-Curiosity – Configure and Build at91bootstrap to Load a MPLAB Harmony v3 Application from SD Memory Card
- SAMA5D29-Curiosity – Copy MPLAB Harmony v3 Application and at91bootstrap to SD Memory Card
- SAMA5D29-Curiosity – Configure and Build at91bootstrap to Load a MPLAB Harmony v3 Application from SQI™ Flash Memory
- SAMA5D29-Curiosity – Writing MPLAB Harmony v3 Application and at91bootstrap to SQI Flash Memory Using SAM-BA ISP
- SAMA5D29-Curiosity – Getting Started with MPLAB Harmony v3 Development: CSP Application: pio_led_on_off_interrupt