Re: How to produce a fast clock output?

From: Jem Berkes (jb_at_users.pc9.org)
Date: 08/08/03


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;
}



Relevant Pages

  • Re: Mixed clocked/combinatorial coding styles (another thread)
    ... I don't consider my SPI code as finished but I've seen what ... of the SPI clock at the master where it *should* be for a normal SPI system. ... But does this choice force me to, for example, clock the CPU ... If that's not your case, then you've got a wimpy CPU, but in that situation you wouldn't have a clock divider, and the data handling would be done differently. ...
    (comp.lang.vhdl)
  • Re: Stolen and degraded time and schedulers
    ... If you want accurate time accounting, don't use the TSC. ... The adjustments that I spoke of above are working regardless of ntp .. ... as does interrupt latency since the clock is essentially ... CPU does work as a timebase, then using the same warping mechanism would ...
    (Linux-Kernel)
  • Re: IIGS Acceleration Idea
    ... from the ram refresh to the slot timing to the mainboard and slot RAM addressing. ... To speed up the IIgs you'd have to implement a new CYA chip to produce all the timing and address signals for the main board plus the faster clock for the IIgs. ... Replace the CPU with a high speed CPU and simple 20x clock circuit. ... you will have to redesign Apple //e or Apple IIgs motherboard. ...
    (comp.sys.apple2)
  • [PATCH] perf_counter: Prevent oopses from per-cpu software counters
    ... context switches or cpu migrations only makes ... This fixes the problem by disallowing the use of the task clock, ... The only software counter that can be used as a per-cpu counter ...
    (Linux-Kernel)
  • Re: IIGS Acceleration Idea
    ... > and slot RAM addressing. ... > board plus the faster clock for the IIgs. ... Replace the CPU with a high speed CPU and simple 20x clock ...
    (comp.sys.apple2)

Quantcast