⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 demozrfdapp.c

📁 zbee potocol 無線通訊測試.平台:microchip 18f452 與 cc2420 zbee 晶片.含coord端與rfd端
💻 C
📖 第 1 页 / 共 3 页
字号:
                while( S2 == 0 && S3 == 0 );

                // Depending which switch is released first, we will bind either
                // switch or lamp.
                //
                // If S2 is released first, we will attempt to bind S2
                // If S3 is released first, we will attempt to bind D1
                // Or else do as per terminal command.
                if ( S2 == 1 && S3 == 0 )
                {
                    // Indicate that we attempting to binding S2 EP
                    D1 = 0;
                    D2 = 1;

                    // Show that we are about to bind S2 EP.
                    ConsolePutROMString(S2BindingMsg);

                    // Wait for S3 to get released
                    while( S3 == 0 );

                    // Perform S2 EP binding - in ZigBee terminology switch is the source
                    // (i.e. sends out the data packets).
                    StartCustomBind(CUSTOM_BIND_SOURCE, EP_S2);

                    // Now wait for respone from coordinator
                    smBinding = SM_BINDING_WAIT;

                    // Clear LEDs to indicate that we are done.
                    D1 = 0;
                    D2 = 0;
                }

                // If S3 is released first, we will attempt to bind D1
                // Or else do as per terminal command.
                else if ( S2 == 0 && S3 == 1 )
                {
                    // Indicate that we are attempting to bind D1 EP
                    D1 = 1;
                    D2 = 0;

                    // Show that we are about to bind D1 EP.
                    ConsolePutROMString(D1BindingMsg);

                    // If this was entered by pressing switches, wait
                    // for both S3 and S2 to get released
                    while( S2 == 0 );

                    // Perform LAMP EP binding - in ZigBee terminology lamp is the destination
                    // (i.e. receives the data packets only)
                    StartCustomBind(CUSTOM_BIND_DEST, EP_D1);

                    // Now wait for respone from coordinator
                    smBinding = SM_BINDING_WAIT;

                    // Clear LEDs to indicate that we are done.
                    D1 = 0;
                    D2 = 0;
                }

                // Clear SW toggled flags as they would be set when user was performing binding
                appFlags.bits.S2Toggled = FALSE;
            }

            // Enable Watchdog to catch lock-ups.
            ENABLE_WDT();

            break;

        case SM_BINDING_WAIT:
            CLRWDT();

            if ( IsCustomBindComplete() )
            {
                ConsolePutROMString(demoBindCompleteMsg);

                // Disable watchdog before returning.
                DISABLE_WDT();

                return;
            }
        }
    }
}


static void InitCustomBind(void)
{
    smBind = SM_BIND_IDLE;

    hCustomBind = APSOpenEP(CUSTOM_BINDER_EP,           // This EP we will use to
                                                        // transfer binding info
                            CUSTOM_BINDER_CLUSTER_ID,   // Cluster ID used to
                                                        // transfer binding info
                            0x0,                        // Destination EP
                                                        // The customer binder
                                                        // on coordinator
                                                        // listens on EP 0
                            TRUE);                      // Direct EP:
                                                        // This is targetted
                                                        // to coordinator only.
}

static BOOL IsLEDBindComplete(void)
{
    switch(ledBind)
    {
    case LED_BIND_IDLE:
        APSSetEP(hCustomBind);

        // To set custom binding info, we are using cluster id of
        // CUSTOM_DEMO_BIND -
        // coordinator must also use this cluster id to match EPs.
        // See CUSTOM_DEMO_BIND in zdo.c
        APSSetClusterID(CUSTOM_BINDER_CLUSTER_ID);

        if ( APSIsPutReady() )
        {
            // For this custom bind transaction, we are using MSG service.
            // Prepare bind information command structure
            bindInfo.cmd = customAction;                // Command code
            memcpy((void*)&bindInfo.nodeAddr,           // Node address
                   (const void*)&macInfo.longAddr,
                   (size_t)(sizeof(bindInfo.nodeAddr)));
            bindInfo.ep = customEP;                     // EP to bind, may be source
                                                        // or dest depending on action
            bindInfo.clusterID = PICDEMZ_LED_CLUSTER_ID;
                                                        // May or may not be used
                                                        // depending on command code


            // As part of MSG APIs, you must declare total length of payload
            // that you intend to transmit.
            APSBeginMSG(sizeof(bindInfo));

            // Now load the entire command structure
            APSPutArray((BYTE*)&bindInfo, sizeof(bindInfo));

            // Send it.
            APSSend();

            // wait for response
            ledBind = LED_BIND_WAIT;

        }
        break;

    case LED_BIND_WAIT:
        if ( APSIsConfirmed() )
        {
            APSRemoveFrame();
            ledBind = LED_BIND_IDLE;

            return TRUE;
        }

        else if ( APSIsTimedOut() )
        {
            APSRemoveFrame();
            ledBind = LED_BIND_IDLE;

            return TRUE;
        }
        break;
    }

    return FALSE;	
}

static BOOL IsCustomBindComplete(void)
{
    switch(smBind)
    {
    case SM_BIND_IDLE:
        APSSetEP(hCustomBind);

        // To set custom binding info, we are using cluster id of
        // CUSTOM_DEMO_BIND -
        // coordinator must also use this cluster id to match EPs.
        // See CUSTOM_DEMO_BIND in zdo.c
        APSSetClusterID(CUSTOM_BINDER_CLUSTER_ID);

        if ( APSIsPutReady() )
        {
            // For this custom bind transaction, we are using MSG service.
            // Prepare bind information command structure
            bindInfo.cmd = customAction;                // Command code
            memcpy((void*)&bindInfo.nodeAddr,           // Node address
                   (const void*)&macInfo.longAddr,
                   (size_t)(sizeof(bindInfo.nodeAddr)));
            bindInfo.ep = customEP;                     // EP to bind, may be source
                                                        // or dest depending on action
            bindInfo.clusterID = PICDEMZ_DEMO_CLUSTER_ID;
                                                        // May or may not be used
                                                        // depending on command code


            // As part of MSG APIs, you must declare total length of payload
            // that you intend to transmit.
            APSBeginMSG(sizeof(bindInfo));

            // Now load the entire command structure
            APSPutArray((BYTE*)&bindInfo, sizeof(bindInfo));

            // Send it.
            APSSend();

            // wait for response
            smBind = SM_BIND_WAIT;

        }
        break;

    case SM_BIND_WAIT:
        if ( APSIsConfirmed() )
        {
            APSRemoveFrame();
            smBind = SM_BIND_IDLE;

            return TRUE;
        }

        else if ( APSIsTimedOut() )
        {
            APSRemoveFrame();
            smBind = SM_BIND_IDLE;

            return TRUE;
        }
        break;
    }

    return FALSE;
}





static BOOL ExecuteMenuChoice(MENU_CMD choice)
{
    LONG_ADDR longAddr;
    BYTE *p;
    char idString[5];
    DWORD_VAL idValue;

    switch(choice)
    {
    case MENU_CMD_SET_ID:
        // Node id is combined with Microchip OUI to create 8-byte MAC address.
        p = &longAddr.v[7];

        // Preset the OUI and three other bytes.
        *p-- = MAC_LONG_ADDR_BYTE7;
        *p-- = MAC_LONG_ADDR_BYTE6;
        *p-- = MAC_LONG_ADDR_BYTE5;
        *p-- = 0x00;
        *p-- = 0x00;
        *p-- = 0x00;

        // Prompt the user.
        ConsolePutROMString(idEntryMsg);
        if ( ConsoleGetString(idString, sizeof(idString)-1) )
        {
            // Convert string to 16-bit integer
            idValue.Val = (WORD)atoi(idString);
            // Make sure that it is a valid number.
            if ( idValue.Val > 0xfffe )
            {
                ConsolePutROMString(invalidValueMsg);

                // Wait for user to acknowledge the message.
                while ( !ConsoleIsGetReady() );
            }
            else
            {

                // Save it locally
                *p-- = idValue.v[1];
                *p = idValue.v[0];

                // Now that we have complete board id, save the info into nonvolatile memory.
                SaveMACLongAddr(&longAddr);

                // Update MAC address info
                memcpy((void*)&macInfo.longAddr, (void*)&longAddr, (size_t)sizeof(macInfo.longAddr));

                // Now ask MAC to set the address
                MACUpdateAddressInfo();


            }
        }

        else
        {
            ConsolePutROMString(invalidValueMsg);
        }

        break;


    case MENU_CMD_JOIN:
        ConsolePutROMString(joinAttemptMsg);

        // Enable Watchdog to catch any lock-ups.
        ENABLE_WDT();

        APLJoin();

        while(1)
        {
            CLRWDT();

            APLTask();

            if ( APLIsJoinComplete() )
            {
                if ( GetLastZError() == ZCODE_NO_ERROR )
                    ConsolePutROMString(associateSuccessMsg);
                else
                    ConsolePutROMString(associateFailMsg);
                break;
            }
        }
        // Disable watchdog before returning.
        DISABLE_WDT();

        break;

    case MENU_CMD_DEMO:
        // TODO: Don't lock up if we haven't joined a network yet
        if(!APLIsIdle())
        {
            ConsolePutROMString(demoBindFailedMsg);
            break;
        }


        StartCustomBind(CUSTOM_BIND_DEMO, 0);   // EP # not used in CUSTOM_BIND_DEMO

        // Enable Watchdog to catch any lock-ups.
        ENABLE_WDT();

        while(1)
        {
            CLRWDT();

            APLTask();

            if ( IsCustomBindComplete() )
            {
                ConsolePutROMString(demoBindCompleteMsg);
                break;
            }
        }
        // Diable watchdog before returning.
        DISABLE_WDT();

        break;



    case MENU_CMD_LEAVE:
        APLLeave();

        // Enable Watchdog to catch any lock-ups.
        ENABLE_WDT();

        while(1)
        {
            CLRWDT();

            APLTask();

            if ( APLIsLeaveComplete() )
            {
                if ( GetLastZError() == ZCODE_NO_ERROR )
                    ConsolePutROMString(leaveSuccessMsg);

                else
                    ConsolePutROMString(leaveFailMsg);

                break;
            }
        }
        // Diable watchdog before returning.
        DISABLE_WDT();

        break;

    case MENU_CMD_NEXT_CHANNEL:
        MACSetNextChannel();
        break;

    case MENU_CMD_UNMODULATED_OP:
        MACTxUnmodulatedCarrier();
        ConsolePutROMString(testTxMsg);

        // Must reset to recover from this command
        while(1);
        break;

    case MENU_CMD_RANDOM_OP:
        MACTxTestPattern();
        ConsolePutROMString(testTxMsg);

        // Must reset to recover from this command
        while(1);
        break;

    case MENU_CMD_QUIT:
        return FALSE;
    }

    return TRUE;
}


static void TestTC77(void)
{
#if !defined(WIN32)
    BYTE v;

    ConsolePutROMString(tc77TestingMsg);

    // Select TC77
    RA2 = 0;

    // To read data from TC77, we need to write dummy data
    SSPBUF = 0x00;

    // Now wait for response from TC77.
    while( !SSPBF );

    // Deselect TC77
    RA2 = 1;

    // Now read the same register to read the response
    v = SSPBUF;

    // For our test purpose, make sure that we read something other than 0x00
    if ( v == 0x00 || v == 0xff )
        ConsolePutROMString(tc77ProblemMsg);

    else
        ConsolePutROMString(tc77TestSuccessMsg);
#endif
}









⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -