//----------------------------------------------------------------------------------
// <copyright file="timer_asm.s" company="LiquidRobotics">
//	Copyright (c) Liquid Robotics Corporation.  All rights reserved.
// </copyright>
//
// <summary>
// 	 Hand-optimized timer routines
// </summary>
//
// <owner>Mike Cookson</owner>
//---------------------------------------------------------------------------------

#include "avr/io.h"

; ===================================================================================
;
; Synopsis :		uint16_t OStmrGetAccumulatedTime(void)
;
; Description :		Reads and then zero's timer/counter 1's count and returns the
;					count to caller.
;
; Arguments : 		n/a
;
; Returns:			The 16-bit timer/counter count prior to zeroing (R25:R24)
; 
; Notes:			1) TIMER.C's OStmrInit() function sets up timer/counter 1; this
;					   function should not be called before this OStmrInit()
;
;					2) This function zeros the timer/counter, so it can only be used
;					   for one purpose at a time (e.g. for measuring the performance
;					   of each task.)
;
; ===================================================================================

	.global OStmrGetAccumulatedTime
	.func	OStmrGetAccumulatedTime
OStmrGetAccumulatedTime:

	in		r20,_SFR_IO_ADDR(SREG)		; read the status register (r0 does not have to be preserved)
	cli									; interrupts off during read
	lds		r24,_SFR_MEM_ADDR(TCNT3L)	; read the LSB
	lds		r25,_SFR_MEM_ADDR(TCNT3H)	; read the MSB
	sts		_SFR_MEM_ADDR(TCNT3H),r1	; zero the MSB
	sts		_SFR_MEM_ADDR(TCNT3L),r1	; zero the LSB
	out		_SFR_IO_ADDR(SREG),r20		; restore the interrupt state
	ret									; return to caller

	.endfunc
