/*
 *
 */
 
#ifndef __CP210X_H
#define __CP210X_H

#ifndef NO_INCLUDE_LOCL
#include "crd_ut10xx.h"
#endif

#ifdef __cplusplus
extern "C"
{
#endif

/**
 * \defgroup low_level_interface_group Low level interfaces.
 * \brief Module contains basic interfaces to CRD-UT10XX hardware components.
 * \{
 */

/** \brief Communication time out in miliseconds. */
#define CRD_UT10XX_TIME_OUT 7000
/** \brief Communication long time out in miliseconds (for initialization only). */
#define CRD_UT10XX_LONG_TIME_OUT 40000

/**
 * \defgroup cp210x_interface_group CP210X low level interface.
 * \brief Module contains basic interface to CP210x USB-UART bridge (mounted in CRD-UT10XX).
 * \{
 */

/** \brief Vendor ID for CP210X (Silicon Labs). */
#define CP210X_VENDOR_ID 0x10c4
/** \brief Product ID for CRD-UT10XX (CMM Sigma). */
/*#define CP210X_PRODUCT_ID 0xea60*/
#define CP210X_PRODUCT_ID 0x82e5

/**
 * \name USB endpoints.
 * \{
 */

/** \brief CP210X input bulk endpoint. */
#define CP210X_IN_BULK_EP 0x81
/** \brief CP210X output bulk endpoint. */
#define CP210X_OUT_BULK_EP 0x01
/** \} */

/* 
 * Config request types.
 */
#define CP210X_REQTYPE_HOST_TO_DEVICE 0x41
#define CP210X_REQTYPE_DEVICE_TO_HOST 0xc1

/* 
 * Config SET requests. To GET, add 1 to the request number.
 */
#define CP210X_UART 0x00 /* Enable / Disable */
#define CP210X_BAUDRATE 0x01 /* (BAUD_RATE_GEN_FREQ / baudrate) */
#define CP210X_BITS 0x03 /* 0x(0)(databits)(parity)(stopbits) */
#define CP210X_BREAK 0x05 /* On / Off */
#define CP210X_CONTROL 0x07 /* Flow control line states */
#define CP210X_MODEMCTL 0x13 /* Modem controls */
#define CP210X_CONFIG_6 0x19 /* 6 bytes of config data ??? */

/* 
 * CP210X_UART 
 */
#define CP210X_UART_DISABLE 0x0000
#define CP210X_UART_ENABLE 0x0001

/* 
 * CP210X_BAUDRATE 
 */
#define CP210X_BAUD_RATE_GEN_FREQ 0x384000

/* 
 * CP210X_BITS 
 */
#define CP210X_BITS_DATA_MASK 0x0f00
#define CP210X_BITS_DATA_5 0x0500
#define CP210X_BITS_DATA_6 0x0600
#define CP210X_BITS_DATA_7 0x0700
#define CP210X_BITS_DATA_8 0x0800
#define CP210X_BITS_DATA_9 0x0900

#define CP210X_BITS_PARITY_MASK 0x00f0
#define CP210X_BITS_PARITY_NONE 0x0000
#define CP210X_BITS_PARITY_ODD 0x0010
#define CP210X_BITS_PARITY_EVEN 0x0020
#define CP210X_BITS_PARITY_MARK 0x0030
#define CP210X_BITS_PARITY_SPACE0 0x0040

#define CP210X_BITS_STOP_MASK 0x000f
#define CP210X_BITS_STOP_1 0x0000
#define CP210X_BITS_STOP_1_5 0x0001
#define CP210X_BITS_STOP_2 0x0002

/* 
 * CP210X_BREAK 
 */
#define CP210X_BREAK_ON 0x0000
#define CP210X_BREAK_OFF 0x0001

/* 
 * CP210X_CONTROL 
 */
#define CP210X_CONTROL_DTR 0x0001
#define CP210X_CONTROL_RTS 0x0002
#define CP210X_CONTROL_CTS 0x0010
#define CP210X_CONTROL_DSR 0x0020
#define CP210X_CONTROL_RING 0x0040
#define CP210X_CONTROL_DCD 0x0080
#define CP210X_CONTROL_WRITE_DTR 0x0100
#define CP210X_CONTROL_WRITE_RTS 0x0200

extern int CP210X_CRD_UT10XX_open(CRD_UT10XX_t *ut);
extern int CP210X_CRD_UT10XX_close(CRD_UT10XX_t *ut);
extern int CP210X_CRD_UT10XX_read(CRD_UT10XX_t *ut, unsigned char *buf, int octets, int timeout_ms);
extern int CP210X_CRD_UT10XX_write(CRD_UT10XX_t *ut, const unsigned char *buf, int octets);

/** \} */

/** \} */

#ifdef __cplusplus
};
#endif

#endif /* ifndef __CP210X_H */

