Page 1 of 1

Nicest way to toggle a port asymmetrically

Posted: Sat Sep 19, 2015 8:16 pm
by tuck1s
Is the following code to toggle a pin asymmetrically OK?

Code: Select all

void ToggleClock(int count, out port clk)
{
  // Doing this part somewhat slowly has little effect on the overall throughput
  //Clock idle low
  timer t;
  uint32_t time;
  int i;
  int clkState = 0;
  const uint32_t clkHighPeriod = 5;      // units of 10ns
  const uint32_t clkLowPeriod = 50;    

  t :> time;                              // get the initial timer value
  for(i = 2*count; i; i--) {              // send 'count' clock toggles
    select {
      case t when timerafter(time) :> void:     // perform periodic task
        clkState = !clkState;
        clk <: clkState;
        time += (clkState? clkHighPeriod: clkLowPeriod);
        break;
    }
  }
}
So the code generates a short HIGH time and a longer LOW time.
(I'm currently trying to improve the sc_sdcard code 4-bit mode to make more use of timed I/O, so it will be less of a CPU hog).

Re: Nicest way to toggle a port asymmetrically

Posted: Wed Nov 11, 2015 10:24 pm
by Ross
its okay in that it will toggle a pin high and low, there are definitely more efficient and accurate ways of doing it but for slow speed stuff should be okay.

Re: Nicest way to toggle a port asymmetrically

Posted: Sat Nov 14, 2015 11:12 am
by tuck1s
Ross wrote:its okay in that it will toggle a pin high and low, there are definitely more efficient and accurate ways of doing it but for slow speed stuff should be okay.
Could you please post a link to some better code?

Re: Nicest way to toggle a port asymmetrically

Posted: Sun Nov 15, 2015 2:34 pm
by will1979
Try:

Code: Select all

while(1) {
  some_port @ time <: 1;
  time += high_time;
  some_port @ time <: 0;
  time += low_time;
}
The good thing about your code is that your task can continue with other work; by adding cases to the select.