#include <stdint.h>
#include <stdio.h>
#include <string.h> 
#include <math.h>
#include <stdlib.h>
#include <util/delay.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <avr/iocan128.h>
#include <avr/iocanxx.h>
#include <avr/eeprom.h>
#include <avr/wdt.h>

#include "gas_temp_monitor.h"
#include "canapi.h"
#include "can.h"
#include "canapi_define.h"
#include "canapi_global.h"
#include "led.h"
char pp[255];// nur für die Funktion  int8_t UART1_Send_Message_String
uint16_t subscribe_ID[MAX_LENGTH_SUBSCRIBE];
uint16_t subscribe_mask[MAX_LENGTH_SUBSCRIBE];

/* array for defined serial error*/
const uint8_t serial_error_number[]=
{ 
	S_ERROR0_NO , S_ERROR1_NO, S_ERROR2_NO , S_ERROR3_NO , S_ERROR4_N0 ,S_ERROR50_NO, S_ERROR51_NO , S_ERROR52_NO, S_ERROR53_NO , S_ERROR54_NO , S_ERROR55_NO  , S_ERROR56_NO , S_ERROR57_NO , S_ERROR6_NO , S_ERROR10_NO ,S_ERROR20_NO ,S_ERROR30_NO , S_ERROR40_NO , S_ERROR70_NO, S_ERROR71_NO, S_ERROR72_NO, S_ERROR73_NO,S_ERROR74_NO , S_ERROR75_NO ,S_ERROR76_NO ,
	S_ERROR77_NO , S_ERROR8_NO, S_ERROR9_NO, S_ERROR11_NO
};
/* pointer of array for defined serial error number*/
const char *serial_error[]=
{ 
	S_ERROR0, S_ERROR1, S_ERROR2, S_ERROR3, S_ERROR4, S_ERROR50, S_ERROR51, S_ERROR52, S_ERROR53, S_ERROR54, S_ERROR55, S_ERROR56, S_ERROR57, S_ERROR6, S_ERROR10,S_ERROR20, S_ERROR30, S_ERROR40, S_ERROR70, S_ERROR71, S_ERROR72, S_ERROR73, S_ERROR74, S_ERROR75, S_ERROR76,
	S_ERROR77, S_ERROR8, S_ERROR9, S_ERROR11
};

/*
 *this function initializes the UART 0 register needed to allow the at90can128 
 *USART transmissions
 */

 int8_t UART0_Init (void){
	 uint8_t intstate1 = SREG;/*save global interrupt flag*/
	 cli();/*disable interrupt*/
	 while(0<<TXC0){
	 }
	 /* set baud rate*/
	 UBRR0H = (uint8_t)(BAUD>>8);
	 UBRR0L = (uint8_t)(BAUD);
	 /* set frame format: i wil set it now as 8 bits, no parity 1 stop bit, asynchron*/
	 UCSR0C = (0<<UMSEL0)|(0<<UPM0)|(0<<USBS0)|(3<<UCSZ0);
	 /*set double speed*/
	 UCSR0A =(1<<U2X0);
	 /*enable receiver and transmitter; enable RX and Data register empty interrupt*/
	 UCSR0B = (1<<TXEN0)|(1<<RXEN0)|(1<<RXCIE0);
	 SREG=intstate1; /*restore global interrupt flag*/
	 return 1;
 }
  
/*
 *this function initializes the CAN register for AT90CAN128 and enable the CAN-controller 
 * the function has not input variable
 * the return value is an integer:
 * 1  -> the CAN initializierung is successfully
 * -1 -> the CAN initializierung is unsuccessfully
 */
 int8_t CAN_Init(int32_t Baudrate)
 {
	 uint8_t intstate2 = SREG;/*save global interrupt flag*/
	 cli();/*disable interrupt*/
	 CANGCON |= (1<<SWRES);                       
	 CANGCON &= ~(1<<SWRES); 
	 /*enable general interrupt interrupt*/
	 CANGIE= (1<<ENIT) | (1<<ENBOFF) |(1<<ENRX) |(1<<ENTX) |(1<<ENERR)|(1<<ENBX)|(1<<ENERG)|(0<<ENOVRT); 
	 /* enable Interrupt of MOBs register*/
	 CANIE2 = 0xFF;  
	 CANIE1 = 0x7F;  	
	
	 for (uint8_t mob = 0; mob < 15; mob++)       
	 {	
		 CANPAGE  = (mob << MOBNB0); /* clear all mailbox*/
		 CANSTMOB = 0x00;  /*clear  MOB status register*/
		 CANCDMOB = 0x00;  /*clear MOB control*/
		 CANGSTA = 0x00;   /*clear CAN general status register*/
		 /*clear all identifier tag register*/
		 CANIDT1 = 0X00;	
		 CANIDT2 = 0X00;
		 CANIDT3 = 0X00;
		 CANIDT4 = 0X00;
		 /*clear all identifier mask register*/
		 CANIDM1 = 0X00;	
		 CANIDM2 = 0X00;
		 CANIDM3 = 0X00;
		 CANIDM4 = 0X00;
		 /*clear all CAN message register*/
		 for (unsigned char j=0; j<8; j++) 
		 {	
			 CANMSG = 0;	        
		 }
	 }
	 /* set CAN baudrate register */
	 if(TWOHUNDERTFIFTY_KPS  == Baudrate)
	 {
		 CANBT1 = (3<<BRP0);                                
		 CANBT2 = (4<<PRS0) | (0<<SJW0);                    
		 CANBT3 = (1<<PHS20) | (1<<PHS10);
		 
		 CANGCON |= (1<<ENASTB); /* set ENA/STB enable mode  */ 
		 CANCDMOB |= (0<<CONMOB0)|(0<<CONMOB1); /*enable reception mode here so that information for CAN come automatically*/  
		 SREG=intstate2; /*restore global interrupt flag*/
		 return 1;
	 }

	 if(ONEHUNDERTTWENTYFIVE_KPS == Baudrate)
	 { 
	 
		 CANBT1 = (1<<BRP0);                                
		 CANBT2 = (1<<PRS0) | (0<<SJW0);                    
		 CANBT3 = (2<<PHS20) | (13<<PHS10); 
		 
		 CANGCON |= (1<<ENASTB); /* set ENA/STB enable mode  */ 
		 CANCDMOB |= (0<<CONMOB0)|(0<<CONMOB1); /*enable reception mode here so that information for CAN come automatically*/  
		 SREG=intstate2; /*restore global interrupt flag*/
		 return 1;
	 } 
	 else 	
	 {
		 return -1;
	 }
  
	
 }

 /*
 *this function deletes some CAN register for AT90CAN128 
 * the  fubnstion has no input and output parameters
 */
 void Init(void)
 {	
	 for (uint8_t mob = 0; mob < 15; mob++)         
	 {	
		 CANPAGE  = (mob << MOBNB0); /* clear all  mailbox*/
		 CANSTMOB = 0x00;  /*clear  MOB status register*/
		 CANCDMOB = 0x00;  /*clear MOB control register*/
		 CANGSTA = 0x00;   /*clear CAN general status register*/
		 /*clear all identifier tag register*/
		 CANIDT1 = 0X00;	
		 CANIDT2 = 0X00;
		 CANIDT3 = 0X00;
		 CANIDT4 = 0X00;
		 /*clear all identifier mask register*/
		 CANIDM1 = 0X00;	
		 CANIDM2 = 0X00;
		 CANIDM3 = 0X00;
		 CANIDM4 = 0X00;
		 for (unsigned char j = 0; j < 8; j++) 
		 {	
			 CANMSG = 0; /*clear all CAN message register*/
		 }
	 }
 }
 
  /*
 *this function deletes some variable
 *the  function has no input and output variable
  */
 void Reset_Parameter_CANSend_Message(void)
 {        /* all parameter initialisieren */
	 memset(decrypt_uartString,0,sizeof(decrypt_uartString));
	 memset(tmp_str,0,sizeof(tmp_str));
	 memset(ptr_uartStruct, 0, sizeof(struct uartStruct));
 
 }
 
  /*
 *this function deletes some variable
 *the  fubnstion has no input and output parameters
  */
 void Reset_Parameter_CANReceive(void)
 {
	 /* all parameter initialisieren */
	 memset(ptr_canStruct, 0, sizeof(struct canStruct));
 }
 
  /*
 *this function initializes the 8 bit counter
 * the function has not input parameter
  */
 int8_t Timer0_Init(void)
 {       
	 uint8_t intstate3 = SREG; /* save global interrupt flag */
	 cli(); /* disable interrupt */
	 TCCR0A = (1<<CS02)|(0<<CS01)|(1<<CS00);  /* set Prescaler 1024 */
	 TIMSK0 |= (1<<TOIE0); /* Timer0 Overflow enable */
	 SREG=intstate3; /* restore global interrupt flag */
	 return 1;
 }
 
   /*
 *this function initializes the 8 bit counter with output compareA
 * the function has not input parameter
 * the output parameter is an integer
 * 1 -> the timer initializierung for timer 0 was successfully
   */
 int8_t Timer0A_Init(void)
 {
	 uint8_t intstate4 = SREG; /* save global interrupt flag */
	 cli(); /* disable interrupt */
	 TCCR0A = (1<<FOC0A)|(1<<CS02)| (0<<CS01)|(1<<CS00);  /* set Prescaler 1024 */
	 OCR0A = 255; 
	 TCNT0 = 20;
	 TIMSK0 |= (1<<OCIE0A); /* enable compare output Interrupt */
	 SREG=intstate4; /*restore global interrupt flag */
	 return 1;
 }

 /*
 *this function dismantled the string to receive various parameters
 the function has a string via serial interface as input parameter and returns no parameter 
 */
 void Decrypt_Uart_String(char decrypt_uartString[MAX_LENGTH_PARAMETER])
 {
	 uint8_t  ptr_decrypt_uartString;
	 uint8_t  length_decryptString;
	 length_decryptString = strlen(decrypt_uartString);
	 if(length_decryptString > MAX_LENGTH_COMMAND)
	 {
		 uart_errorCode = Communication_Error(S_ERROR6,S_ERROR6_NO, "erra");
		
	 }
	 else{
		 const char *delims= " "; 
		 char *test = NULL;			
		 char *result = NULL; /* pointer initialisieren */
		 result = strtok_r(decrypt_uartString,delims,&test);  /*search space in string */	 
		 ptr_decrypt_uartString = 0; /* pointer of setParameter*/

		 while( result != NULL ) 
		 {
			 strcpy(setParameter[ptr_decrypt_uartString],result);
			 result = strtok_r( NULL, delims,&test );
			 ptr_decrypt_uartString++;
			
		 }

		 /* call the function for the conversion Frameformet in Canformat*/
		 Convert_UartFormat_to_CanFormat(setParameter);
		 
	 }
 }
 
 
 /*
 *this function assigns the various parameters of the string to the structure and reviewed the range of individual  parameters.
 the  function has a two dimensional array from string via serial interface as input and returns no parameter
 */
 

 void  Convert_UartFormat_to_CanFormat(char string[MAX_PARAMETER][MAX_LENGTH_PARAMETER])
 {  
 	
	 /* convert char in Hexadezimal*/
 	 
	 char* ptr_setParameter[12];
	 ptr_uartStruct->Name = setParameter[0];
	 ptr_uartStruct->Uart_Message_ID = (uint32_t) strtoul(setParameter[1], &ptr_setParameter[1],16);
	 ptr_uartStruct->Uart_Mask       = (uint32_t) strtoul(setParameter[2], &ptr_setParameter[2],16);
	 ptr_uartStruct->Uart_Rtr        = (uint8_t)  strtoul(setParameter[3], &ptr_setParameter[3],16);
	 ptr_uartStruct->Uart_Length     = (uint8_t)  strtoul(setParameter[4], &ptr_setParameter[4],16); 
	 ptr_uartStruct->Uart_Data[0]    = (uint16_t) strtoul(setParameter[5], &ptr_setParameter[5],16);
	 ptr_uartStruct->Uart_Data[1]    = (uint16_t) strtoul(setParameter[6], &ptr_setParameter[6],16);
	 ptr_uartStruct->Uart_Data[2]    = (uint16_t) strtoul(setParameter[7], &ptr_setParameter[7],16);
	 ptr_uartStruct->Uart_Data[3]    = (uint16_t) strtoul(setParameter[8], &ptr_setParameter[8],16);
	 ptr_uartStruct->Uart_Data[4]    = (uint16_t) strtoul(setParameter[9], &ptr_setParameter[9],16);
	 ptr_uartStruct->Uart_Data[5]    = (uint16_t) strtoul(setParameter[10],&ptr_setParameter[10],16);
	 ptr_uartStruct->Uart_Data[6]    = (uint16_t) strtoul(setParameter[11],&ptr_setParameter[11],16);
	 ptr_uartStruct->Uart_Data[7]    = (uint16_t) strtoul(setParameter[12],&ptr_setParameter[12],16);
 	  
	 /*check range of parameter*/
	 if(                  ((strcmp (ptr_uartStruct->Name, "SEND")== 0)||
				       (strcmp (ptr_uartStruct->Name, "SUBS")== 0)||
				       (strcmp (ptr_uartStruct->Name, "USUB")== 0)||
				       (strcmp (ptr_uartStruct->Name, "STAT")== 0)||
				       (strcmp (ptr_uartStruct->Name, "WRRG")== 0)||
				       (strcmp (ptr_uartStruct->Name, "RERG")== 0)||
				       (strcmp (ptr_uartStruct->Name, "RADC")== 0)||
				       (strcmp (ptr_uartStruct->Name, "TEMP"))== 0) &&      
				       (((0x7FFFFFF)>= (ptr_uartStruct->Uart_Message_ID))  && (( 0 ==(*ptr_setParameter[0]))   || (0x20==(*ptr_setParameter[1])  )))&&
				       (((0x7FFFFFF)>=(ptr_uartStruct->Uart_Mask))         && (( 0 ==(*ptr_setParameter[1]))   || (0x20==(*ptr_setParameter[2])  )))&&
				       (((1)>=(ptr_uartStruct->Uart_Rtr))                  && (( 0 ==(*ptr_setParameter[2]))   || (0x20==(*ptr_setParameter[3])  )))&&
				       (((8)>= (ptr_uartStruct->Uart_Length))              && (( 0 ==(*ptr_setParameter[3]))   || (0x20==(*ptr_setParameter[4])  )))&&
				       (((0XFF)>=(ptr_uartStruct->Uart_Data[0]))           && (( 0 ==(*ptr_setParameter[4]))   || (0x20==(*ptr_setParameter[5])  )))&&
				       (((0XFF)>=(ptr_uartStruct->Uart_Data[1]))           && (( 0 ==(*ptr_setParameter[5]))   || (0x20==(*ptr_setParameter[6])  )))&&
				       (((0XFF)>=(ptr_uartStruct->Uart_Data[2]))           && (( 0 == (*ptr_setParameter[6]))  || (0x20==(*ptr_setParameter[7])  )))&&
				       (((0XFF)>=(ptr_uartStruct->Uart_Data[3]))           && (( 0 ==(*ptr_setParameter[7]))   || (0x20==(*ptr_setParameter[8])  )))&&
				       (((0XFF)>=(ptr_uartStruct->Uart_Data[4]))           && (( 0 ==(*ptr_setParameter[8]))   || (0x20==(*ptr_setParameter[9])  )))&&
				       (((0XFF)>=((ptr_uartStruct->Uart_Data[5]))          && (( 0 == (*ptr_setParameter[9]))  || (0x20==(*ptr_setParameter[10])  )))&&
				       (((0XFF)>=(ptr_uartStruct->Uart_Data[6]))           && (( 0 == (*ptr_setParameter[10])) || (0x20==(*ptr_setParameter[11]) )))&&
				       (((0XFF)>=(ptr_uartStruct->Uart_Data[7]))           && (( 0 == (*ptr_setParameter[11])) || (0x20==(*ptr_setParameter[12]) ))))
	   
	   )
	 {
		 
		
		 Choose_Function(ptr_uartStruct);
		 /*clear the variable setParameter*/	
		 for(uint8_t count_setParameter = 0; count_setParameter < 12 ;count_setParameter++)
		 {
			 setParameter[count_setParameter][0] = 0;
		 }
	 }
 
	 else{
 		
		 if(  (strcmp (ptr_uartStruct->Name, "SEND")!= 0) &&  
				     (strcmp (ptr_uartStruct->Name, "SUBS")!= 0) && 
				     (strcmp (ptr_uartStruct->Name, "USUB")!= 0) &&
				     (strcmp (ptr_uartStruct->Name, "STAT")!= 0) &&
				     (strcmp (ptr_uartStruct->Name, "REIO")!= 0) &&
				     (strcmp (ptr_uartStruct->Name, "RERG")!= 0) &&
				     (strcmp (ptr_uartStruct->Name, "RADC")!= 0) &&
				     (strcmp (ptr_uartStruct->Name, "TEMP")!= 0))
		 {
			 uart_errorCode = Communication_Error(S_ERROR0,S_ERROR0_NO, "erra");
			 setParameter[0][0] = '\0';
		 }

		 if((0x7FFFFFF) < ptr_uartStruct->Uart_Message_ID) 
		 {
			 uart_errorCode = Communication_Error(S_ERROR1,S_ERROR1_NO, "erra");
			 setParameter[1][0] = '\0';
			 *ptr_setParameter[1] = 0;
		 }					 
		 if(0 != (*ptr_setParameter[1]) ) 
		 {
			 uart_errorCode = Communication_Error(S_ERROR10,S_ERROR10_NO, "erra");
			 *ptr_setParameter[1] = 0;
		 }
		 
		 if((0x7FFFFFF) < ptr_uartStruct->Uart_Mask) 
		 {
			 uart_errorCode = Communication_Error(S_ERROR2,S_ERROR2_NO, "erra");
			 setParameter[2][0] = '\0';
			 *ptr_setParameter[2] = 0;
		 }					 
		 if(0 != (*ptr_setParameter[2]) ) 
		 {
			 uart_errorCode = Communication_Error(S_ERROR20,S_ERROR20_NO, "erra");
			 *ptr_setParameter[2] = 0;
		 }
 		 
		 if((1) < ptr_uartStruct->Uart_Rtr) 
		 {
			 uart_errorCode = Communication_Error(S_ERROR3,S_ERROR3_NO, "erra");
			 setParameter[3][0] = '\0';
			 *ptr_setParameter[3] = 0;
		 }					 
		 if(0 != (*ptr_setParameter[3]) ) 
		 {
			 uart_errorCode = Communication_Error(S_ERROR30,S_ERROR30_NO ,"erra");
			 *ptr_setParameter[3] = 0;
		 }
		 
		 
		 if((8) < ptr_uartStruct->Uart_Length) 
		 {
			 uart_errorCode = Communication_Error(S_ERROR4, S_ERROR4_N0,"erra");
			 setParameter[4][0] = '\0';
			 *ptr_setParameter[4] = 0;
		 }					 
		 if(0 != (*ptr_setParameter[4]) ) 
		
		 {
			 uart_errorCode = Communication_Error(S_ERROR40 ,S_ERROR40_NO, "erra");
			 memset(&(ptr_setParameter[4][0]),'\0',MAX_LENGTH_PARAMETER);
			 *ptr_setParameter[4] = 0;
		 }

		 for (int i = 0; i<7; i++)
		 {
			 if((0XFF) < ptr_uartStruct->Uart_Data[i]) 
			 {
				
				 uart_errorCode = Communication_Error(serial_error[i+5],serial_error_number[i+5], "erra");
				 ptr_uartStruct->Uart_Data[i] = 0;	
				 setParameter[i+5][0] = '\0';
				 *ptr_setParameter[i+5] = 0;			
			 }		 		 
		 }
		 
		 for (int i=5; i<13; i++)
		 {
			 if(0 != (*ptr_setParameter[i]) ) 
			 {
				 uart_errorCode = Communication_Error(serial_error[i+13],serial_error_number[i+13], "erra");
				 *ptr_setParameter[i] = 0;
			 }	
			 		 
		 }
 			
	 }
	
  
 }
 
 
 
 /*
 *this function calls another function in relation to the first parameter of the string
 the function has a pointer of the serial structur as input and returns no parameter
 */
 void Choose_Function(struct uartStruct *ptr_uartStruct)
 {



	 if ((strcmp (ptr_uartStruct->Name, "SEND") == 0) && (ptr_uartStruct->Uart_Rtr == 0))
	 
		 Send_Message(ptr_uartStruct); /* call function with name  Send_Message */
	 else if ((strcmp (ptr_uartStruct->Name, "SEND")== 0)&& (ptr_uartStruct->Uart_Rtr == 1))
		
		 Receive_Message(ptr_uartStruct); /* call function with name Receive_message */

	 else if ((strcmp (ptr_uartStruct->Name, "SUBS") == 0))
	 {
		 Subscribe_Message(ptr_uartStruct); /* call function with name Subscribe_Message */
	 }
	
	 else if ((strcmp (ptr_uartStruct->Name, "USUB") == 0))
	 {
		 Unsubscribe_Message(ptr_uartStruct); /* call function with name Unsubscribe_Message */
	 }
	 else if ((strcmp (ptr_uartStruct->Name, "STAT") == 0))
	 { 
		
	
		 readStatus(ptr_uartStruct); /* call function with name readTempSensor */
		
	 }
	
	 else if ((strcmp (ptr_uartStruct->Name, "TEMP") == 0))
	 { 		
		
		 readTempSensor(ptr_uartStruct); /* call function with name readTempSensor */
		
	 }
	 else if ((strcmp (ptr_uartStruct->Name, "WRRG") == 0))
	 { 	
		 writeRegister(ptr_uartStruct); /* call function with name writeRegister  */
		
	 }
	 else if ((strcmp (ptr_uartStruct->Name, "RERG") == 0))
	 { 		
		 readRegister(ptr_uartStruct); /* call function with name  readRegister */
		
	 }
	 else if ((strcmp (ptr_uartStruct->Name, "RADC") == 0))
	 { 		
	
		 read_ADC(ptr_uartStruct); /* call function with name  readRegister */
		
	 }
	 else 
	 {
		 uart_errorCode = Communication_Error(S_ERROR0,S_ERROR0_NO, "erra");
	 }
		 
 }

/*
 * this function will read certain messages on the bus
 the function has a pointer of the serial structur as input and returns no parameter
*/
 void Subscribe_Message(struct uartStruct *ptr_uartStruct)
 { 
	 int8_t findMob;
	 uint8_t equality = 1;
	 for(uint8_t count_subscribe = 2; count_subscribe <= 14; count_subscribe++)
	 {
		 if((ptr_uartStruct->Uart_Message_ID) == (subscribe_ID[count_subscribe]) && (ptr_uartStruct->Uart_Mask)== (subscribe_mask[count_subscribe]) )
		 {
			 equality = 0;
			 mailbox_errorCode = Communication_Error(M_ERROR2,M_ERROR2_NO,"errm");
			
		 }
			 
	 }
	 if (1 == equality)
	 {
	 
		 subscribe_ID[ptr_subscribe]=(ptr_uartStruct->Uart_Message_ID);
		 subscribe_mask[ptr_subscribe]=(ptr_uartStruct->Uart_Mask);
		 ptr_subscribe++;
		 findMob=Get_FreeMob();
 
		 if(14 < ptr_subscribe )
		 {
			 ptr_subscribe = 2;
		 }
		 
		 if( (-1) == findMob) 	
		 {
			 mailbox_errorCode = Communication_Error(M_ERROR0,M_ERROR0_NO, "errm");
		 }
		 else{
		
			 CANPAGE = ( findMob<<MOBNB0);
			 CANSTMOB = 0x00; /* cancel pending operation */
			 CANCDMOB = 0x00;		
			 CANHPMOB = 0x00; /* enable direct mob indexing */
 
 
			 if( ((MAX_ELF_BIT) >=  (ptr_uartStruct->Uart_Message_ID)) ||((MAX_ELF_BIT) >= (ptr_uartStruct->Uart_Mask)) )
			 {
				 /*set identifier to send*/
				 CANIDT2 = ((ptr_uartStruct->Uart_Message_ID)&0x7)<<5;           
				 CANIDT1 = (ptr_uartStruct->Uart_Message_ID)>>3;   
				 /* enable comparison*/
				 CANIDM4 =(1<<RTRMSK);
		 
				 /* set identifier Mask*/
				 CANIDM2 = ((ptr_uartStruct->Uart_Mask)&0x7)<<5;         
				 CANIDM1 = (ptr_uartStruct->Uart_Mask)>>3;  
		

			 }  
 	
			 else{
				 /*set identifier to send  */ 
				 CANIDT4 = ((ptr_uartStruct->Uart_Message_ID)&0x7)<<3; 
				 CANIDT3 = (ptr_uartStruct->Uart_Message_ID)>>5; 
				 CANIDT2 = (ptr_uartStruct->Uart_Message_ID)>>5;        
				 CANIDT1 = (ptr_uartStruct->Uart_Message_ID)>>3;   
				 /*set identifier mask  */ 
				 CANIDM4 = ((ptr_uartStruct->Uart_Mask)&0x7)<<3; 
				 CANIDM3 = (ptr_uartStruct->Uart_Mask)>>5; 
				 CANIDM2 = (ptr_uartStruct->Uart_Mask)>>5;        
				 CANIDM1 = (ptr_uartStruct->Uart_Mask)>>3;    
 
			 }  	
			 CANCDMOB = (1<<CONMOB1); /* enable reception mode */  
			 unsigned mask = 1 << findMob;
			 CANIE2 |=  mask;
			 CANIE1 |= (mask>>8); 
		 }
	 }
 }
  
 
  /*
 * this function does not read certain messages on the bus
 the function has a pointer of the serial structur as input and returns no parameter
  */
 void Unsubscribe_Message(struct uartStruct *ptr_uartStruct)
 { 
 
	 uint8_t inequality = 1;
	 for(uint8_t count_mob = 2; count_mob <= 14; count_mob++) 
	 {
		
		 CANPAGE = (count_mob<<MOBNB0);
		 
		 if ( ((CANIDT2>>5) |(CANIDT1<<3))==((ptr_uartStruct->Uart_Message_ID)) && ((((CANIDM2>>5) |(CANIDM1<<3)) == (ptr_uartStruct->Uart_Mask))))			 
		 {
			 CANSTMOB = 0x00; /* cancel pending operation */
			 CANCDMOB = 0x00; /* very important,that disable MOB*/	
			 CANHPMOB = 0x00;
			 CANIDM1=0X00;	
			 CANIDM2=0X00;
			 CANIDT1=0X00;	
			 CANIDT2=0X00;
			 unsigned mask=1<<count_mob;
			 CANIE2 &= ~mask;
			 CANIE1 &= ~(mask>>8);
			 inequality = 0;
			 for(int count_subscribe = 2; count_subscribe <= 14; count_subscribe++) 
			 {
				 if (((ptr_uartStruct->Uart_Message_ID) == ( subscribe_ID[count_subscribe])) && (((ptr_uartStruct->Uart_Mask) == ( subscribe_mask[count_subscribe]) ) ))
				 {
					 subscribe_ID[count_subscribe]= 0;
					 subscribe_mask[count_subscribe]= 0;
				 }
				 			 			 
			 }	 
			
		 }
		
	 }
	 if ( 1 == inequality)
	 {
		 Communication_Error(M_ERROR1,M_ERROR1_NO,"errm");
	 }
 } 
 
/*
 * this function runs a command and expects no data
 the function has a pointer of the serial structur as input and returns no parameter
*/

 void Send_Message( struct uartStruct *ptr_uartStruct )
 {

	 CANPAGE = (1<<MOBNB0);/*set channel number  */
	 CANIDT4 = (ptr_uartStruct->Uart_Rtr<<RTRTAG); /* enable remote transmition request */
	 CANCDMOB = (ptr_uartStruct->Uart_Length<<DLC0); /* set lenght of data*/
	

	 if( (MAX_ELF_BIT) >= (ptr_uartStruct->Uart_Message_ID))
	 {
		 CANCDMOB |= (0<<IDE); /* enable CAN standard 11 bit */
		 /*set Identifier to send */
		 CANIDT2 = ((ptr_uartStruct->Uart_Message_ID)&0x7)<<5;                
		 CANIDT1 = (ptr_uartStruct->Uart_Message_ID)>>3;                      
	 }
	 else
	 {
		 CANCDMOB |= (1<<IDE); /* enable CAN standard 29 bit */
		 /*set Identifier to send  */ 
		 CANIDT2 = ((ptr_uartStruct->Uart_Message_ID)&0x7)<<3; 
		 CANIDT2 = (ptr_uartStruct->Uart_Message_ID)>>5; 
		 CANIDT2 = (ptr_uartStruct->Uart_Message_ID)>>5;       
		 CANIDT1 = (ptr_uartStruct->Uart_Message_ID)>>5; 
	 }                
	 
	 /*put data in mailbox*/
	 for (uint8_t count_data =0; count_data<8; count_data++)
	 {							
		 CANMSG = ptr_uartStruct->Uart_Data[count_data];

	 }
	 CANSTMOB=0x00;
	 CANCDMOB |= (1<<CONMOB0);        /*enable transmition mode*/
	 /*call the function verify that the sending of data is complete*/
	 Wait_for_Can_Send_Message_Finished();
 }
 
 /*
 * this function runs a command and expects data
 the function has a pointer of the serial structur as input and returns no parameter
 */
 void Receive_Message(struct uartStruct *ptr_uartStruct)
 { 
	
	 CANPAGE = (0<<MOBNB0);/*set channel number */	 
	 CANSTMOB = 0x00;    	/* cancel pending operation */
	 CANCDMOB = 0x00;	
	 CANIDT4 = (ptr_uartStruct->Uart_Rtr<<RTRTAG); /* enable remote transmition request */
	 CANCDMOB = (ptr_uartStruct->Uart_Length<<DLC0); /* set lenght of data*/
	
	 if(( (MAX_ELF_BIT ) >= (ptr_uartStruct->Uart_Message_ID)) ||( (MAX_ELF_BIT) >= (ptr_uartStruct->Uart_Mask)))
	 {
		 CANCDMOB |= (0<<IDE); /* enable CAN standard 11 bit */
		 /*set Identifier to send */
		 CANIDT2 = ((ptr_uartStruct->Uart_Message_ID)&0x7)<<5;                
		 CANIDT1 = (ptr_uartStruct->Uart_Message_ID)>>3;  
		 /*set Mask to receive only Message */
		 CANIDM1 = 0xFF;
		 CANIDM2 = 0xFF;
		 CANIDM3 = 0xFF;
		 CANIDM4 = 0xFF;
		
	 }
	 
	 else{
		 CANCDMOB |= (1<<IDE); /* enable CAN standard 29 bit */
		 /*set Identifier to send  */ 
		 CANIDT2 = ((ptr_uartStruct->Uart_Message_ID)&0x7)<<3; 
		 CANIDT2 = (ptr_uartStruct->Uart_Message_ID)>>5; 
		 CANIDT2 = (ptr_uartStruct->Uart_Message_ID)>>5;       
		 CANIDT1 = (ptr_uartStruct->Uart_Message_ID)>>5; 

	 }
   
	 /* put data in mailbox*/
	 for (uint8_t count_data=0; count_data<8; count_data++)
	 {							
		 CANMSG = ptr_uartStruct->Uart_Data[count_data];
	 }
	 CANSTMOB = 0;
	
	 CANCDMOB |= (1<<CONMOB0); /*enable transmition mode*/
	
	 /*call the function verify that the sending of data is complete*/
	 Wait_for_Can_Receive_message_Finished();
 
 }
 

 
/*
 *this function wait until the command is sent
 the function has no input and output parameter 
*/
 void Wait_for_Can_Send_Message_Finished(void)
 {
	 double can_timeout1 = TIMEOUT_C; /*Timeout for CAN-communication*/
	 while (!( CANSTMOB & (1<<TXOK)) && (--can_timeout1 >0)) 
	 {
		 /* do nothing  */
	 }
	 if(  1 >= can_timeout1  )
	 {

		 can_errorCode = Communication_Error(C_ERROR9,C_ERROR9_NO, "errc");
		 can_timeout1  = TIMEOUT_C;
	 }
	 
	 CANSTMOB &= ~(1<<TXOK); /* reset transmition flag */	
	 CANCDMOB &= ~(1<<CONMOB0);  /* disable transmition mode */
	 snprintf(tmp_str,BUFFER_SIZE-1,"RECV %s",READY);
	 UART0_Send_Message_String(tmp_str);
	 /* all parameter initialisieren */
	 Reset_Parameter_CANSend_Message();
	 
 }
 
 /*
 *this function wait until the command in case the request is sent
 the function has no input and output parameter 
 */

 void Wait_for_Can_Receive_message_Finished(void)
 {   
	
	 double can_timeout2 = TIMEOUT_C; /*Timeout for CAN-communication*/
	 while (!( CANSTMOB & (1<<RXOK)) && (--can_timeout2>0)) 
	 {
		
		 /* do nothing  */
		  
	 }
	 if(( 1 >= can_timeout2 ) && (CANSTMOB & (1<<TXOK)))
	 {
		 can_errorCode = Communication_Error(C_ERROR9,C_ERROR9_NO, "errc");
		 can_timeout2 = TIMEOUT_C;
	 }
     
	 else
	 {
		 CANSTMOB &= ~(1<<TXOK);  /* reset transmition flag */	
		 CANCDMOB &= ~(1<<CONMOB0); /* disable transmition mode */
	 }
	
	 /* all parameter initialisieren */
	 Reset_Parameter_CANSend_Message();
	
 }

 /*
 *This function grabs the CAN data received in a string
 the function has a poimter of the CAN structur as input and returns no parameter
 */
 
 void Convert_pack_canFrame_to_UartFormat(struct canStruct *ptr_canStruct)
 {
	 uint16_t pack_canFrame[11];/* variable for storage received a complete CAN-Frame via CAN-Bus */ 
	 uint8_t ptr_pack_canFrame = 3; /*pointer to receive CAN-data */
	 char temp_canString[MAX_LENGTH_CAN_DATA];/*tmpory variable for storage received CAN data*/
	 pack_canFrame[0] = ptr_canStruct->Can_Mob;
	 pack_canFrame[1] = ptr_canStruct->Can_Message_ID;
	 pack_canFrame[2] = ptr_canStruct->Can_Length;
	 for ( uint8_t count_can_data = 0; count_can_data < 8; count_can_data++)
	 {               
		 pack_canFrame[ptr_pack_canFrame] = ptr_canStruct->Can_Data[count_can_data];
		 ptr_pack_canFrame++;
	 }
	
	 /* grabs the received data in a string */
	 for( uint8_t i = 0; i < 11; i++ ) 
	 {
		 snprintf(tmp_str,BUFFER_SIZE-1,"%x",pack_canFrame[i]);
		 strncat( temp_canString,tmp_str,BUFFER_SIZE-1);  /* the next word attach the rear */
		 strncat( temp_canString, " ",BUFFER_SIZE-1 ); /* insert space*/ 
	 }
	 
	 strncat(canString,"RECV",BUFFER_SIZE-1); 
	 strncat(canString, " ",BUFFER_SIZE-1 ); 
	 strncat(canString, temp_canString,BUFFER_SIZE-1 );
	 snprintf(tmp_str,BUFFER_SIZE-1,"%s",canString);
	 UART0_Send_Message_String(tmp_str);
	 Put_in_RingBuffer (canString);
	 memset(ptr_canStruct, 0, sizeof(struct canStruct)); /*clear Can-structure*/ 
	 memset(canString,0,sizeof(canString)); /*clear the variable CanString */ 
	 memset(temp_canString,0,sizeof(temp_canString)); /*clear the variable temp_canString*/ 
	 /*all parameter initialisieren*/
	 Reset_Parameter_CANReceive();
	 
 }
 /*the function puts the received CAN data in  the ringbuffer
 * the fuction has as input the CAN data in string format 
 * and an integer as output:
 * 0-> ok the buffer is not full
 * -1 -> the buffer is full  
 */
 int8_t Put_in_RingBuffer (char PutData[MAX_LENGTH_CAN_DATA])
 {
	                                             
	 if( MAX_INPUT <= ptr_buffer_in )
	 {;
	 ptr_buffer_in = 0;
	 snprintf(tmp_str,BUFFER_SIZE-1,"ERROR:%s",G_ERROR2);
	 UART0_Send_Message_String(tmp_str);
	 return -1;	 
	 }
	 else
	 {
		 strncpy(ring_buffer[ptr_buffer_in],canString,sizeof(ring_buffer[ptr_buffer_in])); 
		 ptr_buffer_in ++;
// 		 snprintf(tmp_str,BUFFER_SIZE-1,"put:%s",ring_buffer[ptr_buffer_in]);
// 		 UART0_Send_Message_String(tmp_str);
		 Get_out_RingBuffer();
		 return 0;
	 }
	
 }
 /*
 * this function gets saved CAN data in ringbuffer
 * the function as no input value 
 * and returns an integer value as output:
 * 0 -> ok the buffer is not full
 * -1 -> the buffer is not full
 */
 
 int8_t Get_out_RingBuffer(void)
 
 {
	 char get_data[MAX_LENGTH_CAN_DATA];
	
	
	 if( MAX_INPUT <= ptr_buffer_out )
	 {
		 ptr_buffer_out = 0;
		 snprintf(tmp_str,BUFFER_SIZE-1,"Error:%s",G_ERROR4);
		 UART0_Send_Message_String(tmp_str);
		 return -1;
	 }
	 else
	 {
		 strncpy(get_data, ring_buffer[ptr_buffer_out],sizeof(get_data));  
// 		 snprintf(tmp_str,BUFFER_SIZE-1,"get:%s",get_data);
// 		 UART0_Send_Message_String(tmp_str);
		 ptr_buffer_out ++;	
		 return 0;
	 }
			 
 }
 	
 /* interrupt for overflow with defined interrupt vector TIMER0_OVF_vect */

 SIGNAL(TIMER0_OVF_vect)
 {

	 static  uint8_t  counter0 = 0;  /* counter for timer0 */
	 counter0++;
	 if(counter0 == 10)  /* 10 overflow for timer0=0.5s */
	 {
		 timer0Ready = 1; /* mark, that we got an CAN_interrupt, to be handled by main */
		 counter0 = 0;	
	 }
 }
 
 /* interrupt for output compare with defined interrupt vector SIG_OUTPUT_COMPARE0 */   
 SIGNAL(SIG_OUTPUT_COMPARE0)
 {
	 static  uint8_t  counter0A = 0; /* counter for timer0 with output compareA */
	 counter0A++;
	 if(counter0A == 9) /*9 overflow for timer0A=0.6s*/
	 {
		 timer0AReady = 1;  /* mark, that we got an CAN_interrupt, to be handled by main */
		 counter0A = 0;	
	 }
 }

 /* interrupt for receive  data via serial communication with defined interrupt vector SIG_UART0_RECV */  
 ISR (SIG_UART0_RECV)
 {  
	 unsigned char c = UDR0;
	 
	 if( c == '\n' ) /* the string is complete? */
	 {   
		
		 uartString[nextCharPos] = '\0';    
		 uartReady = 1;  /* mark, that we got an CAN_interrupt, to be handled by main */
		 nextCharPos = 0;	
	 }

	 else 
	 {
		 uartString[nextCharPos] = c;
		 nextCharPos++;
		 uartString[nextCharPos] = '\0';
	 }
 }
 

 /* interrupt for receive data via CAN communication with defined interrupt vector CANIT_vect */ 

 ISR(CANIT_vect)
 {
	 uint8_t save_canpage = CANPAGE;   
	 mob = Get_Interrupt_of_Mob(CANSIT2 | (CANSIT1 << 8)); /*get the index of the MOb for which the interrupt occured*/
	 CANPAGE = mob<<4; /* select concerned MOb page */
	 if (CANSTMOB & (1<<TXOK))
	 {       CANSTMOB &= ~(1<<TXOK); /* disable transmition mode*/	  
	 CANCDMOB = (1<<CONMOB0); /* reset receive mode*/     
      
	 }

	 else if (CANSTMOB & (1<<RXOK))
	 {
		 ptr_canStruct->Can_Length = (CANCDMOB & (0xF<<DLC0));
		 for ( uint8_t i=0; i< ptr_canStruct->Can_Length; i++)
		 {	 /* get data of selected MOb */
			 ptr_canStruct->Can_Data[i]= CANMSG ;                
		 }
		 /*get identifier */
		 ptr_canStruct->Can_Message_ID = 0;
		 ptr_canStruct->Can_Message_ID = CANIDT2>>5;
		 ptr_canStruct->Can_Message_ID |= CANIDT1<<3; 
		 ptr_canStruct->Can_Mob = mob; /*get mailbox */
		 CANSTMOB &= ~(1<<RXOK); /* clear interrupt */	
		 CANCDMOB = (1<<CONMOB1); /* reset receive mode*/
		 canReady = 1; /* mark, that we got an CAN_interrupt, to be handled by main */
	 }
	 CANPAGE =  save_canpage;   /* restore CANPAGE*/
 }
 
 /* this function gets index of MOb for which an interrupt was detected
 * the function has an uint32_t CANSIT register as input parameter
 * the function has unsigned integer as input:
 * mob -> valid message object block
 * NOMOB -> no valid  message object block
 */
 	
 uint8_t Get_Interrupt_of_Mob( uint32_t bitmask) 
 {
	
	 if( bitmask == 0) return NOMOB;

 /*the bitmask is shifted to the rigth until the LSB is one
	 the number of such shift operations is the index of the MOb*/
	 for( mob = 0; (bitmask & 0x01) == 0; bitmask >>= 1, mob++);

	 /*security check*/
	 if ( mob > 14 ) return NOMOB;
	 else return mob;
 }

 
  /*
 *this function gives the various CAN error
 the function has no input and output parameters 
  */
 void Get_CanError(void)
 {


	 if (CANSTMOB & (1<<BERR))
	 {
		 CANSTMOB &= ~(1<<BERR);  
		 can_errorCode = Communication_Error(C_ERROR3,C_ERROR3_NO, "errc");
	 }
	 if (CANSTMOB & (1<<SERR))
	 {
		 CANSTMOB &= ~(1<<SERR);
		 can_errorCode = Communication_Error(C_ERROR4,C_ERROR4_NO, "errc");
	 }
	 if (CANSTMOB & (1<<CERR))
	 {
		 CANSTMOB &= ~(1<<CERR);
		 can_errorCode = Communication_Error(C_ERROR5,C_ERROR5_NO, "errc");
	 }
	 if (CANSTMOB & (1<<FERR))
	 {
		 CANSTMOB &= ~(1<<FERR);
		 can_errorCode = Communication_Error(C_ERROR6,C_ERROR6_NO, "errc");
	 }
	 if (CANSTMOB & (1<<AERR))
	 {
		 CANSTMOB &= ~(1<<AERR);
		 can_errorCode = Communication_Error(C_ERROR7,C_ERROR7_NO, "errc");
		
	 }
 }
 
