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.

CC2530 TIMAC Example Project Stops working after some time

Other Parts Discussed in Thread: TIMAC, CC2530, CC2420

Hi,

I am trying to modify the TIMAC example program for CC2530. The demo program works for some time, but then the boards stop responding and the control is sometimes shifted to the Hazard lights function. I have debugged the problem without success so far.

I am not clear on the memory allocation and de-allocation aspects of this sample program. SPecifically, I think the memory de-allocation is not occuring correctly in the sample program. Unfortunately, the changes I have made seem to prolong the life of the example program, but the problem still lives.

Please see the modified process hal loop in the demo program. I have modified this to add two events to meet my processing needs. I have tried to correct the memory leak, but seems this needs further inputs. Can somebody please help me sort this issue.

Also, if there is some other documentation on the TIMAC besides the PDFs that ship with the MAC in the documents folder, I would be grateful if that can be shared, since this documentation is fairly sketchy and limited in terms of information.

Br

Anadi

uint16 MSA_ProcessEvent(uint8 taskId, uint16 events)
{
  uint8* pMsg;
  macCbackEvent_t* pData;
  static uint8 index;
  static uint8 sequence;
  static uint8 beaconNotifyCnt = 0;
  static uint8 scanCompleteCnt = 0;
  static uint8 associateCnt = 0;
  uint8 led_tmp_blnk_cnt = 0;

  if (events & SYS_EVENT_MSG)
  {
    while ((pMsg = osal_msg_receive(MSA_TaskId)) != NULL)
    {
      switch ( *pMsg )
      {
        case MAC_MLME_ASSOCIATE_IND:
          MSA_AssociateRsp((macCbackEvent_t*)pMsg);
 
          break;

        case MAC_MLME_ASSOCIATE_CNF:
          /* Retrieve the message */
          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);

            /* Poll for data if it's not setup for direct messaging */
            for(led_tmp_blnk_cnt = 0; led_tmp_blnk_cnt < 2; led_tmp_blnk_cnt++)
            {
               HAL_TURN_ON_LED3();
               HAL_TURN_ON_LED1();
               HAL_LED_BLINK_DELAY();
               HAL_TURN_OFF_LED3();
               HAL_TURN_OFF_LED1();
               HAL_LED_BLINK_DELAY();
            }
           
            /* Control Node is found. From now onwards, ping it */
            osal_start_timerEx(MSA_TaskId, MSA_PING_CN_EVENT, MSA_PING_CN_PERIOD);
           
          }
          break;

        case MAC_MLME_COMM_STATUS_IND:
          break;

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

          /* 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_SYNC_LOSS_IND:
       
        osal_start_timerEx(MSA_TaskId, MSA_SEARCH_CN_EVENT, MSA_SEARCH_CN_PERIOD);
       
        break;

        case MAC_MLME_START_CNF:
          /* 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;
          }
          break;

        case MAC_MLME_SCAN_CNF:
          /* Check if there is any Coordinator out there */
          pData = (macCbackEvent_t *) pMsg;
          scanCompleteCnt++;

          if ((msa_IsSampleBeacon) && pData->scanCnf.hdr.status == MAC_SUCCESS)
          {
            /* Control Node found */
            /* Call Associate Req */
            associateCnt++;
            MSA_AssociateReq();
          }
          else
          {
            /* Not able to find Control Node, go to sleep */
            osal_start_timerEx(MSA_TaskId, MSA_SEARCH_CN_EVENT, MSA_SEARCH_CN_PERIOD);           
          }
         
          for(led_tmp_blnk_cnt = 0; led_tmp_blnk_cnt < 1; led_tmp_blnk_cnt++)
          {
               HAL_TURN_ON_LED3();
               HAL_LED_BLINK_DELAY();              
               HAL_TURN_OFF_LED3();
               HAL_LED_BLINK_DELAY();              
          }
         
          break;

        case MAC_MCPS_DATA_CNF:
          pData = (macCbackEvent_t *) pMsg;

          /*
          if ((pData->dataCnf.hdr.status == MAC_SUCCESS) ||
              (pData->dataCnf.hdr.status == MAC_CHANNEL_ACCESS_FAILURE) ||
              (pData->dataCnf.hdr.status == MAC_NO_ACK))
          */
          if ((pData->dataCnf.hdr.status == MAC_SUCCESS))
          {
            /* If ping was successfully sent, go to sleep */
            osal_start_timerEx(MSA_TaskId, MSA_PING_CN_EVENT, MSA_PING_CN_PERIOD);
                       
            for(led_tmp_blnk_cnt = 0; led_tmp_blnk_cnt < 1; led_tmp_blnk_cnt++)
            {
               HAL_TURN_ON_LED3();
               HAL_LED_BLINK_DELAY();    
               HAL_TURN_ON_LED1();
               HAL_LED_BLINK_DELAY();        
               HAL_TURN_OFF_LED1();
               HAL_LED_BLINK_DELAY();  
               HAL_TURN_OFF_LED3();
               HAL_LED_BLINK_DELAY();            
            }
          }
          else
          {
            /* If ping was not sent successfully, Control Node is lost */
           
            msa_IsStarted = FALSE; /* Device is no longer connected */
           
            /* Manual */
            for(led_tmp_blnk_cnt = 0; led_tmp_blnk_cnt < 2; led_tmp_blnk_cnt++)
            {
               HAL_TURN_ON_LED3();
               HAL_LED_BLINK_DELAY();
               HAL_LED_BLINK_DELAY();              
               HAL_TURN_OFF_LED3();
               HAL_LED_BLINK_DELAY();
               HAL_LED_BLINK_DELAY();
            }
           
            /* Search for device */
            osal_start_timerEx(MSA_TaskId, MSA_SEARCH_CN_EVENT, MSA_SEARCH_CN_PERIOD);
          }

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

        case MAC_MCPS_DATA_IND:
          pData = (macCbackEvent_t*)pMsg;

          if (MSA_DataCheck ( pData->dataInd.msdu.p, pData->dataInd.msdu.len ))
          {
            HalLedSet (HAL_LED_1, 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 );
            }
          }
         
          /* Added in case the allocation is from MAC - Verify this */
          mac_msg_deallocate((uint8 **)&pMsg);
         
          break;
      }

      /* Should it be MAC or OSAL ??? */
      /* Deallocate */
      //mac_msg_deallocate((uint8 **)&pMsg);
      if(pMsg != NULL)
      {
          osal_msg_deallocate(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;
        }
      }
      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 );

      }

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

    return events ^ MSA_SEND_EVENT;
  }
 
  /* Search for a Control Node */
  if (events & MSA_SEARCH_CN_EVENT)
  {
    for(led_tmp_blnk_cnt = 0; led_tmp_blnk_cnt < 2; led_tmp_blnk_cnt++)
    {
        HAL_TURN_ON_LED1();
        HAL_LED_BLINK_DELAY();
        HAL_TURN_OFF_LED1();
        HAL_LED_BLINK_DELAY();
    }
   
    MSA_ScanReq(MAC_SCAN_PASSIVE, MSA_MAC_BEACON_ORDER + 1);

    return events ^ MSA_SEARCH_CN_EVENT;
  }
 
  /* Ping Control Node */
  if (events & MSA_PING_CN_EVENT)
  {
    /*Send ping message request */
   
    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);
     
      /*@TODO: fill sequence */
      //msa_Data1[3] = sequence;
    }
   
    for(led_tmp_blnk_cnt = 0; led_tmp_blnk_cnt < 2; led_tmp_blnk_cnt++)
    {
        HAL_TURN_ON_LED3();
        HAL_TURN_ON_LED1();              
        HAL_LED_BLINK_DELAY();
        HAL_TURN_OFF_LED3();
        HAL_TURN_OFF_LED1();
        HAL_LED_BLINK_DELAY();              
    }
   
    MSA_McpsDataReq((uint8*)msa_Data1,
                    MSA_PACKET_LENGTH,
                    TRUE,
                    msa_CoordShortAddr );
   
    /* Also need to ensure that if there is an acknowledged mode in use, then
       there is a time out and purging of the data request */

    return events ^ MSA_PING_CN_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)
{

  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   beaconEnable: TRUE/FALSE
 *
 * @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);

}

/**************************************************************************************************
 *
 * @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);
#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 2430 Use the MSB to ensure the stability */
  msa_ExtAddr2[6] = HI_UINT16( AtoD ) >> 4;
  msa_ExtAddr2[7] = HI_UINT16( AtoD ) >> 3;
#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);

  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();
  }

}

/**************************************************************************************************
 *
 * @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)
{
  /* Assign the short address  for the Device, from pool */
  uint16 assocShortAddress = msa_DevShortAddrList[msa_NumOfDevices];

  /* 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;
  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;

  /* 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, MAC_SEC_LEVEL_NONE, MAC_KEY_ID_MODE_NONE)) != 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;
    pData->mac.txOptions = MAC_TXOPTION_NO_RETRANS;

    /* 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.result.pPanDescriptor = msa_PanDesc;

  /* 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;
  syncReq.trackBeacon    = FALSE;

  /* 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_1 )
  {
    /* Start the device as a direct message device and beacon disabled*/
    if (!msa_IsStarted)
    {
      /* Decide if direct or indirect messaging is used */
      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);
         }
      }
    }
  }

  if ( keys & HAL_KEY_SW_2 )
  {
    /* Start sending message */
    if (msa_IsStarted)
    {
      /* Set App's state to SEND or IDLE */
      if (msa_State == MSA_IDLE_STATE)
        msa_State = MSA_SEND_STATE;
      else
        msa_State = MSA_IDLE_STATE;

      /* Send Event to the App to carry out the changes */
      osal_start_timerEx(MSA_TaskId, MSA_SEND_EVENT, 100);
    }
  }
}

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

  • Sorry missed on the complete description ... Actually, the memory allocation mechanism needs a little bit of explanation. 1. Why is mac_msg_deallocate((uint8 **)&pMsg); used to deallocate all memory allocations? 2. Isn't osal_msg_deallocate(pMsg); required to deallocate the memory allocation from using osal from the cback function? 3. What is the expected behaviour in case the data transmission fails due to the co-ordinator moving out of range? In the present code, in this scenario, the whole code stops to work completely!!! Br Anadi
  • Has this question been answered (osal_msg_deallocate vs. mac_msg_deallocate)?  I have the same concerns.  Thank you.