Re: How to produce a fast clock output?
From: Jem Berkes (jb_at_users.pc9.org)
Date: 08/08/03
- Next message: Michael B Allen: "Re: Programming for hardware"
- Previous message: Daihard: "Re: GCC 2.96 and "-march=i686""
- In reply to: Amundsen Zhuang: "Re: How to produce a fast clock output?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Fri, 08 Aug 2003 06:35:09 GMT
>> > How could I produce a clock output that's cycle is 20us?
>>
>> You can't, unless you can gain exclusive use of the CPU. If you
>> did this, the system would no longer work. All UNIX tasks are
>> subject to time-slicing, meaning that you can only use so much
>> time before the system cuts over to another task. During the time
>> that the system is processing these other tasks, your task is
>> suspended.
>
> OK. I needn't keep the clock running continuous. I only want the clock
> running in the time-slice. The clock can be interrupted.
> So, how can I get such a fast clock? By the way, I have to implement
> it on parallel port. I can't use other hardware resource.
> Thanks a lot.
Well at least the hardware should support that clock (20us period = 50
KHz) since the parallel port contains 74LS374 chips as buffers/latches,
which support a maximum clock frequency around 20 MHz.
One thing you might want to try is running a root process that does
direct port I/O. To control the rate you could, um, poll the result from
the "rdtsc" (read time stamp counter) which is an extremely precise CPU
counter on Pentium and newer (including AMDs). The time increments from
RDTSC are 1/(your CPU rate), so with a 1 GHz processor the granularity is
1ns.
The following will work on linux if run by root. I didn't write the
timing parts in there; try this and see what kind of a clock you get out!
RUN AT YOUR OWN RISK!!!
#include <stdio.h>
#include <unistd.h>
#include <asm/io.h>
#include <time.h>
#define BASEPORT 0x378
void control_bit(int bitno, int state) {
unsigned char current;
current = inb(BASEPORT+2); /* read current value */
current &= ~(1 << bitno); /* clear the bit in question */
current |= (state << bitno); /* set this bit's new state */
outb(current, BASEPORT+2); /* write modified value */
}
int main() {
time_t start_time = time(NULL);
if (ioperm(BASEPORT, 3, 1)) {
perror("opening port");
return 1;
}
while (time(NULL) - start_time < 10) {
control_bit(0, 0); /* make fastest possible clock */
control_bit(0, 1); /* for a few seconds */
}
if (ioperm(BASEPORT, 3, 0)) {
perror("releasing port");
return 1;
}
return 0;
}
- Next message: Michael B Allen: "Re: Programming for hardware"
- Previous message: Daihard: "Re: GCC 2.96 and "-march=i686""
- In reply to: Amundsen Zhuang: "Re: How to produce a fast clock output?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|