]> cloudbase.mooo.com Git - avrcpm.git/blobdiff - avr/timer.asm
SVN --> GIT
[avrcpm.git] / avr / timer.asm
index d6c02c39e198f86ec730da30a8b931491ac31007..e64d0811b14fbda80db04d71447544fcc1d93b75 100644 (file)
@@ -17,7 +17,7 @@
 ;    You should have received a copy of the GNU General Public License
 ;    along with avrcpm.  If not, see <http://www.gnu.org/licenses/>.
 ;
-;    $Id$
+;    $Id: timer.asm 153 2014-11-12 12:59:42Z rapid $
 ;
 
        .dseg
@@ -40,8 +40,8 @@ timer_sec:
 ; don't change order here, clock put/get depends on it.
 cntms_out:             ; register for ms
        .byte   2
-utime_io:              ; register for uptime. 
-       .byte   4       
+utime_io:              ; register for uptime.
+       .byte   4
 cnt_1ms:
 .equ ot_1ms    = cnt_1ms-timer_var
        .byte   2
@@ -50,10 +50,10 @@ uptime:
        .byte   4
 timer_top:
 .equ timer_size        = timer_top - timer_base
-       
+
 .equ utofs     = cnt_1ms-cntms_out
 .equ timerofs  = cnt_1ms-timer_ms
+
 clk_out:
 .equ oclk_out  = clk_out-timer_var
        .byte   7       ;
@@ -62,28 +62,28 @@ clock:
        .byte   7       ;Format (bin): s m h D M YY
 .equ clkofs    = clock-clk_out
 
-       .cseg   
+       .cseg
 
 ; ------------- system timer 1ms ---------------
 
 
 ; Timer/Counter1 Compare Match B interrupt
-       
+
        INTERRUPT OC1Baddr
 
 .if TIMER_DEBUG
        cbi     PORTC,5
-.endif 
+.endif
        push    zl
        in      zl,SREG
        push    zl
        push    zh
        inm8    zl,OCR1BL
        inm8    zh,OCR1BH
-       addiw   z,F_CPU/1000
+       addiw   z,TC_1MS
        outm8   OCR1BH,zh
        outm8   OCR1BL,zl
-       
+
        push    yl
        push    yh
        ldiw    y,timer_var
@@ -93,7 +93,7 @@ clock:
        subi    zl,1
        brcs    syscl0                          ;timer was 0 before (not running)
        sts     srx_char_to,zl                  ;timer is running, store new value
-       brne    syscl0                          
+       brne    syscl0
        rcall   srx_to
 syscl0:
 #endif
@@ -101,7 +101,7 @@ syscl0:
        subi    zl,1
        brcs    syscl_t1n
        std     y+ot_timer1,zl
-syscl_t1n:     
+syscl_t1n:
        ldd     zl,y+ot_timer2
        subi    zl,1
        brcs    syscl_t2n
@@ -169,7 +169,7 @@ syscl_clk_date:
        ldd     zl,y+o_clock+5          ;year
        andi    zl,0x03
        brne    syscl_clknl
-       inc     zh                      ;leap year 
+       inc     zh                      ;leap year
 syscl_clknl:
        ldd     zl,y+o_clock+3          ;day
        inc     zl
@@ -201,7 +201,7 @@ syscl_end:
        pop     zl
 .if TIMER_DEBUG
        sbi     PORTC,5
-.endif 
+.endif
        reti
 
 ; days per month
@@ -225,18 +225,18 @@ dly_loop:
        ret
 
 ; ----------------------------------------------
-; 
+;
 clockget:
        ldiw    z,clk_out
        tst     temp3
        breq    clkget_copy             ;lowest byte requestet, latch clock
-       
+
        add     zl,temp3
        adc     zh,_0
        ld      temp,z
 clkget_end:
        ret
-       
+
 
 clkget_copy:
        ldi     temp3,7
@@ -255,7 +255,7 @@ clkget_l:
        std     z+1,temp2
        ldi     temp3,5
 clkget_l2:
-       ld      temp,-z 
+       ld      temp,-z
        rcall   binbcd2
        st      z,temp
        dec     temp3
@@ -265,7 +265,7 @@ clkget_l2:
        ret                             ;req. byte in temp
 
 ; ----------------------------------------------
