stamp.core
Class CPU

java.lang.Object
  |
  +--stamp.core.CPU

public class CPU
extends Object

Utility methods for accessing the Javelin Stamp hardware core. This class contains many of the native methods integrated into the Javelin Stamp. There is no need to create an instance of CPU since all of its methods and fields are declared static.


Field Summary
static int MAX_NUM_VPS
          The maximum number of virtual peripherals that can be installed at once.
static int pin0
          A constant representing pin P0.
static int pin1
          A constant representing pin P1.
static int pin10
          A constant representing pin P10.
static int pin11
          A constant representing pin P11.
static int pin12
          A constant representing pin P12.
static int pin13
          A constant representing pin P13.
static int pin14
          A constant representing pin P14.
static int pin15
          A constant representing pin P15.
static int pin2
          A constant representing pin P2.
static int pin3
          A constant representing pin P3.
static int pin4
          A constant representing pin P4.
static int pin5
          A constant representing pin P5.
static int pin6
          A constant representing pin P6.
static int pin7
          A constant representing pin P7.
static int pin8
          A constant representing pin P8.
static int pin9
          A constant representing pin P9.
static int[] pins
          An array representing all of the pins on the module.
static int PORTA
          A constant representing the first I/O port on the Javelin Stamp.
static int PORTB
          A constant representing the second I/O port on the Javelin Stamp.
static int POST_CLOCK_LSB
          Specify post-clock sampling and LSB-first transmission for the shift methods.
static int POST_CLOCK_MSB
          Specify post-clock sampling and MSB-first transmission for the shift methods.
static int PRE_CLOCK_LSB
          Specify pre-clock sampling and LSB-first transmission for the shift methods.
static int PRE_CLOCK_MSB
          Specify pre-clock sampling and MSB-first transmission for the shift methods.
static int SHIFT_LSB
          Specify LSB-first transmission for the shift methods.
static int SHIFT_MSB
          Specify MSB-first transmission for the shift methods.
 
Method Summary
static boolean carry()
          Internal method.
static int count(int timeout, int portPin, boolean edge)
          Count the number of rising or falling edge transitions that occur during a set period of time.
static void delay(int period)
          Delays the program execution for a period of time.
static void install(int routine)
          Internal method.
static void installVP(VirtualPeripheral vp)
          Install a new Virtual Peripheral.
static void message(char[] data, int length)
          Internal method.
static void nap(int period)
          Put the Javelin Stamp into sleep mode for a period of time.
static int pulseIn(int timeout, int portPin, boolean pinState)
          Measure the length of a single pulse.
static void pulseOut(int length, int portPin)
          Generate a pulse of a specific duration.If the pin is an input then it will be changed to be an output and remain as an output when complete.
static int rcTime(int timeout, int portPin, boolean pinState)
          Measure the length of time it takes for a pin to match a desired value.
static boolean readPin(int portPin)
          Reads the logic value on a pin.
static byte readPort(int port)
          Read the value on a port.
static int readRegister(int address)
          Internal method.
static int readSRAM(int addr)
          Internal method.
static void removeVP(VirtualPeripheral vp)
          Remove a virtual peripheral from its slot.
static void setInput(int portPin)
          Make a pin an input.
static void setOutput(int portPin)
          Internal method.
static int shiftIn(int dataPortPin, int clockPortPin, int bitCount, int mode)
          Read data from a synchronous serial device.
static void shiftOut(int dataPortPin, int clockPortPin, int bitCount, int mode, int data)
          Write data to a synchronous serial device.
static void uninstall(int bank)
          Internal method.
static void writeObject(int address, Object value)
          Internal method.
static void writePin(int portPin, boolean value)
          Set a pin to a logic state.
static void writePort(int port, byte value)
          Output a value onto a port.
static void writeRegister(int address, int value)
          Internal method.
static void writeSRAM(int addr, int value)
          Internal method.
 
Methods inherited from class java.lang.Object
equals
 

Field Detail

pin0

public static final int pin0
A constant representing pin P0.

pin1

public static final int pin1
A constant representing pin P1.

pin2

public static final int pin2
A constant representing pin P2.

pin3

public static final int pin3
A constant representing pin P3.

pin4

public static final int pin4
A constant representing pin P4.

pin5

public static final int pin5
A constant representing pin P5.

pin6

public static final int pin6
A constant representing pin P6.

pin7

public static final int pin7
A constant representing pin P7.

pin8

public static final int pin8
A constant representing pin P8.

pin9

public static final int pin9
A constant representing pin P9.

pin10

public static final int pin10
A constant representing pin P10.

pin11

public static final int pin11
A constant representing pin P11.

pin12

public static final int pin12
A constant representing pin P12.

pin13

public static final int pin13
A constant representing pin P13.

pin14

public static final int pin14
A constant representing pin P14.

pin15

public static final int pin15
A constant representing pin P15.

pins

public static int[] pins
An array representing all of the pins on the module. The nth element of the array refers to the nth pin. i.e. CPU.pins[5] == CPU.pin5

PORTA

public static final int PORTA
A constant representing the first I/O port on the Javelin Stamp. This port contains pins 0-7.

