Skip to content

Commit

Permalink
v0.4.0rc0
Browse files Browse the repository at this point in the history
  • Loading branch information
nerdralph committed Jul 15, 2020
1 parent c939a30 commit aaf8a32
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 42 deletions.
18 changes: 18 additions & 0 deletions src/ArduinoShrink.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
5 changes: 5 additions & 0 deletions src/macros.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

.macro GLABEL name
.global \name
\name:
.endm
9 changes: 4 additions & 5 deletions src/micros.S
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
30 changes: 5 additions & 25 deletions src/millis.S
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions src/millis.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ void init_millis()
TCCR0B = _BV(CS01) | _BV(CS00); // div64 prescaler
TIMSK0 = _BV(TOIE0); // enable overflow interrupt
}

17 changes: 5 additions & 12 deletions src/t0_ovfl_isr.S
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@
; ArduinoShrink

#include <avr/io.h>
#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
Expand All @@ -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

0 comments on commit aaf8a32

Please sign in to comment.