#ifndef X_CONTROLLER_H
#define X_CONTROLLER_H

#include "x_basic_types.h"
#include "xp_id_plate_info.h"
#include "xp_plane_adr.h"

//#include "xp_controller_fpga.h"
//#include "xp_controller_cpld.h"
//#include "x_command.h"

/*-----------------------------------------------------------------------------
Define the types
-----------------------------------------------------------------------------*/
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
// write reg
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
typedef struct {
//0
//
	union
	{
		UInt16 all;
		struct 
		{
	     UInt16 use_int13 :1;
	     UInt16 reserv :15;
		} bit;

	} setup;

} T_controller_write;

#define T_CONTROLLER_WRITE_DEFAULTS {0}
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
// read reg
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
typedef struct {
	union
	{
		UInt16 all;
		struct 
		{
	    UInt16 reserv :9;

	     UInt16 errHWP_trig :1; //TODO: есть ли этот сигнал непонятно, нужна проверка прошивки!!!
	     UInt16 pwm_wdog :1;
		 UInt16 errHWP :1;

	     UInt16 status_er0 :1;
	     UInt16 error_pbus :1;
	     UInt16 er0_trig :1; //er0+erHWP
	     UInt16 er0_out  :1;
		} bit;	
	} errors;

	union
	{
		UInt16 all;
		struct 
		{
	     UInt16 reserv0_3               :4;
	     UInt16 slave_addr_error        :4; // адрес слейва, по которому произошел сбой у параллельной шины.

	     UInt16 count_error_pbus        :4; // ParallelBus - number of errors. if errrors > failCnt   then ER0 = active. (задается по адресу 200С)
		 UInt16 reserv12                :1; //
	     UInt16 err_transmit_hwp_bus    :1; // HWP data transmit fail
	     UInt16 err_sbus                :1; // serialBus_Error
	     UInt16 sbus_updated            :1; // serialBus_DataUpdated
		} bit;	
	} errors_buses;

	union
	{
		UInt16 all;
		struct 
		{
	     UInt16 int13_inited :1;
	     UInt16 reserv0  :15;
		} bit;	
	} status;


} T_controller_read;

#define T_CONTROLLER_READ_DEFAULTS {0,0,0}
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
// main struct
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
typedef struct TS_controller
{
	T_component_status		    	status;
	unsigned int					build;
	T_controller_write				write;
    T_controller_read				read;

    void (*init)();	    // Pointer to calculation function
    int (*read_all)();	    // Pointer to calculation function
    void (*enable_int13)();	    // Pointer to calculation function
    void (*disable_int13)();	    // Pointer to calculation function


} T_controller;

typedef T_controller *T_controller_handle;

// command type
/*typedef enum {
	controller_Command_ReadParameters = 1

} T_controller_Command;
*/

#define T_controller_DEFAULTS { component_NotReady,\
0,\
T_CONTROLLER_WRITE_DEFAULTS,\
T_CONTROLLER_READ_DEFAULTS,\
(void (*)(Uint32))controller_init,\
(int (*)(Uint32))controller_read_all,\
(void (*)(Uint32))controller_int13_enable,\
(void (*)(Uint32))controller_int13_disable\
}


/*------------------------------------------------------------------------------
 Prototypes for the functions
------------------------------------------------------------------------------*/

void controller_init(T_controller_handle);
int controller_read_all(T_controller_handle);

//T_controller_read controller_read_errors(T_controller_handle);

void controller_int13_enable(T_controller_handle);
void controller_int13_disable(T_controller_handle);


#endif 
