📄 halgenfuns.c
字号:
#include <COMMON.h>
#include <intrins.h>
#include "halregssrf.h"
#ifdef __TRANSFER__
sbit SPI_CLK = P0^0;
sbit SPI_SO = P0^1;
sbit SPI_SI = P0^2;
sbit SPI_CS = P0^3;
sbit GDO0_PIN = P0^6;
#endif
#ifdef __RECV__
sbit SPI_CLK = P0^0;
sbit SPI_SO = P0^1;
sbit SPI_SI = P0^2;
sbit SPI_CS = P0^3;
sbit GDO0_PIN = P0^7;
#endif
#define LQI 1
#define BYTES_IN_RXFIFO 0x7F
#define CRC_OK 0x80
//-------------------------------------------------------------------------------------------------------
// void halWait(UINT16 timeout)
//
// DESCRIPTION:
// Runs an idle loop for [timeout] microseconds.
//
// ARGUMENTS:
// UINT8 timeout
// The timeout in microseconds
//-------------------------------------------------------------------------------------------------------
void halWait(UINT16 timeout) {
do {
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
} while (--timeout);
}// halWait
//-------------------------------------------------------------------------------------------------------
// Macro to reset the CCxxx0 and wait for it to be ready
#define RESET_CCxxx0() \
do { \
SPI_CS = 0; \
while (SPI_SO); \
SPI0DAT = CCxxx0_SRES; \
SPIF=0; \
while(!SPIF); \
SPI_CS = 1; \
} while (0)
//-------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------
// Macro to reset the CCxxx0 after power_on and wait for it to be ready
// IMPORTANT NOTICE:
// The file Wait.c must be included if this macro shall be used
// The file is located under: ..\Lib\Chipcon\Hal\CCxx00
//
// min 40 us
// <----------------------->
// CSn |--| |--------------------| |-----------
// | | | | |
// -- ----------
//
// MISO |---------|
// - - - - - - - - - - - - - - - -| | |
// -- ------
// Unknown / don't care
//
// MOSI - - - - - - - - - - - - - - - ---------- - - - - -
// | SRES |
// - - - - - - - - - - - - - - - ---------- - - - - -
//
#define POWER_UP_RESET_CCxxx0() \
do { \
SPI_CS = 1; \
halWait(1); \
SPI_CS = 0; \
halWait(1); \
SPI_CS = 1; \
halWait(41); \
RESET_CCxxx0(); \
} while (0)
//-------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------
// void halSpiStrobe(BYTE strobe)
//
// DESCRIPTION:
// Function for writing a strobe command to the CCxxx0
//
// ARGUMENTS:
// BYTE strobe
// Strobe command
//-------------------------------------------------------------------------------------------------------
void halSpiStrobe(BYTE strobe) {
SPI_CS = 0;
while (SPI_SO);
SPI0DAT = strobe;
SPIF=0;
while(!SPIF);
SPI_CS = 1;
}// halSpiStrobe
//-------------------------------------------------------------------------------------------------------
// BYTE halSpiReadStatus(BYTE addr)
//
// 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 halSpiReadStatus(BYTE addr) {
UINT8 x;
SPI_CS = 0;
while (SPI_SO);
SPI0DAT = (addr | READ_BURST);
SPIF=0;
while(!SPIF);
SPI0DAT = 0;
SPIF=0;
while(!SPIF);
x = SPI0DAT;
SPI_CS = 1;
return x;
}// halSpiReadStatus
//-------------------------------------------------------------------------------------------------------
// BYTE halSpiReadReg(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 halSpiReadReg(BYTE addr) {
UINT8 x;
SPI_CS = 0;
while (SPI_SO);
SPI0DAT = (addr | READ_SINGLE);
SPIF=0;
while(!SPIF);
SPI0DAT = 0;
SPIF=0;
while(!SPIF);
x = SPI0DAT;
SPI_CS = 1;
return x;
}// halSpiReadReg
//-------------------------------------------------------------------------------------------------------
// 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(BYTE addr, BYTE *buffer, BYTE count) {
UINT8 i;
SPI_CS = 0;
while (SPI_SO);
SPI0DAT = (addr | READ_BURST);
SPIF=0;
while(!SPIF);
for (i = 0; i < count; i++) {
SPI0DAT = 0;
SPIF=0;
while(!SPIF);
buffer[i] = SPI0DAT;
}
SPI_CS = 1;
}// halSpiReadBurstReg
//-------------------------------------------------------------------------------------------------------
// void halSpiWriteReg(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 halSpiWriteReg(BYTE addr, BYTE value) {
SPI_CS = 0;
while (SPI_SO);
SPI0DAT = addr;
SPIF=0;
while(!SPIF);
SPI0DAT = value;
SPIF=0;
while(!SPIF);
SPI_CS = 1;
}// halSpiWriteReg
//-------------------------------------------------------------------------------------------------------
// void halSpiWriteBurstReg(BYTE addr, BYTE *buffer, BYTE count)
//
// DESCRIPTION:
// This function writes to multiple CCxxx0 register, using SPI burst access.
//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -