Cascoda SDK
Cascoda SDK for building software to run with CA-821x transceivers
usb-exchange.c File Reference
#include <assert.h>
#include <dlfcn.h>
#include <pthread.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "hidapi/hidapi.h"
#include "ca821x-generic-exchange.h"
#include "ca821x-posix-util-internal.h"
#include "ca821x-queue.h"
#include "ca821x_api.h"
#include "usb-exchange.h"
Include dependency graph for usb-exchange.c:

Classes

struct  usb_exchange_priv
 The usb exchange private-data struct representing a single device. More...
 

Macros

#define USB_VID   0x0416
 
#define USB_PID   0x5020
 
#define MAX_FRAG_SIZE   64
 Maximum USB fragment size. More...
 
#define POLL_DELAY   2
 Max time to wait on rx data in milliseconds. More...
 
#define FRAG_LEN_MASK   0x3F
 
#define FRAG_LAST_MASK   (1 << 7)
 
#define FRAG_FIRST_MASK   (1 << 6)
 
#define ARRAY_LENGTH(array)   (sizeof((array)) / sizeof((array)[0]))
 
#define NO_CONST_CHAR(x)   (((char *)(x)))
 

Functions

void test_frag_loopback ()
 Run to test fragmentation. More...
 
void usb_apply_raspi_workaround (struct ca821x_dev *pDeviceRef)
 This function applies a workaround that is necessary on the Raspberry pi for reliable USB communications. More...
 
ssize_t usb_try_read (struct ca821x_dev *pDeviceRef, uint8_t *buf)
 
ca_error usb_try_write (const uint8_t *buffer, size_t len, struct ca821x_dev *pDeviceRef)
 
void flush_unread_usb (struct ca821x_dev *pDeviceRef)
 
ca_error usb_exchange_init (ca821x_errorhandler callback, const char *path, struct ca821x_dev *pDeviceRef, char *serial_num)
 Initialise the usb exchange. More...
 
void usb_exchange_deinit (struct ca821x_dev *pDeviceRef)
 Deinitialise the usb exchange, so that it can be reinitialised by another process, or reopened later. More...
 
ca_error usb_exchange_enumerate (util_device_found aCallback, void *aContext)
 Function to enumerate all of the USB connected devices, calling aCallback with a struct describing each one. More...
 
int usb_exchange_reset (unsigned long resettime, struct ca821x_dev *pDeviceRef)
 Send a hard reset to the ca821x. More...
 

Macro Definition Documentation

◆ ARRAY_LENGTH

#define ARRAY_LENGTH (   array)    (sizeof((array)) / sizeof((array)[0]))

◆ FRAG_FIRST_MASK

#define FRAG_FIRST_MASK   (1 << 6)

◆ FRAG_LAST_MASK

#define FRAG_LAST_MASK   (1 << 7)

◆ FRAG_LEN_MASK

#define FRAG_LEN_MASK   0x3F

◆ MAX_FRAG_SIZE

#define MAX_FRAG_SIZE   64

Maximum USB fragment size.

◆ NO_CONST_CHAR

#define NO_CONST_CHAR (   x)    (((char *)(x)))

◆ POLL_DELAY

#define POLL_DELAY   2

Max time to wait on rx data in milliseconds.

◆ USB_PID

#define USB_PID   0x5020

◆ USB_VID

#define USB_VID   0x0416

Function Documentation

◆ flush_unread_usb()

void flush_unread_usb ( struct ca821x_dev pDeviceRef)

◆ test_frag_loopback()

void test_frag_loopback ( )

Run to test fragmentation.

Crashes upon fail.

◆ usb_apply_raspi_workaround()

void usb_apply_raspi_workaround ( struct ca821x_dev pDeviceRef)

This function applies a workaround that is necessary on the Raspberry pi for reliable USB communications.

Essentially, on the raspberry pi, if there is not regular USB transmission to the OUT endpoint, the next OUT transmission will take up to 1000ms to complete. This behaviour has only been observed on the Raspberry pi. The hotfix for this is to send a USB transmission every 1 second on the raspi platform.

◆ usb_exchange_deinit()

void usb_exchange_deinit ( struct ca821x_dev pDeviceRef)

Deinitialise the usb exchange, so that it can be reinitialised by another process, or reopened later.

Parameters
pDeviceRefPointer to initialised ca821x_device_ref struct

◆ usb_exchange_enumerate()

ca_error usb_exchange_enumerate ( util_device_found  aCallback,
void *  aContext 
)

Function to enumerate all of the USB connected devices, calling aCallback with a struct describing each one.

The struct passed to aCallback will only be valid for the duration that the function is called. This function will not return until every callback has been called.

Parameters
aCallbackThe callback to call with each result
aContextThe generic void pointer to provide to the callback when it is called
Return values
CA_ERROR_SUCCESSEnumeration successful
CA_ERROR_NOT_FOUNDNo devices found

◆ usb_exchange_init()

ca_error usb_exchange_init ( ca821x_errorhandler  callback,
const char *  path,
struct ca821x_dev pDeviceRef,
char *  serial_num 
)

Initialise the usb exchange.

If callback is specified, then it will be used to report any fatal errors back to the application, which can react as required (i.e. crash gracefully or attempt to reset the ca821x)

If path is specified, then the exchange will attempt to open that device, and fail if it cannot. The path of a device can be obtained with usb_exchange_enumerate.

If serial_num is specified, then the exchange will run an additional check when iterating through all devices, looking for one with matching serial number; Otherwise it will carry out the normal iteration and try to open the first available device.

Parameters
[in]callbackFunction pointer to an error-handling callback (can be NULL)
[in]pathString representing an exchange & system specific path to a device (can be NULL)
[in]pDeviceRefPointer to initialised ca821x_device_ref struct
[in]serial_numSerial number of the target device (can be NULL)
Return values
CA_ERROR_SUCCESSsuccess
CA_ERROR_NOT_FOUNDNo available devices found (or device at path not found)
CA_ERROR_NO_ACCESSThe specified path cannot be opened (e.g. no permissions, already in use)
CA_ERROR_ALREADYThe pDeviceRef is already initialised

◆ usb_exchange_reset()

int usb_exchange_reset ( unsigned long  resettime,
struct ca821x_dev pDeviceRef 
)

Send a hard reset to the ca821x.

This should not be necessary, but is provided in case the ca821x becomes unresponsive to spi.

Parameters
[in]resettimeThe length of time (in ms) to hold the reset pin active for. 1ms is usually a suitable value for this.
[in]pDeviceRefPointer to initialised ca821x_device_ref struct

◆ usb_try_read()

ssize_t usb_try_read ( struct ca821x_dev pDeviceRef,
uint8_t *  buf 
)

◆ usb_try_write()

ca_error usb_try_write ( const uint8_t *  buffer,
size_t  len,
struct ca821x_dev pDeviceRef 
)