#ifndef _CAN_SETUP
#define _CAN_SETUP


#include <CAN_project.h>

#include "word_structurs.h"
#include "DSP281x_Device.h"

#define MAX_COUNT_UNITES_TERMINAL   4  // фикс
#define MAX_COUNT_UNITES_UKSS       16 // фикс
#define MAX_COUNT_UNITES_MPU        4  // фикс
#define MAX_COUNT_UNITES_ALARM_LOG  2  // фикс

////////////////////////////////////////

#define CAN_ADR_TERMINAL_DEFAULT            0x00EEEE01
#define START_CAN_ADR_UNITS_DEFAULT         0x00235500
#define CAN_ADR_MPU_DEFAULT                 0x00CEB0E1
#define CAN_ADR_ALARM_LOG_DEFAULT           0x0BCDEF01


////////////////////////////////////////
// подсчет кол-ва блоков MPU
// должны быть активированы в CAN_project.c
////////////////////////////////////////

#ifndef USE_MPU_0
#define USE_MPU_0      0
#endif

#ifndef USE_MPU_1
#define USE_MPU_1      0
#endif

#ifndef USE_MPU_2
#define USE_MPU_2      0
#endif

#ifndef USE_MPU_3
#define USE_MPU_3      0
#endif



#define MPU_UNIT_QUA_UNITS     (  USE_MPU_0 + USE_MPU_1  \
                            + USE_MPU_2 + USE_MPU_3 \
                            )   //кол-во ящиков для MPU_CAN





///////////////////////////////////////////
// кол-во ящиков для TERMINAL_CAN
///////////////////////////////////////////

////////////////////////////////////////
// подсчет кол-ва блоков TERMINAL
// должны быть активированы в CAN_project.c
////////////////////////////////////////
#ifndef USE_TERMINAL_1_OSCIL
#define USE_TERMINAL_1_OSCIL      0
#endif

#ifndef USE_TERMINAL_1_CMD
#define USE_TERMINAL_1_CMD      0
#endif

#ifndef USE_TERMINAL_2_OSCIL
#define USE_TERMINAL_2_OSCIL      0
#endif

#ifndef USE_TERMINAL_2_CMD
#define USE_TERMINAL_2_CMD      0
#endif



#define TERMINAL_UNIT_QUA_UNITS     (  USE_TERMINAL_1_OSCIL + USE_TERMINAL_1_CMD  \
                            + USE_TERMINAL_2_OSCIL + USE_TERMINAL_2_CMD \
                            )   //кол-во ящиков для TERMINAL_CAN


////////////////////////////////////////
// подсчет кол-ва блоков ALARM_LOG
// должны быть активированы в CAN_project.c
////////////////////////////////////////

#ifndef USE_ALARM_LOG_0
#define USE_ALARM_LOG_0      0
#endif

#ifndef USE_ALARM_LOG_1
#define USE_ALARM_LOG_1      0
#endif



#define ALARM_LOG_UNIT_QUA_UNITS     (  USE_ALARM_LOG_0 + USE_ALARM_LOG_1  \
                            )   //кол-во ящиков для ALARM_LOG_CAN






////////////////////////////////////////
// подсчет кол-ва блоков ukss
// должны быть активированы в CAN_project.c
////////////////////////////////////////

#ifndef USE_UKSS_0
#define USE_UKSS_0      0
#endif

#ifndef USE_UKSS_1
#define USE_UKSS_1      0
#endif

#ifndef USE_UKSS_2
#define USE_UKSS_2      0
#endif

#ifndef USE_UKSS_3
#define USE_UKSS_3      0
#endif

#ifndef USE_UKSS_4
#define USE_UKSS_4      0
#endif

#ifndef USE_UKSS_5
#define USE_UKSS_5      0
#endif

#ifndef USE_UKSS_6
#define USE_UKSS_6      0
#endif

#ifndef USE_UKSS_7
#define USE_UKSS_7      0
#endif

#ifndef USE_UKSS_8
#define USE_UKSS_8      0
#endif

#ifndef USE_UKSS_9
#define USE_UKSS_9      0
#endif

#ifndef USE_UKSS_10
#define USE_UKSS_10      0
#endif

#ifndef USE_UKSS_11
#define USE_UKSS_11      0
#endif