/*	
 * this function gives the various CAN error on the channel
 the function has no input and output parameters 
 */ 
 void Get_BusState(void)
 {
	
	 if (CANGSTA & (1<<BOFF))
	 {
		 can_errorCode = Communication_Error(C_ERROR0,C_ERROR0_NO, "errc");
		 CAN_Init(TWOHUNDERTFIFTY_KPS); /* CAN initialisieren */
	
	 }
	 if (CANGSTA & (1<<ERRP)){
		 can_errorCode = Communication_Error(C_ERROR1,C_ERROR1_NO, "errc"); 
		 CAN_Init(TWOHUNDERTFIFTY_KPS); /* CAN initialisieren */
	
	 }
 }
 
 
 /* this function gets the free communication channel  
 *  the function has no input parameter 
 *  and returns a integer value
 *  freemob -> valid message object block
 *  -1 -> no valid message object block
 */
 
 int8_t Get_FreeMob(void)
 {
	 uint8_t ctrlReg;
	
	 for ( uint8_t freemob = 2; freemob <= 14; freemob++)
	 {
		 CANPAGE = freemob << 4;
		 ctrlReg = (CANCDMOB & ((1<<CONMOB0) |(1<<CONMOB1)));
		 if (ctrlReg == 0)
		 {
			 return freemob;
		 }
	 }
	 return -1;

 }
