/* ---------------------------------------------------------------------------
 * smc4000io.c - v1.2                                    (c) 2007 Micro-key bv
 * ---------------------------------------------------------------------------
 * Micro-key bv
 * Industrieweg 28, 9804 TG Noordhorn
 * Postbus 92, 9800 AA Zuidhorn
 * The Netherlands
 * Tel: +31 594 503020
 * Fax: +31 594 505825
 * Email: support@microkey.nl
 * Web: www.microkey.nl
 * ---------------------------------------------------------------------------
 * Description: SMC4000 IO functions.
 * ---------------------------------------------------------------------------
 * Version(s):  1.0, 31-07-2006, Henk Stegeman.
 *                   Creation.
 *              1.1, 12-10-2007, Jos Pasop.
 *                   Added extenderboard support.
 *              1.2, 27-11-2007, Jos Pasop.
 *                   Added callibration tables for DAC.
 * ---------------------------------------------------------------------------
 */

/* ---------------------------------------------------------------------------
 * System include files.
 * ---------------------------------------------------------------------------
 */
#include <fcntl.h>
#include <sys/ioctl.h>

/* ---------------------------------------------------------------------------
 * Application include files.
 * ---------------------------------------------------------------------------
 */
#include "types.h"
#include "smc4000io.h"

/* ---------------------------------------------------------------------------
 * Local constant and macro definitions.
 * ---------------------------------------------------------------------------
 */
#define AIN0_7          0x1
#define AOUT0_5         0x2
#define DIN0_7          0x3
#define DOUT0_15        0x4
#define REL0_5          0x5
#define RTC             0x6
#define VCC             0x7
#define VCORE           0x8
#define VDDAT           0x9
#define VBAT            0xa
#define TEMP            0xb
#define REVNUM          0xc
#define EXTBOARD        0xd             
#define DINEXT0_3       0xe             
#define DOUTEXT0_3      0xf
#define PWRRELMB        0x10
#define PWRRELEB        0x11
#define AIN0_15         0x12
#define AOUT0_11        0x13
#define SERMB           0x14
#define SEREB           0x15
#define ADCCALMB        0x16
#define ADCCALEB        0x17
#define DACCALMB        0x18
#define DACCALEB        0x19 

#define READ_CMD(id) (id | 0x8000)
#define WRITE_CMD(id) (id)

/* ---------------------------------------------------------------------------
 * Local type definitions.
 * ---------------------------------------------------------------------------
 */

/* ---------------------------------------------------------------------------
 * Global variable definitions.
 * ---------------------------------------------------------------------------
 */

/* ---------------------------------------------------------------------------
 * Local variable definitions.
 * ---------------------------------------------------------------------------
 */

/* ---------------------------------------------------------------------------
 * Local function declarations.
 * ---------------------------------------------------------------------------
 */
int ain0_7Read (int file, UINT16 * data)
    {
    return ioctl (file, READ_CMD(AIN0_7), data);
    }

int aout0_5Write (int file, UINT16 * data)
    {
    return ioctl (file, WRITE_CMD(AOUT0_5), data);
    }

int din0_7Read (int file, UINT16 * data)
    {
    return ioctl (file, READ_CMD(DIN0_7), data);
    }

int dout0_15Write (int file, UINT16 * data)
    {
    return ioctl (file, WRITE_CMD(DOUT0_15), data);
    }
    
int rel0_5Write (int file, UINT16 * data)
    {
    return ioctl (file, WRITE_CMD(REL0_5), data);
    }    
    
int vccRead (int file, UINT16 * data)
    {
    return ioctl (file, READ_CMD(VCC), data);
    } 
 
int vddatRead (int file, UINT16 * data)
    {
    return ioctl (file, READ_CMD(VDDAT), data);
    } 
 
int vbatRead (int file, UINT16 * data)
    {
    return ioctl (file, READ_CMD(VBAT), data);
    } 
 
int vcoreRead (int file, UINT16 * data)
    {
    return ioctl (file, READ_CMD(VCORE), data);
    }  
 
int tempRead (int file, UINT16 * data)
    {
    return ioctl (file, READ_CMD(TEMP), data);
    } 
       
int revnumRead (int file, UINT16 * data)
    {
    return ioctl (file, READ_CMD(REVNUM), data);
    }

int extboardRead (int file, UINT16 * data)
    {
    return ioctl (file, READ_CMD(EXTBOARD), data);
    }

int dinext0_3Read (int file, UINT16 * data)
    {
    return ioctl (file, READ_CMD(DINEXT0_3), data);
    }

int doutext0_3Write (int file, UINT16 * data)
    {
    return ioctl (file, WRITE_CMD(DOUTEXT0_3), data);
    }

int pwrrelmbWrite (int file, UINT16 * data)
    {
    return ioctl (file, WRITE_CMD(PWRRELMB), data);
    }

int pwrrelebWrite (int file, UINT16 * data)
    {
    return ioctl (file, WRITE_CMD(PWRRELEB), data);
    }

int ain0_15Read (int file, UINT16 * data)
    {
    return ioctl (file, READ_CMD(AIN0_15), data);
    }

int aout0_11Write (int file, UINT16 * data)
    {
    return ioctl (file, WRITE_CMD(AOUT0_11), data);
    }

int sermbRead (int file, UINT16 * data)
    {
    return ioctl (file, READ_CMD(SERMB), data);
    }

int serebRead (int file, UINT16 * data)
    {
    return ioctl (file, READ_CMD(SEREB), data);
    }

int adccalmbRead (int file, CALIBRATION_VALUE_DESCR data [])
    {
    return ioctl (file, READ_CMD(ADCCALMB), (UINT16 * ) data);
    }

int adccalmbWrite (int file, CALIBRATION_VALUE_DESCR data [])
    {
    return ioctl (file, WRITE_CMD(ADCCALMB), (UINT16 * ) data);
    }

int adccalebRead (int file, CALIBRATION_VALUE_DESCR data [])
    {
    return ioctl (file, READ_CMD(ADCCALEB), (UINT16 *) data);
    }

int adccalebWrite (int file, CALIBRATION_VALUE_DESCR data [])
    {
    return ioctl (file, WRITE_CMD(ADCCALEB), (UINT16 *) data);
    }

int daccalmbRead (int file, CALIBRATION_VALUE_DESCR data [])
    {
    return ioctl (file, READ_CMD(DACCALMB), (UINT16 * ) data);
    }

int daccalmbWrite (int file, CALIBRATION_VALUE_DESCR data [])
    {
    return ioctl (file, WRITE_CMD(DACCALMB), (UINT16 * ) data);
    }

int daccalebRead (int file, CALIBRATION_VALUE_DESCR data [])
    {
    return ioctl (file, READ_CMD(DACCALEB), (UINT16 *) data);
    }

int daccalebWrite (int file, CALIBRATION_VALUE_DESCR data [])
    {
    return ioctl (file, WRITE_CMD(DACCALEB), (UINT16 *) data);
    }
