Cheetah SPI Host Adapter Data Sheet v2.01
| Prev: Firmware | Table of Contents | Next: Legal / Contact |
- 5 API Documentation
- 5.1 Introduction
- 5.2 General Data Types
- 5.3 Notes on Status Codes
-
5.4 General
-
Interface
- Find Devices (cheetah_find_devices)
- Find Devices (ch_find_devices_ext)
- Open a Cheetah device (ch_open)
- Open a Cheetah device (ch_open_ext)
- Close a Cheetah (ch_close)
- Get Port (ch_port)
- Get Unique ID (ch_unique_id)
- Status String (ch_status_string)
- Version (ch_version)
- Sleep (ch_sleep_ms)
- Target Power (ch_target_power)
- Host Interface Speed (ch_host_ifce_speed)
-
Interface
-
5.5 SPI Interface
- SPI Overview
-
SPI Functions
- Set Bitrate (ch_spi_bitrate)
- Configure SPI Bus Parameters (ch_spi_configure)
- Clear Batch Queue (ch_spi_queue_clear)
- Queue the Cheetah Output Enable (ch_spi_queue_oe)
- Queue Slave Select Signals (ch_spi_queue_ss)
- Queue a Byte (ch_spi_queue_byte)
- Queue a Byte Array (ch_spi_queue_array)
- Queue a Delay in Cycles (ch_spi_queue_delay_cycles)
- Queue a Delay in Nanoseconds (ch_spi_queue_delay_ns)
- Get Shift Length (ch_spi_batch_length)
- Execute SPI Shift (ch_spi_batch_shift)
- Submit an Asynchronous SPI Shift (ch_spi_async_submit)
- Collect an Asynchronous SPI Shift (ch_spi_async_collect)
- 5.6 Error Codes
5 API Documentation
5.1 Introduction
The API documentation describes the Cheetah Rosetta C bindings.
5.2 General Data Types
The following definitions are provided for convenience.
typedef unsigned char u08; typedef unsigned short u16; typedef unsigned int u32; typedef unsigned long long u64; typedef signed char s08; typedef signed short s16; typedef signed int s32; typedef signed long long s64;
5.3 Notes on Status Codes
Most of the Cheetah API functions return a status or error code back to the caller. The complete list of status codes is provided at the end of this chapter. All of the error codes are assigned values less than 0, separating these responses from any numerical values returned by certain API functions.
Each API function can return one of two error codes with respect to the loading of the Cheetah DLL, CH_UNABLE_TO_LOAD_LIBRARY and CH_INCOMPATIBLE_LIBRARY. If these status codes are received, refer to the previous sections in this datasheet that discuss the DLL and API integration of the Cheetah software. Furthermore, all API calls can potentially return the errors CH_UNABLE_TO_LOAD_DRIVER or CH_INCOMPATIBLE_DRIVER. If either of these errors are seen, please make sure the driver is installed and of the correct version. Where appropriate, compare the language binding versions (e.g., CH_HEADER_VERSION found in cheetah.h and CH_CFILE_VERSION found in cheetah.c) to verify that there are no mismatches. Next, ensure that the Rosetta language binding (e.g., cheetah.c and cheetah.h) are from the same release as the Cheetah DLL. If all of these versions are synchronized and there are still problems, please contact Total Phase support for assistance.
Note that any API function that accepts a Cheetah handle can potentially return the error code CH_INVALID_HANDLE if the handle does not correspond to a valid Cheetah device that has already been opened. If this error is received, check the application code to ensure that the ch_open command returned a valid handle and that this handle was not corrupted before being passed to the offending API function.
Finally, any API call that communicates with a Cheetah device can also return the error CH_COMMUNICATION_ERROR. This means that while the Cheetah handle is valid and the communication channel is open, there was an error communicating with the device. This is possible if the device was unplugged while being used.
These common status responses are not reiterated for each function. Only the error codes that are specific to each API function are described below.
All of the possible error codes, along with their values and status strings, are listed following the API documentation.
5.4 General
Interface
Find Devices (cheetah_find_devices)
int ch_find_devices (int nelem, u16 * devices);
Get a list of ports to which Cheetah devices are attached.
| Arguments | |
|---|---|
| nelem | Maximum size of the array |
| devices | array into which the port numbers are returned |
| Return Value |
|---|
| This function returns the number of devices found, regardless of the array size. |
| Specific Error Codes |
|---|
| None. |
| Details |
|---|
Each element of the array is written with the port number. Devices that are in use are OR’ed with CH_PORT_NOT_FREE (0x8000). Under Linux, such devices correspond to Cheetah adapters that are currently in use. Under Windows, such devices are currently in use, but it is not known if the device is a Cheetah adapter. Example: Devices are attached to port 0, 1, 2. If the input array is NULL, it is not filled with any values. If there are more devices than the array size (as specified by nelem), only the first nelem port numbers will be written into the array. |
Find Devices (ch_find_devices_ext)
int ch_find_devices_ext (int nelem, u16 * devices, u32 * unique_ids);
Get a list of ports and unique IDs to which Cheetah devices are attached.
| Arguments | |
|---|---|
| nelem | Maximum size of the array |
| devices | array into which the port numbers are returned |
| unique_ids | array into which the unique IDs are returned |
| Return Value |
|---|
| This function returns the number of devices found, regardless of the array size. |
| Specific Error Codes |
|---|
| None. |
| Details |
|---|
This function is the same as ch_find_devices() except that is also returns the unique IDs of each Cheetah device. The IDs are guaranteed to be non-zero if valid. The IDs are the unsigned integer representation of the 10-digit serial numbers. |
Open a Cheetah device (ch_open)
Cheetah ch_open (int port_number);
Open a connection to the Cheetah at the given port.
| Arguments | |
|---|---|
| port_number | The Cheetah port number. This port number is the the same as the one obtained from the ch_find_devices function. It is a zero-based number. |
| Return Value |
|---|
| This function returns a Cheetah handle, which is guaranteed to be greater than zero if valid. |
| Specific Error Codes | |
|---|---|
| CH_UNABLE_TO_OPEN | The specified port is not connected to a Cheetah device or the port is already in use. |
| CH_INCOMPATIBLE_DEVICE | There is a version mismatch between the DLL and the hardware. The DLL is not of a sufficient version for interoperability with the hardware version or vice versa. See ch_open_ext() for more information. |
| Details |
|---|
This function is recommended for use in simple applications where extended information is not required. For more complex applications, the use of ch_open_ext() is recommended. |
Open a Cheetah device (ch_open_ext)
Cheetah ch_open_ext (int port_number, CheetahExt *ch_ext);
Open the Cheetah port, returning extended information in the supplied structure.
| Arguments | |
|---|---|
| port_number | same as ch_open |
| ch_ext | pointer to pre-allocated structure for extended version information available on open |
| Return Value |
|---|
| This function returns a Cheetah handle, which is guaranteed to be greater than zero if valid. |
| Specific Error Codes | |
|---|---|
| CH_UNABLE_TO_OPEN | The specified port is not connected to a Cheetah device or the port is already in use. |
| CH_INCOMPATIBLE_DEVICE | There is a version mismatch between the DLL and the hardware. The DLL is not of a sufficient version for interoperability with the hardware version or vice versa. The version information will be available in the memory pointed to by ch_ext. |
| Details |
|---|
If 0 is passed as the pointer to the structure ch_ext, this function will behave exactly like ch_open(). The CheetahExt structure is described below: The CheetahVersion structure describes the various version dependencies of Cheetah components. It can be used to determine which component caused an incompatibility error. All version numbers are of the format: (major << 8) | minor The structure is zeroed before the open is attempted. It is filled with whatever information is available. For example, if the hardware version is not filled, then the device could not be queried for its version number. This function is recommended for use in complex applications where extended information is required. For simpler applications, the use of ch_open() is recommended. |
Close a Cheetah (ch_close)
int ch_close (Cheetah cheetah);
Close the connection to the given Cheetah.
| Arguments | |
|---|---|
| cheetah | handle of a Cheetah adapter to be closed |
| Return Value |
|---|
| The number of adapters closed is returned on success. This will usually be 1. |
| Specific Error Codes |
|---|
| None. |
| Details |
|---|
| If the handle argument is zero, the function will attempt to close all possible handles, thereby closing all open Cheetah adapters. The total number of Cheetah adapters closed is returned by the function. |
Get Port (ch_port)
int ch_port (Cheetah cheetah);
Return the port number for this Cheetah handle.
| Arguments | |
|---|---|
| cheetah | handle of a Cheetah adapter |
| Return Value |
|---|
| The port number corresponding to the given handle is returned. It is a zero-based number. |
| Specific Error Codes |
|---|
| None. |
| Details |
|---|
None. |
Get Unique ID (ch_unique_id)
u32 ch_unique_id (Cheetah cheetah);
Return the unique ID of the given Cheetah device.
| Arguments | |
|---|---|
| cheetah | handle of a Cheetah adapter |
| Return Value |
|---|
| This function returns the unique ID for this Cheetah adapter. The IDs are guaranteed to be non-zero if valid. The ID is the unsigned integer representation of the 10-digit serial number. |
| Specific Error Codes |
|---|
| None. |
| Details |
|---|
None. |
Status String (ch_status_string)
const char *ch_status_string (int status);
Return the status string for the given status code.
| Arguments | |
|---|---|
| status | status code returned by a Cheetah API function |
| Return Value |
|---|
| This function returns a human readable string that corresponds to status. If the code is not valid, it returns a NULL string. |
| Specific Error Codes |
|---|
| None. |
| Details |
|---|
None. |
Version (ch_version)
int ch_version (Cheetah cheetah, CheetahVersion *version);
Return the version matrix for the device attached to the given handle.
| Arguments | |
|---|---|
| cheetah | handle of a Cheetah adapter |
| version | pointer to pre-allocated structure |
| Return Value |
|---|
| A Cheetah status code is returned with CH_OK on success. |
| Specific Error Codes |
|---|
| None. |
| Details |
|---|
If the handle is 0 or invalid, only the software version is set. See the details of ch_open_ext for the definition of CheetahVersion. |
Sleep (ch_sleep_ms)
u32 ch_sleep_ms (u32 milliseconds);
Sleep for given amount of time.
| Arguments | |
|---|---|
| milliseconds | number of milliseconds to sleep |
| Return Value |
|---|
| This function returns the number of milliseconds slept. |
| Specific Error Codes |
|---|
| None. |
| Details |
|---|
This function provides a convenient cross-platform function to sleep the current thread using standard operating system functions. The accuracy of this function depends on the operating system scheduler. This function will return the number of milliseconds that were actually slept. |
Target Power (ch_target_power)
int ch_target_power (Cheetah cheetah, u08 power_mask);
Activate/deactivate target power pins 4 and 6.
| Arguments | |
|---|---|
| cheetah | handle of a Cheetah adapter |
| power_mask | enumerated values specifying power pin state. See Table 2. |
CH_TARGET_POWER_OFF |
Disable target power pins |
CH_TARGET_POWER_ON |
Enable target power pins |
CH_TARGET_POWER_QUERY |
Queries the target power pin state |
| Return Value |
|---|
| The current state of the target power pins on the Cheetah adapter will be returned. The configuration will be described by the same values as in the table above. |
| Specific Error Codes |
|---|
| None. |
| Details |
|---|
Both target power pins are controlled together. Independent control is not supported. This function may be executed in any operation mode. |
Host Interface Speed (ch_host_ifce_speed)
int ch_host_ifce_speed (Cheetah cheetah);
Return the interface host speed.
| Arguments | |
|---|---|
| cheetah | handle of a Cheetah adapter |
| Return Value | ||||
|---|---|---|---|---|
This function returns enumerated values specifying the USB speed at which the host computer is communicating with the given Cheetah device. See Table 3. Table 3: Host interface speed enumerated types
|
| Specific Error Codes |
|---|
| None. |
| Details |
|---|
Used to determine the USB communication rate between the Cheetah device and the host. A High Speed USB interface is highly recommended to take full advantage of the high speed SPI interface. |
5.5 SPI Interface
SPI Overview
In order to use the Cheetah API to send SPI data across the bus at high speed, commands are accumulated in a queue until a call is made to batch shift all of the queued commands. The sequence of commands in the command queue for a typical SPI transaction is:
Call ch_spi_queue_clear to clear the command queue.
Call ch_spi_queue_oe to add a command to the queue to enable the Cheetah device’s outputs on the SPI bus.
Call ch_spi_queue_ss to add a command to the queue to enable the slave select signal.
Call ch_spi_queue_byte and ch_spi_queue_array to queue data to be sent across the SPI bus.
Call ch_spi_queue_ss to queue a command to disable the slave select signal.
Optionally, call ch_spi_queue_oe to queue a command to disable the outputs of the Cheetah device.
Call ch_spi_batch_shift to send the accumulated accumulated commands across the SPI bus.
Please note that the queue is not cleared after it has been executed. This allows the same queue to be executed multiple times without re-queuing commands. The only way to clear a queue is by calling ch_spi_queue_clear.
If the user wishes to repeat the exact same shift sequence on the bus, the last step can be executed without the preceding ones. Also, the OE enable commands and SS assert/deassert commands are held persistent on the Cheetah device. For example, if the SS line is left asserted after one transaction, it will stay asserted until modified in a subsequent transaction.
The data sent from the slave device to the Cheetah device across the MISO line will be filled into the data_in parameter of the ch_spi_batch_shift function. Also, ch_spi_batch_length can be called prior to calling ch_spi_batch_shift to find out how large of an array needs to be allocated for the data_in argument.
The SPI master and slave must both be configured to use the same bit protocol (mode). Prior to sending any data across the SPI bus, the application should call ch_spi_configure and ch_spi_bitrate to set the proper configuration for the SPI communication.
This architecture for the Cheetah API allows for maximum flexibility of the control lines (SS1, SS2, and SS3) and provides for a mechanism to avoid expensive USB scheduling delays (as described in a previous section of this datasheet). For example, one can send several separate SPI packets with the following sequence:
Assert the OE.
Assert the appropriate SS, queue the data, and deassert SS.
Add optional delay on the bus.
Assert the appropriate SS, queue the data, and deassert SS.
Repeat the last two steps as many times as desired.
Call the batch shift function.
This grouping of packets into a single transactions can be very efficient for programming devices such as EEPROMs or flash memories that require a separate SPI packets for each page that needs to be written. The delay step would then correspond to the page write time specified in the memory’s datasheet.
Note that the ch_spi_batch_shift function will only return to the caller after the entire queue has been shifted on the SPI bus. There is an alternative, non-blocking implementation that can be used in lieu of ch_spi_batch_shift. The functions ch_spi_async_submit and ch_spi_async_collect allow the user to submit a batch queue and perform other tasks (including assembling and submitting a subsequent queue) before collecting the results of the initial SPI shift operation. This is an advanced feature that has traditionally only been needed by a few users; a detailed description is provided in the relevant API documentation. The error code CH_SPI_ASYNC_PENDING can be returned by other synchronous functions in the event that asynchronous batches are pending (i.e., have not been yet collected).
SPI Functions
Set Bitrate (ch_spi_bitrate)
int ch_spi_bitrate (Cheetah cheetah, int bitrate_khz);
Set the SPI bitrate in kilohertz.
| Arguments | |
|---|---|
| cheetah | handle of a Cheetah adapter |
| bitrate_khz | the requested bitrate in kHz. |
| Return Value |
|---|
This function returns the actual bitrate set. |
| Specific Error Codes |
|---|
| None. |
| Details |
|---|
The power-on default bitrate is 1 MHz. Only certain discrete bitrates are supported by the Cheetah adapter. As such, this actual bitrate set will be less than or equal to the requested bitrate unless the requested value is less than 100 kHz, in which case the Cheetah adapter will default to 100 kHz. The maximum supported speed for the Cheetah adapter is 50 MHz. If bitrate_khz is 0, the function will set the bit rate to the minimum value, 100 kHz. |
Configure SPI Bus Parameters (ch_spi_configure)
int ch_spi_configure (Cheetah cheetah, CheetahSpiPolarity polarity, CheetahSpiPhase phase, CheetahSpiBitorder bitorder, u08 ss_polarity);
Configure the SPI interface.
| Arguments | |
|---|---|
| cheetah | handle of a Cheetah adapter |
| polarity | the polarity of the clock signal (see Table 4) |
| phase | the phase of the clock signal to sample on (see Table 5) |
| bitorder | the bit order, big endian or little endian (see Table 6) |
| ss_polarity | the polarity of the slave select signals |
CH_SPI_POL_RISING_FALLING |
Clock idle low, active high |
CH_SPI_POL_FALLING_RISING |
Clock idle high, active low |
CH_SPI_PHASE_SAMPLE_SETUP |
Sample on the leading edge of the clock signal |
CH_SPI_PHASE_SETUP_SAMPLE |
Sample on the trailing edge of the clock signal |
CH_SPI_BITORDER_MSB |
Big-endian bit order |
CH_SPI_BITORDER_LSB |
Little-endian bit order |
| Return Value |
|---|
A Cheetah status code is returned with CH_OK on success. |
| Specific Error Codes |
|---|
| None. |
| Details |
|---|
These configuration parameters specify how to clock the bits that are sent and received on the Cheetah SPI interface. The polarity option specifies which transition constitutes the leading edge and which transition is the falling edge. For example, CH_SPI_POL_RISING_FALLING would configure the SPI to idle the SCLK clock line low. The clock would then transition low-to-high on the leading edge and high-to-low on the trailing edge. The phase option determines whether to sample or setup on the leading edge of the clock signal. For example, CH_SPI_PHASE_SAMPLE_SETUP would configure the SPI to sample on the leading edge and setup on the trailing edge of the clock. For example, mode 3 in the figure found in the "SPI Background" chapter would correspond to the pair (CH_SPI_POL_FALLING_RISING, CH_SPI_PHASE_SETUP_SAMPLE). The bitorder option is used to indicate whether LSB or MSB is shifted first. The ss_polarity option is a bit mask that indicates whether each SS line is active high or active low. For example, setting ss_polarity to 0x05 would mean that SS3 and SS1 are active high and SS2 is active low. |
Clear Batch Queue (ch_spi_queue_clear)
int ch_spi_queue_clear (Cheetah cheetah);
Clear the batch queue.
| Arguments | |
|---|---|
| cheetah | handle of a Cheetah adapter |
| Return Value |
|---|
A Cheetah status code is returned with CH_OK on success. |
| Specific Error Codes |
|---|
| None. |
| Details |
|---|
All queued data and commands are removed from the queue. |
Queue the Cheetah Output Enable (ch_spi_queue_oe)
int ch_spi_queue_oe (Cheetah cheetah, u08 oe);
Enable/disable the outputs.
| Arguments | |
|---|---|
| cheetah | handle of a Cheetah adapter |
| oe | 0 to disable the outputs, and 1 to enable |
| Return Value |
|---|
A Cheetah status code is returned with CH_OK on success. |
| Specific Error Codes |
|---|
| None. |
| Details |
|---|
This function enables and disables the outputs on the Cheetah device. When enabled, the Cheetah device connects to the SPI bus and is ready to drive the signal lines. When the Cheetah output enable is disabled, the device disconnects from the bus and each signal line is held at their current values with very weak internal pull-up or pull-down on the Cheetah device. Call this function to enable the Cheetah outputs before calling any of the other queue functions. |
Queue Slave Select Signals (ch_spi_queue_ss)
int ch_spi_queue_ss (Cheetah cheetah, u08 active);
Queue Slave Select assertion/deassertion.
| Arguments | |
|---|---|
| cheetah | handle of a Cheetah adapter |
| active | a bit mask based on the 3 SS lines where 1 corresponds to assertion and 0 to deassertion |
| Return Value |
|---|
A Cheetah status code is returned with CH_OK on success. |
| Specific Error Codes |
|---|
| None. |
| Details |
|---|
This function adds a command to the batch queue to assert/deassert the SPI slave select lines. The active parameter is a bit mask. For example, setting active to 0x05 would mean that SS3 and SS1 are asserted and SS2 is deasserted. The polarity of the slave select is determined from a previous call to ch_spi_configure. |
Queue a Byte (ch_spi_queue_byte)
int ch_spi_queue_byte (Cheetah cheetah, int count, u08 data_out);
Queue a byte value to be sent across the bus one or more times.
| Arguments | |
|---|---|
| cheetah | handle of a Cheetah adapter |
| count | number of bytes to put in queue |
| data_out | value of the byte to queue |
| Return Value |
|---|
Returns the actual number of times the byte in data_out was added to the queue. This should equal count. |
| Specific Error Codes |
|---|
| None. |
| Details |
|---|
Queues count number of bytes to send and sets each byte to the value of data_out. For this command, and the ch_spi_queue_array command, the polarity, phase, and bit ordering will be determined by the configuration set with the ch_spi_configure function. |
Queue a Byte Array (ch_spi_queue_array)
int ch_spi_queue_array (Cheetah cheetah, u16 num_bytes, const u08 * data_out);
Queue an array of bytes to be sent across the bus.
| Arguments | |
|---|---|
| cheetah | handle of a Cheetah adapter |
| num_bytes | number of bytes in data_out |
| data_out | byte array to queue |
| Return Value |
|---|
Returns the number of bytes queued. This should equal num_bytes. |
| Specific Error Codes |
|---|
| None. |
| Details |
|---|
The array will be processed with a minimal run length encoding algorithm, so repeated sequences of single bytes will be sent more efficiently. This helps reduce outgoing (host to Cheetah) bandwidth on the USB bus. For this command, and the ch_spi_queue_byte command, the polarity, phase, and bit ordering will be determined by the configuration set with the ch_spi_configure function. |
Queue a Delay in Cycles (ch_spi_queue_delay_cycles)
int ch_spi_queue_delay_cycles (Cheetah cheetah, int cycles);
Queue a delay value on the bus in units of clock cycles.
| Arguments | |
|---|---|
| cheetah | handle of a Cheetah adapter |
| cycles | cycles of delay to add to the outbound shift |
| Return Value |
|---|
Returns the actual number of cycles of delay that were queued. |
| Specific Error Codes |
|---|
| None. |
| Details |
|---|
Queues cycles amount of delay on the bus. These are in units of clock cycles as set with ch_spi_bitrate. The delays can only be queued in multiples of 8. The function will return the actual number of cycles queued. The requested number of cycles will be rounded up to the next multiple of 8. The requested number of cycles must be greater than zero and less than or equal to 2 32 −9. If the requested number of cycles is out of bounds, no delay is queued and the function will return 0. |
Queue a Delay in Nanoseconds (ch_spi_queue_delay_ns)
int ch_spi_queue_delay_ns (Cheetah cheetah, int nanoseconds);
Queue a delay value on the bus in units of nanoseconds.
| Arguments | |
|---|---|
| cheetah | handle of a Cheetah adapter |
| nanoseconds | amount of time for delay in nanoseconds |
| Return Value |
|---|
Returns the actual number of nanoseconds of delay that were queued. |
| Specific Error Codes |
|---|
| None. |
| Details |
|---|
Queues nanoseconds amount of delay on the bus. The fundamental unit of delay that can be queued on the Cheetah SPI bus is 8 times the clock period. Therefore, requested delay will be rounded up to the next even multiple of this time span. The function will return the actual number of nanoseconds queued. The requested number of nanoseconds must be greater than zero and less than or equal to 2 seconds. If the requested number of nanoseconds is out of bounds, no delay is queued and the function will return 0. |
Get Shift Length (ch_spi_batch_length)
int ch_spi_batch_length (Cheetah cheetah);
Returns the number of data bytes in the queue.
| Arguments | |
|---|---|
| cheetah | handle of a Cheetah adapter |
| Return Value |
|---|
Returns the number of data bytes in the queue. This does not include the commands (SS assertion, OE, etc). It only corresponds to the number of bytes that will be shifted out on the SPI bus. The value returned is also the number of bytes to expect from the slave device when the currently queued commands are executed. |
| Specific Error Codes |
|---|
| None. |
| Details |
|---|
None. |
Execute SPI Shift (ch_spi_batch_shift)
int ch_spi_batch_shift (Cheetah cheetah, int num_bytes, u08 * data_in);
Perform the SPI shift operations in the queue.
| Arguments | |
|---|---|
| cheetah | handle of a Cheetah adapter |
| num_bytes | number of bytes to put into data_in |
| data_in | buffer to place data received from slave device |
| Return Value |
|---|
The number of bytes sent by the Cheetah device across the SPI bus. |
| Specific Error Codes | |
|---|---|
| CH_SPI_BATCH_EMPTY_QUEUE | The queue was empty. |
| Details |
|---|
This function performs all of the accumulated commands in the queue and shifts them in order onto the SPI bus. After the operation completes, the batch queue is not cleared. Therefore, this function may be called repeatedly if the same sequence of commands is to be shifted across the bus multiple times. As data is shifted by the Cheetah device onto the MOSI line, the slave device will shift the same amount of data back across the MISO line. This function will put the first num_bytes number of bytes received from the slave device into the data_in array. To ensure that all of the data from the slave device is captured, call ch_spi_batch_length to determine how much data to expect from the slave device. Setting num_bytes to 0 is permissible for cases where the data from slave device is not required and can simply be discarded. If the number of bytes received from the slave device is less than num_bytes, only the number of bytes received will be put into data_in. However, all of the outgoing bytes in the queue will still be shifted. When setting num_bytes to a value less than the total outgoing length, a special optimization will automatically take effect. This optimization will help reduce the traffic on the USB bus in the inbound direction (Cheetah to host). This optimization introduces an 8 clock cycle delay in the operation of the Cheetah device at the point that the Cheetah device stops sending the slave response back to the host. |
Submit an Asynchronous SPI Shift (ch_spi_async_submit)
int ch_spi_async_submit (Cheetah cheetah);
Submit the SPI shift operations in the queue for asynchronous execution.
| Arguments | |
|---|---|
| cheetah | handle of a Cheetah adapter |
| Return Value |
|---|
The number of bytes to be sent by the Cheetah device across the SPI bus. |
| Specific Error Codes | |
|---|---|
| CH_SPI_BATCH_EMPTY_QUEUE | The queue was empty. |
| CH_SPI_ASYNC_MAX_REACHED | The maximum number of outstanding asynchronous batches has been reached. |
| CH_SPI_ASYNC_EXCESS_DELAY | The queue has a queued delay after the last shifted byte, which is not permissible for asynchronous batch shifting. |
| Details |
|---|
This function will submit the current batch queue asynchronously to the Cheetah. A temporary outgoing buffer will be created to store the batch queue. An internal incoming buffer will be also created to asynchronously capture the slave response data. The application programmer does not have to explicitly manage these two buffers. The function will immediately return after queuing this batch onto the USB, rather than waiting for the shift to complete on the SPI bus. At this point, the application can submit another batch to the queue. This can be done immediately by submitting the same queue a second time without altering it — the application simply needs to call ch_spi_async_submit again. Or, the application may clear the queue and assemble a different batch all together (see the ch_spi_queue family of functions). Any subsequent calls to ch_spi_async_submit will again create a temporary outgoing buffer and copy the current batch into it. Likewise, a temporary incoming buffer will also be created. Note that the submitted batch should be sufficiently long (in real time) so that it does not complete before the application can submit more batches (and also collect the first batch). This will allow the adjacent batches to shift with very little delay between them. How long to be safe? First, there is always the possibility that the application’s process could be scheduled out by the operating system before it has an opportunity to submit the subsequent batch. The operating system scheduler timeslice may be as much as 10 ms. Therefore, submitted batches should be long enough to bridge one, if not two, time slices. Second, if the application is performing its own functions between the submission of two batches, the length of the batches should be long enough to accommodate the CPU time of those functions. Keep in mind that there can significant memory overhead for each asynchronous batch:
Hence, it is important to not queue many megabytes of batches with the asynchronous interface. Additionally, only a fixed number of batches can be submitted and be left pending prior to collection. This number is fixed to 16. Considering the time delay between the last queued command of one asynchronous batch and the first queued command of an immediately subsequent asynchronous batch, there potentially can be delays inserted due to a padding algorithm that takes effect at the end of any given batch. In general, there can be a maximum delay of (2040 / spi_bitrate_hz) present on the SPI bus between two asynchronous batches. Finally, the asynchronous interface is only useful if the outgoing data of any asynchronous batch does not rely on the return MISO data of a previous asynchronous batch. |
Collect an Asynchronous SPI Shift (ch_spi_async_collect)
int ch_spi_async_collect (Cheetah cheetah, int num_bytes, u08 * data_in);
Collect a previously submitted asynchronous SPI shift queue.
| Arguments | |
|---|---|
| cheetah | handle of a Cheetah adapter |
| num_bytes | number of bytes to put into data_in |
| data_in | buffer to place data received from slave device |
| Return Value |
|---|
The number of bytes sent by the Cheetah device across the SPI bus. |
| Specific Error Codes | |
|---|---|
| CH_SPI_ASYNC_EMPTY | There are no outstanding asynchronous batches to be collected. |
| Details |
|---|
This function can be called at anytime after submitting a batch for asynchronous processing. It will block until the first pending batch completes. For example, if there are 5 batches outstanding, this function will return after 1 batch has fully completed, leaving the other 4 batches still outstanding. The function will fill the user-supplied buffer with the incoming MISO data for the completed batch. The incoming data will be copied from the internal incoming buffer originally created by ch_spi_async_submit into the user supplied data_in buffer. The internal buffers (both outgoing and incoming) for the completed batch will be deleted before this function returns to the application. If ch_close is called without collecting pending asynchronous batches, those batches will be canceled, even if they are in progress. All temporary buffers will be freed as well. A suggested mode of operation would be as follows:
Note that this merely is a recommendation for use and developers can modify this procedure as it suits their own application requirements. The application must keep full accounting of how many batches have been submitted and how many are collected during each step of the process. It is even possible that the application will not need multiple threads if it can process the data between steps 2 and 3 and guarantee the algorithm will not take too long to process the incoming data. |
5.6 Error Codes
Literal Name |
Value |
ch_status_string() return value |
CH_OK |
0 |
ok |
CH_UNABLE_TO_LOAD_LIBRARY |
-1 |
unable to load library |
CH_UNABLE_TO_LOAD_DRIVER |
-2 |
unable to load USB driver |
CH_UNABLE_TO_LOAD_FUNCTION |
-3 |
unable to load binding function |
CH_INCOMPATIBLE_LIBRARY |
-4 |
incompatible library version |
CH_INCOMPATIBLE_DEVICE |
-5 |
incompatible device version |
CH_INCOMPATIBLE_DRIVER |
-6 |
incompatible driver version |
CH_COMMUNICATION_ERROR |
-7 |
communication error |
CH_UNABLE_TO_OPEN |
-8 |
unable to open device |
CH_UNABLE_TO_CLOSE |
-9 |
unable to close device |
CH_INVALID_HANDLE |
-10 |
invalid device handle |
CH_CONFIG_ERROR |
-11 |
configuration error |
CH_UNKNOWN_PROTOCOL |
-12 |
unknown protocol |
CH_STILL_ACTIVE |
-13 |
still active |
CH_FUNCTION_NOT_AVAILABLE |
-14 |
function not available |
CH_OS_ERROR |
-15 |
os error |
CH_SPI_WRITE_ERROR |
-100 |
spi write error |
CH_SPI_BATCH_EMPTY_QUEUE |
-101 |
spi batch empty queue |
CH_SPI_BATCH_SHORT_BUFFER |
-102 |
spi batch short buffer |
CH_SPI_ASYNC_EMPTY |
-103 |
spi async empty |
CH_SPI_ASYNC_PENDING |
-104 |
spi async pending |
CH_SPI_ASYNC_MAX_REACHED |
-105 |
spi async max reached |
CH_SPI_ASYNC_EXCESS_DELAY |
-106 |
spi async excess trailing delay |