PORTB

public static final int PORTB
A constant representing the second I/O port on the Javelin Stamp. This port contains pins 8-15.

PRE_CLOCK_MSB

public static final int PRE_CLOCK_MSB
Specify pre-clock sampling and MSB-first transmission for the shift methods.

POST_CLOCK_MSB

public static final int POST_CLOCK_MSB
Specify post-clock sampling and MSB-first transmission for the shift methods.

PRE_CLOCK_LSB

public static final int PRE_CLOCK_LSB
Specify pre-clock sampling and LSB-first transmission for the shift methods.

POST_CLOCK_LSB

public static final int POST_CLOCK_LSB
Specify post-clock sampling and LSB-first transmission for the shift methods.

SHIFT_MSB

public static final int SHIFT_MSB
Specify MSB-first transmission for the shift methods.

SHIFT_LSB

public static final int SHIFT_LSB
Specify LSB-first transmission for the shift methods.

MAX_NUM_VPS

public static final int MAX_NUM_VPS
The maximum number of virtual peripherals that can be installed at once.
Method Detail

installVP

public static void installVP(VirtualPeripheral vp)
                      throws IllegalArgumentException
Install a new Virtual Peripheral.
Parameters:
vp - the virtual peripheral instance to install.
Returns:
the bank number the virtual peripheral has been installed in.
Throws:
IllegalArgumentException - the virtual peripheral could not be installed.

removeVP

public static void removeVP(VirtualPeripheral vp)
Remove a virtual peripheral from its slot. The VP will stop executing immediately and its slot will be available to install a different VP.
Parameters:
vp - the virtual peripheral to remove.

delay

public static void delay(int period)
Delays the program execution for a period of time. The delay method does not return until the delay period has expired. During a delay any installed Virtual Peripherals will continue to run.

Parameters:
period - the length of time to delay for, measured in 95.48 microsecond units.

nap

public static void nap(int period)
Put the Javelin Stamp into sleep mode for a period of time. The accuracy of the sleep timer is very poor. See the manual for more information.
Parameters:
period - specifies the period for which the Stamp should sleep. The following values are valid:
  • 0 - 16ms
  • 1 - 32ms
  • 2 - 64ms
  • 3 - 128ms
  • 4 - 256ms
  • 5 - 512ms
  • 6 - 1024ms
  • 7 - 2048ms

count

public static int count(int timeout,
                        int portPin,
                        boolean edge)
Count the number of rising or falling edge transitions that occur during a set period of time. If the pin is an output then it will be changed to be an input and will remain as an input when the counting is complete.

To ensure reliable detection the input signal must maintain a value for at least 8.68us. This means that a square wave must have a period of at least 17.36us and a frequency less than 57.6kHz.

Parameters:
timeout - the amount of time to count transitions over. Measured in 8.68us units. The value of 32,767 allows a maximum measurement time of 284.4ms.
portPin - the I/O pin to measure transitions on.
edge - the type of edge to count. Pass true to count rising edges, or false to count falling edges.
Returns:
the number of rising edges or falling edges that occured on the pin during the timeout period.

rcTime

public static int rcTime(int timeout,
                         int portPin,
                         boolean pinState)
Measure the length of time it takes for a pin to match a desired value. If the pin is an output then it will be changed to be an input and will remain as an input when rcTime is complete. rcTime can be used to measure the charge/discharge time of a RC (resistor-capacitor) circuit in order to read an analogue value.
Parameters:
timeout - the maximum measurable time.
portPin - the I/O pin to measure.
pinState - the pin state to wait for.
Returns:
the amount of time taken for the pin to change to the desired value, measured in 8.68us units. A value of -1 indicates that the timeout expired before the desired state was reached.

pulseIn

public static int pulseIn(int timeout,
                          int portPin,
                          boolean pinState)
Measure the length of a single pulse. Waits for the value of the selected pin to match the value selected by the state parameter and then times how long it takes for the pin to return to its original state. If the pin is an output then it will be changed to be an input and will remain as an input when complete.

A pulse of less than 8.68us in duration may be missed.

Parameters:
timeout - the maximum amount of time to spend in the pulseIn method. The timeout covers the complete time of the process—both the wait for the start of the pulse and the time of the pulse.
portPin - the I/O pin to measure the pulse on.
pinState - whether to measure a high pulse (true) or a low pulse (false).
Returns:
the length of the pulse in 8.68us units. A value of 0 indicates that no start edge was detected. A value of -1 indicates that no stop edge was detected.

pulseOut

public static void pulseOut(int length,
                            int portPin)
Generate a pulse of a specific duration.If the pin is an input then it will be changed to be an output and remain as an output when complete. To ensure that a positive going pulse or negative going pulse is generated it is a good idea to write a value to the pin first to put it into the correct state. pulseOut inverts the value of the selected pin, waits for the specified length of time, and then returns the pin to its original value.

The time is measured in 8.68us units with a maximum value of 32767 giving a maximum pulse time of 284.4ms. A time of 0 will not cause a pulse. pulseOut can be used to generate the control pulses for a servo motor.

