/*  Copyright 2010, Unpublished Work of Technologic Systems
 *  All Rights Reserved.
 *  Sample utility to access the TS-81xx ADC from a TS-4500.
 *  An opencore that includes the ADC core is required.
 *  gcc ts8100adc.c sbus.c -Wall -march=armv4 -o ts8100adc
 */
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "sbus.h"

int main(void) {
	int i, x;	

	sbuslock();
	winpoke16(0x21000, 0x18); // enable all channels
	winpoke16(0x21002, 0x3f); // enable all channels
	sbusunlock();
	usleep(500000); // wait enough time for conversions

	for (i=1; i<7; i++) {
		sbuslock();
		x = (signed short)winpeek16(0x21002 + 2*i);
		sbusunlock();
		if (i > 2) x = (x * 1006)/200;
		x = (x * 2048)/0x8000;
		printf("adc%d=%d\n", i, x);
	}
	return 0;
}
