Skip to content

Commit 407c4af

Browse files
committed
new millis ISR equivalent to Arduno
1 parent 121a75e commit 407c4af

File tree

2 files changed

+25
-25
lines changed

2 files changed

+25
-25
lines changed

src/millis.S

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,22 @@
11
; (c) Ralph Doncaster 2020
22
; ArduinoShrink
33

4-
; #include <avr/io.h>
5-
6-
;struct {
7-
; uint32_t t0_ovfl;
8-
; uint8_t ovfl_pad;
9-
;} t0_millis;
4+
#include "macros.inc"
105

116
; to force t0_isr to get linked in
127
.global t0_ovfl_isr
138

14-
.global millis_raw
15-
millis_raw:
9+
; atomically read t0_millis, return in r25:r22 as per avr-gcc ABI
10+
GLABEL millis_impl
1611
ldi ZL, lo8(t0_millis)
1712
ldi ZH, hi8(t0_millis)
18-
1:
13+
_reload:
1914
ld r22, Z
2015
ldd r23, Z+1
2116
ldd r24, Z+2
2217
ldd r25, Z+3
2318
ld r0, Z
2419
cp r0, r22 ; has millis changed?
25-
brne 1b
20+
brne _reload
2621
ret
22+

src/millis.c

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
// (c) Ralph Doncaster 2020
22
// ArduinoShrink
33

4-
#include <Arduino.h>
5-
6-
struct {
7-
volatile uint32_t t0_ovfl;
8-
uint8_t ovfl_pad;
9-
} t0_millis;
4+
//#include <Arduino.h>
5+
#include <avr/io.h>
6+
#include "as_common.h"
107

118
extern uint32_t micros_raw();
129
uint32_t micros()
@@ -23,22 +20,29 @@ uint32_t micros()
2320
return u;
2421
}
2522

26-
// avr-libc math.h does not declare gcc log2 builtin
27-
double log2(double);
23+
extern uint32_t millis_impl();
24+
uint32_t millis()
25+
{
26+
return millis_impl();
27+
}
2828

29-
// portability macros for mega8
30-
#ifndef TCCR0B
31-
#define TCCR0B TCCR0
32-
#define TIMSK0 TIMSK
33-
#endif
29+
#define CPU_MHZ (F_CPU / 1000000)
30+
#define MICROS_PER_T0_OVFL ((T0_PRESCALE * 256) / CPU_MHZ)
31+
#define MILLIS_INC (MICROS_PER_T0_OVFL / 1000)
32+
#define FRACT_INC ((MICROS_PER_T0_OVFL % 1000) >> 2)
3433

3534
__attribute(( section(".init8"), used, naked))
3635
void init_millis()
3736
{
37+
// for millis ISR
38+
asm ( ".global MILLIS_INC\n");
39+
asm ( ".equ MILLIS_INC, %0\n" :: "M" (MILLIS_INC) );
40+
asm ( ".global FRACT_INC\n");
41+
asm ( ".equ FRACT_INC, %0\n" :: "M" (FRACT_INC) );
42+
3843
// scaling factor for t0 vs 16Mhz
3944
uint8_t t0_scale = log2(16000000/F_CPU);
4045
asm ( ".equ T0_SCALE, %0\n" :: "M" (t0_scale) );
41-
TCCR0B = _BV(CS01) | _BV(CS00); // div64 prescaler
42-
TIMSK0 = _BV(TOIE0); // enable overflow interrupt
46+
TIMSK0 = _BV(TOIE0); // enable T0 overflow interrupt
4347
}
4448

0 commit comments

Comments
 (0)