This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

UART corresponded Ports on Smartrf05EB and SmartRF05BB boards

Other Parts Discussed in Thread: CC2530, CC2430, CC2420, Z-STACK, TIMAC

Hi Folks, 

I was wondering if someone help me with how to connect the external source to UART corresponded Ports on Smartrf05EB and SmartRF05BB boards ?

what are the exact ports numbers related to UART on these two modules?

Thank you 

  • You can refer to CC2530EM schematic and smartrf05eb schematic in SmartRF05EB User's Guide.

  • Thanks for the help , but I couldn't find it :( would you please help me with having it with more clarification please ...
    Thank you
  • If you also refer to Table 7-1 in CC2530 user guide, CC2530 UART RX P0.2 which is mapped to P18 Pin9 on SmartRF05EB  and UART TX P0.3 which is mapped to P18 Pin11 on SmartRF05EB.

  • Thank you for your help and consideration ,

    Do you have same information for Smartrf05BB as well?

    Thank you
  • Hi Chen ,

    I was wondering if you can help me with a code that I already tried it and help me with updating it for my application

    Thanks
  • I don't get it. Can you elaborate what your problem is?
  • I wrote a code for communicating two board , but not sure it is correct or not , also I need both board to get the output and could send and recive data , could you please check it for me ? If you may let me have your email

  • Please show me how you do it on this forum and I would try to help.
  • Hi Again ,

    I wrote following code for coordinator , which I think it is wrong ,
    The node will receive data from outsource ( 2 wired data bus ) and transmit it to Coordinator
    The coordinator should receive the same Data from Node and transmit it to the outsource ( 2 wired data bus )

    would you please look at this and let me know which changes should apply

    thanks

    /**************************************************************************************************
    Filename: msa.c (coordinator)
    Revised: $Date: 2012-10-17 10:13:54 -0700 (Wed, 17 Oct 2012) $
    Revision: $Revision: 31845 $

    Description: This file contains the sample application that can be use to test
    the functionality of the MAC, HAL and low level.


    Copyright 2006-2012 Texas Instruments Incorporated. All rights reserved.

    IMPORTANT: Your use of this Software is limited to those specific rights
    granted under the terms of a software license agreement between the user
    who downloaded the software, his/her employer (which must be your employer)
    and Texas Instruments Incorporated (the "License"). You may not use this
    Software unless you agree to abide by the terms of the License. The License
    limits your use, and you acknowledge, that the Software may not be modified,
    copied or distributed unless embedded on a Texas Instruments microcontroller
    or used solely and exclusively in conjunction with a Texas Instruments radio
    frequency transceiver, which is integrated into your product. Other than for
    the foregoing purpose, you may not use, reproduce, copy, prepare derivative
    works of, modify, distribute, perform, display or sell this Software and/or
    its documentation for any purpose.

    YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
    PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
    INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
    NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
    TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
    NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
    LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
    INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
    OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
    OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
    (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.

    Should you have any questions regarding your right to use this Software,
    contact Texas Instruments Incorporated at www.TI.com.
    **************************************************************************************************/

    /**************************************************************************************************

    Description:

    KEY UP (or S1)
    - Non Beacon
    - First board in the network will be setup as the coordinator
    - Any board after the first one will be setup as the device
    |
    KEY LEFT | KEY RIGHT(or S2)
    ----+----- - Start transmitting
    |
    |
    KEY DOWN



    **************************************************************************************************/


    /**************************************************************************************************
    * Includes
    **************************************************************************************************/
    //#define Debug_msa
    #ifdef Debug_msa
    #define Debug_msa_1
    #define Debug_msa_2
    #endif

    //general includes
    #include "string.h"
    /* Hal Driver includes */
    #include "hal_types.h"
    #include "hal_key.h"
    #include "hal_timer.h"
    #include "hal_drivers.h"
    #include "hal_led.h"
    #include "hal_adc.h"
    #include "hal_lcd.h"
    #include "hal_board_cfg.h"
    #include "hal_assert.h"
    #include "hal_uart.h"

    /* OS includes */
    #include "OSAL.h"
    #include "OSAL_Tasks.h"
    #include "OSAL_PwrMgr.h"

    /* Application Includes */
    #include "OnBoard.h"

    /* For Random numbers */
    #include "mac_radio_defs.h"

    /* MAC Application Interface */
    #include "mac_api.h"
    #include "mac_main.h"

    #ifdef MAC_SECURITY
    #include "mac_spec.h"

    /* MAC Sceurity */
    #include "mac_security_pib.h"
    #include "mac_security.h"
    #endif /* MAC_SECURITY */

    /* Application */
    #include "msa.h"

    /**************************************************************************************************
    * Constant
    **************************************************************************************************/

    #define MSA_DEV_SHORT_ADDR 0x0000 /* Device initial short address - This will change after association */

    #define MSA_HEADER_LENGTH 4 /* Header includes DataLength + DeviceShortAddr + Sequence */
    #define MSA_ECHO_LENGTH 8 /* Echo packet */

    #define MSA_MAC_MAX_RESULTS 5 /* Maximun number of scan result that will be accepted */
    #define MSA_MAX_DEVICE_NUM 10 /* Maximun number of devices can associate with the coordinator */

    #if defined (HAL_BOARD_CC2420DB)
    #define MSA_HAL_ADC_CHANNEL HAL_ADC_CHANNEL_0 /* AVR - Channel 0 and Resolution 10 */
    #define MSA_HAL_ADC_RESOLUTION HAL_ADC_RESOLUTION_10
    #elif defined (HAL_BOARD_DZ1611) || defined (HAL_BOARD_DZ1612) || defined (HAL_BOARD_DRFG4618) || defined (HAL_BOARD_F2618)
    #define MSA_HAL_ADC_CHANNEL HAL_ADC_CHANNEL_0 /* DZ1611 and DZ1612 - Channel 0 and Resolution 12 */
    #define MSA_HAL_ADC_RESOLUTION HAL_ADC_RESOLUTION_12
    #else
    #define MSA_HAL_ADC_CHANNEL HAL_ADC_CHANNEL_7 /* CC2430 EB & DB - Channel 7 and Resolution 14 */
    #define MSA_HAL_ADC_RESOLUTION HAL_ADC_RESOLUTION_14
    #endif

    #define MSA_EBR_PERMITJOINING TRUE
    #define MSA_EBR_LINKQUALITY 1
    #define MSA_EBR_PERCENTFILTER 0xFF

    #define Hal_ADC TRUE
    #define ENABLE_CS() {CS = 0;halSleepWait(1);}
    #define DISABLE_CS(x) {halSleepWait(1);CS = 1;halSleepWait(x);}

    /* Size table for MAC structures */
    const CODE uint8 msa_cbackSizeTable [] =
    {
    0, /* unused */
    sizeof(macMlmeAssociateInd_t), /* MAC_MLME_ASSOCIATE_IND */
    sizeof(macMlmeAssociateCnf_t), /* MAC_MLME_ASSOCIATE_CNF */
    sizeof(macMlmeDisassociateInd_t), /* MAC_MLME_DISASSOCIATE_IND */
    sizeof(macMlmeDisassociateCnf_t), /* MAC_MLME_DISASSOCIATE_CNF */
    sizeof(macMlmeBeaconNotifyInd_t), /* MAC_MLME_BEACON_NOTIFY_IND */
    sizeof(macMlmeOrphanInd_t), /* MAC_MLME_ORPHAN_IND */
    sizeof(macMlmeScanCnf_t), /* MAC_MLME_SCAN_CNF */
    sizeof(macMlmeStartCnf_t), /* MAC_MLME_START_CNF */
    sizeof(macMlmeSyncLossInd_t), /* MAC_MLME_SYNC_LOSS_IND */
    sizeof(macMlmePollCnf_t), /* MAC_MLME_POLL_CNF */
    sizeof(macMlmeCommStatusInd_t), /* MAC_MLME_COMM_STATUS_IND */
    sizeof(macMcpsDataCnf_t), /* MAC_MCPS_DATA_CNF */
    sizeof(macMcpsDataInd_t), /* MAC_MCPS_DATA_IND */
    sizeof(macMcpsPurgeCnf_t), /* MAC_MCPS_PURGE_CNF */
    sizeof(macEventHdr_t), /* MAC_PWR_ON_CNF */
    sizeof(macMlmePollInd_t) /* MAC_MLME_POLL_IND */
    };

    /**************************************************************************************************
    * Local Variables
    **************************************************************************************************/

    /*
    Extended address of the device, for coordinator, it will be msa_ExtAddr1
    For any device, it will be msa_ExtAddr2 with the last 2 bytes from a ADC read
    */
    sAddrExt_t msa_ExtAddr;
    sAddrExt_t msa_ExtAddr1 = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88};
    sAddrExt_t msa_ExtAddr2 = {0xA0, 0xB0, 0xC0, 0xD0, 0xE0, 0xF0, 0x00, 0x00};

    /* Coordinator and Device information */
    uint16 msa_PanId = MSA_PAN_ID;
    uint16 msa_CoordShortAddr = MSA_COORD_SHORT_ADDR;
    uint16 msa_DevShortAddr = MSA_DEV_SHORT_ADDR;

    /*
    List of short addresses that the coordinator will use
    to assign to each the device that associates to it
    */
    uint16 msa_DevShortAddrList[] = {0x0001, 0x0002, 0x0003, 0x0004, 0x0005,
    0x0006, 0x0007, 0x0008, 0x0009, 0x000A,
    0x000B, 0x000C, 0x000D, 0x000E, 0x000F,
    0x0010, 0x0011, 0x0012, 0x0013, 0x0014,
    0x0015, 0x0016, 0x0017, 0x0018, 0x0019,
    0x001A, 0x001B, 0x001C, 0x001D, 0x001E,
    0x001F, 0x0020, 0x0021, 0x0022, 0x0023,
    0x0024, 0x0025, 0x0026, 0x0027, 0x0028,
    0x0029, 0x002A, 0x002B, 0x002C, 0x002D,
    0x002E, 0x002F, 0x0030, 0x0031, 0x0032};

    /* Current number of devices associated to the coordinator */
    uint8 msa_NumOfDevices = 0;

    #ifdef MAC_SECURITY
    /* Current number of devices (including coordinator) communicating to this device */
    uint8 msa_NumOfSecuredDevices = 0;
    #endif /* MAC_SECURITY */

    /* Predefined packet that will be sent out by the coordinator */
    uint8 msa_Data1[MSA_PACKET_LENGTH];

    /* Predefined packet that will be sent back to the coordinator by the device */
    uint8 msa_Data2[MSA_ECHO_LENGTH];

    /* TRUE and FALSE value */
    bool msa_MACTrue = TRUE;
    bool msa_MACFalse = FALSE;

    /* Beacon payload, this is used to determine if the device is not zigbee device */
    uint8 msa_BeaconPayload[] = {0x22, 0x33, 0x44};
    uint8 msa_BeaconPayloadLen = 3;

    /* Contains pan descriptor results from scan */
    macPanDesc_t msa_PanDesc[MSA_MAC_MAX_RESULTS];

    /* flags used in the application */
    bool msa_IsCoordinator = FALSE; /* True if the device is started as a Pan Coordinate */
    bool msa_IsStarted = FALSE; /* True if the device started, either as Pan Coordinator or device */
    bool msa_IsSampleBeacon = FALSE; /* True if the beacon payload match with the predefined */
    bool msa_IsDirectMsg = FALSE; /* True if the messages will be sent as direct messages */
    uint8 msa_State = MSA_IDLE_STATE; /* Either IDLE state or SEND state */

    /* Structure that used for association request */
    macMlmeAssociateReq_t msa_AssociateReq;

    /* Structure that used for association response */
    macMlmeAssociateRsp_t msa_AssociateRsp;

    /* Structure that contains information about the device that associates with the coordinator */
    typedef struct
    {
    uint16 devShortAddr;
    uint8 isDirectMsg;
    sAddrExt_t deviceAddress;
    }msa_DeviceInfo_t;

    /* Array contains the information of the devices */
    static msa_DeviceInfo_t msa_DeviceRecord[MSA_MAX_DEVICE_NUM];

    uint8 msa_SuperFrameOrder;
    uint8 msa_BeaconOrder;

    /* Task ID */
    uint8 MSA_TaskId;

    /*Command Buffer for UART */
    uint8 cmdbuff[5];
    bool Debug = FALSE;
    char Data_type;
    bool GSM = FALSE;

    uint8 Sequence_number = 0;


    #ifdef MAC_SECURITY
    /**************************************************************************************************
    * MAC security related constants
    **************************************************************************************************/
    #define MSA_KEY_TABLE_ENTRIES 1
    #define MSA_KEY_ID_LOOKUP_ENTRIES 1
    #define MSA_KEY_DEVICE_TABLE_ENTRIES 8
    #define MSA_KEY_USAGE_TABLE_ENTRIES 1
    #define MSA_DEVICE_TABLE_ENTRIES 0 /* can grow up to MAX_DEVICE_TABLE_ENTRIES */
    #define MSA_SECURITY_LEVEL_ENTRIES 1
    #define MSA_MAC_SEC_LEVEL MAC_SEC_LEVEL_ENC_MIC_32
    #define MSA_MAC_KEY_ID_MODE MAC_KEY_ID_MODE_1
    #define MSA_MAC_KEY_SOURCE {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
    #define MSA_MAC_DEFAULT_KEY_SOURCE {0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33}
    #define MSA_MAC_KEY_INDEX 3 /* cannot be zero for implicit key identifier */


    /* Default MSA Security Parameters for outgoing frames */
    bool macSecurity = TRUE;
    uint8 msa_securityLevel = MSA_MAC_SEC_LEVEL;
    uint8 msa_keyIdMode = MSA_MAC_KEY_ID_MODE;
    uint8 msa_keySource[] = MSA_MAC_KEY_SOURCE;
    uint8 msa_keyIndex = MSA_MAC_KEY_INDEX;


    /**************************************************************************************************
    * Security PIBs for outgoing frames
    **************************************************************************************************/
    const keyIdLookupDescriptor_t msa_keyIdLookupList[] =
    {
    {
    {0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x03}, 0x01 /* index 3, 9 octets */
    }
    };

    /* Key device list can be modified at run time
    */
    keyDeviceDescriptor_t msa_keyDeviceList[] =
    {
    {0x00, false, false},
    {0x00, false, false},
    {0x00, false, false},
    {0x00, false, false},
    {0x00, false, false},
    {0x00, false, false},
    {0x00, false, false},
    {0x00, false, false}
    };

    const keyUsageDescriptor_t msa_keyUsageList[] =
    {
    {MAC_FRAME_TYPE_DATA, MAC_DATA_REQ_FRAME}
    };

    const keyDescriptor_t msa_keyTable[] =
    {
    {
    (keyIdLookupDescriptor_t *)msa_keyIdLookupList, MSA_KEY_ID_LOOKUP_ENTRIES,
    (keyDeviceDescriptor_t *)msa_keyDeviceList, MSA_KEY_DEVICE_TABLE_ENTRIES,
    (keyUsageDescriptor_t *)msa_keyUsageList, MSA_KEY_USAGE_TABLE_ENTRIES,
    {0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
    }
    };

    const uint8 msa_keyDefaultSource[] = MSA_MAC_DEFAULT_KEY_SOURCE;


    /**************************************************************************************************
    * Security PIBs for incoming frames
    **************************************************************************************************/

    /* Device table can be modified at run time. */
    deviceDescriptor_t msa_deviceTable[] =
    {
    {0, 0xFFFF, 0, 0, FALSE},
    {0, 0xFFFF, 0, 0, FALSE},
    {0, 0xFFFF, 0, 0, FALSE},
    {0, 0xFFFF, 0, 0, FALSE},
    {0, 0xFFFF, 0, 0, FALSE},
    {0, 0xFFFF, 0, 0, FALSE},
    {0, 0xFFFF, 0, 0, FALSE},
    {0, 0xFFFF, 0, 0, FALSE}
    };

    const securityLevelDescriptor_t msa_securityLevelTable[] =
    {
    {MAC_FRAME_TYPE_DATA, MAC_DATA_REQ_FRAME, MAC_SEC_LEVEL_NONE, FALSE}
    };
    #else /* MAC_SECURITY */
    uint8 msa_securityLevel = MAC_SEC_LEVEL_NONE;
    uint8 msa_keyIdMode = MAC_KEY_ID_MODE_NONE;
    uint8 msa_keySource[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
    uint8 msa_keyIndex = 0;
    #endif /* MAC_SECURITY */


    /**************************************************************************************************
    * Local Function Prototypes
    **************************************************************************************************/
    /* Setup routines */
    void MSA_CoordinatorStartup(void);
    void MSA_DeviceStartup(void);

    /* MAC related routines */
    void MSA_AssociateReq(void);
    void MSA_AssociateRsp(macCbackEvent_t* pMsg);
    void MSA_McpsDataReq(uint8* data, uint8 dataLength, bool directMsg, uint16 dstShortAddr);
    void MSA_McpsPollReq(void);
    void MSA_ScanReq(uint8 scanType, uint8 scanDuration);
    void MSA_SyncReq(void);

    /* Support */
    bool MSA_BeaconPayLoadCheck(uint8* pSdu);
    bool MSA_DataCheck(uint8* data, uint8 dataLength);

    void MT_UartProcessRxData(uint8 port, uint8 event);

    void setCood(void);
    void UART_setup(void);

    /*delay function (1 = 1 sec)*/
    void delay(double delay);
    //void UDP_Send(uint8 size, uint8 *DataArray);
    bool write_status = true;


    #ifdef MAC_SECURITY
    /**************************************************************************************************
    *
    * @fn MSA_SecurityInit
    *
    * @brief Initialize the security part of the application
    *
    * @param none
    *
    * @return none
    *
    **************************************************************************************************/
    static void MSA_SecurityInit(void)
    {
    uint8 keyTableEntries = MSA_KEY_TABLE_ENTRIES;
    uint8 autoRequestSecLevel = MAC_SEC_LEVEL_NONE;
    uint8 securityLevelEntries = MSA_SECURITY_LEVEL_ENTRIES;

    /* Write key table PIBs */
    MAC_MlmeSetSecurityReq(MAC_KEY_TABLE, (void *)msa_keyTable);
    MAC_MlmeSetSecurityReq(MAC_KEY_TABLE_ENTRIES, &keyTableEntries );

    /* Write default key source to PIB */
    MAC_MlmeSetSecurityReq(MAC_DEFAULT_KEY_SOURCE, (void *)msa_keyDefaultSource);

    /* Write security level table to PIB */
    MAC_MlmeSetSecurityReq(MAC_SECURITY_LEVEL_TABLE, (void *)msa_securityLevelTable);
    MAC_MlmeSetSecurityReq(MAC_SECURITY_LEVEL_TABLE_ENTRIES, &securityLevelEntries);

    /* TBD: MAC_AUTO_REQUEST is true on by default
    * need to set auto request security PIBs.
    * dieable auto request security for now
    */
    MAC_MlmeSetSecurityReq(MAC_AUTO_REQUEST_SECURITY_LEVEL, &autoRequestSecLevel);

    /* Turn on MAC security */
    MAC_MlmeSetReq(MAC_SECURITY_ENABLED, &macSecurity);
    }


    /**************************************************************************************************
    *
    * @fn MSA_SecuredDeviceTableUpdate
    *
    * @brief Update secured device table and key device descriptor handle
    *
    * @param panID - Secured network PAN ID
    * @param shortAddr - Other device's short address
    * @param extAddr - Other device's extended address
    * @param pNumOfSecuredDevices - pointer to number of secured devices
    *
    * @return none
    *
    **************************************************************************************************/
    static void MSA_SecuredDeviceTableUpdate(uint16 panID,
    uint16 shortAddr,
    sAddrExt_t extAddr,
    uint8 *pNumOfSecuredDevices)
    {
    /* Update key device list */
    msa_keyDeviceList[*pNumOfSecuredDevices].deviceDescriptorHandle = *pNumOfSecuredDevices;

    /* Update device table */
    msa_deviceTable[*pNumOfSecuredDevices].panID = panID;
    msa_deviceTable[*pNumOfSecuredDevices].shortAddress = shortAddr;
    sAddrExtCpy(msa_deviceTable[*pNumOfSecuredDevices].extAddress, extAddr);
    MAC_MlmeSetSecurityReq(MAC_DEVICE_TABLE, msa_deviceTable);

    /* Increase the number of secured devices */
    (*pNumOfSecuredDevices)++;
    MAC_MlmeSetSecurityReq(MAC_DEVICE_TABLE_ENTRIES, pNumOfSecuredDevices);
    }
    #endif /* MAC_SECURITY */


    /**************************************************************************************************
    *
    * @fn MSA_Init
    *
    * @brief Initialize the application
    *
    * @param taskId - taskId of the task after it was added in the OSAL task queue
    *
    * @return none
    *
    **************************************************************************************************/
    void MSA_Init(uint8 taskId)
    {
    uint8 i;

    /* Initialize the task id */
    MSA_TaskId = taskId;

    /* initialize MAC features */
    MAC_InitDevice();
    MAC_InitCoord();

    /* Initialize MAC beacon */
    MAC_InitBeaconDevice();
    MAC_InitBeaconCoord();

    /* Reset the MAC */
    MAC_MlmeResetReq(TRUE);

    #ifdef MAC_SECURITY
    /* Initialize the security part of the application */
    MSA_SecurityInit();
    #endif /* MAC_SECURITY */

    /* Initialize the data packet */
    for (i=MSA_HEADER_LENGTH; i<MSA_PACKET_LENGTH; i++)
    {
    msa_Data1[i] = i-MSA_HEADER_LENGTH;
    }

    /* Initialize the echo packet */
    for (i=0; i<MSA_ECHO_LENGTH; i++)
    {
    msa_Data2[i] = 0xEE;
    }

    msa_BeaconOrder = MSA_MAC_BEACON_ORDER;
    msa_SuperFrameOrder = MSA_MAC_SUPERFRAME_ORDER;

    //HalLedBlink (HAL_LED_2, 0, 40, 200);

    //SetCood is a custom function to make the coordinator automatically start up its own network
    //Previously, the default settings were to press the joystick on the SmartRF05EB to
    //make the coordinator start its own network (previously defined in MSA_HandleKeys)
    setCood();


    //MSA_DeviceStartup();
    /*
    Unnecessary to put this command here.
    MSA_DeviceStartup() gets called in the MAC_MLME_SCAN_CNF event handler.
    */

    }

    /**************************************************************************************************
    *
    * @fn MSA_ProcessEvent
    *
    * @brief This routine handles events
    *
    * @param taskId - ID of the application task when it registered with the OSAL
    * events - Events for this task
    *
    * @return 16bit - Unprocessed events
    *
    **************************************************************************************************/
    uint16 MSA_ProcessEvent(uint8 taskId, uint16 events)
    {
    uint8* pMsg;
    macCbackEvent_t* pData;
    const uint8 TestMarker[] = {0xDE, 0xAD,0xBE, 0xEF, 0xDE, 0xAD,0xBE, 0xEF};

    #ifdef MAC_SECURITY
    uint16 panID;
    uint16 panCoordShort;
    sAddrExt_t panCoordExtAddr;
    #endif /* MAC_SECURITY */

    static uint8 index;
    static uint8 sequence;

    if (events & SYS_EVENT_MSG)
    {
    while ((pMsg = osal_msg_receive(MSA_TaskId)) != NULL)
    {
    switch ( *pMsg )
    {
    case MAC_MLME_ASSOCIATE_IND:
    #ifdef Debug_msa_1
    HalUARTWrite( 0, "MAC_MLME_ASSOCIATE_IND \n\r", 30 );
    #endif
    MSA_AssociateRsp((macCbackEvent_t*)pMsg);
    break;

    case MAC_MLME_ASSOCIATE_CNF:
    /* Retrieve the message */
    #ifdef Debug_msa_1
    HalUARTWrite( 0, "MAC_MLME_ASSOCIATE_CNF \n\r", 30 );
    #endif

    if (Debug){HalUARTWrite( 0, "MAC_MLME_ASSOCIATE_CNF \n\r", 30 );}
    pData = (macCbackEvent_t *) pMsg;

    if ((!msa_IsStarted) && (pData->associateCnf.hdr.status == MAC_SUCCESS))
    {
    msa_IsStarted = TRUE;

    /* Retrieve MAC_SHORT_ADDRESS */

    msa_DevShortAddr = pData->associateCnf.assocShortAddress;

    /* Setup MAC_SHORT_ADDRESS - obtained from Association */
    MAC_MlmeSetReq(MAC_SHORT_ADDRESS, &msa_DevShortAddr);

    #ifdef MAC_SECURITY
    /* Add the coordinator to device table and key device table for security */
    MAC_MlmeGetReq(MAC_PAN_ID, &panID);
    MAC_MlmeGetSecurityReq(MAC_PAN_COORD_SHORT_ADDRESS, &panCoordShort);
    MAC_MlmeGetSecurityReq(MAC_PAN_COORD_EXTENDED_ADDRESS, &panCoordExtAddr);
    MSA_SecuredDeviceTableUpdate(panID, panCoordShort,
    panCoordExtAddr, &msa_NumOfSecuredDevices);
    #endif /* MAC_SECURITY */

    //HalLedBlink(HAL_LED_3, 0, 90, 1000);

    /* Poll for data if it's not setup for direct messaging */
    if (!msa_IsDirectMsg)
    {
    osal_start_timerEx(MSA_TaskId, MSA_POLL_EVENT, MSA_WAIT_PERIOD);
    }
    }
    break;

    case MAC_MLME_COMM_STATUS_IND:
    #ifdef Debug_msa_1
    HalUARTWrite( 0, "MAC_MLME_COMM_STATUS_IND \n\r", 30 );
    #endif
    break;

    case MAC_MLME_BEACON_NOTIFY_IND:
    /* Retrieve the message */
    pData = (macCbackEvent_t *) pMsg;

    /* Check for correct beacon payload */
    if (!msa_IsStarted)
    {
    msa_IsSampleBeacon = MSA_BeaconPayLoadCheck(pData->beaconNotifyInd.pSdu);

    /* If it's the correct beacon payload, retrieve the data for association req */
    if (msa_IsSampleBeacon)
    {
    msa_AssociateReq.logicalChannel = MSA_MAC_CHANNEL;
    msa_AssociateReq.coordAddress.addrMode = SADDR_MODE_SHORT;
    msa_AssociateReq.coordAddress.addr.shortAddr = pData->beaconNotifyInd.pPanDesc->coordAddress.addr.shortAddr;
    msa_AssociateReq.coordPanId = pData->beaconNotifyInd.pPanDesc->coordPanId;
    if (msa_IsDirectMsg)
    msa_AssociateReq.capabilityInformation = MAC_CAPABLE_ALLOC_ADDR | MAC_CAPABLE_RX_ON_IDLE;
    else
    msa_AssociateReq.capabilityInformation = MAC_CAPABLE_ALLOC_ADDR;
    msa_AssociateReq.sec.securityLevel = MAC_SEC_LEVEL_NONE;

    /* Retrieve beacon order and superframe order from the beacon */
    msa_BeaconOrder = MAC_SFS_BEACON_ORDER(pData->beaconNotifyInd.pPanDesc->superframeSpec);
    msa_SuperFrameOrder = MAC_SFS_SUPERFRAME_ORDER(pData->beaconNotifyInd.pPanDesc->superframeSpec);
    }
    }

    break;

    case MAC_MLME_START_CNF:
    #ifdef Debug_msa_1
    HalUARTWrite( 0, "MAC_MLME_START_CNF\n\r", 30 );
    #endif
    /* Retrieve the message */
    pData = (macCbackEvent_t *) pMsg;
    /* Set some indicator for the Coordinator */
    if ((!msa_IsStarted) && (pData->startCnf.hdr.status == MAC_SUCCESS))
    {
    msa_IsStarted = TRUE;
    msa_IsCoordinator = TRUE;
    HalLedSet (HAL_LED_1, HAL_LED_MODE_ON);
    }
    break;

    case MAC_MLME_SCAN_CNF:

    #ifdef Debug_msa_1
    HalUARTWrite( 0, "MAC_MLME_SCAN_CNF\n\r", 30 );
    #endif
    /* Check if there is any Coordinator out there */
    pData = (macCbackEvent_t *) pMsg;

    /* If there is no other on the channel or no other with sampleBeacon start as coordinator */
    if (((pData->scanCnf.resultListSize == 0) && (pData->scanCnf.hdr.status == MAC_NO_BEACON)) || (!msa_IsSampleBeacon))
    {
    MSA_CoordinatorStartup();
    uint8 uart_comd[3];
    uart_comd[0] = 0x02;
    uart_comd[1] = 0x05;
    uart_comd[2] = 0x03;
    HalUARTWrite( 0, uart_comd , 3);
    HalLcdWriteString( "PAN Coordinator", HAL_LCD_LINE_1 );
    delay(1);
    }

    else if ((msa_IsSampleBeacon) && pData->scanCnf.hdr.status == MAC_SUCCESS)
    {
    /* Start the devive up as beacon enabled or not */
    MSA_DeviceStartup();

    /* Call Associate Req */
    MSA_AssociateReq();
    }

    break;

    case MAC_MCPS_DATA_CNF:
    pData = (macCbackEvent_t *) pMsg;

    /* If last transmission completed, ready to send the next one */
    if ((pData->dataCnf.hdr.status == MAC_SUCCESS) ||
    (pData->dataCnf.hdr.status == MAC_CHANNEL_ACCESS_FAILURE) ||
    (pData->dataCnf.hdr.status == MAC_NO_ACK))
    {
    osal_start_timerEx(MSA_TaskId, MSA_SEND_EVENT, MSA_WAIT_PERIOD);
    }

    mac_msg_deallocate((uint8**)&pData->dataCnf.pDataReq);
    break;

    case MAC_MCPS_DATA_IND:
    #ifdef Debug_msa_1
    HalUARTWrite( 0, "MAC_MCPS_DATA_IND\n\r", 30 );
    #endif
    pData = (macCbackEvent_t*)pMsg;


    if (MSA_DataCheck ( pData->dataInd.msdu.p, pData->dataInd.msdu.len ))
    {
    //HalLedSet (HAL_LED_3, HAL_LED_MODE_TOGGLE);

    /* Only send the echo back if the received */
    if (!msa_IsCoordinator)
    {
    if (MSA_ECHO_LENGTH >= 4)
    {
    /* Return the first 4 bytes and the last byte of the received packet */
    msa_Data2[0] = pData->dataInd.msdu.p[0];
    msa_Data2[1] = pData->dataInd.msdu.p[1];
    msa_Data2[2] = pData->dataInd.msdu.p[2];
    msa_Data2[3] = pData->dataInd.msdu.p[3];
    }

    MSA_McpsDataReq(msa_Data2,
    MSA_ECHO_LENGTH,
    TRUE,
    msa_CoordShortAddr );
    }
    }

    if (Sequence_number == 0xFF){
    Sequence_number = 0x00; //Reset sequence number back to zero
    }




    /*-------------------- COORDINATOR RECEPTION --------------------

    Note(s): The coordinator has two tasks.
    1. Receive the array of sensor data from the node.
    2. Form an array containing the sensor data and outputting it to the serial
    connection to the computer.
    */





    //Check if the 4th byte of the application array sent represents a character 'E'.
    //Node should be coded to send 'E' as the 4th byte in the application array
    if (pData->dataInd.msdu.p[4] == 'E'){
    HalLedSet (HAL_LED_3, HAL_LED_MODE_TOGGLE);
    //Send ECG Data back to PC via UART
    Sequence_number++;
    //********************* CHANGE PACKET LENGTH HERE ******************************
    uint8 packetLength = 0x15;
    int packetLengthInt = 21;
    //********************* CHANGE PACKET LENGTH HERE ******************************

    //Form a new array consisting of the sensor data and send it to the computer
    //length of packet is also at DataFrame[1]
    uint8 DataFrame[21];
    DataFrame[0] = 0x01;
    DataFrame[1] = packetLength;
    osal_memcpy((void *)&(DataFrame[2]), (void *) TestMarker, 8);

    uint16 temp_short_add;
    // temp_short_add = pData->dataInd.msdu.p[14]; //Correct the packet which contains
    // //msa_DevShortAddress in SensorNode
    temp_short_add = pData->dataInd.mac.srcAddr.addr.shortAddr;
    for(int No_of_Devices = 0; No_of_Devices < msa_NumOfDevices; No_of_Devices++){
    if (msa_DeviceRecord[No_of_Devices].devShortAddr == temp_short_add){
    sAddrExt_t temp_device_ext;
    sAddrExtCpy(temp_device_ext, msa_DeviceRecord[No_of_Devices].deviceAddress);

    DataFrame[2] = temp_device_ext[7];
    DataFrame[3] = temp_device_ext[6];
    DataFrame[4] = temp_device_ext[5];
    DataFrame[5] = temp_device_ext[4];
    DataFrame[6] = temp_device_ext[3];
    DataFrame[7] = temp_device_ext[2];
    DataFrame[8] = temp_device_ext[1];
    DataFrame[9] = temp_device_ext[0];
    }
    }
    DataFrame[10] = Sequence_number;
    DataFrame[11] = 'E';
    DataFrame[12] = pData->dataInd.msdu.p[5];
    DataFrame[13] = pData->dataInd.msdu.p[6];
    DataFrame[14] = 'T';
    DataFrame[15] = pData->dataInd.msdu.p[8];
    DataFrame[16] = pData->dataInd.msdu.p[9];
    DataFrame[17] = 'P';
    DataFrame[18] = pData->dataInd.msdu.p[11];
    DataFrame[19] = pData->dataInd.msdu.p[12];
    DataFrame[20] = pData->dataInd.msdu.p[13];

    if (DataFrame[1] == packetLength) {
    //********************* CHANGE PACKET LENGTH HERE ******************************
    HalUARTWrite( 0, DataFrame , packetLengthInt);
    cmdbuff[0] = DataFrame[0];
    cmdbuff[1] = DataFrame[11];
    cmdbuff[2] = DataFrame[14];
    cmdbuff[3] = DataFrame[17];
    }






    /*-------------------- END OF COORDINATOR RECEPTION --------------------*/


    }
    break;
    }

    /* Deallocate */
    mac_msg_deallocate((uint8 **)&pMsg);
    }


    return events ^ SYS_EVENT_MSG;
    }

    /* This event handles polling for in-direct message device */
    if (events & MSA_POLL_EVENT)
    {
    MSA_McpsPollReq();
    osal_start_timerEx(MSA_TaskId, MSA_POLL_EVENT, MSA_WAIT_PERIOD);

    return events ^ MSA_POLL_EVENT;
    }

    /* This event will blink a LED then send out a DataReq and continue to do so every WAIT_PERIOD */
    if (events & MSA_SEND_EVENT)
    {

    /* Do it again */
    if (msa_State == MSA_SEND_STATE)
    {
    /* Start sending */
    if (msa_IsCoordinator)
    {
    if (MSA_PACKET_LENGTH >= MSA_HEADER_LENGTH)
    {
    /* Coordinator sending to devices. Use the associated list of device to send out msg */
    msa_Data1[0] = MSA_PACKET_LENGTH;
    msa_Data1[1] = HI_UINT16(msa_DeviceRecord[index].devShortAddr);
    msa_Data1[2] = LO_UINT16(msa_DeviceRecord[index].devShortAddr);
    msa_Data1[3] = sequence;
    }

    MSA_McpsDataReq((uint8*)msa_Data1,
    MSA_PACKET_LENGTH,
    msa_DeviceRecord[index].isDirectMsg,
    msa_DeviceRecord[index].devShortAddr );

    /* Reset the index if it reaches the current number of associated devices */
    if (++index == msa_NumOfDevices)
    {
    index = 0;
    }
    msa_State = MSA_IDLE_STATE;
    }
    else
    {
    if (MSA_PACKET_LENGTH >= MSA_HEADER_LENGTH)
    {
    /* Device sending to coordinator */
    msa_Data1[0] = MSA_PACKET_LENGTH;
    msa_Data1[1] = HI_UINT16(msa_CoordShortAddr);
    msa_Data1[2] = LO_UINT16(msa_CoordShortAddr);
    msa_Data1[3] = sequence;
    }
    MSA_McpsDataReq((uint8*)msa_Data1,
    MSA_PACKET_LENGTH,
    TRUE,
    msa_CoordShortAddr );

    }


    /* Reset the index if it reaches the current number of associated devices */
    if (++index == msa_NumOfDevices)
    {
    index = 0;
    }

    if (sequence++ == 0xFF)
    {
    sequence = 0;
    }

    //HalLedSet (HAL_LED_1, HAL_LED_MODE_BLINK);
    }

    return events ^ MSA_SEND_EVENT;
    }
    return 0;
    }

    /**************************************************************************************************
    *
    * @fn MAC_CbackEvent
    *
    * @brief This callback function sends MAC events to the application.
    * The application must implement this function. A typical
    * implementation of this function would allocate an OSAL message,
    * copy the event parameters to the message, and send the message
    * to the application's OSAL event handler. This function may be
    * executed from task or interrupt context and therefore must
    * be reentrant.
    *
    * @param pData - Pointer to parameters structure.
    *
    * @return None.
    *
    **************************************************************************************************/
    void MAC_CbackEvent(macCbackEvent_t *pData)
    {
    //HalLedSet (HAL_LED_2, HAL_LED_MODE_BLINK);
    macCbackEvent_t *pMsg = NULL;

    uint8 len = msa_cbackSizeTable[pData->hdr.event];

    switch (pData->hdr.event)
    {
    case MAC_MLME_BEACON_NOTIFY_IND:

    len += sizeof(macPanDesc_t) + pData->beaconNotifyInd.sduLength +
    MAC_PEND_FIELDS_LEN(pData->beaconNotifyInd.pendAddrSpec);
    if ((pMsg = (macCbackEvent_t *) osal_msg_allocate(len)) != NULL)
    {
    /* Copy data over and pass them up */
    osal_memcpy(pMsg, pData, sizeof(macMlmeBeaconNotifyInd_t));
    pMsg->beaconNotifyInd.pPanDesc = (macPanDesc_t *) ((uint8 *) pMsg + sizeof(macMlmeBeaconNotifyInd_t));
    osal_memcpy(pMsg->beaconNotifyInd.pPanDesc, pData->beaconNotifyInd.pPanDesc, sizeof(macPanDesc_t));
    pMsg->beaconNotifyInd.pSdu = (uint8 *) (pMsg->beaconNotifyInd.pPanDesc + 1);
    osal_memcpy(pMsg->beaconNotifyInd.pSdu, pData->beaconNotifyInd.pSdu, pData->beaconNotifyInd.sduLength);
    }
    break;

    case MAC_MCPS_DATA_IND:
    pMsg = pData;
    break;

    default:
    if ((pMsg = (macCbackEvent_t *) osal_msg_allocate(len)) != NULL)
    {
    osal_memcpy(pMsg, pData, len);
    }
    break;
    }

    if (pMsg != NULL)
    {
    osal_msg_send(MSA_TaskId, (uint8 *) pMsg);
    }
    }

    /**************************************************************************************************
    *
    * @fn MAC_CbackCheckPending
    *
    * @brief Returns the number of indirect messages pending in the application
    *
    * @param None
    *
    * @return Number of indirect messages in the application
    *
    **************************************************************************************************/
    uint8 MAC_CbackCheckPending(void)
    {
    return (0);
    }

    /**************************************************************************************************
    *
    * @fn MSA_CoordinatorStartup()
    *
    * @brief Update the timer per tick
    *
    * @param n/a
    *
    * @return None
    *
    **************************************************************************************************/
    void MSA_CoordinatorStartup()
    {
    macMlmeStartReq_t startReq;

    /* Setup MAC_EXTENDED_ADDRESS */
    sAddrExtCpy(msa_ExtAddr, msa_ExtAddr1);
    MAC_MlmeSetReq(MAC_EXTENDED_ADDRESS, &msa_ExtAddr);

    /* Setup MAC_SHORT_ADDRESS */
    MAC_MlmeSetReq(MAC_SHORT_ADDRESS, &msa_CoordShortAddr);

    /* Setup MAC_BEACON_PAYLOAD_LENGTH */
    MAC_MlmeSetReq(MAC_BEACON_PAYLOAD_LENGTH, &msa_BeaconPayloadLen);

    /* Setup MAC_BEACON_PAYLOAD */
    MAC_MlmeSetReq(MAC_BEACON_PAYLOAD, &msa_BeaconPayload);

    /* Enable RX */
    MAC_MlmeSetReq(MAC_RX_ON_WHEN_IDLE, &msa_MACTrue);

    /* Setup MAC_ASSOCIATION_PERMIT */
    MAC_MlmeSetReq(MAC_ASSOCIATION_PERMIT, &msa_MACTrue);

    /* Fill in the information for the start request structure */
    startReq.startTime = 0;
    startReq.panId = msa_PanId;
    startReq.logicalChannel = MSA_MAC_CHANNEL;
    startReq.beaconOrder = msa_BeaconOrder;
    startReq.superframeOrder = msa_SuperFrameOrder;
    startReq.panCoordinator = TRUE;
    startReq.batteryLifeExt = FALSE;
    startReq.coordRealignment = FALSE;
    startReq.realignSec.securityLevel = FALSE;
    startReq.beaconSec.securityLevel = FALSE;

    /* Call start request to start the device as a coordinator */
    MAC_MlmeStartReq(&startReq);
    HalLcdWriteString( "PAN Coordinator", HAL_LCD_LINE_1 );
    #ifdef SPI_On
    SPI_GetDeviceInfor();
    #endif

    /* Allow Beacon mode coordinator to sleep */
    if (msa_BeaconOrder != 15)
    {
    /* Power saving */
    MSA_PowerMgr (MSA_PWR_MGMT_ENABLED);
    }

    }

    /**************************************************************************************************
    *
    * @fn MSA_DeviceStartup()
    *
    * @brief Update the timer per tick
    *
    * @param beaconEnable: TRUE/FALSE
    *
    * @return None
    *
    **************************************************************************************************/
    void MSA_DeviceStartup()
    {
    uint16 AtoD = 0;

    #if (defined HAL_ADC) && (HAL_ADC == TRUE)
    AtoD = HalAdcRead (MSA_HAL_ADC_CHANNEL, MSA_HAL_ADC_RESOLUTION);
    #else
    AtoD = MAC_RADIO_RANDOM_WORD();
    #endif

    /*
    Setup MAC_EXTENDED_ADDRESS
    */
    #if defined (HAL_BOARD_CC2420DB) || defined (HAL_BOARD_DZ1611) || defined (HAL_BOARD_DZ1612) || \
    defined (HAL_BOARD_DRFG4618) || defined (HAL_BOARD_F2618)
    /* Use HI and LO of AtoD on CC2420 and MSP430 */
    msa_ExtAddr2[6] = HI_UINT16( AtoD );
    msa_ExtAddr2[7] = LO_UINT16( AtoD );
    #else
    /* On CC2430 and CC2530 use current MAC timer */
    AtoD = macMcuPrecisionCount();
    msa_ExtAddr2[6] = HI_UINT16( AtoD );
    msa_ExtAddr2[7] = LO_UINT16( AtoD );
    #endif

    sAddrExtCpy(msa_ExtAddr, msa_ExtAddr2);
    MAC_MlmeSetReq(MAC_EXTENDED_ADDRESS, &msa_ExtAddr);

    /* Setup MAC_BEACON_PAYLOAD_LENGTH */
    MAC_MlmeSetReq(MAC_BEACON_PAYLOAD_LENGTH, &msa_BeaconPayloadLen);

    /* Setup MAC_BEACON_PAYLOAD */
    MAC_MlmeSetReq(MAC_BEACON_PAYLOAD, &msa_BeaconPayload);

    /* Setup PAN ID */
    MAC_MlmeSetReq(MAC_PAN_ID, &msa_PanId);

    /* This device is setup for Direct Message */
    if (msa_IsDirectMsg)
    MAC_MlmeSetReq(MAC_RX_ON_WHEN_IDLE, &msa_MACTrue);
    else
    MAC_MlmeSetReq(MAC_RX_ON_WHEN_IDLE, &msa_MACFalse);

    /* Setup Coordinator short address */
    MAC_MlmeSetReq(MAC_COORD_SHORT_ADDRESS, &msa_AssociateReq.coordAddress.addr.shortAddr);

    #ifdef MAC_SECURITY
    /* Setup Coordinator short address for security */
    MAC_MlmeSetSecurityReq(MAC_PAN_COORD_SHORT_ADDRESS, &msa_AssociateReq.coordAddress.addr.shortAddr);
    #endif /* MAC_SECURITY */

    if (msa_BeaconOrder != 15)
    {
    /* Setup Beacon Order */
    MAC_MlmeSetReq(MAC_BEACON_ORDER, &msa_BeaconOrder);

    /* Setup Super Frame Order */
    MAC_MlmeSetReq(MAC_SUPERFRAME_ORDER, &msa_SuperFrameOrder);

    /* Sync request */
    MSA_SyncReq();
    }

    /* Power saving */
    MSA_PowerMgr (MSA_PWR_MGMT_ENABLED);

    }

    /**************************************************************************************************
    *
    * @fn MSA_AssociateReq()
    *
    * @brief
    *
    * @param None
    *
    * @return None
    *
    **************************************************************************************************/
    void MSA_AssociateReq(void)
    {
    MAC_MlmeAssociateReq(&msa_AssociateReq);
    }

    /**************************************************************************************************
    *
    * @fn MSA_AssociateRsp()
    *
    * @brief This routine is called by Associate_Ind inorder to return the response to the device
    *
    * @param pMsg - pointer to the structure recieved by MAC_MLME_ASSOCIATE_IND
    *
    * @return None
    *
    **************************************************************************************************/
    void MSA_AssociateRsp(macCbackEvent_t* pMsg)
    {

    // bool exist_node = false;
    // for(int No_of_Devices = 0; No_of_Devices < msa_NumOfDevices; No_of_Devices++){
    //
    //
    // sAddrExtCpy(msa_AssociateRsp.deviceAddress, pMsg->associateInd.deviceAddress);
    // msa_AssociateRsp.assocShortAddress = msa_DeviceRecord[No_of_Devices].devShortAddr;
    // msa_AssociateRsp.status = MAC_SUCCESS;
    // msa_AssociateRsp.sec.securityLevel = MAC_SEC_LEVEL_NONE;
    // exist_node = true;
    //
    // uint8 new_node_comd[5] = {0x02 ,0x4c, 0x00, 0x00, 0x03};
    // new_node_comd[2] = msa_DeviceRecord[No_of_Devices].devShortAddr >> 8;
    // new_node_comd[3] = msa_DeviceRecord[No_of_Devices].devShortAddr;
    // HalUARTWrite( 0, new_node_comd , 5);
    //
    // MAC_MlmeAssociateRsp(&msa_AssociateRsp);
    //
    //
    // }
    //
    // if(!exist_node){

    /* Assign the short address for the Device, from pool */
    uint16 assocShortAddress = msa_DevShortAddrList[msa_NumOfDevices];

    #ifdef MAC_SECURITY
    uint16 panID;

    /* Add device to device table for security */
    MAC_MlmeGetReq(MAC_PAN_ID, &panID);
    MSA_SecuredDeviceTableUpdate(panID, assocShortAddress,
    pMsg->associateInd.deviceAddress,
    &msa_NumOfSecuredDevices);
    #endif /* MAC_SECURITY */

    /* Build the record for this device */
    msa_DeviceRecord[msa_NumOfDevices].devShortAddr = msa_DevShortAddrList[msa_NumOfDevices];
    msa_DeviceRecord[msa_NumOfDevices].isDirectMsg = pMsg->associateInd.capabilityInformation & MAC_CAPABLE_RX_ON_IDLE;
    sAddrExtCpy(msa_DeviceRecord[msa_NumOfDevices].deviceAddress, pMsg->associateInd.deviceAddress);
    msa_NumOfDevices++;

    /* If the number of devices are more than MAX_DEVICE_NUM, turn off the association permit */
    if (msa_NumOfDevices == MSA_MAX_DEVICE_NUM)
    MAC_MlmeSetReq(MAC_ASSOCIATION_PERMIT, &msa_MACFalse);

    /* Fill in association respond message */
    sAddrExtCpy(msa_AssociateRsp.deviceAddress, pMsg->associateInd.deviceAddress);
    msa_AssociateRsp.assocShortAddress = assocShortAddress;
    msa_AssociateRsp.status = MAC_SUCCESS;

    msa_AssociateRsp.sec.securityLevel = MAC_SEC_LEVEL_NONE;

    uint8 new_node_comd[5] = {0x02 ,0x4c, 0x00, 0x00, 0x03};
    new_node_comd[2] = assocShortAddress >> 8;
    new_node_comd[3] = assocShortAddress;
    HalUARTWrite( 0, new_node_comd , 5);

    /* Call Associate Response */
    MAC_MlmeAssociateRsp(&msa_AssociateRsp);
    // }
    }

    /**************************************************************************************************
    *
    * @fn MSA_McpsDataReq()
    *
    * @brief This routine calls the Data Request
    *
    * @param data - contains the data that would be sent
    * dataLength - length of the data that will be sent
    *
    * @return None
    *
    **************************************************************************************************/
    void MSA_McpsDataReq(uint8* data, uint8 dataLength, bool directMsg, uint16 dstShortAddr)
    {
    macMcpsDataReq_t *pData;
    static uint8 handle = 0;

    if ((pData = MAC_McpsDataAlloc(dataLength, msa_securityLevel, msa_keyIdMode)) != NULL)
    {
    pData->mac.srcAddrMode = SADDR_MODE_SHORT;
    pData->mac.dstAddr.addrMode = SADDR_MODE_SHORT;
    pData->mac.dstAddr.addr.shortAddr = dstShortAddr;
    pData->mac.dstPanId = msa_PanId;
    pData->mac.msduHandle = handle++;
    pData->mac.txOptions = MAC_TXOPTION_ACK;

    /* MAC security parameters */
    osal_memcpy( pData->sec.keySource, msa_keySource, MAC_KEY_SOURCE_MAX_LEN );
    pData->sec.securityLevel = msa_securityLevel;
    pData->sec.keyIdMode = msa_keyIdMode;
    pData->sec.keyIndex = msa_keyIndex;

    /* If it's the coordinator and the device is in-direct message */
    if (msa_IsCoordinator)
    {
    if (!directMsg)
    {
    pData->mac.txOptions |= MAC_TXOPTION_INDIRECT;
    }
    }

    /* Copy data */
    osal_memcpy (pData->msdu.p, data, dataLength);

    /* Send out data request */
    MAC_McpsDataReq(pData);
    }

    }

    /**************************************************************************************************
    *
    * @fn MSA_McpsPollReq()
    *
    * @brief Performs a poll request on the coordinator
    *
    * @param None
    *
    * @return None
    *
    **************************************************************************************************/
    void MSA_McpsPollReq(void)
    {
    macMlmePollReq_t pollReq;

    /* Fill in information for poll request */
    pollReq.coordAddress.addrMode = SADDR_MODE_SHORT;
    pollReq.coordAddress.addr.shortAddr = msa_CoordShortAddr;
    pollReq.coordPanId = msa_PanId;
    pollReq.sec.securityLevel = MAC_SEC_LEVEL_NONE;

    /* Call poll reuqest */
    MAC_MlmePollReq(&pollReq);
    }

    /**************************************************************************************************
    *
    * @fn MacSampelApp_ScanReq()
    *
    * @brief Performs active scan on specified channel
    *
    * @param None
    *
    * @return None
    *
    **************************************************************************************************/
    void MSA_ScanReq(uint8 scanType, uint8 scanDuration)
    {
    macMlmeScanReq_t scanReq;

    /* Fill in information for scan request structure */
    scanReq.scanChannels = (uint32) 1 << MSA_MAC_CHANNEL;
    scanReq.scanType = scanType;
    scanReq.scanDuration = scanDuration;
    scanReq.maxResults = MSA_MAC_MAX_RESULTS;
    scanReq.permitJoining = MSA_EBR_PERMITJOINING;
    scanReq.linkQuality = MSA_EBR_LINKQUALITY;
    scanReq.percentFilter = MSA_EBR_PERCENTFILTER;
    scanReq.result.pPanDescriptor = msa_PanDesc;
    osal_memset(&scanReq.sec, 0, sizeof(macSec_t));

    /* Call scan request */
    MAC_MlmeScanReq(&scanReq);
    }

    /**************************************************************************************************
    *
    * @fn MSA_SyncReq()
    *
    * @brief Sync Request
    *
    * @param None
    *
    * @return None
    *
    **************************************************************************************************/
    void MSA_SyncReq(void)
    {
    macMlmeSyncReq_t syncReq;

    /* Fill in information for sync request structure */
    syncReq.logicalChannel = MSA_MAC_CHANNEL;
    syncReq.channelPage = MAC_CHANNEL_PAGE_0;
    syncReq.trackBeacon = TRUE;

    /* Call sync request */
    MAC_MlmeSyncReq(&syncReq);
    }

    /**************************************************************************************************
    *
    * @fn MSA_BeaconPayLoadCheck()
    *
    * @brief Check if the beacon comes from MSA but not zigbee
    *
    * @param pSdu - pointer to the buffer that contains the data
    *
    * @return TRUE or FALSE
    *
    **************************************************************************************************/
    bool MSA_BeaconPayLoadCheck(uint8* pSdu)
    {
    uint8 i = 0;
    for (i=0; i<msa_BeaconPayloadLen; i++)
    {
    if (pSdu[i] != msa_BeaconPayload[i])
    {
    return FALSE;
    }
    }

    return TRUE;
    }

    /**************************************************************************************************
    *
    * @fn MSA_DataCheck()
    *
    * @brief Check if the data match with the predefined data
    *
    * @param data - pointer to the buffer where the data will be checked against the predefined data
    * dataLength - length of the data
    *
    * @return TRUE if the data matched else it's the response / echo packet
    *
    **************************************************************************************************/
    bool MSA_DataCheck(uint8* data, uint8 dataLength)
    {
    uint8 i = 0;

    if (data[0] == dataLength)
    {
    for (i=MSA_HEADER_LENGTH; i<(data[0] - 1); i++)
    {
    if (data[i] != msa_Data1[i])
    return FALSE;
    }
    }
    else
    {
    return FALSE;
    }
    return TRUE;
    }

    /**************************************************************************************************
    *
    * @fn MSA_HandleKeys
    *
    * @brief Callback service for keys
    *
    * @param keys - keys that were pressed
    * state - shifted
    *
    * @return void
    *
    **************************************************************************************************/
    void MSA_HandleKeys(uint8 keys, uint8 shift)
    {


    if ( keys & HAL_KEY_SW_3 )
    {
    //HalLcdWriteScreen("CLEAR" , "ALL");
    //SPI_CleanAll();
    }

    if ( keys & HAL_KEY_SW_4 )
    {
    /*uint16 t_len;
    uint8 temp_buff1[12] = {0x01,0x54,0x2B,0x43,0x4D,0x47,0x44,0x3D,0x31,0x2C,0x34,0x0D};
    t_len=HalUARTWrite( 0, temp_buff1 , 12);
    HalLcdWriteStringValue( "Transmitted:",t_len,10, HAL_LCD_LINE_1 );
    delay(1);*/
    }





    }

    /**************************************************************************************************
    **************************************************************************************************/
    void delay(double delay){
    //this function will delay the system when called
    int long j = 0;
    for(j = 0; j < (delay*1000); j++){
    // halSleepWait(1000);
    }
    }

    void MT_UartProcessRxData(uint8 port, uint8 event)
    {

    //This function is the UART callback funtion.

    if (!GSM){
    HalUARTRead( 0, cmdbuff, 5);


    if (cmdbuff[0] == 0x08){
    HalLcdWriteString( "Reset UART", HAL_LCD_LINE_1 );
    }

    //switch(cmdbuff[0]){
    //case 0x02 :
    if (cmdbuff[1] == 'E'){

    HalLcdWriteString( "ECG Rx", HAL_LCD_LINE_1 );
    //Print the current data comand on the LCD screen and update the Data_type variable
    int tempvariable = cmdbuff[3];
    HalLcdWriteValue(tempvariable,10,HAL_LCD_LINE_2);
    Data_type = cmdbuff[1];
    }

    /*if (cmdbuff[1] == 0 && cmdbuff[2] == 1 && cmdbuff[3] == 2){
    //Resend the node list to the PC again
    HalLcdWriteString( "Refresh", HAL_LCD_LINE_1 );
    uint8 new_node_comd[4] = {0x02 ,0x4c, 0x00, 0x00};
    for (int z = 0; z < msa_NumOfDevices ;z++){
    new_node_comd[2] = msa_DeviceRecord[z].devShortAddr >> 8;
    new_node_comd[3] = msa_DeviceRecord[z].devShortAddr;
    HalUARTWrite( 0, new_node_comd , 4);
    }
    }*/
    //break;

    }


    }

    //UDP: User datagram Protocol
    /*
    void UDP_Send(uint8 size, uint8 *DataArray){
    uint8 output[100],temp_output[100];
    //for (int n = 0; n < 100;n++){
    //output[n] = 0;
    //temp_output[n] = 0;
    //}

    for(int i = 0; i < size; i++){
    uint8 fn = (*DataArray)/16;
    if(fn>9){
    fn = fn + 7;


    }
    char first = '0' + fn;
    temp_output[2*i] = first;
    uint8 sn = (*DataArray)%16;
    if(sn>9){
    sn = sn + 7;
    }
    char second = '0' + sn;
    temp_output[2*i + 1] = second;
    DataArray = DataArray + 1;
    }
    temp_output[size*2] = 0x03;
    output[0] = 'H'; //Header of the Packet
    for(int i = 1; i < (size*2+2); i++ ){
    output[i] = temp_output[i-1];

    }


    // doesnt output size of Array

    uint16 asize = sizeof(output);
    HalUARTWrite( 0,output,size*2+2 );



    }*/

    //automatically sets the device as either coordinator/node depending on the presence of coordinator/node
    void setCood(void){
    //MSA_DeviceStartup();
    HalLedBlink ( HAL_LED_1, 0, 50, 500 );
    if (!msa_IsStarted){
    /* Decide if direct or indirect messaging is used , set msa_IsDirectMsg to True */
    msa_IsDirectMsg = MSA_DIRECT_MSG_ENABLED;

    if (msa_IsDirectMsg)
    {
    /* Start the device as an DIRECT messaging device */
    if (msa_BeaconOrder != 15)
    MSA_ScanReq(MAC_SCAN_PASSIVE, MSA_MAC_BEACON_ORDER + 1);
    else
    MSA_ScanReq(MAC_SCAN_ACTIVE, 3);
    }
    else
    {
    /* Start the device as an INDIRECT messaging device and beacon disabled */
    /* Beacon network doesn't work with polling */
    if (!msa_IsStarted)
    {
    msa_IsDirectMsg = FALSE;
    MSA_ScanReq(MAC_SCAN_ACTIVE, 3);
    }
    }
    }
    }
  • **************************************************************************************************/

    /**************************************************************************************************

    Description:

    KEY UP (or S1)
    - Non Beacon
    - First board in the network will be setup as the coordinator
    - Any board after the first one will be setup as the device
    |
    KEY LEFT | KEY RIGHT(or S2)
    ----+----- - Start transmitting
    |
    |
    KEY DOWN



    **************************************************************************************************/


    /**************************************************************************************************
    * Includes
    **************************************************************************************************/
    //#define Debug_msa
    #ifdef Debug_msa
    #define Debug_msa_1
    #define Debug_msa_2
    #endif

    //general includes
    #include "string.h"
    /* Hal Driver includes */
    #include "hal_types.h"
    #include "hal_key.h"
    #include "hal_timer.h"
    #include "hal_drivers.h"
    #include "hal_led.h"
    #include "hal_adc.h"
    #include "hal_lcd.h"
    #include "hal_board_cfg.h"
    #include "hal_assert.h"
    #include "hal_uart.h"

    /* OS includes */
    #include "OSAL.h"
    #include "OSAL_Tasks.h"
    #include "OSAL_PwrMgr.h"

    /* Application Includes */
    #include "OnBoard.h"

    /* For Random numbers */
    #include "mac_radio_defs.h"

    /* MAC Application Interface */
    #include "mac_api.h"
    #include "mac_main.h"

    #ifdef MAC_SECURITY
    #include "mac_spec.h"

    /* MAC Sceurity */
    #include "mac_security_pib.h"
    #include "mac_security.h"
    #endif /* MAC_SECURITY */

    /* Application */
    #include "msa.h"

    /**************************************************************************************************
    * Constant
    **************************************************************************************************/

    #define MSA_DEV_SHORT_ADDR 0x0000 /* Device initial short address - This will change after association */

    #define MSA_HEADER_LENGTH 4 /* Header includes DataLength + DeviceShortAddr + Sequence */
    #define MSA_ECHO_LENGTH 8 /* Echo packet */

    #define MSA_MAC_MAX_RESULTS 5 /* Maximun number of scan result that will be accepted */
    #define MSA_MAX_DEVICE_NUM 10 /* Maximun number of devices can associate with the coordinator */

    #if defined (HAL_BOARD_CC2420DB)
    #define MSA_HAL_ADC_CHANNEL HAL_ADC_CHANNEL_0 /* AVR - Channel 0 and Resolution 10 */
    #define MSA_HAL_ADC_RESOLUTION HAL_ADC_RESOLUTION_10
    #elif defined (HAL_BOARD_DZ1611) || defined (HAL_BOARD_DZ1612) || defined (HAL_BOARD_DRFG4618) || defined (HAL_BOARD_F2618)
    #define MSA_HAL_ADC_CHANNEL HAL_ADC_CHANNEL_0 /* DZ1611 and DZ1612 - Channel 0 and Resolution 12 */
    #define MSA_HAL_ADC_RESOLUTION HAL_ADC_RESOLUTION_12
    #else
    #define MSA_HAL_ADC_CHANNEL HAL_ADC_CHANNEL_7 /* CC2430 EB & DB - Channel 7 and Resolution 14 */
    #define MSA_HAL_ADC_RESOLUTION HAL_ADC_RESOLUTION_14
    #endif

    #define MSA_EBR_PERMITJOINING TRUE
    #define MSA_EBR_LINKQUALITY 1
    #define MSA_EBR_PERCENTFILTER 0xFF

    #define Hal_ADC TRUE
    #define ENABLE_CS() {CS = 0;halSleepWait(1);}
    #define DISABLE_CS(x) {halSleepWait(1);CS = 1;halSleepWait(x);}

    /* Size table for MAC structures */
    const CODE uint8 msa_cbackSizeTable [] =
    {
    0, /* unused */
    sizeof(macMlmeAssociateInd_t), /* MAC_MLME_ASSOCIATE_IND */
    sizeof(macMlmeAssociateCnf_t), /* MAC_MLME_ASSOCIATE_CNF */
    sizeof(macMlmeDisassociateInd_t), /* MAC_MLME_DISASSOCIATE_IND */
    sizeof(macMlmeDisassociateCnf_t), /* MAC_MLME_DISASSOCIATE_CNF */
    sizeof(macMlmeBeaconNotifyInd_t), /* MAC_MLME_BEACON_NOTIFY_IND */
    sizeof(macMlmeOrphanInd_t), /* MAC_MLME_ORPHAN_IND */
    sizeof(macMlmeScanCnf_t), /* MAC_MLME_SCAN_CNF */
    sizeof(macMlmeStartCnf_t), /* MAC_MLME_START_CNF */
    sizeof(macMlmeSyncLossInd_t), /* MAC_MLME_SYNC_LOSS_IND */
    sizeof(macMlmePollCnf_t), /* MAC_MLME_POLL_CNF */
    sizeof(macMlmeCommStatusInd_t), /* MAC_MLME_COMM_STATUS_IND */
    sizeof(macMcpsDataCnf_t), /* MAC_MCPS_DATA_CNF */
    sizeof(macMcpsDataInd_t), /* MAC_MCPS_DATA_IND */
    sizeof(macMcpsPurgeCnf_t), /* MAC_MCPS_PURGE_CNF */
    sizeof(macEventHdr_t), /* MAC_PWR_ON_CNF */
    sizeof(macMlmePollInd_t) /* MAC_MLME_POLL_IND */
    };

    /**************************************************************************************************
    * Local Variables
    **************************************************************************************************/

    /*
    Extended address of the device, for coordinator, it will be msa_ExtAddr1
    For any device, it will be msa_ExtAddr2 with the last 2 bytes from a ADC read
    */
    sAddrExt_t msa_ExtAddr;
    sAddrExt_t msa_ExtAddr1 = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88};
    sAddrExt_t msa_ExtAddr2 = {0xA0, 0xB0, 0xC0, 0xD0, 0xE0, 0xF0, 0x00, 0x00};

    /* Coordinator and Device information */
    uint16 msa_PanId = MSA_PAN_ID;
    uint16 msa_CoordShortAddr = MSA_COORD_SHORT_ADDR;
    uint16 msa_DevShortAddr = MSA_DEV_SHORT_ADDR;

    /*
    List of short addresses that the coordinator will use
    to assign to each the device that associates to it
    */
    uint16 msa_DevShortAddrList[] = {0x0001, 0x0002, 0x0003, 0x0004, 0x0005,
    0x0006, 0x0007, 0x0008, 0x0009, 0x000A,
    0x000B, 0x000C, 0x000D, 0x000E, 0x000F,
    0x0010, 0x0011, 0x0012, 0x0013, 0x0014,
    0x0015, 0x0016, 0x0017, 0x0018, 0x0019,
    0x001A, 0x001B, 0x001C, 0x001D, 0x001E,
    0x001F, 0x0020, 0x0021, 0x0022, 0x0023,
    0x0024, 0x0025, 0x0026, 0x0027, 0x0028,
    0x0029, 0x002A, 0x002B, 0x002C, 0x002D,
    0x002E, 0x002F, 0x0030, 0x0031, 0x0032};

    /* Current number of devices associated to the coordinator */
    uint8 msa_NumOfDevices = 0;

    #ifdef MAC_SECURITY
    /* Current number of devices (including coordinator) communicating to this device */
    uint8 msa_NumOfSecuredDevices = 0;
    #endif /* MAC_SECURITY */

    /* Predefined packet that will be sent out by the coordinator */
    uint8 msa_Data1[MSA_PACKET_LENGTH];

    /* Predefined packet that will be sent back to the coordinator by the device */
    uint8 msa_Data2[MSA_ECHO_LENGTH];

    /* TRUE and FALSE value */
    bool msa_MACTrue = TRUE;
    bool msa_MACFalse = FALSE;

    /* Beacon payload, this is used to determine if the device is not zigbee device */
    uint8 msa_BeaconPayload[] = {0x22, 0x33, 0x44};
    uint8 msa_BeaconPayloadLen = 3;

    /* Contains pan descriptor results from scan */
    macPanDesc_t msa_PanDesc[MSA_MAC_MAX_RESULTS];

    /* flags used in the application */
    bool msa_IsCoordinator = FALSE; /* True if the device is started as a Pan Coordinate */
    bool msa_IsStarted = FALSE; /* True if the device started, either as Pan Coordinator or device */
    bool msa_IsSampleBeacon = FALSE; /* True if the beacon payload match with the predefined */
    bool msa_IsDirectMsg = FALSE; /* True if the messages will be sent as direct messages */
    uint8 msa_State = MSA_IDLE_STATE; /* Either IDLE state or SEND state */

    /* Structure that used for association request */
    macMlmeAssociateReq_t msa_AssociateReq;

    /* Structure that used for association response */
    macMlmeAssociateRsp_t msa_AssociateRsp;

    /* Structure that contains information about the device that associates with the coordinator */
    typedef struct
    {
    uint16 devShortAddr;
    uint8 isDirectMsg;
    sAddrExt_t deviceAddress;
    }msa_DeviceInfo_t;

    /* Array contains the information of the devices */
    static msa_DeviceInfo_t msa_DeviceRecord[MSA_MAX_DEVICE_NUM];

    uint8 msa_SuperFrameOrder;
    uint8 msa_BeaconOrder;

    /* Task ID */
    uint8 MSA_TaskId;

    /*Command Buffer for UART */
    uint8 cmdbuff[5];
    bool Debug = FALSE;
    char Data_type;
    bool GSM = FALSE;

    uint8 Sequence_number = 0;


    #ifdef MAC_SECURITY
    /**************************************************************************************************
    * MAC security related constants
    **************************************************************************************************/
    #define MSA_KEY_TABLE_ENTRIES 1
    #define MSA_KEY_ID_LOOKUP_ENTRIES 1
    #define MSA_KEY_DEVICE_TABLE_ENTRIES 8
    #define MSA_KEY_USAGE_TABLE_ENTRIES 1
    #define MSA_DEVICE_TABLE_ENTRIES 0 /* can grow up to MAX_DEVICE_TABLE_ENTRIES */
    #define MSA_SECURITY_LEVEL_ENTRIES 1
    #define MSA_MAC_SEC_LEVEL MAC_SEC_LEVEL_ENC_MIC_32
    #define MSA_MAC_KEY_ID_MODE MAC_KEY_ID_MODE_1
    #define MSA_MAC_KEY_SOURCE {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
    #define MSA_MAC_DEFAULT_KEY_SOURCE {0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33}
    #define MSA_MAC_KEY_INDEX 3 /* cannot be zero for implicit key identifier */


    /* Default MSA Security Parameters for outgoing frames */
    bool macSecurity = TRUE;
    uint8 msa_securityLevel = MSA_MAC_SEC_LEVEL;
    uint8 msa_keyIdMode = MSA_MAC_KEY_ID_MODE;
    uint8 msa_keySource[] = MSA_MAC_KEY_SOURCE;
    uint8 msa_keyIndex = MSA_MAC_KEY_INDEX;


    /**************************************************************************************************
    * Security PIBs for outgoing frames
    **************************************************************************************************/
    const keyIdLookupDescriptor_t msa_keyIdLookupList[] =
    {
    {
    {0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x03}, 0x01 /* index 3, 9 octets */
    }
    };

    /* Key device list can be modified at run time
    */
    keyDeviceDescriptor_t msa_keyDeviceList[] =
    {
    {0x00, false, false},
    {0x00, false, false},
    {0x00, false, false},
    {0x00, false, false},
    {0x00, false, false},
    {0x00, false, false},
    {0x00, false, false},
    {0x00, false, false}
    };

    const keyUsageDescriptor_t msa_keyUsageList[] =
    {
    {MAC_FRAME_TYPE_DATA, MAC_DATA_REQ_FRAME}
    };

    const keyDescriptor_t msa_keyTable[] =
    {
    {
    (keyIdLookupDescriptor_t *)msa_keyIdLookupList, MSA_KEY_ID_LOOKUP_ENTRIES,
    (keyDeviceDescriptor_t *)msa_keyDeviceList, MSA_KEY_DEVICE_TABLE_ENTRIES,
    (keyUsageDescriptor_t *)msa_keyUsageList, MSA_KEY_USAGE_TABLE_ENTRIES,
    {0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
    }
    };

    const uint8 msa_keyDefaultSource[] = MSA_MAC_DEFAULT_KEY_SOURCE;


    /**************************************************************************************************
    * Security PIBs for incoming frames
    **************************************************************************************************/

    /* Device table can be modified at run time. */
    deviceDescriptor_t msa_deviceTable[] =
    {
    {0, 0xFFFF, 0, 0, FALSE},
    {0, 0xFFFF, 0, 0, FALSE},
    {0, 0xFFFF, 0, 0, FALSE},
    {0, 0xFFFF, 0, 0, FALSE},
    {0, 0xFFFF, 0, 0, FALSE},
    {0, 0xFFFF, 0, 0, FALSE},
    {0, 0xFFFF, 0, 0, FALSE},
    {0, 0xFFFF, 0, 0, FALSE}
    };

    const securityLevelDescriptor_t msa_securityLevelTable[] =
    {
    {MAC_FRAME_TYPE_DATA, MAC_DATA_REQ_FRAME, MAC_SEC_LEVEL_NONE, FALSE}
    };
    #else /* MAC_SECURITY */
    uint8 msa_securityLevel = MAC_SEC_LEVEL_NONE;
    uint8 msa_keyIdMode = MAC_KEY_ID_MODE_NONE;
    uint8 msa_keySource[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
    uint8 msa_keyIndex = 0;
    #endif /* MAC_SECURITY */


    /**************************************************************************************************
    * Local Function Prototypes
    **************************************************************************************************/
    /* Setup routines */
    void MSA_CoordinatorStartup(void);
    void MSA_DeviceStartup(void);

    /* MAC related routines */
    void MSA_AssociateReq(void);
    void MSA_AssociateRsp(macCbackEvent_t* pMsg);
    void MSA_McpsDataReq(uint8* data, uint8 dataLength, bool directMsg, uint16 dstShortAddr);
    void MSA_McpsPollReq(void);
    void MSA_ScanReq(uint8 scanType, uint8 scanDuration);
    void MSA_SyncReq(void);

    /* Support */
    bool MSA_BeaconPayLoadCheck(uint8* pSdu);
    bool MSA_DataCheck(uint8* data, uint8 dataLength);

    void MT_UartProcessRxData(uint8 port, uint8 event);

    void setCood(void);
    void UART_setup(void);

    /*delay function (1 = 1 sec)*/
    void delay(double delay);
    //void UDP_Send(uint8 size, uint8 *DataArray);
    bool write_status = true;


    #ifdef MAC_SECURITY
    /**************************************************************************************************
    *
    * @fn MSA_SecurityInit
    *
    * @brief Initialize the security part of the application
    *
    * @param none
    *
    * @return none
    *
    **************************************************************************************************/
    static void MSA_SecurityInit(void)
    {
    uint8 keyTableEntries = MSA_KEY_TABLE_ENTRIES;
    uint8 autoRequestSecLevel = MAC_SEC_LEVEL_NONE;
    uint8 securityLevelEntries = MSA_SECURITY_LEVEL_ENTRIES;

    /* Write key table PIBs */
    MAC_MlmeSetSecurityReq(MAC_KEY_TABLE, (void *)msa_keyTable);
    MAC_MlmeSetSecurityReq(MAC_KEY_TABLE_ENTRIES, &keyTableEntries );

    /* Write default key source to PIB */
    MAC_MlmeSetSecurityReq(MAC_DEFAULT_KEY_SOURCE, (void *)msa_keyDefaultSource);

    /* Write security level table to PIB */
    MAC_MlmeSetSecurityReq(MAC_SECURITY_LEVEL_TABLE, (void *)msa_securityLevelTable);
    MAC_MlmeSetSecurityReq(MAC_SECURITY_LEVEL_TABLE_ENTRIES, &securityLevelEntries);

    /* TBD: MAC_AUTO_REQUEST is true on by default
    * need to set auto request security PIBs.
    * dieable auto request security for now
    */
    MAC_MlmeSetSecurityReq(MAC_AUTO_REQUEST_SECURITY_LEVEL, &autoRequestSecLevel);

    /* Turn on MAC security */
    MAC_MlmeSetReq(MAC_SECURITY_ENABLED, &macSecurity);
    }


    /**************************************************************************************************
    *
    * @fn MSA_SecuredDeviceTableUpdate
    *
    * @brief Update secured device table and key device descriptor handle
    *
    * @param panID - Secured network PAN ID
    * @param shortAddr - Other device's short address
    * @param extAddr - Other device's extended address
    * @param pNumOfSecuredDevices - pointer to number of secured devices
    *
    * @return none
    *
    **************************************************************************************************/
    static void MSA_SecuredDeviceTableUpdate(uint16 panID,
    uint16 shortAddr,
    sAddrExt_t extAddr,
    uint8 *pNumOfSecuredDevices)
    {
    /* Update key device list */
    msa_keyDeviceList[*pNumOfSecuredDevices].deviceDescriptorHandle = *pNumOfSecuredDevices;

    /* Update device table */
    msa_deviceTable[*pNumOfSecuredDevices].panID = panID;
    msa_deviceTable[*pNumOfSecuredDevices].shortAddress = shortAddr;
    sAddrExtCpy(msa_deviceTable[*pNumOfSecuredDevices].extAddress, extAddr);
    MAC_MlmeSetSecurityReq(MAC_DEVICE_TABLE, msa_deviceTable);

    /* Increase the number of secured devices */
    (*pNumOfSecuredDevices)++;
    MAC_MlmeSetSecurityReq(MAC_DEVICE_TABLE_ENTRIES, pNumOfSecuredDevices);
    }
    #endif /* MAC_SECURITY */


    /**************************************************************************************************
    *
    * @fn MSA_Init
    *
    * @brief Initialize the application
    *
    * @param taskId - taskId of the task after it was added in the OSAL task queue
    *
    * @return none
    *
    **************************************************************************************************/
    void MSA_Init(uint8 taskId)
    {
    uint8 i;

    /* Initialize the task id */
    MSA_TaskId = taskId;

    /* initialize MAC features */
    MAC_InitDevice();
    MAC_InitCoord();

    /* Initialize MAC beacon */
    MAC_InitBeaconDevice();
    MAC_InitBeaconCoord();

    /* Reset the MAC */
    MAC_MlmeResetReq(TRUE);

    #ifdef MAC_SECURITY
    /* Initialize the security part of the application */
    MSA_SecurityInit();
    #endif /* MAC_SECURITY */

    /* Initialize the data packet */
    for (i=MSA_HEADER_LENGTH; i<MSA_PACKET_LENGTH; i++)
    {
    msa_Data1[i] = i-MSA_HEADER_LENGTH;
    }

    /* Initialize the echo packet */
    for (i=0; i<MSA_ECHO_LENGTH; i++)
    {
    msa_Data2[i] = 0xEE;
    }

    msa_BeaconOrder = MSA_MAC_BEACON_ORDER;
    msa_SuperFrameOrder = MSA_MAC_SUPERFRAME_ORDER;

    //HalLedBlink (HAL_LED_2, 0, 40, 200);

    //SetCood is a custom function to make the coordinator automatically start up its own network
    //Previously, the default settings were to press the joystick on the SmartRF05EB to
    //make the coordinator start its own network (previously defined in MSA_HandleKeys)
    setCood();


    //MSA_DeviceStartup();
    /*
    Unnecessary to put this command here.
    MSA_DeviceStartup() gets called in the MAC_MLME_SCAN_CNF event handler.
    */

    }

    /**************************************************************************************************
    *
    * @fn MSA_ProcessEvent
    *
    * @brief This routine handles events
    *
    * @param taskId - ID of the application task when it registered with the OSAL
    * events - Events for this task
    *
    * @return 16bit - Unprocessed events
    *
    **************************************************************************************************/
    uint16 MSA_ProcessEvent(uint8 taskId, uint16 events)
    {
    uint8* pMsg;
    macCbackEvent_t* pData;
    const uint8 TestMarker[] = {0xDE, 0xAD,0xBE, 0xEF, 0xDE, 0xAD,0xBE, 0xEF};

    #ifdef MAC_SECURITY
    uint16 panID;
    uint16 panCoordShort;
    sAddrExt_t panCoordExtAddr;
    #endif /* MAC_SECURITY */

    static uint8 index;
    static uint8 sequence;

    if (events & SYS_EVENT_MSG)
    {
    while ((pMsg = osal_msg_receive(MSA_TaskId)) != NULL)
    {
    switch ( *pMsg )
    {
    case MAC_MLME_ASSOCIATE_IND:
    #ifdef Debug_msa_1
    HalUARTWrite( 0, "MAC_MLME_ASSOCIATE_IND \n\r", 30 );
    #endif
    MSA_AssociateRsp((macCbackEvent_t*)pMsg);
    break;

    case MAC_MLME_ASSOCIATE_CNF:
    /* Retrieve the message */
    #ifdef Debug_msa_1
    HalUARTWrite( 0, "MAC_MLME_ASSOCIATE_CNF \n\r", 30 );
    #endif

    if (Debug){HalUARTWrite( 0, "MAC_MLME_ASSOCIATE_CNF \n\r", 30 );}
    pData = (macCbackEvent_t *) pMsg;

    if ((!msa_IsStarted) && (pData->associateCnf.hdr.status == MAC_SUCCESS))
    {
    msa_IsStarted = TRUE;

    /* Retrieve MAC_SHORT_ADDRESS */

    msa_DevShortAddr = pData->associateCnf.assocShortAddress;

    /* Setup MAC_SHORT_ADDRESS - obtained from Association */
    MAC_MlmeSetReq(MAC_SHORT_ADDRESS, &msa_DevShortAddr);

    #ifdef MAC_SECURITY
    /* Add the coordinator to device table and key device table for security */
    MAC_MlmeGetReq(MAC_PAN_ID, &panID);
    MAC_MlmeGetSecurityReq(MAC_PAN_COORD_SHORT_ADDRESS, &panCoordShort);
    MAC_MlmeGetSecurityReq(MAC_PAN_COORD_EXTENDED_ADDRESS, &panCoordExtAddr);
    MSA_SecuredDeviceTableUpdate(panID, panCoordShort,
    panCoordExtAddr, &msa_NumOfSecuredDevices);
    #endif /* MAC_SECURITY */

    //HalLedBlink(HAL_LED_3, 0, 90, 1000);

    /* Poll for data if it's not setup for direct messaging */
    if (!msa_IsDirectMsg)
    {
    osal_start_timerEx(MSA_TaskId, MSA_POLL_EVENT, MSA_WAIT_PERIOD);
    }
    }
    break;

    case MAC_MLME_COMM_STATUS_IND:
    #ifdef Debug_msa_1
    HalUARTWrite( 0, "MAC_MLME_COMM_STATUS_IND \n\r", 30 );
    #endif
    break;

    case MAC_MLME_BEACON_NOTIFY_IND:
    /* Retrieve the message */
    pData = (macCbackEvent_t *) pMsg;

    /* Check for correct beacon payload */
    if (!msa_IsStarted)
    {
    msa_IsSampleBeacon = MSA_BeaconPayLoadCheck(pData->beaconNotifyInd.pSdu);

    /* If it's the correct beacon payload, retrieve the data for association req */
    if (msa_IsSampleBeacon)
    {
    msa_AssociateReq.logicalChannel = MSA_MAC_CHANNEL;
    msa_AssociateReq.coordAddress.addrMode = SADDR_MODE_SHORT;
    msa_AssociateReq.coordAddress.addr.shortAddr = pData->beaconNotifyInd.pPanDesc->coordAddress.addr.shortAddr;
    msa_AssociateReq.coordPanId = pData->beaconNotifyInd.pPanDesc->coordPanId;
    if (msa_IsDirectMsg)
    msa_AssociateReq.capabilityInformation = MAC_CAPABLE_ALLOC_ADDR | MAC_CAPABLE_RX_ON_IDLE;
    else
    msa_AssociateReq.capabilityInformation = MAC_CAPABLE_ALLOC_ADDR;
    msa_AssociateReq.sec.securityLevel = MAC_SEC_LEVEL_NONE;

    /* Retrieve beacon order and superframe order from the beacon */
    msa_BeaconOrder = MAC_SFS_BEACON_ORDER(pData->beaconNotifyInd.pPanDesc->superframeSpec);
    msa_SuperFrameOrder = MAC_SFS_SUPERFRAME_ORDER(pData->beaconNotifyInd.pPanDesc->superframeSpec);
    }
    }

    break;

    case MAC_MLME_START_CNF:
    #ifdef Debug_msa_1
    HalUARTWrite( 0, "MAC_MLME_START_CNF\n\r", 30 );
    #endif
    /* Retrieve the message */
    pData = (macCbackEvent_t *) pMsg;
    /* Set some indicator for the Coordinator */
    if ((!msa_IsStarted) && (pData->startCnf.hdr.status == MAC_SUCCESS))
    {
    msa_IsStarted = TRUE;
    msa_IsCoordinator = TRUE;
    HalLedSet (HAL_LED_1, HAL_LED_MODE_ON);
    }
    break;

    case MAC_MLME_SCAN_CNF:

    #ifdef Debug_msa_1
    HalUARTWrite( 0, "MAC_MLME_SCAN_CNF\n\r", 30 );
    #endif
    /* Check if there is any Coordinator out there */
    pData = (macCbackEvent_t *) pMsg;

    /* If there is no other on the channel or no other with sampleBeacon start as coordinator */
    if (((pData->scanCnf.resultListSize == 0) && (pData->scanCnf.hdr.status == MAC_NO_BEACON)) || (!msa_IsSampleBeacon))
    {
    MSA_CoordinatorStartup();
    uint8 uart_comd[3];
    uart_comd[0] = 0x02;
    uart_comd[1] = 0x05;
    uart_comd[2] = 0x03;
    HalUARTWrite( 0, uart_comd , 3);
    HalLcdWriteString( "PAN Coordinator", HAL_LCD_LINE_1 );
    delay(1);
    }

    else if ((msa_IsSampleBeacon) && pData->scanCnf.hdr.status == MAC_SUCCESS)
    {
    /* Start the devive up as beacon enabled or not */
    MSA_DeviceStartup();

    /* Call Associate Req */
    MSA_AssociateReq();
    }

    break;

    case MAC_MCPS_DATA_CNF:
    pData = (macCbackEvent_t *) pMsg;

    /* If last transmission completed, ready to send the next one */
    if ((pData->dataCnf.hdr.status == MAC_SUCCESS) ||
    (pData->dataCnf.hdr.status == MAC_CHANNEL_ACCESS_FAILURE) ||
    (pData->dataCnf.hdr.status == MAC_NO_ACK))
    {
    osal_start_timerEx(MSA_TaskId, MSA_SEND_EVENT, MSA_WAIT_PERIOD);
    }

    mac_msg_deallocate((uint8**)&pData->dataCnf.pDataReq);
    break;

    case MAC_MCPS_DATA_IND:
    #ifdef Debug_msa_1
    HalUARTWrite( 0, "MAC_MCPS_DATA_IND\n\r", 30 );
    #endif
    pData = (macCbackEvent_t*)pMsg;


    if (MSA_DataCheck ( pData->dataInd.msdu.p, pData->dataInd.msdu.len ))
    {
    //HalLedSet (HAL_LED_3, HAL_LED_MODE_TOGGLE);

    /* Only send the echo back if the received */
    if (!msa_IsCoordinator)
    {
    if (MSA_ECHO_LENGTH >= 4)
    {
    /* Return the first 4 bytes and the last byte of the received packet */
    msa_Data2[0] = pData->dataInd.msdu.p[0];
    msa_Data2[1] = pData->dataInd.msdu.p[1];
    msa_Data2[2] = pData->dataInd.msdu.p[2];
    msa_Data2[3] = pData->dataInd.msdu.p[3];
    }

    MSA_McpsDataReq(msa_Data2,
    MSA_ECHO_LENGTH,
    TRUE,
    msa_CoordShortAddr );
    }
    }

    if (Sequence_number == 0xFF){
    Sequence_number = 0x00; //Reset sequence number back to zero
    }




    /*-------------------- COORDINATOR RECEPTION --------------------

    Note(s): The coordinator has two tasks.
    1. Receive the array of sensor data from the node.
    2. Form an array containing the sensor data and outputting it to the serial
    connection to the computer.
    */





    //Check if the 4th byte of the application array sent represents a character 'E'.
    //Node should be coded to send 'E' as the 4th byte in the application array
    if (pData->dataInd.msdu.p[4] == 'E'){
    HalLedSet (HAL_LED_3, HAL_LED_MODE_TOGGLE);
    //Send ECG Data back to PC via UART
    Sequence_number++;
    //********************* CHANGE PACKET LENGTH HERE ******************************
    uint8 packetLength = 0x15;
    int packetLengthInt = 21;
    //********************* CHANGE PACKET LENGTH HERE ******************************

    //Form a new array consisting of the sensor data and send it to the computer
    //length of packet is also at DataFrame[1]
    uint8 DataFrame[21];
    DataFrame[0] = 0x01;
    DataFrame[1] = packetLength;
    osal_memcpy((void *)&(DataFrame[2]), (void *) TestMarker, 8);

    uint16 temp_short_add;
    // temp_short_add = pData->dataInd.msdu.p[14]; //Correct the packet which contains
    // //msa_DevShortAddress in SensorNode
    temp_short_add = pData->dataInd.mac.srcAddr.addr.shortAddr;
    for(int No_of_Devices = 0; No_of_Devices < msa_NumOfDevices; No_of_Devices++){
    if (msa_DeviceRecord[No_of_Devices].devShortAddr == temp_short_add){
    sAddrExt_t temp_device_ext;
    sAddrExtCpy(temp_device_ext, msa_DeviceRecord[No_of_Devices].deviceAddress);

    DataFrame[2] = temp_device_ext[7];
    DataFrame[3] = temp_device_ext[6];
    DataFrame[4] = temp_device_ext[5];
    DataFrame[5] = temp_device_ext[4];
    DataFrame[6] = temp_device_ext[3];
    DataFrame[7] = temp_device_ext[2];
    DataFrame[8] = temp_device_ext[1];
    DataFrame[9] = temp_device_ext[0];
    }
    }
    DataFrame[10] = Sequence_number;
    DataFrame[11] = 'E';
    DataFrame[12] = pData->dataInd.msdu.p[5];
    DataFrame[13] = pData->dataInd.msdu.p[6];
    DataFrame[14] = 'T';
    DataFrame[15] = pData->dataInd.msdu.p[8];
    DataFrame[16] = pData->dataInd.msdu.p[9];
    DataFrame[17] = 'P';
    DataFrame[18] = pData->dataInd.msdu.p[11];
    DataFrame[19] = pData->dataInd.msdu.p[12];
    DataFrame[20] = pData->dataInd.msdu.p[13];

    if (DataFrame[1] == packetLength) {
    //********************* CHANGE PACKET LENGTH HERE ******************************
    HalUARTWrite( 0, DataFrame , packetLengthInt);
    cmdbuff[0] = DataFrame[0];
    cmdbuff[1] = DataFrame[11];
    cmdbuff[2] = DataFrame[14];
    cmdbuff[3] = DataFrame[17];
    }






    /*-------------------- END OF COORDINATOR RECEPTION --------------------*/


    }
    break;
    }

    /* Deallocate */
    mac_msg_deallocate((uint8 **)&pMsg);
    }


    return events ^ SYS_EVENT_MSG;
    }

    /* This event handles polling for in-direct message device */
    if (events & MSA_POLL_EVENT)
    {
    MSA_McpsPollReq();
    osal_start_timerEx(MSA_TaskId, MSA_POLL_EVENT, MSA_WAIT_PERIOD);

    return events ^ MSA_POLL_EVENT;
    }

    /* This event will blink a LED then send out a DataReq and continue to do so every WAIT_PERIOD */
    if (events & MSA_SEND_EVENT)
    {

    /* Do it again */
    if (msa_State == MSA_SEND_STATE)
    {
    /* Start sending */
    if (msa_IsCoordinator)
    {
    if (MSA_PACKET_LENGTH >= MSA_HEADER_LENGTH)
    {
    /* Coordinator sending to devices. Use the associated list of device to send out msg */
    msa_Data1[0] = MSA_PACKET_LENGTH;
    msa_Data1[1] = HI_UINT16(msa_DeviceRecord[index].devShortAddr);
    msa_Data1[2] = LO_UINT16(msa_DeviceRecord[index].devShortAddr);
    msa_Data1[3] = sequence;
    }

    MSA_McpsDataReq((uint8*)msa_Data1,
    MSA_PACKET_LENGTH,
    msa_DeviceRecord[index].isDirectMsg,
    msa_DeviceRecord[index].devShortAddr );

    /* Reset the index if it reaches the current number of associated devices */
    if (++index == msa_NumOfDevices)
    {
    index = 0;
    }
    msa_State = MSA_IDLE_STATE;
    }
    else
    {
    if (MSA_PACKET_LENGTH >= MSA_HEADER_LENGTH)
    {
    /* Device sending to coordinator */
    msa_Data1[0] = MSA_PACKET_LENGTH;
    msa_Data1[1] = HI_UINT16(msa_CoordShortAddr);
    msa_Data1[2] = LO_UINT16(msa_CoordShortAddr);
    msa_Data1[3] = sequence;
    }
    MSA_McpsDataReq((uint8*)msa_Data1,
    MSA_PACKET_LENGTH,
    TRUE,
    msa_CoordShortAddr );

    }


    /* Reset the index if it reaches the current number of associated devices */
    if (++index == msa_NumOfDevices)
    {
    index = 0;
    }

    if (sequence++ == 0xFF)
    {
    sequence = 0;
    }

    //HalLedSet (HAL_LED_1, HAL_LED_MODE_BLINK);
    }

    return events ^ MSA_SEND_EVENT;
    }
    return 0;
    }

    /**************************************************************************************************
    *
    * @fn MAC_CbackEvent
    *
    * @brief This callback function sends MAC events to the application.
    * The application must implement this function. A typical
    * implementation of this function would allocate an OSAL message,
    * copy the event parameters to the message, and send the message
    * to the application's OSAL event handler. This function may be
    * executed from task or interrupt context and therefore must
    * be reentrant.
    *
    * @param pData - Pointer to parameters structure.
    *
    * @return None.
    *
    **************************************************************************************************/
    void MAC_CbackEvent(macCbackEvent_t *pData)
    {
    //HalLedSet (HAL_LED_2, HAL_LED_MODE_BLINK);
    macCbackEvent_t *pMsg = NULL;

    uint8 len = msa_cbackSizeTable[pData->hdr.event];

    switch (pData->hdr.event)
    {
    case MAC_MLME_BEACON_NOTIFY_IND:

    len += sizeof(macPanDesc_t) + pData->beaconNotifyInd.sduLength +
    MAC_PEND_FIELDS_LEN(pData->beaconNotifyInd.pendAddrSpec);
    if ((pMsg = (macCbackEvent_t *) osal_msg_allocate(len)) != NULL)
    {
    /* Copy data over and pass them up */
    osal_memcpy(pMsg, pData, sizeof(macMlmeBeaconNotifyInd_t));
    pMsg->beaconNotifyInd.pPanDesc = (macPanDesc_t *) ((uint8 *) pMsg + sizeof(macMlmeBeaconNotifyInd_t));
    osal_memcpy(pMsg->beaconNotifyInd.pPanDesc, pData->beaconNotifyInd.pPanDesc, sizeof(macPanDesc_t));
    pMsg->beaconNotifyInd.pSdu = (uint8 *) (pMsg->beaconNotifyInd.pPanDesc + 1);
    osal_memcpy(pMsg->beaconNotifyInd.pSdu, pData->beaconNotifyInd.pSdu, pData->beaconNotifyInd.sduLength);
    }
    break;

    case MAC_MCPS_DATA_IND:
    pMsg = pData;
    break;

    default:
    if ((pMsg = (macCbackEvent_t *) osal_msg_allocate(len)) != NULL)
    {
    osal_memcpy(pMsg, pData, len);
    }
    break;
    }

    if (pMsg != NULL)
    {
    osal_msg_send(MSA_TaskId, (uint8 *) pMsg);
    }
    }

    /**************************************************************************************************
    *
    * @fn MAC_CbackCheckPending
    *
    * @brief Returns the number of indirect messages pending in the application
    *
    * @param None
    *
    * @return Number of indirect messages in the application
    *
    **************************************************************************************************/
    uint8 MAC_CbackCheckPending(void)
    {
    return (0);
    }

    /**************************************************************************************************
    *
    * @fn MSA_CoordinatorStartup()
    *
    * @brief Update the timer per tick
    *
    * @param n/a
    *
    * @return None
    *
    **************************************************************************************************/
    void MSA_CoordinatorStartup()
    {
    macMlmeStartReq_t startReq;

    /* Setup MAC_EXTENDED_ADDRESS */
    sAddrExtCpy(msa_ExtAddr, msa_ExtAddr1);
    MAC_MlmeSetReq(MAC_EXTENDED_ADDRESS, &msa_ExtAddr);

    /* Setup MAC_SHORT_ADDRESS */
    MAC_MlmeSetReq(MAC_SHORT_ADDRESS, &msa_CoordShortAddr);

    /* Setup MAC_BEACON_PAYLOAD_LENGTH */
    MAC_MlmeSetReq(MAC_BEACON_PAYLOAD_LENGTH, &msa_BeaconPayloadLen);

    /* Setup MAC_BEACON_PAYLOAD */
    MAC_MlmeSetReq(MAC_BEACON_PAYLOAD, &msa_BeaconPayload);

    /* Enable RX */
    MAC_MlmeSetReq(MAC_RX_ON_WHEN_IDLE, &msa_MACTrue);

    /* Setup MAC_ASSOCIATION_PERMIT */
    MAC_MlmeSetReq(MAC_ASSOCIATION_PERMIT, &msa_MACTrue);

    /* Fill in the information for the start request structure */
    startReq.startTime = 0;
    startReq.panId = msa_PanId;
    startReq.logicalChannel = MSA_MAC_CHANNEL;
    startReq.beaconOrder = msa_BeaconOrder;
    startReq.superframeOrder = msa_SuperFrameOrder;
    startReq.panCoordinator = TRUE;
    startReq.batteryLifeExt = FALSE;
    startReq.coordRealignment = FALSE;
    startReq.realignSec.securityLevel = FALSE;
    startReq.beaconSec.securityLevel = FALSE;

    /* Call start request to start the device as a coordinator */
    MAC_MlmeStartReq(&startReq);
    HalLcdWriteString( "PAN Coordinator", HAL_LCD_LINE_1 );
    #ifdef SPI_On
    SPI_GetDeviceInfor();
    #endif

    /* Allow Beacon mode coordinator to sleep */
    if (msa_BeaconOrder != 15)
    {
    /* Power saving */
    MSA_PowerMgr (MSA_PWR_MGMT_ENABLED);
    }

    }

    /**************************************************************************************************
    *
    * @fn MSA_DeviceStartup()
    *
    * @brief Update the timer per tick
    *
    * @param beaconEnable: TRUE/FALSE
    *
    * @return None
    *
    **************************************************************************************************/
    void MSA_DeviceStartup()
    {
    uint16 AtoD = 0;

    #if (defined HAL_ADC) && (HAL_ADC == TRUE)
    AtoD = HalAdcRead (MSA_HAL_ADC_CHANNEL, MSA_HAL_ADC_RESOLUTION);
    #else
    AtoD = MAC_RADIO_RANDOM_WORD();
    #endif

    /*
    Setup MAC_EXTENDED_ADDRESS
    */
    #if defined (HAL_BOARD_CC2420DB) || defined (HAL_BOARD_DZ1611) || defined (HAL_BOARD_DZ1612) || \
    defined (HAL_BOARD_DRFG4618) || defined (HAL_BOARD_F2618)
    /* Use HI and LO of AtoD on CC2420 and MSP430 */
    msa_ExtAddr2[6] = HI_UINT16( AtoD );
    msa_ExtAddr2[7] = LO_UINT16( AtoD );
    #else
    /* On CC2430 and CC2530 use current MAC timer */
    AtoD = macMcuPrecisionCount();
    msa_ExtAddr2[6] = HI_UINT16( AtoD );
    msa_ExtAddr2[7] = LO_UINT16( AtoD );
    #endif

    sAddrExtCpy(msa_ExtAddr, msa_ExtAddr2);
    MAC_MlmeSetReq(MAC_EXTENDED_ADDRESS, &msa_ExtAddr);

    /* Setup MAC_BEACON_PAYLOAD_LENGTH */
    MAC_MlmeSetReq(MAC_BEACON_PAYLOAD_LENGTH, &msa_BeaconPayloadLen);

    /* Setup MAC_BEACON_PAYLOAD */
    MAC_MlmeSetReq(MAC_BEACON_PAYLOAD, &msa_BeaconPayload);

    /* Setup PAN ID */
    MAC_MlmeSetReq(MAC_PAN_ID, &msa_PanId);

    /* This device is setup for Direct Message */
    if (msa_IsDirectMsg)
    MAC_MlmeSetReq(MAC_RX_ON_WHEN_IDLE, &msa_MACTrue);
    else
    MAC_MlmeSetReq(MAC_RX_ON_WHEN_IDLE, &msa_MACFalse);

    /* Setup Coordinator short address */
    MAC_MlmeSetReq(MAC_COORD_SHORT_ADDRESS, &msa_AssociateReq.coordAddress.addr.shortAddr);

    #ifdef MAC_SECURITY
    /* Setup Coordinator short address for security */
    MAC_MlmeSetSecurityReq(MAC_PAN_COORD_SHORT_ADDRESS, &msa_AssociateReq.coordAddress.addr.shortAddr);
    #endif /* MAC_SECURITY */

    if (msa_BeaconOrder != 15)
    {
    /* Setup Beacon Order */
    MAC_MlmeSetReq(MAC_BEACON_ORDER, &msa_BeaconOrder);

    /* Setup Super Frame Order */
    MAC_MlmeSetReq(MAC_SUPERFRAME_ORDER, &msa_SuperFrameOrder);

    /* Sync request */
    MSA_SyncReq();
    }

    /* Power saving */
    MSA_PowerMgr (MSA_PWR_MGMT_ENABLED);

    }

    /**************************************************************************************************
    *
    * @fn MSA_AssociateReq()
    *
    * @brief
    *
    * @param None
    *
    * @return None
    *
    **************************************************************************************************/
    void MSA_AssociateReq(void)
    {
    MAC_MlmeAssociateReq(&msa_AssociateReq);
    }

    /**************************************************************************************************
    *
    * @fn MSA_AssociateRsp()
    *
    * @brief This routine is called by Associate_Ind inorder to return the response to the device
    *
    * @param pMsg - pointer to the structure recieved by MAC_MLME_ASSOCIATE_IND
    *
    * @return None
    *
    **************************************************************************************************/
    void MSA_AssociateRsp(macCbackEvent_t* pMsg)
    {

    // bool exist_node = false;
    // for(int No_of_Devices = 0; No_of_Devices < msa_NumOfDevices; No_of_Devices++){
    //
    //
    // sAddrExtCpy(msa_AssociateRsp.deviceAddress, pMsg->associateInd.deviceAddress);
    // msa_AssociateRsp.assocShortAddress = msa_DeviceRecord[No_of_Devices].devShortAddr;
    // msa_AssociateRsp.status = MAC_SUCCESS;
    // msa_AssociateRsp.sec.securityLevel = MAC_SEC_LEVEL_NONE;
    // exist_node = true;
    //
    // uint8 new_node_comd[5] = {0x02 ,0x4c, 0x00, 0x00, 0x03};
    // new_node_comd[2] = msa_DeviceRecord[No_of_Devices].devShortAddr >> 8;
    // new_node_comd[3] = msa_DeviceRecord[No_of_Devices].devShortAddr;
    // HalUARTWrite( 0, new_node_comd , 5);
    //
    // MAC_MlmeAssociateRsp(&msa_AssociateRsp);
    //
    //
    // }
    //
    // if(!exist_node){

    /* Assign the short address for the Device, from pool */
    uint16 assocShortAddress = msa_DevShortAddrList[msa_NumOfDevices];

    #ifdef MAC_SECURITY
    uint16 panID;

    /* Add device to device table for security */
    MAC_MlmeGetReq(MAC_PAN_ID, &panID);
    MSA_SecuredDeviceTableUpdate(panID, assocShortAddress,
    pMsg->associateInd.deviceAddress,
    &msa_NumOfSecuredDevices);
    #endif /* MAC_SECURITY */

    /* Build the record for this device */
    msa_DeviceRecord[msa_NumOfDevices].devShortAddr = msa_DevShortAddrList[msa_NumOfDevices];
    msa_DeviceRecord[msa_NumOfDevices].isDirectMsg = pMsg->associateInd.capabilityInformation & MAC_CAPABLE_RX_ON_IDLE;
    sAddrExtCpy(msa_DeviceRecord[msa_NumOfDevices].deviceAddress, pMsg->associateInd.deviceAddress);
    msa_NumOfDevices++;

    /* If the number of devices are more than MAX_DEVICE_NUM, turn off the association permit */
    if (msa_NumOfDevices == MSA_MAX_DEVICE_NUM)
    MAC_MlmeSetReq(MAC_ASSOCIATION_PERMIT, &msa_MACFalse);

    /* Fill in association respond message */
    sAddrExtCpy(msa_AssociateRsp.deviceAddress, pMsg->associateInd.deviceAddress);
    msa_AssociateRsp.assocShortAddress = assocShortAddress;
    msa_AssociateRsp.status = MAC_SUCCESS;

    msa_AssociateRsp.sec.securityLevel = MAC_SEC_LEVEL_NONE;

    uint8 new_node_comd[5] = {0x02 ,0x4c, 0x00, 0x00, 0x03};
    new_node_comd[2] = assocShortAddress >> 8;
    new_node_comd[3] = assocShortAddress;
    HalUARTWrite( 0, new_node_comd , 5);

    /* Call Associate Response */
    MAC_MlmeAssociateRsp(&msa_AssociateRsp);
    // }
    }

    /**************************************************************************************************
    *
    * @fn MSA_McpsDataReq()
    *
    * @brief This routine calls the Data Request
    *
    * @param data - contains the data that would be sent
    * dataLength - length of the data that will be sent
    *
    * @return None
    *
    **************************************************************************************************/
    void MSA_McpsDataReq(uint8* data, uint8 dataLength, bool directMsg, uint16 dstShortAddr)
    {
    macMcpsDataReq_t *pData;
    static uint8 handle = 0;

    if ((pData = MAC_McpsDataAlloc(dataLength, msa_securityLevel, msa_keyIdMode)) != NULL)
    {
    pData->mac.srcAddrMode = SADDR_MODE_SHORT;
    pData->mac.dstAddr.addrMode = SADDR_MODE_SHORT;
    pData->mac.dstAddr.addr.shortAddr = dstShortAddr;
    pData->mac.dstPanId = msa_PanId;
    pData->mac.msduHandle = handle++;
    pData->mac.txOptions = MAC_TXOPTION_ACK;

    /* MAC security parameters */
    osal_memcpy( pData->sec.keySource, msa_keySource, MAC_KEY_SOURCE_MAX_LEN );
    pData->sec.securityLevel = msa_securityLevel;
    pData->sec.keyIdMode = msa_keyIdMode;
    pData->sec.keyIndex = msa_keyIndex;

    /* If it's the coordinator and the device is in-direct message */
    if (msa_IsCoordinator)
    {
    if (!directMsg)
    {
    pData->mac.txOptions |= MAC_TXOPTION_INDIRECT;
    }
    }

    /* Copy data */
    osal_memcpy (pData->msdu.p, data, dataLength);

    /* Send out data request */
    MAC_McpsDataReq(pData);
    }

    }

    /**************************************************************************************************
    *
    * @fn MSA_McpsPollReq()
    *
    * @brief Performs a poll request on the coordinator
    *
    * @param None
    *
    * @return None
    *
    **************************************************************************************************/
    void MSA_McpsPollReq(void)
    {
    macMlmePollReq_t pollReq;

    /* Fill in information for poll request */
    pollReq.coordAddress.addrMode = SADDR_MODE_SHORT;
    pollReq.coordAddress.addr.shortAddr = msa_CoordShortAddr;
    pollReq.coordPanId = msa_PanId;
    pollReq.sec.securityLevel = MAC_SEC_LEVEL_NONE;

    /* Call poll reuqest */
    MAC_MlmePollReq(&pollReq);
    }

    /**************************************************************************************************
    *
    * @fn MacSampelApp_ScanReq()
    *
    * @brief Performs active scan on specified channel
    *
    * @param None
    *
    * @return None
    *
    **************************************************************************************************/
    void MSA_ScanReq(uint8 scanType, uint8 scanDuration)
    {
    macMlmeScanReq_t scanReq;

    /* Fill in information for scan request structure */
    scanReq.scanChannels = (uint32) 1 << MSA_MAC_CHANNEL;
    scanReq.scanType = scanType;
    scanReq.scanDuration = scanDuration;
    scanReq.maxResults = MSA_MAC_MAX_RESULTS;
    scanReq.permitJoining = MSA_EBR_PERMITJOINING;
    scanReq.linkQuality = MSA_EBR_LINKQUALITY;
    scanReq.percentFilter = MSA_EBR_PERCENTFILTER;
    scanReq.result.pPanDescriptor = msa_PanDesc;
    osal_memset(&scanReq.sec, 0, sizeof(macSec_t));

    /* Call scan request */
    MAC_MlmeScanReq(&scanReq);
    }

    /**************************************************************************************************
    *
    * @fn MSA_SyncReq()
    *
    * @brief Sync Request
    *
    * @param None
    *
    * @return None
    *
    **************************************************************************************************/
    void MSA_SyncReq(void)
    {
    macMlmeSyncReq_t syncReq;

    /* Fill in information for sync request structure */
    syncReq.logicalChannel = MSA_MAC_CHANNEL;
    syncReq.channelPage = MAC_CHANNEL_PAGE_0;
    syncReq.trackBeacon = TRUE;

    /* Call sync request */
    MAC_MlmeSyncReq(&syncReq);
    }

    /**************************************************************************************************
    *
    * @fn MSA_BeaconPayLoadCheck()
    *
    * @brief Check if the beacon comes from MSA but not zigbee
    *
    * @param pSdu - pointer to the buffer that contains the data
    *
    * @return TRUE or FALSE
    *
    **************************************************************************************************/
    bool MSA_BeaconPayLoadCheck(uint8* pSdu)
    {
    uint8 i = 0;
    for (i=0; i<msa_BeaconPayloadLen; i++)
    {
    if (pSdu[i] != msa_BeaconPayload[i])
    {
    return FALSE;
    }
    }

    return TRUE;
    }

    /**************************************************************************************************
    *
    * @fn MSA_DataCheck()
    *
    * @brief Check if the data match with the predefined data
    *
    * @param data - pointer to the buffer where the data will be checked against the predefined data
    * dataLength - length of the data
    *
    * @return TRUE if the data matched else it's the response / echo packet
    *
    **************************************************************************************************/
    bool MSA_DataCheck(uint8* data, uint8 dataLength)
    {
    uint8 i = 0;

    if (data[0] == dataLength)
    {
    for (i=MSA_HEADER_LENGTH; i<(data[0] - 1); i++)
    {
    if (data[i] != msa_Data1[i])
    return FALSE;
    }
    }
    else
    {
    return FALSE;
    }
    return TRUE;
    }

    /**************************************************************************************************
    *
    * @fn MSA_HandleKeys
    *
    * @brief Callback service for keys
    *
    * @param keys - keys that were pressed
    * state - shifted
    *
    * @return void
    *
    **************************************************************************************************/
    void MSA_HandleKeys(uint8 keys, uint8 shift)
    {


    if ( keys & HAL_KEY_SW_3 )
    {
    //HalLcdWriteScreen("CLEAR" , "ALL");
    //SPI_CleanAll();
    }

    if ( keys & HAL_KEY_SW_4 )
    {
    /*uint16 t_len;
    uint8 temp_buff1[12] = {0x01,0x54,0x2B,0x43,0x4D,0x47,0x44,0x3D,0x31,0x2C,0x34,0x0D};
    t_len=HalUARTWrite( 0, temp_buff1 , 12);
    HalLcdWriteStringValue( "Transmitted:",t_len,10, HAL_LCD_LINE_1 );
    delay(1);*/
    }





    }

    /**************************************************************************************************
    **************************************************************************************************/
    void delay(double delay){
    //this function will delay the system when called
    int long j = 0;
    for(j = 0; j < (delay*1000); j++){
    // halSleepWait(1000);
    }
    }

    void MT_UartProcessRxData(uint8 port, uint8 event)
    {

    //This function is the UART callback funtion.

    if (!GSM){
    HalUARTRead( 0, cmdbuff, 5);


    if (cmdbuff[0] == 0x08){
    HalLcdWriteString( "Reset UART", HAL_LCD_LINE_1 );
    }

    //switch(cmdbuff[0]){
    //case 0x02 :
    if (cmdbuff[1] == 'E'){

    HalLcdWriteString( "ECG Rx", HAL_LCD_LINE_1 );
    //Print the current data comand on the LCD screen and update the Data_type variable
    int tempvariable = cmdbuff[3];
    HalLcdWriteValue(tempvariable,10,HAL_LCD_LINE_2);
    Data_type = cmdbuff[1];
    }

    /*if (cmdbuff[1] == 0 && cmdbuff[2] == 1 && cmdbuff[3] == 2){
    //Resend the node list to the PC again
    HalLcdWriteString( "Refresh", HAL_LCD_LINE_1 );
    uint8 new_node_comd[4] = {0x02 ,0x4c, 0x00, 0x00};
    for (int z = 0; z < msa_NumOfDevices ;z++){
    new_node_comd[2] = msa_DeviceRecord[z].devShortAddr >> 8;
    new_node_comd[3] = msa_DeviceRecord[z].devShortAddr;
    HalUARTWrite( 0, new_node_comd , 4);
    }
    }*/
    //break;

    }


    }

    //UDP: User datagram Protocol
    /*
    void UDP_Send(uint8 size, uint8 *DataArray){
    uint8 output[100],temp_output[100];
    //for (int n = 0; n < 100;n++){
    //output[n] = 0;
    //temp_output[n] = 0;
    //}

    for(int i = 0; i < size; i++){
    uint8 fn = (*DataArray)/16;
    if(fn>9){
    fn = fn + 7;


    }
    char first = '0' + fn;
    temp_output[2*i] = first;
    uint8 sn = (*DataArray)%16;
    if(sn>9){
    sn = sn + 7;
    }
    char second = '0' + sn;
    temp_output[2*i + 1] = second;
    DataArray = DataArray + 1;
    }
    temp_output[size*2] = 0x03;
    output[0] = 'H'; //Header of the Packet
    for(int i = 1; i < (size*2+2); i++ ){
    output[i] = temp_output[i-1];

    }


    // doesnt output size of Array

    uint16 asize = sizeof(output);
    HalUARTWrite( 0,output,size*2+2 );



    }*/

    //automatically sets the device as either coordinator/node depending on the presence of coordinator/node
    void setCood(void){
    //MSA_DeviceStartup();
    HalLedBlink ( HAL_LED_1, 0, 50, 500 );
    if (!msa_IsStarted){
    /* Decide if direct or indirect messaging is used , set msa_IsDirectMsg to True */
    msa_IsDirectMsg = MSA_DIRECT_MSG_ENABLED;

    if (msa_IsDirectMsg)
    {
    /* Start the device as an DIRECT messaging device */
    if (msa_BeaconOrder != 15)
    MSA_ScanReq(MAC_SCAN_PASSIVE, MSA_MAC_BEACON_ORDER + 1);
    else
    MSA_ScanReq(MAC_SCAN_ACTIVE, 3);
    }
    else
    {
    /* Start the device as an INDIRECT messaging device and beacon disabled */
    /* Beacon network doesn't work with polling */
    if (!msa_IsStarted)
    {
    msa_IsDirectMsg = FALSE;
    MSA_ScanReq(MAC_SCAN_ACTIVE, 3);
    }
    }
    }
    }
  • Hi
    As mentioned before :

    1- The Node will receive the Data from outsource ( 2 wired data bus ) and transmit it to coordinator
    2- coordinator will receive the same data form the Node and will end it to outsource ( 2 wired data bus )

    I have got the folloing code for Coordinator and Node : would you please help me in case I need to change some parts,:

    Coordinator :


    /*-------------------- COORDINATOR RECEPTION --------------------

    Note(s): The coordinator has two tasks.
    1. Receive the data of sensor data from the node.
    2. Form a node containing the sensor data and outputting it to the outsource
    connection to the computer.
    */





    //Check if the 4th byte of the application array sent represents a character 'E'.
    //Node should be coded to send 'E' as the 4th byte in the application array
    if (pData->dataInd.msdu.p[4] == 'E'){
    HalLedSet (HAL_LED_3, HAL_LED_MODE_TOGGLE);
    //Send ECG Data back to PC via UART
    Sequence_number++;
    //********************* CHANGE PACKET LENGTH HERE ******************************
    uint8 packetLength = 0x15;
    int packetLengthInt = 21;
    //********************* CHANGE PACKET LENGTH HERE ******************************

    //Form a new array consisting of the sensor data and send it to the computer
    //length of packet is also at DataFrame[1]
    uint8 DataFrame[21];
    DataFrame[0] = 0x01;
    DataFrame[1] = packetLength;
    osal_memcpy((void *)&(DataFrame[2]), (void *) TestMarker, 8);

    uint16 temp_short_add;
    // temp_short_add = pData->dataInd.msdu.p[14]; //Correct the packet which contains
    // //msa_DevShortAddress in SensorNode
    temp_short_add = pData->dataInd.mac.srcAddr.addr.shortAddr;
    for(int No_of_Devices = 0; No_of_Devices < msa_NumOfDevices; No_of_Devices++){
    if (msa_DeviceRecord[No_of_Devices].devShortAddr == temp_short_add){
    sAddrExt_t temp_device_ext;
    sAddrExtCpy(temp_device_ext, msa_DeviceRecord[No_of_Devices].deviceAddress);

    DataFrame[2] = temp_device_ext[7];
    DataFrame[3] = temp_device_ext[6];
    DataFrame[4] = temp_device_ext[5];
    DataFrame[5] = temp_device_ext[4];
    DataFrame[6] = temp_device_ext[3];
    DataFrame[7] = temp_device_ext[2];
    DataFrame[8] = temp_device_ext[1];
    DataFrame[9] = temp_device_ext[0];
    }
    }
    DataFrame[10] = Sequence_number;
    DataFrame[11] = 'E';
    DataFrame[12] = pData->dataInd.msdu.p[5];
    DataFrame[13] = pData->dataInd.msdu.p[6];
    DataFrame[14] = 'T';
    DataFrame[15] = pData->dataInd.msdu.p[8];
    DataFrame[16] = pData->dataInd.msdu.p[9];
    DataFrame[17] = 'P';
    DataFrame[18] = pData->dataInd.msdu.p[11];
    DataFrame[19] = pData->dataInd.msdu.p[12];
    DataFrame[20] = pData->dataInd.msdu.p[13];

    if (DataFrame[1] == packetLength) {
    //********************* CHANGE PACKET LENGTH HERE ******************************
    HalUARTWrite( 0, DataFrame , packetLengthInt);
    cmdbuff[0] = DataFrame[0];
    cmdbuff[1] = DataFrame[11];
    cmdbuff[2] = DataFrame[14];
    cmdbuff[3] = DataFrame[17];
    }






    /*-------------------- END OF COORDINATOR RECEPTION --------------------*/


    }
    break;
    }

    /* Deallocate */
    mac_msg_deallocate((uint8 **)&pMsg);
    }


    return events ^ SYS_EVENT_MSG;
    }

    /* This event handles polling for in-direct message device */
    if (events & MSA_POLL_EVENT)
    {
    MSA_McpsPollReq();
    osal_start_timerEx(MSA_TaskId, MSA_POLL_EVENT, MSA_WAIT_PERIOD);

    return events ^ MSA_POLL_EVENT;
    }

    /* This event will blink a LED then send out a DataReq and continue to do so every WAIT_PERIOD */
    if (events & MSA_SEND_EVENT)
    {

    /* Do it again */
    if (msa_State == MSA_SEND_STATE)
    {
    /* Start sending */
    if (msa_IsCoordinator)
    {
    if (MSA_PACKET_LENGTH >= MSA_HEADER_LENGTH)
    {
    /* Coordinator sending to devices. Use the associated list of device to send out msg */
    msa_Data1[0] = MSA_PACKET_LENGTH;
    msa_Data1[1] = HI_UINT16(msa_DeviceRecord[index].devShortAddr);
    msa_Data1[2] = LO_UINT16(msa_DeviceRecord[index].devShortAddr);
    msa_Data1[3] = sequence;
    }

    MSA_McpsDataReq((uint8*)msa_Data1,
    MSA_PACKET_LENGTH,
    msa_DeviceRecord[index].isDirectMsg,
    msa_DeviceRecord[index].devShortAddr );

    /* Reset the index if it reaches the current number of associated devices */
    if (++index == msa_NumOfDevices)
    {
    index = 0;
    }
    msa_State = MSA_IDLE_STATE;
    }
    else
    {
    if (MSA_PACKET_LENGTH >= MSA_HEADER_LENGTH)
    {
    /* Device sending to coordinator */
    msa_Data1[0] = MSA_PACKET_LENGTH;
    msa_Data1[1] = HI_UINT16(msa_CoordShortAddr);
    msa_Data1[2] = LO_UINT16(msa_CoordShortAddr);
    msa_Data1[3] = sequence;
    }
    MSA_McpsDataReq((uint8*)msa_Data1,
    MSA_PACKET_LENGTH,
    TRUE,
    msa_CoordShortAddr );

    }


    /* Reset the index if it reaches the current number of associated devices */
    if (++index == msa_NumOfDevices)
    {
    index = 0;
    }

    if (sequence++ == 0xFF)
    {
    sequence = 0;
    }

    //HalLedSet (HAL_LED_1, HAL_LED_MODE_BLINK);
    }

    return events ^ MSA_SEND_EVENT;
    }
    return 0;
    }
  • Hi
    I was trying to copy the code here but seesm it is not possible ,

    my email is: houshyar_honarpajooh @ yahoo.com , if you could send me an email I will forward the codes

    Thanks
  • I suggest you use a simple UART example to make sure your UART work first.
  • could you please share the simple URAT example with me ? Thank you
  • Do you use Z-Stack?
  • no it was not working , and I will just use IEEE 802.15.4 not Zegbee
  • Do you mean you use TIMAC?
  • Yes I have to use TIMAC , but I am new with micro controller and I don't know how to write a code to use UART for both coordinator and node to communicate with each other and also other device as well