Nicest way to toggle a port asymmetrically

All technical discussions and projects around startKIT
Post Reply
User avatar
tuck1s
Active Member
Posts: 32
Joined: Thu Sep 25, 2014 1:19 am

Nicest way to toggle a port asymmetrically

Post 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).


User avatar
Ross
XCore Expert
Posts: 962
Joined: Thu Dec 10, 2009 9:20 pm
Location: Bristol, UK

Post 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.
User avatar
tuck1s
Active Member
Posts: 32
Joined: Thu Sep 25, 2014 1:19 am

Post 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?
will1979
Member
Posts: 12
Joined: Mon Oct 26, 2015 11:06 am

Post 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.
Post Reply