Port counter overflow? Topic is solved

Technical questions regarding the XTC tools and programming with XMOS.
User avatar
sirop
Junior Member
Posts: 5
Joined: Mon Oct 10, 2016 3:53 pm

Port counter overflow?

Post by sirop »

Hello.

Probably, this is not the first question about overflow,
but my search on the forum did not provide me enough certainty about the right anwser...

There is this example from Xmos Programming Guide about "Performing I/O on specific clock edges":

Code: Select all

void do_toggle ( out port p) {
       int count ;
       p <: 0 @ count ; // timestamped output
       while (1) {
           count += 3;
           p @ count <: 1; // timed output
           count += 2;
           p @ count <: 0; // timed output
      }
}
count is declared to be int , but on the other hand side Xmos Programming Guide says
A port has an internal 16-bit counter, which is incremented on each falling edge of
its clock.
Why should count be a 32 bit signed integer instead of unsigned 16 bit short?
Is it because of possible overflow on the 16 bit port counter?

But what happens when the port counter is reset due to overflow?
Will the clocked signal from the above example get a phase shift because of the port counter reset?

Thanks.
View Solution
User avatar
andrew
Verified
Experienced Member
Posts: 117
Joined: Fri Dec 11, 2009 10:22 am

Post by andrew »

The port counters are 16 bit numbers. They are clocked at the rate of the port. A short will be sufficient in place of an int or unsigned but offers little to no advantage as the underlying instruction works on a 32 bit register, however, the upper 16 bits are ignored.

For clocked output the port will do the output when its 16 bit counter equals the lower 16 bits of the assigned output time.

For a timestamped input when the input happens then the lower 16 bits of the variable will be assigned to the ports 16 bit counter.

Hope that helps. Let me know if you need more info.
User avatar
sirop
Junior Member
Posts: 5
Joined: Mon Oct 10, 2016 3:53 pm

Post by sirop »

andrew wrote: For clocked output the port will do the output when its 16 bit counter equals the lower 16 bits of the assigned output time.
Thanks so far.

Let us look at the clocked output.
With 16 bit we can represent (2^16)-1=65535 .
Let the period of the square wave as in the example in my post above be 6000 counts.
Then 10 periods (6000*10=60000) will fit into 16 bit counter.

But what happens at the 11th period?
5535 counts will pass, then the counter will reset itself ( because of overflow) and then after 6000-5535=465 counts the new period of the square wave will begin?
User avatar
andrew
Verified
Experienced Member
Posts: 117
Joined: Fri Dec 11, 2009 10:22 am

Post by andrew »

When the port counter reaches its max value it will wrap around and then continue to increment until it matches the assigned value, in your case:

time output port counter
0 hi 0
6000 lo 6000
12000 hi 12000
18000 lo 18000
24000 hi 24000
30000 lo 30000
36000 hi 36000
42000 lo 42000
48000 hi 48000
54000 lo 54000
60000 hi 60000
66000 lo 464
72000 hi 6464
78000 lo 12464
84000 hi 18464
90000 lo 24464


So if you choose to use an unsigned or a short it doesn't matter as the variable will wrap in exactly the same way as the port counter. The 11th clock will be observed 6000 counts after the one before.