-; 
+;
 clockput:
        ldiw    z,clk_out
        add     zl,temp3
@@ -275,12 +275,12 @@ clockput:
        breq    clkput_copy             ;lowest byte stored, latch clock
 clkput_end:
        ret
-               
+
 
 clkput_copy:
        ldi     temp3,5
 clkput_l2:
-       ld      temp,z  
+       ld      temp,z
        rcall   bcdbin2
        st      z+,temp
        dec     temp3
@@ -300,7 +300,7 @@ clkput_l:
        dec     temp3
        brne    clkput_l
        sei
-#if I2C
+#if I2C_SUPPORT
        rcall   rtc_set                 ; set hardware clock
 #endif
        ret
@@ -324,12 +324,11 @@ tobcd_l:
        add     temp,_tmp0
        pop     temp2
        ret
-       
+
 
 binbcd4:
        ldi     temp3,16
-       mov     _tmp0,temp
-       mov     _tmp1,temp2
+       movw    _tmp0,temp
        clr     temp
        clr     temp2
 binbcd4l:
@@ -365,10 +364,10 @@ bcdbin2:
        swap    temp2
        andi    temp2,0x0f
        andi    temp,0x0f               ;temp  = low digit
-       mov     _tmp0,temp2
+       mov     r0,temp2
        ldi     temp2,10
-       mul     temp2,_tmp0             ;high digit * 10
-       add     temp,_tmp0              ;high digit * 10 + low digit
+       mul     temp2,r0                ;high digit * 10
+       add     temp,r0                 ;high digit * 10 + low digit
        pop     temp2
        ret
 
@@ -380,13 +379,13 @@ bcdbin4:
        ldi     temp2,100
        mul     temp,temp2
        pop     temp
-       mov     temp2,_tmp1
-       add     temp,_tmp0
+       mov     temp2,r1
+       add     temp,r0
        adc     temp2,_0
        ret
 
 
-#if I2C
+#if I2C_SUPPORT
 
 ; ----------------------------------------------
 ; Set software clock from hardware clock
@@ -407,20 +406,21 @@ rtc_get:
        rcall   i2c_write
        ldi     temp2,3
        rcall   i2c_read                ;get year (stored in RTC-RAM addr. 10h)
-       tst     temp
-       brmi    rtc_get_e               ;i2c error
+       andi    temp,0x3
+       breq    rtc_get_e               ;i2c error
 
        ldd     temp3,z+1               ;save year
-       ldd     temp4,z+2
+       ldd     xl,   z+2
 
        ldi     temp2,2                 ;register pointer. 2 = secs
        std     z+1,temp2
        rcall   i2c_write
        ldi     temp2,6
        rcall   i2c_read
-       tst     temp
-       brmi    rtc_get_e
+       andi    temp,0x3
+       breq    rtc_get_e               ;i2c error
 
+       mov     temp2,xl                ;year century
        ldd     temp,z+4                ;get year
        rol     temp
        rol     temp
@@ -428,8 +428,8 @@ rtc_get:
        eor     temp,temp3
        andi    temp,0x03
        breq    rtc_get_1
-       inc     temp3
-       adc     temp4,_0
+       subi    temp3, low(-1)
+       sbci    temp2, high(-1)
 rtc_get_1:
        ldiw    x,clock
        cli
@@ -452,7 +452,7 @@ rtc_get_1:
        rcall   bcdbin2
        st      x+,temp                 ;store month
        st      x+,temp3                ;store year
-       st      x+,temp4                ;store year century
+       st      x+,temp2                ;store year century
        sei
 
 rtc_get_e:
@@ -466,10 +466,10 @@ rtc_get_e:
 
 ;----------------------------------------------
 ; Set hardware clock from software clock
-; 
+;
 ; Register:    temp2:  s
 ;              temp3:  m
-;              temp4:  h
+;              xh:     h
 ;              xl:     D
 ;              temp:   M
 ;              yl:     Yl
@@ -480,7 +480,7 @@ rtc_set:
        cli
        ldd     temp2,z+0               ;sec
        ldd     temp3,z+1               ;min
-       ldd     temp4,z+2               ;hours
+       ldd     xh,z+2                  ;hours
        ldd     xl,z+3                  ;day
        ldd     temp,z+4                ;month
        ldd     yl,z+5                  ;yearl
@@ -497,7 +497,7 @@ rtc_set:
        andi    xl,0xc0
        or      temp,xl                 ;   combine with day
        push    temp                    ;-2 save year/day
-       mov     temp,temp4
+       mov     temp,xh
        rcall   binbcd2
        push    temp                    ;-3 save hours
        mov     temp,temp3
@@ -536,10 +536,10 @@ rtc_set:
        out     sph,zh
 
        ret
-#endif
+#endif /* I2C_SUPPORT */
 
 ; ----------------------------------------------
-; 
+;
 
 utimeget:
        ldi     temp,0xFF
@@ -547,13 +547,13 @@ utimeget:
        subi    temp3,1
        brcs    utimget_end             ;Rel. port number = 0 ? (controlport)
        breq    utimget_copy            ;lowest byte requestet, latch clock
-       
+
        add     zl,temp3
        adc     zh,_0
        ld      temp,z
 utimget_end:
        ret
-       
+
 utimget_copy:
        ldi     temp2,6
        cli
@@ -570,7 +570,7 @@ utimget_l:
 utimeput:
        subi    temp3,1
        brcc    utput__1
-       
+
        ; clock control
 
        cpi     temp,starttimercmd
@@ -583,8 +583,8 @@ utimeput:
        brne    utcp_ex
        rjmp    uptime_print            ;cnt_1ms
 utcp_ex:
-       ret     
-       
+       ret
+
 utput_quit:
        rcall   timer_print
        rjmp    timer_start
@@ -592,12 +592,12 @@ utput_quit:
 utput__1:
        ldiw    z,cntms_out
        breq    utput__copy             ;lowest byte requestet, latch clock
-       
+
        add     zl,temp3
        adc     zh,_0
        st      z,temp
        ret
-               
+
 utput__copy:
        st      z,temp
        adiw    z,5
@@ -628,8 +628,10 @@ ts_loop:
 
 ; print timer
 ;
-       
+
 timer_print:
+       push    r15             ;
+       push    r14             ;
        push    yh
        push    yl
        ldiw    z,timer_ms
@@ -644,9 +646,9 @@ timer_print:
        ld      temp2,z+
        sbc     yh,temp2
        brsh    tp_s
-       
+
        addiw   y,1000
-       sec     
+       sec
 tp_s:
        push    yh
        push    yl
@@ -659,15 +661,15 @@ tp_s:
        ld      yh,z+
        sbc     temp2,yh
 
-       ldd     temp3,z+timerofs
+       ldd     r14,z+timerofs
        ld      yl,z+
-       sbc     temp3,yl
+       sbc     r14,yl
 
        sei
-       ldd     temp4,z+timerofs
+       ldd     r15,z+timerofs
        ld      yh,z+
-       sbc     temp4,yh
-       
+       sbc     r15,yh
+
        printnewline
        printstring "Timer running. Elapsed: "
        rcall   print_ultoa
@@ -675,43 +677,48 @@ tp_s:
        printstring "."
        pop     temp
        pop     temp2
-       ldi     temp3,0
-       ldi     temp4,0
+       clr     r14
+       clr     r15
        rcall   print_ultoa
        printstring "s."
 
        pop     yl
        pop     yh
+       pop     r14
+       pop     r15
        ret
-       
+
 uptime_print:
+       push    r15
+       push    r14
        ldiw    z,cnt_1ms
        cli
        ld      temp,z+
        push    temp
        ld      temp,z+
        push    temp
-       
+
        ld      temp,z+
        ld      temp2,z+
-       ld      temp3,z+
+       ld      r14,z+
        sei
-       ld      temp4,z+
-       
+       ld      r15,z+
+
        printnewline
        printstring "Uptime: "
-       
+
        rcall   print_ultoa
        printstring ","
 
-       ldi     temp3,0
-       ldi     temp4,0
+       clr     r14
+       clr     r15
        pop     temp2
        pop     temp
        rcall print_ultoa
        printstring "s."
 
+       pop     r14
+       pop     r15
        ret
 
 ; vim:set ts=8 noet nowrap
-