#include "io08GT32.h"
#include "spi.h"
#include "cc1100.h"
#include "cpu_init.h"
#include "hc08def.h"
// define Set/Clear Operations
#define Set_CSN PTED|=0x04
#define Clear_CSN PTED&=~0x04
unsigned char SPI_State;
unsigned char ClockAdjust;
unsigned int TimerValue;
unsigned char BurstMode,BurstInit;
unsigned char SPI_Buffer[_maxburst_];
//------------------------------------------------------
unsigned char SPI_Access(unsigned char addr, unsigned char data)
{
unsigned char SPI_Temp;
SEI();
Clear_CSN; // force CS low for SPI access
if ((BurstMode==0)||(BurstInit==1))
{
while (PTED&0x08) // wait for MISO to go low --> CC1100 is ready to act
{
}
}
WaitSPI(); // wait for buffer ready to transmit
SPI_Temp=SPIS; // clear SPI status register again
if ((BurstMode==0)||(BurstInit==1))
{
SPID=addr; // write address
while (~SPIS&0x80) // wait for SPI RX data to be ready
{
}
SPI_State=SPID;
WaitSPI(); // and wait again for buffer ready
}
SPI_Temp=SPIS;
SPID=data;
while (~SPIS&0x80) // wait for SPI RX data to be ready
{
}
SPI_Temp=SPID; // read status from SPI register
if (BurstMode==0)
Set_CSN; // and set CS high again to finish SPI access
CLI();
return SPI_Temp;
}
//------------------------------------------------------
unsigned char SPI_rx(unsigned char addr)
{
unsigned char dummy;
//SEI(); // disable Interrupts
BurstMode=0;
BurstInit=0;
addr+=0x80; // modify for read access
dummy=SPI_Access(addr,0x00);
//CLI(); // enable interrupts again
return dummy; // and return received data
}
//------------------------------------------------------
void SPI_tx(unsigned char addr,unsigned char data)
{
unsigned char dummy;
BurstMode=0;
BurstInit=0;
addr&=0x3f; // modify for write access
dummy=SPI_Access(addr,data);
}
//------------------------------------------------------
void SPI_BurstTx(unsigned char addr, unsigned char NumBytes)
{
unsigned char e,dummy;
BurstMode=1;
BurstInit=1;
addr+=0x40; // modify for write access, set burst bit
for (e=0;e<NumBytes;e++)
{
dummy=SPI_Access(addr,SPI_Buffer[e]);
BurstInit=0;
if (addr==0x3f) // Burst Write to TX-FIFO ?
while ((SPI_State&0x0f)<2)
{ // if yes: danger of underflow ? then wait until FIFO is clear again
}
}
Set_CSN;
}
//------------------------------------------------------
void SPI_BurstRx(unsigned char addr,unsigned char NumBytes)
{
unsigned char e;
for (e=0;e<NumBytes;e++)
SPI_Buffer[e]=0;
BurstMode=1;
BurstInit=1;
addr+=0xc0; // modify for write access, set burst bit
for (e=0;e<NumBytes;e++)
{
SPI_Buffer[e]=SPI_Access(addr,0x00);
BurstInit=0;
}
Set_CSN;
}
//-----------------------------------------------------
void SPI_Reset(void)
{ // resets the CC1100 via special SPI control, thus located in this segment
unsigned char dummy;
Clear_CSN;
Set_CSN;
hwdelay(10); // wait for more than 40us for system to settle
BurstMode=1; // set to keep CSn low after command strobe
BurstInit=1;
dummy=SPI_Access(0x30,0); // send System reset strobe
while (PTED&0x08) // and wait for MISO to go low again
{
}
Set_CSN; // disable SPI again --> System reset accomplished
}
//------------------------------------------------------
void WaitSPI(void)
{
while ((SPIS&0x20)==0) // wait until data exchange is finished (SPTEF=1)
{
}
}
//------------------------------------------------------
void SPIInit(void)
{
// Initializes the SPI Interface to the CC1100 with the corresponding parameters
// Disable SPI RX and TX Interrupts, Enable SPI, set uC as Master, CPOL and CPHA=1, MSB first
Set_CSN;
SPIC1=0b01011110;
// allow external routine to handle SS pin
SPIC2=0b00000000;
// first set SPI Baud rate according to uC reset state before taking over clock from CC1100
// after Reset, Bus Clock is approx. 16 MHz
SPIBR=0x00; // set 2 MHz SPI Clock
}