Skip to content
This repository has been archived by the owner on Jan 29, 2023. It is now read-only.

Commit

Permalink
v1.2.0 to fix multiple-definitions linker error
Browse files Browse the repository at this point in the history
### Releases v1.2.0

1. Fix `multiple-definitions` linker error. Drop `src_cpp` and `src_h` directories
2. Improve accuracy by using `float`, instead of `uint32_t` for `dutycycle`. Check [Change Duty Cycle #1](khoih-prog/ESP8266_PWM#1 (comment))
3. DutyCycle to be optionally updated at the end current PWM period instead of immediately. Check [DutyCycle to be updated at the end current PWM period #2](khoih-prog/ESP8266_PWM#2)
4. Optimize library code by using `reference-passing` instead of `value-passing`
5. Add example [multiFileProject](examples/multiFileProject) to demo for multiple-file project
6. Update examples accordingly
  • Loading branch information
khoih-prog authored Feb 8, 2022
1 parent 32c65d7 commit c5c1aa5
Show file tree
Hide file tree
Showing 21 changed files with 2,151 additions and 1,773 deletions.
10 changes: 5 additions & 5 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ If you don't find anything, please [open a new issue](https://github.com/khoih-p

Please ensure to specify the following:

* Arduino IDE version (e.g. 1.8.16) or Platform.io version
* `Teensyduino` Core Version (e.g. Teensy core v1.55)
* Arduino IDE version (e.g. 1.8.19) or Platform.io version
* `Teensyduino` Core Version (e.g. Teensy core v1.56)
* Contextual information (e.g. what you were trying to achieve)
* Simplest possible steps to reproduce
* Anything that might be relevant in your opinion, such as:
Expand All @@ -26,10 +26,10 @@ Please ensure to specify the following:
### Example

```
Arduino IDE version: 1.8.16
Teensyduino Core Version 1.55
Arduino IDE version: 1.8.19
Teensyduino Core Version 1.56
OS: Ubuntu 20.04 LTS
Linux xy-Inspiron-3593 5.4.0-90-generic #101-Ubuntu SMP Fri Oct 15 20:00:55 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
Linux xy-Inspiron-3593 5.4.0-99-generic #112-Ubuntu SMP Thu Feb 3 13:50:55 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
Context:
I encountered a crash while trying to use the Timer Interrupt.
Expand Down
298 changes: 178 additions & 120 deletions README.md

Large diffs are not rendered by default.

19 changes: 11 additions & 8 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
## Table of Contents

* [Changelog](#changelog)
* [Releases v1.2.0](#releases-v120)
* [Releases v1.1.0](#releases-v110)
* [Initial Releases v1.0.0](#Initial-Releases-v100)

Expand All @@ -20,6 +21,16 @@

## Changelog

### Releases v1.2.0

1. Fix `multiple-definitions` linker error. Drop `src_cpp` and `src_h` directories
2. Improve accuracy by using `float`, instead of `uint32_t` for `dutycycle`. Check [Change Duty Cycle #1](https://github.com/khoih-prog/ESP8266_PWM/issues/1#issuecomment-1024969658)
3. DutyCycle to be optionally updated at the end current PWM period instead of immediately. Check [DutyCycle to be updated at the end current PWM period #2](https://github.com/khoih-prog/ESP8266_PWM/issues/2)
4. Optimize library code by using `reference-passing` instead of `value-passing`
5. Add example [multiFileProject](examples/multiFileProject) to demo for multiple-file project
6. Update examples accordingly


### Releases v1.1.0

1. Add functions to modify PWM settings on-the-fly
Expand All @@ -31,11 +42,3 @@

2. The hybrid ISR-based PWM channels can generate from very low (much less than 1Hz) to highest PWM frequencies up to 500-1000Hz with acceptable accuracy.

---
---

## Copyright

Copyright 2021- Khoi Hoang


15 changes: 8 additions & 7 deletions examples/ISR_8_PWMs_Array/ISR_8_PWMs_Array.ino
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#define USING_MICROS_RESOLUTION true //false

// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error
#include "Teensy_Slow_PWM.h"

#include <SimpleTimer.h> // https://github.com/jfturcot/SimpleTimer
Expand All @@ -48,8 +49,8 @@
#if defined(__IMXRT1062__)
// For Teensy 4.0 and 4.1
// Don't change these numbers to make higher Timer freq. System can hang
#define HW_TIMER_INTERVAL_MS 0.0333f
#define HW_TIMER_INTERVAL_FREQ 30000L
#define HW_TIMER_INTERVAL_MS 0.01f
#define HW_TIMER_INTERVAL_FREQ 100000L
#elif defined(__MK66FX1M0__)
// For Teensy 3.6
// Don't change these numbers to make higher Timer freq. System can hang
Expand Down Expand Up @@ -99,15 +100,15 @@ uint32_t PWM_Pin[] =
#define NUMBER_ISR_PWMS ( sizeof(PWM_Pin) / sizeof(uint32_t) )

// You can assign any interval for any timer here, in Hz
double PWM_Freq[NUMBER_ISR_PWMS] =
float PWM_Freq[] =
{
1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f,
};

// You can assign any interval for any timer here, in Microseconds
uint32_t PWM_DutyCycle[NUMBER_ISR_PWMS] =
float PWM_DutyCycle[] =
{
5, 10, 20, 25, 30, 35, 40, 45
5.0, 10.0, 20.0, 30.0, 40.0, 45.0, 50.0, 55.0
};

typedef void (*irqCallback) ();
Expand Down Expand Up @@ -149,7 +150,7 @@ void doingSomething7()
}


irqCallback irqCallbackStartFunc[NUMBER_ISR_PWMS] =
irqCallback irqCallbackStartFunc[] =
{
doingSomething0, doingSomething1, doingSomething2, doingSomething3,
doingSomething4, doingSomething5, doingSomething6, doingSomething7
Expand Down Expand Up @@ -200,7 +201,7 @@ void setup()
// You can use up to 16 timer for each ISR_PWM
for (uint16_t i = 0; i < NUMBER_ISR_PWMS; i++)
{
//void setPWM(uint32_t pin, uint32_t frequency, uint32_t dutycycle
//void setPWM(uint32_t pin, float frequency, float dutycycle
// , timer_callback_p StartCallback = nullptr, timer_callback_p StopCallback = nullptr)

// You can use this with PWM_Freq in Hz
Expand Down
44 changes: 23 additions & 21 deletions examples/ISR_8_PWMs_Array_Complex/ISR_8_PWMs_Array_Complex.ino
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#define USING_MICROS_RESOLUTION true //false

// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error
#include "Teensy_Slow_PWM.h"

#include <SimpleTimer.h> // https://github.com/jfturcot/SimpleTimer
Expand All @@ -48,8 +49,8 @@
#if defined(__IMXRT1062__)
// For Teensy 4.0 and 4.1
// Don't change these numbers to make higher Timer freq. System can hang
#define HW_TIMER_INTERVAL_MS 0.0333f
#define HW_TIMER_INTERVAL_FREQ 30000L
#define HW_TIMER_INTERVAL_MS 0.01f
#define HW_TIMER_INTERVAL_FREQ 100000L
#elif defined(__MK66FX1M0__)
// For Teensy 3.6
// Don't change these numbers to make higher Timer freq. System can hang
Expand Down Expand Up @@ -112,9 +113,9 @@ typedef struct
irqCallback irqCallbackStartFunc;
irqCallback irqCallbackStopFunc;

uint32_t PWM_Freq;
float PWM_Freq;

uint32_t PWM_DutyCycle;
float PWM_DutyCycle;

uint32_t deltaMicrosStart;
uint32_t previousMicrosStart;
Expand All @@ -134,29 +135,30 @@ void doingSomethingStop(int index);

#else // #if USE_COMPLEX_STRUCT

volatile unsigned long deltaMicrosStart [NUMBER_ISR_PWMS] = { 0, 0, 0, 0, 0, 0, 0, 0 };
volatile unsigned long previousMicrosStart [NUMBER_ISR_PWMS] = { 0, 0, 0, 0, 0, 0, 0, 0 };
volatile unsigned long deltaMicrosStart [] = { 0, 0, 0, 0, 0, 0, 0, 0 };
volatile unsigned long previousMicrosStart [] = { 0, 0, 0, 0, 0, 0, 0, 0 };

volatile unsigned long deltaMicrosStop [NUMBER_ISR_PWMS] = { 0, 0, 0, 0, 0, 0, 0, 0 };
volatile unsigned long previousMicrosStop [NUMBER_ISR_PWMS] = { 0, 0, 0, 0, 0, 0, 0, 0 };
volatile unsigned long deltaMicrosStop [] = { 0, 0, 0, 0, 0, 0, 0, 0 };
volatile unsigned long previousMicrosStop [] = { 0, 0, 0, 0, 0, 0, 0, 0 };


// You can assign any interval for any timer here, in Microseconds
uint32_t PWM_Period[NUMBER_ISR_PWMS] =
uint32_t PWM_Period[] =
{
1000L, 500L, 333L, 250L, 200L, 166L, 142L, 125L
};

// You can assign any interval for any timer here, in Hz
double PWM_Freq[NUMBER_ISR_PWMS] =
// You can assign any interval for any timer here, in Hz
float PWM_Freq[] =
{
1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f,
};

// You can assign any interval for any timer here, in Microseconds
uint32_t PWM_DutyCycle[NUMBER_ISR_PWMS] =
float PWM_DutyCycle[] =
{
5, 10, 20, 25, 30, 35, 40, 45
5.0, 10.0, 20.0, 30.0, 40.0, 45.0, 50.0, 55.0
};

void doingSomethingStart(int index)
Expand Down Expand Up @@ -270,17 +272,17 @@ void doingSomethingStop7()

#if USE_COMPLEX_STRUCT

ISR_PWM_Data curISR_PWM_Data[NUMBER_ISR_PWMS] =
ISR_PWM_Data curISR_PWM_Data[] =
{
// pin, irqCallbackStartFunc, irqCallbackStopFunc, PWM_Freq, PWM_DutyCycle, deltaMicrosStart, previousMicrosStart, deltaMicrosStop, previousMicrosStop
{ LED_BUILTIN, doingSomethingStart0, doingSomethingStop0, 1, 5, 0, 0, 0, 0 },
{ PIN_D0, doingSomethingStart1, doingSomethingStop1, 2, 10, 0, 0, 0, 0 },
{ PIN_D1, doingSomethingStart2, doingSomethingStop2, 3, 20, 0, 0, 0, 0 },
{ PIN_D2, doingSomethingStart3, doingSomethingStop3, 4, 25, 0, 0, 0, 0 },
{ PIN_D3, doingSomethingStart4, doingSomethingStop4, 5, 30, 0, 0, 0, 0 },
{ PIN_D4, doingSomethingStart5, doingSomethingStop5, 6, 35, 0, 0, 0, 0 },
{ PIN_D5, doingSomethingStart6, doingSomethingStop6, 7, 40, 0, 0, 0, 0 },
{ PIN_D6, doingSomethingStart7, doingSomethingStop7, 8, 45, 0, 0, 0, 0 },
{ PIN_D2, doingSomethingStart3, doingSomethingStop3, 4, 30, 0, 0, 0, 0 },
{ PIN_D3, doingSomethingStart4, doingSomethingStop4, 5, 40, 0, 0, 0, 0 },
{ PIN_D4, doingSomethingStart5, doingSomethingStop5, 6, 45, 0, 0, 0, 0 },
{ PIN_D5, doingSomethingStart6, doingSomethingStop6, 7, 50, 0, 0, 0, 0 },
{ PIN_D6, doingSomethingStart7, doingSomethingStop7, 8, 55, 0, 0, 0, 0 },
};


Expand All @@ -304,13 +306,13 @@ void doingSomethingStop(int index)

#else // #if USE_COMPLEX_STRUCT

irqCallback irqCallbackStartFunc[NUMBER_ISR_PWMS] =
irqCallback irqCallbackStartFunc[] =
{
doingSomethingStart0, doingSomethingStart1, doingSomethingStart2, doingSomethingStart3,
doingSomethingStart4, doingSomethingStart5, doingSomethingStart6, doingSomethingStart7
};

irqCallback irqCallbackStopFunc[NUMBER_ISR_PWMS] =
irqCallback irqCallbackStopFunc[] =
{
doingSomethingStop0, doingSomethingStop1, doingSomethingStop2, doingSomethingStop3,
doingSomethingStop4, doingSomethingStop5, doingSomethingStop6, doingSomethingStop7
Expand Down Expand Up @@ -428,7 +430,7 @@ void setup()
curISR_PWM_Data[i].previousMicrosStart = startMicros;
//ISR_PWM.setInterval(curISR_PWM_Data[i].PWM_Period, curISR_PWM_Data[i].irqCallbackStartFunc);

//void setPWM(uint32_t pin, uint32_t frequency, uint32_t dutycycle
//void setPWM(uint32_t pin, float frequency, float dutycycle
// , timer_callback_p StartCallback = nullptr, timer_callback_p StopCallback = nullptr)

// You can use this with PWM_Freq in Hz
Expand Down
13 changes: 7 additions & 6 deletions examples/ISR_8_PWMs_Array_Simple/ISR_8_PWMs_Array_Simple.ino
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#define USING_MICROS_RESOLUTION true //false

// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error
#include "Teensy_Slow_PWM.h"

#include <SimpleTimer.h> // https://github.com/jfturcot/SimpleTimer
Expand All @@ -48,8 +49,8 @@
#if defined(__IMXRT1062__)
// For Teensy 4.0 and 4.1
// Don't change these numbers to make higher Timer freq. System can hang
#define HW_TIMER_INTERVAL_MS 0.0333f
#define HW_TIMER_INTERVAL_FREQ 30000L
#define HW_TIMER_INTERVAL_MS 0.01f
#define HW_TIMER_INTERVAL_FREQ 100000L
#elif defined(__MK66FX1M0__)
// For Teensy 3.6
// Don't change these numbers to make higher Timer freq. System can hang
Expand Down Expand Up @@ -99,15 +100,15 @@ uint32_t PWM_Pin[] =
#define NUMBER_ISR_PWMS ( sizeof(PWM_Pin) / sizeof(uint32_t) )

// You can assign any interval for any timer here, in Hz
double PWM_Freq[NUMBER_ISR_PWMS] =
float PWM_Freq[] =
{
1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f,
};

// You can assign any interval for any timer here, in Microseconds
uint32_t PWM_DutyCycle[NUMBER_ISR_PWMS] =
float PWM_DutyCycle[] =
{
5, 10, 20, 25, 30, 35, 40, 45
5.0, 10.0, 20.0, 30.0, 40.0, 45.0, 50.0, 55.0
};

////////////////////////////////////////////////
Expand Down Expand Up @@ -154,7 +155,7 @@ void setup()
// You can use up to 16 timer for each ISR_PWM
for (uint16_t i = 0; i < NUMBER_ISR_PWMS; i++)
{
//void setPWM(uint32_t pin, uint32_t frequency, uint32_t dutycycle
//void setPWM(uint32_t pin, float frequency, float dutycycle
// , timer_callback_p StartCallback = nullptr, timer_callback_p StopCallback = nullptr)

// You can use this with PWM_Freq in Hz
Expand Down
9 changes: 5 additions & 4 deletions examples/ISR_Changing_PWM/ISR_Changing_PWM.ino
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#define USING_MICROS_RESOLUTION true //false

// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error
#include "Teensy_Slow_PWM.h"

#define LED_OFF HIGH
Expand Down Expand Up @@ -86,19 +87,19 @@ void TimerHandler()
uint32_t PWM_Pin = LED_BUILTIN;

// You can assign any interval for any timer here, in Hz
double PWM_Freq1 = 1.0f;
float PWM_Freq1 = 1.0f;
// You can assign any interval for any timer here, in Hz
double PWM_Freq2 = 2.0f;
float PWM_Freq2 = 2.0f;

// You can assign any interval for any timer here, in microseconds
uint32_t PWM_Period1 = 1000000 / PWM_Freq1;
// You can assign any interval for any timer here, in microseconds
uint32_t PWM_Period2 = 1000000 / PWM_Freq2;

// You can assign any duty_cycle for any PWM here, from 0-100
uint32_t PWM_DutyCycle1 = 50;
float PWM_DutyCycle1 = 50.0;
// You can assign any duty_cycle for any PWM here, from 0-100
uint32_t PWM_DutyCycle2 = 90;
float PWM_DutyCycle2 = 90.0;

// Channel number used to identify associated channel
int channelNum;
Expand Down
9 changes: 5 additions & 4 deletions examples/ISR_Modify_PWM/ISR_Modify_PWM.ino
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#define USING_MICROS_RESOLUTION true //false

// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error
#include "Teensy_Slow_PWM.h"

#define LED_OFF HIGH
Expand Down Expand Up @@ -86,19 +87,19 @@ void TimerHandler()
uint32_t PWM_Pin = LED_BUILTIN;

// You can assign any interval for any timer here, in Hz
double PWM_Freq1 = 1.0f;
float PWM_Freq1 = 1.0f;
// You can assign any interval for any timer here, in Hz
double PWM_Freq2 = 2.0f;
float PWM_Freq2 = 2.0f;

// You can assign any interval for any timer here, in microseconds
uint32_t PWM_Period1 = 1000000 / PWM_Freq1;
// You can assign any interval for any timer here, in microseconds
uint32_t PWM_Period2 = 1000000 / PWM_Freq2;

// You can assign any duty_cycle for any PWM here, from 0-100
uint32_t PWM_DutyCycle1 = 10;
float PWM_DutyCycle1 = 50.0;
// You can assign any duty_cycle for any PWM here, from 0-100
uint32_t PWM_DutyCycle2 = 90;
float PWM_DutyCycle2 = 90.0;

// Channel number used to identify associated channel
int channelNum;
Expand Down
13 changes: 13 additions & 0 deletions examples/multiFileProject/multiFileProject.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/****************************************************************************************************************************
multiFileProject.cpp
For nRF52-based boards using Adafruit_nRF52_Arduino core
Written by Khoi Hoang
Built by Khoi Hoang https://github.com/khoih-prog/nRF52_Slow_PWM
Licensed under MIT license
*****************************************************************************************************************************/

// To demo how to include files in multi-file Projects

#include "multiFileProject.h"
21 changes: 21 additions & 0 deletions examples/multiFileProject/multiFileProject.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/****************************************************************************************************************************
multiFileProject.h
For nRF52-based boards using Adafruit_nRF52_Arduino core
Written by Khoi Hoang
Built by Khoi Hoang https://github.com/khoih-prog/nRF52_Slow_PWM
Licensed under MIT license
*****************************************************************************************************************************/

// To demo how to include files in multi-file Projects

#pragma once

#define USING_MICROS_RESOLUTION true //false

// Default is true, uncomment to false
//#define CHANGING_PWM_END_OF_CYCLE false

// Can be included as many times as necessary, without `Multiple Definitions` Linker Error
#include "Teensy_Slow_PWM.hpp"
Loading

0 comments on commit c5c1aa5

Please sign in to comment.