📄 coodinator.c
字号:
//-----------------------------------------------------------------------------
//
// file: coodinator.c
// description: demonstration program, coordinator
// Version: 1.0
//
//-------------------------------------------------------------------------------
#pragma SFR
#pragma NOP
#pragma STOP
#pragma HALT
#pragma DI
#pragma EI
///////////////////////////////////////////////////////////////////////////////
// Include
#include "../include/config.h"
#include <stdio.h>
#include <mac_headers.h>
#include "../include/780547.h"
#include "../include/mac_def.h"
///////////////////////////////////////////////////////////////////////////////
// prototype
int UART_StartTx(char *buffer);
///////////////////////////////////////////////////////////////////////////////
// extern
extern volatile WORD g_myShortAddr;
extern WORD g_PanId; // The PAN ID
extern ADDRESS g_coordAddr; // Address of the Coordinator
extern BYTE pRxBuffer[PACKET_LENGTH];
extern BOOL g_KeyPush;
extern BYTE g_KeyMap;
extern BYTE g_Device_Num; // The number of the Device
extern WORD g_LED_brink_time; // LED brink time
extern BOOL g_RcvPacket;
extern BOOL g_AssocIndication; // for Coordinator
//-------------------------------------------------------------------
// Init Coordinator (Data receiver side)
void InitCoordinator() // __fn__
{
MAC_ENUM status;
BYTE setAttributeValue;
BYTE i,j;
LED_ON(LED2); // LED2 is ON when Coodinator.
/////////////////////////////////////////////////////////////////
// Initialize variable
g_coordAddr.Short = COORD_SHORT;
// Setting of PAN ID
g_PanId = PANID;
// modify PIB attributes
mlmeSetRequest(MAC_SHORT_ADDRESS, (BYTE*) &g_coordAddr); // Set the MAC Short Address
setAttributeValue = TRUE;
mlmeSetRequest(MAC_RX_ON_WHEN_IDLE, &setAttributeValue); // Receive OK, while Idle
mlmeSetRequest(MAC_ASSOCIATION_PERMIT, &setAttributeValue); // permit Associate
mlmeSetRequest(MAC_PAN_ID, &g_PanId);
setAttributeValue = FALSE;
mlmeSetRequest(MAC_SECURITY_MODE, &setAttributeValue);
WDTE = 0xAC; // Reset Watch Dog Timer
// Flush the TX FIFO
DISABLE_GLOBAL_INT();
FASTSPI_STROBE(CC2420_SFLUSHTX);
ENABLE_GLOBAL_INT();
mrxResetRxEngine();
// Start as a beacon-enabled PAN
for(i=0; i<2; i++) { // Re-try (max 2 times) if error
status = mlmeStartRequest(
g_PanId, // The new PAN identifier
CHANNEL_NO, // logicalChannel
BEACON_ORDER, // beaconOrder
SUPERFRAME_ORDER, // superframeOrder
TRUE, // panCoordinator
FALSE, // batteryLifeExtension
FALSE, // coordRealignment
FALSE); // securityEnable
WDTE = 0xAC; // Reset Watch Dog Timer
if (status == SUCCESS) {
UART_StartTx("\r\n-- mlmeStartRequest SUCCESS, then START PAN.\r\n>");
return;
}
}
LED_ON(LED3);// LED3 ON when failure
UART_StartTx("\r\n-- mlmeStartRequest FAILED, then you need RESET.\r\n>");
while(1); // forever
}
//-------------------------------------------------------------------
// Coordinator Proc (Data receiver side)
void CoordinatorProc() // __fn__
{
BYTE i,j;
char s[64];
///UART_StartTx("\r\n*C21*\r\n>");
//-------------------------------------------------
// Associate Indication ?
if (g_AssocIndication) {
sprintf(s, "\r\n-- mlmeAssociateIndication from DEV:%d\r\n>", g_Device_Num);
UART_StartTx(s);
g_AssocIndication = FALSE;
}
///UART_StartTx("\r\n*C22*\r\n>");
//-------------------------------------------------
// Receive the packet from Transmitting Side
if (g_RcvPacket) {
///UART_StartTx("\r\n*C23*\r\n>");
// SW4 pushed
if (pRxBuffer[0] == TRUE) {
sprintf(s, "\r\n-- mcpsDataIndication SW4 from DEV:%d\r\n>", g_LED_brink_time);
UART_StartTx(s);
for(i=0;i<g_LED_brink_time;i++) {
LED_ON(LED2);
for(j=0; j< 50; j++) { // 500ms
halWait(1000);
}
LED_OFF(LED2);
for(j=0; j<100; j++) { // 1000ms
halWait(1000);
}
}
}
// SW3 pushed
if (pRxBuffer[1]==TRUE) {
sprintf(s, "\r\n-- mcpsDataIndication SW3 from DEV:%d\r\n>", g_LED_brink_time);
UART_StartTx(s);
for(i=0; i<g_LED_brink_time; i++) {
LED_ON(LED3);
for(j=0; j< 50; j++){ // 500ms
halWait(1000);
}
LED_OFF(LED3);
for(j=0; j<100; j++){ // 1000ms
halWait(1000);
}
}
}
g_RcvPacket = FALSE;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -