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

Commit bb76ba2

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** 2. Add notes `howto upload by drag-and-drop` to `CURIOSITY` virtual drive
1 parent 84639c6 commit bb76ba2

33 files changed

+4388
-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_TimerInterrupt
2+
3+
### Reporting Bugs
4+
5+
Please report bugs in ATtiny_TimerInterrupt 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_TimerInterrupt/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_TimerInterrupt/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_TimerInterrupt/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: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
## ATtiny_TimerInterrupt Library
2+
3+
[![arduino-library-badge](https://www.ardu-badge.com/badge/ATtiny_TimerInterrupt.svg?)](https://www.ardu-badge.com/ATtiny_TimerInterrupt)
4+
[![GitHub release](https://img.shields.io/github/release/khoih-prog/ATtiny_TimerInterrupt.svg)](https://github.com/khoih-prog/ATtiny_TimerInterrupt/releases)
5+
[![GitHub](https://img.shields.io/github/license/mashape/apistatus.svg)](https://github.com/khoih-prog/ATtiny_TimerInterrupt/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_TimerInterrupt.svg)](http://github.com/khoih-prog/ATtiny_TimerInterrupt/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+
### Initial Release v1.0.0
20+
21+
1. Intial release to support Arduino **AVR ATtiny-based boards (ATtiny3217, etc.) using megaTinyCore**
22+
2. Add notes `howto upload by drag-and-drop` to `CURIOSITY` virtual drive
23+
24+
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/****************************************************************************************************************************
2+
Argument_Complex.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_TimerInterrupt
7+
Licensed under MIT license
8+
9+
Now with we can use these new 16 ISR-based timers, while consuming only 1 hwarware Timer.
10+
Their independently-selected, maximum interval is practically unlimited (limited only by unsigned long miliseconds)
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 "megaAVR_TimerInterrupt.h"
24+
// _TIMERINTERRUPT_LOGLEVEL_ from 0 to 4
25+
// Don't define _TIMERINTERRUPT_LOGLEVEL_ > 0. Only for special ISR debugging only. Can hang the system.
26+
#define TIMER_INTERRUPT_DEBUG 0
27+
#define _TIMERINTERRUPT_LOGLEVEL_ 0
28+
29+
// Select USING_FULL_CLOCK == true for 20/16MHz to Timer TCBx => shorter timer, but better accuracy
30+
// Select USING_HALF_CLOCK == true for 10/ 8MHz to Timer TCBx => shorter timer, but better accuracy
31+
// Select USING_250KHZ == true for 250KHz to Timer TCBx => longer timer, but worse accuracy
32+
// Not select for default 250KHz to Timer TCBx => longer timer, but worse accuracy
33+
#define USING_FULL_CLOCK true
34+
#define USING_HALF_CLOCK false
35+
#define USING_250KHZ false // Not supported now
36+
37+
// Try to use RTC, TCA0 or TCD0 for millis()
38+
#define USE_TIMER_0 false // Check if used by millis(), Servo or tone()
39+
#define USE_TIMER_1 true // Check if used by millis(), Servo or tone()
40+
41+
#if USE_TIMER_0
42+
#define CurrentTimer ITimer0
43+
#elif USE_TIMER_1
44+
#define CurrentTimer ITimer1
45+
#else
46+
#error You must select one Timer
47+
#endif
48+
49+
// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error
50+
#include "ATtiny_TimerInterrupt.h"
51+
52+
#ifdef LED_BUILTIN
53+
#undef LED_BUILTIN
54+
55+
// To modify according to your board
56+
// For Curiosity Nano ATtiny3217 => PIN_PA3
57+
#if defined(ARDUINO_AVR_CuriosityNano3217)
58+
#define LED_BUILTIN PIN_PA3
59+
#else
60+
// standard Arduino pin 13
61+
#define LED_BUILTIN PIN_PA3
62+
#endif
63+
#endif
64+
65+
// standard Serial
66+
#define SerialDebug Serial
67+
68+
69+
struct pinStruct
70+
{
71+
unsigned int Pin1;
72+
unsigned int Pin2;
73+
unsigned int Pin3;
74+
};
75+
76+
volatile pinStruct myOutputPins = { LED_BUILTIN, A0, A1 };
77+
78+
void TimerHandler1(unsigned int outputPinsAddress)
79+
{
80+
static bool toggle = false;
81+
82+
//timer interrupt toggles pins
83+
#if (TIMER_INTERRUPT_DEBUG > 1)
84+
SerialDebug.print("Toggle pin1 = "); SerialDebug.println( ((pinStruct *) outputPinsAddress)->Pin1 );
85+
#endif
86+
87+
digitalWrite(((pinStruct *) outputPinsAddress)->Pin1, toggle);
88+
89+
#if (TIMER_INTERRUPT_DEBUG > 1)
90+
SerialDebug.print("Read pin2 A0 ("); SerialDebug.print(((pinStruct *) outputPinsAddress)->Pin2 );
91+
SerialDebug.print(") = ");
92+
SerialDebug.println(digitalRead(((pinStruct *) outputPinsAddress)->Pin2) ? "HIGH" : "LOW" );
93+
94+
SerialDebug.print("Read pin3 A1 ("); SerialDebug.print(((pinStruct *) outputPinsAddress)->Pin3 );
95+
SerialDebug.print(") = ");
96+
SerialDebug.println(digitalRead(((pinStruct *) outputPinsAddress)->Pin3) ? "HIGH" : "LOW" );
97+
#endif
98+
99+
toggle = !toggle;
100+
}
101+
102+
#define TIMER1_INTERVAL_MS 1000
103+
104+
void setup()
105+
{
106+
pinMode(myOutputPins.Pin1, OUTPUT);
107+
pinMode(myOutputPins.Pin2, INPUT_PULLUP);
108+
pinMode(myOutputPins.Pin3, INPUT_PULLUP);
109+
110+
SerialDebug.begin(115200);
111+
while (!SerialDebug && millis() < 5000);
112+
113+
SerialDebug.print(F("\nStarting Argument_Complex on ")); SerialDebug.println(BOARD_NAME);
114+
SerialDebug.println(AT_TINY_TIMER_INTERRUPT_VERSION);
115+
SerialDebug.print(F("CPU Frequency = ")); SerialDebug.print(F_CPU / 1000000); SerialDebug.println(F(" MHz"));
116+
117+
SerialDebug.print(F("TCB Clock Frequency = "));
118+
119+
#if USING_FULL_CLOCK
120+
SerialDebug.println(F("Full clock (20/16MHz, etc) for highest accuracy"));
121+
#elif USING_HALF_CLOCK
122+
SerialDebug.println(F("Half clock (10/8MHz, etc.) for high accuracy"));
123+
#else
124+
SerialDebug.println(F("250KHz for lower accuracy but longer time"));
125+
#endif
126+
127+
// Timer TCB2 is used for micros(), millis(), delay(), etc and can't be used
128+
CurrentTimer.init();
129+
130+
if (CurrentTimer.attachInterruptInterval(TIMER1_INTERVAL_MS, TimerHandler1, (unsigned int) &myOutputPins))
131+
{
132+
SerialDebug.print(F("Starting ITimer OK, millis() = ")); SerialDebug.println(millis());
133+
}
134+
else
135+
SerialDebug.println(F("Can't set ITimer. Select another freq. or timer"));
136+
}
137+
138+
void loop()
139+
{
140+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/****************************************************************************************************************************
2+
Argument_None.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_TimerInterrupt
7+
Licensed under MIT license
8+
9+
Now with we can use these new 16 ISR-based timers, while consuming only 1 hwarware Timer.
10+
Their independently-selected, maximum interval is practically unlimited (limited only by unsigned long miliseconds)
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 "megaAVR_TimerInterrupt.h"
24+
// _TIMERINTERRUPT_LOGLEVEL_ from 0 to 4
25+
// Don't define _TIMERINTERRUPT_LOGLEVEL_ > 0. Only for special ISR debugging only. Can hang the system.
26+
#define TIMER_INTERRUPT_DEBUG 0
27+
#define _TIMERINTERRUPT_LOGLEVEL_ 0
28+
29+
// Select USING_FULL_CLOCK == true for 20/16MHz to Timer TCBx => shorter timer, but better accuracy
30+
// Select USING_HALF_CLOCK == true for 10/ 8MHz to Timer TCBx => shorter timer, but better accuracy
31+
// Select USING_250KHZ == true for 250KHz to Timer TCBx => longer timer, but worse accuracy
32+
// Not select for default 250KHz to Timer TCBx => longer timer, but worse accuracy
33+
#define USING_FULL_CLOCK true
34+
#define USING_HALF_CLOCK false
35+
#define USING_250KHZ false // Not supported now
36+
37+
// Try to use RTC, TCA0 or TCD0 for millis()
38+
#define USE_TIMER_0 false // Check if used by millis(), Servo or tone()
39+
#define USE_TIMER_1 true // Check if used by millis(), Servo or tone()
40+
41+
#if USE_TIMER_0
42+
#define CurrentTimer ITimer0
43+
#elif USE_TIMER_1
44+
#define CurrentTimer ITimer1
45+
#else
46+
#error You must select one Timer
47+
#endif
48+
49+
// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error
50+
#include "ATtiny_TimerInterrupt.h"
51+
52+
#define TIMER1_INTERVAL_MS 1000
53+
54+
#ifdef LED_BUILTIN
55+
#undef LED_BUILTIN
56+
57+
// To modify according to your board
58+
// For Curiosity Nano ATtiny3217 => PIN_PA3
59+
#if defined(ARDUINO_AVR_CuriosityNano3217)
60+
#define LED_BUILTIN PIN_PA3
61+
#else
62+
// standard Arduino pin 13
63+
#define LED_BUILTIN PIN_PA3
64+
#endif
65+
#endif
66+
67+
// standard Serial
68+
#define SerialDebug Serial
69+
70+
void TimerHandler1(void)
71+
{
72+
static bool toggle = false;
73+
74+
//timer interrupt toggles pin LED_BUILTIN
75+
digitalWrite(LED_BUILTIN, toggle);
76+
toggle = !toggle;
77+
}
78+
79+
void setup()
80+
{
81+
pinMode(LED_BUILTIN, OUTPUT);
82+
83+
SerialDebug.begin(115200);
84+
while (!SerialDebug && millis() < 5000);
85+
86+
SerialDebug.print(F("\nStarting Argument_None on ")); SerialDebug.println(BOARD_NAME);
87+
SerialDebug.print(F("CPU Frequency = ")); SerialDebug.print(F_CPU / 1000000); SerialDebug.println(F(" MHz"));
88+
89+
SerialDebug.print(F("TCB Clock Frequency = "));
90+
91+
#if USING_FULL_CLOCK
92+
SerialDebug.println(F("Full clock (20/16MHz, etc) for highest accuracy"));
93+
#elif USING_HALF_CLOCK
94+
SerialDebug.println(F("Half clock (10/8MHz, etc.) for high accuracy"));
95+
#else
96+
SerialDebug.println(F("250KHz for lower accuracy but longer time"));
97+
#endif
98+
99+
CurrentTimer.init();
100+
101+
if (CurrentTimer.attachInterruptInterval(TIMER1_INTERVAL_MS, TimerHandler1))
102+
{
103+
SerialDebug.print(F("Starting ITimer OK, millis() = ")); SerialDebug.println(millis());
104+
}
105+
else
106+
SerialDebug.println(F("Can't set ITimer. Select another freq. or timer"));
107+
}
108+
109+
void loop()
110+
{
111+
}

0 commit comments

Comments
 (0)