#ifndef USE_UKSS_12
#define USE_UKSS_12      0
#endif

#ifndef USE_UKSS_13
#define USE_UKSS_13      0
#endif

#ifndef USE_UKSS_14
#define USE_UKSS_14     0
#endif

#ifndef USE_UKSS_15
#define USE_UKSS_15      0
#endif


#ifndef USE_R_B_0
#define USE_R_B_0   0
#endif
#ifndef USE_R_B_1
#define USE_R_B_1   0
#endif
#ifndef USE_R_B_2
#define USE_R_B_2   0
#endif
#ifndef USE_R_B_3
#define USE_R_B_3   0
#endif
#ifndef USE_R_B_4
#define USE_R_B_4   0
#endif
#ifndef USE_R_B_5
#define USE_R_B_5   0
#endif
#ifndef USE_R_B_6
#define USE_R_B_6   0
#endif
#ifndef USE_R_B_7
#define USE_R_B_7   0
#endif
#ifndef USE_R_B_8
#define USE_R_B_8   0
#endif
#ifndef USE_R_B_9
#define USE_R_B_9   0
#endif
#ifndef USE_R_B_10
#define USE_R_B_10   0
#endif
#ifndef USE_R_B_11
#define USE_R_B_11   0
#endif
#ifndef USE_R_B_12
#define USE_R_B_12   0
#endif
#ifndef USE_R_B_13
#define USE_R_B_13   0
#endif
#ifndef USE_R_B_14
#define USE_R_B_14   0
#endif
#ifndef USE_R_B_15
#define USE_R_B_15   0
#endif


#define UNIT_QUA_UNITS     (  USE_UKSS_0 + USE_UKSS_1 \
                            + USE_UKSS_2 + USE_UKSS_3 \
                            + USE_UKSS_4 + USE_UKSS_5 \
                            + USE_UKSS_6 + USE_UKSS_7 \
                            + USE_UKSS_8 + USE_UKSS_9 \
                            + USE_UKSS_10 + USE_UKSS_11 \
                            + USE_UKSS_12 + USE_UKSS_13 \
                            + USE_UKSS_14 + USE_UKSS_15)   // 2       //3//8 //количество CAN устройств передающих в системе CANMODBUS - кол-во Unites

///////////////////////////////////////////
// настройки CAN_Open
///////////////////////////////////////////

#ifdef INGITIM_CAN_OPEN
    #define MBOX0_CANOPEN       0x00000013 //0x180 t1PDO1
    #define MBOX1_CANOPEN       0x0000018d //0x280 t2PDO1
    #define MBOX2_CANOPEN       0x000000c5 //0x380 t3PDO1
    #define MBOX3_CANOPEN       0x12000000 //0x480 t4PDO1
    #define MBOX4_CANOPEN       0x16000000 //0x580 t4PDO1
    #define MBOX5_CANOPEN       0x1a000000 //0x680 t4PDO1

#define CANOPEN_CAN_SETUP_DEFAULT { {MBOX0_CANOPEN,MBOX1_CANOPEN,MBOX2_CANOPEN,0,0,0,0,0,0,0,0,0,0,0,0,0},  \
                                {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},  \
                                {0,1,2,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},  \
                                {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}, \
                                3}

#endif


#ifdef BWC_CAN_FATEC
    #define MBOX0_CANOPEN       0x08040000  //201; 010 0000 0001
    #define MBOX1_CANOPEN       0x0C040000  //301; 011 0000 0001
    #define MBOX2_CANOPEN       0x10040000 //401; 100 0000 0001
    #define MBOX3_CANOPEN       0x14040000 //501; 101 0000 0001
    #define MBOX4_CANOPEN       0x08080000 //202; 010 0000 0010
    #define MBOX5_CANOPEN       0x0C080000 //302; 011 0000 0010
    #define MBOX6_CANOPEN       0x10080000 //402; 100 0000 0010
    #define MBOX7_CANOPEN       0x14080000 //502; 101 0000 0010

#define CANOPEN_CAN_SETUP_DEFAULT { {MBOX0_CANOPEN,MBOX1_CANOPEN,MBOX2_CANOPEN,MBOX3_CANOPEN,MBOX4_CANOPEN,MBOX5_CANOPEN,MBOX6_CANOPEN,MBOX7_CANOPEN,0,0,0,0,0,0,0,0},  \
                                {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},  \
                                {0,1,2,3,4,5,6,7,-1,-1,-1,-1,-1,-1,-1,-1},  \
                                {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}, \
                                0}

#endif

#ifdef BWC_CAN_SIEMENS
    #define MBOX0_CANOPEN       0x00040000  //  0x08040000  //201; 010 0000 0001
    #define MBOX1_CANOPEN       0x00080000  //301; 011 0000 0001

#define CANOPEN_CAN_SETUP_DEFAULT { {MBOX0_CANOPEN,MBOX1_CANOPEN,0,0,0,0,0,0,0,0,0,0,0,0,0,0},  \
                                {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},  \
                                {0,1,2,3,4,5,6,7,-1,-1,-1,-1,-1,-1,-1,-1},  \
                                {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}, \
                                0}
#endif


#ifndef CANOPEN_CAN_SETUP_DEFAULT
#define CANOPEN_CAN_SETUP_DEFAULT { {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},  \
                                {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},  \
                                {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},  \
                                {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}, \
                                0}
#endif


/////////////////////////////////////////////////////////////////////





extern int CAN_input_data[];
extern int CAN_output_data[];
extern int* CAN_output;

extern int flag_enable_can_from_mpu;


/////////////////////////////////////////////////////////////////////


#define UNIT_QUA	        32      //12 // ­  количество CAN устройств в системе - Ящиков



#define TERMINAL_UNIT_LEN   128
#define UNIT_LEN	        128
#define FIFO_LEN	        10
#define NEW_FIFO_LEN        128
#define NEW_CYCLE_FIFO_LEN        256
#define NEW_CYCLE_FIFO_LEN_MASK        (NEW_CYCLE_FIFO_LEN-1)




////////////////////////////////////////////////////
////////////////////////////////////////////////////
////////////////////////////////////////////////////

typedef struct
{
    int * adr_from;
    unsigned int adr_to;
    unsigned int adr;
	unsigned int quant;
	int busy;
	int FLY;
	int extended;

} CYCLE;


typedef struct
{
    int * adr_from;
    unsigned long adr_to;
    unsigned long adr;
    unsigned long quant;
    int busy;
    int FLY;
    int extended;
    int box;
    int priority;
    unsigned int quant_block;

} NEW_CYCLE_DATA;

typedef struct
{
	int box;
	long hiword;
	long loword;

} PACK;



typedef struct
{
	int adr;
	PACK pak[FIFO_LEN];

}FIFO;


typedef struct
{
    int index_data;         // текущий индекс при загрузке данными
    int index_send;         // текущий индекс при передаче
    int flag_inter;         // мы ждем сброса в прерывании
    unsigned int count_lost; // количество потерь
    unsigned int count_load;       // загрузка массива
    unsigned int count_free;       // свобода массива
    NEW_CYCLE_DATA cycle_data[NEW_CYCLE_FIFO_LEN];
    int cycle_box[UNIT_QUA];
    int lost_box[UNIT_QUA];


}NEW_CYCLE_FIFO;

//////////////////////////////////////
//////////////////////////////////////
//////////////////////////////////////

#define USE_BOX	            1
#define NOT_USE_BOX	        0

#define CAN_BOX_TYPE_IN		1
#define CAN_BOX_TYPE_OUT	2


#define FREE_TYPE_BOX 		    0
#define UNITS_TYPE_BOX 		    1
#define MPU_TYPE_BOX 		    2
#define CANOPEN_TYPE_BOX 	    3
#define SMPU_TYPE_BOX 		    4
#define TERMINAL_TYPE_BOX       5
#define ALARM_LOG_TYPE_BOX       6

/////////////////////////////////////////////////////////////////////

#define CAN_BOX_PRIORITY_NORMAL     0
#define CAN_BOX_PRIORITY_LOW        -1
#define CAN_BOX_PRIORITY_HIGH       1

/////////////////////////////////////////////////////////////////////
#define CAN_BOX_EXTENDED_ADR    1
#define CAN_BOX_STANDART_ADR    0
/////////////////////////////////////////////////////////////////////
#define CAN_BOX_STAT_ON         1
#define CAN_BOX_STAT_OFF        0
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////

typedef struct {
  long can_in_mbox_adr[16];
  long can_out_mbox_adr[16];
  int adr_box[16];
  int adr_in_mbox[16];

  int max_number;

} CANOPEN_CAN_SETUP;

/////////////////////////////////////////////////////////////////////
typedef struct {
  long can_mbox_adr[32];
//  long can_out_mbox_adr[16];
  int type_box[32];
  int local_number_box[32];
  int type_in_out_box[32];

  int max_number_in;
  int max_number_out;

} MAILBOXS_CAN_SETUP;

#define MAILBOXS_CAN_SETUP_DEFAULT { {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},  \
								{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},  \
								{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},  \
								{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},  \
								0, \
								0}


/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////


typedef struct {
  unsigned long can_base_adr;
  long can_in_mbox_adr[MAX_COUNT_UNITES_UKSS];
  long can_out_mbox_adr[MAX_COUNT_UNITES_UKSS];
  int adr_box[MAX_COUNT_UNITES_UKSS+1];
  int adr_in_mbox[MAX_COUNT_UNITES_UKSS+1];
  int adr_out_mbox[MAX_COUNT_UNITES_UKSS+1];

  int active_box[MAX_COUNT_UNITES_UKSS];
  int adr_detect_refresh[MAX_COUNT_UNITES_UKSS];
  int revers_box[MAX_COUNT_UNITES_UKSS];

  unsigned int CAN_count_cycle_input_units[MAX_COUNT_UNITES_UKSS];

  int max_number;

} UNITES_CAN_SETUP;

/////////////////////////////////////////////////////////////////////

typedef struct {
  unsigned long can_base_adr;
  long can_in_mbox_adr[MAX_COUNT_UNITES_MPU];
  long can_out_mbox_adr[MAX_COUNT_UNITES_MPU];
  int adr_box[MAX_COUNT_UNITES_MPU];

  int adr_in_mbox[MAX_COUNT_UNITES_MPU];
  int adr_out_mbox[MAX_COUNT_UNITES_MPU];

  int active_box[MAX_COUNT_UNITES_MPU];

  unsigned int CAN_count_cycle_input_units[MAX_COUNT_UNITES_MPU];


  int adr_detect_refresh[MAX_COUNT_UNITES_MPU];

  int max_number;

} MPU_CAN_SETUP;

/////////////////////////////////////////////////////////////////////

typedef struct {
  unsigned long can_base_adr;
  long can_in_mbox_adr[MAX_COUNT_UNITES_TERMINAL];
  long can_out_mbox_adr[MAX_COUNT_UNITES_TERMINAL];
  int adr_box[MAX_COUNT_UNITES_TERMINAL];

  int adr_in_mbox[MAX_COUNT_UNITES_TERMINAL];
  int adr_out_mbox[MAX_COUNT_UNITES_TERMINAL];

  int active_box[MAX_COUNT_UNITES_TERMINAL];

  unsigned int CAN_count_cycle_input_units[MAX_COUNT_UNITES_TERMINAL];

  int max_number;

} TERMINAL_CAN_SETUP;

////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////

typedef struct {
  unsigned long can_base_adr;
  long can_in_mbox_adr[MAX_COUNT_UNITES_ALARM_LOG];
  long can_out_mbox_adr[MAX_COUNT_UNITES_ALARM_LOG];
  int adr_box[MAX_COUNT_UNITES_ALARM_LOG];

  int adr_in_mbox[MAX_COUNT_UNITES_ALARM_LOG];
  int adr_out_mbox[MAX_COUNT_UNITES_ALARM_LOG];

  int active_box[MAX_COUNT_UNITES_ALARM_LOG];

  unsigned int CAN_count_cycle_input_units[MAX_COUNT_UNITES_ALARM_LOG];

  int max_number;

} ALARM_LOG_CAN_SETUP;

////////////////////////////////////////////////////////////////////////////////
#define _UNITS_DEFAULT_ZERO             {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
#define _UNITS_DEFAULT_MINUS_ONE        {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}

#define UNITES_CAN_SETUP_DEFAULT  { START_CAN_ADR_UNITS_DEFAULT, _UNITS_DEFAULT_ZERO,  \
                                _UNITS_DEFAULT_ZERO,  \
                                _UNITS_DEFAULT_MINUS_ONE, \
                                _UNITS_DEFAULT_MINUS_ONE, \
                                _UNITS_DEFAULT_MINUS_ONE, \
								{USE_UKSS_0,USE_UKSS_1,USE_UKSS_2,USE_UKSS_3,USE_UKSS_4,USE_UKSS_5, \
								USE_UKSS_6,USE_UKSS_7,USE_UKSS_8,USE_UKSS_9,USE_UKSS_10, \
								USE_UKSS_11,USE_UKSS_12,USE_UKSS_13,USE_UKSS_14,USE_UKSS_15},  \
								_UNITS_DEFAULT_ZERO, \
								{USE_R_B_0,USE_R_B_1,USE_R_B_2,USE_R_B_3,USE_R_B_4,USE_R_B_5,USE_R_B_6,USE_R_B_7,USE_R_B_8, \
								 USE_R_B_9,USE_R_B_10,USE_R_B_11,USE_R_B_12,USE_R_B_13,USE_R_B_14,USE_R_B_15}, \
								 _UNITS_DEFAULT_ZERO,  \
								0}

/////////////////////////////////////////////////////////////////////

#define _MPU_DEFAULT_ZERO             {0,0,0,0}
#define _MPU_DEFAULT_MINUS_ONE        {-1,-1,-1,-1}

#define  MPU_CAN_SETUP_DEFAULT  { CAN_ADR_MPU_DEFAULT, _MPU_DEFAULT_ZERO,  \
                                _MPU_DEFAULT_ZERO,  \
                                _MPU_DEFAULT_MINUS_ONE, \
                                _MPU_DEFAULT_MINUS_ONE, \
                                _MPU_DEFAULT_MINUS_ONE, \
								{USE_MPU_0,USE_MPU_1,USE_MPU_2,USE_MPU_3},  \
								_MPU_DEFAULT_ZERO,  \
								_MPU_DEFAULT_ZERO,  \
								0}

//-------------------------------------------------------------------------------//

#define _TERMINAL_DEFAULT_ZERO             {0,0,0,0}
#define _TERMINAL_DEFAULT_MINUS_ONE        {-1,-1,-1,-1}

#define  TERMINAL_CAN_SETUP_DEFAULT  {CAN_ADR_TERMINAL_DEFAULT, _TERMINAL_DEFAULT_ZERO,  \
                                _TERMINAL_DEFAULT_ZERO,  \
                                _TERMINAL_DEFAULT_MINUS_ONE, \
                                _TERMINAL_DEFAULT_MINUS_ONE, \
                                _TERMINAL_DEFAULT_MINUS_ONE, \
                                {USE_TERMINAL_1_OSCIL,USE_TERMINAL_1_CMD,USE_TERMINAL_2_OSCIL,USE_TERMINAL_2_CMD},  \
                                _TERMINAL_DEFAULT_ZERO,  \
                                0}
//-------------------------------------------------------------------------------//
#define _ALARM_LOG_DEFAULT_ZERO             {0,0}
#define _ALARM_LOG_DEFAULT_MINUS_ONE        {-1,-1}

#define  ALARM_LOG_CAN_SETUP_DEFAULT  {CAN_ADR_ALARM_LOG_DEFAULT, _ALARM_LOG_DEFAULT_ZERO,  \
                                _ALARM_LOG_DEFAULT_ZERO,  \
                                _ALARM_LOG_DEFAULT_MINUS_ONE, \
                                _ALARM_LOG_DEFAULT_MINUS_ONE, \
                                _ALARM_LOG_DEFAULT_MINUS_ONE, \
                                {USE_ALARM_LOG_0,USE_ALARM_LOG_1},  \
                                _ALARM_LOG_DEFAULT_ZERO,  \
                                0}

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

typedef struct {
    WORD_INT2BITS_STRUCT  buf[TERMINAL_UNIT_LEN];
} TERMINAL_UNITES_STRUCT;
////
typedef TERMINAL_UNITES_STRUCT *TERMINAL_UNITES_STRUCT_handle;
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////


extern int TerminalUnites[TERMINAL_UNIT_QUA_UNITS][TERMINAL_UNIT_LEN];

extern int Unites[UNIT_QUA_UNITS][UNIT_LEN];


////////////////////////////////////////////////////////////////////////////////
extern CYCLE cycle[];	//	26.01.2011 Dimas


