SunSPOT API V3.0


com.sun.spot.peripheral
Interface IAT91_TC

All Superinterfaces:
IDriver

public interface IAT91_TC
extends IDriver

Interface to an AT91 Timer Counter.

The AT91 Timer Counter includes three identical 16-bit Timer Counter channels.

Each channel can be independently programmed to perform a wide range of functions including frequency measurement, event counting, interval measurement, pulse generation, delay timing and pulse width modulation.

Each channel has three external clock inputs, five internal clock inputs and two multi-purpose input/output signals which can be configured by the user. Each channel drives an internal interrupt signal which can be programmed to generate processor interrupts.

For a full description of the AT91 Timer Counter please refer to the Atmel documentation. Here we will provide a few basics to allow using the Timer to generate a periodic interrupt. For Timer Counter bit definitions please see TimerCounterBits.

Several signals are provided on the SPOT processor board via the top connector, though the initial eDemo sensor board does not make them available. The available signals are the three external clock inputs (TCLK0, TCLK1, TCLK2) and the general purpose input/output pins for channel 0 (TIOA0, TIOB0).

The Timer Counter can operate in two distinct modes: Capture & Waveform generation. In either mode the rate at which the Timer counts is determined by which internal clock is used. The available clock speeds are:

To use the Timer to measure an interval use Capture Mode, enable the clock to start it counting, and at the end of the interval just read the counter value:

    IAT91_TC timer = Spot.getInstance().getAT91_TC(0);
    timer.configure(TC_CAPT | TC_CLKS_MCK32);
    timer.enableAndReset(); 
    ... interval to measure ...
    int cntr = timer.counter();
    double interval = cntr * 0.5342;  // time in microseconds
 
To generate periodic interrupts modify the above code to set the RC Register to the number of counts that will span the desired period, and enable interrupts on RC Compare:
    IAT91_TC timer = Spot.getInstance().getAT91_TC(0);
    int cnt = (int)(25000 / 0.5342);  // number of clock counts for 25 milliseconds
    timer.configure(TC_CAPT | TC_CPCTRG | TC_CLKS_MCK32);
    timer.setRegC(cnt);
    timer.enableAndReset();
    while(true) {
        timer.enableIrq(TC_CPCS);     // Enable RC Compare interrupt
        timer.waitForIrq();
        timer.status();               // Clear interrupt pending flag
        doTask();                     // method will be called every 25 milliseconds
    }
 
A Java thread detects an interrupt by calling waitForIrq(). This method performs a Channel IO request. If the Timer Counter interrupt bit is set the request returns immediately. If not the calling thread is blocked until the interrupt occurs. To clear the interrupt bit call status().

To handle an interrupt in Java you must:

  1. Call configure(int) to configure the timer so that it will generate an interrupt request.
  2. Call enableAndReset() to start the timer counting
  3. Call enableIrq(int) to enable one or more of the interrupt sources associated with this TC channel.
  4. Call waitForIrq() to wait for the interrupt.
  5. Call status() to clear the interrupt.
  6. Call enableIrq(int) to allow another interrupt.
  7. Repeat from 4)

If you don't want more interrupts then don't call enableIrq(int).

Author:
Syntropy
See Also:
AT91 Spec

Method Summary
 void blockSync()
          Perform a block sync command, which causes a reset of all three counters
 void claimTCLK()
          Enable PIO use of shared TCLK line
 void claimTIOA()
          Disable PIO use of shared TIOA line
 void claimTIOB()
          Disable PIO use of shared TIOB line
 void configure(int mask)
          Configure the Timer-Counter
 void configureXC(int xcMask)
          Configure the block mode inputs appropriate to this TC channel
 int counter()
          Read current counter value
 void disable()
          Disable the counter
 void disableIrq(int mask)
          Disable one or more of the interrupt sources associated with this TC channel.
 void enable()
          Enable counter
 void enableAndReset()
          Enable counter and cause software trigger which forces a reset on next clock edge
 void enableIrq(int mask)
          Enable one or more of the interrupt sources associated with this TC channel.
 int regA()
          Read current Reg A value
 int regB()
          Read current Reg B value
 void setRegA(int i)
          Set the value of the A Compare Register
 void setRegC(int i)
          Set the value of the C Compare Register
 int status()
          Read current status
 void unclaimTIOA()
          Enable PIO use of shared TIOA line
 void waitForIrq()
          Suspend this thread until this TC generates an interrupt.
 
Methods inherited from interface com.sun.spot.peripheral.IDriver
getDriverName, setUp, shutDown, tearDown
 

Method Detail

configure

void configure(int mask)
Configure the Timer-Counter

Parameters:
mask - bits to set in TC_CMR

enableAndReset

void enableAndReset()
Enable counter and cause software trigger which forces a reset on next clock edge


enable

void enable()
Enable counter


counter

int counter()
Read current counter value

Returns:
TC_CV

status

int status()
Read current status

Returns:
TC_SR

claimTIOA

void claimTIOA()
Disable PIO use of shared TIOA line


unclaimTIOA

void unclaimTIOA()
Enable PIO use of shared TIOA line


claimTIOB

void claimTIOB()
Disable PIO use of shared TIOB line


claimTCLK

void claimTCLK()
Enable PIO use of shared TCLK line


regA

int regA()
Read current Reg A value

Returns:
TC_RA

regB

int regB()
Read current Reg B value

Returns:
TC_RB

setRegC

void setRegC(int i)
Set the value of the C Compare Register

Parameters:
i - Value to be set

setRegA

void setRegA(int i)
Set the value of the A Compare Register

Parameters:
i - Value to be set

disable

void disable()
Disable the counter


configureXC

void configureXC(int xcMask)
Configure the block mode inputs appropriate to this TC channel

Parameters:
xcMask - Bits to be set in TC_BMR

blockSync

void blockSync()
Perform a block sync command, which causes a reset of all three counters


enableIrq

void enableIrq(int mask)
Enable one or more of the interrupt sources associated with this TC channel.

Parameters:
mask - Bit mask identifying the required interrupt source(s)

waitForIrq

void waitForIrq()
Suspend this thread until this TC generates an interrupt. You must call enableIrq(int) prior to this, and again if you want a subsequent interrupt.


disableIrq

void disableIrq(int mask)
Disable one or more of the interrupt sources associated with this TC channel.

Parameters:
mask - Bit mask identifying the required interrupt source(s)

SunSPOT API V3.0


Copyright © 2006, 2007 Sun Microsystems, Inc. All Rights Reserved.