#include "includes.h"



 /****************************************************************************  
 * ReadRawRC()  
 ****************************************************************************/   
 unsigned char ReadRawRC(unsigned char Address)   
 {   
     unsigned char a;   
	 tUInt loop=0;
	 
	 // zde hrozi kolize mezi RX UART a odesilanim na SPI
	 // aby se tomu zabranilo, tak UART ma prioritu
	 if (BusControl.BusState & SCI_BUSY)
	  {
		do { 
			 ClearWDT();
		     Start_TimerA();
			 traic.byte = INT_LEVEL_TIMER_A;
			 loop++;
			} while ((BusControl.BusState & SCI_BUSY) && (loop<10));	// omezenei proti zatuhnuti smysky
	  }
	  
	  
	 SPI_CS = 0;
	 asm ("nop");	 
	 
     SPI_TranscieveByte((Address<<1)|0x80);   
	 a = SPI_TranscieveByte(0x00);    
	 	 	 
	 asm ("nop");
     SPI_CS = 1;	
	 
     return a;         
 } 
    

   
 /****************************************************************************  
 * WriteRawRC()  
 ****************************************************************************/   
 void WriteRawRC(unsigned char Address, unsigned char value)   
 {   
     tUInt loop=0;
	 
	 // zde hrozi kolize mezi RX UART a odesilanim na SPI
	 // aby se tomu zabranilo, tak UART ma prioritu
	 if (BusControl.BusState & SCI_BUSY)
	  {
		do {
			 ClearWDT();
		     Start_TimerA();
			 traic.byte = INT_LEVEL_TIMER_A;
			 loop++;
		   } while ((BusControl.BusState & SCI_BUSY) && (loop<10));		// pro jistotu omezit smycku proti zatuhnuti
	  }

	 SPI_CS = 0;
	 asm ("nop");

     SPI_TranscieveByte((Address<<1)&0x7f);   
     SPI_TranscieveByte(value);   
	 
	 asm ("nop");
	 SPI_CS = 1;

 }  


/****************************************************************************  
 * SPI_TranscieveByte()  
 ****************************************************************************/   
char SPI_TranscieveByte(unsigned char value)
 {   
   char i,temp;   

   for(i=0;i<8;i++)   
    {     
     SPI_CLK = 0;   
     if(value & 0x80)
      {    
       SPI_MOSI = 1;   
      }   
     else   
      {    
       SPI_MOSI = 0;   
      }   
	  
     value <<= 1;   
     SPI_CLK = 1; 
	 temp <<= 1;   
	 
     if(SPI_MISO)   
      temp |= 0x01;   
    } 
	  
     SPI_CLK = 0;   
	 SPI_MOSI = 0;   
 
     return (temp);   
 }   
    
 
   
