#include <stdint.h>

/* tsadc.h
 * Library for accessing TS-ADC16 from TS7xxx PC/104 SBCs
 * Users should only need to know about the function definitions that appear here.
 * The following assumptions are made:
 * - There is exactly one TS-ADC16 board in the stack.
 * - Jumper 3 is installed on the TS-ADC16.
 * - Processes running this code have root privileges.
 */

/* Exactly one of the following should be defined.
 * for 7200, 7250, 7260 or 7300: #define TS7200
 * for 7350 or 7370: #define TS7350
 * for 7800: #define TS7800
 */
//#define TS7200
#define TS7350
//#define TS7800

/* adc_sample samples all channels up to max_chan and stores the accumulated
 * results in data[].  data[] must be an array of length 2*(max_chan+1) or
 * longer.  (If longer, elements beyond the required length will not be
 * touched.)  Each channel will be sampled at s_rate MHz if possible, otherwise
 * as fast as possible.  For each channel, the first n_discard values will be
 * discarded, and then the next n_accum values will be added up.  Thus each
 * value returned in data[] should be interpreted in a range from 0 to 
 * (0x10000)*(n_accum).  This function currently assumes a low number of 
 * samples per channel are needed.  Specifically, it is required that 
 * (n_discard + n_accum) * 2*(max_chan + 1) <= 512.
 * max_chan ranges from 0 to 7.
 */
int adc_sample(int max_chan, int s_rate, int n_discard, int n_accum, unsigned int *data);

/* adc_acquire will attempt to acquire n_samples samples at s_rate Hz.
   It will return the number of samples actually acquired.
   This function reads either 1 channel or 1 pair of channels.
 */
int32_t adc_acquire(int8_t channel_id, int32_t s_rate, int32_t n_samples,  uint16_t *data1, uint16_t *data2);

/* dac_set sets a new analog output value.
 * dac_id must be {0..3}
 * value must be {0x0..0xfff}
 */
int dac_set(unsigned char dac_id, unsigned short value);

/* adc_counter returns the current value stored in a counter.
 * counter_id must be {0..3}
 */
unsigned short adc_counter(unsigned char counter_id);

/* adc_digital_in returns an unsigned char value where bits 0-3 report the 
 * DIO values.
 */
unsigned char adc_digital_in();

/* adc_digital_out toggles the digital output pin
 * output should be 0 or 1
 */
int adc_digital_out(unsigned char output);

/* tsadc_init initializes the TS-ADC16, and if necessary configures the PC/104
 * port correctly.
 * It returns 0 if the operation is successful, otherwise -1.
 * Using it is currently optional, because if the adc is not initialized then
 * any of the functions above will automatically initialize it.
 */
int tsadc_init();