/*
 *USART0 must be initialized before using this function
 *for use this function, be sure that data is no longer then 8 bit 
 *(single character) this function will send  8 bits from the at90can128 
 */

 void UART0_Transmit(uint8_t data)
 {
	
	 /* wait for empty transmit buffer */
	 while(!(UCSR0A & (1<<UDRE0)))
	 {
		 /* do nothing*/
	 }
	 /* put the data into the buffer */

	 UDR0 = data;	
	 
 }
 /*
 *this function sends a string to serial communication
 * the input parameter is a defined global vaviable tmp_str
 * the output parameter is an integer
 * 0 -> the message is sent
 */
 
 int8_t UART0_Send_Message_String(char *tmp_str){
	 
	 for ( uint16_t j = 0; j < strlen ((char *) tmp_str) && j <  BUFFER_SIZE; j++)
	 {
		 UART0_Transmit (tmp_str[j]);
	 }
	 UART0_Transmit ('\n');
	 memset(tmp_str,0,sizeof(tmp_str)); /*clear tmp_str variable*/
	 return 0;
 }
 
 int8_t UART1_Send_Message_String(char *tmp_str){
	 
	 for ( uint16_t j = 0; j < strlen ((char *) tmp_str) && j <  BUFFER_SIZE; j++)
	 {
		 UART0_Transmit (tmp_str[j]);
	 }
	//UART0_Transmit ('\n');
	 memset(tmp_str,0,sizeof(tmp_str)); /*clear tmp_str variable*/
	 memset(pp,0,sizeof(pp)); /*clear tmp_str variable*/
	 //memset(onetemp,0,sizeof(onetemp)); /*clear tmp_str variable*/
	 return 0;
 }

 /*
 *this function indicates an error for the serial,CAN and Mailbox communication
 * the function has as input the description of error and the error number
 * the output is an unsigned integer
 * 0 -> the error is  signaled
 * 1 -> an error has occured
 */
 
// uint8_t Communication_Error( const char Error[MAX_LENGTH_ERROR],const uint8_t Error_Number, char Error_typ[5])
 uint8_t Communication_Error( char Error[MAX_LENGTH_ERROR], uint8_t Error_Number,char Error_typ[5])
 {

	 if(strlen(Error)> MAX_LENGTH_ERROR)
	 {
		 snprintf(tmp_str,(BUFFER_SIZE-1),"ERRA %d %s",Error_Number, Error);
		 UART0_Send_Message_String(tmp_str);
		 return 0;
                 //return  1; 
	 }
	 
	 if(Error_typ == "erra")
	 {
		 snprintf(tmp_str,(BUFFER_SIZE-1),"ERRA %d %s",Error_Number, Error);
		 UART0_Send_Message_String(tmp_str);
		 return  1;
	 }	
	 if(Error_typ == "errc")
	 {
		 snprintf(tmp_str,(BUFFER_SIZE-1),"ERRC %d %s",Error_Number, Error);
		 UART0_Send_Message_String(tmp_str);
		 return  1;
	 }
	 if(Error_typ == "errm")
	 {
		 snprintf(tmp_str,(BUFFER_SIZE-1),"ERRM %d %s",Error_Number, Error);
		 UART0_Send_Message_String(tmp_str);
		 return  1;
	 }	
 }
	 
 /*
 *this function indicates general error 
 * the function has a error description and error nummer as input parameter, the output parameter is an integer
 * the function has as input the description of error and the error number
 * the output is an unsigned integer
 * 0 -> the error is signaled
 * 1 -> an error has occured
 */
 
 uint8_t General_Communication_Error(char Error[MAX_LENGTH_ERROR],uint8_t Error_Number)
 {
	 if(strlen(Error)> MAX_LENGTH_ERROR)
	 {
		 snprintf(tmp_str,(BUFFER_SIZE-1),"ERRA %d %s",S_ERROR8_NO,S_ERROR8);
		 UART0_Send_Message_String(tmp_str);
		 return 0;
	 }
	 else
	 {
		 snprintf(tmp_str,BUFFER_SIZE-1,"Error %d %s",Error_Number,Error);
		 UART0_Send_Message_String(tmp_str);
		 return 1;
	 } 
 }
 
 
 /* this function writes a value the status in the register with the of his adress 
 * as input parameters the function needs a keyword, an adress and a value
 * the function has no return parameter
 */
 void writeRegister(struct uartStruct *ptr_uartStruct)
 { 
	 
	 if(0XFF < ptr_uartStruct->Uart_Message_ID) 
	 { 
		 uart_errorCode = Communication_Error(S_ERROR9,S_ERROR9_NO, "erra");
	 }
	 else if(0XFF < ptr_uartStruct->Uart_Mask) 
	 {
		 uart_errorCode = Communication_Error(S_ERROR11,S_ERROR11_NO, "erra");
	 }
	 else
	 {
		 StartValue = 1;

		 /* What is that?, Michael */
		 Adr_Reg = ptr_uartStruct->Uart_Message_ID;
		 Val_Reg= ptr_uartStruct->Uart_Mask;
		 Ptr_Reg = Adr_Reg;
		 *Ptr_Reg = Val_Reg;
		 snprintf(tmp_str,BUFFER_SIZE-1,"RECV the value %x has been written in adresse %x", Val_Reg,Adr_Reg);
		 UART0_Send_Message_String(tmp_str);
	 }
	   
 }
 
 /*this function reads the status of a register with the of his adress
 * as input parameters the function needs a keyword and an adress
 * the function has no return parameter
 */
 void readRegister(struct uartStruct *ptr_uartStruct)
 { 
	 
	
	 if(0XFF < ptr_uartStruct->Uart_Message_ID) 
	 { 
		 uart_errorCode = Communication_Error(S_ERROR9,S_ERROR9_NO, "erra");
	 }
	 else if(0XFF < ptr_uartStruct->Uart_Mask) 
	 {
		 uart_errorCode = Communication_Error(S_ERROR11,S_ERROR11_NO, "erra");
	 } 
	 else{
		 uint8_t  *rAdr_Reg;

		 if(0 == StartValue){

			 rAdr_Reg = ptr_uartStruct->Uart_Message_ID;
			 snprintf(tmp_str,BUFFER_SIZE-1,"RECV %x",*rAdr_Reg);
			 UART0_Send_Message_String(tmp_str);	 
		 } 
		 
		if(1 == StartValue)
		 {
			 snprintf(tmp_str,BUFFER_SIZE-1,"RECV %x",Val_Reg);
			 UART0_Send_Message_String(tmp_str);
			 StartValue = 0;
		 }	
	 }
	
 }
 

