From aaf8a3282ddcdef79c5ba224093feb0720df160c Mon Sep 17 00:00:00 2001 From: Nerd Ralph Date: Tue, 14 Jul 2020 21:46:50 -0300 Subject: [PATCH] v0.4.0rc0 --- src/ArduinoShrink.h | 18 ++++++++++++++++++ src/macros.inc | 5 +++++ src/micros.S | 9 ++++----- src/millis.S | 30 +++++------------------------- src/millis.c | 1 + src/t0_ovfl_isr.S | 17 +++++------------ 6 files changed, 38 insertions(+), 42 deletions(-) create mode 100644 src/macros.inc diff --git a/src/ArduinoShrink.h b/src/ArduinoShrink.h index fee8a86..81fc9a1 100644 --- a/src/ArduinoShrink.h +++ b/src/ArduinoShrink.h @@ -12,3 +12,21 @@ inline void delayMicroseconds(unsigned int us) _delay_us(us); } +extern "C" uint32_t micros_raw(); +inline uint32_t micros() +{ + /* + register uint32_t micros_ asm("r22"); + asm ("call micros_raw" : "=r" (micros_) ); + return micros_; + */ + // asm volatile ("call micros_raw"); + return micros_raw() * 4; +} + +extern "C" uint32_t millis_raw(); +inline uint32_t millis() +{ + uint32_t m = millis_raw(); + return m + ((m * 1573) >> 16); +} diff --git a/src/macros.inc b/src/macros.inc new file mode 100644 index 0000000..f101d1c --- /dev/null +++ b/src/macros.inc @@ -0,0 +1,5 @@ + +.macro GLABEL name + .global \name + \name: +.endm diff --git a/src/micros.S b/src/micros.S index 7987101..dd05ac2 100644 --- a/src/micros.S +++ b/src/micros.S @@ -11,10 +11,11 @@ ;} t0_millis; -;.global lsl4_r22_r30 +; to force t0_isr to get linked in +.global t0_ovfl_isr -.global micros -micros: +.global micros_raw +micros_raw: ldi ZL, lo8(t0_millis) ldi ZH, hi8(t0_millis) 1: @@ -25,6 +26,4 @@ micros: in r0, TCNT0 sub r0, r22 ; TCNT0 overflow? brcs 1b - ldi r30, 2 ; multiply by micros/tick - rcall lsl4_r22_r30 ret diff --git a/src/millis.S b/src/millis.S index ba48b46..2b48454 100644 --- a/src/millis.S +++ b/src/millis.S @@ -9,38 +9,18 @@ ;} t0_millis; ; to force t0_isr to get linked in -.global lsl4_r22_r20 +.global t0_ovfl_isr -.global millis -millis: +.global millis_raw +millis_raw: ldi ZL, lo8(t0_millis) ldi ZH, hi8(t0_millis) +1: ld r22, Z ldd r23, Z+1 ldd r24, Z+2 ldd r25, Z+3 ld r0, Z cp r0, r22 ; has millis changed? - brne millis - ; millis = t0_ovfl * 1.024 , so multiply by fractional addition - movw r18, r22 - movw r20, r24 - rcall shift_add ; adds t0_ovfl / 128 - rcall shift_add ; adds t0_ovfl / 64 - ; rcall 1f - ; todo: add t0_ovfl / 2048 -1: add r18, r24 ; adds t0_ovfl / 16384 - adc r19, r25 - adc r20, r1 - movw r22, r18 - movw r24, r20 -ret - -shift_add: - ldi r30, 1 - rcall lsl4_r22_r30 - add r18, r23 - adc r19, r24 - adc r20, r25 - adc r21, r1 + brne 1b ret diff --git a/src/millis.c b/src/millis.c index 7c0fdf1..17128a2 100644 --- a/src/millis.c +++ b/src/millis.c @@ -20,3 +20,4 @@ void init_millis() TCCR0B = _BV(CS01) | _BV(CS00); // div64 prescaler TIMSK0 = _BV(TOIE0); // enable overflow interrupt } + diff --git a/src/t0_ovfl_isr.S b/src/t0_ovfl_isr.S index 3b64074..18f35df 100644 --- a/src/t0_ovfl_isr.S +++ b/src/t0_ovfl_isr.S @@ -2,14 +2,17 @@ ; ArduinoShrink #include +#include "macros.inc" ;struct { ; uint32_t t0_ovfl; ; uint8_t ovfl_pad; ;} t0_millis; -.global TIMER0_OVF_vect -TIMER0_OVF_vect: +; to help with linking +GLABEL t0_ovfl_isr + +GLABEL TIMER0_OVF_vect push r16 in r16, SREG-0x20 push r16 @@ -29,13 +32,3 @@ t0_ovfl_inc: pop r16 reti -; shift left r22-r25 (ulong), arg in r30 -.global lsl4_r22_r30 -lsl4_r22_r30: - lsl r22 - rol r23 - rol r24 - rol r25 - dec r30 - brne lsl4_r22_r30 -ret