📄 mac_app.c
字号:
//-----------------------------------------------------------------------------
//
// file: mac_app.c
// description: demonstration program, main funcion
// Version: 1.0
//
//-------------------------------------------------------------------------------
#pragma SFR
#pragma NOP
#pragma STOP
#pragma HALT
#pragma DI
#pragma EI
///#pragma section @@DATA app at 0xEB00///
/* interrupt */
#pragma interrupt INTKR KeyRet_Int
///////////////////////////////////////////////////////////////////////////////
// Include
#include "../include/config.h"
#include <mac_headers.h>
#include "../include/780547.h"
#include "../include/mac_def.h"
///////////////////////////////////////////////////////////////////////////////
// constant
QWORD macaddress = {0x22958919,0x00000047};
BYTE *g_MacAddress = (BYTE *)&macaddress;
UINT8 spiCnt ;///
extern __sreg INT32 volatile test;////
extern char str1[4];///
extern char str2[32][4];///
extern INT8 volatile COMA;
extern INT8 volatile INTP;
///////////////////////////////////////////////////////////////////////////////
// prototype
void Initialize_Ports();
void InitEndDevice();
void EndDevProc();
void InitCoordinator();
void CoordinatorProc();
void UART_SetSpeed(unsigned short speed);
int UART_StartTx(char *buffer);
int UART_StartRx();
extern BYTE g_RxBuffer[COMM_BUFFER_SIZE];
extern BYTE g_RxCount;
extern BOOL g_RxLine;
//-------------------------------------------------------------------------------------------------------
// global variable
BYTE pTxBuffer[PACKET_LENGTH];
BYTE pRxBuffer[PACKET_LENGTH];
//-------------------------------------------------------------------------------------------------------
// Addresses
// The short address given to
volatile WORD g_myShortAddr;
// common
WORD g_PanId; // The PAN ID
ADDRESS g_coordAddr; // Address of the Coordinator
// for end device
BOOL g_KeyPush = FALSE;
BYTE g_KeyMap;
BOOL g_DataConfirm = FALSE;
MAC_ENUM g_DataStatus;
// for Coodinator
BOOL g_RcvPacket = FALSE;
BYTE g_Device_Num = 0; // The number of the Device
WORD g_LED_brink_time = 0; // LED brink time
BOOL g_AssocIndication = FALSE; // for Coordinator
//-------------------------------------------------------------------
// Hardware Initialize
void hdwinit(void) // __fn__
{
UINT8* ucAdrPtr;
UINT16 uiEndIXRAM;
// Memory Configuration
IMS = 0xCC; // 128KB ROM, 1KB internal high speed RAM
IXS = 0x00; // 6KB expansion RAM
//-----------------------------------------------------
// clock setting
OSCCTL = 0x41; // P121,P122傪X1,X2偵丅10MHz<Fxh
///OSCCTL = 0x40; // 8
MOC = 0; // X1敪怳夞楬摦嶌
while (OSTC.0 == 0) // 敪怳埨掕帪娫懸偪
;
MCM = 7; // X1摦嶌愝掕
PCC = 0; // fcpu = fxp
RCM = 0x01; // 崅懍Ring-OSC掆巭
//-----------------------------------------------------
// Init IXRAM
ucAdrPtr = (UINT8*) 0xE000;
uiEndIXRAM = 0xF7FF;
while( ucAdrPtr < (UINT8*) uiEndIXRAM){
*ucAdrPtr++ = 0;
}
//-----------------------------------------------------
// Initialize Ports(for LED,Key,and so on)
Initialize_Ports();
//-------------------------------------------
// UART
UART_SetSpeed(COMM_BAUD_RATE);
//-----------------------------------------------------
// Setting of the state of the interrupt
CLEAR_FIFOP_MASK();
CLEAR_UART_MASK();
///ENABLE_T3_COMPA_INT();
/*
PPR1=0;///
TMPR001=0;///
TMPR000=1;
TMPR010=1;
TMPR011=1;
*/
}
//-------------------------------------------------------------------
// Main Function
void main(void)
{
BYTE *ptrTemp;
BYTE i,j;
BOOL bEndDevice = FALSE;
BYTE chngAddr = 0;
/*
TIMER3_INIT( 0x00, BM(7)|BM(5), 0x00);///BM(7)|BM(5),
TIMER3_SET_COMPA_VALUE(MAC_TIMER_OVERFLOW_VALUE);///
TIMER3_START(TIMER3_CLK);
ENABLE_T3_COMPA_INT();///
halWait(12000);///
sprintf(str, "\r\n%ld\r\n", test);///
UART_StartTx(str);///
*/
//-------------------------------------------
// Judge of Mode
for(i=0; i<3; i++) {
chngAddr = (SW1_6 == 0 ? 4 : 0) + (SW1_7 == 0 ? 2 : 0) + (SW1_8 == 0 ? 1 : 0);
if (chngAddr != 0) bEndDevice = TRUE;
NOP();
}
// Set Extension Address
if (bEndDevice) {
// Display Version
///UART_StartTx("\r\n*** NEC 78K0/KF2+/CC2420 MAC test App. (as END-DEVICE) ***\r\n>");
///UART_StartTx("\r\nE-D\r\n>");
// Device Case
macaddress.ldword += chngAddr;
} else {
// Display Version
UART_StartTx("\r\n*** NEC 78K0/KF2+/CC2420 MAC test App. (as COORDINATOR) ***\r\n>");
// Coordinatior Case
macaddress.ldword += 0x10;
}
//-------------------------------------------
// MAC layer Initialize
// Read MAC address
ptrTemp= (BYTE*) &aExtendedAddress.ldword;
halReadAddress(ptrTemp);
// Initialize MAC
MAC_INIT();
// Power up
mpmSetRequest(MPM_CC2420_ON);
while (mpmGetState() != MPM_CC2420_ON);
///UART_StartTx("\r\M1\r\n>");
// Reset MAC
mlmeResetRequest(TRUE);
/////////////////////////////////////////////////////////////////
// enable interrupt.
EI();
//-------------------------------------------
// LED control for seeing selection of Mode
if (bEndDevice) {
///UART_StartTx("\r\nE\r\n>");
InitEndDevice(); // End Device
} else {
InitCoordinator(); // Coordinator, (Receiving Side)
}
WDTE = 0xAC; // Reset Watch Dog Timer
//-------------------------------------------
// LED OFF
for(i=0; i<5; i++) {
HALT();
}
LED_OFF(LED2);
LED_OFF(LED3);
LED_OFF(LED4);
CLEAR_KEY_MASK();
KRIF = FALSE;
UART_StartTx("\r\n1\r\n>");
/////////////////////////////////////////////
// Main Loop
while (TRUE) {
WDTE = 0xAC; // Reset Watch Dog Timer
if (bEndDevice) {
EndDevProc();
} else {
CoordinatorProc();
}
WDTE = 0xAC; // Reset Watch Dog Timer
///////////////////////////////////////////////////
// Do HALT to save the POWER.
HALT();
} // while
}
//-------------------------------------------------------------------
// Key interrupt Process
__interrupt void KeyRet_Int() // __fn__
{
DI();
// Indicate of the Key push
if (!g_KeyPush) {
g_KeyPush = TRUE;
g_KeyMap = P7;
g_KeyMap ^= 0xff; // turn out because of low active
}
EI();
}
//-------------------------------------------------------------------
// Setting of I/O port
void Initialize_Ports() // __fn__
{
//-----------------------------------------------------
// P0
P0 = 0x50;
PM0 = P0_DIR;
PU0 = P0_PULLUP;
//-----------------------------------------------------
// P1
PM1 = P1_DIR;
(P1.3) = 1; // for UART6 output
//-----------------------------------------------------
// P2
// PM2 = P2_DIR;
//-----------------------------------------------------
// P3
PM3 = P3_DIR;
PU3 = P3_PULLUP;
//-----------------------------------------------------
// P4
LED_OFF(LED2);
LED_OFF(LED3);
LED_OFF(LED4);
PM4 = P4_DIR;
(P4.3) = 1; // for CSn output
//-----------------------------------------------------
// P5
P5 = 0x01; // VREG_EN = H, RESET_N = L
PM5 = P5_DIR;
PU5 = P5_PULLUP;
//-----------------------------------------------------
// P6
PM6 = P6_DIR;
PU6 = P6_PULLUP;
//-----------------------------------------------------
// P7
PM7 = P7_DIR;
PU7 = P7_PUP;
KRIF = 0; // clear key interrupt
KRM = SW_DETECT;
//-----------------------------------------------------
// P14
P14 = 0x00;
PM14 = P14_DIR;
PU14 = P14_PULLUP;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -