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.

CCS/CC2640R2F: Adverting on 3 Sec, off 6 sec.

Part Number: CC2640R2F
Other Parts Discussed in Thread: LAUNCHXL-CC2640R2

Tool/software: Code Composer Studio

Hello,

I want the cc2640r2 to send advering for 3 sec and stop sending for 6 second then repeat forever to save the power, how to modify the following code to do that?

void startAdvertising(void){
uint8_t advertEnabled = TRUE;
uint16_t advertDuration = 3;
GAP_SetParamValue(TGAP_LIM_ADV_TIMEOUT, advertDuration);
GAPRole_SetParameter(GAPROLE_ADVERT_ENABLED, sizeof(uint8_t), &advertEnabled);
}

1.Could you please help to provide the sudo code?

2. How can I verify the current status is at GAP_ADTYPE_FLAGS_LIMITED?

Many thanks,

Jack

  • Hi,

    There are better ways to save power during BLE advertising :)

    I would advice you to review the SimpleLink Academy labs and have a look to the examples already provided in your SDK.

    I hope this will help,

  • Hello,

    CR23032 is used for my design to save power is very important that's why I want to keep the adverting on and off.

    The folliwng code could not stop the adverting, do you know why?

    1. define DEFAULT_DISCOVERABLE_MODE             GAP_ADTYPE_FLAGS_LIMITED
    2. uint16 gapRole_AdvertOffTime = 15000;  
    3. GAPRole_SetParameter( GAPROLE_ADVERT_OFF_TIME, sizeof( uint16 ), &gapRole_AdvertOffTime );//GAPROLE_ADVERT_OFF_TIME
    4. uint16 ADV_TIMEOUT = 20;  
    5. GAP_SetParamValue( TGAP_LIM_ADV_TIMEOUT, ADV_TIMEOUT );

    Thank you

  • I suppose you can create timer event and use the following codes to control enable/disable advertising.

    advertEnabled = TRUE; //FALSE

    GAPRole_SetParameter(GAPROLE_ADVERT_ENABLED, sizeof(uint8_t), &advertEnabled);

  • HI YK,

    I put the following codes on the buttom of keyfobdemo.c but it wo'nt stop the adverting, could you please help to check, what's wrong with my code?

    void AAA(void){
    uint8_t advertEnabled = FALSE;
    uint16_t advertDuration = 3;
    GAP_SetParamValue(TGAP_LIM_ADV_TIMEOUT, advertDuration);
    GAPRole_SetParameter(GAPROLE_ADVERT_ENABLED, sizeof(uint8_t), &advertEnabled);
      Util_startClock(&batteryCheckClock); 
    GAPRole_SetParameter(GAPROLE_ADVERT_ENABLED, sizeof(uint8_t),
                           FALSE);
    }
    Thank you,
  • Hi Jack,

    Have you consult the SimpleLink Academy labs as I suggested? There is a sub-section dedicated to "Limited Advertising".

    I hope this will help,

    Kind regards,

  • Hello,

    Would that be possible someone could help to provide  the example codes to have adverting on 3 sec, off 3 sec, and so on forever?

    Thanks a lot

  • Hello YK,

    Thanks for the example.

    However, the advertng is on for 10 sec, then stop forever, any hint for the issue.

    the codes below.

    .........................................

    #define SBP_ALL_EVENTS                        (SBP_ICALL_EVT        | \
                                                   SBP_QUEUE_EVT        | \
                                                   ADV_PERIODIC_EVT     | \
                                                   SBP_QUEUE_PING_EVT)

    Util_constructClock(&periodicClockAdv,SimpleBLEPeripheral_clockHandler,
    ADV_PERIODIC_EVT_PERIOD,0,false,ADV_PERIODIC_EVT);
        Util_startClock(&periodicClockAdv);
    ........................

      SimpleBLEPeripheral_init();
      // Application main loop
      for (;;)
      {
        uint32_t events;
        // Waits for an event to be posted associated with the calling thread.
        // Note that an event associated with a thread is posted when a
        // message is queued to the message receive queue of the thread
        events = Event_pend(syncEvent, Event_Id_NONE, SBP_ALL_EVENTS,
                            ICALL_TIMEOUT_FOREVER);
    if ( events & ADV_PERIODIC_EVT)

    {
    Util_startClock(&periodicClockAdv);
    ..............................................
    static void SimpleBLEPeripheral_clockHandler(UArg arg)
    {

    Event_post(syncEvent, ADV_PERIODIC_EVT);
    }

  • I have test it on my LAUNCHXL-CC2640R2 and it can toggle advertising every 10 seconds. I would suggest you to do the test from scratch again.

  • Hi YK,

    If you do not mind, could you please help to send me your wroking simple_peripheral.c for me to test?

    Or post here, if you could not send me simple_peripheral.c?

    I want to test on my side.

    Many thanks,

  • After you add the codes, can you see "if ( events & ADV_PERIODIC_EVT)..." is triggered every 10 seconds?

  • Hi YK,

    i did see the adverting on for 10 sec then off forever, I guess the "if ( events & ADV_PERIODIC_EVT)" does not work the second time.

    I suspect ADV_PERIODIC_EVT did not trigger  the second time but I do not see anything wrong the my codes, that's why I want to see your codes cor the comparison. would that be possible.

    Below is how to trigger the ADV_PERIODIC_EVT

    #define SBP_ALL_EVENTS (SBP_ICALL_EVT | \
    SBP_QUEUE_EVT | \
    ADV_PERIODIC_EVT | \
    SBP_QUEUE_PING_EVT)

    .....................................

    #define ADV_PERIODIC_EVT Event_Id_03

    .................................................

    Util_constructClock(&periodicClockAdv,SimpleBLEPeripheral_clockHandler,
    ADV_PERIODIC_EVT_PERIOD,0,false,ADV_PERIODIC_EVT);

    ...............................................................

    static void SimpleBLEPeripheral_clockHandler(UArg arg)
    {
    //uint32_t events;

    // Store the event.
    //events |= arg;

    // Wake up the application.
    // Semaphore_post(sem);
    // Wake up

    the application.
    // Event_post (events,arg);
    Event_post(syncEvent, ADV_PERIODIC_EVT);

    Would that be possible you could help to share your codes?Or help to let me know what's wrong with my code about  ADV_PERIODIC_EVT?

    Thanks a lot

  • I already post my code on the blog. Do you set breakpoint inside "if (events & ADV_PERIODIC_EVT)..." and make sure it hits every 10 seconds?

  • Hi YK,

    Would that be possible to you could help to share complete simple_preiphere.c?Since you said it works on your side. The Blog is just part of codes so I could not reproduce here on my side.

    I use IAR ,how to set the breakpoint?

    Thanks in advance,

    Jack

  • In IAR, you can click at the line you want to set breakpoint and press "F9" on keyboard to set breakpoint on the line. There will be a red dot in front of the line if you set breakpoint there.

  • Hi YK,

    I set the breakpoint inside if (events & ADV_PERIODIC_EVT)..., it is the same. Only adverting for 10 seconds then stop forever.

    Any idea for the issues? Or is it possible I could see your complete working simple_preipheral.c?

    Thanks a lot

  • After you set breakpoint, can you see "if ( events & ADV_PERIODIC_EVT)..." is triggered every 10 seconds?

  • Hello YK,

    No, I see adverting for 10 seconds  then no adverting forever.

    Any chance I could have your complete working simple_peripheral.c to try on my site?

    Or any idea about the issues might be?

    Thanks in advance,

    Jack

  • You keep telling me “No, I see adverting for 10 seconds  then no adverting forever.” but never answer my question. Please answer my question so I can help you further.

  • Hi YK,

    I am sorry, I do not see "if ( events & ADV_PERIODIC_EVT)..." is triggered every 10 seconds.

    I see green arrow and red dot  stay at "if ( events & ADV_PERIODIC_EVT) "

    If I hit debug-->GO, the green arrow will come back then stay at  "if ( events & ADV_PERIODIC_EVT) "  forever.

    Is that something you want me to try? Or I missed something?

    Thank you,

    Jack

  • Can you provide a screenshot to show me where you set the breakpoint?

  • You should set breakpoint on “if(advertEnabledPeriod==TRUE)...” instead of “if (events & ADV_PERIODIC_EVT)”. Try it and let me know the result.

  • Hi YK,

    Green arrow and red dot stay together  ,if ( events & ADV_PERIODIC_EVT) is not toggled every 10 seconds as on the picture.

    Should I hit debug-->GO?

  • Yes, you should hit debug-->GO and check if it hits again 10 seconds later.

  • Hello YK,

    If I hit GO again, I do not know where the green arrow goes? ( Could not find green arrow  is where?)

    I could not see if ( events & ADV_PERIODIC_EVT) is not toggled every 10 seconds.. ( Or do you think green arrow would move to fast or me to see it? )

    Any idea?

    Thanks in advance,

  • It should go back to “if(advertEnabledPeriod==TRUE)...” 10 seconds later after you hit GO.

  • Hi YK,

    No, I do not see it goes back to “if(advertEnabledPeriod==TRUE)...” 10 seconds later after I hit GO.

    Any chance I could get your complete working simple_preipheral.c to check?

    Thanks in advance,

    Jack

  • You should put "if (events & ADV_PERIODIC_EVT)..." right before "if (events & SBP_PERIODIC_EVT){...". You can check the following example codes in red and where I put them.

          // If RTOS queue is not empty, process app message.
          if (events & SBP_QUEUE_EVT)
          {
            while (!Queue_empty(appMsgQueue))
            {
              sbpEvt_t *pMsg = (sbpEvt_t *)Util_dequeueMsg(appMsgQueue);
              if (pMsg)
              {
                // Process message.
                SimplePeripheral_processAppMsg(pMsg);
    
                // Free the space from the message.
                ICall_free(pMsg);
              }
            }
          }
    
          if (events & ADV_PERIODIC_EVT)//YK for ADV
          {
            Util_startClock(&periodicClockAdv);
            if(advertEnabledPeriod==TRUE){
                advertEnabledPeriod=FALSE;
            } else {
                advertEnabledPeriod=TRUE;
            }
            GAPRole_SetParameter(GAPROLE_ADVERT_ENABLED, sizeof(uint8_t),
                                     &advertEnabledPeriod);
    
          }
    
          if (events & SBP_PERIODIC_EVT)
          {
            Util_startClock(&periodicClock);
    
            // Perform periodic application task
            SimplePeripheral_performPeriodicTask();
          }

  • Hi YK,

    Sorry, still the same, "if (events & ADV_PERIODIC_EVT)..." not toggled every 10 seconds. ( It does on for 10 seconds then stop forever ).

    I attach the picture, if you could help to check?

    Thanks a lot,

  • Does it hit first time?

  • Hello YK,

    It does hit the first time, then I do not know where the green arrow goes? ( Could not find green arrow any more )

    Thank you,

  • Where do you put the following code in SimplePeripheral_init()?

    Util_constructClock(&periodicClockAdv, SimplePeripheral_clockHandler,
                          ADV_PERIODIC_EVT_PERIOD, 0, false, ADV_PERIODIC_EVT);
        Util_startClock(&periodicClockAdv);

  • Hi YK,

    Please help to check the codes below in red and help to let me know, if I have placed the right place?

    static void SimpleBLEPeripheral_init(void)
    {
    // ******************************************************************
    // N0 STACK API CALLS CAN OCCUR BEFORE THIS CALL TO ICall_registerApp
    // ******************************************************************
    // Register the current thread as an ICall dispatcher application
    // so that the application can send and receive messages.
    #define ADV_MAX_LEN 31
    #define ADVDATA_MANUF_DATA_IDX 5
    // ==== END SOLUTION ====
    // How often to perform periodic event (in msec)
    //#define SBP_PERIODIC_EVT_PERIOD 10000 // 10 seconds.
    #define ADV_PERIODIC_EVT Event_Id_03
    #define ADV_PERIODIC_EVT_PERIOD 10000
    uint8_t advertEnabledPeriod = TRUE;
    static Clock_Struct periodicClockAdv;

    Util_constructClock(&periodicClockAdv,SimpleBLEPeripheral_clockHandler,
    ADV_PERIODIC_EVT_PERIOD,0,false,ADV_PERIODIC_EVT);

    Util_startClock(&periodicClockAdv);
    ICall_registerApp(&selfEntity, &syncEvent);

    #ifdef USE_RCOSC
    RCOSC_enableCalibration();
    #endif // USE_RCOSC

    Thanks a lot,

    Jack

  • I see you don’t follow my step exactly so I suggest you to check the steps in the blog exactly to test again.

  • Hi YK,

    Good morning

    May I know what exactly I should put those codes? I am sorry, the blog is not clear for me.

    By the way, is there any reason I could not get your complete working simple.periphere.c to test here on my site?

    Thank you,

  • I can help and guide you how to debug but I don't want to just give you the code for easy solution. You won't learn anything in this way.

  • Hi YK,

    May I know the what's the right location to put those code inside the simple_preiphere_init ();?

    Thanks a lot

  • You can put it right before "dispHandle = Display_open(SBP_DISPLAY_TYPE, NULL);"

  • Hi YK,

    Move there but the same,only hit once. Anything I could try?

    After modification codes below.


    // Create one-shot clocks for internal periodic events.
    Util_constructClock(&periodicClock, SimpleBLEPeripheral_clockHandler,
    SBP_PERIODIC_EVT_PERIOD, 0, false, SBP_PERIODIC_EVT);
    Util_constructClock(&periodicClockAdv,SimpleBLEPeripheral_clockHandler,
    ADV_PERIODIC_EVT_PERIOD,0,false,ADV_PERIODIC_EVT);

    Util_startClock(&periodicClockAdv);


    dispHandle = Display_open(SBP_DISPLAY_TYPE, NULL);

    Thank you,

  • Maybe attache your simple_peripheral.c.

  • 3162.simple_peripheral.c
    /******************************************************************************
    
     @file       simple_peripheral.c
    
     @brief This file contains the Simple Peripheral sample application for use
            with the CC2650 Bluetooth Low Energy Protocol Stack.
    
     Group: CMCU, SCS
     Target Device: CC2640R2
    
     ******************************************************************************
     
     Copyright (c) 2013-2017, Texas Instruments Incorporated
     All rights reserved.
    
     Redistribution and use in source and binary forms, with or without
     modification, are permitted provided that the following conditions
     are met:
    
     *  Redistributions of source code must retain the above copyright
        notice, this list of conditions and the following disclaimer.
    
     *  Redistributions in binary form must reproduce the above copyright
        notice, this list of conditions and the following disclaimer in the
        documentation and/or other materials provided with the distribution.
    
     *  Neither the name of Texas Instruments Incorporated nor the names of
        its contributors may be used to endorse or promote products derived
        from this software without specific prior written permission.
    
     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
     AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
     CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
     EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    
     ******************************************************************************
     Release Name: simplelink_cc2640r2_sdk_1_40_00_45
     Release Date: 2017-07-20 17:16:59
     *****************************************************************************/
    
    /*********************************************************************
     * INCLUDES
     */
    #include <string.h>
    
    #include <ti/sysbios/knl/Task.h>
    #include <ti/sysbios/knl/Clock.h>
    #include <ti/sysbios/knl/Event.h>
    #include <ti/sysbios/knl/Queue.h>
    #include <ti/display/Display.h>
    
    #if defined( USE_FPGA ) || defined( DEBUG_SW_TRACE )
    #include <driverlib/ioc.h>
    #endif // USE_FPGA | DEBUG_SW_TRACE
    
    #include <icall.h>
    #include "util.h"
    /* This Header file contains all BLE API and icall structure definition */
    #include "icall_ble_api.h"
    
    #include "devinfoservice.h"
    #include "simple_gatt_profile.h"
    
    #if defined(FEATURE_OAD) || defined(IMAGE_INVALIDATE)
    #include "oad_target.h"
    #include "oad.h"
    #endif //FEATURE_OAD || IMAGE_INVALIDATE
    
    #include "peripheral.h"
    
    #ifdef USE_RCOSC
    #include "rcosc_calibration.h"
    #endif //USE_RCOSC
    
    
    #include "board.h"
    
    #if !defined(Display_DISABLE_ALL)
    #include "iotboard_key.h"
    #include <menu/two_btn_menu.h>
    
    #include "simple_peripheral_menu.h"
    #endif  // !Display_DISABLE_ALL
    
    #include "simple_peripheral.h"
    
    
    #include "hw_gpio.h"
    /*********************************************************************
     * CONSTANTS
     */
    
    // Advertising interval when device is discoverable (units of 625us, 160=100ms)
    #define DEFAULT_ADVERTISING_INTERVAL          999
    
    // General discoverable mode: advertise indefinitely
    #define DEFAULT_DISCOVERABLE_MODE             GAP_ADTYPE_FLAGS_GENERAL
    
    #ifndef FEATURE_OAD
    // Minimum connection interval (units of 1.25ms, 80=100ms) for automatic
    // parameter update request
    #define DEFAULT_DESIRED_MIN_CONN_INTERVAL     888
    
    // Maximum connection interval (units of 1.25ms, 800=1000ms) for automatic
    // parameter update request
    #define DEFAULT_DESIRED_MAX_CONN_INTERVAL     999
    
    #else // FEATURE_OAD
    // Increase the the connection interval to allow for higher throughput for OAD
    
    // Minimum connection interval (units of 1.25ms, 8=10ms) for automatic
    // parameter update request
    #define DEFAULT_DESIRED_MIN_CONN_INTERVAL     8
    
    // Maximum connection interval (units of 1.25ms, 8=10ms) for automatic
    // parameter update request
    #define DEFAULT_DESIRED_MAX_CONN_INTERVAL     8
    #endif // FEATURE_OAD
    
    // Slave latency to use for automatic parameter update request
    #define DEFAULT_DESIRED_SLAVE_LATENCY         0
    
    // Supervision timeout value (units of 10ms, 1000=10s) for automatic parameter
    // update request
    #define DEFAULT_DESIRED_CONN_TIMEOUT          1000
    
    // After the connection is formed, the peripheral waits until the central
    // device asks for its preferred connection parameters
    #define DEFAULT_ENABLE_UPDATE_REQUEST         GAPROLE_LINK_PARAM_UPDATE_WAIT_REMOTE_PARAMS
    
    // Connection Pause Peripheral time value (in seconds)
    #define DEFAULT_CONN_PAUSE_PERIPHERAL         6
    
    // How often to perform periodic event (in msec)
    #define SBP_PERIODIC_EVT_PERIOD               5000
    
    // Application specific event ID for HCI Connection Event End Events
    #define SBP_HCI_CONN_EVT_END_EVT              0x0001
    
    // Type of Display to open
    #if !defined(Display_DISABLE_ALL)
      #if defined(BOARD_DISPLAY_USE_LCD) && (BOARD_DISPLAY_USE_LCD!=0)
        #define SBP_DISPLAY_TYPE Display_Type_LCD
      #elif defined (BOARD_DISPLAY_USE_UART) && (BOARD_DISPLAY_USE_UART!=0)
        #define SBP_DISPLAY_TYPE Display_Type_UART
      #else // !BOARD_DISPLAY_USE_LCD && !BOARD_DISPLAY_USE_UART
        #define SBP_DISPLAY_TYPE 0 // Option not supported
      #endif // BOARD_DISPLAY_USE_LCD && BOARD_DISPLAY_USE_UART
    #else // BOARD_DISPLAY_USE_LCD && BOARD_DISPLAY_USE_UART
      #define SBP_DISPLAY_TYPE 0 // No Display
    #endif // !Display_DISABLE_ALL
    
    #ifdef FEATURE_OAD
    // The size of an OAD packet.
    #define OAD_PACKET_SIZE                       ((OAD_BLOCK_SIZE) + 2)
    #endif // FEATURE_OAD
    
    // Task configuration
    #define SBP_TASK_PRIORITY                     1
    
    #ifndef SBP_TASK_STACK_SIZE
    #define SBP_TASK_STACK_SIZE                   644
    #endif
    
    // Application events
    #define SBP_STATE_CHANGE_EVT                  0x0001
    #define SBP_CHAR_CHANGE_EVT                   0x0002
    #define SBP_KEY_CHANGE_EVT                    0x0004
    
    // Internal Events for RTOS application
    #define SBP_ICALL_EVT                         ICALL_MSG_EVENT_ID // Event_Id_31
    #define SBP_QUEUE_EVT                         UTIL_QUEUE_EVENT_ID // Event_Id_30
    #define SBP_PERIODIC_EVT                      Event_Id_00
    
    //#ifdef FEATURE_OAD
    // Additional Application Events for OAD
    #define SBP_QUEUE_PING_EVT                    Event_Id_02
    
    // Bitwise OR of all events to pend on with OAD
    #define SBP_ALL_EVENTS                        (SBP_ICALL_EVT        | \
                                                   SBP_QUEUE_EVT        | \
                                                   ADV_PERIODIC_EVT     | \
                                                   SBP_PERIODIC_EVT     | \
                                                   SBP_QUEUE_PING_EVT)
    //#else
    // Bitwise OR of all events to pend on
    //#define SBP_ALL_EVENTS                        (SBP_ICALL_EVT        | \
                                                 SBP_QUEUE_EVT        | \
                                               ADV_PERIODIC_EVT)
    //#endif /* FEATURE_OAD */
    
    // Row numbers for two-button menu
    #define SBP_ROW_RESULT        TBM_ROW_APP
    #define SBP_ROW_STATUS_1      (TBM_ROW_APP + 1)
    #define SBP_ROW_STATUS_2      (TBM_ROW_APP + 2)
    #define SBP_ROW_ROLESTATE     (TBM_ROW_APP + 3)
    #define SBP_ROW_BDADDR        (TBM_ROW_APP + 4)
    
    /*********************************************************************
     * TYPEDEFS
     */
    
    // App event passed from profiles.
    typedef struct
    {
      appEvtHdr_t hdr;  // event header.
    } sbpEvt_t;
    
    /*********************************************************************
     * GLOBAL VARIABLES
     */
    
    // Display Interface
    Display_Handle dispHandle = NULL;
    
    /*********************************************************************
     * LOCAL VARIABLES
     */
    
    // Entity ID globally used to check for source and/or destination of messages
    static ICall_EntityID selfEntity;
    static void SimpleBLEPeripheral_clockHandler();
    
    
    // Event globally used to post local events and pend on system and
    // local events.
    static ICall_SyncHandle syncEvent;
    
    // Clock instances for internal periodic events.
    static Clock_Struct periodicClock;
    
    // Queue object used for app messages
    static Queue_Struct appMsg;
    static Queue_Handle appMsgQueue;
    
    #if defined(FEATURE_OAD)
    // Event data from OAD profile.
    static Queue_Struct oadQ;
    static Queue_Handle hOadQ;
    #endif //FEATURE_OAD
    
    // Task configuration
    Task_Struct sbpTask;
    Char sbpTaskStack[SBP_TASK_STACK_SIZE];
    
    // Scan response data (max size = 31 bytes)
    static uint8_t scanRspData[] =
    {
      // complete name
      0x14,   // length of this data
      GAP_ADTYPE_LOCAL_NAME_COMPLETE,
      'S',
      'i',
      'm',
      'p',
      'l',
      'e',
      'B',
      'L',
      'E',
      'P',
      'e',
      'r',
      'i',
      'p',
      'h',
      'e',
      'r',
      'a',
      'l',
    
      // connection interval range
      0x05,   // length of this data
      GAP_ADTYPE_SLAVE_CONN_INTERVAL_RANGE,
      LO_UINT16(DEFAULT_DESIRED_MIN_CONN_INTERVAL),   // 100ms
      HI_UINT16(DEFAULT_DESIRED_MIN_CONN_INTERVAL),
      LO_UINT16(DEFAULT_DESIRED_MAX_CONN_INTERVAL),   // 1s
      HI_UINT16(DEFAULT_DESIRED_MAX_CONN_INTERVAL),
    
      // Tx power level
      0x02,   // length of this data
      GAP_ADTYPE_POWER_LEVEL,
      5       // 0dBm
    };
    
    // Advertisement data (max size = 31 bytes, though this is
    // best kept short to conserve power while advertising)
    static uint8_t advertData[] =
    {
      // Flags: this field sets the device to use general discoverable
      // mode (advertises indefinitely) instead of general
      // discoverable mode (advertise for 30 seconds at a time)
      0x02,   // length of this data
      GAP_ADTYPE_FLAGS,
      DEFAULT_DISCOVERABLE_MODE | GAP_ADTYPE_FLAGS_BREDR_NOT_SUPPORTED,
    
      // service UUID, to notify central devices what services are included
      // in this peripheral
    #if !defined(FEATURE_OAD) || defined(FEATURE_OAD_ONCHIP)
      0x03,   // length of this data
    #else //OAD for external flash
      0x05,  // length of this data
    #endif //FEATURE_OAD
      GAP_ADTYPE_16BIT_MORE,      // some of the UUID's, but not all
    #ifdef FEATURE_OAD
      LO_UINT16(OAD_SERVICE_UUID),
      HI_UINT16(OAD_SERVICE_UUID),
    #endif //FEATURE_OAD
    #ifndef FEATURE_OAD_ONCHIP
      LO_UINT16(SIMPLEPROFILE_SERV_UUID),
      HI_UINT16(SIMPLEPROFILE_SERV_UUID)
    #endif //FEATURE_OAD_ONCHIP
    };
    
    // GAP GATT Attributes
    static uint8_t attDeviceName[GAP_DEVICE_NAME_LEN] = "Simple Peripheral";
    
    // Globals used for ATT Response retransmission
    static gattMsgEvent_t *pAttRsp = NULL;
    static uint8_t rspTxRetry = 0;
    
    /*********************************************************************
     * LOCAL FUNCTIONS
     */
    
    static void SimpleBLEPeripheral_init( void );
    static void SimpleBLEPeripheral_taskFxn(UArg a0, UArg a1);
    
    static uint8_t SimpleBLEPeripheral_processStackMsg(ICall_Hdr *pMsg);
    static uint8_t SimpleBLEPeripheral_processGATTMsg(gattMsgEvent_t *pMsg);
    static void SimpleBLEPeripheral_processAppMsg(sbpEvt_t *pMsg);
    static void SimpleBLEPeripheral_processStateChangeEvt(gaprole_States_t newState);
    static void SimpleBLEPeripheral_processCharValueChangeEvt(uint8_t paramID);
    static void SimpleBLEPeripheral_performPeriodicTask(void);
    
    
    static void SimpleBLEPeripheral_sendAttRsp(void);
    static void SimpleBLEPeripheral_freeAttRsp(uint8_t status);
    
    static void SimpleBLEPeripheral_stateChangeCB(gaprole_States_t newState);
    #ifndef FEATURE_OAD_ONCHIP
    static void SimpleBLEPeripheral_charValueChangeCB(uint8_t paramID);
    #endif //!FEATURE_OAD_ONCHIP
    static void SimpleBLEPeripheral_enqueueMsg(uint8_t event, uint8_t state);
    
    #ifdef FEATURE_OAD
    void SimpleBLEPeripheral_processOadWriteCB(uint8_t event, uint16_t connHandle,
                                               uint8_t *pData);
    #endif //FEATURE_OAD
    
    #if !defined(Display_DISABLE_ALL)
    void SimpleBLEPeripheral_keyChangeHandler(uint8 keys);
    static void SimpleBLEPeripheral_handleKeys(uint8_t keys);
    #endif  // !Display_DISABLE_ALL
    
    /*********************************************************************
     * EXTERN FUNCTIONS
     */
    extern void AssertHandler(uint8 assertCause, uint8 assertSubcause);
    
    /*********************************************************************
     * PROFILE CALLBACKS
     */
    
    // Peripheral GAPRole Callbacks
    static gapRolesCBs_t SimpleBLEPeripheral_gapRoleCBs =
    {
      SimpleBLEPeripheral_stateChangeCB     // GAPRole State Change Callbacks
    };
    
    // GAP Bond Manager Callbacks
    // These are set to NULL since they are not needed. The application
    // is set up to only perform justworks pairing.
    static gapBondCBs_t simpleBLEPeripheral_BondMgrCBs =
    {
      NULL, // Passcode callback
      NULL  // Pairing / Bonding state Callback
    };
    
    // Simple GATT Profile Callbacks
    #ifndef FEATURE_OAD_ONCHIP
    static simpleProfileCBs_t SimpleBLEPeripheral_simpleProfileCBs =
    {
      SimpleBLEPeripheral_charValueChangeCB // Simple GATT Characteristic value change callback
    };
    #endif //!FEATURE_OAD_ONCHIP
    
    #ifdef FEATURE_OAD
    static oadTargetCBs_t simpleBLEPeripheral_oadCBs =
    {
      SimpleBLEPeripheral_processOadWriteCB // OAD Profile Characteristic value change callback.
    };
    #endif //FEATURE_OAD
    
    /*********************************************************************
     * PUBLIC FUNCTIONS
     */
    
    /*********************************************************************
     * @fn      SimpleBLEPeripheral_createTask
     *
     * @brief   Task creation function for the Simple Peripheral.
     *
     * @param   None.
     *
     * @return  None.
     */
    void SimpleBLEPeripheral_createTask(void)
    {
      Task_Params taskParams;
    
      // Configure task
      Task_Params_init(&taskParams);
      taskParams.stack = sbpTaskStack;
      taskParams.stackSize = SBP_TASK_STACK_SIZE;
      taskParams.priority = SBP_TASK_PRIORITY;
    
      Task_construct(&sbpTask, SimpleBLEPeripheral_taskFxn, &taskParams, NULL);
    }
    
    /*********************************************************************
     * @fn      SimpleBLEPeripheral_init
     *
     * @brief   Called during initialization and contains application
     *          specific initialization (ie. hardware initialization/setup,
     *          table initialization, power up notification, etc), and
     *          profile initialization/setup.
     *
     * @param   None.
     *
     * @return  None.
     */
    static void SimpleBLEPeripheral_init(void)
    {
      // ******************************************************************
      // N0 STACK API CALLS CAN OCCUR BEFORE THIS CALL TO ICall_registerApp
      // ******************************************************************
      // Register the current thread as an ICall dispatcher application
      // so that the application can send and receive messages.
    #define ADV_MAX_LEN 31
    #define ADVDATA_MANUF_DATA_IDX 5
    // ==== END SOLUTION ====
    // How often to perform periodic event (in msec)
    //#define SBP_PERIODIC_EVT_PERIOD    10000  // 10 seconds.
    #define ADV_PERIODIC_EVT Event_Id_03
    #define ADV_PERIODIC_EVT_PERIOD 10000
    uint8_t advertEnabledPeriod = TRUE;
    static Clock_Struct periodicClockAdv;
    
      ICall_registerApp(&selfEntity, &syncEvent);
    
    #ifdef USE_RCOSC
      RCOSC_enableCalibration();
    #endif // USE_RCOSC
    
    #if defined( USE_FPGA )
      // configure RF Core SMI Data Link
      IOCPortConfigureSet(IOID_12, IOC_PORT_RFC_GPO0, IOC_STD_OUTPUT);
      IOCPortConfigureSet(IOID_11, IOC_PORT_RFC_GPI0, IOC_STD_INPUT);
    
      // configure RF Core SMI Command Link
      IOCPortConfigureSet(IOID_10, IOC_IOCFG0_PORT_ID_RFC_SMI_CL_OUT, IOC_STD_OUTPUT);
      IOCPortConfigureSet(IOID_9, IOC_IOCFG0_PORT_ID_RFC_SMI_CL_IN, IOC_STD_INPUT);
    
      // configure RF Core tracer IO
      IOCPortConfigureSet(IOID_8, IOC_PORT_RFC_TRC, IOC_STD_OUTPUT);
    #else // !USE_FPGA
      #if defined( DEBUG_SW_TRACE )
        // configure RF Core tracer IO
        IOCPortConfigureSet(IOID_8, IOC_PORT_RFC_TRC, IOC_STD_OUTPUT | IOC_CURRENT_4MA | IOC_SLEW_ENABLE);
      #endif // DEBUG_SW_TRACE
    #endif // USE_FPGA
    
      // Create an RTOS queue for message from profile to be sent to app.
      appMsgQueue = Util_constructQueue(&appMsg);
    //#define SBP_PERIODIC_EVT_PERIOD               10000
    
      // Create one-shot clocks for internal periodic events.
      Util_constructClock(&periodicClock, SimpleBLEPeripheral_clockHandler,
                         SBP_PERIODIC_EVT_PERIOD, 0, false, SBP_PERIODIC_EVT);
    Util_constructClock(&periodicClockAdv,SimpleBLEPeripheral_clockHandler,
    ADV_PERIODIC_EVT_PERIOD,0,false,ADV_PERIODIC_EVT);
    
        Util_startClock(&periodicClockAdv);
    
    
      dispHandle = Display_open(SBP_DISPLAY_TYPE, NULL);
    
      // Set GAP Parameters: After a connection was established, delay in seconds
      // before sending when GAPRole_SetParameter(GAPROLE_PARAM_UPDATE_ENABLE,...)
      // uses GAPROLE_LINK_PARAM_UPDATE_INITIATE_BOTH_PARAMS or
      // GAPROLE_LINK_PARAM_UPDATE_INITIATE_APP_PARAMS
      // For current defaults, this has no effect.
      GAP_SetParamValue(TGAP_CONN_PAUSE_PERIPHERAL, DEFAULT_CONN_PAUSE_PERIPHERAL);
    
      // Setup the Peripheral GAPRole Profile. For more information see the User's Guide:
      // http://software-dl.ti.com/lprf/ble5stack-docs-latest/html/ble-stack/gaprole.html
      {
        // Device starts advertising upon initialization of GAP
        uint8_t initialAdvertEnable = TRUE;
    
        // By setting this to zero, the device will go into the waiting state after
        // being discoverable for 30.72 second, and will not being advertising again
        // until re-enabled by the application
        uint16_t advertOffTime = 0;
    
        uint8_t enableUpdateRequest = DEFAULT_ENABLE_UPDATE_REQUEST;
        uint16_t desiredMinInterval = DEFAULT_DESIRED_MIN_CONN_INTERVAL;
        uint16_t desiredMaxInterval = DEFAULT_DESIRED_MAX_CONN_INTERVAL;
        uint16_t desiredSlaveLatency = DEFAULT_DESIRED_SLAVE_LATENCY;
        uint16_t desiredConnTimeout = DEFAULT_DESIRED_CONN_TIMEOUT;
    
        // Set the Peripheral GAPRole Parameters
        //GAPRole_SetParameter(GAPROLE_ADVERT_ENABLED, sizeof(uint8_t),
        //                     &initialAdvertEnable);
        //GAPRole_SetParameter(GAPROLE_ADVERT_OFF_TIME, sizeof(uint16_t),
                //             &advertOffTime);
    
        GAPRole_SetParameter(GAPROLE_SCAN_RSP_DATA, sizeof(scanRspData),
                             scanRspData);
        GAPRole_SetParameter(GAPROLE_ADVERT_DATA, sizeof(advertData), advertData);
    
        GAPRole_SetParameter(GAPROLE_PARAM_UPDATE_ENABLE, sizeof(uint8_t),
                             &enableUpdateRequest);
        GAPRole_SetParameter(GAPROLE_MIN_CONN_INTERVAL, sizeof(uint16_t),
                             &desiredMinInterval);
        GAPRole_SetParameter(GAPROLE_MAX_CONN_INTERVAL, sizeof(uint16_t),
                             &desiredMaxInterval);
        GAPRole_SetParameter(GAPROLE_SLAVE_LATENCY, sizeof(uint16_t),
                             &desiredSlaveLatency);
        GAPRole_SetParameter(GAPROLE_TIMEOUT_MULTIPLIER, sizeof(uint16_t),
                             &desiredConnTimeout);
      }
    
      // Set the Device Name characteristic in the GAP GATT Service
      // For more information, see the section in the User's Guide:
      // http://software-dl.ti.com/lprf/ble5stack-docs-latest/html/ble-stack/gaprole.html
      GGS_SetParameter(GGS_DEVICE_NAME_ATT, GAP_DEVICE_NAME_LEN, attDeviceName);
    
      // Set GAP Parameters to set the advertising interval
      // For more information, see the GAP section of the User's Guide:
      // http://software-dl.ti.com/lprf/ble5stack-docs-latest/html/ble-stack/gatt.html#gap-gatt-service-ggs
      {
        // Use the same interval for general and limited advertising.
        // Note that only general advertising will occur based on the above configuration
        uint16_t advInt = DEFAULT_ADVERTISING_INTERVAL;
    
        GAP_SetParamValue(TGAP_LIM_DISC_ADV_INT_MIN, advInt);
        GAP_SetParamValue(TGAP_LIM_DISC_ADV_INT_MAX, advInt);
        GAP_SetParamValue(TGAP_GEN_DISC_ADV_INT_MIN, advInt);
        GAP_SetParamValue(TGAP_GEN_DISC_ADV_INT_MAX, advInt);
      }
    
      // Setup the GAP Bond Manager. For more information see the section in the
      // User's Guide:
      // http://software-dl.ti.com/lprf/ble5stack-docs-latest/html/ble-stack/gapbondmngr.html#
      {
        // Hard code the passkey that will be used for pairing. The GAPBondMgr will
        // use this key instead of issuing a callback to the application. This only
        // works if both sides of the connection know to use this same key at
        // compile-time.
        uint32_t passkey = 0; // passkey "000000"
        // Don't send a pairing request after connecting; the peer device must
        // initiate pairing
        uint8_t pairMode = GAPBOND_PAIRING_MODE_WAIT_FOR_REQ;
        // Use authenticated pairing: require passcode.
        uint8_t mitm = TRUE;
        // This device only has display capabilities. Therefore, it will display the
        // passcode during pairing. However, since the default passcode is being
        // used, there is no need to display anything.
        uint8_t ioCap = GAPBOND_IO_CAP_DISPLAY_ONLY;
        // Request bonding (storing long-term keys for re-encryption upon subsequent
        // connections without repairing)
        uint8_t bonding = TRUE;
    
        GAPBondMgr_SetParameter(GAPBOND_DEFAULT_PASSCODE, sizeof(uint32_t),
                                &passkey);
        GAPBondMgr_SetParameter(GAPBOND_PAIRING_MODE, sizeof(uint8_t), &pairMode);
        GAPBondMgr_SetParameter(GAPBOND_MITM_PROTECTION, sizeof(uint8_t), &mitm);
        GAPBondMgr_SetParameter(GAPBOND_IO_CAPABILITIES, sizeof(uint8_t), &ioCap);
        GAPBondMgr_SetParameter(GAPBOND_BONDING_ENABLED, sizeof(uint8_t), &bonding);
      }
    
      // Initialize GATT attributes
      GGS_AddService(GATT_ALL_SERVICES);           // GAP GATT Service
      GATTServApp_AddService(GATT_ALL_SERVICES);   // GATT Service
      DevInfo_AddService();                        // Device Information Service
    
    #ifndef FEATURE_OAD_ONCHIP
      SimpleProfile_AddService(GATT_ALL_SERVICES); // Simple GATT Profile
    #endif //!FEATURE_OAD_ONCHIP
    
    #ifdef FEATURE_OAD
      VOID OAD_addService();                       // OAD Profile
      OAD_register((oadTargetCBs_t *)&simpleBLEPeripheral_oadCBs);
      hOadQ = Util_constructQueue(&oadQ);
    #endif //FEATURE_OAD
    
    #ifdef IMAGE_INVALIDATE
      Reset_addService();
    #endif //IMAGE_INVALIDATE
    
    
    #ifndef FEATURE_OAD_ONCHIP
      // Setup the SimpleProfile Characteristic Values
      // For more information, see the sections in the User's Guide:
      // http://software-dl.ti.com/lprf/ble5stack-docs-latest/html/ble-stack/gatt.html#
      // http://software-dl.ti.com/lprf/ble5stack-docs-latest/html/ble-stack/gatt.html#gattservapp-module
      {
        uint8_t charValue1 = 1;
        uint8_t charValue2 = 2;
        uint8_t charValue3 = 3;
        uint8_t charValue4 = 4;
        uint8_t charValue5[SIMPLEPROFILE_CHAR5_LEN] = { 1, 2, 3, 4, 5 };
    
        SimpleProfile_SetParameter(SIMPLEPROFILE_CHAR1, sizeof(uint8_t),
                                   &charValue1);
        SimpleProfile_SetParameter(SIMPLEPROFILE_CHAR2, sizeof(uint8_t),
                                   &charValue2);
        SimpleProfile_SetParameter(SIMPLEPROFILE_CHAR3, sizeof(uint8_t),
                                   &charValue3);
        SimpleProfile_SetParameter(SIMPLEPROFILE_CHAR4, sizeof(uint8_t),
                                   &charValue4);
        SimpleProfile_SetParameter(SIMPLEPROFILE_CHAR5, SIMPLEPROFILE_CHAR5_LEN,
                                   charValue5);
      }
    
      // Register callback with SimpleGATTprofile
      SimpleProfile_RegisterAppCBs(&SimpleBLEPeripheral_simpleProfileCBs);
    #endif //!FEATURE_OAD_ONCHIP
    
      // Start Bond Manager and register callback
      VOID GAPBondMgr_Register(&simpleBLEPeripheral_BondMgrCBs);
    
      // Register with GAP for HCI/Host messages. This is needed to receive HCI
      // events. For more information, see the section in the User's Guide:
      // http://software-dl.ti.com/lprf/ble5stack-docs-latest/html/ble-stack/hci.html
      GAP_RegisterForMsgs(selfEntity);
    
      // Register for GATT local events and ATT Responses pending for transmission
      GATT_RegisterForMsgs(selfEntity);
    
      // Set default values for Data Length Extension
      // This should be included only if Extended Data Length Feature is enabled
      // in build_config.opt in stack project.
      {
        //Set initial values to maximum, RX is set to max. by default(251 octets, 2120us)
        #define APP_SUGGESTED_PDU_SIZE 251 //default is 27 octets(TX)
        #define APP_SUGGESTED_TX_TIME 2120 //default is 328us(TX)
    
        // This API is documented in hci.h
        // See BLE5-Stack User's Guide for information on using this command:
        // http://software-dl.ti.com/lprf/ble5stack-docs-latest/html/ble-stack/data-length-extensions.html
        // HCI_LE_WriteSuggestedDefaultDataLenCmd(APP_SUGGESTED_PDU_SIZE, APP_SUGGESTED_TX_TIME);
      }
    
    #if defined (BLE_V42_FEATURES) && (BLE_V42_FEATURES & PRIVACY_1_2_CFG)
      // Initialize GATT Client
      GATT_InitClient();
    
      // This line masks the Resolvable Private Address Only (RPAO) Characteristic
      // in the GAP GATT Server from being detected by remote devices. This value
      // cannot be toggled without power cycling but should remain consistent across
      // power-cycles. Removing this command when Privacy is used will cause this
      // device to be treated in Network Privacy Mode by bonded devices - this means
      // that after disconnecting they will not respond to this device's PDUs which
      // contain its Identity Address.
      // Devices wanting to use Network Privacy Mode with other BT5 devices, this
      // line should be commented out.
      GGS_SetParamValue(GGS_DISABLE_RPAO_CHARACTERISTIC);
    #endif // BLE_V42_FEATURES & PRIVACY_1_2_CFG
    
    #if !defined(Display_DISABLE_ALL)
      // Set the title of the main menu
      #if defined FEATURE_OAD
        #if defined (HAL_IMAGE_A)
          TBM_SET_TITLE(&sbpMenuMain, "BLE Peripheral A");
        #else
          TBM_SET_TITLE(&sbpMenuMain, "BLE Peripheral B");
        #endif // HAL_IMAGE_A
      #else
        TBM_SET_TITLE(&sbpMenuMain, "BLE Peripheral");
      #endif // FEATURE_OAD
    
      // Initialize Two-Button Menu module
      tbm_setItemStatus(&sbpMenuMain, TBM_ITEM_NONE, TBM_ITEM_ALL);
      tbm_initTwoBtnMenu(dispHandle, &sbpMenuMain, 3, NULL);
    
      // Init key debouncer
      Board_initKeys(SimpleBLEPeripheral_keyChangeHandler);
    #endif  // !Display_DISABLE_ALL
    
    #if !defined (USE_LL_CONN_PARAM_UPDATE)
      // Get the currently set local supported LE features
      // The will result in a HCI_LE_READ_LOCAL_SUPPORTED_FEATURES event that
      // will get received in the main task processing loop. At this point,
      // feature bits can be set / cleared and the features can be updated.
      HCI_LE_ReadLocalSupportedFeaturesCmd();
    #endif // !defined (USE_LL_CONN_PARAM_UPDATE)
    
      // Start the GAPRole
      VOID GAPRole_StartDevice(&SimpleBLEPeripheral_gapRoleCBs);
      
      
      HwGPIOInit();
      HwGPIOSet(Board_RLED,1);
    }
    
    /*********************************************************************
     * @fn      SimpleBLEPeripheral_taskFxn
     *
     * @brief   Application task entry point for the Simple Peripheral.
     *
     * @param   a0, a1 - not used.
     *
     * @return  None.
     */
    static void SimpleBLEPeripheral_taskFxn(UArg a0, UArg a1)
    {
    #define ADV_MAX_LEN 31
    #define ADVDATA_MANUF_DATA_IDX 5
    // ==== END SOLUTION ====
    // How often to perform periodic event (in msec)
    // #define SBP_PERIODIC_EVT_PERIOD    10000  // 10 seconds.
    #define ADV_PERIODIC_EVT Event_Id_03
    #define ADV_PERIODIC_EVT_PERIOD 10000
    uint8_t advertEnabledPeriod = TRUE;
    static Clock_Struct periodicClockAdv;
    
    
      // Initialize application
      SimpleBLEPeripheral_init();
    
      // Application main loop
      for (;;)
      {
        uint32_t events;
    
        // Waits for an event to be posted associated with the calling thread.
        // Note that an event associated with a thread is posted when a
        // message is queued to the message receive queue of the thread
        events = Event_pend(syncEvent, Event_Id_NONE, SBP_ALL_EVENTS,
                            ICALL_TIMEOUT_FOREVER);
    
    
       if (events)
        {
          ICall_EntityID dest;
          ICall_ServiceEnum src;
          ICall_HciExtEvt *pMsg = NULL;
    
          // Fetch any available messages that might have been sent from the stack
          if (ICall_fetchServiceMsg(&src, &dest,
                                    (void **)&pMsg) == ICALL_ERRNO_SUCCESS)
          {
            uint8 safeToDealloc = TRUE;
    
            if ((src == ICALL_SERVICE_CLASS_BLE) && (dest == selfEntity))
            {
              ICall_Stack_Event *pEvt = (ICall_Stack_Event *)pMsg;
    
              // Check for BLE stack events first
              if (pEvt->signature == 0xffff)
              {
                // The GATT server might have returned a blePending as it was trying
                // to process an ATT Response. Now that we finished with this
                // connection event, let's try sending any remaining ATT Responses
                // on the next connection event.
                if (pEvt->event_flag & SBP_HCI_CONN_EVT_END_EVT)
                {
                  // Try to retransmit pending ATT Response (if any)
                  SimpleBLEPeripheral_sendAttRsp();
                }
              }
              else
              {
                // Process inter-task message
                safeToDealloc = SimpleBLEPeripheral_processStackMsg((ICall_Hdr *)pMsg);
              }
            }
    
            if (pMsg && safeToDealloc)
            {
              ICall_freeMsg(pMsg);
            }
          }
    
          // If RTOS queue is not empty, process app message.
          if (events & SBP_QUEUE_EVT)
          {
            while (!Queue_empty(appMsgQueue))
            {
              sbpEvt_t *pMsg = (sbpEvt_t *)Util_dequeueMsg(appMsgQueue);
              if (pMsg)
              {
                // Process message.
                SimpleBLEPeripheral_processAppMsg(pMsg);
    
                // Free the space from the message.
                ICall_free(pMsg);
              }
            }
          }
    if ( events & ADV_PERIODIC_EVT)
    
    
    {
    Util_startClock(&periodicClockAdv);
    if(advertEnabledPeriod == TRUE){
    advertEnabledPeriod=FALSE;
    } else {
    advertEnabledPeriod=TRUE;
    }
    
    
    GAPRole_SetParameter(GAPROLE_ADVERT_ENABLED, sizeof(uint8_t),
    &advertEnabledPeriod);
    
      }
    
    
          if (events & SBP_PERIODIC_EVT)
          {
            Util_startClock(&periodicClock);
    
            // Perform periodic application task
            SimpleBLEPeripheral_performPeriodicTask();
          }
    
    #ifdef FEATURE_OAD
          if (events & SBP_QUEUE_PING_EVT)
          {
            while (!Queue_empty(hOadQ))
            {
              oadTargetWrite_t *oadWriteEvt = Queue_get(hOadQ);
    
              // Identify new image.
              if (oadWriteEvt->event == OAD_WRITE_IDENTIFY_REQ)
              {
                OAD_imgIdentifyWrite(oadWriteEvt->connHandle, oadWriteEvt->pData);
              }
              // Write a next block request.
              else if (oadWriteEvt->event == OAD_WRITE_BLOCK_REQ)
              {
                OAD_imgBlockWrite(oadWriteEvt->connHandle, oadWriteEvt->pData);
              }
    
              // Free buffer.
              ICall_free(oadWriteEvt);
            }
          }
    #endif //FEATURE_OAD
        }
      }
    }
    
    /*********************************************************************
     * @fn      SimpleBLEPeripheral_processStackMsg
     *
     * @brief   Process an incoming stack message.
     *
     * @param   pMsg - message to process
     *
     * @return  TRUE if safe to deallocate incoming message, FALSE otherwise.
     */
    static uint8_t SimpleBLEPeripheral_processStackMsg(ICall_Hdr *pMsg)
    {
      uint8_t safeToDealloc = TRUE;
    
      switch (pMsg->event)
      {
        case GATT_MSG_EVENT:
          // Process GATT message
          safeToDealloc = SimpleBLEPeripheral_processGATTMsg((gattMsgEvent_t *)pMsg);
          break;
    
        case HCI_GAP_EVENT_EVENT:
          {
    
            // Process HCI message
            switch(pMsg->status)
            {
              case HCI_COMMAND_COMPLETE_EVENT_CODE:
                // Process HCI Command Complete Event
                {
    
    #if !defined (USE_LL_CONN_PARAM_UPDATE)
                  // This code will disable the use of the LL_CONNECTION_PARAM_REQ
                  // control procedure (for connection parameter updates, the
                  // L2CAP Connection Parameter Update procedure will be used
                  // instead). To re-enable the LL_CONNECTION_PARAM_REQ control
                  // procedures, define the symbol USE_LL_CONN_PARAM_UPDATE
                  // The L2CAP Connection Parameter Update procedure is used to
                  // support a delta between the minimum and maximum connection
                  // intervals required by some iOS devices.
    
                  // Parse Command Complete Event for opcode and status
                  hciEvt_CmdComplete_t* command_complete = (hciEvt_CmdComplete_t*) pMsg;
                  uint8_t   pktStatus = command_complete->pReturnParam[0];
    
                  //find which command this command complete is for
                  switch (command_complete->cmdOpcode)
                  {
                    case HCI_LE_READ_LOCAL_SUPPORTED_FEATURES:
                      {
                        if (pktStatus == SUCCESS)
                        {
                          uint8_t featSet[8];
    
                          // Get current feature set from received event (bits 1-9
                          // of the returned data
                          memcpy( featSet, &command_complete->pReturnParam[1], 8 );
    
                          // Clear bit 1 of byte 0 of feature set to disable LL
                          // Connection Parameter Updates
                          CLR_FEATURE_FLAG( featSet[0], LL_FEATURE_CONN_PARAMS_REQ );
    
                          // Update controller with modified features
                          HCI_EXT_SetLocalSupportedFeaturesCmd( featSet );
                        }
                      }
                      break;
    
                    default:
                      //do nothing
                      break;
                  }
    #endif // !defined (USE_LL_CONN_PARAM_UPDATE)
    
                }
                break;
    
              case HCI_BLE_HARDWARE_ERROR_EVENT_CODE:
                AssertHandler(HAL_ASSERT_CAUSE_HARDWARE_ERROR,0);
                break;
    
              // LE Events
              case HCI_LE_EVENT_CODE:
                {
                  hciEvt_BLEPhyUpdateComplete_t *pPUC
                    = (hciEvt_BLEPhyUpdateComplete_t*) pMsg;
    
                  // A Phy Update Has Completed or Failed
                  if (pPUC->BLEEventCode == HCI_BLE_PHY_UPDATE_COMPLETE_EVENT)
                  {
                    if (pPUC->status != SUCCESS)
                    {
                      Display_print0(dispHandle, SBP_ROW_STATUS_1, 0,
                                     "PHY Change failure");
                    }
                    else
                    {
                      Display_print0(dispHandle, SBP_ROW_STATUS_1, 0,
                                     "PHY Update Complete");
                      // Only symmetrical PHY is supported.
                      // rxPhy should be equal to txPhy.
                      Display_print1(dispHandle, SBP_ROW_STATUS_2, 0,
                                     "Current PHY: %s",
                                     (pPUC->rxPhy == HCI_PHY_1_MBPS) ? "1 Mbps" :
    
    // Note: BLE_V50_FEATURES is always defined and long range phy (PHY_LR_CFG) is
    //       defined in build_config.opt
    #if (BLE_V50_FEATURES & PHY_LR_CFG)
                                       ((pPUC->rxPhy == HCI_PHY_2_MBPS) ? "2 Mbps" :
                                           "Coded:S2"));
    #else  // !PHY_LR_CFG
                                       "2 Mbps");
    #endif // PHY_LR_CFG
                    }
                  }
                }
                break;
    
              default:
                break;
            }
          }
          break;
    
          default:
            // do nothing
            break;
    
        }
    
      return (safeToDealloc);
    }
    
    /*********************************************************************
     * @fn      SimpleBLEPeripheral_processGATTMsg
     *
     * @brief   Process GATT messages and events.
     *
     * @return  TRUE if safe to deallocate incoming message, FALSE otherwise.
     */
    static uint8_t SimpleBLEPeripheral_processGATTMsg(gattMsgEvent_t *pMsg)
    {
      // See if GATT server was unable to transmit an ATT response
      if (pMsg->hdr.status == blePending)
      {
        // No HCI buffer was available. Let's try to retransmit the response
        // on the next connection event.
        if (HCI_EXT_ConnEventNoticeCmd(pMsg->connHandle, selfEntity,
                                       SBP_HCI_CONN_EVT_END_EVT) == SUCCESS)
        {
          // First free any pending response
          SimpleBLEPeripheral_freeAttRsp(FAILURE);
    
          // Hold on to the response message for retransmission
          pAttRsp = pMsg;
    
          // Don't free the response message yet
          return (FALSE);
        }
      }
      else if (pMsg->method == ATT_FLOW_CTRL_VIOLATED_EVENT)
      {
        // ATT request-response or indication-confirmation flow control is
        // violated. All subsequent ATT requests or indications will be dropped.
        // The app is informed in case it wants to drop the connection.
    
        // Display the opcode of the message that caused the violation.
        Display_print1(dispHandle, SBP_ROW_RESULT, 0, "FC Violated: %d", pMsg->msg.flowCtrlEvt.opcode);
      }
      else if (pMsg->method == ATT_MTU_UPDATED_EVENT)
      {
        // MTU size updated
        Display_print1(dispHandle, SBP_ROW_RESULT, 0, "MTU Size: %d", pMsg->msg.mtuEvt.MTU);
      }
    
      // Free message payload. Needed only for ATT Protocol messages
      GATT_bm_free(&pMsg->msg, pMsg->method);
    
      // It's safe to free the incoming message
      return (TRUE);
    }
    
    /*********************************************************************
     * @fn      SimpleBLEPeripheral_sendAttRsp
     *
     * @brief   Send a pending ATT response message.
     *
     * @param   none
     *
     * @return  none
     */
    static void SimpleBLEPeripheral_sendAttRsp(void)
    {
      // See if there's a pending ATT Response to be transmitted
      if (pAttRsp != NULL)
      {
        uint8_t status;
    
        // Increment retransmission count
        rspTxRetry++;
    
        // Try to retransmit ATT response till either we're successful or
        // the ATT Client times out (after 30s) and drops the connection.
        status = GATT_SendRsp(pAttRsp->connHandle, pAttRsp->method, &(pAttRsp->msg));
        if ((status != blePending) && (status != MSG_BUFFER_NOT_AVAIL))
        {
          // Disable connection event end notice
          HCI_EXT_ConnEventNoticeCmd(pAttRsp->connHandle, selfEntity, 0);
    
          // We're done with the response message
          SimpleBLEPeripheral_freeAttRsp(status);
        }
        else
        {
          // Continue retrying
          Display_print1(dispHandle, SBP_ROW_STATUS_1, 0, "Rsp send retry: %d", rspTxRetry);
        }
      }
    }
    
    /*********************************************************************
     * @fn      SimpleBLEPeripheral_freeAttRsp
     *
     * @brief   Free ATT response message.
     *
     * @param   status - response transmit status
     *
     * @return  none
     */
    static void SimpleBLEPeripheral_freeAttRsp(uint8_t status)
    {
      // See if there's a pending ATT response message
      if (pAttRsp != NULL)
      {
        // See if the response was sent out successfully
        if (status == SUCCESS)
        {
          Display_print1(dispHandle, SBP_ROW_STATUS_1, 0, "Rsp sent retry: %d", rspTxRetry);
        }
        else
        {
          // Free response payload
          GATT_bm_free(&pAttRsp->msg, pAttRsp->method);
    
          Display_print1(dispHandle, SBP_ROW_STATUS_1, 0, "Rsp retry failed: %d", rspTxRetry);
        }
    
        // Free response message
        ICall_freeMsg(pAttRsp);
    
        // Reset our globals
        pAttRsp = NULL;
        rspTxRetry = 0;
      }
    }
    
    /*********************************************************************
     * @fn      SimpleBLEPeripheral_processAppMsg
     *
     * @brief   Process an incoming callback from a profile.
     *
     * @param   pMsg - message to process
     *
     * @return  None.
     */
    static void SimpleBLEPeripheral_processAppMsg(sbpEvt_t *pMsg)
    {
      switch (pMsg->hdr.event)
      {
        case SBP_STATE_CHANGE_EVT:
          SimpleBLEPeripheral_processStateChangeEvt((gaprole_States_t)pMsg->
                                                    hdr.state);
          break;
    
        case SBP_CHAR_CHANGE_EVT:
          SimpleBLEPeripheral_processCharValueChangeEvt(pMsg->hdr.state);
          break;
    
    #if !defined(Display_DISABLE_ALL)
        case SBP_KEY_CHANGE_EVT:
          SimpleBLEPeripheral_handleKeys(pMsg->hdr.state);
          break;
    #endif  // !Display_DISABLE_ALL
    
        default:
          // Do nothing.
          break;
      }
    }
    
    /*********************************************************************
     * @fn      SimpleBLEPeripheral_stateChangeCB
     *
     * @brief   Callback from GAP Role indicating a role state change.
     *
     * @param   newState - new state
     *
     * @return  None.
     */
    static void SimpleBLEPeripheral_stateChangeCB(gaprole_States_t newState)
    {
      SimpleBLEPeripheral_enqueueMsg(SBP_STATE_CHANGE_EVT, newState);
    }
    
    /*********************************************************************
     * @fn      SimpleBLEPeripheral_processStateChangeEvt
     *
     * @brief   Process a pending GAP Role state change event.
     *
     * @param   newState - new state
     *
     * @return  None.
     */
    static void SimpleBLEPeripheral_processStateChangeEvt(gaprole_States_t newState)
    {
    #ifdef PLUS_BROADCASTER
      static bool firstConnFlag = false;
    #endif // PLUS_BROADCASTER
    
      switch ( newState )
      {
        case GAPROLE_STARTED:
          {
            uint8_t ownAddress[B_ADDR_LEN];
            uint8_t systemId[DEVINFO_SYSTEM_ID_LEN];
    
            GAPRole_GetParameter(GAPROLE_BD_ADDR, ownAddress);
    
            // use 6 bytes of device address for 8 bytes of system ID value
            systemId[0] = ownAddress[0];
            systemId[1] = ownAddress[1];
            systemId[2] = ownAddress[2];
    
            // set middle bytes to zero
            systemId[4] = 0x00;
            systemId[3] = 0x00;
    
            // shift three bytes up
            systemId[7] = ownAddress[5];
            systemId[6] = ownAddress[4];
            systemId[5] = ownAddress[3];
    
            DevInfo_SetParameter(DEVINFO_SYSTEM_ID, DEVINFO_SYSTEM_ID_LEN, systemId);
    
            // Display device address
            Display_print0(dispHandle, SBP_ROW_BDADDR, 0, Util_convertBdAddr2Str(ownAddress));
            Display_print0(dispHandle, SBP_ROW_ROLESTATE, 0, "Initialized");
          }
          break;
    
        case GAPROLE_ADVERTISING:
          Display_print0(dispHandle, SBP_ROW_ROLESTATE, 0, "Advertising");
          break;
    
    #ifdef PLUS_BROADCASTER
        // After a connection is dropped, a device in PLUS_BROADCASTER will continue
        // sending non-connectable advertisements and shall send this change of
        // state to the application.  These are then disabled here so that sending
        // connectable advertisements can resume.
        case GAPROLE_ADVERTISING_NONCONN:
          {
            uint8_t advertEnabled = FALSE;
    
            // Disable non-connectable advertising.
            //GAPRole_SetParameter(GAPROLE_ADV_NONCONN_ENABLED, sizeof(uint8_t),
                     //          &advertEnabled);
    
            advertEnabled = TRUE;
    
            // Enabled connectable advertising.
           // GAPRole_SetParameter(GAPROLE_ADVERT_ENABLED, sizeof(uint8_t),
             //                    &advertEnabled);
    
            // Reset flag for next connection.
            firstConnFlag = false;
    
            SimpleBLEPeripheral_freeAttRsp(bleNotConnected);
          }
          break;
    #endif //PLUS_BROADCASTER
    
        case GAPROLE_CONNECTED:
          {
            linkDBInfo_t linkInfo;
            uint8_t numActive = 0;
    
            Util_startClock(&periodicClock);
    
            numActive = linkDB_NumActive();
    
            // Use numActive to determine the connection handle of the last
            // connection
            if ( linkDB_GetInfo( numActive - 1, &linkInfo ) == SUCCESS )
            {
              Display_print1(dispHandle, SBP_ROW_ROLESTATE, 0, "Num Conns: %d", (uint16_t)numActive);
              Display_print0(dispHandle, SBP_ROW_STATUS_1, 0, Util_convertBdAddr2Str(linkInfo.addr));
            }
            else
            {
              uint8_t peerAddress[B_ADDR_LEN];
    
              GAPRole_GetParameter(GAPROLE_CONN_BD_ADDR, peerAddress);
    
              Display_print0(dispHandle, SBP_ROW_ROLESTATE, 0, "Connected");
              Display_print0(dispHandle, SBP_ROW_STATUS_1, 0, Util_convertBdAddr2Str(peerAddress));
            }
    
    #if !defined(Display_DISABLE_ALL)
            tbm_setItemStatus(&sbpMenuMain, TBM_ITEM_ALL, TBM_ITEM_NONE);
    #endif  // !Display_DISABLE_ALL
    
            #ifdef PLUS_BROADCASTER
              // Only turn advertising on for this state when we first connect
              // otherwise, when we go from connected_advertising back to this state
              // we will be turning advertising back on.
              if (firstConnFlag == false)
              {
                uint8_t advertEnabled = FALSE; // Turn on Advertising
    
                // Disable connectable advertising.
                //GAPRole_SetParameter(GAPROLE_ADVERT_ENABLED, sizeof(uint8_t),
                            //         &advertEnabled);
    
                // Set to true for non-connectable advertising.
                advertEnabled = TRUE;
    
                // Enable non-connectable advertising.
              //  GAPRole_SetParameter(GAPROLE_ADV_NONCONN_ENABLED, sizeof(uint8_t),
                                //     &advertEnabled);
                firstConnFlag = true;
              }
            #endif // PLUS_BROADCASTER
          }
          break;
    
        case GAPROLE_CONNECTED_ADV:
          Display_print0(dispHandle, SBP_ROW_ROLESTATE, 0, "Connected Advertising");
          break;
    
        case GAPROLE_WAITING:
          Util_stopClock(&periodicClock);
          SimpleBLEPeripheral_freeAttRsp(bleNotConnected);
    
          Display_print0(dispHandle, SBP_ROW_ROLESTATE, 0, "Disconnected");
    
    #if !defined(Display_DISABLE_ALL)
          // Disable PHY change
          tbm_setItemStatus(&sbpMenuMain, TBM_ITEM_NONE, TBM_ITEM_ALL);
    #endif  // !Display_DISABLE_ALL
    
          // Clear remaining lines
          Display_clearLines(dispHandle, SBP_ROW_RESULT, SBP_ROW_STATUS_2);
          break;
    
        case GAPROLE_WAITING_AFTER_TIMEOUT:
          SimpleBLEPeripheral_freeAttRsp(bleNotConnected);
    
          Display_print0(dispHandle, SBP_ROW_RESULT, 0, "Timed Out");
    
    #if !defined(Display_DISABLE_ALL)
          tbm_setItemStatus(&sbpMenuMain, TBM_ITEM_NONE, TBM_ITEM_ALL);
    #endif  // !Display_DISABLE_ALL
    
          // Clear remaining lines
          Display_clearLines(dispHandle, SBP_ROW_STATUS_1, SBP_ROW_STATUS_2);
    
          #ifdef PLUS_BROADCASTER
            // Reset flag for next connection.
            firstConnFlag = false;
          #endif // PLUS_BROADCASTER
          break;
    
        case GAPROLE_ERROR:
          Display_print0(dispHandle, SBP_ROW_RESULT, 0, "Error");
          break;
    
        default:
          Display_clearLines(dispHandle, SBP_ROW_RESULT, SBP_ROW_STATUS_2);
          break;
      }
    
    }
    
    #ifndef FEATURE_OAD_ONCHIP
    /*********************************************************************
     * @fn      SimpleBLEPeripheral_charValueChangeCB
     *
     * @brief   Callback from Simple Profile indicating a characteristic
     *          value change.
     *
     * @param   paramID - parameter ID of the value that was changed.
     *
     * @return  None.
     */
    static void SimpleBLEPeripheral_charValueChangeCB(uint8_t paramID)
    {
      SimpleBLEPeripheral_enqueueMsg(SBP_CHAR_CHANGE_EVT, paramID);
    }
    #endif //!FEATURE_OAD_ONCHIP
    
    /*********************************************************************
     * @fn      SimpleBLEPeripheral_processCharValueChangeEvt
     *
     * @brief   Process a pending Simple Profile characteristic value change
     *          event.
     *
     * @param   paramID - parameter ID of the value that was changed.
     *
     * @return  None.
     */
    static void SimpleBLEPeripheral_processCharValueChangeEvt(uint8_t paramID)
    {
    #ifndef FEATURE_OAD_ONCHIP
      uint8_t newValue;
    
      switch(paramID)
      {
        case SIMPLEPROFILE_CHAR1:
          SimpleProfile_GetParameter(SIMPLEPROFILE_CHAR1, &newValue);
    
          Display_print1(dispHandle, SBP_ROW_STATUS_1, 0, "Char 1: %d", (uint16_t)newValue);
          break;
    
        case SIMPLEPROFILE_CHAR3:
          SimpleProfile_GetParameter(SIMPLEPROFILE_CHAR3, &newValue);
    
          Display_print1(dispHandle, SBP_ROW_STATUS_1, 0, "Char 3: %d", (uint16_t)newValue);
          break;
    
        default:
          // should not reach here!
          break;
      }
    #endif //!FEATURE_OAD_ONCHIP
    }
    
    /*********************************************************************
     * @fn      SimpleBLEPeripheral_performPeriodicTask
     *
     * @brief   Perform a periodic application task. This function gets called
     *          every five seconds (SBP_PERIODIC_EVT_PERIOD). In this example,
     *          the value of the third characteristic in the SimpleGATTProfile
     *          service is retrieved from the profile, and then copied into the
     *          value of the the fourth characteristic.
     *
     * @param   None.
     *
     * @return  None.
     */
    static void SimpleBLEPeripheral_performPeriodicTask(void)
    {
    #ifndef FEATURE_OAD_ONCHIP
      uint8_t valueToCopy;
    
      // Call to retrieve the value of the third characteristic in the profile
      if (SimpleProfile_GetParameter(SIMPLEPROFILE_CHAR3, &valueToCopy) == SUCCESS)
      {
        // Call to set that value of the fourth characteristic in the profile.
        // Note that if notifications of the fourth characteristic have been
        // enabled by a GATT client device, then a notification will be sent
        // every time this function is called.
        SimpleProfile_SetParameter(SIMPLEPROFILE_CHAR4, sizeof(uint8_t),
                                   &valueToCopy);
      }
    #endif //!FEATURE_OAD_ONCHIP
    }
    
    
    #ifdef FEATURE_OAD
    /*********************************************************************
     * @fn      SimpleBLEPeripheral_processOadWriteCB
     *
     * @brief   Process a write request to the OAD profile.
     *
     * @param   event      - event type:
     *                       OAD_WRITE_IDENTIFY_REQ
     *                       OAD_WRITE_BLOCK_REQ
     * @param   connHandle - the connection Handle this request is from.
     * @param   pData      - pointer to data for processing and/or storing.
     *
     * @return  None.
     */
    void SimpleBLEPeripheral_processOadWriteCB(uint8_t event, uint16_t connHandle,
                                               uint8_t *pData)
    {
      oadTargetWrite_t *oadWriteEvt = ICall_malloc( sizeof(oadTargetWrite_t) + \
                                                 sizeof(uint8_t) * OAD_PACKET_SIZE);
    
      if ( oadWriteEvt != NULL )
      {
        oadWriteEvt->event = event;
        oadWriteEvt->connHandle = connHandle;
    
        oadWriteEvt->pData = (uint8_t *)(&oadWriteEvt->pData + 1);
        memcpy(oadWriteEvt->pData, pData, OAD_PACKET_SIZE);
    
        Queue_put(hOadQ, (Queue_Elem *)oadWriteEvt);
    
        // Post the application's event.  For OAD, no event flag is used.
        Event_post(syncEvent, SBP_QUEUE_PING_EVT);
      }
      else
      {
        // Fail silently.
      }
    }
    #endif //FEATURE_OAD
    
    /*********************************************************************
     * @fn      SimpleBLEPeripheral_clockHandler
     *
     * @brief   Handler function for clock timeouts.
     *
     * @param   arg - event type
     *
     * @return  None.
     */
    static void SimpleBLEPeripheral_clockHandler(UArg arg)
    {
    //uint32_t events;
    
    // Store the event.
     //events |= arg;
    
      // Wake up the application.
      // Semaphore_post(sem);
      // Wake up the application.
    // Event_post (events,arg);
    Event_post(syncEvent, ADV_PERIODIC_EVT);
    
    }
    
    #if !defined(Display_DISABLE_ALL)
    /*********************************************************************
     * @fn      SimpleBLEPeripheral_keyChangeHandler
     *
     * @brief   Key event handler function
     *
     * @param   keys - bitmap of pressed keys
     *
     * @return  none
     */
    void SimpleBLEPeripheral_keyChangeHandler(uint8 keys)
    {
      SimpleBLEPeripheral_enqueueMsg(SBP_KEY_CHANGE_EVT, keys);
    }
    #endif  // !Display_DISABLE_ALL
    
    /*********************************************************************
     * @fn      SimpleBLEPeripheral_enqueueMsg
     *
     * @brief   Creates a message and puts the message in RTOS queue.
     *
     * @param   event - message event.
     * @param   state - message state.
     *
     * @return  None.
     */
    static void SimpleBLEPeripheral_enqueueMsg(uint8_t event, uint8_t state)
    {
      sbpEvt_t *pMsg;
    
      // Create dynamic pointer to message.
      if ((pMsg = ICall_malloc(sizeof(sbpEvt_t))))
      {
        pMsg->hdr.event = event;
        pMsg->hdr.state = state;
    
        // Enqueue the message.
        Util_enqueueMsg(appMsgQueue, syncEvent, (uint8*)pMsg);
      }
    }
    
    #if !defined(Display_DISABLE_ALL)
    /*********************************************************************
     * @fn      SimpleBLEPeripheral_handleKeys
     *
     * @brief   Handles all key events for this device.
     *
     * @param   keys - bit field for key events. Valid entries:
     *                 KEY_LEFT
     *                 KEY_RIGHT
     *
     * @return  none
     */
    static void SimpleBLEPeripheral_handleKeys(uint8_t keys)
    {
    
    }
    
    /*********************************************************************
     * @fn      SimpleBLEPeripheral_doSetPhy
     *
     * @brief   Set PHY preference.
     *
     * @param   index - 0: 1M PHY
     *                  1: 2M PHY
     *                  2: 1M + 2M PHY
     *                  3: CODED PHY (Long range) (when PHY_LR_CFG is defined)
     *                  4: 1M + 2M + CODED PHY (when PHY_LR_CFG is defined)
     *
     * @return  always true
     */
    bool SimpleBLEPeripheral_doSetPhy(uint8 index)
    {
      uint8_t gapRoleState;
      uint16_t connectionHandle;
      static uint8_t phy[] = {
        HCI_PHY_1_MBPS, HCI_PHY_2_MBPS, HCI_PHY_1_MBPS | HCI_PHY_2_MBPS,
    
      // Note: BLE_V50_FEATURES is always defined and long range phy (PHY_LR_CFG) is
      //       defined in build_config.opt
      // To use the long range phy, HCI_PHY_CODED needs to be included
      #if (BLE_V50_FEATURES & PHY_LR_CFG)
        HCI_PHY_CODED, HCI_PHY_1_MBPS | HCI_PHY_2_MBPS | HCI_PHY_CODED,
      #endif  // PHY_LR_CFG
      };
    
      GAPRole_GetParameter(GAPROLE_STATE, &gapRoleState);
      GAPRole_GetParameter(GAPROLE_CONNHANDLE, &connectionHandle);
    
      // Set Phy Preference on the current connection. Apply the same value
      // for RX and TX.
      HCI_LE_SetPhyCmd(connectionHandle, 0, phy[index], phy[index], 0);
    
      Display_print1(dispHandle, SBP_ROW_RESULT, 0, "PHY preference: %s",
                     TBM_GET_ACTION_DESC(&sbpMenuMain, index));
    
      Display_clearLine(dispHandle, SBP_ROW_STATUS_1);
    
      return true;
    }
    #endif  // !Display_DISABLE_ALL
    static void SimplePeripheral_init(void)
    {// === SOLUTION [Add Open PIN Driver] ===
    // === SOLUTION [Add LED Support] ===
    /* Pin driver handles */
    static PIN_Handle ledPinHandle;
    
    /* Global memory storage for a PIN_Config table */
    static PIN_State ledPinState;
    
    /*
     * Initial LED pin configuration table
     *   - LEDs Board_LED0 is off.
     */
    PIN_Config ledPinTable[] = {
      Board_LED0 | PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW | PIN_PUSHPULL | PIN_DRVSTR_MAX,
      PIN_TERMINATE
    };
    
    
      ledPinHandle = PIN_open(&ledPinState, ledPinTable);
    // ==== END SOLUTION ====
    
     // #define SBP_ALL_EVENTS                        (SBP_ICALL_EVT        | \
      //                                             SBP_QUEUE_EVT        | \
       //                                            ADV_PERIODIC_EVT		|\
        //                                           SBP_PERIODIC_EVT)
    
    // === SOLUTION [Add Advertising Defines] ===
    
    
    
    }
    /*********************************************************************
    *********************************************************************/
    

  • I see you are using modified simple_peripheral.c in simplelink_cc2640r2_sdk_1_40_00_45. Please try to use original simple_peripheral example in latest SDK 3.30 to test this.

  • Hi YK,

    I got the errors below when I run  original simple_peripheral example in latest SDK 3.30 , if you could help?

    js: "C:/ti/simplelink_cc2640r2_sdk_3_30_00_20/kernel/tirtos/packages/iar/tools/configuro/Main.xs", line 133: Error: xdc.tools.configuro: Error: Can't find the platform package 'ti.platforms.tiva'. TI platforms are n o longer shipped as part of

  • Do you use IAR EWARM-8.32.2? By the way, I would suggest you to use CCS 9.2 to save your time.

  • Hi YK,

    I have upgraded to IAR EWARM-8.32.2, error is the same "Error: Can't find the platform package 'ti.platforms.tiva'. Any idea to fix it?

    Thanks in advance

  • I don't use IAR EWARM-8.32.2 so I don't know how to solve this issue. I already suggest you to use CCS 9.2 instead of IAR.

  • Hi,

    The error you are experiencing has been discussed before on the forum (here).

    Regards,