Hi,
01000101 wrote:I've been looking over some of the datasheets for both the PIT and the HPET and I can not figure out how to initialise a timer frequency and (without interrupts) read back a time later one after the clock has been running. What I would like to do is to get a timer to run and then be able to poll the chip for the amount of time passed since start. The only way I know how to and I could find in google was to use an IRQ to update variables and ACk the IRQ, but I would like to do this without any interrupts.
Possible? I think so.
You can read the "current count" from the PIT, but without using an IRQ it'd be very difficult to determine how many times the current count has rolled over and therefore the "current count" value will be mostly useless (unless you're measuring very short lengths of time). The current count rolls over every 55 ms (with 65536 as the reload value) or less (with a smaller reload value), so you'd need to make sure you read the current count more often than that to detect roll over.
In any case here's some code to read the current count from PIT channel 0. I didn't write it (I cut & pasted from a very informative document called "pctim003.txt", that you could search the web for):
Code: Select all
; Function to read the count register (down-counter) of CTC channel zero,
; assuming that the channel is in mode three, with a divisor of 65536.
; Returns the count in up-counter format. Requires an 8254 timer chip.
; Part of the PC Timing FAQ / Application notes
; By K. Heidenstrom ([email protected])
;
_read_timer0_mode3 PROC near ; or FAR for far code model
; unsigned int read_timer0_mode3(void);
pushf ; Keep interrupt flag
mov al,11100010b ; Latch and read back status byte only
cli ; Lock out interrupts
out 43h,al ; Send it
jmp SHORT $+2 ; Delay
in al,40h ; Get status byte
mov ah,al ; To AH
jmp SHORT $+2 ; Delay
mov al,00000000b ; Latch count for timer 0
out 43h,al ; Send it
jmp SHORT $+2 ; Delay
in al,40h ; Get lobyte of count
mov dl,al ; Save in DL
jmp SHORT $+2 ; Delay
in al,40h ; Get hibyte of count
mov dh,al ; Save in DH
jmp SHORT $+2 ; Delay
mov al,11100010b ; Latch and read back status byte again
out 43h,al ; Send it
jmp SHORT $+2 ; Delay
in al,40h ; Get status byte
popf ; Restore interrupt flag
neg dx ; Convert to ascending count
xor al,ah ; Did the output change?
jns GotCount ; If not, no problemo
test dh,dh ; Was count high or low?
js GotCount ; If count was about to carry, keep old
not ah ; If count just carried, change output
GotCount: shl ah,1 ; Get output pin status to CF
cmc ; Pin high = count 0-32767
rcr dx,1 ; Pin low = count 32768-65535
ret ; Return 16-bit ascending count in DX
_read_timer0_mode3 ENDP
If you do use the IRQ to keep track of the number of times the current count has rolled over then you'll have another problem - race conditions. For e.g. you disable IRQs, then the count rolls over, then you latch the count and read it (and end up with a count that doesn't correspond with the "number of times roll over occurred" variable). To avoid this you'd need to read the "number of times roll over occurred" variable, then disable IRQs and read the count and enable IRQs, then check if the "number of times roll over occurred" variable changed (and retry if it did change).
I haven't done anything with HPET yet, so it'd be best to find someone who knows about it (or read the HPET specification yourself to figure it out). In any case I would expect the same problems as you get with the PIT (with a different amount of time before roll over occurs, and a different way to read the current count).
Cheers,
Brendan