/*
 A command-line FX2/FPGA write tool. Takes an address and a data value.

 Copyright (c) 2006 Dimtel, Inc., All Rights Reserved

 $Id: usbw.c,v 1.1 2012/11/11 05:25:19 dim Exp $
 $Date: 2012/11/11 05:25:19 $
 $Author: dim $
 $Revision: 1.1 $
 $Log: usbw.c,v $
 Revision 1.1  2012/11/11 05:25:19  dim
 Moved from iGp12/src/usbio, updated to handle LLRF4 and iGp12

 Revision 1.1.1.1  2010/10/05 16:42:44  dim
 Initial import of iGp12

 Revision 1.2  2010/03/12 19:04:59  dim
 Updates for multiple boards, bug fixes

 Revision 1.1.1.1  2009/06/02 20:26:02  dim
 Initial import

 Revision 1.2  2009/06/02 19:56:51  dim
 Full support for service functions (fw load, 8051 control, EEPROM) added

 Revision 1.1.1.1  2006/11/16 19:38:15  dim
 Initial import

*/

#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "usbio.h"

extern char *exnm;

int main(int argc, char **argv)
{
  unsigned int addr, val, rc;
  usbio_context *ctxt;

  exnm = basename(argv[0]);

  if (argc != 3)
  {
    fprintf(stderr, "Usage: %s address val\n", exnm);
    return 0;
  }
  
  addr = strtoul(argv[1], NULL, 0);
  val  = strtoul(argv[2], NULL, 0);
  
  ctxt = usbio_open_serial(0, getenv("BOARD_SERNO"));
  if (ctxt == NULL)
  {
    fprintf(stderr, "%s: usbio_open() failed\n", exnm);
    return 1;
  }
  
  rc = usbio_block_write(ctxt, addr, &val, 1);
  if (rc != 1)
  {
    fprintf(stderr, "%s: usbio_block_write() failed\n", exnm);
    return 2;
  }
  printf("Wrote 0x%08x to 0x%08x\n", val, addr);
  usbio_close(ctxt, 0);
  
  return 0;
}
