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

Commit c6387d1

Browse files
authored
v1.2.0 to fix multiple-definitions linker error
### 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
1 parent 4764b9c commit c6387d1

21 files changed

+1455
-1141
lines changed

CONTRIBUTING.md

+6-5
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ If you don't find anything, please [open a new issue](https://github.com/khoih-p
1414

1515
Please ensure to specify the following:
1616

17-
* Arduino IDE version (e.g. 1.8.16) or Platform.io version
18-
* Arduino `mbed` Core Version (e.g. Arduino `mbed` core v1.3.2 or Arduino `mbed_nano` core v2.5.2 )
17+
* Arduino IDE version (e.g. 1.8.19) or Platform.io version
18+
* Arduino `mbed` Core Version (e.g. Arduino `mbed` core v1.3.2 or Arduino `mbed_nano` core v2.6.1)
1919
* Contextual information (e.g. what you were trying to achieve)
2020
* Simplest possible steps to reproduce
2121
* Anything that might be relevant in your opinion, such as:
@@ -26,10 +26,10 @@ Please ensure to specify the following:
2626
### Example
2727

2828
```
29-
Arduino IDE version: 1.8.16
30-
Arduino mbed_nano` core v2.5.2
29+
Arduino IDE version: 1.8.19
30+
Arduino mbed_nano` core v2.6.1
3131
OS: Ubuntu 20.04 LTS
32-
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
32+
Linux xy-Inspiron-3593 5.4.0-97-generic #110-Ubuntu SMP Thu Jan 13 18:22:13 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
3333
3434
Context:
3535
I encountered a crash while trying to use the Timer Interrupt.
@@ -50,3 +50,4 @@ There are usually some outstanding feature requests in the [existing issues list
5050
### Sending Pull Requests
5151

5252
Pull Requests with changes and fixes are also welcome!
53+

README.md

+185-159
Large diffs are not rendered by default.

changelog.md

+10
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
## Table of Contents
1313

1414
* [Changelog](#changelog)
15+
* [Releases v1.2.0](#releases-v120)
1516
* [Releases v1.1.0](#releases-v110)
1617
* [Initial Releases v1.0.0](#Initial-Releases-v100)
1718

@@ -20,6 +21,15 @@
2021

2122
## Changelog
2223

24+
### Releases v1.2.0
25+
26+
1. Fix `multiple-definitions` linker error. Drop `src_cpp` and `src_h` directories
27+
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)
28+
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)
29+
4. Optimize library code by using `reference-passing` instead of `value-passing`
30+
5. Add example [multiFileProject](examples/multiFileProject) to demo for multiple-file project
31+
6. Update examples accordingly
32+
2333
### Releases v1.1.0
2434

2535
1. Add functions to modify PWM settings on-the-fly

examples/ISR_16_PWMs_Array/ISR_16_PWMs_Array.ino

+8-16
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,6 @@
1111
The accuracy is nearly perfect compared to software timers. The most important feature is they're ISR-based timers
1212
Therefore, their executions are not blocked by bad-behaving functions / tasks.
1313
This important feature is absolutely necessary for mission-critical tasks.
14-
15-
Version: 1.0.0
16-
17-
Version Modified By Date Comments
18-
------- ----------- ---------- -----------
19-
1.0.0 K.Hoang 25/09/2021 Initial coding for nRF52-based boards using Arduino mbed_nano core, such as Nano_33_BLE
2014
*****************************************************************************************************************************/
2115

2216
#if !( ARDUINO_ARCH_NRF52840 && TARGET_NAME == ARDUINO_NANO33BLE )
@@ -92,31 +86,31 @@ void TimerHandler()
9286
//////////////////////////////////////////////////////
9387

9488
// You can assign pins here. Be carefull to select good pin to use or crash, e.g pin 6-11
95-
uint32_t PWM_Pin[NUMBER_ISR_PWMS] =
89+
uint32_t PWM_Pin[] =
9690
{
9791
LED_BUILTIN, LED_BLUE, LED_RED, PIN_D0, PIN_D1, PIN_D2, PIN_D3, PIN_D4,
9892
PIN_D5, PIN_D6, PIN_D7, PIN_D8, PIN_D9, PIN_D10, PIN_D11, PIN_D12
9993
};
10094

10195
// You can assign any interval for any timer here, in microseconds
102-
uint32_t PWM_Period[NUMBER_ISR_PWMS] =
96+
uint32_t PWM_Period[] =
10397
{
10498
1000000L, 500000L, 333333L, 250000L, 200000L, 166667L, 142857L, 125000L,
10599
111111L, 100000L, 66667L, 50000L, 40000L, 33333L, 25000L, 20000L
106100
};
107101

108102
// You can assign any interval for any timer here, in Hz
109-
double PWM_Freq[NUMBER_ISR_PWMS] =
103+
float PWM_Freq[] =
110104
{
111105
1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f,
112106
9.0f, 10.0f, 15.0f, 20.0f, 25.0f, 30.0f, 40.0f, 50.0f
113107
};
114108

115109
// You can assign any interval for any timer here, in milliseconds
116-
uint32_t PWM_DutyCycle[NUMBER_ISR_PWMS] =
110+
float PWM_DutyCycle[] =
117111
{
118-
5, 10, 20, 30, 40, 45, 50, 55,
119-
60, 65, 70, 75, 80, 85, 90, 95
112+
5.0, 10.0, 20.0, 30.0, 40.0, 45.0, 50.0, 55.0,
113+
60.0, 65.0, 70.0, 75.0, 80.0, 85.0, 90.0, 95.0
120114
};
121115

122116
typedef void (*irqCallback) ();
@@ -189,7 +183,7 @@ void doingSomething15()
189183
{
190184
}
191185

192-
irqCallback irqCallbackStartFunc[NUMBER_ISR_PWMS] =
186+
irqCallback irqCallbackStartFunc[] =
193187
{
194188
doingSomething0, doingSomething1, doingSomething2, doingSomething3,
195189
doingSomething4, doingSomething5, doingSomething6, doingSomething7,
@@ -218,12 +212,11 @@ void setup()
218212
else
219213
Serial.println(F("Can't set ITimer. Select another freq. or timer"));
220214

221-
#if 1
222215
// Just to demonstrate, don't use too many ISR Timers if not absolutely necessary
223216
// You can use up to 16 timer for each ISR_PWM
224217
for (uint16_t i = 0; i < NUMBER_ISR_PWMS; i++)
225218
{
226-
//void setPWM(uint32_t pin, uint32_t frequency, uint32_t dutycycle
219+
//void setPWM(uint32_t pin, float frequency, float dutycycle
227220
// , timer_callback_p StartCallback = nullptr, timer_callback_p StopCallback = nullptr)
228221

229222
#if USING_PWM_FREQUENCY
@@ -241,7 +234,6 @@ void setup()
241234
#endif
242235
#endif
243236
}
244-
#endif
245237
}
246238

247239
void loop()

examples/ISR_16_PWMs_Array_Complex/ISR_16_PWMs_Array_Complex.ino

+18-23
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,6 @@
1111
The accuracy is nearly perfect compared to software timers. The most important feature is they're ISR-based timers
1212
Therefore, their executions are not blocked by bad-behaving functions / tasks.
1313
This important feature is absolutely necessary for mission-critical tasks.
14-
15-
Version: 1.0.0
16-
17-
Version Modified By Date Comments
18-
------- ----------- ---------- -----------
19-
1.0.0 K.Hoang 25/09/2021 Initial coding for nRF52-based boards using Arduino mbed_nano core, such as Nano_33_BLE
2014
*****************************************************************************************************************************/
2115

2216
#if !( ARDUINO_ARCH_NRF52840 && TARGET_NAME == ARDUINO_NANO33BLE )
@@ -106,12 +100,12 @@ typedef struct
106100
irqCallback irqCallbackStopFunc;
107101

108102
#if USING_PWM_FREQUENCY
109-
uint32_t PWM_Freq;
103+
float PWM_Freq;
110104
#else
111105
uint32_t PWM_Period;
112106
#endif
113107

114-
uint32_t PWM_DutyCycle;
108+
float PWM_DutyCycle;
115109

116110
uint64_t deltaMicrosStart;
117111
uint64_t previousMicrosStart;
@@ -131,40 +125,41 @@ void doingSomethingStop(int index);
131125

132126
#else // #if USE_COMPLEX_STRUCT
133127

134-
volatile unsigned long deltaMicrosStart [NUMBER_ISR_PWMS] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
135-
volatile unsigned long previousMicrosStart [NUMBER_ISR_PWMS] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
128+
volatile unsigned long deltaMicrosStart [] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
129+
volatile unsigned long previousMicrosStart [] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
136130

137-
volatile unsigned long deltaMicrosStop [NUMBER_ISR_PWMS] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
138-
volatile unsigned long previousMicrosStop [NUMBER_ISR_PWMS] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
131+
volatile unsigned long deltaMicrosStop [] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
132+
volatile unsigned long previousMicrosStop [] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
139133

140134
// You can assign pins here. Be carefull to select good pin to use or crash, e.g pin 6-11
141-
uint32_t PWM_Pin[NUMBER_ISR_PWMS] =
135+
uint32_t PWM_Pin[] =
142136
{
143137
LED_BUILTIN, LED_BLUE, LED_RED, PIN_D0, PIN_D1, PIN_D2, PIN_D3, PIN_D4,
144138
PIN_D5, PIN_D6, PIN_D7, PIN_D8, PIN_D9, PIN_D10, PIN_D11, PIN_D12
145139
};
146140

147141
// You can assign any interval for any timer here, in microseconds
148-
uint32_t PWM_Period[NUMBER_ISR_PWMS] =
142+
uint32_t PWM_Period[] =
149143
{
150144
1000000L, 500000L, 333333L, 250000L, 200000L, 166667L, 142857L, 125000L,
151145
111111L, 100000L, 66667L, 50000L, 40000L, 33333L, 25000L, 20000L
152146
};
153147

154148
// You can assign any interval for any timer here, in Hz
155-
double PWM_Freq[NUMBER_ISR_PWMS] =
149+
float PWM_Freq[] =
156150
{
157151
1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f,
158152
9.0f, 10.0f, 15.0f, 20.0f, 25.0f, 30.0f, 40.0f, 50.0f
159153
};
160154

161155
// You can assign any interval for any timer here, in milliseconds
162-
uint32_t PWM_DutyCycle[NUMBER_ISR_PWMS] =
156+
float PWM_DutyCycle[] =
163157
{
164-
5, 10, 20, 30, 40, 45, 50, 55,
165-
60, 65, 70, 75, 80, 85, 90, 95
158+
5.0, 10.0, 20.0, 30.0, 40.0, 45.0, 50.0, 55.0,
159+
60.0, 65.0, 70.0, 75.0, 80.0, 85.0, 90.0, 95.0
166160
};
167161

162+
168163
void doingSomethingStart(int index)
169164
{
170165
unsigned long currentMicros = micros();
@@ -356,7 +351,7 @@ void doingSomethingStop15()
356351

357352
#if USING_PWM_FREQUENCY
358353

359-
ISR_PWM_Data curISR_PWM_Data[NUMBER_ISR_PWMS] =
354+
ISR_PWM_Data curISR_PWM_Data[] =
360355
{
361356
// pin, irqCallbackStartFunc, irqCallbackStopFunc, PWM_Freq, PWM_DutyCycle, deltaMicrosStart, previousMicrosStart, deltaMicrosStop, previousMicrosStop
362357
{ LED_BUILTIN, doingSomethingStart0, doingSomethingStop0, 1, 5, 0, 0, 0, 0 },
@@ -379,7 +374,7 @@ void doingSomethingStop15()
379374

380375
#else // #if USING_PWM_FREQUENCY
381376

382-
ISR_PWM_Data curISR_PWM_Data[NUMBER_ISR_PWMS] =
377+
ISR_PWM_Data curISR_PWM_Data[] =
383378
{
384379
// pin, irqCallbackStartFunc, irqCallbackStopFunc, PWM_Period, PWM_DutyCycle, deltaMicrosStart, previousMicrosStart, deltaMicrosStop, previousMicrosStop
385380
{ LED_BUILTIN, doingSomethingStart0, doingSomethingStop0, 1000000L, 5, 0, 0, 0, 0 },
@@ -422,15 +417,15 @@ void doingSomethingStop(int index)
422417

423418
#else // #if USE_COMPLEX_STRUCT
424419

425-
irqCallback irqCallbackStartFunc[NUMBER_ISR_PWMS] =
420+
irqCallback irqCallbackStartFunc[] =
426421
{
427422
doingSomethingStart0, doingSomethingStart1, doingSomethingStart2, doingSomethingStart3,
428423
doingSomethingStart4, doingSomethingStart5, doingSomethingStart6, doingSomethingStart7,
429424
doingSomethingStart8, doingSomethingStart9, doingSomethingStart10, doingSomethingStart11,
430425
doingSomethingStart12, doingSomethingStart13, doingSomethingStart14, doingSomethingStart15
431426
};
432427

433-
irqCallback irqCallbackStopFunc[NUMBER_ISR_PWMS] =
428+
irqCallback irqCallbackStopFunc[] =
434429
{
435430
doingSomethingStop0, doingSomethingStop1, doingSomethingStop2, doingSomethingStop3,
436431
doingSomethingStop4, doingSomethingStop5, doingSomethingStop6, doingSomethingStop7,
@@ -535,7 +530,7 @@ void setup()
535530
curISR_PWM_Data[i].previousMicrosStart = startMicros;
536531
//ISR_PWM.setInterval(curISR_PWM_Data[i].PWM_Period, curISR_PWM_Data[i].irqCallbackStartFunc);
537532

538-
//void setPWM(uint32_t pin, uint32_t frequency, uint32_t dutycycle
533+
//void setPWM(uint32_t pin, float frequency, float dutycycle
539534
// , timer_callback_p StartCallback = nullptr, timer_callback_p StopCallback = nullptr)
540535

541536
#if USING_PWM_FREQUENCY

examples/ISR_16_PWMs_Array_Simple/ISR_16_PWMs_Array_Simple.ino

+5-13
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,6 @@
1111
The accuracy is nearly perfect compared to software timers. The most important feature is they're ISR-based timers
1212
Therefore, their executions are not blocked by bad-behaving functions / tasks.
1313
This important feature is absolutely necessary for mission-critical tasks.
14-
15-
Version: 1.0.0
16-
17-
Version Modified By Date Comments
18-
------- ----------- ---------- -----------
19-
1.0.0 K.Hoang 25/09/2021 Initial coding for nRF52-based boards using Arduino mbed_nano core, such as Nano_33_BLE
2014
*****************************************************************************************************************************/
2115

2216
#if !( ARDUINO_ARCH_NRF52840 && TARGET_NAME == ARDUINO_NANO33BLE )
@@ -107,21 +101,19 @@ uint32_t PWM_Period[NUMBER_ISR_PWMS] =
107101
};
108102

109103
// You can assign any interval for any timer here, in Hz
110-
double PWM_Freq[NUMBER_ISR_PWMS] =
104+
float PWM_Freq[] =
111105
{
112106
1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f,
113107
9.0f, 10.0f, 15.0f, 20.0f, 25.0f, 30.0f, 40.0f, 50.0f
114108
};
115109

116110
// You can assign any interval for any timer here, in milliseconds
117-
uint32_t PWM_DutyCycle[NUMBER_ISR_PWMS] =
111+
float PWM_DutyCycle[] =
118112
{
119-
5, 10, 20, 30, 40, 45, 50, 55,
120-
60, 65, 70, 75, 80, 85, 90, 95
113+
5.0, 10.0, 20.0, 30.0, 40.0, 45.0, 50.0, 55.0,
114+
60.0, 65.0, 70.0, 75.0, 80.0, 85.0, 90.0, 95.0
121115
};
122116

123-
124-
125117
////////////////////////////////////////////////
126118

127119
void setup()
@@ -147,7 +139,7 @@ void setup()
147139
// You can use up to 16 timer for each ISR_PWM
148140
for (uint16_t i = 0; i < NUMBER_ISR_PWMS; i++)
149141
{
150-
//void setPWM(uint32_t pin, uint32_t frequency, uint32_t dutycycle
142+
//void setPWM(uint32_t pin, float frequency, float dutycycle
151143
// , timer_callback_p StartCallback = nullptr, timer_callback_p StopCallback = nullptr)
152144

153145
#if USING_PWM_FREQUENCY

examples/ISR_Changing_PWM/ISR_Changing_PWM.ino

+4-10
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,6 @@
1111
The accuracy is nearly perfect compared to software timers. The most important feature is they're ISR-based timers
1212
Therefore, their executions are not blocked by bad-behaving functions / tasks.
1313
This important feature is absolutely necessary for mission-critical tasks.
14-
15-
Version: 1.0.0
16-
17-
Version Modified By Date Comments
18-
------- ----------- ---------- -----------
19-
1.0.0 K.Hoang 25/09/2021 Initial coding for nRF52-based boards using Arduino mbed_nano core, such as Nano_33_BLE
2014
*****************************************************************************************************************************/
2115

2216
#if !( ARDUINO_ARCH_NRF52840 && TARGET_NAME == ARDUINO_NANO33BLE )
@@ -77,19 +71,19 @@ void TimerHandler()
7771
uint32_t PWM_Pin = LED_BUILTIN;
7872

7973
// You can assign any interval for any timer here, in Hz
80-
double PWM_Freq1 = 1.0f;
74+
float PWM_Freq1 = 1.0f;
8175
// You can assign any interval for any timer here, in Hz
82-
double PWM_Freq2 = 2.0f;
76+
float PWM_Freq2 = 2.0f;
8377

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

8983
// You can assign any duty_cycle for any PWM here, from 0-100
90-
uint32_t PWM_DutyCycle1 = 50;
84+
float PWM_DutyCycle1 = 50.0;
9185
// You can assign any duty_cycle for any PWM here, from 0-100
92-
uint32_t PWM_DutyCycle2 = 90;
86+
float PWM_DutyCycle2 = 90.0;
9387

9488
// Channel number used to identify associated channel
9589
int channelNum;

examples/ISR_Modify_PWM/ISR_Modify_PWM.ino

+4-10
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,6 @@
1111
The accuracy is nearly perfect compared to software timers. The most important feature is they're ISR-based timers
1212
Therefore, their executions are not blocked by bad-behaving functions / tasks.
1313
This important feature is absolutely necessary for mission-critical tasks.
14-
15-
Version: 1.0.0
16-
17-
Version Modified By Date Comments
18-
------- ----------- ---------- -----------
19-
1.0.0 K.Hoang 25/09/2021 Initial coding for nRF52-based boards using Arduino mbed_nano core, such as Nano_33_BLE
2014
*****************************************************************************************************************************/
2115

2216
#if !( ARDUINO_ARCH_NRF52840 && TARGET_NAME == ARDUINO_NANO33BLE )
@@ -77,19 +71,19 @@ void TimerHandler()
7771
uint32_t PWM_Pin = LED_BUILTIN;
7872

7973
// You can assign any interval for any timer here, in Hz
80-
double PWM_Freq1 = 1.0f;
74+
float PWM_Freq1 = 1.0f;
8175
// You can assign any interval for any timer here, in Hz
82-
double PWM_Freq2 = 2.0f;
76+
float PWM_Freq2 = 2.0f;
8377

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

8983
// You can assign any duty_cycle for any PWM here, from 0-100
90-
uint32_t PWM_DutyCycle1 = 10;
84+
float PWM_DutyCycle1 = 50.0;
9185
// You can assign any duty_cycle for any PWM here, from 0-100
92-
uint32_t PWM_DutyCycle2 = 90;
86+
float PWM_DutyCycle2 = 90.0;
9387

9488
// Channel number used to identify associated channel
9589
int channelNum;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/****************************************************************************************************************************
2+
multiFileProject.cpp
3+
4+
For nRF52-based boards using Arduino mbed_nano core, such as Nano_33_BLE
5+
Written by Khoi Hoang
6+
7+
Built by Khoi Hoang https://github.com/khoih-prog/nRF52_MBED_Slow_PWM
8+
Licensed under MIT license
9+
*****************************************************************************************************************************/
10+
11+
// To demo how to include files in multi-file Projects
12+
13+
#include "multiFileProject.h"

0 commit comments

Comments
 (0)