extern NEW_CYCLE_FIFO new_cycle_fifo;

extern int CanOpenUnites[CANOPENUNIT_LEN];

//add yura
extern MAILBOXS_CAN_SETUP mailboxs_can_setup;

extern FIFO fifo;

extern int CAN_timeout[];
extern int CAN_request_sent[];
extern unsigned int CAN_timeout_cicle[];


//////////////////////////////////////////////////
//////////////////////////////////////////////////
int init_units_can_boxs(UNITES_CAN_SETUP *p);
int init_canopen_can_boxs(CANOPEN_CAN_SETUP *p);
int init_mpu_can_boxs(MPU_CAN_SETUP *p );
int init_terminal_can_boxs(TERMINAL_CAN_SETUP *p );
int init_alarm_log_can_boxs(ALARM_LOG_CAN_SETUP *p );
//////////////////////////////////////////////////

void init_mailboxs_can( UNITES_CAN_SETUP    *p_units,
                        MPU_CAN_SETUP       *p_mpu,
                        TERMINAL_CAN_SETUP  *p_terminal,
                        ALARM_LOG_CAN_SETUP *p_alarm_log,
                        CANOPEN_CAN_SETUP   *p_canopen,
                        MAILBOXS_CAN_SETUP  *p_mailboxs
                        );


void init_all_mailboxs(unsigned long can_base_adr_units, unsigned long can_base_adr_mpu, unsigned long can_base_adr_alarm_log, unsigned long can_base_adr_terminal);




void InitCan(unsigned long can_base_adr_units, unsigned long can_base_adr_mpu, unsigned long can_base_adr_alarm_log, unsigned long can_base_adr_terminal);


//void CAN_send(int box, unsigned long hiword, unsigned long loword);
//void CAN_word_send(int type_box, int box, int Addr, int Data);
//void CAN_array_send(int type_box, int box, int Addr, int * Data);

void CAN_cycle_send(int type_box, int box, unsigned long Addr, int * Data, unsigned long quant, int extended, int priority);

//void FIFO_send(int box, unsigned long hiword, unsigned long loword);

//int CAN_FLY_free(int box);
//int CAN_FIFO_free(unsigned int quant);

int CAN_cycle_free(int box);
int CAN_cycle_full_free(int box, int statistics_flag);

//void CAN_cycle_stop(int box);


//void CAN_cycle_step(int box);


void CAN_request(unsigned int addr, unsigned int quant);
void CAN_assign(unsigned int addr, unsigned int quant);


void reset_CAN_timeout_cicle(int box);
void inc_CAN_timeout_cicle();

unsigned int test_can_live_mpu(void);
unsigned int test_can_live_terminal(int n);
void InitCanSoft(void);

void timer_pause_enable_can_from_mpu(void);
void timer_pause_enable_can_from_terminal(void);
void read_manch_can(void);
void write_manch_can(void);
void detect_time_refresh_units(int box, int adr);
void detect_time_refresh_mpu(int box, int adr);



void parse_data_from_mbox(unsigned int box, unsigned long hiword,
                                            unsigned long loword);


int get_real_out_mbox(int type_box, int box);
int get_real_in_mbox(int type_box, int box);



void messagePaserToUnitesIngitim(int box, unsigned long h_word, unsigned long l_word);




//////////////////
void new_fifo_calc_load(void);
int new_fifo_load(int box,unsigned long hiword, unsigned long loword);

int new_cycle_fifo_load(int box, unsigned long adr, int * adr_from,  unsigned long addr_to, unsigned long quant, int extended, int priority, int cmd_wait);

int get_new_cycle_fifo_load_level(void);


void CAN_send2(int box,unsigned long hiword, unsigned long loword);
int CAN_cycle_fifo_step(void);
int CAN_cycle_fifo_one_box(void);

//////////////////
int CAN_may_be_send_cycle_fifo(void);

void stop_can_interrupt(void);
void start_can_interrupt(void);


//// Prototype statements for functions found within this file.
interrupt void CAN_handler(void);
interrupt void CAN_reset_err(void);



extern UNITES_CAN_SETUP unites_can_setup;
extern MPU_CAN_SETUP mpu_can_setup;


extern unsigned int CanTimeOutErrorTR;
extern unsigned int CanBusOffError;

#endif	// _CAN_SETUP

