Skip to content

Commit 0753318

Browse files
committed
Implement _getCurrentMillisTimer();
1 parent 632b2fc commit 0753318

File tree

8 files changed

+87
-44
lines changed

8 files changed

+87
-44
lines changed

ChangeLog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ Changes listed here are checked in to GitHub ("master" branch unless specificall
2424

2525
### Planned 2.6.11
2626
* Pending: New toolchain version.
27+
* Make wiring.c have the functions referred to in the doc.
28+
* Fix some of the constants for timers so that all timers can still get uniform codes specifying the portmux and (for non-TCA's) which pin within the mux it is, which matters forthe other core.
2729
* Documentation improvements.
2830
* Corrected bug sometimes encountered when using serial under atypical cases the causes of which remain mysterious), where it would complain about `__poll_dre_done`.
2931
* Correct bug with Comparator (#1236)

megaavr/cores/megatinycore/Arduino.h

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ void initVariant();
314314
void takeOverTCA0();
315315
void takeOverTCD0();
316316

317+
uint8_t getCurrentMillisTimer();
317318

318319

319320

@@ -515,7 +516,7 @@ uint32_t microsecondsToMillisClockCycles(uint32_t microseconds);
515516
#define TIMERB6 (0x26) // TCB6
516517
#define TIMERB7 (0x27) // TCB7
517518
#define TIMERD0 (0x40) // If any of these bits match it's potentially on TCD0
518-
#define DACOUT (0x80) /// If the high bit is set, it;s either the DAC oone of the new timers.
519+
#define DACOUT (0xF8) /// If the high bit is set, it;s either the DAC oone of the new timers.
519520
#define TIMERE0 (0x10) // I do not expect TCE and TCA to ever coexist on the same chip. TCE comes with WEX and the need for the PLL. Would they put TCA's on a part that had the infrastructre for TCEs? Doubtful
520521
#define TIMERE1 (0x08)
521522
#define TIMERF0 (0xA0)
@@ -524,16 +525,17 @@ uint32_t microsecondsToMillisClockCycles(uint32_t microseconds);
524525
* For the millis timer, there's nothing weird here.
525526
* But the timer table constants contain more information than that for these. When user code interprets the timer table entries it is critical to do it right:
526527
* 1. If 0x40 is set, TCD0 can output here. bits 4 and 5 contain information on what channel, and bits 0-2 specify what the PORTMUX must be set to.
527-
* 2. If 0x20 us set, there is a TCB can output PWM there.
528+
* 2. If there's a TCE/TCF, if 0x90 is set, it's TCE0, 0xA0 is TCE1,
529+
* 2. If 0x20 is set, there is a TCB can output PWM there.
528530
* 2a. If 0x20 is set, check 0x10 - if that's set, it's the alt pin mapping. This is currently not returned by the table, and I assess it to be unlikely to be of use
529531
* 4. If 0x10 is set, it's a TCA0 pin. This is never used in the timer table, but digitalPinToTimerNow() can return it.
530532
* 5. If 0x08 is set, it's a TCA1 pin. This is never used in the timer table, but digitalPinToTimerNow() can return it.
531533
* Ergo, use bitwise ands
532534
*/
533535

534-
#define TIMERRTC (0x90) // RTC with internal osc
535-
#define TIMERRTC_XTAL (0x91) // RTC with crystal
536-
#define TIMERRTC_CLK (0x92) // RTC with ext clock
536+
#define TIMERRTC (0xF0) // RTC with internal osc
537+
#define TIMERRTC_XTAL (0xF1) // RTC with crystal
538+
#define TIMERRTC_CLK (0xF2) // RTC with ext clock
537539

538540
#if !defined(MEGATINYCORE)
539541
/* Not used in table */
@@ -608,16 +610,24 @@ uint32_t microsecondsToMillisClockCycles(uint32_t microseconds);
608610
#define TIMERD0_7WOB (0x57) // hypothetical TCD0 WOB ALT7
609611
#define TIMERD0_7WOC (0x67) // hypothetical TCD0 WOC ALT7
610612
#define TIMERD0_7WOD (0x77) // hypothetical TCD0 WOD ALT7
611-
612-
613-
#define TIMERE0_MUX0 (0x90) // TCE/WEX mux
614-
#define TIMERE0_MUX1 (0x91) // TCE/WEX mux
615-
#define TIMERE0_MUX2 (0x92) // TCE/WEX mux
616-
#define TIMERE0_MUX3 (0x93) // TCE/WEX mux
617-
#define TIMERE0_MUX4 (0x94) // TCE/WEX mux
618-
#define TIMERE0_MUX5 (0x95) // TCE/WEX mux
619-
#define TIMERE0_MUX6 (0x96) // Hypothetical TCE/WEX mux
620-
#define TIMERE0_MUX7 (0x97) // Hypothetical TCE/WEX mux
613+
/* 0x01xx 1xxx for potential futyre TCD1 */
614+
615+
#define TIMERE0_MUX0 (0x90) // TCE/WEX mux PORTA
616+
#define TIMERE0_MUX1 (0x91) // TCE/WEX mux PORTB (TBA)
617+
#define TIMERE0_MUX2 (0x92) // TCE/WEX mux PORTC
618+
#define TIMERE0_MUX3 (0x93) // TCE/WEX mux PORTD
619+
#define TIMERE0_MUX4 (0x94) // TCE/WEX mux PORTE
620+
#define TIMERE0_MUX5 (0x95) // TCE/WEX mux PORTF
621+
#define TIMERE0_MUX6 (0x96) // TCE/WEX mux PORTG (TBA)
622+
#define TIMERE0_MUX7 (0x97) // TCE/WEX mux (TBA)
623+
#define TIMERE0_MUX8 (0x98) // TCE/WEX mux PORTA2
624+
#define TIMERE0_MUX9 (0x99) // TCE/WEX mux PORTC2
625+
#define TIMERE0_MUX10 (0x9A) // TCE/WEX mux
626+
#define TIMERE0_MUX11 (0x9B) // TCE/WEX mux
627+
#define TIMERE0_MUX12 (0x9C) // TCE/WEX mux
628+
#define TIMERE0_MUX13 (0x9D) // TCE/WEX mux
629+
#define TIMERE0_MUX14 (0x9E) // TCE/WEX mux
630+
#define TIMERE0_MUX15 (0x9F) // TCE/WEX mux
621631
/*
622632
// They might make a chip with 2 of them!
623633
#define TIMERE1_MUX0 (0xA0) // Hypothetical TCE/WEX mux
@@ -683,6 +693,13 @@ uint32_t microsecondsToMillisClockCycles(uint32_t microseconds);
683693
*/
684694

685695
#endif
696+
697+
#define MILLIS_RUNNING (0x00)
698+
#define MILLIS_PAUSED (0x01)
699+
#define MILLIS_ON_ALT_TIMER (0x02)
700+
701+
702+
686703
__attribute__ ((noinline)) void _delayMicroseconds(unsigned int us);
687704

688705

megaavr/cores/megatinycore/wiring.c

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ void init_timers();
4747
*/
4848
uint8_t __PeripheralControl = 0xFF;
4949

50+
uint8_t __MillisState = 0xFF;
51+
5052
// the prescaler is set so that timer ticks every 64 clock cycles, and the
5153
// the overflow handler is called every 256 ticks.
5254

@@ -1327,6 +1329,7 @@ void stop_millis()
13271329
#else
13281330
_timer->INTCTRL &= ~TCB_CAPT_bm;
13291331
#endif
1332+
__MillisState == 0x01;
13301333
#endif
13311334
}
13321335

@@ -1369,10 +1372,21 @@ void restart_millis()
13691372
}
13701373

13711374

1375+
uint8_t getCurrentMillisTimer() {
1376+
#if defined(MILLIS_USE_TIMERNONE)
1377+
return NOT_ON_TIMER;
1378+
#elif !defined(MILLIS_TIMER)
1379+
badCall("Can't happen! Millis defines busted");
1380+
#else
1381+
if (!__MillisState) {
1382+
return MILLIS_TIMER;
1383+
} else {
1384+
return __MillisState;
1385+
}
1386+
#endif
1387+
}
13721388

1373-
1374-
void __attribute__((weak)) init_millis()
1375-
{
1389+
void __attribute__((weak)) init_millis() {
13761390
#if defined(MILLIS_USE_TIMERNONE)
13771391
badCall("init_millis() is only valid with millis time keeping enabled.");
13781392
#else
@@ -1393,8 +1407,7 @@ void __attribute__((weak)) init_millis()
13931407
pTCD->INTCTRL = 0x01; // enable interrupt
13941408
pTCD->CTRLA = TIMERD0_PRESCALER | 0x01; // set clock source and enable!
13951409
#elif defined(MILLIS_USE_TIMERRTC)
1396-
while(RTC.STATUS); // if RTC is currently busy, spin until it's not.
1397-
// to do: add support for RTC timer initialization
1410+
while(RTC.STATUS);
13981411
RTC.PER = 0xFFFF;
13991412
#ifdef MILLIS_USE_TIMERRTC_XTAL
14001413
_PROTECTED_WRITE(CLKCTRL.XOSC32KCTRLA,0x03);
@@ -1414,11 +1427,11 @@ void __attribute__((weak)) init_millis()
14141427
// CLK_PER/1 is 0b00, . CLK_PER/2 is 0b01, so bitwise OR of valid divider with enable works
14151428
_timer->CTRLA = TIME_TRACKING_TIMER_DIVIDER|TCB_ENABLE_bm; // Keep this last before enabling interrupts to ensure tracking as accurate as possible
14161429
#endif
1430+
__MillisState == 0x00;
14171431
#endif
14181432
}
14191433

1420-
void set_millis(__attribute__((unused))uint32_t newmillis)
1421-
{
1434+
void set_millis(__attribute__((unused))uint32_t newmillis) {
14221435
#if defined(MILLIS_USE_TIMERNONE)
14231436
badCall("set_millis() is only valid with millis timekeeping enabled.");
14241437
(void)newmillis; // unused parameter
@@ -1663,7 +1676,9 @@ void __attribute__((weak)) init_ADC0() {
16631676
/* Default low latency mode on
16641677
* Users can turn it off if they care about power consumption while ADC is on
16651678
* and chip is awake, since these parts don't have the perverse ADC-left-on
1666-
* behavior of classic AVRs. */
1679+
* behavior of classic AVRs.
1680+
* ... Yes they do afterall. Fudge. Fixed on Ex though.
1681+
*/
16671682
pADC->CTRLC = TIMEBASE_1US; // defined in Arduino.h.
16681683
pADC->PGACTRL = ADC_PGABIASSEL_3_4X_gc | ADC_ADCPGASAMPDUR_15CLK_gc;
16691684
/* Note that we don't *enable* it automatically in init().

megaavr/extras/CompileTestSketches/timing/test_millis_micros/test_millis_micros.ino

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@
1212
void setup() {
1313
/* test manipulation functions */
1414
#ifndef MILLIS_USE_TIMERNONE
15+
GPIOR0 = _getCurrentMillisTimer();
1516
stop_millis();
17+
GPIOR0 = _getCurrentMillisTimer();
1618
set_millis(_SFR_MEM32(0x001C));
1719
restart_millis();
20+
GPIOR0 = _getCurrentMillisTimer();
1821
#endif
1922
}
2023
void loop() {

megaavr/extras/Ref_Defines.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,10 @@ if (MILLIS_TIMER & 0x40) {
6060
} else if (MILLIS_TIMER & 0x80) {
6161
//RTC
6262
}
63+
```
6364

65+
Note also the existence of the run-time function,
6466

65-
```
6667
### Timer identifier interpretation (digitalPinToTimer() return values)
6768
These are 8-bit values. They are fully enumerated and much more detail provided near the end of this document.
6869

megaavr/extras/Ref_Interrupts.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,7 @@ ISR(PERIPHERAL_INT_vect, ISR_NAKED)
356356

357357

358358
## Appendix: Vector Index for tinyAVR
359+
For debugging - "Duplicate of vector 25?! the hell one is that?!"
359360

360361
| Vector Name | T2 | 16k+ | 2-8k |
361362
|-------------------|----|------|------|
@@ -399,3 +400,7 @@ ISR(PERIPHERAL_INT_vect, ISR_NAKED)
399400
| USART1_RXC_vect | 27 | . | . |
400401
| USART1_TXC_vect | 28 | . | . |
401402
| NVMCTRL_EE_vect | 29 | 30 | 25 |
403+
404+
## Why do the few pieces of documentation mentioning this speak as though it were impossible to give better compile errors?
405+
406+
This is a bit of a puzzler. It appears that the names are "set in stone" when the C runtimes are precompiled. Which is only done for instruction set variants, and so doesn't have the names of the vectors; further, it sounds as though there's no way to get the linker to give better errors. It definitely seems that there is a communictions bottleneck between everything else, and the linker.

megaavr/extras/Ref_Robust.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ BOD will keep the chip in reset when you it knows the voltage is too low to keep
1616
### If you don't use BOD
1717
You need to ask yourself a few hard questions:
1818
* ~if~ When the system is hung in production, how hard/embarrassing will it be to have to run to reset them by hand?
19-
* If it's hard to reset the device (mounted on a 20 meter pole, in the canopy of a tree, in a drainage ditch six feet up, or worst of all, mounted on a 20 meter pole attached to the canopy of a tree growing our of a drainage ditch located at a customer site.), use the watchdog timer!
19+
* If it's hard to reset the device (mounted on a 20 meter pole, in the canopy of a tree, in a drainage ditch six feet up, or worst of all, mounted on a 20 meter pole attached to the canopy of a tree growing out of a drainage ditch located at a customer site.), use the watchdog timer!
2020
* Running a simple program at 20 MHz, lowering voltage until output ceases, and then restoring voltage does NOT reliably restore execution without BOD!
2121

2222
### A bootloader is likely the right approach if you want end users to be uploading updates

megaavr/libraries/Logic/Tricks_and_Tips.md

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -169,25 +169,25 @@ Propagation time (Tp) is in ns, and has been rounded to quarter-nanoseconds to d
169169
Current was also measured using a bench power supply. As the power supply in quetion is known to be of low quality (typical low cost chinese import bench supply). It was deemed sufficiently accurate for this purpose, though its readings were sometimes not reproducible, leading to uncertainty about whether the discrepancy was within the microcontroller or elsewheremember, this is a sample size of 1 tested under conditions governed by expediency, not precision. I did, however, set the cpu speed to 1 MHz to reach lower voltages and to make the control numbers more useful
170170
171171
172-
| Vdd | F(case1) | F(case2) | Tp CCL | Tp EVSYS | I (case1) | I (case2) | I(control) |Notes |
173-
|-------|----------|----------|---------------------|-----------|-----------|------------|----|
174-
| 5.2 | 84.x MHz | 49 MHz | 6.0 | 4.25 | n/t | n/t | n/t | (VUSB, current unmetered) |
175-
| 5.0 | 83.x MHz | 48 MHz | 6.0 | 4.50 | 17 mA | 6 mA | 0 | . |
176-
| 4.5 | 78 MHz | 44 MHz | 6.5 | 5.0 | 12 mA | 6 | 0 | . |
177-
| 4.0 | 71 MHz | 40 MHz | 7.0 | 5.5 | 7.5 mA | 6 | 0 | . |
178-
| 3.5 | 63 MHz | 36 MHz | 8.0 | 6.0 | 6 mA | 6 | 0 | . |
179-
| 3.3 | 59 MHz | 34 MHz | 8.5 | 6.25 | 6 mA | 5 | 0 | . |
180-
| 3.3 | 59 MHz | n/t | 8.5 | n/t | n/t | n/t | n/t | (From 3.3v regulator (LDL1117), current unmetered) |
181-
| 3.0 | 53 MHz | 30.5 MHz | 9.5 | 7.0 | 6 mA | 3 | 0 | . |
182-
| 2.5 | 42 MHz | 24.1 MHz | 12.0 | 8.75 | 4 mA | 0 | 0 | At 20 Mhz, 2.4V no longer works.Raising voltage again does not fix it, power cycle needed. That is not surprising |
183-
| 2.0 | 29 MHz | 16.6 MHz | 17.25 | 13.0 | 0 mA | 0 | 0 | Current readings obviously of |
184-
| 1.9 | 26.0 MHz | 15.0 MHz | 19.25 | 14.0 | 0 mA | 0 | 0 | little value below here. But |
185-
| 1.8 | 23.4 MHz | 13.4 MHz | 21.5 | 16.0 | 0 mA | 0 | 0 | This is the minimum rated voltage for these parts, and the lowest BOD voltage |
186-
| 1.7 | 20.5 MHz | 11.8 MHz | 24.5 | 18.0 | 0 mA | 0 | 0 | . |
187-
| 1.6 | 17.8 MHz | 10.1 MHz | 28.0 | 21.5 | 0 mA | 0 | 0 | . |
188-
| 1.5 | 15.1 MHz | 8.6 MHz | 33.0 | 25.0 | 0 mA | 0 | 0 | . |
189-
| 1.4 | 12.3 MHz | 7.1 MHz | 40.75 | 29.75 | 0 mA | 0 | 0 | At least the CCL kept at it down to 1.4v, before hitting the power on reset threshold at 1.3V |
190-
| 1.3 | 0 MHz | 0 MHz | n/a | n/a | 0 mA | 0 mA | 0 mA | Chip below POR threshold |
172+
| Vdd | F(case1) | F(case2) | Tp CCL | Tp EVSYS | I (case1) | I (case2) | I(control) |Notes
173+
|-------|----------|----------|----------|----------|-----------|-----------|------------|----
174+
| 5.2 | 84.x MHz | 49 MHz | 6.0 | 4.25 | n/t | n/t | n/t | (VUSB, current unmetered)
175+
| 5.0 | 83.x MHz | 48 MHz | 6.0 | 4.50 | 17 mA | 6 mA | 0 | .
176+
| 4.5 | 78 MHz | 44 MHz | 6.5 | 5.0 | 12 mA | 6 | 0 | .
177+
| 4.0 | 71 MHz | 40 MHz | 7.0 | 5.5 | 7.5 mA | 6 | 0 | .
178+
| 3.5 | 63 MHz | 36 MHz | 8.0 | 6.0 | 6 mA | 6 | 0 | .
179+
| 3.3 | 59 MHz | 34 MHz | 8.5 | 6.25 | 6 mA | 5 | 0 | .
180+
| 3.3 | 59 MHz | n/t | 8.5 | n/t | n/t | n/t | n/t | (From 3.3v regulator (LDL1117), current unmetered)
181+
| 3.0 | 53 MHz | 30.5 MHz | 9.5 | 7.0 | 6 mA | 3 | 0 | .
182+
| 2.5 | 42 MHz | 24.1 MHz | 12.0 | 8.75 | 4 mA | 0 | 0 | At 20 Mhz, 2.4V no longer works.Raising voltage again does not fix it, power cycle needed. That is not surprising
183+
| 2.0 | 29 MHz | 16.6 MHz | 17.25 | 13.0 | 0 mA | 0 | 0 | Current readings obviously of
184+
| 1.9 | 26.0 MHz | 15.0 MHz | 19.25 | 14.0 | 0 mA | 0 | 0 | little value below here. But
185+
| 1.8 | 23.4 MHz | 13.4 MHz | 21.5 | 16.0 | 0 mA | 0 | 0 | This is the minimum rated voltage for these parts, and the lowest BOD voltage
186+
| 1.7 | 20.5 MHz | 11.8 MHz | 24.5 | 18.0 | 0 mA | 0 | 0 | .
187+
| 1.6 | 17.8 MHz | 10.1 MHz | 28.0 | 21.5 | 0 mA | 0 | 0 | .
188+
| 1.5 | 15.1 MHz | 8.6 MHz | 33.0 | 25.0 | 0 mA | 0 | 0 | .
189+
| 1.4 | 12.3 MHz | 7.1 MHz | 40.75 | 29.75 | 0 mA | 0 | 0 | At least the CCL kept at it down to 1.4v, before hitting the power on reset threshold at 1.3V
190+
| 1.3 | 0 MHz | 0 MHz | n/a | n/a | 0 mA | 0 mA | 0 mA | Chip below POR threshold
191191
192192
193193
Conclusions:

0 commit comments

Comments
 (0)