📄 mac_support.c
字号:
/*******************************************************************************************************
* *
* ********** *
* ************ *
* *** *** *
* *** +++ *** *
* *** + + *** *
* *** + CHIPCON CC2420 INTEGRATED 802.15.4 MAC AND PHY *
* *** + + *** Various Support/Utility Functions *
* *** +++ *** *
* *** *** *
* ************ *
* ********** *
* *
*******************************************************************************************************
* CONFIDENTIAL *
* The use of this file is restricted by the signed MAC software license agreement. *
* *
* Copyright Chipcon AS, 2004 *
*******************************************************************************************************
* This module contains support/utility functions for the MAC sublayer. *
*******************************************************************************************************
* Compiler: AVR-GCC *
* Target platform: CC2420DB, CC2420 + any ATMEGA MCU *
*******************************************************************************************************
* The revision history is located at the bottom of this file *
*******************************************************************************************************/
#pragma SFR
#pragma NOP
#pragma STOP
#pragma HALT
#pragma DI
#pragma EI
#include "mac_headers.h"
UINT8 oldPhyTransmitPower = OUTPUT_POWER_0DBM;
/*******************************************************************************************************
*******************************************************************************************************
************************** GENERAL FUNCTIONS **************************
*******************************************************************************************************
*******************************************************************************************************/
//-------------------------------------------------------------------------------------------------------
// void msupWaitTask(MAC_TASK_INFO *pTask)
//
// DESCRIPTION:
// This task is meant to be placed behind another task to make sure that this task has finihed.
// When the task is run, it will set an 8-bit boolean value, pointed to by the taskData, to FALSE.
//
// TASK DATA:
// A BOOL pointer to the flag to be changed to FALSE
//-------------------------------------------------------------------------------------------------------
void msupWaitTask(MAC_TASK_INFO *pTask) {
ENABLE_FIFOP_INT();
*((BOOL*) pTask->taskData) = FALSE;
mschRemoveTask(pTask->priority, MSCH_KEEP_TASK_IN_PROGRESS_BM);
} // msupWaitTask
//-------------------------------------------------------------------------------------------------------
// BOOL msupCompareQword(QWORD *pA, QWORD *pB)
//
// DESCRIPTION:
// Support function for comparing two 64-bit QWORDs, A and B
//
// PARAMETERS:
// QWORD *pA
// pointer to QWORD A
// QWORD *pB
// pointer to QWORD B
//
// RETURN VALUE:
// BOOL
// TRUE if A and B are equal, false otherwise
//-------------------------------------------------------------------------------------------------------
BOOL msupCompareQword(QWORD *pA, QWORD *pB) {
///return (*pA == *pB);
return( (pA->ldword== pB->ldword)&&(pA->hdword==pB->hdword));///
} // msupCompareQword
//-------------------------------------------------------------------------------------------------------
// void msupReverseCopy(BYTE *pDestination, BYTE *pSource, UINT8 length)
//
// DESCRIPTION:
// Copies an array of data in the reverse order, so that the first part of the destination can
// overlap with the last part of the source:
//
// Source F---------------L (last byte "L" is moved first -> avoids overwriting)
// Destination F---------------L
//-------------------------------------------------------------------------------------------------------
void msupReverseCopy(BYTE *pDestination, BYTE *pSource, UINT8 length) {
pDestination += length;
pSource += length;
while (length--) {
*(--pDestination) = *(--pSource);
}
} // msupReverseCopy
//-------------------------------------------------------------------------------------------------------
// The random generator polynom
#define RANDOM_POLY 0x1021
// The 16-bit shift register which is used to generate random numbers
WORD random;
//-------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------
// void msupInitRandom(void)
//
// DESCRIPTION:
// Initialise 16-bit random word from CC2420 receiver chain. Note that this function assumes that
// the crystal oscillator is running, and that the receiver is not currently in use.
//-------------------------------------------------------------------------------------------------------
void msupInitRandom(void) {
UINT8 i;
// Enable RXBPF, ADC and VGA
FASTSPI_SETREG(CC2420_MANAND, 0xFFF1);
// Output ADC_Q[0] to CCA pin
FASTSPI_SETREG(CC2420_IOCFG1, 0x0001);
random = 0;
// If random == 0, the pseudo-random values will remain 0 (forever) and must therefore be avoided!
while (random == 0) {
for (i = 0; i < 16; i++) {
// Sample ADC_Q[0] 16 times and shift into random register
random = (random << 1) | (!CCA_IS_1());
}
}
// Set CC2420 registers back to default
FASTSPI_SETREG(CC2420_MANAND, 0xFFFF);
FASTSPI_SETREG(CC2420_IOCFG1, 0x0000);
} // msupInitRandom
//-------------------------------------------------------------------------------------------------------
// BYTE msupGetRandomByte(void)
//
// DESCRIPTION:
// Get a single return pseudo-random byte, initialized from msupInitRandom()
//
// RETURN VALUE:
// BYTE
// The low part of the random word
//-------------------------------------------------------------------------------------------------------
BYTE msupGetRandomByte(void) {
UINT8 n = 8;
do {
if ((random >> 8) & 0x80) {
random = (random << 1) ^ RANDOM_POLY;
} else {
random = (random << 1);
}
} while (--n);
return (BYTE) random;
} // msupGetRandomByte
//-------------------------------------------------------------------------------------------------------
// void msupSetTransmitPower(void)
//
// DESCRIPTION:
// Changes the Transiver transmit power.
// The function checks the ppib.phyTransmitPower to see if it has changed.
// if the parameter is changed the changes will be writen to the radio chip.
// Alowed values of ppib.phyTransmitPower are: OUTPUT_POWER_0DBM, OUTPUT_POWER_N1DBM
// OUTPUT_POWER_N3DBM, OUTPUT_POWER_N5DBM, OUTPUT_POWER_N7DBM, OUTPUT_POWER_N10DBM
// OUTPUT_POWER_N15DBM and OUTPUT_POWER_N25DBM
//
// PARAMETERS:
//-------------------------------------------------------------------------------------------------------
#if MAC_OPT_TRANSMIT_POWER
void msupSetTransmitPower(void)
{
UINT16 cc2420Value;
//If parameter is changed verify and write to chip.
if (oldPhyTransmitPower != ppib.phyTransmitPower)
{
switch(ppib.phyTransmitPower)
{
case OUTPUT_POWER_0DBM:
cc2420Value = CC2420_0DBM;
break;
case OUTPUT_POWER_N1DBM:
cc2420Value = CC2420_N1DBM;
break;
case OUTPUT_POWER_N3DBM:
cc2420Value = CC2420_N3DBM;
break;
case OUTPUT_POWER_N5DBM:
cc2420Value = CC2420_N5DBM;
break;
case OUTPUT_POWER_N7DBM:
cc2420Value = CC2420_N7DBM;
break;
case OUTPUT_POWER_N10DBM:
cc2420Value = CC2420_N10DBM;
break;
case OUTPUT_POWER_N15DBM:
cc2420Value = CC2420_N15DBM;
break;
case OUTPUT_POWER_N25DBM:
cc2420Value = CC2420_N25DBM;
break;
//No valid value reset to old
default:
ppib.phyTransmitPower = oldPhyTransmitPower;
cc2420Value = CC2420_0DBM;
break;
}
oldPhyTransmitPower = ppib.phyTransmitPower;
FASTSPI_SETREG(CC2420_TXCTRL, cc2420Value);
}
}// msupSetTransmitPower
#endif
/*******************************************************************************************************
*******************************************************************************************************
************************** RF CHANNEL SETTINGS **************************
*******************************************************************************************************
*******************************************************************************************************/
//-------------------------------------------------------------------------------------------------------
// BOOL msupChannelValid(UINT8 logicalChannel)
//
// DESCRIPTION:
// Support function for verifying that the logicalChannel is within the valid range for the 2.4 GHz
// channels supported by CC2420, which are channels 11 (2405 MHz) through 26 (2480 MHz).
//
// PARAMETERS:
// UINT8 logicalChannel
// The channel number
//
// RETURN VALUE:
// BOOL
// TRUE for channels >= 11 and <= 26, otherwise FALSE
//-------------------------------------------------------------------------------------------------------
BOOL msupChannelValid(UINT8 logicalChannel) {
return ((logicalChannel >= 11) && (logicalChannel <= 26));
} // msupChannelValid
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -