📄 common.c
字号:
#include "common.h"
// Defines
#define CRC_OK 0x80
#define GDO0_PIN P0_6
#define RSSI 0
#define LQI 1
#define WRITE_BURST 0x40
#define READ_SINGLE 0x80
#define READ_BURST 0xC0
void Software_delay(unsigned int i)
{
while(i--)
{;}
}
void send_uart0_a_byte(unsigned char sendbyte)
{
while (!(IFG1 & UTXIFG0)); // USART0 TX buffer ready?
TXBUF0 = sendbyte;
}
//=================================================================
//往SPI写数据
//IN:value
//OUT:spi_status
//=======================================================================
unsigned char SPI_write(unsigned char value)
{
unsigned char i,spi_status;
spi_status=0;
for(i=0;i<8;i++)
{
if(value&0x80)
{
P_SCLK_L;
P_SI_H;
}
else
{
P_SCLK_L;
P_SI_L;
}
Software_delay(5);
P_SCLK_H;
value <<=1;
spi_status<<=1;
if( P_SO !=0 )
spi_status=spi_status|0x01;
else
spi_status=spi_status&0xfe;
}
P_SCLK_L;
Software_delay(5);
return(spi_status);
}
//=================================================================
//从SPI读RF数据
//IN:NONE
//OUT:value
//=======================================================================
unsigned char SPI_read(void)
{
unsigned char i,value;
value=0;
for(i=0;i<8;i++)
{
value <<=1;
P_SCLK_H;
if(P_SO) value|=0x01;
else value&=0xFE;
// Software_delay(5);
P_SCLK_L;
}
// Software_delay(5);
return value;
}
//-------------------------------------------------------------------------------------------------------
// BYTE halSpiReadReg(BYTE addr)
//
// DESCRIPTION:
// This function gets the value of a single specified CCxxx0 register.
// 通过SPI读单个寄存器的数据
// ARGUMENTS:
// BYTE addr
// Address of the CCxxx0 register to be accessed.
//
// RETURN VALUE:
// BYTE
// Value of the accessed CCxxx0 register.
//-------------------------------------------------------------------------------------------------------
unsigned char halSpiReadReg(unsigned char addr)
{
unsigned char value;
P_CSn_L;
while(P_SO);
addr|=READ_SINGLE;
SPI_write(addr);
value=SPI_read();
P_CSn_H;
return value;
}
//-------------------------------------------------------------------------------------------------------
// BYTE halSpiReadStatus(BYTE addr)
//
// DESCRIPTION:
// This function reads a CCxxx0 status register.
// 通过SPI读RF的状态
// ARGUMENTS:
// BYTE addr
// Address of the CCxxx0 status register to be accessed.
//
// RETURN VALUE:
// BYTE
// Value of the accessed CCxxx0 status register.
//-------------------------------------------------------------------------------------------------------
unsigned char halSpiReadStatus(unsigned char addr)
{
unsigned char value;
P_CSn_L;
while(P_SO);
addr|=READ_BURST;
SPI_write(addr);
value=SPI_read();
P_CSn_H;
return value;
}
//-------------------------------------------------------------------------------------------------------
// void halSpiWriteReg(BYTE addr, BYTE value)
//
// DESCRIPTION:
// Function for writing to a single CCxxx0 register
// 通过SPI写数据到一个寄存器
// ARGUMENTS:
// BYTE addr
// Address of a specific CCxxx0 register to accessed.
// BYTE value
// Value to be written to the specified CCxxx0 register.
//-------------------------------------------------------------------------------------------------------
void halSpiWriteReg(unsigned char addr, unsigned char value)
{
P_CSn_L;
while(P_SO);
addr&=0x7F;
SPI_write(addr);
SPI_write(value);
P_CSn_H;
}
//-------------------------------------------------------------------------------------------------------
// void halSpiReadBurstReg(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 halSpiReadBurstReg(unsigned char addr, unsigned char *buffer, unsigned char count)
{
unsigned char j,value;
P_CSn_L;
while(P_SO);
addr|=READ_BURST;
SPI_write(addr);
for(j=0;j<count;j++)
{
value=SPI_read();
buffer[j]=value;
}
P_CSn_H;
}
//-------------------------------------------------------------------------------------------------------
// void halSpiWriteBurstReg(BYTE addr, BYTE *buffer, BYTE count)
//
// DESCRIPTION:
// This function writes to multiple CCxxx0 register, using SPI burst access.
// 写多个数据到RF寄存器里
// ARGUMENTS:
// BYTE addr
// Address of the first CCxxx0 register to be accessed.
// BYTE *buffer
// Array of bytes to be written into a corresponding range of
// CCxx00 registers, starting by the address specified in _addr_.
// BYTE count
// Number of bytes to be written to the subsequent CCxxx0 registers.
//-------------------------------------------------------------------------------------------------------
void halSpiWriteBurstReg(unsigned char addr, unsigned char *buffer, unsigned char count)
{
unsigned char j,value;
P_CSn_L;
while(P_SO);
addr|=WRITE_BURST;
SPI_write(addr);
for(j=0;j<count;j++)
{
value=buffer[j];
SPI_write(value);
Software_delay(20);
}
P_CSn_H;
}
//-------------------------------------------------------------------------------------------------------
// void halSpiStrobe(BYTE strobe)
//
// DESCRIPTION:
// Function for writing a strobe command to the CCxxx0
// 写一个命令到RF
// ARGUMENTS:
// BYTE strobe
// Strobe command
//-------------------------------------------------------------------------------------------------------
void halSpiStrobe(unsigned char strobe)
{
P_CSn_L;
while(P_SO);
SPI_write(strobe);
P_CSn_H;
}
//-------------------------------------------------------------------------------------------------------
// void halRfSendPacket(BYTE *txBuffer, UINT8 size)
// 发送数据包
// ARGUMENTS:
// BYTE *txBuffer
// Pointer to a buffer containg the data that are going to be transmitted
//
// UINT8 size
// The size of the txBuffer
//-------------------------------------------------------------------------------------------------------
void halRfSendPacket(unsigned char *txBuffer, unsigned char size)
{
//halSpiStrobe(CCxxx0_SIDLE);
// Software_delay(1000);
//halSpiStrobe(CCxxx0_SFTX);
halSpiWriteBurstReg(CCxxx0_TXFIFO, txBuffer, size);
Software_delay(3000);
halSpiStrobe(CCxxx0_STX);
//Software_delay(2000);
while(!P_GDO0);
while(P_GDO0);
}
//-------------------------------------------------------------------------------------------------------
// BOOL halRfReceivePacket(BYTE *rxBuffer, UINT8 *length)
// 接收数据包
//
// ARGUMENTS:
// BYTE *rxBuffer
// Pointer to the buffer where the incoming data should be stored
// UINT8 *length
// Pointer to a variable containing the size of the buffer where the incoming data should be
// stored. After this function returns, that variable holds the packet length.
//
// RETURN VALUE:
// BOOL
// TRUE: CRC OK
// FALSE: CRC NOT OK
//-------------------------------------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -