📄 ti_cc_spi.c
字号:
#include "TI_CC_Spi.h"
extern RF_SETTINGS rfSettings;
extern BYTE paTable[1];
#define WRITE_BURST 0x40
#define READ_SINGLE 0x80
#define READ_BURST 0xC0
#define ENABLE_TX TI_CC_SpiStrobe(CCxxx0_STX)
#define ENABLE_RX TI_CC_SpiStrobe(CCxxx0_SRX)
#define BYTES_IN_RXFIFO 0x7F
#define GDO0_PIN (PIND & 0x01)
#define GDO2_PIN (PIND & 0x02)
#define MISO_1 (PINB & 0x08)
#define CRC_OK 0x80
#define RSSI 0
#define LQI 1
#define BYTES_IN_RXFIFO 0x7F
// Macro to SPI
#define SPI_WAIT() do {while (!(SPSR & BM(SPIF)));} while (0)
#define SPI_TX(x) do {SPDR = x; SPI_WAIT(); } while (0)
#define SPI_RX(x) do {SPDR = 0; SPI_WAIT();x = SPDR; } while (0)
//-------------------------------------------------------------------------------------------------------
// void Wait(BYTE timeout)
//
// DESCRIPTION:
// Runs an idle loop for [timeout] microseconds.
//
// ARGUMENTS:
// BYTE timeout
// The timeout in microseconds
//-------------------------------------------------------------------------------------------------------
void Wait(BYTE timeout)
{
// This sequence uses exactly 8 clock cycle for each round
do {
NOP();
NOP();
NOP();
NOP();
} while (--timeout);
} //Wait
void Spi_Setup(void)
{
SPCR = BM(SPE) | BM(MSTR); //SPI ENABLE;
//MSTR置位主机模;
//CPOL=0,CPHA=0 工作模式为模式0 ;
//SPR1=0,SPR0=0,Focr/4;
}
//-------------------------------------------------------------------------------------------------------
// IMPORTANT NOTICE:
//
// min 40 us
// <------------------>
// CSn |--| |--------------------| |-----
// | | | | |
// -- ------------
//
// MISO |----|
// -----------------------------| | |
// -- ---------
// Unknown / don't care
// SRES done
//-------------------------------------------------------------------------------------------------
void TI_CC_PowerupResetCCxxx0(void)
{
SPI_DISABLE();
Wait(1);
SPI_ENABLE();
Wait(1);
SPI_DISABLE();
Wait(41);
SPI_ENABLE();
while ((PINB&0x08));
SPDR = CCxxx0_SRES;
SPI_WAIT();
while ((PINB&0x08));
SPI_DISABLE();
}
//-------------------------------------------------------------------------------------------------------
// BYTE TI_CC_SpiReadReg(BYTE addr)
// 读单个寄存器
// DESCRIPTION:
// This function gets the value of a single specified CCxxx0 register.
//
// ARGUMENTS:
// BYTE addr
// Address of the CCxxx0 register to be accessed.
//
// RETURN VALUE:
// BYTE
// Value of the accessed CCxxx0 register.
//-------------------------------------------------------------------------------------------------------
BYTE TI_CC_SpiReadReg(BYTE addr)
{
UINT8 x;
SPI_ENABLE();
NOP();
while(MISO_1);
NOP();
SPI_TX(addr | READ_SINGLE);
SPI_RX(x);
NOP();
SPI_DISABLE();
return x;
}// TI_CC_SpiReadReg
//-------------------------------------------------------------------------------------------------------
// BYTE TI_CC_SpiReadStatus(BYTE addr)
// 读CC1100 状态寄存器
// DESCRIPTION:
// This function reads a CCxxx0 status register.
//
// ARGUMENTS:
// BYTE addr
// Address of the CCxxx0 status register to be accessed.
//
// RETURN VALUE:
// BYTE
// Value of the accessed CCxxx0 status register.
//-------------------------------------------------------------------------------------------------------
BYTE TI_CC_SpiReadStatus(BYTE addr)
{
UINT8 x;
SPI_ENABLE();
NOP();
while(MISO_1);
NOP();
SPI_TX(addr | READ_BURST);
SPI_RX(x);
NOP();
SPI_DISABLE();
return x;
}// TI_CC_SpiReadStatus
//-------------------------------------------------------------------------------------------------------
// void TI_CC_SpiReadBurstReg(BYTE addr, BYTE *buffer, BYTE count)
// 连续读取缓冲区数据
// DESCRIPTION:
// This function reads multiple CCxxx0 register, using SPI burst access.
//
// ARGUMENTS:
// BYTE addr
// Address of the first CCxxx0 register to be accessed.
// BYTE *buffer
// Pointer to a byte array which stores the values read from a
// corresponding range of CCxxx0 registers.
// BYTE count
// Number of bytes to be written to the subsequent CCxxx0 registers.
//-------------------------------------------------------------------------------------------------------
void TI_CC_SpiReadBurstReg(BYTE addr, BYTE *buffer, BYTE count)
{
UINT8 i;
SPI_ENABLE();
while(MISO_1);
NOP();
SPI_TX(addr | READ_BURST);
for (i = 0; i < count; i++)
{
SPI_RX(buffer[i]);
}
NOP();
SPI_DISABLE();
buffer[i] = 0; // add a terminal char
}// TI_CC_SpiReadBurstReg
//-------------------------------------------------------------------------------------------------------
// void TI_CC_SpiStrobe(BYTE strobe)
// 发送命令字节
// DESCRIPTION:
// Function for writing a strobe command to the CCxxx0
//
// ARGUMENTS:
// BYTE strobe
// Strobe command
//-------------------------------------------------------------------------------------------------------
void TI_CC_SpiStrobe(BYTE strobe)
{
SPI_ENABLE();
NOP();
while(MISO_1);
NOP();
SPI_TX(strobe);
NOP();
SPI_DISABLE();
}// TI_CC_SpiStrobe
//-------------------------------------------------------------------------------------------------------
// void TI_CC_SpiWriteReg(BYTE addr, BYTE value)
// 写单个寄存器
// DESCRIPTION:
// Function for writing to a single CCxxx0 register
//
// ARGUMENTS:
// BYTE addr
// Address of a specific CCxxx0 register to accessed.
// BYTE value
// Value to be written to the specified CCxxx0 register.
//-------------------------------------------------------------------------------------------------------
void TI_CC_SpiWriteReg(BYTE addr, BYTE value)
{
SPI_ENABLE();
NOP();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -