/*	
    timebase.c
    Test Code for the FoxBone peripheral TimeBase1 on FoxVHDL Board 
    (based on Fox Bone protocol interface rel 0.7)		
    For more info see: http://www.acmesystems.it/?id=120
    Author: Roberto Asquini & John Crispin
      Copyright (C) 2006 Acme Systems srl (http://www.acmesystems.it)
      This is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published by
      the Free Software Foundation; either version 2 of the License, or
      (at your option) any later version.

      This example is distributed in the hope that it will be useful,
      but WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      GNU General Public License for more details.

      To have a copy of the GNU General Public License write to the Free Software
      Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include "stdio.h"
#include "stdlib.h"
#include "unistd.h"
#include "sys/ioctl.h"
#include "fcntl.h"
#include "time.h"
#include "string.h"
#include "timebase_userspace.h"

// Global variables
int fd_timebase;

int FOXTIMEBASE_Open(void) {
  fd_timebase = open("/dev/timebase", O_RDWR);
  if (fd_timebase <= 0) {
	printf("FOXTIMEBASE: open error on /dev/timebase\n");
	return(-1);
  }
  return(0);	
}

// -----------------------------------------------------------------------------
// FoxBone_Close
// -----------------------------------------------------------------------------
void FOXTIMEBASE_Close(void) {
  close(fd_timebase);
}

void print_usage(void){
	printf("timebase usage: timebase command value\n"
           "valid commands:\n"
           "e = enable timebase;\n"
           "d = disable timebase;\n"
	   "h = set high timevalue;\n"
	   "l = set low timevalue\n\n"	
	   "Typical usages: \n"
           "timebase e (enable)\n"
	   "timebase h 1d0 (set high value to 0x1d0 which)\n"
	   "               (makes the led blink fast)\n");

};

void FOXTIMEBASE_command(int myargc, char **myargv) {
	int retval, value = 0;

	// minimal input validation
	if(myargc == 1){
		print_usage();
		return;
	};
	if ((myargc!=3) && (myargv[1][0] != 'e') && (myargv[1][0] != 'd')) {
		print_usage();
		return;
	}
	
	if((myargv[1][0] != 'e') && (myargv[1][0] != 'd')){
		retval = sscanf(myargv[2],"%x", &value);
	};
	
		
	switch ( myargv[1][0]) {
		case 'e':	
			ioctl(fd_timebase, IOCTL_FOXBONE_TIMEBASE_ENABLE, 0);  
			break;

		case 'd':
			ioctl(fd_timebase, IOCTL_FOXBONE_TIMEBASE_DISABLE, 0);  
			break;

		case 'h':
			ioctl(fd_timebase, IOCTL_FOXBONE_TIMEBASE_SETHI, value);
			break;
		
		case 'l':
			ioctl(fd_timebase, IOCTL_FOXBONE_TIMEBASE_SETLO, value);
			break;
			
		default:
		       print_usage();
		}


}

int main(int argc, char **argv) {
	int retcode;
	
	retcode = FOXTIMEBASE_Open();
	if (retcode != 0) {
		exit(1);
	}
	
	FOXTIMEBASE_command(argc,argv); // launch the requested command
	
	FOXTIMEBASE_Close();

	return(0);
} // end main()


