/**********************************************************************
 *****  INCLUDE FILE FOR HW DEFINITION IN CORE LIBRARY	          *****
 *****  (WARNING: do not change variable names				      *****
 *****                                                            *****
 *****  Name: core_lib.h                                          *****
 *****  Desc: hardware layer definitions					      *****
 *****  Ver.: 1.0                                                 *****
 *****  Date: 01/2011		                                      *****
 *****  Auth: Martin Mechacek		                              *****
 *****  Comp: ESTELAR s.r.o.									  *****
 *****                                                            *****
 *****  Copyright ESTELAR s.r.o. (c) 2010, all rights reserved    *****
 **********************************************************************/ 

#ifndef _DEF_GR_U01_H

#define _DEF_GR_U01_H


/////////////////////// IMPORTANT /////////////////////////////////////
/////  Your project must be compiled with define: CORE_GR_U01 /////////	
/////  														  /////////	
/////  unit: GR-U01 										  /////////	
/////  desc: ridici jednotka do rozvadece					  /////////	


/**********************************************************************
 *****  IMPLEMENTATION AREA							 			   ****
 *****  set specification according to your hardware		       ****
 *********************************************************************/ 


//========================================================================
//>> define hw pin access
struct _st_io {                                          /* struct IO    */
             union {                                    /* PUCR1        */
                   unsigned char BYTE;                  /*  Byte Access */
                   struct {                             /*  Bit  Access */
                          unsigned char B7:1;           /*    Bit 7     */
                          unsigned char B6:1;           /*    Bit 6     */
                          unsigned char B5:1;           /*    Bit 5     */
                          unsigned char B4:1;           /*    Bit 4     */
                          unsigned char   :1;           /*    Bit 3     */
                          unsigned char B2:1;           /*    Bit 2     */
                          unsigned char B1:1;           /*    Bit 1     */
                          unsigned char B0:1;           /*    Bit 0     */
                          }      BIT;                   /*              */
                   }            PUCR1;                  /*              */
             union {                                    /* PUCR5        */
                   unsigned char BYTE;                  /*  Byte Access */
                   struct {                             /*  Bit  Access */
                          unsigned char   :2;           /*    Bit 7,6   */
                          unsigned char B5:1;           /*    Bit 5     */
                          unsigned char B4:1;           /*    Bit 4     */
                          unsigned char B3:1;           /*    Bit 3     */
                          unsigned char B2:1;           /*    Bit 2     */
                          unsigned char B1:1;           /*    Bit 1     */
                          unsigned char B0:1;           /*    Bit 0     */
                          }      BIT;                   /*              */
                   }            PUCR5;                  /*              */
             char               wk1[2];                 /*              */
             union {                                    /* PDR1         */
                   unsigned char BYTE;                  /*  Byte Access */
                   struct {                             /*  Bit  Access */
                          unsigned char B7:1;           /*    Bit 7     */
                          unsigned char B6:1;           /*    Bit 6     */
                          unsigned char B5:1;           /*    Bit 5     */
                          unsigned char B4:1;           /*    Bit 4     */
                          unsigned char   :1;           /*    Bit 3     */
                          unsigned char B2:1;           /*    Bit 2     */
                          unsigned char B1:1;           /*    Bit 1     */
                          unsigned char B0:1;           /*    Bit 0     */
                          }      BIT;                   /*              */
                   }            PDR1;                   /*              */
             union {                                    /* PDR2         */
                   unsigned char BYTE;                  /*  Byte Access */
                   struct {                             /*  Bit  Access */
                          unsigned char   :3;           /*    Bit 7-5   */
                          unsigned char B4:1;           /*    Bit 4     */
                          unsigned char B3:1;           /*    Bit 3     */
                          unsigned char B2:1;           /*    Bit 2     */
                          unsigned char B1:1;           /*    Bit 1     */
                          unsigned char B0:1;           /*    Bit 0     */
                          }      BIT;                   /*              */
                   }            PDR2;                   /*              */
             union {                                    /* PDR3         */
                   unsigned char BYTE;                  /*  Byte Access */
                   struct {                             /*  Bit  Access */
                          unsigned char B7:1;           /*    Bit 7     */
                          unsigned char B6:1;           /*    Bit 6     */
                          unsigned char B5:1;           /*    Bit 5     */
                          unsigned char B4:1;           /*    Bit 4     */
                          unsigned char B3:1;           /*    Bit 3     */
                          unsigned char B2:1;           /*    Bit 2     */
                          unsigned char B1:1;           /*    Bit 1     */
                          unsigned char B0:1;           /*    Bit 0     */
                          }      BIT;                   /*              */
                   }            PDR3;                   /*              */
             char               wk2;                    /*              */
             union {                                    /* PDR5         */
                   unsigned char BYTE;                  /*  Byte Access */
                   struct {                             /*  Bit  Access */
                          unsigned char B7:1;           /*    Bit 7     */
                          unsigned char B6:1;           /*    Bit 6     */
                          unsigned char B5:1;           /*    Bit 5     */
                          unsigned char B4:1;           /*    Bit 4     */
                          unsigned char B3:1;           /*    Bit 3     */
                          unsigned char B2:1;           /*    Bit 2     */
                          unsigned char B1:1;           /*    Bit 1     */
                          unsigned char B0:1;           /*    Bit 0     */
                          }      BIT;                   /*              */
                   }            PDR5;                   /*              */
             union {                                    /* PDR6         */
                   unsigned char BYTE;                  /*  Byte Access */
                   struct {                             /*  Bit  Access */
                          unsigned char B7:1;           /*    Bit 7     */
                          unsigned char B6:1;           /*    Bit 6     */
                          unsigned char B5:1;           /*    Bit 5     */
                          unsigned char B4:1;           /*    Bit 4     */
                          unsigned char B3:1;           /*    Bit 3     */
                          unsigned char B2:1;           /*    Bit 2     */
                          unsigned char B1:1;           /*    Bit 1     */
                          unsigned char B0:1;           /*    Bit 0     */
                          }      BIT;                   /*              */
                   }            PDR6;                   /*              */
             union {                                    /* PDR7         */
                   unsigned char BYTE;                  /*  Byte Access */
                   struct {                             /*  Bit  Access */
                          unsigned char   :1;           /*    Bit 7     */
                          unsigned char B6:1;           /*    Bit 6     */
                          unsigned char B5:1;           /*    Bit 5     */
                          unsigned char B4:1;           /*    Bit 4     */
                          unsigned char   :1;           /*    Bit 3     */
                          unsigned char B2:1;           /*    Bit 2     */
                          unsigned char B1:1;           /*    Bit 1     */
                          unsigned char B0:1;           /*    Bit 0     */
                          }      BIT;                   /*              */
                   }            PDR7;                   /*              */
             union {                                    /* PDR8         */
                   unsigned char BYTE;                  /*  Byte Access */
                   struct {                             /*  Bit  Access */
                          unsigned char B7:1;           /*    Bit 7     */
                          unsigned char B6:1;           /*    Bit 6     */
                          unsigned char B5:1;           /*    Bit 5     */
                          }      BIT;                   /*              */
                   }            PDR8;                   /*              */
             char               wk3;                    /*              */
             union {                                    /* PDRB         */
                   unsigned char BYTE;                  /*  Byte Access */
                   struct {                             /*  Bit  Access */
                          unsigned char B7:1;           /*    Bit 7     */
                          unsigned char B6:1;           /*    Bit 6     */
                          unsigned char B5:1;           /*    Bit 5     */
                          unsigned char B4:1;           /*    Bit 4     */
                          unsigned char B3:1;           /*    Bit 3     */
                          unsigned char B2:1;           /*    Bit 2     */
                          unsigned char B1:1;           /*    Bit 1     */
                          unsigned char B0:1;           /*    Bit 0     */
                          }      BIT;                   /*              */
                   }            PDRB;                   /*              */
             union {                                    /* PDRC         */
                   unsigned char BYTE;                  /*  Byte Access */
                   struct {                             /*  Bit  Access */
                          unsigned char   :6;           /*              */
                          unsigned char B1:1;           /*    Bit 1     */
                          unsigned char B0:1;           /*    Bit 0     */
                          }      BIT;                   /*              */
                   }            PDRC;                   /*              */
             char               wk4;                    /*              */
             union {                                    /* PMR1         */
                   unsigned char BYTE;                  /*  Byte Access */
                   struct {                             /*  Bit  Access */
                          unsigned char IRQ3:1;         /*    IRQ3      */
                          unsigned char IRQ2:1;         /*    IRQ2      */
                          unsigned char IRQ1:1;         /*    IRQ1      */
                          unsigned char IRQ0:1;         /*    IRQ0      */
                          unsigned char TXD2:1;         /*    TXD2      */
                          unsigned char _PWM:1;         /*    PWM       */
                          unsigned char TXD :1;         /*    TXD       */
                          unsigned char TMOW:1;         /*    TMOW      */
                          }      BIT;                   /*              */
                   }            PMR1;                   /*              */
             union {                                    /* PMR5         */
                   unsigned char BYTE;                  /*  Byte Access */
                   struct {                             /*  Bit  Access */
                          unsigned char POF57:1;        /*    POF57     */
                          unsigned char POF56:1;        /*    POF56     */
                          unsigned char WKP5 :1;        /*    WKP5      */
                          unsigned char WKP4 :1;        /*    WKP4      */
                          unsigned char WKP3 :1;        /*    WKP3      */
                          unsigned char WKP2 :1;        /*    WKP2      */
                          unsigned char WKP1 :1;        /*    WKP1      */
                          unsigned char WKP0 :1;        /*    WKP0      */
                          }      BIT;                   /*              */
                   }            PMR5;                   /*              */
             union {                                    /* PMR3         */
                   unsigned char BYTE;                  /*  Byte Access */
                   struct {                             /*  Bit  Access */
                          unsigned char      :3;        /*              */
                          unsigned char POF24:1;        /*    POF24     */
                          unsigned char POF23:1;        /*    POF23     */
                          }      BIT;                   /*              */
                   }            PMR3;                   /*              */
             char               wk5;                    /*              */
             unsigned char      PCR1;                   /* PCR1         */
             unsigned char      PCR2;                   /* PCR2         */
             unsigned char      PCR3;                   /* PCR3         */
             char               wk6;                    /*              */
             unsigned char      PCR5;                   /* PCR5         */
             unsigned char      PCR6;                   /* PCR6         */
             unsigned char      PCR7;                   /* PCR7         */
             unsigned char      PCR8;                   /* PCR8         */
             char               wk7[2];                 /*              */
             unsigned char      PCRC;                   /* PCRC         */
};                                                      /*              */
#define _IO      (*(volatile struct _st_io    *)0xFFFFD0) /* IO    Address*/



#if HW_VERSION < 3

// define RS485 bus connections - verze HW 1-2
// vysilani dat na sbernici probiha pouze aktivaci budice RS485 signalem DE (VTXD)

// vystup TxD musi byt trvale v '0'
  #define RS485_DRIVER_TX_LO	  _IO.PDR2.BYTE &= ~0x04   	// nastavuje '0' na pin TxD - P22

// pin RxD je pripojen standartne na RxD vstup UARTU - P21, musi byt fyzicky propojen na VRXD
// pin VRXD slouzi pro zpetny odposlech stavu sbernice pro vyhodnoceni kolize
  #define VRXD 				  _IO.PDR1.BIT.B0				// definice pinu VRXD - P10
  
#else

// define RS485 bus connections - od verze HW 3
// vysilani dat na sbernici probiha pouze aktivaci budice RS485 signalem DE (VTXD)

// vstup budice DI (TxD) je na desce trvale v '0'  
  #define RS485_DRIVER_TX_LO	  ;;						// pin je hardwarove pripojen na GND

// pin RxD je pripojen standartne na RxD vstup UARTU - P21, musi byt fyzicky propojen na VRXD
// pin VRXD slouzi pro zpetny odposlech stavu sbernice pro vyhodnoceni kolize pres interrupt
  #define VRXD 				  (_IO.PDR1.BIT.B4)				// definice pinu VRXD-P14 = IRQ0

#endif

// data se vysilaji pomoci signalu DE - pin P20 shodne pro vsechny verze HW
// pokud DE=1 na sbernici se zapisuje 0 a opacne, v klidu je nastaven na Lo
  #define SET_VTXD(data) 	if(data) _IO.PDR2.BIT.B0 = 1; else _IO.PDR2.BIT.B0 = 0		// nastavuje hodnotu na pin DE - P20

//========================================================================
//>> BUS_BAUDRATE 
// 19 200 bd serial constant definifion
// konstanty BUS_FREE_TIMEOUT, BUS_WAIT_TIMEOUT jsou definovany podle rychlosti volani BusTimer()
// ostatni se musi presne odmerit osciloskopem podle rychlosti procesoru, mista pro mereni jsou oznacena ve funkci BusPutChar
//------------------------------------------------------------------------

  #define 	BUS_BAUDRATE    	19200	// prenosova rychlost na sbernici	
  #define	BUS_FREE_TIMEOUT	3		// 5 ms - pokud trva klid na lince tuto dobu, bude nastaven priznak BUS_FREE (cca 10 znaku) - !! v praxi 5 - 6 ms 
  #define	BUS_WAIT_TIMEOUT	15		// doba cekani na nastaveni priznaku BUS_FREE (5x BUS_FREE_TIMEOUT)

  #define 	PERIOD10us			7		// konstanta pro dobu aktivace driveru RS485 - 10 us - hodnota neni kriticka
  #define 	BITPERIOD   		36		// konstanta pro dobu trvani jednoho bitu - 50us
  #define 	BITPERIOD1   		22		// doba trvani jednoho bitu - pokud se zapisuje jednicka ve smycce je vice instrukci proto je kratsi)
										// pro rychlost 19200 je ve smycce vlozena instrukce NOP pro dosazeni presneho casovani 
										// hodnota je svazana s PERIOD10us, presne nastaveni je mozne doladit zmenou PERIOD10us (+-1)
// pro rychlost 19200 je doba prenosu 1 byte 468us
// prodleva mezi jednotlivymi byty je cca 20us - pokud je procesor prilis rychly vlozi se prodleva 
  #define 	BYTEMARKER   		0		// doplneni prodlevy
	
//<< end of BUS_BAUDRATE


//========================================================================
//>> define interrupts controll
struct _st_sci3 {                                       /* struct SCI3  */
               union {                                  /* SMR          */
                     unsigned char BYTE;                /*  Byte Access */
                     struct {                           /*  Bit  Access */
                            unsigned char COM :1;       /*    COM       */
                            unsigned char CHR :1;       /*    CHR       */
                            unsigned char PE  :1;       /*    PE        */
                            unsigned char PM  :1;       /*    PM        */
                            unsigned char STOP:1;       /*    STOP      */
                            unsigned char MP  :1;       /*    MP        */
                            unsigned char CKS :2;       /*    CKS       */
                            }      BIT;                 /*              */
                     }          SMR;                    /*              */
               unsigned char    BRR;                    /* BRR          */
               union {                                  /* SCR3         */
                     unsigned char BYTE;                /*  Byte Access */
                     struct {                           /*  Bit  Access */
                            unsigned char TIE :1;       /*    TIE       */
                            unsigned char RIE :1;       /*    RIE       */
                            unsigned char TE  :1;       /*    TE        */
                            unsigned char RE  :1;       /*    RE        */
                            unsigned char MPIE:1;       /*    MPIE      */
                            unsigned char TEIE:1;       /*    TEIE      */
                            unsigned char CKE :2;       /*    CKE       */
                            }      BIT;                 /*              */
                     }          SCR3;                   /*              */
               unsigned char    TDR;                    /* TDR          */
               union {                                  /* SSR          */
                     unsigned char BYTE;                /*  Byte Access */
                     struct {                           /*  Bit  Access */
                            unsigned char TDRE:1;       /*    TDRE      */
                            unsigned char RDRF:1;       /*    RDRF      */
                            unsigned char OER :1;       /*    OER       */
                            unsigned char FER :1;       /*    FER       */
                            unsigned char PER :1;       /*    PER       */
                            unsigned char TEND:1;       /*    TEND      */
                            unsigned char MPBR:1;       /*    MPBR      */
                            unsigned char MPBT:1;       /*    MPBT      */
                            }      BIT;                 /*              */
                     }          SSR;                    /*              */
               unsigned char    RDR;                    /* RDR          */
};                        

union _un_ienr1 {                                        /* union IENR1  */
               unsigned char BYTE;                      /*  Byte Access */
               struct {                                 /*  Bit  Access */
                      unsigned char IENDT:1;            /*    IENDT     */
                      unsigned char IENTA:1;            /*    IENTA     */
                      unsigned char IENWP:1;            /*    IENWP     */
                      unsigned char      :1;            /*              */
                      unsigned char IEN3 :1;            /*    IEN3      */
                      unsigned char IEN2 :1;            /*    IEN2      */
                      unsigned char IEN1 :1;            /*    IEN1      */
                      unsigned char IEN0 :1;            /*    IEN0      */
                      }      BIT;                       /*              */
};                                                      /*              */

union _un_irr1 {                                         /* union IRR1   */
              unsigned char BYTE;                       /*  Byte Access */
              struct {                                  /*  Bit  Access */
                     unsigned char IRRDT:1;             /*    IRRDT     */
                     unsigned char IRRTA:1;             /*    IRRTA     */
                     unsigned char      :2;             /*              */
                     unsigned char IRRI3:1;             /*    IRRI3     */
                     unsigned char IRRI2:1;             /*    IRRI2     */
                     unsigned char IRRI1:1;             /*    IRRI1     */
                     unsigned char IRRI0:1;             /*    IRRI0     */
                     }      BIT;                        /*              */
};                                                      /*              */
                              /*              */
#define _SCI3    (*(volatile struct _st_sci3  *)0xFFFFA8) /* SCI3  Address*/
#define _IENR1   (*(volatile union  _un_ienr1 *)0xFFFFF4) /* IENR1 Address*/
#define _IRR1    (*(volatile union  _un_irr1  *)0xFFFFF6) /* IRR1  Address*/

// define macros for sci interrupt enable/disable
#define RX_InterruptEnable		{  _SCI3.SCR3.BYTE   |= 0x40; }  	   // povol RX Interrupt
#define RX_InterruptDisable		{  _SCI3.SCR3.BYTE   &= ~0x40; }   	   // zakaz RX Interrupt

#define RX_ClearFlags			{  _SCI3.SSR.BYTE = 0; _SCI3.RDR; }    // vymazani prijijateho bytu a souvisejicich flagu

#define VRX_InterruptEnable		{  _IENR1.BIT.IEN0 = 1; }  	   		   // povol VRX Interrupt - IRQ0
#define VRX_InterruptDisable	{  _IENR1.BIT.IEN0 = 0; }   		   // zakaz VRX Interrupt - IRQ0
#define VRX_ClearFlags			{  _IRR1.BIT.IRRI0 = 0; }		       // vymazani flagu IRQ0

#define ALL_InterruptDisable	{  asm ("ldc %0l,ccr"::"r"(0xC0)); }    // zakazat vsechna preruseni pro presne casovani odesilani na sbernici
#define ALL_InterruptEnable		{  asm ("ldc %0l,ccr"::"r"(0)); }   	// povolit vsechna preruseni pro presne casovani odesilani na sbernici

// maximalni delka celeho jednoho paketu pro prijem i odesilani
// pokud je prijat delsi paket, bude korektne obslouzen, ale data z neho nebudou zpracovana
// soucasne generuje chybu ERR_SCI_LONG do gData.SciState
// aplikace by mela odeslat chybovou zpravu
// dle nastavene hodnoty se alokuje potrebna pamet, proto je treba prihlednout k HW moznostem konkretniho zarizeni
#define MAX_MESSAGE_LEN			32	

// maximalni delka prijimaciho bufferu ve kterem se ukladaji 
// prijata data z linky nez se zpracuji jako pakety - co nejvice podle velikosti dostupne RAM
// touto hodnotou se maskuje, tedy musi to byt vzdy souvisla rada jednicek !!!
#define RX_BUFF_SIZE			0x1ff		// 0x7f = 127 bytes, // 0xff = 256 bytes
// bufer musi byt co nejvetsi protoze jinak se data ztrati, napriklad pro hromadnem zapinani 10 rele

/////// end of IMPLEMENTATION AREA ///////////////////////////////////*/

#endif 		// __DEF_GR_U01


