/*
 * USB_API_layer.h
 *
 * Created: 6.3.2016 13:05:30
 *  Author: Chenda
 */ 


#ifndef USB_API_LAYER_H_
#define USB_API_LAYER_H_
/************************************************************************/
/* Minimum status reporting structure                                   */
/************************************************************************/
#define MAX_EVENT_QUEUE			8

struct stMinimumStatusReportingStructure {
	uint16_t QUEStionableStatus;
	uint16_t OPERationStatus;
	struct Error_Event_structure {
		uint8_t ErrorEvent[MAX_EVENT_QUEUE];
		uint8_t ReadPointer;
		uint8_t WritePointer;
	} ErrEvent;
	uint8_t StdEventStatReg;
	uint8_t StatByte;
};

// Error Event Error queue macros
#define mSRS_ERREV_INC_R_PTR(x)		x->ErrEvent.ReadPointer++;x->ErrEvent.ReadPointer &= (MAX_EVENT_QUEUE-1)
#define mSRS_ERREV_INC_W_PTR(x)		x->ErrEvent.WritePointer++;x->ErrEvent.WritePointer &= (MAX_EVENT_QUEUE-1)
#define mSRS_ERREV_QUEUE_RD(x)		x->ErrEvent.ErrorEvent[x->ErrEvent.ReadPointer];mSRS_ERREV_INC_R_PTR(x)
#define mSRS_ERREV_QUEUE_WR(x,y)	x->ErrEvent.ErrorEvent[x->ErrEvent.WritePointer] = y;mSRS_ERREV_INC_W_PTR(x)

// Status reporting structure macros
#define mSRS_STB_QUES_UPD(x)	(x->StatByte = (x->QUEStionableStatus)? (x->StatByte|(1<<3)):(x->StatByte&~(1<<3)))
#define mSRS_STB_OPER_UPD(x)	(x->StatByte = (x->OPERationStatus)? (x->StatByte|(1<<7)):(x->StatByte&~(1<<7)))
#define mSRS_STB_EVREG_UPD(x)	(x->StatByte = (x->StdEventStatReg)? (x->StatByte|(1<<5)):(x->StatByte&~(1<<5)))
#define mSRS_STB_EVQUEUE_UPD(x)	(x->StatByte = (x->ErrEvent.ErrorEvent[x->ErrEvent.ReadPointer])? (x->StatByte|(1<<2)):(x->StatByte&~(1<<2)))
// Questionable status macros
#define mSRS_QUES_VOLT_SET(x)	(x->QUEStionableStatus |= (1<<0))
#define mSRS_QUES_VOLT_CLR(x)	(x->QUEStionableStatus &= ~(1<<0))
#define mSRS_QUES_CURR_SET(x)	(x->QUEStionableStatus |= (1<<1))
#define mSRS_QUES_CURR_CLR(x)	(x->QUEStionableStatus &= ~(1<<1))
#define mSRS_QUES_POW_SET(x)	(x->QUEStionableStatus |= (1<<3))
#define mSRS_QUES_POW_CLR(x)	(x->QUEStionableStatus &= ~(1<<3))
#define mSRS_QUES_INST_SET(x)	(x->QUEStionableStatus |= (1<<13))
#define mSRS_QUES_INST_CLR(x)	(x->QUEStionableStatus &= ~(1<<13))
#define mSRS_QUES_CMDW_SET(x)	(x->QUEStionableStatus |= (1<<14))
#define mSRS_QUES_CMDW_CLR(x)	(x->QUEStionableStatus &= ~(1<<14))
// Operation status macros
#define mSRS_OPER_SETT_SET(x)	(x->OPERationStatus |= (1<<1))
#define mSRS_OPER_SETT_CLR(x)	(x->OPERationStatus &= ~(1<<1))
#define mSRS_OPER_MEAS_SET(x)	(x->OPERationStatus |= (1<<4))
#define mSRS_OPER_MEAS_CLR(x)	(x->OPERationStatus &= ~(1<<4))
#define mSRS_OPER_INST_SET(x)	(x->OPERationStatus |= (1<<13))
#define mSRS_OPER_INST_CLR(x)	(x->OPERationStatus &= ~(1<<13))
#define mSRS_OPER_PROG_SET(x)	(x->OPERationStatus |= (1<<14))
#define mSRS_OPER_PROG_CLR(x)	(x->OPERationStatus &= ~(1<<14))
// Standard event status register macros
#define mSRS_EVREG_OPCOMP_SET(x)	(x->StdEventStatReg |= (1<<0))
#define mSRS_EVREG_OPCOMP_CLR(x)	(x->StdEventStatReg &= ~(1<<0))
#define mSRS_EVREG_REQCTRL_SET(x)	(x->StdEventStatReg |= (1<<1))
#define mSRS_EVREG_REQCTRL_CLR(x)	(x->StdEventStatReg &= ~(1<<1))
#define mSRS_EVREG_QRYERR_SET(x)	(x->StdEventStatReg |= (1<<2))
#define mSRS_EVREG_QRYERR_CLR(x)	(x->StdEventStatReg &= ~(1<<2))
#define mSRS_EVREG_DEVDEPERR_SET(x)	(x->StdEventStatReg |= (1<<3))
#define mSRS_EVREG_DEVDEPERR_CLR(x)	(x->StdEventStatReg &= ~(1<<3))
#define mSRS_EVREG_EXECERR_SET(x)	(x->StdEventStatReg |= (1<<4))
#define mSRS_EVREG_EXECERR_CLR(x)	(x->StdEventStatReg &= ~(1<<4))
#define mSRS_EVREG_CMDERR_SET(x)	(x->StdEventStatReg |= (1<<5))
#define mSRS_EVREG_CMDERR_CLR(x)	(x->StdEventStatReg &= ~(1<<5))
#define mSRS_EVREG_USRREQ_SET(x)	(x->StdEventStatReg |= (1<<6))
#define mSRS_EVREG_USRREQ_CLR(x)	(x->StdEventStatReg &= ~(1<<6))
#define mSRS_EVREG_PWRON_SET(x)		(x->StdEventStatReg |= (1<<7))
#define mSRS_EVREG_PWRON_CLR(x)		(x->StdEventStatReg &= ~(1<<7))

#endif /* USB_API_LAYER_H_ */