?? onboard_xbee.c
字號(hào):
/***************************************************************
* Modified version of OnBoard.c for XBee platform.
*
****************************************************************/
#include "ZComDef.h"
#include "OnBoard_XBee.h"
#include "OnBoard.h"
#include "Osal.h"
#include "nwk.h"
#ifdef TARGET_XBEE
#define INTERRUPT
#define KBIE 0x02
#define KBACK 0x04
#define TurnOnLED1 PTCD &= ~0x20;
#define TurnOnLED2 PTDD &= ~0x02;
#define TurnOnLED3 PTBD &= ~0x10;
#define TurnOnLED4 PTBD &= ~0x20;
#define TurnOffLED1 PTCD |= 0x20;
#define TurnOffLED2 PTDD |= 0x02;
#define TurnOffLED3 PTBD |= 0x10;
#define TurnOffLED4 PTBD |= 0x20;
#define TurnOnBuzzer
#define TurnOffBuzzer
#define TurnOnBigLight
#define TurnOffBigLight
/*********************************************************************
* EXTERNAL VARIABLES
*/
// Used to calculate the used stack
extern char __SEG_START_SSTACK[];
extern char __SEG_END_SSTACK[];
#define ONBOARD_STACK_START __SEG_START_SSTACK
#define ONBOARD_STACK_END __SEG_END_SSTACK
#define STACK_INIT_VALUE 0xA5
/*********************************************************************
* EXTERNAL FUNCTIONS
*/
extern void nwk_set_poll_evt( void );
extern void nwk_sendDummyData( void );
/*********************************************************************
* LOCAL VARIABLES
*/
byte _SavedKeys;
/* OnBoard task id */
byte OnBoard_TaskID;
// LED state at last set/clr/blink update
static byte ledState;
#if defined ( BLINK_LEDS )
// LED Status Table
static ledSts ledStatusTable[NBR_OF_LEDS];
#endif
// Registered keys task ID, initialized to NOT USED.
byte registeredKeysTaskID = 0xFF;
/*********************************************************************
* LOCAL FUNCTIONS
*/
void KBIntSetup( byte state_on );
void KBInit( void );
void runThroughLEDs( void );
void LCDWaitLong( int w );
void UpdateLeds( void );
void LedOnOff( byte mask, byte on );
/*********************************************************************
* @fn Low Voltage Detect Interrupt
* @brief This ISR is called when power is low.
* @param
* @return
*/
void INTERRUPT LowVoltageDetectISR( void )
{
// PUT YOUR CODE HERE to handle low power indicator
// This code will toggle LEDs forever until power is gone
osal_int_disable( INTS_ALL ); // Might not be needed since this is ISR.
for ( ;; )
runThroughLEDs();
}
/*********************************************************************
* EVENT PROCESSING FUNCTIONS
*
* These functions perform event-based (interrupt & timer) processing
* of board level I/O, such as, keypress inputs and LED displays.
*/
/*********************************************************************
* @fn OnBoard_TaskInit
*
* @brief
*
* OnBoard Monitoring Task Initialization. This function is put into
* the task table.
*
* @param byte task_id - task ID of the MT Task
*
* @return void
*/
void OnBoard_TaskInit( byte task_id )
{
OnBoard_TaskID = task_id;
#if defined ( KB_INT )
// This is to avoid interrupts until normal processing
osal_set_event( task_id, KEYPRESS_SETUP_INT_EVT );
#else
// Start the key polling
osal_set_event( task_id, KEYPRESS_POLL_EVT );
#endif
} /* OnBoard_TaskInit() */
/*********************************************************************
* @fn OnBoard_ProcessEvent
*
* @brief
*
* OnBoard Task Event Processor. This task is put into the
* task table.
*
* @param byte task_id - task ID of the MT Task
* @param UINT16 events - event(s) for the MT Task
*
* @return void
*/
void OnBoard_ProcessEvent( byte task_id, UINT16 events )
{
#if defined ( BLINK_LEDS )
// Process LED blink event
if ( events & LED_BLINK_EVT )
{
UpdateLeds(); // Update LED status
}
#endif
#if defined ( KB_INT )
if ( events & KEYPRESS_SETUP_INT_EVT )
KBIntSetup( true );
if ( events & KEYPRESS_DEBOUNCE_EVT )
osal_start_timerEx( OnBoard_TaskID, KEYPRESS_POLL_EVT, 50 );
#endif
if ( events & KEYPRESS_POLL_EVT )
{
gb60_read_keys();
#if defined ( KB_INT )
osal_set_event( task_id, KEYPRESS_SETUP_INT_EVT );
#else
osal_start_timerEx( OnBoard_TaskID, KEYPRESS_POLL_EVT, 100 );
#endif
}
#if defined ( FS_RX_FLOWCONTROL )
if ( events & RX1_FLOW_TIMER_EVT )
{
SERIAL_PORT1_RX_RTS_DISABLE;
SERIAL_PORT1_RX_RTS_ENABLE;
osal_start_timerEx( OnBoard_TaskID, RX1_FLOW_TIMER_EVT, 1000 );
}
if ( events & RX2_FLOW_TIMER_EVT )
{
SERIAL_PORT2_RX_RTS_DISABLE;
SERIAL_PORT2_RX_RTS_ENABLE;
osal_start_timerEx( OnBoard_TaskID, RX2_FLOW_TIMER_EVT, 1000 );
}
#endif // FS_RX_FLOWCONTROL
} /* OnBoard_ProcessEvent() */
/*********************************************************************
* @fn Initialize the Board Peripherals
* @brief
* @param
* @return
*/
void InitBoard( void )
{
// Set the direction for the LED Port
PTCDD |= 0x20;
PTDDD |= 0x02;
PTBDD |= 0x30;
// Turn the LEDs off
SetLed( LED_ALL, LED_OFF );
// Initialize the Keyboard Interrupt Module
KBInit();
// Initialize the LCD Module
// Initialize the ports for the Serial RTS (Flow control)
#if defined ( FS_RX_FLOWCONTROL )
#if defined ( SERIAL_PORT1 )
SERIAL_PORT1_RX_RTS_PORTENABLE;
SERIAL_PORT1_RX_RTS_PORTDIRECTION;
SERIAL_PORT1_RX_RTS_ENABLE;
#if defined ( FS_RX_FLOWCONTROL )
osal_start_timerEx( OnBoard_TaskID, RX1_FLOW_TIMER_EVT, 1000 );
#endif
#endif
#if defined ( SERIAL_PORT2 )
SERIAL_PORT2_RX_RTS_PORTENABLE;
SERIAL_PORT2_RX_RTS_PORTDIRECTION;
SERIAL_PORT2_RX_RTS_ENABLE;
#if defined ( FS_RX_FLOWCONTROL )
osal_start_timerEx( OnBoard_TaskID, RX2_FLOW_TIMER_EVT, 1000 );
#endif
#endif
#endif // FS_RX_FLOWCONTROL
}
/*********************************************************************
* LED Support
*/
/*********************************************************************
* @fn SetLed
*
* @brief Turn on, turn off, blink User LEDs
* @param mask - LED bit mask
* @param mode - LED_ON,LED_OFF,LED_BLINK
*
* @return current LED status, 1=on
*/
byte SetLed( byte mask, byte mode )
{
#if defined ( BLINK_LEDS )
byte led;
ledSts *sts;
if ( mode == LED_BLINK )
// Default blink, 1 time, D% duty cycle
BlinkLed( mask, 1, LED_DUTY_CYCLE, LED_BLINK_TIME );
else if ( mode == LED_FLASH )
// Default flash, N times, D% duty cycle
BlinkLed( mask, LED_FLASH_COUNT, LED_DUTY_CYCLE, LED_BLINK_TIME );
else
{
// ON, OFF, or TOGGLE
led = LED1;
mask &= LED_ALL;
sts = ledStatusTable;
while ( mask )
{
if ( mask & led )
{
if ( mode != LED_TOGGLE )
sts->mode = mode; // ON or OFF
else
sts->mode ^= LED_ON; // Toggle
LedOnOff( led, sts->mode );
mask ^= led;
}
led <<= 1;
sts++;
}
}
#else
LedOnOff( mask, mode );
#endif
return ( ledState );
}
/*********************************************************************
* @fn BlinkLed
*
* @brief Blink specified LEDs
* @param mask - LED bit mask
* @param nbr - number of time, 0=contiunuous
* @param on - percentage of cycle that's on
* @param time - milliseconds for one on/off cycle
* @return none
*/
void BlinkLed( byte mask, byte nbr, byte on, uint16 time )
{
#if defined ( BLINK_LEDS )
byte led;
ledSts *sts;
if ( mask && on && time )
{
if ( on < 100 )
{
led = LED1;
mask &= LED_ALL;
sts = ledStatusTable;
while ( mask )
{
if ( mask & led )
{
sts->mode = LED_OFF; // Stop previous blink
sts->time = time; // Time for one on/off cycle
sts->onPct = on; // % of cycle LED is on
sts->todo = nbr; // Number of blink cycles
if ( !nbr ) sts->mode |= LED_FLASH; // Continuous
sts->next = osal_GetSystemClock(); // Start now
sts->mode |= LED_BLINK; // Enable blinking
mask ^= led;
}
led <<= 1;
sts++;
}
osal_set_event( OnBoard_TaskID, LED_BLINK_EVT );
}
else
SetLed( mask, LED_ON ); // >= 100%, turn on
}
else
SetLed( mask, LED_OFF ); // No on time, turn off
#else
on = (mask & ledState) ? LED_OFF : LED_ON;
LedOnOff( mask, on ); // Toggle
#endif
}
#if defined ( BLINK_LEDS )
/*********************************************************************
* @fn UpdateLeds
*
* @brief Updates blinking/flashing LEDs
* @param none
* @return none
*/
void UpdateLeds( void )
{
byte led;
byte pct;
byte mask;
ledSts *sts;
uint32 time;
uint16 next;
uint16 wait;
next = 0;
led = LED1;
mask = LED_ALL;
sts = ledStatusTable;
while ( mask )
{
if ( mask & led )
{
if ( sts->mode & LED_BLINK )
{
time = osal_GetSystemClock();
if ( time >= sts->next )
{
if (sts->mode & LED_ON )
{
pct = 100 - sts->onPct; // Percentage of cycle for off
sts->mode &= ~LED_ON; // Say it's not on
LedOnOff( led, LED_OFF ); // Turn it off
if ( !(sts->mode & LED_FLASH) )
{
sts->todo--; // Not continuous, reduce count
if ( !sts->todo )
sts->mode ^= LED_BLINK; // No more blinks
}
}
else
{
pct = sts->onPct; // Percentage of cycle for on
sts->mode |= LED_ON; // Say it's on
LedOnOff( led, LED_ON ); // Turn it on
}
if ( sts->mode & LED_BLINK )
{
wait = (((uint32)pct * (uint32)sts->time) / 100);
sts->next = time + wait;
}
else
wait = 0;
}
else
wait = sts->next - time; // Time left
if ( !next || ( wait < next ) )
next = wait;
}
mask ^= led;
}
led <<= 1;
sts++;
}
if ( next )
osal_start_timer( LED_BLINK_EVT, next ); // Come back later
}
#endif // BLINK_LEDS
/*********************************************************************
* @fn LedOnOff
*
* @brief Turns specified LED on or off
* @param mask - LED bit mask
* @param on - LED_ON,LED_OFF,LED_BLINK
* @return none
*/
void LedOnOff( byte mask, byte on )
{
if ( mask & LED1 )
{
if ( on == LED_ON ){
TurnOnLED1;
}
else{
TurnOffLED1;
}
}
if ( mask & LED2 )
{
if ( on == LED_ON ){
TurnOnLED2;
}
else{
TurnOffLED2;
}
}
if ( mask & LED3 )
{
if ( on == LED_ON ){
TurnOnLED3;
}
else{
TurnOffLED3;
}
}
if ( mask & LED4 )
{
if ( on == LED_ON ){
TurnOnLED4;
}
else{
TurnOffLED4;
}
}
// Remember current state
if ( on )
ledState |= mask;
else
ledState &= ~mask;
}
void runThroughLEDs( void )
{
byte x;
for ( x = LED1; x <= LED4; x<<=1 )
{
SetLed( x, LED_ON );
LCDWaitLong( 50 );
SetLed( x, LED_OFF );
LCDWaitLong( 50 );
}
}
void BigLight_On( void )
{
TurnOnBigLight;
}
void BigLight_Off( void )
{
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -