📄 cc2420.c
字号:
{
cc2420_bramspi = FALSE;
cc2420_ramaddr = addr;
cc2420_ramlen = length;
cc2420_rambuf = buffer;
OS_CLR_CC_CS_PIN();
SPDR = (cc2420_ramaddr & 0x7F) | 0x80;
while (!(SPSR & 0x80)) {
}
;
status = SPDR;
SPDR = (cc2420_ramaddr >> 1) & 0xC0;
while (!(SPSR & 0x80)) {
}
;
status = SPDR;
for (i = 0; i < cc2420_ramlen; i++) {
SPDR = cc2420_rambuf[i];
while (!(SPSR & 0x80)) {
}
;
}
}
OS_SET_CC_CS_PIN();
os_atomic_end(_nesc_atomic); }
cc2420_bramspi = TRUE;
return SUCCESS;
}
//-------------------------------------------------------------------------
uint8_t cc2420_writetxfifo(uint8_t len, uint8_t *msg)
{
uint8_t i = 0;
uint8_t status;
{ os_atomic_t _nesc_atomic = os_atomic_start();
{
cc2420fifo_bspi = FALSE;
cc2420fifo_txlength = len;
cc2420fifo_txbuf = msg;
OS_CLR_CC_CS_PIN();
SPDR = 0x3E;
while (!(SPSR & 0x80)) {
}
;
status = SPDR;
for (i = 0; i < cc2420fifo_txlength; i++) {
SPDR = *cc2420fifo_txbuf;
cc2420fifo_txbuf++;
while (!(SPSR & 0x80)) {
}
;
}
cc2420fifo_bspi = TRUE;
}
os_atomic_end(_nesc_atomic); }
OS_SET_CC_CS_PIN();
// printf("a packet will be send!\n");
//cc2420_sendcmd(cc2420_STXON); //send
//printf("a packet is success send!\n");
return status;
}
//-----------------------------------------------------------------------
void cc2420_readrxfifo()
{
uint8_t status;
uint8_t i;
//printf("开始一个读fifo任务\n");
{ os_atomic_t _nesc_atomic = os_atomic_start();
{
cc2420fifo_bspi = FALSE;
OS_CLR_CC_CS_PIN();
SPDR = 0x3F | 0x40;//read rxfifo
while (!(SPSR & 0x80)) ;
status = SPDR;
SPDR = 0;
while (!(SPSR & 0x80)) ;
cc2420_rxlength = SPDR;
//printf("开始分配空间给rxbuf,rxflag is %x\n",cc2420_rxflag);
cc2420_rxbuf[cc2420_rxflag] =malloc(cc2420_rxlength +1);
// printf("point is %04x\n",cc2420_rxbuf[cc2420_rxflag] );
if (cc2420_rxlength > 0) {
cc2420_rxbuf[cc2420_rxflag] [0] = cc2420_rxlength ;
cc2420_rxlength ++;
for (i = 1; i < cc2420_rxlength ; i++) {
SPDR = 0;
while (!(SPSR & 0x80)) {
}
;
cc2420_rxbuf[cc2420_rxflag] [i] = SPDR;
}
}
cc2420fifo_bspi = TRUE;
rfpackernum--;
cc2420_rxflag++;
cc2420_rxflag&=0x01;
}
os_atomic_end(_nesc_atomic); }
OS_SET_CC_CS_PIN();
OS_post(&demo_handlepacket);
}
//-------------------------------------------------------------------------
inline uint8_t cc2420_oscon(void)
{
uint8_t i;
uint8_t status;
bool bXoscOn = FALSE;
i = 0;
cc2420_sendcmd(cc2420_SXOSCON);
while (i < 200 && bXoscOn == FALSE) {
OS_uwait(100);
status = cc2420_sendcmd(cc2420_SNOP);
status = status & (1 << 6);
if (status) {
bXoscOn = TRUE;
}
i++;
}
if (!bXoscOn) {
printf("osc is unsuccess now\n");
return FAIL;
}
printf("osc is success now\n");
return SUCCESS;
}
inline uint8_t cc2420_oscoff(void) {
cc2420_sendcmd(cc2420_SXOSCOFF); //turn-off crystal
return SUCCESS;
}
//--------------------------------------------------------
inline uint8_t cc2420_VREFOn(void){
OS_SET_CC_VREN_PIN(); //turn-on
OS_uwait(1800);
printf("vref is on now\n");
return SUCCESS;
}
inline uint8_t cc2420_VREFOff(void){
OS_CLR_CC_VREN_PIN(); //turn-off
OS_uwait(1800);
return SUCCESS;
}
//------------------------------------------------------------------
inline uint8_t cc2420_enableAutoAck() {
cc2420_currentparameters[CP_MDMCTRL0] |= (1 << cc2420_MDMCTRL0_AUTOACK);
return cc2420_writereg(cc2420_MDMCTRL0,cc2420_currentparameters[CP_MDMCTRL0]);
}
inline uint8_t cc2420_disableAutoAck() {
cc2420_currentparameters[CP_MDMCTRL0] &= ~(1 << cc2420_MDMCTRL0_AUTOACK);
return cc2420_writereg(cc2420_MDMCTRL0,cc2420_currentparameters[CP_MDMCTRL0]);
}
inline uint8_t cc2420_enableAddrDecode() {
cc2420_currentparameters[CP_MDMCTRL0] |= (1 << cc2420_MDMCTRL0_ADRDECODE);
return cc2420_writereg(cc2420_MDMCTRL0,cc2420_currentparameters[CP_MDMCTRL0]);
}
inline uint8_t cc2420_disableAddrDecode() {
cc2420_currentparameters[CP_MDMCTRL0] &= ~(1 << cc2420_MDMCTRL0_ADRDECODE);
return cc2420_writereg(cc2420_MDMCTRL0,cc2420_currentparameters[CP_MDMCTRL0]);
}
//---------------------------------------------------------命令cc2420进入rx模式
inline uint8_t cc2420_setrxmode(void)
{
cc2420_sendcmd(cc2420_SRXON);
return SUCCESS;
}
inline uint8_t cc2420_settxmode(void)
{
cc2420_sendcmd(cc2420_STXON);
return SUCCESS;
}
inline uint8_t cc2420_settxccamode(void)
{
cc2420_sendcmd(cc2420_STXONCCA);
return SUCCESS;
}
inline uint8_t cc2420_setrfoff(void)
{
cc2420_sendcmd(cc2420_SRFOFF);
return SUCCESS;
}
//---------------------------------------------------------设置cc2420 的频道
inline uint8_t cc2420_setchnl(uint8_t chnl)
{
int fsctrl;
fsctrl = 357 + 5 * (chnl - 11);
cc2420_currentparameters[CP_FSCTRL] = (cc2420_currentparameters[CP_FSCTRL] & 0xfc00) | (fsctrl << 0);
cc2420_writereg(cc2420_FSCTRL, cc2420_currentparameters[CP_FSCTRL]);
return SUCCESS;
}
//---------------------------------------------------------设置cc2420短地址,同时写入cc2420
inline uint8_t cc2420_setshortaddress(uint16_t addr)
{
addr = toLSB16(addr);
cc2420_writelargeram(cc2420_RAM_SHORTADR, 2, (uint8_t *)&addr);
printf("shortaddress is set\n");
return SUCCESS;
}
inline uint8_t cc2420_getshortaddress(uint16_t * addr)
{
cc2420_readlargeram(cc2420_RAM_SHORTADR, 2, (uint8_t *)addr);
printf("shortaddress is get\n");
return SUCCESS;
}
inline uint8_t cc2420_setpanid(uint16_t id)
{
id = toLSB16(id);
cc2420_writelargeram(cc2420_RAM_PANID, 2, (uint8_t *)&id);
printf("PANID is set\n");
return SUCCESS;
}
inline uint8_t cc2420_getpanid(uint16_t * id)
{
cc2420_readlargeram(cc2420_RAM_PANID, 2, (uint8_t *)&id);
printf("PANID is get\n");
return SUCCESS;
}
//------------------------------------------------------------------------------
inline void cc2420_init(void)
{
cc2420_pin_init();
SPI_init();
cc2420_VREFOn();
OS_CLR_CC_RSTN_PIN();
OS_uwait(10);
OS_SET_CC_RSTN_PIN();
OS_uwait(10);
cc2420_oscon();
cc2420_reginit();
cc2420_setreg();
cc2420_rxbuf_now=0;
cc2420_rxlength_now=0;
cc2420_rxflag=0;
cc2420_rxbufuse=0;
rfpackernum=0;
printf("cc2420 init is success\n");
}
void demo_handlepacket(void)
{
uint8_t num,i,flag;
flag=(cc2420_rxflag+cc2420_rxbufuse)&0x01;
cc2420_rxbuf_now=cc2420_rxbuf[flag];
//intf("\nreceive a packet in channel %04x!\n",cc2420_readreg(cc2420_FSCTRL));
printf("\n---------------receive dsn is %02x\n",cc2420_rxbuf_now[3]);
free(cc2420_rxbuf[flag]);
cc2420_rxbufuse--;
if ((cc2420_rxbufuse==0) && (rfpackernum!=0))
{
cc2420_rxbufuse++;
OS_post(&cc2420_readrxfifo);
}
cc2420_rxbuf[flag]=NULL;
//printf("释放rxbuf,now point is %04x\n",cc2420_rxbuf[flag]);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -