/****************************************************************************
 Title	:	TERMINAL FOR VISUALIZATION AND CONTROL CONDITIONS IN HOUSE
 Author	:	Patrik Szalay <xszala01@stud.feec.vutbr.cz>
 File	:	serial.h
 Software:	CodeWarrior Development Studio Version: 10.6 Build Id:140329
 Target	:	MC9S08LH64
*****************************************************************************/

#ifndef SERIAL_H_
#define SERIAL_H_

#include <hidef.h>
#include "derivative.h"
#include "temp_meas.h"
#include "IO.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
//#include "LCD_GD-5360P.h"
#include "regulation.h"
//#include "clock.h"
#include "UI.h"


#define	OSCCLK_FREQ_KHZ		32768L	/* "L" postfix evaluated as a long value */
#define BUSCLK_FREQ_KHZ		4194L	/* "L" postfix evaluated as a long value */

	/* Bus frequency in kHz = fosc / 2 */
#define BUSCLK_FREQ_KHZ_OSC		OSCCLK_FREQ_KHZ/2



/* SCI defines */ 
#define TERMINAL_BAUD_RATE	9600
#define SCI_BUFFER_LENGHT	160	/* MUST BE AT LEAST 20 */
#define nullvalue 			0
#define SCIBD_BAUD_RATE ((BUSCLK_FREQ_KHZ * 10) / (16 * (TERMINAL_BAUD_RATE/100)))
#define SCIBD_BD_9600		27
#define SCIBD_BD_14400		18
#define SCIBD_BD_19200		14
#define SCIBD_BD_38400		7


#define SCI_CRC_POLYNOMIAL 0x0123
#define SCI_SHIFTED_DIVISOR 0x918000 /* This is the 0x0123 polynomial shifted to farthest left of three bytes */

#define LOCAL_TERM_ADDR 0x10
#define GUI_ADDR 0x20
#define MATLAB_ADDR 0x30

/** SCIS - SCI Status Register;  **/
typedef union {
  byte Byte;
  struct {
    byte SCISDRX     :1;                                        /* SCI Status Register Data Received Bit */
    byte SCISCONN    :1;                                        /* SCI Status Register Connected Bit */ 
    byte SCISACK     :1;                                        /* SCI Status Register Acknowledge Bit */
    byte SCISDTX     :1;                                        /* SCI Status Register Data Transferring Bit */
    byte SCISACKRQ   :1;                                        /* SCI Status Register Acknowledgement Required Bit */
    byte SCIS5       :1;                                        /* SCI Status Register Bit 5 */
    byte SCIS6       :1;                                        /* SCI Status Register Bit 6 */
    byte SCIS7       :1;                                        /* SCI Status Register Bit 7 */
  } Bits;
} SCISSTR;
extern volatile SCISSTR SCIS;
#define SCISReg                         SCIS.Byte
#define SCIS_SCISDRX                    SCIS.Bits.SCISDRX
#define SCIS_SCISCONN                   SCIS.Bits.SCISCONN
#define SCIS_SCISACK                    SCIS.Bits.SCISACK
#define SCIS_SCISDTX                    SCIS.Bits.SCISDTX
#define SCIS_SCISACKRQ                  SCIS.Bits.SCISACKRQ
#define SCIS_SCIS5                      SCIS.Bits.SCIS5
#define SCIS_SCIS6                      SCIS.Bits.SCIS6
#define SCIS_SCIS7                      SCIS.Bits.SCIS7

#define SCIS_SCISDRX_MASK               1U
#define SCIS_SCISCONN_MASK              2U
#define SCIS_SCISACK_MASK               4U
#define SCIS_SCISDTX_MASK               8U
#define SCIS_SCISACKRQ_MASK             16U
#define SCIS_SCIS5_MASK                 32U
#define SCIS_SCIS6_MASK                 64U
#define SCIS_SCIS7_MASK                 128U

extern byte SCIACKRQ_TOut;
#define ACKRQTIMEOUT	2


void InitSCI (void);


void SCITransmit(unsigned int bytes, char* ptr);
void SCIRead(void);
void SCISendMessage(ADDRESS address, byte task, SCIRegAddress regAddress, byte bytes, byte *data);
byte CRC(byte* message, byte length);
void SCIRingACK(ADDRESS srcAddress);
void ConvertValueToMessage(char *res, decimal value);
void SCISendMeas(ADDRESS dstAddress, SCIRegAddress device);
void SCIForbidden(ADDRESS dstAddress, SCIRegAddress reg);
void SCISendERR(ADDRESS dstAddress, SCIRegAddress reg);
void SCIRetransmit(void);
void SCIRTX(ADDRESS srcAddress, SCIRegAddress reg);
void SCIResetTimeOut(void);
void SCIResetSyst(ADDRESS srcAddress);
void SCIDisconnect(ADDRESS srcAddress);
bool SCIConnCheck(ADDRESS srcAddress);
bool SCIACKCheck(ADDRESS srcAddress, SCIRegAddress reg);
void SCIACKRQ(ADDRESS address, SCIRegAddress reg);
ADDRESS SCIGetACKAddress(void);
decimal ConvertDataToDecimalValue(byte *data);
void SCISet(ADDRESS srcAddress, SCIRegAddress reg, byte *data);
void SCISerGet(ADDRESS dstAddress, byte regCount, byte *dataBuffer);
void SCISerSet(ADDRESS dstAddress, byte regCount, byte *dataBuffer);
void setClock(byte *data);


interrupt VectorNumber_Vsci1tx void ISR_SCI_TC (void);
interrupt VectorNumber_Vsci1rx void ISR_SCI_RX (void);
#endif /* SERIAL_H_ */
