Nicest way to toggle a port asymmetrically

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

Nicest way to toggle a port asymmetrically

Postby tuck1s » Sat Sep 19, 2015 8:16 pm

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: 768
Joined: Thu Dec 10, 2009 9:20 pm

Postby Ross » Wed Nov 11, 2015 10:24 pm

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

Postby tuck1s » Sat Nov 14, 2015 11:12 am

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

Postby will1979 » Sun Nov 15, 2015 2:34 pm

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.

Who is online

Users browsing this forum: No registered users and 4 guests