Programming Examples

TO THE FULLEST EXTENT OF THE LAW, PII MAKES NO EXPRESSED OR IMPLIED WARRANTY AS TO THE SUITABILITY OF ANY DESIGN ASSISTANCE OR APPLICATION NOTES PROVIDED TO THE CUSTOMER, AND ASSUMES NO LIABILITY FOR ANY DESIGN ASSISTANCE OR APPLICATION NOTES. These examples and notes are free and offered as educational tools. Users of these examples are urged to understand them and evaluate them for appropriateness. Persistor does not offer individualized programming or design assistance.

For the CF2

ADWalk - The enclosed files are examples discussed in the CF2 Programmer's manual (ver 1.1) section on Programming For Serial A/D and RecipeCards. Three examples evolve from sampling a single channel to block sampling and writing onto CompactFlash.

SerRec_lp - This is the better starting point for people who wish to develop a CommLogger type application. It demonstrates logging serial data on the main (SCI) UART. Data is buffered using ping pong buffers and written to a file on the CompactFlash. The CF2 sleeps if there are no incoming characters.

CtrlC - This program demonstrates patching the BIOS to use a substitute for SCIRxFilter to intercept and act on Control-C (you could use it to watch for any character or a sequence of characters).

DirectWrite - A method of writing data directly to the Compact Flash to help ensure data integrity.

LPStop - This example demonstrates waking the CF2 from LPStop from a serial port character. The CF2 has an internal connection from RXD to IRQ4. We can take advantage of this by placing the CF2 into a low power state and waking the system when a character comes across the serial port.

GenericQSPInew - How to use the QSPI on the CF2. It can be used to interface to two SPI devices at the same time. This revised (10-23-03) example is more efficient and easier to understand.

TASKMAN - This simple application demonstrates putting a CF2 to sleep until one or more tasks need to be serviced. This application is meant for engineers who need to sleep for a long period and then perform some measurement. Two approaches are possible. One uses Suspend mode and consumes the least amount of power. The second method uses the PIT and LPSTOP. When using Suspend, power is removed from the CF2 resulting in the lowest power consumption.

OAPH - demonstrates using the CF2 for fast A/D data collection. Data is collected on one channel at 20KHz. The data can be written to a BigIDEA or IBM Microdrive.

Simple TPU UART - We perform a 16 bit A/D conversion on 8 channels and write the results as hex to both the main UART and the TPU UART. If a character is sent from the TPU UART end it is displayed on the main UART.

GPS NMEA strings through TPU UARTS - This is an example of using the TPU UART on the CF2 to receive NMEA data from a GPS. One channel is used to receive the NMEA string from the GPS. After checking and processing the data, the 2D or 3D position is transmitted from a second channel. It would be easy to modify this program to extract data from other NMEA strings.

Logging NMEA data and sending A/D readings - An example of using two TPU UART ports on the R216AU RecipeCard. One TPU UART logs GPS NMEA data (at 4800 bps). The other TPU port outputs A/D samples at 38400 bps once every 5 seconds (a rate that is easily changed). The NMEA data is written to a file on the CompactFlash (appended to one large file). The NMEA data can be monitored via the main UART which is left free for this purpose. Data collection activity of the NMEA data can be seen with the RecipeCard LED. The LED is GREEN when serial data is being gathered and RED when the file is being written.

Store a 64K burst of 50KHz A/D readings - Sample 50K samples per second bursts of A/D samples to RAM using our R216AU. The data is then saved onto the CompactFlash (appended to one file). The buffer is 64K but can be increased (or decreased) to suit your application. (This example does not contain enough comments to explain how it works. Most users desiring fast (>1K sps) A/D sampling to CompactFlash should use the SPIIIDAQ foreground example found below.)

Wake from LPStop on incoming (software) UART character - It allows the CF2 to be woken from LPStop (FastStop) on receipt of a character on the TPU Rx line. This is accomplished by using IRQ5. The TPU Rx pin needs to be connected to IRQ5 for this example to work. This code can serve as a starter example for your own application.