Parameters:
length - the length of the pulse to generate, measured in 8.68us units.
portPin - the I/O to generate the pulse on.

readPin

public static boolean readPin(int portPin)
Reads the logic value on a pin. If the pin is an output then it will be changed to be an input and will remain as an input when complete.
Parameters:
portPin - the I/O pin to read.
Returns:
the state of the pin where true indicates high and false indicates low.

writePin

public static void writePin(int portPin,
                            boolean value)
Set a pin to a logic state. Makes the selected pin high or low as selected by the state parameter. If the pin is an input then it will be changed to be an output and remain as an output when complete.
Parameters:
portPin - the I/O pin to write.
value - the state the set the pin to.

shiftIn

public static int shiftIn(int dataPortPin,
                          int clockPortPin,
                          int bitCount,
                          int mode)
Read data from a synchronous serial device. The clock pin outputs a clock signal and the data is clocked in form the data pin. If the data pin is an output then it will be changed to be an input and remain as an input when complete. If the clock pin is an input then it will be changed to be an output and remain as an output when complete.

The mode parameter selects pre/post clock sampling. Note that some devices require pre-clock sampling. When pre-clock sampling is used the data pin is sampled to obtain the first bit prior to any change of the clock pin. Clearly the device must have had a clock pulse to output the first bit and this is normally supplied by the previous call to shiftIn or shiftOut. Most synchronous serial devices require a write (shiftOut) prior to reading and so the clock for the first bit will be supplied by the shiftOut call. If the device must be read without first being written to then the first read (shiftIn) should request an extra bit to provide the initial clock pulse.

Note that the bit count can be set to higher than 16 (up to 256) if desired. When a number greater than 16 is used the last 16 bits will be returned.

Parameters:
dataPortPin - the I/O pin to read data on.
clockPortPin - the I/O pin on which to provide the clock to the external device.
bitCount - the number of bits to count in (from 1 to 16). bitCount must not be 0.
mode - the shifting and clocking mode to employ. This determines whether bits are shifted into the least-significant-bit (LSb) or most-significant-bit (MSb) first. The clocking mode determines whether the data is sampled before or after the first clock transition. See the description for the possible values.
Returns:
the bits shifted in on the data pin. The result contains bitCount bits and is justified according to the mode setting. If MSb first is selected then the result will be the data read in on the port. If LSb first is selected then the data is returned MSb justified and must be shifted right (16-bitCount) bits to obtain the correct value.

shiftOut

public static void shiftOut(int dataPortPin,
                            int clockPortPin,
                            int bitCount,
                            int mode,
                            int data)
Write data to a synchronous serial device. The clock pin outputs a clock signal and the data is clocked out the data pin. If the data pin is an input then it will be changed to be an output and remain as an output when complete. Likewise if the clock pin is an input then it will be changed to be an output and remain as an output when complete.

Both the clock and data pins should be initialized before calling shiftOut. shiftOut works by inverting the pins so you can generate either idle high or idle low signals.

If LSb first is selected then the data can be passed directly. If MSb first is selected then if less than 16 bits are to be shift out, data should be shifted left to MSb justify it.

Parameters:
dataPortPin - the I/O pin to write data on.
clockPortPin - the I/O pin on which to provide the clock to the external device.
bitCount - the number of bits to count out (from 1 to 16). bitCount must not be 0.
mode - the shifting mode to employ. This determines whether bits are shifted into the least-significant-bit (LSb) or most-significant-bit (MSb) first. Valid values are CPU.SHIFT_MSB and CPU.SHIFT_LSB.

writePort

public static void writePort(int port,
                             byte value)
Output a value onto a port. The lower 8 bits of value will be written to the port. Pins on the port will not be converted to outputs first. This method does not affect the direction of the port.

This method will disturb any virtual peripherals which are using the port.

Parameters:
port - the port to control. Can be either CPU.PORTA, or CPU.PORTB.
value - the value to write to the port.

readPort

public static byte readPort(int port)
Read the value on a port. Read the value currently on a port.
Parameters:
port - the port to read. Can be either CPU.PORTA, or CPU.PORTB.
Returns:
the value on the port.

setInput

public static void setInput(int portPin)
Make a pin an input. The specified pin will be converted to an input. More than one pin can be specified using the + operator, as long as all pins are on the same port.
Parameters:
portPin - the pin to make into an input.

writeRegister

public static void writeRegister(int address,
                                 int value)
Internal method. Do not use.

readRegister

public static int readRegister(int address)
Internal method. Do not use.

writeObject

public static void writeObject(int address,
                               Object value)
Internal method. Do not use.

carry

public static boolean carry()
Internal method. Do not use.

uninstall

public static void uninstall(int bank)
Internal method. Do not use.

install

public static void install(int routine)
Internal method. Do not use.

message

public static void message(char[] data,
                           int length)
Internal method. Do not use.

setOutput

public static void setOutput(int portPin)
Internal method. Do not use.

writeSRAM

public static void writeSRAM(int addr,
                             int value)
Internal method. Do not use.

readSRAM

public static int readSRAM(int addr)
Internal method. Do not use.