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.

Using Simple Peer to Peer code example to monitor RSSI value

I am using the example code of simple peer to peer to monitor the RSSI value recieved from a CC2510DK-MINI device and then when this device falls below a certain RSSI value I will then send a warning signal to the user. Preferably a buzzer connected to a port on the device. There is also a need for the transmitter to only transmit every 2 seconds. So with this there are two alterations I need to consider. My first question is where would the delay need to be inserted to make sure a signal is only sent every 2 seconds.

/* delay longer and longer -- then start over */
    delay = (delay+1) & 0x03;
    /* put the sequence ID in the message */
    msg[1] = ++sTxTid;
    SMPL_Send(sLinkID1, msg, sizeof(msg));
  }
}

Would it be sufficient to insert the delay in the line just before the SMPL_Send function shown above?

The other question is how would I be able to read the RSSI value interpret it and compare it against a predetermined threshold in the LinkListen program? Would it need to be placed into the below if statement function? 

 

 if (sSemaphore)
     {
       *(msg+1) = ++tid;
       SMPL_Send(sLinkID2, msg, 2);

       /* Reset semaphore. This is not properly protected and there is a race
        * here. In theory we could miss a message. Good enough for a demo, though.
        */
       sSemaphore = 0;
     }

Any help would be greatly appreciated

MH

  • Here is the full code for both programs.

    LinkTo:

    /*----------------------------------------------------------------------------
     *  Demo Application for SimpliciTI 
     * 
     *  L. Friedman 
     *  Texas Instruments, Inc.
     *---------------------------------------------------------------------------- */
    
    /********************************************************************************************
      Copyright 2007-2009 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.
    **************************************************************************************************/
    
    #include "bsp.h"
    #include "mrfi.h"
    #include "nwk_types.h"
    #include "nwk_api.h"
    #include "bsp_leds.h"
    #include "bsp_buttons.h"
    #include "nwk_pll.h"
    
    static void linkTo(void);
    
    void toggleLED(uint8_t);
    
    static uint8_t  sTxTid=0, sRxTid=0;
    static linkID_t sLinkID1 = 0;
    
    /* application Rx frame handler. */
    static uint8_t sRxCallback(linkID_t);
    
    /* For FHSS systems, calls to NWK_DELAY() will also call nwk_pllBackgrounder()
     * during the delay time so if you use the system delay mechanism in a loop,
     * you don't need to also call the nwk_pllBackgrounder() function.
     */
    #define SPIN_ABOUT_A_SECOND  NWK_DELAY(1000)
    
    void main (void)
    {
      BSP_Init();
    
      /* If an on-the-fly device address is generated it must be done before the
       * call to SMPL_Init(). If the address is set here the ROM value will not 
       * be used. If SMPL_Init() runs before this IOCTL is used the IOCTL call 
       * will not take effect. One shot only. The IOCTL call below is conformal. 
       */
    #ifdef I_WANT_TO_CHANGE_DEFAULT_ROM_DEVICE_ADDRESS_PSEUDO_CODE
      {
        addr_t lAddr;
    
        createRandomAddress(&lAddr);
        SMPL_Ioctl(IOCTL_OBJ_ADDR, IOCTL_ACT_SET, &lAddr);
      }
    #endif /* I_WANT_TO_CHANGE_DEFAULT_ROM_DEVICE_ADDRESS_PSEUDO_CODE */
    
      /* On FHSS systems the call to SMPL_Init will not return until we have
       * locked onto a reference clock.  Also, on return the radio will always
       * be on in receive mode (at least for now).
       */
      
      /* This call will fail because the join will fail since there is no Access Point 
       * in this scenario. But we don't care -- just use the default link token later. 
       * We supply a callback pointer to handle the message returned by the peer. 
       */
      SMPL_Init(sRxCallback);
    
      /* turn on LEDs. */
      if (!BSP_LED2_IS_ON())
      {
        toggleLED(2);
      }
      if (!BSP_LED1_IS_ON())
      {
        toggleLED(1);
      }
    
      /* wait for a button press... */
      do {
        FHSS_ACTIVE( nwk_pllBackgrounder( false ) ); /* manage FHSS */
        if (BSP_BUTTON1() || BSP_BUTTON2())
        {
          break;
        }
      } while (1);
    
      /* never coming back... */
      linkTo();
    
      /* but in case we do... */
      while (1) ;
    }
    
    static void linkTo()
    {
      uint8_t  msg[2], delay = 0;
    
      while (SMPL_SUCCESS != SMPL_Link(&sLinkID1))
      {
        /* blink LEDs until we link successfully */
        toggleLED(1);
        toggleLED(2);
        SPIN_ABOUT_A_SECOND; /* manages FHSS implicitly */
      }
    
      /* we're linked. turn off red LED. received messages will toggle the green LED. */
      if (BSP_LED2_IS_ON())
      {
        toggleLED(2);
      }
    
    #ifndef FREQUENCY_HOPPING
      /* turn on RX. default is RX off. */
      SMPL_Ioctl( IOCTL_OBJ_RADIO, IOCTL_ACT_RADIO_RXON, 0);
    #endif
      
      /* put LED to toggle in the message */
      msg[0] = 2;  /* toggle red */
      while (1)
      {
        SPIN_ABOUT_A_SECOND; /* manages FHSS implicitly */
        if (delay > 0x00)
        {
          SPIN_ABOUT_A_SECOND; /* manages FHSS implicitly */
        }
        if (delay > 0x01)
        {
          SPIN_ABOUT_A_SECOND; /* manages FHSS implicitly */
        }
        if (delay > 0x02)
        {
          SPIN_ABOUT_A_SECOND; /* manages FHSS implicitly */
        }
    
        /* delay longer and longer -- then start over */
        delay = (delay+1) & 0x03;
        /* put the sequence ID in the message */
        msg[1] = ++sTxTid;
        SMPL_Send(sLinkID1, msg, sizeof(msg));
      }
    }
    
    
    void toggleLED(uint8_t which)
    {
      if (1 == which)
      {
        BSP_TOGGLE_LED1();
      }
      else if (2 == which)
      {
        BSP_TOGGLE_LED2();
      }
      return;
    }
    
    /* handle received frames. */
    static uint8_t sRxCallback(linkID_t port)
    {
      uint8_t msg[2], len, tid;
    
      /* is the callback for the link ID we want to handle? */
      if (port == sLinkID1)
      {
        /* yes. go get the frame. we know this call will succeed. */
         if ((SMPL_SUCCESS == SMPL_Receive(sLinkID1, msg, &len)) && len)
         {
           /* Check the application sequence number to detect
            * late or missing frames... 
            */
           tid = *(msg+1);
           if (tid)
           {
             if (tid > sRxTid)
             {
               /* we're good. toggle LED in the message */
               toggleLED(*msg);
               sRxTid = tid;
             }
           }
           else
           {
             /* the wrap case... */
             if (sRxTid)
             {
               /* we're good. toggle LED in the message */
               toggleLED(*msg);
               sRxTid = tid;
             }
           }
           /* drop frame. we're done with it. */
           return 1;
         }
      }
      /* keep frame for later handling. */
      return 0;
    }
    

    LinkListen:

    /*----------------------------------------------------------------------------
     *  Demo Application for SimpliciTI
     *
     *  L. Friedman
     *  Texas Instruments, Inc.
     *----------------------------------------------------------------------------
     */
    /******************************************************************************************
    
      Copyright 2007-2009 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.
    **************************************************************************************************/
    
    #include "bsp.h"
    #include "mrfi.h"
    #include "nwk_types.h"
    #include "nwk_api.h"
    #include "bsp_leds.h"
    #include "bsp_buttons.h"
    #include "nwk_pll.h"
    
    static void linkFrom(void);
    
    void toggleLED(uint8_t);
    
    static          uint8_t  sRxTid = 0;
    static          linkID_t sLinkID2 = 0;
    static volatile uint8_t  sSemaphore = 0;
    
    /* Rx callback handler */
    static uint8_t sRxCallback(linkID_t);
    
    void main (void)
    {
      BSP_Init();
    
      /* If an on-the-fly device address is generated it must be done before the
       * call to SMPL_Init(). If the address is set here the ROM value will not
       * be used. If SMPL_Init() runs before this IOCTL is used the IOCTL call
       * will not take effect. One shot only. The IOCTL call below is conformal.
       */
    #ifdef I_WANT_TO_CHANGE_DEFAULT_ROM_DEVICE_ADDRESS_PSEUDO_CODE
      {
        addr_t lAddr;
    
        createRandomAddress(&lAddr);
        SMPL_Ioctl(IOCTL_OBJ_ADDR, IOCTL_ACT_SET, &lAddr);
      }
    #endif /* I_WANT_TO_CHANGE_DEFAULT_ROM_DEVICE_ADDRESS_PSEUDO_CODE */
    
      /* On FHSS systems the call to SMPL_Init will not return until we have
       * locked onto a reference clock.  Also, on return the radio will always
       * be on in receive mode (at least for now).
       */
      
      /* This call will fail because the join will fail since there is no Access Point
       * in this scenario. But we don't care -- just use the default link token later.
       * We supply a callback pointer to handle the message returned by the peer.
       */
      SMPL_Init(sRxCallback);
    
      /* turn on LEDs. */
      if (!BSP_LED2_IS_ON())
      {
        toggleLED(2);
      }
      if (!BSP_LED1_IS_ON())
      {
        toggleLED(1);
      }
        
      /* wait for a button press... */
      do {
        FHSS_ACTIVE( nwk_pllBackgrounder( false ) ); /* manage FHSS */
        if (BSP_BUTTON1() || BSP_BUTTON2())
        {
          break;
        }
      } while (1);
    
      /* never coming back... */
      linkFrom();
    
      /* but in case we do... */
      while (1) ;
    }
    
    static void linkFrom()
    {
      uint8_t     msg[2], tid = 0;
    
      /* Turn off one LED so we can tell the device is now listening.
       * Received messages will toggle the other LED.
       */
      toggleLED(1);
    
       /* listen for link forever... */
      while (1)
      {
        /* SMPL_LinkListen handles FHSS implicittly */
        if (SMPL_SUCCESS == SMPL_LinkListen(&sLinkID2))
        {
          break;
        }
        /* Implement fail-to-link policy here. otherwise, listen again. */
      }
    
       /* turn on LED1 on the peer in response to receiving a frame. */
       *msg = 0x01;
    
       /* turn on RX. default is RX off. */
       SMPL_Ioctl( IOCTL_OBJ_RADIO, IOCTL_ACT_RADIO_RXON, 0);
    
       while (1)
       {
        FHSS_ACTIVE( nwk_pllBackgrounder( false ) ); /* manage FHSS */
    
         /* Wait for a frame to be received. The Rx handler, which is running in
          * ISR thread, will post to this semaphore allowing the application to
          * send the reply message in the user thread.
          */
         if (sSemaphore)
         {
           *(msg+1) = ++tid;
           SMPL_Send(sLinkID2, msg, 2);
    
           /* Reset semaphore. This is not properly protected and there is a race
            * here. In theory we could miss a message. Good enough for a demo, though.
            */
           sSemaphore = 0;
         }
       }
    }
    
    void toggleLED(uint8_t which)
    {
      if (1 == which)
      {
        BSP_TOGGLE_LED1();
      }
      else if (2 == which)
      {
        BSP_TOGGLE_LED2();
      }
      return;
    }
    
    /* handle received messages */
    static uint8_t sRxCallback(linkID_t port)
    {
      uint8_t msg[2], len, tid;
    
      /* is the callback for the link ID we want to handle? */
      if (port == sLinkID2)
      {
        /* yes. go get the frame. we know this call will succeed. */
         if ((SMPL_SUCCESS == SMPL_Receive(sLinkID2, msg, &len)) && len)
         {
           /* Check the application sequence number to detect
            * late or missing frames...
            */
           tid = *(msg+1);
           if (tid)
           {
             if (tid > sRxTid)
             {
               /* we're good. toggle LED */
               toggleLED(*msg);
               sRxTid = tid;
             }
           }
           else
           {
             /* wrap case... */
             if (sRxTid)
             {
               /* we're good. toggle LED */
               toggleLED(*msg);
               sRxTid = tid;
             }
           }
           /* Post to the semaphore to let application know so it sends
            * the reply
            */
           sSemaphore = 1;
           /* drop frame. we're done with it. */
           return 1;
         }
      }
      /* keep frame for later handling */
      return 0;
    }