Multiple Definitions
Linker Error From v2.0.2, h-only
style is used for this library.
Please have a look at HOWTO Fix Multiple Definitions
Linker Error
This library enables you to use Hardware Timers on an STM32H7-based Portenta_H7 board to create and output PWM to pins.
This library is using the same or similar functions as other FastPWM libraries, as follows, to enable you to port your PWM code easily between platforms
The most important feature is they're purely hardware-based PWM channels. Therefore, their executions are not blocked by bad-behaving functions / tasks. This important feature is absolutely necessary for mission-critical tasks.
This important feature is absolutely necessary for mission-critical tasks. These hardware timers, using interrupt, still work even if other functions are blocking. Moreover, they are much more precise (certainly depending on clock frequency accuracy) than other software timers using millis()
or micros()
. That's necessary if you need to measure some data requiring better accuracy.
The PWM_Multi will demonstrate the usage of multichannel PWM using multiple Hardware Timers. The 4 independent Hardware Timers are used to control 4 different PWM outputs, with totally independent frequencies and dutycycles. You can start, stop, change and restore the settings of any PWM channel on-the-fly.
Being hardware-based PWM, their executions are not blocked by bad-behaving functions / tasks, such as connecting to WiFi, Internet or Blynk services.
This non-being-blocked important feature is absolutely necessary for mission-critical tasks.
Imagine you have a system with a mission-critical function, measuring water level and control the sump pump or doing something much more important. You normally use a software timer to poll, or even place the function in loop(). But what if another function is blocking the loop() or setup().
So your function might not be executed, and the result would be disastrous.
You'd prefer to have your function called, no matter what happening with other functions (busy loop, bug, etc.).
The correct choice is to use a Hardware Timer with Interrupt to call your function.
These hardware timers, using interrupt, still work even if other functions are blocking. Moreover, they are much more precise (certainly depending on clock frequency accuracy) than other software timers using millis()
or micros()
. That's necessary if you need to measure some data requiring better accuracy.
Functions using normal software timers, relying on loop()
and calling millis()
, won't work if the loop()
or setup()
is blocked by certain operation. For example, certain function is blocking while it's connecting to WiFi or some services.
The catch is your function is now part of an ISR (Interrupt Service Routine), and must be lean / mean, and follow certain rules. More to read on:
Arduino IDE 1.8.19+
for Arduino. ArduinoCore-mbed mbed_portenta core 3.5.4+
for Arduino Portenta_H7 boards, such as Portenta_H7 Rev2 ABX00042, etc.. The best and easiest way is to use Arduino Library Manager
. Search for Portenta_H7_PWM, then select / install the latest version.
You can also use this link for more detailed instructions.
Another way to install is to:
Portenta_H7_PWM-main.zip
.Portenta_H7_PWM-main
directory Portenta_H7_PWM-main
folder to Arduino libraries' directory such as ~/Arduino/libraries/
.Multiple Definitions
Linker ErrorThe current library implementation, using xyz-Impl.h
instead of standard xyz.cpp
, possibly creates certain Multiple Definitions
Linker error in certain use cases.
You can include this .hpp
file
// Can be included as many times as necessary, without `Multiple Definitions` Linker Error
#include "Portenta_H7_PWM.hpp" //https://github.com/khoih-prog/Portenta_H7_PWM
in many files. But be sure to use the following .h
file in just 1 .h
, .cpp
or .ino
file, which must not be included in any other file, to avoid Multiple Definitions
Linker Error
// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error
#include "Portenta_H7_PWM.h" //https://github.com/khoih-prog/Portenta_H7_PWM
Check the new multiFileProject example for a HOWTO
demo.
To be able to upload firmware to Portenta_H7 using Arduino IDE in Linux (Ubuntu, etc.), you have to copy the file portenta_post_install.sh into mbed_portenta directory (~/.arduino15/packages/arduino/hardware/mbed_portenta/3.4.1/portenta_post_install.sh).
Then run the following command using sudo
$ cd ~/.arduino15/packages/arduino/hardware/mbed_portenta/3.4.1
$ chmod 755 portenta_post_install.sh
$ sudo ./portenta_post_install.sh
This will create the file /etc/udev/rules.d/49-portenta_h7.rules
as follows:
# Portenta H7 bootloader mode UDEV rules
SUBSYSTEMS=="usb", ATTRS{idVendor}=="2341", ATTRS{idProduct}=="035b", GROUP="plugdev", MODE="0666"
Supposing the ArduinoCore-mbed core version is 3.4.1. Now only one file must be copied into the directory:
~/.arduino15/packages/arduino/hardware/mbed_portenta/3.4.1/portenta_post_install.sh
Whenever a new version is installed, remember to copy this files into the new version directory. For example, new version is x.yy.zz
This file must be copied into the directory:
~/.arduino15/packages/arduino/hardware/mbed_portenta/x.yy.zz/portenta_post_install.sh
The Timers of STM32s are numerous, yet very sophisticated and powerful.
In general, across the STM32 microcontrollers families, the timer peripherals that have the same name also have the same features set, but there are a few exceptions.
The general purpose timers embedded by the STM32 microcontrollers share the same backbone structure; they differ only on the level of features embedded by a given timer peripheral.
The level of features integration for a given timer peripheral is decided based on the applications field that it targets.
The timer peripherals can be classified as:
• Advanced-configuration timers like TIM1 and TIM8 among others. • General-purpose configuration timers like TIM2 and TIM3 among others • Lite-configuration timers like TIM9, TIM10, TIM12 and TIM16 among others • Basic-configuration timers like TIM6 and TIM7 among others.
More information can be found at Embedded-Lab STM32 TIMERS
To be sure which Timer is available for the board you're using, check the Core Package's related files. For example, for Portenta_H7 using STM32H747XI, check this file:
~/.arduino15/packages/STM32/hardware/stm32/2.0.0/system/Drivers/CMSIS/Device/ST/STM32H7xx/Include/stm32h7xx.h
The information will be as follows:
typedef struct
{
__IO uint32_t CR1; /*!< TIM control register 1, Address offset: 0x00 */
__IO uint32_t CR2; /*!< TIM control register 2, Address offset: 0x04 */
__IO uint32_t SMCR; /*!< TIM slave mode control register, Address offset: 0x08 */
__IO uint32_t DIER; /*!< TIM DMA/interrupt enable register, Address offset: 0x0C */
__IO uint32_t SR; /*!< TIM status register, Address offset: 0x10 */
__IO uint32_t EGR; /*!< TIM event generation register, Address offset: 0x14 */
__IO uint32_t CCMR1; /*!< TIM capture/compare mode register 1, Address offset: 0x18 */
__IO uint32_t CCMR2; /*!< TIM capture/compare mode register 2, Address offset: 0x1C */
__IO uint32_t CCER; /*!< TIM capture/compare enable register, Address offset: 0x20 */
__IO uint32_t CNT; /*!< TIM counter register, Address offset: 0x24 */
__IO uint32_t PSC; /*!< TIM prescaler, Address offset: 0x28 */
__IO uint32_t ARR; /*!< TIM auto-reload register, Address offset: 0x2C */
__IO uint32_t RCR; /*!< TIM repetition counter register, Address offset: 0x30 */
__IO uint32_t CCR1; /*!< TIM capture/compare register 1, Address offset: 0x34 */
__IO uint32_t CCR2; /*!< TIM capture/compare register 2, Address offset: 0x38 */
__IO uint32_t CCR3; /*!< TIM capture/compare register 3, Address offset: 0x3C */
__IO uint32_t CCR4; /*!< TIM capture/compare register 4, Address offset: 0x40 */
__IO uint32_t BDTR; /*!< TIM break and dead-time register, Address offset: 0x44 */
__IO uint32_t DCR; /*!< TIM DMA control register, Address offset: 0x48 */
__IO uint32_t DMAR; /*!< TIM DMA address for full transfer, Address offset: 0x4C */
uint32_t RESERVED1; /*!< Reserved, 0x50 */
__IO uint32_t CCMR3; /*!< TIM capture/compare mode register 3, Address offset: 0x54 */
__IO uint32_t CCR5; /*!< TIM capture/compare register5, Address offset: 0x58 */
__IO uint32_t CCR6; /*!< TIM capture/compare register6, Address offset: 0x5C */
__IO uint32_t AF1; /*!< TIM alternate function option register 1, Address offset: 0x60 */
__IO uint32_t AF2; /*!< TIM alternate function option register 2, Address offset: 0x64 */
__IO uint32_t TISEL; /*!< TIM Input Selection register, Address offset: 0x68 */
} TIM_TypeDef;
and
#define PERIPH_BASE 0x40000000UL /*!< Base address of : AHB/ABP Peripherals
/*!< Peripheral memory map */
#define APB1PERIPH_BASE PERIPH_BASE
/*!< APB1 peripherals */
/*!< D2_APB1PERIPH peripherals */
#define TIM2_BASE (D2_APB1PERIPH_BASE + 0x0000UL)
#define TIM3_BASE (D2_APB1PERIPH_BASE + 0x0400UL)
#define TIM4_BASE (D2_APB1PERIPH_BASE + 0x0800UL)
#define TIM5_BASE (D2_APB1PERIPH_BASE + 0x0C00UL)
#define TIM6_BASE (D2_APB1PERIPH_BASE + 0x1000UL)
#define TIM7_BASE (D2_APB1PERIPH_BASE + 0x1400UL)
#define TIM12_BASE (D2_APB1PERIPH_BASE + 0x1800UL)
#define TIM13_BASE (D2_APB1PERIPH_BASE + 0x1C00UL)
#define TIM14_BASE (D2_APB1PERIPH_BASE + 0x2000UL)
/*!< APB2 peripherals */
#define TIM1_BASE (D2_APB2PERIPH_BASE + 0x0000UL)
#define TIM8_BASE (D2_APB2PERIPH_BASE + 0x0400UL)
...
#define TIM9_BASE (APB2PERIPH_BASE + 0x4000UL)
#define TIM10_BASE (APB2PERIPH_BASE + 0x4400UL)
#define TIM11_BASE (APB2PERIPH_BASE + 0x4800UL)
...
#define TI15_BASE (D2_APB2PERIPH_BASE + 0x4000UL)
#define TIM16_BASE (D2_APB2PERIPH_BASE + 0x4400UL)
#define TIM17_BASE (D2_APB2PERIPH_BASE + 0x4800UL)
...
#define HRTIM1_BASE (D2_APB2PERIPH_BASE + 0x7400UL)
#define HRTIM1_TIMA_BASE (HRTIM1_BASE + 0x00000080UL)
#define HRTIM1_TIMB_BASE (HRTIM1_BASE + 0x00000100UL)
#define HRTIM1_TIMC_BASE (HRTIM1_BASE + 0x00000180UL)
#define HRTIM1_TIMD_BASE (HRTIM1_BASE + 0x00000200UL)
#define HRTIM1_TIME_BASE (HRTIM1_BASE + 0x00000280UL)
#define HRTIM1_COMMON_BASE (HRTIM1_BASE + 0x00000380UL)
...
#define TIM2 ((TIM_TypeDef *) TIM2_BASE)
#define TIM3 ((TIM_TypeDef *) TIM3_BASE)
#define TIM4 ((TIM_TypeDef *) TIM4_BASE)
#define TIM5 ((TIM_TypeDef *) TIM5_BASE)
#define TIM6 ((TIM_TypeDef *) TIM6_BASE)
#define TIM7 ((TIM_TypeDef *) TIM7_BASE)
#define TIM13 ((TIM_TypeDef *) TIM13_BASE)
#define TIM14 ((TIM_TypeDef *) TIM14_BASE)
...
#define TIM1 ((TIM_TypeDef *) TIM1_BASE)
#define TIM8 ((TIM_TypeDef *) TIM8_BASE)
...
#define TIM12 ((TIM_TypeDef *) TIM12_BASE)
#define TIM15 ((TIM_TypeDef *) TIM15_BASE)
#define TIM16 ((TIM_TypeDef *) TIM16_BASE)
#define TIM17 ((TIM_TypeDef *) TIM17_BASE)
...
#define HRTIM1 ((HRTIM_TypeDef *) HRTIM1_BASE)
#define HRTIM1_TIMA ((HRTIM_Timerx_TypeDef *) HRTIM1_TIMA_BASE)
#define HRTIM1_TIMB ((HRTIM_Timerx_TypeDef *) HRTIM1_TIMB_BASE)
#define HRTIM1_TIMC ((HRTIM_Timerx_TypeDef *) HRTIM1_TIMC_BASE)
#define HRTIM1_TIMD ((HRTIM_Timerx_TypeDef *) HRTIM1_TIMD_BASE)
#define HRTIM1_TIME ((HRTIM_Timerx_TypeDef *) HRTIM1_TIME_BASE)
#define HRTIM1_COMMON ((HRTIM_Common_TypeDef *) HRTIM1_COMMON_BASE)
This is the temporary list for Portenta_H7 Timers which can be used. The available Timers certainly depends on they are being used for other purpose (core, application, libraries, etc.) or not. You have to exhausively test yourself to be sure.
TIM1, TIM4, TIM7, TIM8, TIM12, TIM13, TIM14, TIM15, TIM16, TIM17
TIM9, TIM10, TIM11. Only for STM32F2, STM32F4 or STM32L1
TIM18, TIM19, TIM20, TIM21, TIM22
TIM2, TIM3, TIM5, TIM6
To know which Timer (TIMx) is used for which pin, check Table 7. STM32H747xI/G pin/ball definition, page 67
The result for PWM-enable pins can be summarized as follows
#define pinD0 D0 // PH15 / TIM8_CH3N
#define pinD1 D1 // PK1 / TIM1_CH1, TIM8_CH3
#define pinD2 D2 // PJ11 / TIM1_CH2, TIM8_CH2N
#define pinD3 D3 // PG7 / HRTIM_CHE2
#define pinD4 D4 // PC7 / TIM3_CH2, TIM8_CH2, HRTIM_CHA2
#define pinD5 D5 // PC6 / TIM3_CH1, TIM8_CH1, HRTIM_CHA1, LPTIM3_OUT
#define pinD6 D6 // PA8 / HRTIM_CHB2 (TIM1_CH1, TIM8_BKIN2)
Before using any Timer for a PWM channel, you have to make sure the Timer has not been used by any other purpose.
// Can't use same TimerIndex again, e.g., the D1 and D2, using TIM1, can't be use concurrently
// That's why D0, D1, D3, D4 and D6 (using TimerIndex 8, 1, HRTIM and 3) are OK together
// Only OK for D0, D1, D2, D4 and D5, PA_0C(D15/A0), PA_1C(D16/A1),
// D3, D6, D7, D8, D9, D10, D11, D12, D13, D14, D17(PC_2C/A2), D18(PC_3C/3), PC2(D19/A4) LEDG, LEDB not OK
#define pinD0 D0 // PH15 / TIM8_CH3N
#define pinD1 D1 // PK1 / TIM1_CH1, TIM8_CH3
#define pinD2 D2 // PJ11 / TIM1_CH2, TIM8_CH2N
#define pinD3 D3 // PG7 / HRTIM_CHE2
#define pinD4 D4 // PC7 / TIM3_CH2, TIM8_CH2, HRTIM_CHA2
#define pinD5 D5 // PC6 / TIM3_CH1, TIM8_CH1, HRTIM_CHA1, LPTIM3_OUT
#define pinD6 D6 // PA8 / HRTIM_CHB2 (TIM1_CH1, TIM8_BKIN2)
// See https://www.st.com/resource/en/datasheet/stm32h747xi.pdf, Table 7, page 53
// Can't use myPin with same TIMx. For example,
// pinD1 and pinD2, using same TIM1, can't be used at the same time
// pinD4 and pinD5, using same TIM3, can't be used at the same time
// pinD3 and pinD6 are using HRTIM, so the minimum freq must be ~770Hz
uint32_t myPin = pinD5;
float dutyCycle = 50.0f;
float freq = 5000.0f;
mbed::PwmOut* pwm = NULL;
void setup()
{
....
setPWM(pwm, myPin, freq, dutyCycle);
}
Function prototype
mbed::PwmOut* setPWM(mbed::PwmOut* &pwm, const pin_size_t& pin, const float& frequency, const float& dutyCycle);
mbed::PwmOut* setPWM_DCPercentage_manual(mbed::PwmOut* &pwm, const pin_size_t& pin, const float& DCPercentage);
Need to call only once for each pin
mbed::PwmOut* pwm = nullptr;
...
setPWM(pwm, myPin, frequency, 0);
after that, if you wish to change both dutyCycle
and frequency
// For 50.0f dutycycle
new_DCPercentage = 50.0f;
setPWM(pwm, myPin, frequency, dutycyclePercent);
or better and much easier to use to change just dutycycle
while using same frequency
new_DCPercentage = 50.0f;
setPWM_DCPercentage_manual(pwm, myPin, new_DCPercentage);
The following is the sample terminal output when running example PWM_Single on Portenta_H7 to demonstrate how to start a single PWM channel, then stop, change, restore the PWM settings on-the-fly.
Starting PWM_Single on PORTENTA_H7_M7
Portenta_H7_PWM v2.1.0
[PWM] Freq = 5000.00, DutyCycle % = 50.00, DutyCycle = 0.50, Pin = 5
========
PW (us)
========
100.00
Stop PWM
0.00
0.00
Change PWM
25.00
25.00
Restore PWM
100.00
100.00
Stop PWM
0.00
0.00
Change PWM
25.00
25.00
Restore PWM
100.00
100.00
The following is the sample terminal output when running example PWM_Multi on Portenta_H7 to demonstrate how to start multiple PWM channels, then stop, change, restore the PWM settings on-the-fly.
Starting PWM_Multi on PORTENTA_H7_M7
Portenta_H7_PWM v2.1.0
[PWM] Freq = 1000.00, DutyCycle % = 50.00, DutyCycle = 0.50, Pin = 0
[PWM] Freq = 2500.00, DutyCycle % = 50.00, DutyCycle = 0.50, Pin = 1
[PWM] Freq = 4000.00, DutyCycle % = 50.00, DutyCycle = 0.50, Pin = 3
[PWM] Freq = 5000.00, DutyCycle % = 50.00, DutyCycle = 0.50, Pin = 5
==========================================================================================================
PW (us) 0 PW (us) 1 PW (us) 2 PW (us) 3
==========================================================================================================
500.00 200.00 125.00 100.00
Stop all PWM
0.00 0.00 0.00 0.00
0.00 0.00 0.00 0.00
Change all PWM
125.00 50.00 31.25 25.00
125.00 50.00 31.25 25.00
Restore all PWM
500.00 200.00 125.00 100.00
500.00 200.00 125.00 100.00
Stop all PWM
0.00 0.00 0.00 0.00
0.00 0.00 0.00 0.00
Change all PWM
125.00 50.00 31.25 25.00
125.00 50.00 31.25 25.00
Restore all PWM
500.00 200.00 125.00 100.00
500.00 200.00 125.00 100.00
The following is the sample terminal output when running example PWM_manual on Portenta_H7 to demonstrate how to use the setPWM_manual()
and setPWM_DCPercentage_manual()
functions in wafeform creation
Starting PWM_manual on PORTENTA_H7_M7
Portenta_H7_PWM v2.1.0
[PWM] Freq = 1000.00, DutyCycle % = 0.00, DutyCycle = 0.00, Pin = 5
[PWM] New pwm
[PWM] Freq = 1000.00, DutyCycle % = 0.00, DutyCycle = 0.00, Pin = 5
[PWM] Freq = 1000.00, DutyCycle % = 5.00, DutyCycle = 0.05, Pin = 5
[PWM] Freq = 1000.00, DutyCycle % = 10.00, DutyCycle = 0.10, Pin = 5
[PWM] Freq = 1000.00, DutyCycle % = 15.00, DutyCycle = 0.15, Pin = 5
[PWM] Freq = 1000.00, DutyCycle % = 20.00, DutyCycle = 0.20, Pin = 5
[PWM] Freq = 1000.00, DutyCycle % = 25.00, DutyCycle = 0.25, Pin = 5
[PWM] Freq = 1000.00, DutyCycle % = 30.00, DutyCycle = 0.30, Pin = 5
[PWM] Freq = 1000.00, DutyCycle % = 35.00, DutyCycle = 0.35, Pin = 5
[PWM] Freq = 1000.00, DutyCycle % = 40.00, DutyCycle = 0.40, Pin = 5
[PWM] Freq = 1000.00, DutyCycle % = 45.00, DutyCycle = 0.45, Pin = 5
[PWM] Freq = 1000.00, DutyCycle % = 50.00, DutyCycle = 0.50, Pin = 5
[PWM] Freq = 1000.00, DutyCycle % = 55.00, DutyCycle = 0.55, Pin = 5
[PWM] Freq = 1000.00, DutyCycle % = 60.00, DutyCycle = 0.60, Pin = 5
[PWM] Freq = 1000.00, DutyCycle % = 65.00, DutyCycle = 0.65, Pin = 5
[PWM] Freq = 1000.00, DutyCycle % = 70.00, DutyCycle = 0.70, Pin = 5
[PWM] Freq = 1000.00, DutyCycle % = 75.00, DutyCycle = 0.75, Pin = 5
[PWM] Freq = 1000.00, DutyCycle % = 80.00, DutyCycle = 0.80, Pin = 5
[PWM] Freq = 1000.00, DutyCycle % = 85.00, DutyCycle = 0.85, Pin = 5
[PWM] Freq = 1000.00, DutyCycle % = 90.00, DutyCycle = 0.90, Pin = 5
[PWM] Freq = 1000.00, DutyCycle % = 95.00, DutyCycle = 0.95, Pin = 5
[PWM] Freq = 1000.00, DutyCycle % = 100.00, DutyCycle = 1.00, Pin = 5
[PWM] Freq = 1000.00, DutyCycle % = 0.00, DutyCycle = 0.00, Pin = 5
Debug is enabled by default on Serial.
You can also change the debugging level _PWM_LOGLEVEL_
from 0 to 4
// Don't define _PWM_LOGLEVEL_ > 0. Only for special ISR debugging only. Can hang the system.
#define _PWM_LOGLEVEL_ 0
If you get compilation errors, more often than not, you may need to install a newer version of the core for Arduino boards.
Sometimes, the library will only work if you update the board core to the latest version because I am using newly added functions.
Submit issues to: Portenta_H7_PWM issues
reference-passing
instead of value-passing
h-only
stylesetPWM_DCPercentage_manual()
to facilitate the setting PWM DC manually by using DCPercentage
dutycycle
while using same frequency
Many thanks for everyone for bug reporting, new feature suggesting, testing and contributing to the development of this library.
GitChris3004 |
Paul van Dinther |
If you want to contribute to this project:
Copyright (c) 2021- Khoi Hoang