Sunday, November 20, 2011

Teenys Clock Trouble

The only time I have had to work on other projects was late a night. My Teensy (an Arduino work-a-like, see previous posts) has been giving me fits. The clock is FAST by about 24 seconds per 6 hours. All previous efforts to correct the problem resulted in No Change.  Jeff - KO7M and I had some time to review my progress (or lack there of) and found that the Teensy does not use the same Clock file (wiring.c) as used by the Arduino (I was modifying the wrong file), in fact it not even named the same under the Teensy directory structure (pins_teensy.c).


void TIMER0_OVF_vect()
{
        asm volatile(
                "push   r24"                            "\n\t"
                "in     r24, __SREG__"                  "\n\t"
                "push   r24"                            "\n\t"

                "lds    r24, timer0_fract_count"        "\n\t"
                "subi   r24, 256 - %0"                  "\n\t"
                "cpi    r24, 125"                       "\n\t"
                "brsh   L_%=_fract_roll"                "\n\t"

        "L_%=_fract_noroll:"                            "\n\t"
                "sts    timer0_fract_count, r24"        "\n\t"
                "lds    r24, timer0_millis_count"       "\n\t"
                "subi   r24, 256 - %1"                  "\n\t"
                "sts    timer0_millis_count, r24"       "\n\t"
                "brcs   L_%=_ovcount"                   "\n\t"

        "L_%=_millis_inc_sext:"
                "lds    r24, timer0_millis_count+1"     "\n\t"
                "sbci   r24, 255"                       "\n\t"
                "sts    timer0_millis_count+1, r24"     "\n\t"
                "brcs   L_%=_ovcount"                   "\n\t"
                "lds    r24, timer0_millis_count+2"     "\n\t"
                "sbci   r24, 255"                       "\n\t"
                "sts    timer0_millis_count+2, r24"     "\n\t"
                "brcs   L_%=_ovcount"                   "\n\t"
                "lds    r24, timer0_millis_count+3"     "\n\t"
                "sbci   r24, 255"                       "\n\t"
                "sts    timer0_millis_count+3, r24"     "\n\t"
                "rjmp   L_%=_ovcount"                   "\n\t"

        "L_%=_fract_roll:"                              "\n\t"
                "subi   r24, 125 - %0"                  "\n\t"
                "sts    timer0_fract_count, r24"        "\n\t"
                "lds    r24, timer0_millis_count"       "\n\t"
                "subi   r24, 256 - %1 - 1"              "\n\t"
                "sts    timer0_millis_count, r24"       "\n\t"
                "brcc   L_%=_millis_inc_sext"           "\n\t"

        "L_%=_ovcount:"
                "lds    r24, timer0_micros_count"       "\n\t"
                "subi   r24, 256 - %2"                  "\n\t"
                "sts    timer0_micros_count, r24"       "\n\t"
                "brcs   L_%=_end"                       "\n\t"
                "lds    r24, timer0_micros_count+1"     "\n\t"
                "sbci   r24, 255"                       "\n\t"
                "sts    timer0_micros_count+1, r24"     "\n\t"
                "brcs   L_%=_end"                       "\n\t"
                "lds    r24, timer0_micros_count+2"     "\n\t"
                "sbci   r24, 255"                       "\n\t"
                "sts    timer0_micros_count+2, r24"     "\n\t"

        "L_%=_end:"
                "pop    r24"                            "\n\t"
                "out    __SREG__, r24"                  "\n\t"
                "pop    r24"                            "\n\t"
                "reti"
                : 
                : "M" (TIMER0_FRACT_INC), "M" (TIMER0_MILLIS_INC),
                  "M" (TIMER0_MICROS_INC)
        );
}
Teensy Clock Code from Original File

The Teensy Clock file is "pin_teensy.c", which is mostly written in Assembly Language. I unfortunately do not know Assembly Language well enough to make modification to attempt to correct the Clock. Jeff suggested we replace a section (the clock) with known working "C" code from Arduino.

The first attempt included a section to try to slow the Clock by an amount equal to the previously observed error. After 6 more hours, the clock was NOW slow by about the same amount as it was FAST before. Jeff suggested we remove the correction and obtain a new base line with just the "C" code installed.


SIGNAL(TIMER0_OVF_vect)
{
 // copy these to local variables so they can be stored in registers
 // (volatile variables must be read from memory on every access)
 unsigned long m = timer0_millis_count;
 unsigned char f = timer0_fract_count;

 m += MILLIS_INC;
 f += FRACT_INC;
 if (f >= FRACT_MAX) {
  f -= FRACT_MAX;
  m += 1;
 }
 
 timer0_fract_count = f;
 timer0_millis_count = m;
 timer0_overflow_count++;
}
Copied Arduino Clock Code

Due to the work in the Shop (listed above) I did not get back to the Clock project for 29 hours. After 29 hours, the base line results indicated that the Clock was spot-on, only less than a second difference could be observed when compared to a wall clock. Wow!

But, the "C" code replacement did something to the "Interrupts" for the Teensy, my push buttons no longer work as expected. We damaged something by removing the Assembly Language code. More investigation will be needed.

For now, I have reverted to the Assembly Language Clock routing, so that all of my GUI development environment works correctly, but with a Clock that runs a little fast.


UPDATE: The new release of Teensyduino 1.04 fixed the problem.

--

No comments:

Post a Comment