📄 wor.c
字号:
INT_ENABLE(INUM_EXTERNAL1, INT_OFF); // Disable external1 interrupts
SET_YLED(LED_OFF); // Turn off yellow LED to indicate unit has stopped.
}// runReceiver
//-------------------------------------------------------------------------------------------------------
// void initReceiver(void)
//
// DESCRIPTION:
// This function configures the Wake-on-Radio specific radio register settings and initializes
// the necessary MCU interrupt given from the radio when a packet has been detected with WOR.
//-------------------------------------------------------------------------------------------------------
void initReceiver(void) {
// Configuring all the WOR related settings
// Enable automatic initial calibration of RCosc.
// Set T_event1 ~ 345 us, enough for XOSC stabilize before starting calibration.
// Enable RC oscillator before starting with WOR (or else it will not wake up).
halSpiWriteReg(CCxxx0_WORCTRL, 0x38); // Not using AUTO_SYNC function.
// Set Event0 timeout = 300 ms (RX polling interval)
// WOR_RES = 0
// T_event0 = 750 / f_xosc * EVENT0 * 2^(5*WOR_RES) = 300 ms // Assuming f_xosc = 26 MHz
// => EVENT0 = 10400 = 0x28A0
halSpiWriteReg(CCxxx0_WOREVT1, 0x28); // High byte Event0 timeout
halSpiWriteReg(CCxxx0_WOREVT0, 0xA0); // Low byte Event0 timeout.
// Setting Rx_timeout < 0.5 %.
// => MCSM2.RX_TIME = 101b
// => Rx_timeout = EVENT0 * 0.1127 = 10400 * 0.1127 = 1.172 ms, i.e. 0.391% RX duty cycle
halSpiWriteReg(CCxxx0_MCSM2, 0x05);
// Enable automatic FS calibration when going from IDLE to RX/TX/FSTXON (starting at EVENT1)
halSpiWriteReg(CCxxx0_MCSM0, 0x18);
// Enable external interrupt when packet is received
// IOCFG2 register = 0x06 => GDO2 pin is asserted when sync word detected/sent, de-asserted at
// end of packet.
// MCU is interrupted by radio on low edge of GDO2, i.e. whenever a packet is received.
ENABLE_GLOBAL_INT(INT_OFF);
SETUP_GDO2_INT(EDGE, LOW); // Enables external interrupt on low edge
INT_SETFLAG(INUM_EXTERNAL1, INT_CLR); // Clears the interrupt flag
ENABLE_GLOBAL_INT(INT_ON);
}// initReceiver
//-------------------------------------------------------------------------------------------------------
// void TIMER1_ISR(void)
//
// DESCRIPTION:
// This interrupt occurs every 1000 us on the transmitter unit. The timer
// registers are reloaded and a packet is sent.
//-------------------------------------------------------------------------------------------------------
void TIMER1_ISR(void) interrupt INUM_TIMER1 {
ENABLE_GLOBAL_INT(INT_OFF);
// Reload timer1 registers to overflow again after same time interval
TH1 = 0xE8;
TL1 = 0x90; // overflows every 1000 us (see initialization of the timer for calculation)
// Is the burst transmission done?
if (burstCount < BURST_LENGTH) { // Continue to send packets
// Send the packet
halRfSendPacket(txBuffer, sizeof(txBuffer));
burstCount++;
} else { // End of burst
INT_ENABLE(INUM_TIMER1, INT_OFF); // Disable the timer
SET_BLED(LED_OFF); // Turn off blue LED. Unit is no longer transmitting.
burstDone = TRUE;
}
ENABLE_GLOBAL_INT(INT_ON);
}// TIMER1_ISR
//-------------------------------------------------------------------------------------------------------
// void EXTERNAL1_ISR(void)
//
// DESCRIPTION:
// This interrupt service routine occurs whenever CCxx00 detects a packet while in Wake-on-Radio
//-------------------------------------------------------------------------------------------------------
void EXTERNAL_ISR(void) interrupt INUM_EXTERNAL1 {
BYTE packetLength;
P_LED4 = ~P_LED4; // Toggle blue LED to indicate MCU is interrupted by Wake-on-Radio.
// The contents of TX FIFO and RX FIFO are cleared when entering SLEEP state (WOR),
// so the RX FIFO does only contain the new packet.
// The CRC_AUTOFLUSH function is enabled: automatically flushing the RX FIFO if received packet
// contains CRC error
// Fixed packet length enabled (1 byte): packets longer than 1 byte is automatically filtered out
// Check if we got any packets in the RX FIFO
// This status register is safe to read since it will not be updated after
// the packet has been received (See the CC1100 or 2500 Errata Note)
packetLength = halSpiReadStatus(CCxxx0_RXBYTES) & RX_BYTES; // Only 7 LSB
if (packetLength != 0) { // Packet received OK.
// Read out of RX FIFO into rxBuffer array..
rxBuffer[0] = halSpiReadReg(CCxxx0_RXFIFO);
// Parsing received data byte into correct char for proper LCD presentation
joystickPosition = rxBuffer[0];
switch(joystickPosition) {
default: // Other received byte values are parsed as 'C' for ease
case JOYSTICK_CENTER:
asciiJoystickPosition[10] = 'C';
break;
case JOYSTICK_LEFT:
asciiJoystickPosition[10] = 'L';
break;
case JOYSTICK_RIGHT:
asciiJoystickPosition[10] = 'R';
break;
case JOYSTICK_UP:
asciiJoystickPosition[10] = 'U';
break;
case JOYSTICK_DOWN:
asciiJoystickPosition[10] = 'D';
break;
case JOYSTICK_PUSHED:
asciiJoystickPosition[10] = 'P';
break;
}
packetsReceived++;
updateLcd = TRUE; // Set flag for update of LCD.
P_LED1 = ~P_LED1; // Toggle green LED for packet received.
} else // No packet in RX FIFO. Invalid packet detection.
P_LED2 = ~P_LED2; // Toggle red LED for error.
// After a wake-up, the radio should be in IDLE and one have to manually put the radio back
// to sleep/WOR.
halSpiStrobe(CCxxx0_SWOR);
}// EXTERNAL_ISR
//-------------------------------------------------------------------------------------------------------
// void showMenu(void)
//
// DESCRIPTION:
// Function to show menu. Select mode by moving joystick left/right.
// Press S1 button to confirm the choice.
//-------------------------------------------------------------------------------------------------------
void showMenu(void) {
mode = MODE_NOT_SET;
ebLcdUpdate("Wor.c", "<- Set Mode ->");
// Select Tx or Rx mode by moving the joystick left or right
do {
halWait(250);
// Get current position of joystick
joystickPosition = ebGetJoystickPosition();
if (prevJoystickPosition != joystickPosition) {
prevJoystickPosition = joystickPosition;
parseMenu(joystickPosition);
}
} while (!ebButtonPushed() || (mode == MODE_NOT_SET));
}// showMenu
//-------------------------------------------------------------------------------------------------------
// void parseMenu(UINT8 joystickPosition)
//
// DESCRIPTION:
// This function selects mode and updates the LCD display based on the joystick position
//
// ARGUMENTS:
// UINT8 joystickPosition
// The position of the joystick
//-------------------------------------------------------------------------------------------------------
void parseMenu(UINT8 joystickPosition) {
switch (joystickPosition) {
case JOYSTICK_LEFT:
if (mode == RX || mode == MODE_NOT_SET) {
mode = TX;
ebLcdUpdate("Mode:", "Tx/Burst");
}
break;
case JOYSTICK_RIGHT:
if (mode == TX || mode == MODE_NOT_SET) {
mode = RX;
ebLcdUpdate("Mode:", "Rx/Wake-on-Radio");
}
break;
default: // Other joystick movements: do nothing
break;
}
}// parseMenu
//-------------------------------------------------------------------------------------------------------
// void intToAscii(UINT32 value)
//
// DESCRIPTION:
// Takes a 32 bits integer as input and converts it to ascii. Puts the result in the global
// variable asciiString[]
//
// ARGUMENTS:
// UINT32 value
// The value to be converted
//-------------------------------------------------------------------------------------------------------
void intToAscii(UINT32 value) {
UINT8 i;
UINT8 j = 0;
UINT8 digit_start = 0;
UINT16 digit = 0;
UINT32 denom = 1000000000;
if (value == 0) {
asciiString[0] = '0';
asciiString[1] = NULL;
} else {
for(i = 10; i > 0; i--) {
digit = value / denom;
if((digit_start == 1) || (digit != 0)) {
digit_start = 1;
value %= denom;
asciiString[j++] = (digit + '0');
}
denom /= 10;
}
asciiString[j++] = NULL;
}
}// intToAscii
/******************************************************************************************************
* Revision history: *
*
* $Log: Wor.c,v $
* Revision 1.6 2006/04/25 15:07:51 a0190596
* added POWER_UP_RESET_CCxxx0()
* tEVENT1 changed from 1.38 ms to 345 us.
* removed manual calibration at start-up
* Comments added
*
* Revision 1.5 2006/03/31 13:30:23 a0190596
* POWER_UP_RESET_CCxxx0() removed
*
* Revision 1.4 2006/03/17 10:37:00 a0190596
* New file structure might have caused variables, defines, and functions to move.
* Comments are added.
*
* Revision 1.3 2005/11/09 10:01:47 sna
* no message
*
* Revision 1.2 2005/10/25 12:23:20 sna
* Register settings moved to separate *.h file
*
* Revision 1.1 2005/10/06 12:13:52 sna
* Initial version in CVS.
*
*
*
******************************************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -