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.

RTOS/CC2640R2F: How to communicate with CC2640 simultaneously with another CC2640 and my APP

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

Tool/software: TI-RTOS

Hi everyone, I'm working with the CC2640R2F examples and everything works very well. I tested the communication between two devices with the Simple-Central and Simple-Peripheral examples and I also tested the communication of the device with my APP to turn on / off the LEDs. Now I'm thinking of something much more elaborate. Communication between APP, Simple-Peripheral (Multi Role) and Simple-Central. That is I turn on the led with my APP and I close the led with a button of my device (Simple-central). I tested the Multi-Role example, but when I connect with Simple-Central, my phone doesn't see the Multi-role. Why?

Regards

  • Because the code would toggle doWrite to FALSE after it runs first time.
  • Then? Can I to write GATT only?
  • Yes, I had told you many times.
  • I set the doWrite to TRUE and I cooment all part of code that talk of read GATT, but i sent the write only 1 times
  • I remember there’s somewhere in the code would do “doWrite=!doWrite;”. You should comment that line out.
  • Yes, it works. Sorry Yikai the last question.  I implement the SIMPLEPROFILE_CHAR1_UUID for turn on/off the led in the second device, now with SIMPLEPROFILE_CHAR3_UUID I want to turn on/off the second led in the second device. I thought so:

    else if (state == BLE_STATE_CONNECTED)
        {
          if (keys & KEY_RIGHT)
          {
              if (charHdl != 0 &&
                  procedureInProgress == FALSE)
              {
                uint8_t status;
    
                // Do a read or write as long as no other read or write is in progress
                if (doWrite)
                {
                  // Do a write
                  attWriteReq_t req;
    
                  req.pValue = GATT_bm_alloc(connHandle, ATT_WRITE_REQ, 1, NULL);
                  if ( req.pValue != NULL )
                  {
                    req.handle = charHdl;
                    req.len = 1;
                    req.pValue[0] = charVal;
                    req.sig = 0;
                    req.cmd = 0;
    
                    status = GATT_WriteCharValue(connHandle, &req, selfEntity);
                    if ( status != SUCCESS )
                    {
                      GATT_bm_free((gattMsg_t *)&req, ATT_WRITE_REQ);
                    }
                  }
                  else
                  {
                    status = bleMemAllocError;
                  }
                }
               /* else
                {
                  // Do a read
                  attReadReq_t req;
    
                  req.handle = charHdl;
                  status = GATT_ReadCharValue(connHandle, &req, selfEntity);
                }*/
    
                if (status == SUCCESS)
                {
                  procedureInProgress = TRUE;
                  //doWrite = !doWrite;
                }
              }
          } else if(keys & KEY_LEFT) {
              if (charHdl != 0 &&
                            procedureInProgress == FALSE)
                        {
                          uint8_t status;
    
                          // Do a read or write as long as no other read or write is in progress
                          if (doWrite)
                          {
                            // Do a write
                            attWriteReq_t req;
    
                            req.pValue = GATT_bm_alloc(connHandle, ATT_WRITE_REQ, 1, NULL);
                            if ( req.pValue != NULL )
                            {
                              req.handle = charHdl;
                              req.len = 1;
                              req.pValue[0] = charVal1;
                              req.sig = 0;
                              req.cmd = 0;
    
                              status = GATT_WriteCharValue(connHandle, &req, selfEntity);
                              if ( status != SUCCESS )
                              {
                                GATT_bm_free((gattMsg_t *)&req, ATT_WRITE_REQ);
                              }
                            }
                            else
                            {
                              status = bleMemAllocError;
                            }
                          }
                        /*  else
                          {
                            // Do a read
                            attReadReq_t req;
    
                            req.handle = charHdl;
                            status = GATT_ReadCharValue(connHandle, &req, selfEntity);
                          }*/
    
                          if (status == SUCCESS)
                          {
                            procedureInProgress = TRUE;
                           // doWrite = !doWrite;
                          }
                        }
          }
        }
    
        return;
      }

    Then when I click on BUTTON RIGHT turn on led1 of second device and when I click on BUTTON LEFT turn on led2 of second device. Now, how can I implement the SIMPLEPROFILE_CHAR3_UUID in SimpleCentral?

  • I suppose you should use different value in req.handle (SIMPLEPROFILE_CHAR1_UUID or SIMPLEPROFILE_CHAR3_UUID) before you do GATT_WriteCharValue.
  • Then, I should to add a new charHdl? and then a new if condition for to use the button right? It's correct?
  • Yes, you can do it in this way.
  • Thanks for your answer. I write this: 

    else if(keys & KEY_LEFT) {
              if (charHdl1 != 0 &&
                            procedureInProgress == FALSE)
                        {
                          uint8_t status;
    
                          // Do a read or write as long as no other read or write is in progress
                          if (doWrite)
                          {
                            // Do a write
                            attWriteReq_t req;
    
                            req.pValue = GATT_bm_alloc(connHandle, ATT_WRITE_REQ, 1, NULL);
                            if ( req.pValue != NULL )
                            {
                              req.handle = charHdl1;
                              req.len = 1;
                              req.pValue[0] = charVal1;
                              req.sig = 0;
                              req.cmd = 0;
    
                              status = GATT_WriteCharValue(connHandle, &req, selfEntity);
                              if ( status != SUCCESS )
                              {
                                GATT_bm_free((gattMsg_t *)&req, ATT_WRITE_REQ);
                              }
                            }
                            else
                            {
                              status = bleMemAllocError;
                            }
                          }
                          if (status == SUCCESS)
                          {
                            procedureInProgress = TRUE;
                          }
                        }
          }

    And I add a new condition for the write in SimpleCentral_processGATTMsg and SimpleCentral_processGATTDiscEvent. But not works

  • I suggest you to set breakpoint in your code and trace why it doesn't work.
  • If I put KEY_LEFT in the KEY_RIGHT cycle it doesn't work. I did a new cycle, but it doesn't work. Tips?

    if(state == BLE_STATE_CONNECTED){
    if (keys & KEY_LEFT)
    {
    if (charHdl1 != 0 &&
    procedureInProgress == FALSE)
    {
    uint8_t status;

    // Do a read or write as long as no other read or write is in progress
    if (doWrite)
    {
    // Do a write
    attWriteReq_t req;

    req.pValue = GATT_bm_alloc(connHandle, ATT_WRITE_REQ, 1, NULL);
    if ( req.pValue != NULL )
    {
    req.handle = charHdl1;
    req.len = 1;
    req.pValue[0] = charVal1;
    req.sig = 0;
    req.cmd = 0;

    status = GATT_WriteCharValue(connHandle, &req, selfEntity);
    if ( status != SUCCESS )
    {
    GATT_bm_free((gattMsg_t *)&req, ATT_WRITE_REQ);
    }
    }
    else
    {
    status = bleMemAllocError;
    }
    }
    if (status == SUCCESS)
    {
    procedureInProgress = TRUE;
    }
    }
    }
    }
  • In original simple_central example, "if(state == BLE_STATE_CONNECTED)..." would only be checked while KEY_RIGHT is triggered. I don't think your code will work since you shouldn't check "if (keys & KEY_LEFT)..." inside "if(state == BLE_STATE_CONNECTED)...".
  • How should i do? With the right button I have to send CHARACTERISTIC 1 and with the left button I have to send CHARACTERISTIC 3
  • Yes, you can do it in this way.
  • yes, but how?
  • I suppose you can clone whole things inside "if (keys & KEY_RIGHT)..." to "if (keys & KEY_LEFT)..." and set req.handle to two different cases.
  • I try this but KEYS_LEFT not works. Why?

    if (keys & KEY_RIGHT)
      {
        if (state == BLE_STATE_IDLE)
        {
          if (scanIdx == -1)
          {
            if (!scanningStarted)
            {
              scanningStarted = TRUE;
              scanRes = 0;
    
              Display_print0(dispHandle, 2, 0, "Discovering...");
              Display_print0(dispHandle, 3, 0, "");
              Display_print0(dispHandle, 4, 0, "");
              Display_print0(dispHandle, 5, 0, "");
              Display_print0(dispHandle, 6, 0, "");
    
              GAPCentralRole_StartDiscovery(DEFAULT_DISCOVERY_MODE,
                                            DEFAULT_DISCOVERY_ACTIVE_SCAN,
                                            DEFAULT_DISCOVERY_WHITE_LIST);
            }
          }
          // Connect if there is a scan result
          else
          {
            // connect to current device in scan result
            uint8_t *peerAddr = devList[scanIdx].addr;
            uint8_t addrType = devList[scanIdx].addrType;
    
            state = BLE_STATE_CONNECTING;
    
            GAPCentralRole_EstablishLink(DEFAULT_LINK_HIGH_DUTY_CYCLE,
                                         DEFAULT_LINK_WHITE_LIST,
                                         addrType, peerAddr);
    
            Display_print0(dispHandle, 2, 0, "Connecting");
            Display_print0(dispHandle, 3, 0, Util_convertBdAddr2Str(peerAddr));
            Display_clearLine(dispHandle, 4);
    
            // Forget the scan results.
            scanRes = 0;
            scanIdx = -1;
          }
        }
        else if (state == BLE_STATE_CONNECTED)
        {
          if (keys & KEY_RIGHT)
          {
              if (charHdl != 0 &&
                  procedureInProgress == FALSE)
              {
                uint8_t status;
    
                // Do a read or write as long as no other read or write is in progress
                if (doWrite)
                {
                  // Do a write
                  attWriteReq_t req;
    
                  req.pValue = GATT_bm_alloc(connHandle, ATT_WRITE_REQ, 1, NULL);
                  if ( req.pValue != NULL )
                  {
                    req.handle = charHdl;
                    req.len = 1;
                    req.pValue[0] = charVal;
                    req.sig = 0;
                    req.cmd = 0;
    
                    status = GATT_WriteCharValue(connHandle, &req, selfEntity);
                    if ( status != SUCCESS )
                    {
                      GATT_bm_free((gattMsg_t *)&req, ATT_WRITE_REQ);
                    }
                  }
                  else
                  {
                    status = bleMemAllocError;
                  }
                }
                if (status == SUCCESS)
                {
                  procedureInProgress = TRUE;
                }
              }
          }
        }
        return;
      } else if(keys & KEY_LEFT){
          if (state == BLE_STATE_IDLE)
              {
                if (scanIdx == -1)
                {
                  if (!scanningStarted)
                  {
                    scanningStarted = TRUE;
                    scanRes = 0;
    
                    Display_print0(dispHandle, 2, 0, "Discovering...");
                    Display_print0(dispHandle, 3, 0, "");
                    Display_print0(dispHandle, 4, 0, "");
                    Display_print0(dispHandle, 5, 0, "");
                    Display_print0(dispHandle, 6, 0, "");
    
                    GAPCentralRole_StartDiscovery(DEFAULT_DISCOVERY_MODE,
                                                  DEFAULT_DISCOVERY_ACTIVE_SCAN,
                                                  DEFAULT_DISCOVERY_WHITE_LIST);
                  }
                }
                // Connect if there is a scan result
                else
                {
                  // connect to current device in scan result
                  uint8_t *peerAddr = devList[scanIdx].addr;
                  uint8_t addrType = devList[scanIdx].addrType;
    
                  state = BLE_STATE_CONNECTING;
    
                  GAPCentralRole_EstablishLink(DEFAULT_LINK_HIGH_DUTY_CYCLE,
                                               DEFAULT_LINK_WHITE_LIST,
                                               addrType, peerAddr);
    
                  Display_print0(dispHandle, 2, 0, "Connecting");
                  Display_print0(dispHandle, 3, 0, Util_convertBdAddr2Str(peerAddr));
                  Display_clearLine(dispHandle, 4);
    
                  // Forget the scan results.
                  scanRes = 0;
                  scanIdx = -1;
                }
              }
              else if (state == BLE_STATE_CONNECTED)
              {
                if (keys & KEY_LEFT)
                {
                    if (charHdl1 != 0 &&
                        procedureInProgress == FALSE)
                    {
                      uint8_t status;
    
                      // Do a read or write as long as no other read or write is in progress
                      if (doWrite)
                      {
                        // Do a write
                        attWriteReq_t req;
    
                        req.pValue = GATT_bm_alloc(connHandle, ATT_WRITE_REQ, 1, NULL);
                        if ( req.pValue != NULL )
                        {
                          req.handle = charHdl1;
                          req.len = 1;
                          req.pValue[0] = charVal1;
                          req.sig = 0;
                          req.cmd = 0;
    
                          status = GATT_WriteCharValue(connHandle, &req, selfEntity);
                          if ( status != SUCCESS )
                          {
                            GATT_bm_free((gattMsg_t *)&req, ATT_WRITE_REQ);
                          }
                        }
                        else
                        {
                          status = bleMemAllocError;
                        }
                      }
                      if (status == SUCCESS)
                      {
                        procedureInProgress = TRUE;
                      }
                    }
                }
              }
              return;
    
      }

  • Do you set breakpoint and debug what happens to your code?
  • Yes, in the second if Not enter. Here:

    else if (state == BLE_STATE_CONNECTED)
    {
    if (keys & KEY_LEFT)
    {
    if (charHdl1 != 0 &&
    procedureInProgress == FALSE)
    {
    uint8_t status;

    // Do a read or write as long as no other read or write is in progress
    if (doWrite)
    {
    // Do a write
    attWriteReq_t req;

    req.pValue = GATT_bm_alloc(connHandle, ATT_WRITE_REQ, 1, NULL);
    if ( req.pValue != NULL )
    {
    req.handle = charHdl1;
    req.len = 1;
    req.pValue[0] = charVal1;
    req.sig = 0;
    req.cmd = 0;

    status = GATT_WriteCharValue(connHandle, &req, selfEntity);
    if ( status != SUCCESS )
    {
    GATT_bm_free((gattMsg_t *)&req, ATT_WRITE_REQ);
    }
    }
    else
    {
    status = bleMemAllocError;
    }
    }
    if (status == SUCCESS)
    {
    procedureInProgress = TRUE;
    }
    }
    }
    }
    return;
  • Since you can debug your code, I suppose you can check why it doesn’t enter if statement.
  • I ran the left button to send the character. But send the same value as the right button. These are my changes:

      if (keys & KEY_LEFT)
                {
                PIN_setOutputValue(ledPinHandle, Board_RLED, 1);
                    if (charHdl1 != 0 &&
                        procedureInProgress == FALSE)
                    {
                      uint8_t status;
    
                      // Do a read or write as long as no other read or write is in progress
                      if (doWrite)
                      {
                        // Do a write
                        attWriteReq_t req1;
    
                        req1.pValue = GATT_bm_alloc(connHandle, ATT_WRITE_REQ, 1, NULL);
                        if ( req1.pValue != NULL )
                        {
                          req1.handle = charHdl1;
                          req1.len = 1;
                          req1.pValue[0] = charVal1;
                          req1.sig = 0;
                          req1.cmd = 0;
    
                          status = GATT_WriteCharValue(connHandle, &req1, selfEntity);
                          if ( status != SUCCESS )
                          {
                            GATT_bm_free((gattMsg_t *)&req1, ATT_WRITE_REQ);
                          }
                        }
                        else
                        {
                          status = bleMemAllocError;
                        }
                      }
                      if (status == SUCCESS)
                      {
                        procedureInProgress = TRUE;
                      }
                    }
                }
    
    /***************************************************************************************/
    else if ((pMsg->method == ATT_WRITE_RSP)  ||
                    ((pMsg->method == ATT_ERROR_RSP) &&
                     (pMsg->msg.errorRsp.reqOpcode == ATT_WRITE_REQ)))
           {
             if (pMsg->method == ATT_ERROR_RSP)
             {
               Display_print1(dispHandle, 4, 0, "Write Error %d", pMsg->msg.errorRsp.errCode);
             }
             else
             {
               // After a successful write, display the value that was written and
               // increment value
                 if(charVal1==0){
                     charVal1=1;
               Display_print1(dispHandle, 4, 0, "LED2 OFF: %d", charVal1);
                 }else{
                     charVal1=0;
                     Display_print1(dispHandle, 4, 0, "LED2 ON: %d", charVal1);
                 }
             }
    
             procedureInProgress = FALSE;
    
           }
    /********************************************************************************************************/
            req.startHandle = svcStartHdl;
            req.endHandle = svcEndHdl;
            req.type.len = ATT_BT_UUID_SIZE;
            req1.startHandle = svcStartHdl;
            req1.endHandle = svcEndHdl;
            req1.type.len = ATT_BT_UUID_SIZE;
            req.type.uuid[0] = LO_UINT16(SIMPLEPROFILE_CHAR1_UUID);
            req.type.uuid[1] = HI_UINT16(SIMPLEPROFILE_CHAR1_UUID);
            req1.type.uuid[0] = LO_UINT16(SIMPLEPROFILE_CHAR3_UUID);
            req1.type.uuid[1] = HI_UINT16(SIMPLEPROFILE_CHAR3_UUID);

  • You should make sure you use correct req1.handle and req1.pValue[0] when KEY_LEFT is triggered.
  • Yes? How do you confirm it?
  • I used req.handle and req.pValue[0] for BUTTON RIGHT
    I used req1.handle and re1.pValue[0] for BUTTON LEFT
  • The point is what are inside your req.handle/req.pValue[0] or req1.handle/re1.pValue[0] when BUTTON RIGHT or BUTTON LEFT is triggered.
  • This is my part of code: 

    if (keys & KEY_RIGHT)
          {
              PIN_setOutputValue(ledPinHandle, Board_GLED, 1);
              if (charHdl != 0 &&
                  procedureInProgress == FALSE)
              {
                uint8_t status;
    
                // Do a read or write as long as no other read or write is in progress
                if (doWrite)
                {
                  // Do a write
                  attWriteReq_t req;
    
                  req.pValue = GATT_bm_alloc(connHandle, ATT_WRITE_REQ, 1, NULL);
                  if ( req.pValue != NULL )
                  {
                    req.handle = charHdl;
                    req.len = 1;
                    req.pValue[0] = charVal;
                    req.sig = 0;
                    req.cmd = 0;
    
                    status = GATT_WriteCharValue(connHandle, &req, selfEntity);
                    if ( status != SUCCESS )
                    {
                      GATT_bm_free((gattMsg_t *)&req, ATT_WRITE_REQ);
                    }
                  }
                  else
                  {
                    status = bleMemAllocError;
                  }
                }
                if (status == SUCCESS)
                {
                  procedureInProgress = TRUE;
                }
              }
          }
          if (keys & KEY_LEFT)
                {
                PIN_setOutputValue(ledPinHandle, Board_RLED, 1);
                    if (charHdl1 != 0 &&
                        procedureInProgress == FALSE)
                    {
                      uint8_t status;
    
                      // Do a read or write as long as no other read or write is in progress
                      if (doWrite)
                      {
                        // Do a write
                        attWriteReq_t req1;
    
                        req1.pValue = GATT_bm_alloc(connHandle, ATT_WRITE_REQ, 1, NULL);
                        if ( req1.pValue != NULL )
                        {
                          req1.handle = charHdl1;
                          req1.len = 1;
                          req1.pValue[0] = charVal1;
                          req1.sig = 0;
                          req1.cmd = 0;
    
                          status = GATT_WriteCharValue(connHandle, &req1, selfEntity);
                          if ( status != SUCCESS )
                          {
                            GATT_bm_free((gattMsg_t *)&req1, ATT_WRITE_REQ);
                          }
                        }
                        else
                        {
                          status = bleMemAllocError;
                        }
                      }
                      if (status == SUCCESS)
                      {
                        procedureInProgress = TRUE;
                      }
                    }
                }

  • Again, I cannot debug your code for you. I suggest you to set a breakpoint to check what are in your req.handle/req.pValue[0] before you do write attribute.