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

Commit 2472f81

Browse files
authored
v1.0.0 to support AVR ATtiny-based boards
### Initial Release v1.0.0 1. Intial release to support Arduino **AVR ATtiny-based boards (ATtiny3217, etc.) using megaTinyCore**
1 parent 0afeb9d commit 2472f81

27 files changed

+3455
-0
lines changed

CONTRIBUTING.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
## Contributing to ATtiny_Slow_PWM
2+
3+
### Reporting Bugs
4+
5+
Please report bugs in ATtiny_Slow_PWM library if you find them.
6+
7+
However, before reporting a bug please check through the following:
8+
9+
* [Existing Open Issues](https://github.com/khoih-prog/ATtiny_Slow_PWM/issues) - someone might have already encountered this.
10+
11+
If you don't find anything, please [open a new issue](https://github.com/khoih-prog/ATtiny_Slow_PWM/issues/new).
12+
13+
### How to submit a bug report
14+
15+
Please ensure to specify the following:
16+
17+
* Arduino IDE version (e.g. 1.8.19) or Platform.io version
18+
* `megaTinyCore` Core Version (e.g. megaTinyCore core v2.5.11)
19+
* Board (e.g. AVR_CuriosityNano3217, etc.)
20+
* Contextual information (e.g. what you were trying to achieve)
21+
* Simplest possible steps to reproduce
22+
* Anything that might be relevant in your opinion, such as:
23+
* Operating system (Windows, Ubuntu, etc.) and the output of `uname -a`
24+
* Network configuration
25+
26+
27+
### Example
28+
29+
```
30+
Arduino IDE version: 1.8.19
31+
Arduino megaTinyCore core v2.5.11
32+
OS: Ubuntu 20.04 LTS
33+
Board: AVR_CuriosityNano3217
34+
Linux xy-Inspiron-3593 5.15.0-46-generic #49~20.04.1-Ubuntu SMP Thu Aug 4 19:15:44 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
35+
36+
Context:
37+
I encountered a crash while trying to use the Timer Interrupt.
38+
39+
Steps to reproduce:
40+
1. ...
41+
2. ...
42+
3. ...
43+
4. ...
44+
```
45+
### Sending Feature Requests
46+
47+
Feel free to post feature requests. It's helpful if you can explain exactly why the feature would be useful.
48+
49+
There are usually some outstanding feature requests in the [existing issues list](https://github.com/khoih-prog/ATtiny_Slow_PWM/issues?q=is%3Aopen+is%3Aissue+label%3Aenhancement), feel free to add comments to them.
50+
51+
### Sending Pull Requests
52+
53+
Pull Requests with changes and fixes are also welcome!
54+
55+

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 Khoi Hoang
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

changelog.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
## ATtiny_Slow_PWM Library
2+
3+
[![arduino-library-badge](https://www.ardu-badge.com/badge/ATtiny_Slow_PWM.svg?)](https://www.ardu-badge.com/ATtiny_Slow_PWM)
4+
[![GitHub release](https://img.shields.io/github/release/khoih-prog/ATtiny_Slow_PWM.svg)](https://github.com/khoih-prog/ATtiny_Slow_PWM/releases)
5+
[![GitHub](https://img.shields.io/github/license/mashape/apistatus.svg)](https://github.com/khoih-prog/ATtiny_Slow_PWM/blob/main/LICENSE)
6+
[![contributions welcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat)](#Contributing)
7+
[![GitHub issues](https://img.shields.io/github/issues/khoih-prog/ATtiny_Slow_PWM.svg)](http://github.com/khoih-prog/ATtiny_Slow_PWM/issues)
8+
9+
## Table of Contents
10+
11+
* [Changelog](#changelog)
12+
* [Initial Release v1.0.0](#initial-release-v100)
13+
14+
---
15+
---
16+
17+
## Changelog
18+
19+
20+
### Initial Release v1.0.0
21+
22+
1. Intial release to support Arduino **AVR ATtiny-based boards (ATtiny3217, etc.) using megaTinyCore**
23+
24+
25+
Lines changed: 230 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
1+
/****************************************************************************************************************************
2+
ISR_8_PWMs_Array.ino
3+
For Arduino AVR ATtiny-based boards (ATtiny3217, etc.) using megaTinyCore
4+
Written by Khoi Hoang
5+
6+
Built by Khoi Hoang https://github.com/khoih-prog/ATtiny_Slow_PWM
7+
Licensed under MIT license
8+
9+
Now even you use all these new 16 ISR-based timers,with their maximum interval practically unlimited (limited only by
10+
unsigned long miliseconds), you just consume only one AVRDx-based timer and avoid conflicting with other cores' tasks.
11+
The accuracy is nearly perfect compared to software timers. The most important feature is they're ISR-based timers
12+
Therefore, their executions are not blocked by bad-behaving functions / tasks.
13+
This important feature is absolutely necessary for mission-critical tasks.
14+
*****************************************************************************************************************************/
15+
16+
// Important Note: To use drag-and-drop into CURIOSITY virtual drive if you can program via Arduino IDE
17+
// For example, check https://ww1.microchip.com/downloads/en/DeviceDoc/40002193A.pdf
18+
19+
#if !( defined(MEGATINYCORE) )
20+
#error This is designed only for MEGATINYCORE megaAVR board! Please check your Tools->Board setting
21+
#endif
22+
23+
// These define's must be placed at the beginning before #include "ATtiny_Slow_PWM.h"
24+
// _PWM_LOGLEVEL_ from 0 to 4
25+
// Don't define _PWM_LOGLEVEL_ > 0. Only for special ISR debugging only. Can hang the system.
26+
#define _PWM_LOGLEVEL_ 0
27+
28+
// Be careful when using MAX_NUMBER_CHANNELS > 16. Max pemissible MAX_NUMBER_CHANNELS is 64
29+
#define MAX_NUMBER_CHANNELS 16
30+
31+
// Select USING_FULL_CLOCK == true for 20/16MHz to Timer TCBx => shorter timer, but better accuracy
32+
// Select USING_HALF_CLOCK == true for 10/ 8MHz to Timer TCBx => shorter timer, but better accuracy
33+
// Select USING_250KHZ == true for 250KHz to Timer TCBx => longer timer, but worse accuracy
34+
// Not select for default 250KHz to Timer TCBx => longer timer, but worse accuracy
35+
#define USING_FULL_CLOCK true
36+
#define USING_HALF_CLOCK false
37+
#define USING_250KHZ false // Not supported now
38+
39+
// Try to use RTC, TCA0 or TCD0 for millis()
40+
#define USE_TIMER_0 true // Check if used by millis(), Servo or tone()
41+
#define USE_TIMER_1 false // Check if used by millis(), Servo or tone()
42+
43+
#if USE_TIMER_0
44+
#define CurrentTimer ITimer0
45+
#elif USE_TIMER_1
46+
#define CurrentTimer ITimer1
47+
#else
48+
#error You must select one Timer
49+
#endif
50+
51+
#define USING_MICROS_RESOLUTION true //false
52+
53+
// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error
54+
#include "ATtiny_Slow_PWM.h"
55+
56+
#include <SimpleTimer.h> // https://github.com/jfturcot/SimpleTimer
57+
58+
#define LED_OFF HIGH
59+
#define LED_ON LOW
60+
61+
#ifdef LED_BUILTIN
62+
#undef LED_BUILTIN
63+
64+
// To modify according to your board
65+
// For Curiosity Nano ATtiny3217 => PIN_PA3
66+
#if defined(ARDUINO_AVR_CuriosityNano3217)
67+
#define LED_BUILTIN PIN_PA3
68+
#else
69+
// standard Arduino pin 13
70+
#define LED_BUILTIN PIN_PA3
71+
#endif
72+
#endif
73+
74+
#define USING_HW_TIMER_INTERVAL_MS false //true
75+
76+
// Don't change these numbers to make higher Timer freq. System can hang
77+
#define HW_TIMER_INTERVAL_MS 0.0333f
78+
#define HW_TIMER_INTERVAL_FREQ 30000L
79+
80+
volatile uint32_t startMicros = 0;
81+
82+
// Init AT_TINY_SLOW_PWM, each can service max 64 different ISR-based PWM channels
83+
AT_TINY_SLOW_PWM_ISR ISR_PWM;
84+
85+
//////////////////////////////////////////////////////
86+
87+
void TimerHandler()
88+
{
89+
ISR_PWM.run();
90+
}
91+
92+
//////////////////////////////////////////////////////
93+
94+
#define PIN_D0 0
95+
#define PIN_D1 1
96+
#define PIN_D2 2
97+
#define PIN_D3 3
98+
#define PIN_D4 4
99+
#define PIN_D5 5
100+
#define PIN_D6 6
101+
102+
//////////////////////////////////////////////////////
103+
104+
// You can assign pins here. Be careful to select good pin to use or crash, e.g pin 6-11
105+
uint32_t PWM_Pin[] =
106+
{
107+
LED_BUILTIN, PIN_D0, PIN_D1, PIN_D2, PIN_D3, PIN_D4, PIN_D5, PIN_D6
108+
};
109+
110+
#define NUMBER_ISR_PWMS ( sizeof(PWM_Pin) / sizeof(uint32_t) )
111+
112+
// You can assign any interval for any timer here, in Hz
113+
float PWM_Freq[] =
114+
{
115+
1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f,
116+
};
117+
118+
// You can assign any interval for any timer here, in Microseconds
119+
float PWM_DutyCycle[] =
120+
{
121+
5.0, 10.0, 20.0, 30.0, 40.0, 45.0, 50.0, 55.0
122+
};
123+
124+
typedef void (*irqCallback) ();
125+
126+
127+
// In AVRDx, avoid doing something fancy in ISR, for example complex Serial.print with String() argument
128+
// The pure simple Serial.prints here are just for demonstration and testing. Must be eliminate in working environment
129+
// Or you can get this run-time error / crash
130+
void doingSomething0()
131+
{
132+
}
133+
134+
void doingSomething1()
135+
{
136+
}
137+
138+
void doingSomething2()
139+
{
140+
}
141+
142+
void doingSomething3()
143+
{
144+
}
145+
146+
void doingSomething4()
147+
{
148+
}
149+
150+
void doingSomething5()
151+
{
152+
}
153+
154+
void doingSomething6()
155+
{
156+
}
157+
158+
void doingSomething7()
159+
{
160+
}
161+
162+
163+
irqCallback irqCallbackStartFunc[] =
164+
{
165+
doingSomething0, doingSomething1, doingSomething2, doingSomething3,
166+
doingSomething4, doingSomething5, doingSomething6, doingSomething7
167+
};
168+
169+
////////////////////////////////////////////////
170+
171+
void setup()
172+
{
173+
Serial.begin(115200);
174+
while (!Serial && millis() < 5000);
175+
176+
Serial.print(F("\nStarting ISR_8_PWMs_Array on ")); Serial.println(BOARD_NAME);
177+
Serial.println(AT_TINY_SLOW_PWM_VERSION);
178+
Serial.print(F("CPU Frequency = ")); Serial.print(F_CPU / 1000000); Serial.println(F(" MHz"));
179+
Serial.print(F("Max number PWM channels = ")); Serial.println(MAX_NUMBER_CHANNELS);
180+
181+
Serial.print(F("TCB Clock Frequency = "));
182+
183+
#if USING_FULL_CLOCK
184+
Serial.println(F("Full clock (20/16MHz, etc) for highest accuracy"));
185+
#elif USING_HALF_CLOCK
186+
Serial.println(F("Half clock (10/8MHz, etc.) for high accuracy"));
187+
#else
188+
Serial.println(F("250KHz for lower accuracy but longer time"));
189+
#endif
190+
191+
#if USING_HW_TIMER_INTERVAL_MS
192+
193+
CurrentTimer.init();
194+
195+
if (CurrentTimer.attachInterruptInterval(HW_TIMER_INTERVAL_MS, TimerHandler))
196+
{
197+
Serial.print(F("Starting ITimer OK, micros() = ")); Serial.println(micros());
198+
}
199+
else
200+
Serial.println(F("Can't set ITimer. Select another freq. or timer"));
201+
202+
#else
203+
204+
CurrentTimer.init();
205+
206+
if (CurrentTimer.attachInterrupt(HW_TIMER_INTERVAL_FREQ, TimerHandler))
207+
{
208+
Serial.print(F("Starting ITimer OK, micros() = ")); Serial.println(micros());
209+
}
210+
else
211+
Serial.println(F("Can't set ITimer. Select another freq. or timer"));
212+
213+
#endif // USING_HW_TIMER_INTERVAL_MS
214+
215+
216+
// Just to demonstrate, don't use too many ISR Timers if not absolutely necessary
217+
// You can use up to 16 timer for each ISR_PWM
218+
for (uint16_t i = 0; i < NUMBER_ISR_PWMS; i++)
219+
{
220+
//void setPWM(uint32_t pin, float frequency, float dutycycle
221+
// , timer_callback_p StartCallback = nullptr, timer_callback_p StopCallback = nullptr)
222+
223+
// You can use this with PWM_Freq in Hz
224+
ISR_PWM.setPWM(PWM_Pin[i], PWM_Freq[i], PWM_DutyCycle[i], irqCallbackStartFunc[i]);
225+
}
226+
}
227+
228+
void loop()
229+
{
230+
}

0 commit comments

Comments
 (0)