MAX7301 (revB) Port Expander - Jan 2009, rev B, schematic error corrected - This program uses the MAX7301 Port Expander to expand the I/O of the the CF2. It is possible to expand the I/O of the CF2 to up to 16 devices per chip select (PCSx). You can also just use one MAX7301 and expand the I/O from 20 to 28 input and/or output pins.

PTA example - The PTA or, Programmable Time Accumulator TPU Function is used to measure the period or high time or low time of a signal. This example demonstrates measurement of a high period as a one-time event. The result is a count of the number of TCR2 periods that occurred during the time the signal was high.

FQM Function - This example uses the TPU to measure a signal frequency. The TPU FQM function is used for this. The test signal is applied to pin 25 (TPU 4). The FQM function works in Single Shot mode and counts the number of cycles of a signal on pin 25 for a period of 32768 (adjustable) TCR1 clocks (by default TCR1 is 4 MHz).

XMODEM and YMODEM over TPU UART - This is a change to the TPU UART example for redirecting the PicoDOS prompt to the TPU port, TUAltConsole.c. These changes are to support XMODEM and YMODEM over TPU UART. Three functions need to be patched in order to allow this to work: SCITxPutChar, SCIRxQueuedCount, and SCIRxGetChar. Look for comments in the code with these functions.

Simple Ping Pong Buffer with PicoZOOM Example - In this example, a loop continuously writes a dummy byte to the buffer and checks to see when the buffer is half full. When half full, the data is then written to CompactFlash. You can watch the write time by watching pin C1 (/DS) of the CF2 with an oscilloscope. Uncomment line 143 to see the effect that PicoZOOM has. You can also change the size of the buffer easily to test that. In this example, the PingPong buffer is 64K and so half is 32K. A message will be displayed as the 32768 bytes are written. To exit this program, send a break (F8 on the keyboard).

CDP68HC68P1 adds 8 I/O ports in QSPI bus - This is actually a CF1 example dragged out of the archives, but it usually is enough to get CF2 users going. It demonstrates how to set up a device on the QSPI serial bus. This particular device is an Intersil (Harris) CDP68HC68P1 8 bit I/O port.

Writing TPU wrapper functions (NITC) - A preliminary document describing the technique with a project. For demonstration purposes, this example uses the New Input Transition Counter (NITC) which is one of the CF2's standard TPU functions which does not already have built-in high level BIOS calls.

Loading applications without Motocross - This application note describes how to load .RUN or .APP files from one CF2 to another CF2 using the MotoCross protocol but with standard CF2 SCI and TPUART functions.

SPIIIDAQ - (b2 May 2007)- SPIIIDAQ (Serial Peripheral Interface Intrinsic Interval Data Acquisition) is a fixed-overhead, high-priority, interrupt-driven, foreground task that permits continuous streaming from a Persistor R216AU or MRCP (using ADS8344 8 channel 16 bit A/D) to CompactFlash (or BIAHD) at precisely timed aggregate sampling rates up to 64000 samples per second.

SPIIIDAQ with BIAHD - Our documentation and examples for BIAHD need rework and we don't recommend it for new users for that reason. However, if you are an experienced user of the BIAHD hard drive controller, add this new BIACmd prototype command to the ToPicoCmdTable and the function to spdq_commands.c. Typing BIA before ESR should quickly set you up to explore SPIIIDAQ with a hard drive.

SyncSPDQ - This application note is an extension of the SPIIIDAQ example that demonstrates a simple technique to synchronize multiple CF2 so that a master starts all CF2s sampling, with a single precision clock if desired, so that the nth sample from all of the boards occurs within, typically, a few microseconds.

TUSimpleTermBreak (Oct 2010) - This is an update to TUSimpleTerm found in CF2TUExamples installed at C:\Program Files\Persistor\MotoCross Support\CFX\Examples\TPU\UARTs. The updated file shows how to change the baud on-the-fly, does better baud rate matching at higher bauds and/or lower clock rates (shown in the baud change function), shows how to correct a problem where TUTxWaitCompletion sometimes returns before the last character is out the UART, and finally provides a function to simulate a break character. The TPU UART cannot generate (or detect) a break, but we can fake a break by temporarily slowing the baud and sending a zero. Replace TUSimpleTerm with this file in the project. The break is activated by a special character in the terminal loop.

A-DExamples Notes (Nov 2010) - This ZIP contains a PDF describing the A-DExamples found at C:\Program Files\Persistor\MotoCross Support\CFX\Examples\A-DExamples and an updated LowPowerAD.c file.

For the CF1

CtrlC - This program demonstrates patching the BIOS to use a substitute for SCIRxFilter to intercept and act on Control-C (you could use it to watch for any character or a sequence of characters).

GenericQSPI - This program demonstrates the use of the QSPI in the Persistor Instruments CF1. It is a simple example to use when trying to interface the CF1 to a single SPI device.

GenericQSPI2 - This program demonstrates the use of the QSPI in the Persistor Instruments CF1. It is similar to the GenericQSPI example with the difference that this example demonstrates interfacing to two SPI devices at the same time.

Servo - This program will drive a PWM signal for use with a hobby-type servo such as can be found on radio controlled cars and aircraft. The interface to the servo will be different for different servos and voltages and is left to the reader. You can run this example and observe the signal that is generated on an oscilloscope.

SFormat - This program should be compiled, loaded and written to the Compact Flash under PicoDOS as SFormat.pxe. SFormat will format a Compact Flash card in the same way that format does. The BIG difference is that SFormat.pxe and autoexec.bat (if such a file exists on the root of the file system) will be loaded into SRAM temporarily as will any other files from the root directory that are specified on the command line. After the card has been formatted, the files will be written back to the Compact Flash card. This is helpful if you usually keep PICODAQ.PXE and/or a few batch files on your Compact Flash cards. If you use SFormat, you will not have to first format your card and then reload all your files using the SanDisk reader. You can just run SFormat with a few files specified (up to 8 depending on size and available memory) and the card will be formatted and re-loaded with your files.

DS1306 Precision Real Time Clock (updated 3-5-04) - This is a group of files which demonstrates using a DS1306 Precision Real Time Clock with a Persistor Instruments CF1. Most of the features of the DS1306 have been added such as setting time, setting alarms, reading/writing NVRAM, setting alarms for use with interrupts and for forcing the CF1 out of suspend mode. This allows you to use the DS1306 to provide a more accurate wake-from-suspend for the CF1. Some applications require precise time keeping while still maintaining the lowest power consumption of the CF1. By using the DS1306 with the CF1 you can maintain a precise time schedule.

A description is provided for each of the functions however, please read the DS1306 technical specification in order to understand how the device works. The sample test program will help you to get started however, the more familiar with the DS1306 you are, the better.

For either the CF1 or CF2

Binary to Decimal Application - One of the most common uses of a CF2 is to collect data via an A/D converter. The data collected is usually from 12 to 16 bit A/D converters. This means that it takes 2 bytes to store one A/D sample. Most of the time, several channels of an A/D are sampled at the same rate and stored one after the other to a binary file on the CompactFlash (or hard disk using the BigIDEA).

One of the most common question we get is: how do I get my data converted from binary to decimal? Bin2Dec will read an input file containing N consecutive binary 2-byte values, convert those values to decimal and store the result in an output file as ASCII. It is assumed that the 16 values read are unsigned values.

The program is run from the command prompt under Windows. From the Start Menu select RUN and type COMMAND in the text box and click OK. You must navigate to the directory where the files are stored. Assuming that the BIN2DEC.EXE is also in the same directory as the data, type the following command:

BIN2DEC INPUT.DAT OUTPUT.TXT /8 /M

This will convert the file INPUT.DAT to decimal and write the output to OUTPUT.TXT (all data is written to the output separated by commas). The /8 means 8 consecutive channels are stored in the input file. The /M can be /M or /I. If /M is used the input data is assumed to be 2 byte values stored most significant byte first. If /I is used then the values are stored low byte first. The ZIP file contains the .EXE file and the C source file. The C source code is included for anyone who wants to modify it to suit a particular application.