- Important Change from v1.2.0
- Why do we need this SAMDUE_Slow_PWM library
- Changelog
- Prerequisites
- Installation
- HOWTO Fix
Multiple Definitions
Linker Error - Usage
- Examples
- Example ISR_8_PWMs_Array_Complex
- Debug Terminal Output Samples
- Debug
- Troubleshooting
- Issues
- TO DO
- DONE
- Contributions and Thanks
- Contributing
- License
- Copyright
Please have a look at HOWTO Fix Multiple Definitions
Linker Error
As more complex calculation and check inside ISR are introduced from v1.2.0, there are consequences as follows
- using min 30uS and max 8 PWM channels for v1.2.0
- using min 20uS and max 8 PWM channels for v1.2.1+
// Don't change these numbers to make higher Timer freq. System can hang
#define HW_TIMER_INTERVAL_US 30L
#define HW_TIMER_INTERVAL_FREQ 50000L
You certainly can modify to use better values according to your board and use-case, just remember to test and reverse to conservative values if crash happens.
Why do we need this SAMDUE_Slow_PWM library
This library enables you to use ISR-based PWM channels on SAM_DUE boards, using Arduino SAM core
, to create and output PWM any GPIO pin. Because this library doesn't use the powerful purely hardware-controlled PWM with many limitations, the maximum PWM frequency is currently limited at 1000Hz, which is still suitable for many real-life applications. Now you can also modify PWM settings on-the-fly.
This library enables you to use Interrupt from Hardware Timers on SAM_DUE boards to create and output PWM to pins. It now supports 16 ISR-based synchronized PWM channels, while consuming only 1 Hardware Timer. PWM interval can be very long (uint64_t microsecs / millisecs). The most important feature is they're ISR-based PWM channels. Therefore, their executions are not blocked by bad-behaving functions or tasks. This important feature is absolutely necessary for mission-critical tasks. These hardware PWM channels, 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 PWM using millis() or micros(). That's necessary if you need to measure some data requiring better accuracy.
As Hardware Timers are rare, and very precious assets of any board, this library now enables you to use up to 16 ISR-based synchronized PWM channels, while consuming only 1 Hardware Timer. Timers' interval is very long (ulong millisecs).
Now with these new 16 ISR-based PWM-channels, the maximum interval is practically unlimited (limited only by unsigned long milliseconds) while the accuracy is nearly perfect compared to software PWM channels.
The most important feature is they're ISR-based PWM channels. Therefore, their executions are not blocked by bad-behaving functions / tasks. This important feature is absolutely necessary for mission-critical tasks.
The ISR_8_PWMs_Array_Complex example will demonstrate the nearly perfect accuracy, compared to software PWM, by printing the actual period / duty-cycle in microsecs
of each of PWM-channels.
Being ISR-based PWM, their executions are not blocked by bad-behaving functions / tasks, such as connecting to WiFi, Internet or Blynk services. You can also have many (up to 16)
PWM channels to use.
This non-being-blocked important feature is absolutely necessary for mission-critical tasks.
You'll see software-based
SimpleTimer is blocked while system is connecting to WiFi / Internet / Blynk, as well as by blocking task
in loop(), using delay() function as an example. The elapsed time then is very unaccurate
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 PWM channels, 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 PWM channels using millis() or micros(). That's necessary if you need to measure some data requiring better accuracy.
Functions using normal software PWM channels, 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:
- SAM_DUE boards**, using
Arduino SAM core
-
Inside the attached function, delay() won’t work and the value returned by millis() will not increment. Serial data received while in the function may be lost. You should declare as volatile any variables that you modify within the attached function.
-
Typically global variables are used to pass data between an ISR and the main program. To make sure variables shared between an ISR and the main program are updated correctly, declare them as volatile.
Arduino IDE 1.8.19+
for Arduino.Arduino SAM core v1.6.12+
- To use with certain example
SimpleTimer library
to use with some examples.
The best and easiest way is to use Arduino Library Manager
. Search for SAMDUE_Slow_PWM, then select / install the latest version.
You can also use this link for more detailed instructions.
Another way to install is to:
- Navigate to SAMDUE_Slow_PWM page.
- Download the latest release
SAMDUE_Slow_PWM-main.zip
. - Extract the zip file to
SAMDUE_Slow_PWM-main
directory - Copy whole
SAMDUE_Slow_PWM-main
folder to Arduino libraries' directory such as~/Arduino/libraries/
.
- Install VS Code
- Install PlatformIO
- Install SAMDUE_Slow_PWM library by using Library Manager. Search for SAMDUE_Slow_PWM in Platform.io Author's Libraries
- Use included platformio.ini file from examples to ensure that all dependent libraries will installed automatically. Please visit documentation for the other options and examples at Project Configuration File
The 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 "SAMDUE_Slow_PWM.hpp" //https://github.com/khoih-prog/SAMDUE_Slow_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 "SAMDUE_Slow_PWM.h" //https://github.com/khoih-prog/SAMDUE_Slow_PWM
Check the new multiFileProject example for a HOWTO
demo.
Have a look at the discussion in Different behaviour using the src_cpp or src_h lib #80
Before using any Timer, you have to make sure the Timer has not been used by any other purpose.
// Init SAMDUE_Slow_PWM, each can service 16 different ISR-based PWM channels
SAMDUE_Slow_PWM ISR_PWM;
void irqCallbackStartFunc()
{
}
void irqCallbackStopFunc()
{
}
////////////////////////////////////////////////
uint16_t attachDueInterrupt(double microseconds, timerCallback callback, const char* TimerName)
{
DueTimerInterrupt dueTimerInterrupt = DueTimer.getAvailable();
dueTimerInterrupt.attachInterruptInterval(microseconds, callback);
uint16_t timerNumber = dueTimerInterrupt.getTimerNumber();
Serial.print(TimerName); Serial.print(F(" attached to Timer(")); Serial.print(timerNumber); Serial.println(F(")"));
return timerNumber;
}
////////////////////////////////////////////////
void setup()
{
....
// Interval in microsecs
attachDueInterrupt(HW_TIMER_INTERVAL_US, TimerHandler, "ITimer");
// You can use this with PWM_Freq in Hz
ISR_PWM.setPWM(PWM_Pin, PWM_Freq, PWM_DutyCycle, irqCallbackStartFunc, irqCallbackStopFunc);
....
}
- ISR_8_PWMs_Array
- ISR_8_PWMs_Array_Complex
- ISR_8_PWMs_Array_Simple
- ISR_Changing_PWM
- ISR_Modify_PWM
- multiFileProject. New
Example ISR_8_PWMs_Array_Complex
The following is the sample terminal output when running example ISR_8_PWMs_Array_Complex to demonstrate how to use multiple PWM channels with complex callback functions, the accuracy of ISR Hardware PWM-channels, especially when system is very busy. The ISR PWM-channels is running exactly according to corresponding programmed periods and duty-cycles
Starting ISR_8_PWMs_Array_Complex on SAM_DUE
SAMDUE_Slow_PWM v1.2.2
CPU Frequency = 84 MHz
Timer Frequency = 84 MHz
[PWM] Using Timer( 0 ) = TC0
[PWM] Channel = 0 , IRQ = TC0_IRQn
ITimer attached to Timer(0)
Channel : 0 Period : 1000000 OnTime : 50000 Start_Time : 2009552
Channel : 1 Period : 500000 OnTime : 50000 Start_Time : 2015589
Channel : 2 Period : 333333 OnTime : 66666 Start_Time : 2021600
Channel : 3 Period : 200000 OnTime : 50000 Start_Time : 2027591
Channel : 4 Period : 100000 OnTime : 30000 Start_Time : 2033591
Channel : 5 Period : 50000 OnTime : 17500 Start_Time : 2039592
Channel : 6 Period : 33333 OnTime : 13333 Start_Time : 2045521
Channel : 7 Period : 20000 OnTime : 9000 Start_Time : 2051429
SimpleTimer (us): 2000, us : 12057006, Dus : 10047524
PWM Channel : 0, prog Period (us): 1000000.00, actual : 1000000, prog DutyCycle : 5.00, actual : 5.00
PWM Channel : 1, prog Period (us): 500000.00, actual : 500000, prog DutyCycle : 10.00, actual : 10.00
PWM Channel : 2, prog Period (us): 333333.33, actual : 333339, prog DutyCycle : 20.00, actual : 20.00
PWM Channel : 3, prog Period (us): 200000.00, actual : 199999, prog DutyCycle : 25.00, actual : 25.00
PWM Channel : 4, prog Period (us): 100000.00, actual : 99999, prog DutyCycle : 30.00, actual : 30.00
PWM Channel : 5, prog Period (us): 50000.00, actual : 50001, prog DutyCycle : 35.00, actual : 35.00
PWM Channel : 6, prog Period (us): 33333.33, actual : 33341, prog DutyCycle : 40.00, actual : 39.95
PWM Channel : 7, prog Period (us): 20000.00, actual : 20000, prog DutyCycle : 45.00, actual : 45.00
SimpleTimer (us): 2000, us : 22120008, Dus : 10063002
PWM Channel : 0, prog Period (us): 1000000.00, actual : 1000000, prog DutyCycle : 5.00, actual : 5.00
PWM Channel : 1, prog Period (us): 500000.00, actual : 500000, prog DutyCycle : 10.00, actual : 10.00
PWM Channel : 2, prog Period (us): 333333.33, actual : 333339, prog DutyCycle : 20.00, actual : 20.00
PWM Channel : 3, prog Period (us): 200000.00, actual : 200000, prog DutyCycle : 25.00, actual : 25.00
PWM Channel : 4, prog Period (us): 100000.00, actual : 100001, prog DutyCycle : 30.00, actual : 30.00
PWM Channel : 5, prog Period (us): 50000.00, actual : 49999, prog DutyCycle : 35.00, actual : 35.00
PWM Channel : 6, prog Period (us): 33333.33, actual : 33340, prog DutyCycle : 40.00, actual : 39.95
PWM Channel : 7, prog Period (us): 20000.00, actual : 20001, prog DutyCycle : 45.00, actual : 45.00
The following is the sample terminal output when running example ISR_8_PWMs_Array on SAM_DUE to demonstrate how to use multiple PWM channels with simple callback functions.
Starting ISR_8_PWMs_Array on SAM_DUE
SAMDUE_Slow_PWM v1.2.2
CPU Frequency = 84 MHz
Timer Frequency = 84 MHz
[PWM] Using Timer( 0 ) = TC0
[PWM] Channel = 0 , IRQ = TC0_IRQn
ITimer attached to Timer(0)
Channel : 0 Period : 1000000 OnTime : 50000 Start_Time : 2008858
Channel : 1 Period : 500000 OnTime : 50000 Start_Time : 2014903
Channel : 2 Period : 333333 OnTime : 66666 Start_Time : 2020913
Channel : 3 Period : 200000 OnTime : 50000 Start_Time : 2026912
Channel : 4 Period : 100000 OnTime : 30000 Start_Time : 2032913
Channel : 5 Period : 50000 OnTime : 17500 Start_Time : 2038913
Channel : 6 Period : 33333 OnTime : 13333 Start_Time : 2044832
Channel : 7 Period : 20000 OnTime : 9000 Start_Time : 2050743
The following is the sample terminal output when running example ISR_8_PWMs_Array_Simple on SAM_DUE to demonstrate how to use multiple PWM channels.
Starting ISR_8_PWMs_Array_Simple on SAM_DUE
SAMDUE_Slow_PWM v1.2.2
CPU Frequency = 84 MHz
Timer Frequency = 84 MHz
[PWM] Using Timer( 0 ) = TC0
[PWM] Channel = 0 , IRQ = TC0_IRQn
ITimer attached to Timer(0)
Channel : 0 Period : 1000000 OnTime : 50000 Start_Time : 2009460
Channel : 1 Period : 500000 OnTime : 50000 Start_Time : 2015503
Channel : 2 Period : 333333 OnTime : 66666 Start_Time : 2021514
Channel : 3 Period : 200000 OnTime : 50000 Start_Time : 2027505
Channel : 4 Period : 100000 OnTime : 30000 Start_Time : 2033505
Channel : 5 Period : 50000 OnTime : 17500 Start_Time : 2039514
Channel : 6 Period : 33333 OnTime : 13333 Start_Time : 2045434
Channel : 7 Period : 20000 OnTime : 9000 Start_Time : 2051343
The following is the sample terminal output when running example ISR_Modify_PWM on SAM_DUE to demonstrate how to modify PWM settings on-the-fly without deleting the PWM channel
Starting ISR_Modify_PWM on SAM_DUE
SAMDUE_Slow_PWM v1.2.2
CPU Frequency = 84 MHz
Timer Frequency = 84 MHz
[PWM] Using Timer( 0 ) = TC0
[PWM] Channel = 0 , IRQ = TC0_IRQn
ITimer attached to Timer(0)
Using PWM Freq = 200.00, PWM DutyCycle = 1.00
Channel : 0 Period : 5000 OnTime : 50 Start_Time : 2012721
Channel : 0 New Period : 10000 OnTime : 555 Start_Time : 12018234
Channel : 0 New Period : 5000 OnTime : 50 Start_Time : 22013234
Channel : 0 New Period : 10000 OnTime : 555 Start_Time : 32018234
Channel : 0 New Period : 5000 OnTime : 50 Start_Time : 42013234
Channel : 0 New Period : 10000 OnTime : 555 Start_Time : 52018234
Channel : 0 New Period : 5000 OnTime : 50 Start_Time : 62023234
Channel : 0 New Period : 10000 OnTime : 555 Start_Time : 72023234
Channel : 0 New Period : 5000 OnTime : 50 Start_Time : 82018234
Channel : 0 New Period : 10000 OnTime : 555 Start_Time : 92023234
Channel : 0 New Period : 5000 OnTime : 50 Start_Time : 102018234
Channel : 0 New Period : 10000 OnTime : 555 Start_Time : 112028234
Channel : 0 New Period : 5000 OnTime : 50 Start_Time : 122023234
Channel : 0 New Period : 10000 OnTime : 555 Start_Time : 132028234
Channel : 0 New Period : 5000 OnTime : 50 Start_Time : 142023234
Channel : 0 New Period : 10000 OnTime : 555 Start_Time : 152028234
Channel : 0 New Period : 5000 OnTime : 50 Start_Time : 162033234
Channel : 0 New Period : 10000 OnTime : 555 Start_Time : 172033234
The following is the sample terminal output when running example ISR_Changing_PWM on SAM_DUE to demonstrate how to modify PWM settings on-the-fly by deleting the PWM channel and reinit the PWM channel
Starting ISR_Changing_PWM on SAM_DUE
SAMDUE_Slow_PWM v1.2.2
CPU Frequency = 84 MHz
Timer Frequency = 84 MHz
[PWM] Using Timer( 0 ) = TC0
[PWM] Channel = 0 , IRQ = TC0_IRQn
ITimer attached to Timer(0)
Using PWM Freq = 1.00, PWM DutyCycle = 50.00
Channel : 0 Period : 1000000 OnTime : 500000 Start_Time : 2012800
Using PWM Freq = 2.00, PWM DutyCycle = 90.00
Channel : 0 Period : 500000 OnTime : 450000 Start_Time : 12018300
Using PWM Freq = 1.00, PWM DutyCycle = 50.00
Channel : 0 Period : 1000000 OnTime : 500000 Start_Time : 22018303
Using PWM Freq = 2.00, PWM DutyCycle = 90.00
Channel : 0 Period : 500000 OnTime : 450000 Start_Time : 32018301
Using PWM Freq = 1.00, PWM DutyCycle = 50.00
Channel : 0 Period : 1000000 OnTime : 500000 Start_Time : 42018304
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: SAMDUE_Slow_PWM issues
- Search for bug and improvement.
- Similar features for remaining Arduino boards
- Basic hardware multi-channel PWM for SAM_DUE, etc. using
Arduino SAM core
- Add Table of Contents
- Add functions to modify PWM settings on-the-fly
- Fix
multiple-definitions
linker error. Dropsrc_cpp
andsrc_h
directories - DutyCycle to be optionally updated at the end current PWM period instead of immediately.
- Add examples multiFileProject to demo for multiple-file project
- Improve accuracy by using
float
, instead ofuint32_t
fordutycycle
- Optimize library code by using
reference-passing
instead ofvalue-passing
- Display informational warning only when
_PWM_LOGLEVEL_
> 3
Many thanks for everyone for bug reporting, new feature suggesting, testing and contributing to the development of this library.
If you want to contribute to this project:
- Report bugs and errors
- Ask for enhancements
- Create issues and pull requests
- Tell other people about this library
- The library is licensed under MIT
Copyright (c) 2021- Khoi Hoang