⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cc2420db_library.c

📁 MatLab图像传感器网络仿真平台WiSNAP
💻 C
📖 第 1 页 / 共 2 页
字号:
/*******************************************************************************
 * Chipcon CC2420DB Library.                                                   *
 *                                                                             *
 * Functions:                                                                  *
 * ==========                                                                  *
 *                                                                             *
 * o Read ATmega128 Register                                                   *
 *   Command..: 'axx<CR/LF>'                                                   *
 *   Response.: ':AXXYY<CR><LF>'                                               *
 *                                                                             *
 * o Write ATmega128 Register                                                  *
 *   Command..: 'bxxyy<CR/LF>'                                                 *
 *   Response.: ':BXXYY<CR><LF>'                                               *
 *                                                                             *
 * o Read CC2420 Register                                                      *
 *   Command..: 'cxx<CR/LF>'                                                   *
 *   Response.: ':CXXYY<CR><LF>'                                               *
 *                                                                             *
 * o Write CC2420 Register                                                     *
 *   Command..: 'dxxyy<CR/LF>'                                                 *
 *   Response.: ':DXXYY<CR><LF>'                                               *
 *                                                                             *
 * o Initialize CC2420 Transceiver                                             *
 *   Command..: 'iccppppaaaa<CR/LF>'                                           *
 *   Response.: ':ICCPPPPAAAA<CR><LF>'                                         *
 *                                                                             *
 * o Receive CC2420 Packet                                                     *
 *   Command..: 'r<CR/LF>'                                                     *
 *   Response.: ':R<CR><LF>' if no packet received.                            *
 *              ':RSSAAAAPPPPLLD...KKRR<CR><LF>' if packet received.           *
 *                                                                             *
 * o Send CC2420 Packet                                                        *
 *   Command..: 'sppppaaaalld...kk<CR/LF>'                                     *
 *   Response.: ':SEE<CR><LF>'                                                 *
 *                                                                             *
 * o Toggle Location LED                                                       *
 *   Command..: 't<CR/LF>'                                                     *
 *   Response.: ':T<CR><LF>'                                                   *
 *                                                                             *
 * o Unknown and Invalid Commands                                              *
 *   Response.: ':?<CR><LF>'                                                   *
 *                                                                             *
 * Arguments:                                                                  *
 * ==========                                                                  *
 *                                                                             *
 * 'A...'       Transceiver/packet source/destination address (hexadecimal).   *
 * 'C...'       Transceiver ISM channel number (hexdecimal).                   *
 * 'D...'       Packet payload (hexadecimal).                                  *
 * 'E...'       Success status (hexadecimal: 0 = error, 1 = success).          *
 * 'K...'       Packet acknowledge request (hexadecimal).                      *
 * 'L...'       Packet payload length in bytes (hexadecimal).                  *
 * 'P...'       Transceiver/packet source/destination PAN ID (hexadecimal).    *
 * 'R...'       Packet received signal strength indicator (hexadecimal).       *
 * 'S...'       Packet sequence number (hexadecimal).                          *
 * 'X...'       Register address (hexadecimal).                                *
 * 'Y...'       Register value (hexadecimal).                                  *
 *                                                                             *
 * Hardware Setup:                                                             *
 * ===============                                                             *
 *                                                                             *
 *                                                                             *
 * Compiler: AVR-GCC                                                           *
 * Platform: Chipcon CC2420DB                                                  *
 * Revision: 02-21-2005                                                        *
 *                                                                             *
 * (C) 2005 Stephan Hengstler, Stanford Wireless Sensor Networks Lab           *
 *******************************************************************************/

#include <include.h>
#include <ctype.h>
#include <stdio.h>

/*******************************************************************************
 * Global Variables                                                            *
 *******************************************************************************/

/* basic rf transmission and reception structures */
BASIC_RF_RX_INFO	rfRxInfo;
BASIC_RF_TX_INFO	rfTxInfo;
BYTE					pTxBuffer[BASIC_RF_MAX_PAYLOAD_SIZE];
BYTE					pRxBuffer[BASIC_RF_MAX_PAYLOAD_SIZE];

/* library-specific packet transmission and reception structures */
BOOL 					sig_packet_rx;

/*******************************************************************************
 * Function: BASIC_RF_RX_INFO* basicRfReceivePacket(BASIC_RF_RX_INFO *pRRI)    *
 *                                                                             *
 * DESCRIPTION:                                                                *
 *    This function is a part of the basic RF library, but must be declared by *
 *    the application. Once the application has turned on the receiver, using  *
 *    basicRfReceiveOn(), all incoming packets will be received by the FIFOP   *
 *    interrupt service routine. When finished, the ISR will call the          *
 *    basicRfReceivePacket() function. Please note that this function must     *
 *    return quickly, since the next received packet will overwrite the active *
 *    BASIC_RF_RX_INFO structure (pointed to by pRRI).                         *
 *                                                                             *
 * ARGUMENTS:                                                                  *
 *	   BASIC_RF_RX_INFO *pRRI                                                   *
 *       The reception structure, which contains all relevant info about the   *
 *       received packet.                                                      *
 *                                                                             *
 * RETURN VALUE:                                                               *
 *    BASIC_RF_RX_INFO*                                                        *
 *       The pointer to the next BASIC_RF_RX_INFO structure to be used by the  *
 *       FIFOP ISR. If there is only one buffer, then return pRRI.             *
 *******************************************************************************/
BASIC_RF_RX_INFO* basicRfReceivePacket(BASIC_RF_RX_INFO *pRRI)
{

	/* blink the red led */
	SET_RLED();
	halWait(10000);
	CLR_RLED();

	/*	signal packet reception */
	sig_packet_rx = TRUE;

	/* continue using the (one and only) reception structure */
	return pRRI;
}

/*******************************************************************************
 * Function: RF Send Packet.                                                   *
 *******************************************************************************/
int RfSendPacket(BASIC_RF_TX_INFO *rfTxInfo)
{
	int				status;

	status = basicRfSendPacket(rfTxInfo);
	if (status)
	{

		/* blink the yellow led */
		SET_YLED();
		halWait(10000);
		CLR_YLED();

	}
	else
	{

		/* blink the orange led */
		SET_OLED();
		halWait(10000);
		CLR_OLED();

	}

	/* return status */
	return status;
}

/*******************************************************************************
 * Function: Read ATmega128 register.                                          *
 *******************************************************************************/
int read_at_register(char *command, char *response)
{
	int				status;
	unsigned int 	address = 0, value = 0;
	
	/* parse command argument(s) */
	status = sscanf(&command[1], "%2X", &address);

	/* execute command */
	value = _SFR_MEM8(address);

	/* generate command response */
	status = sprintf(&response[0], ":%c%02X%02X\r\n",
		command[0], address, value);

	/* return status */
	return status;
}

/*******************************************************************************
 * Function: Write ATmega128 register.                                         *
 *******************************************************************************/
int write_at_register(char *command, char *response)
{
	int				status;
	unsigned int 	address = 0, value = 0;

	/* parse command argument(s) */
	status = sscanf(&command[1], "%2X%2X", &address, &value);

	/* execute command */
	_SFR_MEM8(address) = value;

	/* generate command response */
	status = sprintf(&response[0], ":%c%02X%02X\r\n",
		command[0], address, value);

	/* return status */
	return status;
}

/*******************************************************************************
 * Function: Read CC2420 register.                                             *
 *******************************************************************************/
int read_cc_register(char *command, char *response)
{
	int				status;
	unsigned int 	address = 0, value = 0;
	
	/* parse command argument(s) */
	status = sscanf(&command[1], "%2X", &address);

	/* execute command */
	FASTSPI_GETREG(address, value);

	/* generate command response */
	status = sprintf(&response[0], ":%c%02X%04X\r\n",
		command[0], address, value);

	/* return status */
	return status;
}

/*******************************************************************************
 * Function: Write CC2420 register.                                            *
 *******************************************************************************/
int write_cc_register(char *command, char *response)
{
	int				status;
	unsigned int 	address = 0, value = 0;
	
	/* parse command argument(s) */
	status = sscanf(&command[1], "%2X%4X", &address, &value);

	/* execute command */
	FASTSPI_SETREG(address, value);

	/* generate command response */
	status = sprintf(&response[0], ":%c%02X%04X\r\n",
		command[0], address, value);

	/* return status */
	return status;
}

/*******************************************************************************
 * Function: Set ATmega128 port direction.                                     *
 *******************************************************************************/

/*******************************************************************************
 * Function: Read ATmega128 port.                                              *
 *******************************************************************************/

/*******************************************************************************
 * Function: Write ATmega128 port.                                             *
 *******************************************************************************/

/*******************************************************************************
 * Function: Initialize CC2420 transceiver.                                    *
 *******************************************************************************/
int init_cc_transceiver(char *command, char *response)
{
	int				status;
	unsigned int 	channel = 0, panid = 0, myaddress = 0;
	
	/* parse command argument(s) */
	status = sscanf(&command[1], "%2X%4X%4X", &channel, &panid, &myaddress);

	/* execute command */
	basicRfInit(&rfRxInfo, channel, panid, myaddress);
	rfRxInfo.pPayload	= pRxBuffer;
	rfTxInfo.pPayload = pTxBuffer;
	basicRfReceiveOn();

	/* generate command response */
	status = sprintf(&response[0], ":%c%02X%04X%04X\r\n",
		command[0], channel, panid, myaddress);

	/* return status */
	return status;
}

/*******************************************************************************
 * Function: Receive CC2420 packet.                                            *
 *******************************************************************************/
int recv_cc_packet(char *command, char *response)
{
	int				i, status;

	/* generate command response */
	if (sig_packet_rx == TRUE)
	{

		/* add packet header */
		status = sprintf(&response[0], ":%c%02X%04X%04X%02X", command[0],
			rfRxInfo.seqNumber, rfRxInfo.srcAddr, rfRxInfo.srcPanId,
			rfRxInfo.length);

		/* add packet payload */
		for (i = 0; i < rfRxInfo.length; i++)
		{
			status = sprintf(&response[14+2*i], "%02X", rfRxInfo.pPayload[i]);
		}

		/* add packet footer */
		status = sprintf(&response[14+2*rfRxInfo.length], "%02X%02X\r\n",
			rfRxInfo.ackRequest, rfRxInfo.rssi & 0xff);

	}
	else
	{

		/* no new packet available */
		status = sprintf(&response[0], ":%c\r\n", command[0]);
	}

	/* clear packet reception */
	sig_packet_rx = FALSE;

	/* return status */
	return status;
}

/*******************************************************************************
 * Function: Send CC2420 packet.                                               *
 *******************************************************************************/
int send_cc_packet(char *command, char *response)
{
	int				i, status;
	unsigned int	dummy = 0;

	/* parse packet header */
	status = sscanf(&command[1], "%4hX%4hX%2X",
 		&rfTxInfo.destPanId, &rfTxInfo.destAddr, &dummy);
	rfTxInfo.destPanId = (command[1] != '0' ? 
		rfTxInfo.destPanId : rfTxInfo.destPanId >> 4);
	rfTxInfo.destAddr = (command[5] != '0' ? 
		rfTxInfo.destAddr : rfTxInfo.destAddr >> 4);
	rfTxInfo.length = (INT8) (command[9] != '0' ? dummy : dummy >> 4);

	/* parse packet payload */
	for (i = 0; i < rfTxInfo.length; i++)
	{
		status = sscanf(&command[11+2*i], "%2X", &dummy);
		rfTxInfo.pPayload[i] = (BYTE) (command[11+2*i] != '0' ?
			dummy : dummy >> 4);;
	}

	/* parse packet footer */
	status = sscanf(&command[11+2*rfTxInfo.length], "%2X",
		&dummy);
	rfTxInfo.ackRequest = (BOOL) dummy & 0x01;

	/* add packet header */
	status = sprintf(&response[0], ":%c%04X%04X%02X", command[0],
		rfTxInfo.destPanId, rfTxInfo.destAddr,rfTxInfo.length);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -