simpletools library  v0.8
Collection of convenience functions for common microcontroller tasks
Macros | Typedefs | Functions | Variables
simpletools.h File Reference

This library provides convenience functions for a variety of microcontroller I/O, timing, conversion, and communication tasks.

CONSTRUCTION ZONE: This library is still preliminary, revisions pending.

dac_ctr, square_wave, and all pwm functions, are currently only supported by the LMM and CMM memory models. More...

#include <propeller.h>
#include <driver.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cog.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/stat.h>
#include <dirent.h>
#include <sys/sd.h>
#include <i2c.h>
#include <math.h>

Go to the source code of this file.

Macros

#define PI   3.141592653589793
#define EEPROM_ADDR   0xA0
#define HOME   1
#define CRSRXY   2
#define CRSRLF   3
#define CRSRRT   4
#define CRSRUP   5
#define CRSRDN   6
#define BEEP   7
#define BKSP   8
#define TAB   9
#define NL   10
#define CLREOL   11
#define CLRDN   12
#define CR   13
#define CRSRX   14
#define CRSRY   15
#define CLS   16
#define MSBPRE   0
#define LSBPRE   1
#define MSBPOST   2
#define LSBPOST   3
#define LSBFIRST   0
#define MSBFIRST   1
#define NCO_PWM_1   (0b00100 << 26)
#define CTR_NCO   (0b100 << 26)
#define CTR_PLL   (0b10 << 26)
#define DUTY_SE   (0b110 << 26)
#define pause_ticks(pticks)   __builtin_propeller_waitcnt(pticks+CNT, 0)
 Delay cog from moving on to the next statement for a certain number of system clock ticks.

Typedefs

typedef FILE * serial
typedef FILE * fdserial
typedef FILE * sdcard
typedef I2C * i2c

Functions

void high (int pin)
 Set an I/O pin to output-high.
void low (int pin)
 Set an I/O pin to output-low.
unsigned int toggle (int pin)
 Toggle the output state of the I/O pin.
unsigned int input (int pin)
 Set an I/O pin to input.
unsigned int reverse (int pin)
 Reverse the direction of an I/O pin.
unsigned int get_state (int pin)
 Check the state of an I/O pin without setting it to input.
unsigned int get_direction (int pin)
 Check the direction of the I/O pin.
unsigned int get_output (int pin)
 Get I/O pin output state.
void set_direction (int pin, int direction)
 Set an I/O pin to a given direction.
void set_output (int pin, int state)
 Set I/O pin output register bit to either 1 or 0.
unsigned int get_states (int endPin, int startPin)
 Get states of a contiguous group of I/O pins.
unsigned int get_directions (int endPin, int startPin)
 Get directions for a contiguous group of I/O pins.
unsigned int get_outputs (int endPin, int startPin)
 Get output settings for a contiguous group of I/O pins.
void set_directions (int endPin, int startPin, unsigned int pattern)
 Set directions for a contiguous group of I/O pins.
void set_outputs (int endPin, int startPin, unsigned int pattern)
 Set output states for a contiguous group of I/O pins.
void pause (int time)
 Delay cog from moving on to the next statement for a certain amount of time.
void mark (void)
 Mark the current time.
int timeout (int time)
 Compares the time against the time elapsed since mark.
void wait (int time)
 Waits a certain number of time increments from the last call to mark or wait functions.
void set_pause_dt (int clockticks)
 Set time increment for pause function.
long count (int pin, long duration)
 Count number of low to high transitions an external input applies to an I/O pin over a certain period of time.
void dac_ctr (int pin, int channel, int dacVal)
 Set D/A voltage.
void dac_ctr_res (int bits)
 Set D/A voltage resolution.
void dac_ctr_stop (void)
 Stop the cog that's transmitting the DAC signal(s).
void freqout (int pin, int msTime, int frequency)
 Use same cog to send square wave of a certain frequency for a certain amount of time. For set and forget with another cog, try square_wave function instead.
int pwm_start (unsigned int cycleMicroseconds)
 Start pulse width modulation (PWM) process in another cog.
void pwm_set (int pin, int channel, int tHigh)
 Set a PWM signal's high time.
void pwm_stop (void)
 Shut down PWM process and reclaim cog and I/O pins for other uses.
long pulse_in (int pin, int state)
 Measure the duration of a pulse applied to an I/O pin.
void pulse_out (int pin, int time)
 Transmit a pulse with an I/O pin.
long rc_time (int pin, int state)
 Set I/O pin to input and measure the time it takes a signal to transition from a start state to the opposite state.
void square_wave (int pin, int channel, int freq)
 Make I/O pin transmit a repeated high/low signal at a certain frequency. High and low times are the same. Frequency can range from 1 Hz to 128 MHz.
void square_wave_stop (void)
 Stop the cog that's transmitting a square wave.
void set_io_timeout (long clockTicks)
 Sets the timeout value for the following timed I/O functions: pulse_in, rc_time.
void set_io_dt (long clockticks)
 Sets the time increment for the following timed I/O functions: count, pulse_in, pulse_out, rc_time.
int shift_in (int pinDat, int pinClk, int mode, int bits)
 Receive data from a synchronous serial device.
void shift_out (int pinDat, int pinClk, int mode, int bits, int value)
 Send data to a synchronous serial device.
FILE * sser_setTxRx (int pinTxOut, int pinRxIn, int baudRate)
 Set up a simple serial driver with transmit & receive pins.
FILE * sser_setTx (int pinTxOut, int baudRate)
 Set up a simple serial driver with just a transmit pin.
FILE * sser_setRx (int pinRxIn, int baudRate)
 Set up a simple serial driver with just a receive pin.
int sser_close (FILE *peripheral)
 Close a simple serial deiver.
FILE * fdser_start (int pinTxOut, int pinRxIn, int baudRate, int mode)
 Set up a full duplex serial driver.
int fdser_stop (FILE *peripheral)
 Stop the serial driver.
I2C * i2c_newbus (int sclpin, int sdapin)
 Set up an I2C bus.
void ee_put_byte (char value, int addr)
 Store a byte value at a certain address in the Propeller Chip's dedicated EEPROM.
char ee_get_byte (int addr)
 Get a byte value from a certain address in the Propeller Chip's dedicated EEPROM.
void ee_put_int (int value, int addr)
 Store an int value at a certain address in the Propeller Chip's dedicated EEPROM. An int value occupies four bytes, so the next value should be stored at an address value that's four bytes higher.
int ee_get_int (int addr)
 Get an int value from a certain address in the Propeller Chip's dedicated EEPROM. If you are fetching several int values, make sure to add 4 to the addr value with each successive call.
void ee_put_str (char *s, int n, int addr)
 Store a string of byte values starting at a certain address in the Propeller Chip's dedicated EEPROM.
char * ee_get_str (char *s, int n, int addr)
 Fetch a string of byte values starting at a certain address in Propeller Chip's dedicated EEPROM.
void ee_put_float32 (float value, int addr)
 Store a 32 bit precision floating point value at a certain address in the Propeller Chip'sdedicated EEPROM. A 32 bit value occupies four bytes so if you are storing values in a sequence, make sure to add 4 to each addr parameter value.
float ee_get_float32 (int addr)
 Fetch a 32 bit precision floating point value from a certain address in the Propeller Chip's dedicated EEPROM. A 32 bit value occupies four bytes so if you are fetching values in a sequence, make sure to add 4 to each addr parameter value.
int sd_mount (int doPin, int clkPin, int diPin, int csPin)
 mount an SD card with the minimal 4-pin interface.
char * itoa (int i, char b[], int base)
 Convert value to zero terminated text string.
int add_driver (_Driver *driverAddr)

Variables

long iodt
long t_timeout
long pauseTicks
long t_mark
char setForget
int fdserDriverIndex
unsigned int buscnt

Detailed Description

This library provides convenience functions for a variety of microcontroller I/O, timing, conversion, and communication tasks.

CONSTRUCTION ZONE: This library is still preliminary, revisions pending.

dac_ctr, square_wave, and all pwm functions, are currently only supported by the LMM and CMM memory models.

Author
Andy Lindsay
Version
0.9 (see details for more info)

This (under development) library provides a set of introductory functions that simplify:

Applications include: monitoring, control and communication with simple peripherals, like lights, buttons, dials, motors, peripheral integrated circuits and prototyping with simple systems that use pulse, or serial communication. (A few examples from the very large list of devices includes: servos, ultrasonic distance sensors, accelerometers, serial liquid crystal, display, etc.)

Intended use: Accompanies introductory electronics, robotics and programming lessons and projects on learn.parallax.com. After these lessons, bridge lessons will be added to familiarize the programmer with standard practices used by the community for adding libraries to support and endless variety of peripherals and applications.

Macro Definition Documentation

#define pause_ticks (   pticks)    __builtin_propeller_waitcnt(pticks+CNT, 0)

Delay cog from moving on to the next statement for a certain number of system clock ticks.

At 80 MHz, each clock tick is 12.5 ns. Code overhead varies depending on memory model and optimization. A simple test if you want a certain number of clock ticks is:

unsigned int ti, tf, us, pauseTicks;
us = CLKFREQ/1000000; // 1 us worth of ticks
pauseTicks = 10*us; // 10 us of ticks
ti = CNT; // Save start time
pause_ticks(pauseTicks); // Call pause_ticks
tf = CNT; // Save end time
printf("pauseTicks = %d\n", pauseTicks); // Display pauseTicks
printf("delayTicks = %d\n", tf - ti); // Display measured
Parameters
pticksthe number of pause clock ticks.

Function Documentation

long count ( int  pin,
long  duration 
)

Count number of low to high transitions an external input applies to an I/O pin over a certain period of time.

Parameters
pinI/O pin number
durationAmount of time the measurement counts transitions
Returns
transitions The number of low to high transitions
void dac_ctr ( int  pin,
int  channel,
int  dacVal 
)

Set D/A voltage.

Launches process into another cog for up to two channels of D/A conversion on any I/O pin. Other libraries may be available that provide D/A for more channels. Check SimpleIDE/Learn/Simple Libraries/Convert for options. For more options, check obex.parallax.com.

This library uses another cog's counter modules (2 per cog) to perform duty modulation, which is useful for D/A conversion. The digital signal it generates will affect LED brightness. The signal can be passed through a low pass RC filter for digital to analog voltage conversion. Add an op amp buffer it it needs to drive a load.

Default resolution is 8-bits for output voltages ranging from 0 V to (255/256) of 3.3 V.

General equation is dacVal * (3.3 V/2^bits)

Default is 8 bits, which results in dacVal * (3.3 V/ 256), so dacVal specifies the number of 256ths of 3.3 V. You can change the resolution with the dac_ctr_res function.

Parameters
pinI/O pin number.
channeluse 0 or 1 to select the cog's CTRA or CTRB counter modules, that are used for D/A conversion.
dacValNumber of 256ths of 3.3 V by default. Use a value from 0 (0 V) to 255 .
void dac_ctr_res ( int  bits)

Set D/A voltage resolution.

Default resolution is 8-bits for output voltages ranging from 0 V to (255/256) of 3.3 V.

General equation is dacVal * (3.3 V/2^bits)

Default is 8 bits, which results in dacVal * (3.3 V/ 256), so dacVal specifies the number of 256ths of 3.3 V.

Parameters
bitsthe D/A converter's resolution in bits.
void dac_ctr_stop ( void  )

Stop the cog that's transmitting the DAC signal(s).

Stops any signals, lets go of any I/O pins, and reclaims the cog for other uses.

char ee_get_byte ( int  addr)

Get a byte value from a certain address in the Propeller Chip's dedicated EEPROM.

Parameters
addrThe EEPROM address that with the byte value that should be fetched.
Returns
value The byte value stored by the EEPROM at the address specified by the addr parameter.
float ee_get_float32 ( int  addr)

Fetch a 32 bit precision floating point value from a certain address in the Propeller Chip's dedicated EEPROM. A 32 bit value occupies four bytes so if you are fetching values in a sequence, make sure to add 4 to each addr parameter value.

Make sure that the Math box is checked in the Project Manger. In Simple View, click the Show Project Manager button in SimpleIDE's bottom-left corner. Then click the Linker tab, and check the Math Lib box.

Parameters
addrThe EEPROM address with the 32-bit floating point float value that should be fetched.
Returns
value The float value stored by the EEPROM at the specified address.
int ee_get_int ( int  addr)

Get an int value from a certain address in the Propeller Chip's dedicated EEPROM. If you are fetching several int values, make sure to add 4 to the addr value with each successive call.

Parameters
addrThe EEPROM address with the int value that should be fetched.
Returns
value The int value stored by the EEPROM at the specified address.
char* ee_get_str ( char *  s,
int  n,
int  addr 
)

Fetch a string of byte values starting at a certain address in Propeller Chip's dedicated EEPROM.

Parameters
sAddress of a char array to receive the string of bytes fetched from EEPROM.
nThe number of bytes to copy from EEPROM to the array.
addrThe EEPROM address of the first byte in the string.
Returns
The address of the array that stores the characters that that were fetched.
void ee_put_byte ( char  value,
int  addr 
)

Store a byte value at a certain address in the Propeller Chip's dedicated EEPROM.

Parameters
valueThe byte value to store in EEPROM.
addrThe EEPROM address where the value is to be stored.
void ee_put_float32 ( float  value,
int  addr 
)

Store a 32 bit precision floating point value at a certain address in the Propeller Chip'sdedicated EEPROM. A 32 bit value occupies four bytes so if you are storing values in a sequence, make sure to add 4 to each addr parameter value.

Make sure that the Math box is checked in the Project Manger. In Simple View, click the Show Project Manager button in SimpleIDE's bottom-left corner. Then click the Linker tab, and check the Math Lib box.

Parameters
valueThe 32-bit floating point float value to store in EEPROM.
addrThe EEPROM address where the value is to be stored.
void ee_put_int ( int  value,
int  addr 
)

Store an int value at a certain address in the Propeller Chip's dedicated EEPROM. An int value occupies four bytes, so the next value should be stored at an address value that's four bytes higher.

Parameters
valueThe int value to store in EEPROM.
addrThe EEPROM address where the value is to be stored.
void ee_put_str ( char *  s,
int  n,
int  addr 
)

Store a string of byte values starting at a certain address in the Propeller Chip's dedicated EEPROM.

Parameters
sAddress of a char array containing the string of bytes.
nThe number of bytes to copy from the array.
addrThe EEPROM address of the first byte in the string.
FILE* fdser_start ( int  pinTxOut,
int  pinRxIn,
int  baudRate,
int  mode 
)

Set up a full duplex serial driver.

Parameters
pinTxOuttransmitting output pin.
pinRxInreceiving input pin.
baudRatein bits per second.
modeA 4-bit binary value. Each digit (bit) can be set to specify one of the behaviors listed below. For example, 0b0011 inverts Tx and Rx because bits 1 and 0 are set to 1.
mode bit 0 = invert Rx---------------------|
mode bit 1 = invert Tx--------------------||
mode bit 2 = open-drain/source Tx--------|||
mode bit 3 = ignore Tx echo on Rx-------||||
0b1111
Returns
a file pointer that you can pass to functions like fptrintf.
int fdser_stop ( FILE *  peripheral)

Stop the serial driver.

Parameters
peripheralthe file pointer fdser_start returned.
void freqout ( int  pin,
int  msTime,
int  frequency 
)

Use same cog to send square wave of a certain frequency for a certain amount of time. For set and forget with another cog, try square_wave function instead.

Parameters
pinI/O pin that sends the frequency
msTimetime in ms that the signal lasts
frequencyfrequency of the signal in Hz. Accepts values from 1 Hz to 128000000 Hz (128 MHz).
unsigned int get_direction ( int  pin)

Check the direction of the I/O pin.

This function will tell you the direction of the I/O pin as seen by the cog executing it. Keep in mind that that your program might make other cogs use the I/O pin as an output, and a cog that treats a pin as an output wins over one that treats it as an input.

Parameters
pinI/O pin number
Returns
I/O pin direction as seen by the cog that runs the function
unsigned int get_directions ( int  endPin,
int  startPin 
)

Get directions for a contiguous group of I/O pins.

Get direction register states from a contiguous group of bits in the cog's output register.

Parameters
endPinthe highest numbered pin.
startPinthe lowest numbered pin.
Returns
states Value containing a binary bit pattern. The value for startPin should be in bit-0, next in bit-1, etc.
unsigned int get_output ( int  pin)

Get I/O pin output state.

Keep in mind that this function reports the value in the output register for the cog running the function. That doesn't tell you if the I/O pin is set to input, or whether another cog is sending a different output state.

Parameters
pinI/O pin number
Returns
ina register bit for I/O pin, either 1 or 0.
unsigned int get_outputs ( int  endPin,
int  startPin 
)

Get output settings for a contiguous group of I/O pins.

Get output register settings for a contiguous group of bits in the cog's output register.

Parameters
endPinthe highest numbered pin.
startPinthe lowest numbered pin.
Returns
pattern Value containing a binary bit pattern. The value for startPin should be in bit-0, next in bit-1, etc.
unsigned int get_state ( int  pin)

Check the state of an I/O pin without setting it to input.

Use this function instead of input if the Propeller needs to maintain an output. For example, you can use this to monitor another cog's or counter's output signal activity on a pin. (Note: if the pin is already set to input, it will return the state the external circuit is applying, just like input.)

Parameters
pinNumber of the I/O pin
Returns
The pin's state. If the pin is an output, 1 = 3.3 V and 0 = 0 V. If the pin is an input, 1 means V > 1.65 V, 0 means it's less.
unsigned int get_states ( int  endPin,
int  startPin 
)

Get states of a contiguous group of I/O pins.

This works the same as getState, but for a group of pins. It tells you the actual state of each pin, regardless of whether it's a voltage applied to input or transmitted by an output.

Parameters
endPinthe highest numbered pin.
startPinthe lowest numbered pin.
Returns
states value containing the binary bit pattern. The value for startPin should be in bit-0, next in bit-1, etc.
void high ( int  pin)

Set an I/O pin to output-high.

This function set makes the Propeller P8X32A connect the I/O pin to its positive 3.3 V supply voltage enabling it to source up to 40 mA of current (max 1 W dissipation per chip).

Parameters
pinNumber of the I/O pin to set high
I2C* i2c_newbus ( int  sclpin,
int  sdapin 
)

Set up an I2C bus.

After you have set up the bus, you can use i2cread and i2cwrite functions in ...propgcc/propeller-elf/include/i2c.h to communicate on the bus.

Parameters
sclpinthe I2C bus' serial clock pin.
sdapinthe I2C bus' serial data pin.
Returns
a pointer to the I2C bus. You will need this to pass to the i2cWrite and i2cRead functions for communication on the bus.
unsigned int input ( int  pin)

Set an I/O pin to input.

This function makes the Propeller connect the I/O pin to its input buffer so that it can return the binary value of the voltage applied by an external circuit.

Parameters
pinNumber of the I/O pin to set to input
Returns
1 or 0 to indicate high or low signal received
char* itoa ( int  i,
char  b[],
int  base 
)

Convert value to zero terminated text string.

Given an int, a character array pointer and a base, this function converts the int into the characters that represent the value in the specified base.

Parameters
ian integer value
b[]a character array pointer
basethe number base for the character representation
Returns
the character array address it received.
void low ( int  pin)

Set an I/O pin to output-low.

This function makes the Propeller P8X32A connect the I/O pin to its ground 0 V supply voltage enabling it to sink up to 40 mA of current (max 1 W dissipation per chip).

Parameters
pinNumber of the I/O pin to set low
void mark ( void  )

Mark the current time.

The timeout function uses the marked time to determine if a timeout has occurred.

void pause ( int  time)

Delay cog from moving on to the next statement for a certain amount of time.

The default time increment is 1 ms, so pause(100) would delay for 100 ms = 1/10th of a second. This time increment can be changed with a call to the set_pause_dt function.

Parameters
timeThe number of time increments to delay.
long pulse_in ( int  pin,
int  state 
)

Measure the duration of a pulse applied to an I/O pin.

Default time increments are specified in 1 microsecond units. Unit size can be changed with a call to set_io_dt function.

Parameters
pinI/O pin number
stateState of the pulse (1 for positive or high pulses, 0 for negative or low pulses.
Returns
tPulse Number of time units the pulse lasted
void pulse_out ( int  pin,
int  time 
)

Transmit a pulse with an I/O pin.

Default time increments are specified in 1 microsecond units. Unit size can be changed with a call to set_io_dt function. The pulse will be positive if the I/O pin is transmitting a low signal before the call. The pulse will be negative if it transmits a high signal before the call. When the pulse is done, the pin returns to whatever state it was in before the pulse. If the pin was an input, it will be changed to output and use whatever value was in the output register bit for the pin. This defaults to low on startup, but you can pre-set it while leaving the pin set to input with the set_output function (or check it with get_output).

Parameters
pinI/O pin number
timeAmount of time the pulse lasts.
void pwm_set ( int  pin,
int  channel,
int  tHigh 
)

Set a PWM signal's high time.

After a single call to pwm_start, this function allows you to set a PWM signal's high time. For example, if your pwm_start call sets up 1000 us (1 ms) you could use this function to make the signal high for 3/4 of its cycle with pwm_set(pin, channel, 750). If the signal goes to a DC motor through an H bridge or other driver circuit, the motor will behave as though it's only getting 3/4 of the supply and turn at roughly 3/4 of full speed.

Parameters
pinI/O pin to send the PWM signal. You can change this value on the fly, which is useful for speed control of a DC motor in two different directions. When the PWM signal changes to a new pin, the cog sets the previous pin to input. If you want it to stay low when the PWM cog lets go, just set the pin low in your code before calling pwm_start.
channelYou have options of 0 or 1 for up to two simultaneous PWM signals. If you have an application in mind that requires more PWM signals, check the SimpleIDE/Learn/Simple Libraries/Motor directory, and also online at obex.parallax.com.
tHighThe high time for each PWM cycle repetition.
int pwm_start ( unsigned int  cycleMicroseconds)

Start pulse width modulation (PWM) process in another cog.

Great for DC motors, can also be used for servos, but the servo library is probably a better choice for that.

A PWM signal sends repeated high signals with a fixed cycle time. Your code will typically control the amount of time a PWM signal is high during each cycle. For example, pwm_start(1000) will establish a 1000 us PWM cycle. You can then use the pwm_set function to send high signals that last anywhere from 0 to 1000 us.

Parameters
cycleMicrosecondsNumber of microseconds the PWM cycle lasts.
void pwm_stop ( void  )

Shut down PWM process and reclaim cog and I/O pins for other uses.

Shut down PWM process and reclaim cog and I/O pins for other uses

long rc_time ( int  pin,
int  state 
)

Set I/O pin to input and measure the time it takes a signal to transition from a start state to the opposite state.

Named rc_time because it is often used to measure a resistor-capacitor circuit's tendency to "decay" to either ground or 5 V (depending on wiring). Default time increments are specified in 1 microsecond units. Unit size can be changed with a call to set_io_dt function. The pulse will be positive if the I/O pin is transmitting a low signal before the call.

Parameters
pinI/O pin number
stateStarting pin state
Returns
time from starting pin
unsigned int reverse ( int  pin)

Reverse the direction of an I/O pin.

If an I/O pin's direction is set to input, this function changes it to output. If it's set to output, this function changes it to input.

Parameters
pinI/O pin number
Returns
The new pin direction.
int sd_mount ( int  doPin,
int  clkPin,
int  diPin,
int  csPin 
)

mount an SD card with the minimal 4-pin interface.

Parameters
doPinthe SD card's data out pin.
clkPinthe SD card's clock pin.
diPinthe SD card's data in pin.
csPinthe SD card's chip select pin.
Returns
status 0 if successful, or an error code.
void set_direction ( int  pin,
int  direction 
)

Set an I/O pin to a given direction.

This function sets an I/O pin to either output or input.

Parameters
pinI/O pin number
directionI/O pin direction
void set_directions ( int  endPin,
int  startPin,
unsigned int  pattern 
)

Set directions for a contiguous group of I/O pins.

Set directions values in a contiguous group of bits in the cog's output register.

Parameters
endPinthe highest numbered pin.
startPinthe lowest numbered pin.
patternvalue containing the binary bit pattern. The value for startPin should be in bit-0, next in bit-1, etc.
void set_io_dt ( long  clockticks)

Sets the time increment for the following timed I/O functions: count, pulse_in, pulse_out, rc_time.

Time increment is set in clock ticks. For example, the default of 1 us units is specified with set_io_dt(CLKFREQ/1000000). For 2 microsecond units, use set_io_dt(CLKFREQ/500000).

Parameters
clockticksNumber of clocktics that represents one I/O time increment.
void set_io_timeout ( long  clockTicks)

Sets the timeout value for the following timed I/O functions: pulse_in, rc_time.

Time increment is set in clock ticks. For example, the default of 0.25 seconds is set with set_io_timeout(CLKFREQ/4). To set the timeout to 20 ms, you could use set_io_timeout(CLKFREQ/50).

Parameters
clockTicksNumber of clock ticks for timed I/O
void set_output ( int  pin,
int  state 
)

Set I/O pin output register bit to either 1 or 0.

This function focuses on the I/O pins output register. If you intend to use it to send high or low signals, consider using high or low functions. This function can also be used in conjunction with set_direction to send high or low signals.

Parameters
pinI/O pin to set high or low.
state1 for high, 0 for low (when pin is actually set to output, which can be done with setDirection.
void set_outputs ( int  endPin,
int  startPin,
unsigned int  pattern 
)

Set output states for a contiguous group of I/O pins.

Set output states of a contiguous group of bits in the cog's output register.

Parameters
endPinthe highest numbered pin.
startPinthe lowest numbered pin.
patternvalue containing the binary bit pattern. The value for startPin should be in bit-0, next in bit-1, etc.
void set_pause_dt ( int  clockticks)

Set time increment for pause function.

Default time increment for pause function is 1 ms. This function allows you to change that delay to custom values. For example, set_pause_dt(CLKFREQ/2000) would set it to 1/2 ms increments. To return to default 1 ms increments, use set_pause_dt(CLKFREQ/1000).

Parameters
clockticksthe number of clock ticks that pause(1) will delay.
int shift_in ( int  pinDat,
int  pinClk,
int  mode,
int  bits 
)

Receive data from a synchronous serial device.

Parameters
pinDatData pin
pinClkClock pin
modeOrder and orientation to clock pulse options: MSBPRE, LSBPRE, MSBPOST,LSBPOST
bitsnumber of binary values to transfer
Returns
value received from the synchronous serial device.
void shift_out ( int  pinDat,
int  pinClk,
int  mode,
int  bits,
int  value 
)

Send data to a synchronous serial device.

Parameters
pinDatData pin
pinClkClock pin
modeOrder that bits are transmitteed, either LSBFIRST or MSBFIRST.
bitsnumber of binary values to transfer
valueto transmit
void square_wave ( int  pin,
int  channel,
int  freq 
)

Make I/O pin transmit a repeated high/low signal at a certain frequency. High and low times are the same. Frequency can range from 1 Hz to 128 MHz.

Uses one additional cog with up to two active channels, each with a selectable frequency. You can change transmit pins on the fly by calling this function on the same channel, but with a different pin. The previous pin will be set to input in that cog. If your code is set to output, it will not affect that setting, only the setting for the cog that is transmitting the square wave. Code in your cog, or some other cog can modulate the signal. A low signal allows the square wave to transmit, and a high signal prevents it.

Parameters
pinI/O pin that transmits square wave frequency. To stop sending the signal and change the pin back to input, pass the pin as a negative number.
channel0 or 1 Selects the counter module to transmit the frequency.
freqSquare wave frequency
void square_wave_stop ( void  )

Stop the cog that's transmitting a square wave.

Stops any signals, lets go of any I/O pins, and reclaims the cog for other uses.

int sser_close ( FILE *  peripheral)

Close a simple serial deiver.

Parameters
peripheralthe name of the simple serial driver’s file pointer.
FILE* sser_setRx ( int  pinRxIn,
int  baudRate 
)

Set up a simple serial driver with just a receive pin.

Parameters
pinRxInreceiving input pin.
baudRaterate in bits per second.
Returns
a file pointer that you can pass to functions like fptrintf.
FILE* sser_setTx ( int  pinTxOut,
int  baudRate 
)

Set up a simple serial driver with just a transmit pin.

Example:

serial lcd = sser_setTx(11, 9600); // Serial driver transmits on P11
fputc(22, LCD); // Transmit 22
fputc(12, lcd); // Transmit 12
pause(5); // Wait 5 ms
fptrintf(lcd, "Hello!!!"); // Transmit a string
Parameters
pinTxOuttransmitting output pin.
baudRaterate in bits per second.
Returns
a file pointer that you can pass to functions like fptrintf.
FILE* sser_setTxRx ( int  pinTxOut,
int  pinRxIn,
int  baudRate 
)

Set up a simple serial driver with transmit & receive pins.

Parameters
pinTxOuttransmitting output pin.
pinRxInreceiving input pin.
baudRaterate in bits per second.
Returns
a file pointer that you can pass to functions like fptrintf.
int timeout ( int  time)

Compares the time against the time elapsed since mark.

The default time increment is 1 us, so timeout(2000) will return 1 if 2 ms or more has elapsed since mark, or 0 if it has not.

Parameters
timeNumber of time increments.
unsigned int toggle ( int  pin)

Toggle the output state of the I/O pin.

Change I/O pin's output state from low to high or high to low. This function assumes that some other function has already set the I/O pin to output.

Parameters
pinI/O pin number
Returns
The new pin state
void wait ( int  time)

Waits a certain number of time increments from the last call to mark or wait functions.

The default time increment is 1 us, so wait(2000) will return wait until 2 us after the last call to mark or wait. This function automatically updates the marked time; you can call it repeatedly without having to call mark.

Parameters
timeNumber of time increments.