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.

state machine defined in Taskfxn in simpleBLECentral

I have created a state machine which processes in the task function to process the various states generated by the callback functions. I thought the SimpleBLECentral_taskfxn runs in an infinite loop. But it doesn't runs after the first iteration

static void SimpleBLECentral_taskFxn(UArg a0, UArg a1)
{
  // Initialize application
  SimpleBLECentral_init();
  
  // Application main loop
  for (;;)
  {
    // Waits for a signal to the semaphore associated with the calling thread.
    // Note that the semaphore associated with a thread is signaled when a
    // message is queued to the message receive queue of the thread or when
    // ICall_signal() function is called onto the semaphore.
    ICall_Errno errno = ICall_wait(ICALL_TIMEOUT_FOREVER);

    if (errno == ICALL_ERRNO_SUCCESS)
    {
      ICall_EntityID dest;
      ICall_ServiceEnum src;
      ICall_HciExtEvt *pMsg = NULL;
      
      if (ICall_fetchServiceMsg(&src, &dest, 
                                (void **)&pMsg) == ICALL_ERRNO_SUCCESS)
      {
        if ((src == ICALL_SERVICE_CLASS_BLE) && (dest == selfEntity))
        {
          // Process inter-task message
          SimpleBLECentral_processStackMsg((ICall_Hdr *)pMsg);
        }

        if (pMsg)
        {
          ICall_freeMsg(pMsg);
        }
      }
    }


    // If RTOS queue is not empty, process app message
    while (!Queue_empty(appMsgQueue))
    {
      sbcEvt_t *pMsg = (sbcEvt_t *)Util_dequeueMsg(appMsgQueue);
      if (pMsg)
      {
        // Process message
        SimpleBLECentral_processAppMsg(pMsg);
        
        // Free the space from the message
        ICall_free(pMsg);
      }
    }

   //state machine defined
    switch(taskState)
    {
       case TASK_CONNECT:
         function1();
         break;

        case TASK_DISCOVER:
          function2();
          break;

         case TASK_DISCONNECT:
            function3();
            break;

         case TASK_SPI:
            function4();
            break;

          case TASK_READ:

       }

    
    if (events & SBC_START_DISCOVERY_EVT)
    {      
      events &= ~SBC_START_DISCOVERY_EVT;
      
      SimpleBLECentral_startDiscovery();
    }
  }
}

 

  • Hello Sid,

    Probably the task is blocked in ICall_wait waiting for the appSEM to be posted. What do you see in the debugger?

    Best wishes
  • 
    

    Hi,

    So It seems the state machine works and fails or gets blocked if If I have the read function inside of the state machine.

    My Read Function

    void Read_ble(uint8_t handle)
    {
    	uint8_t status;
    	attReadReq_t req1;
    	req1.handle = handle;
    	status = GATT_ReadCharValue(connHandle, &req1, selfEntity);
    	if ( status == SUCCESS )
    	{
    	  printf("read operation complete\n\r");
    	}
    }

    This function gets properly executed using the key press but gets stuck in the state machine. 
    I am changing the state in the processGATTMsg 

    static void SimpleBLECentral_processGATTMsg(gattMsgEvent_t *pMsg)
    {
      if (state == BLE_STATE_CONNECTED)
      {
        // See if GATT server was unable to transmit an ATT response
        if (pMsg->hdr.status == blePending)
        {
          // No HCI buffer was available. App can try to retransmit the response
          // on the next connection event. Drop it for now.
        //  LCD_WRITE_STRING_VALUE("ATT Rsp dropped", pMsg->method, 10, LCD_PAGE5);
        }
        else if ((pMsg->method == ATT_READ_RSP)   ||
                 ((pMsg->method == ATT_ERROR_RSP) &&
                  (pMsg->msg.errorRsp.reqOpcode == ATT_READ_REQ)))
        {
          if (pMsg->method == ATT_ERROR_RSP)
          {
              uint8 status = pMsg->msg.errorRsp.errCode;
          LCD_WRITE_STRING_VALUE("Read Error", status, 10,
                                   LCD_PAGE5);
          }
          else
          {
        	  printf("success 1 \n\r");
            // After a successful read, display the read value
            int length = pMsg->msg.readRsp.len;
            char *str_length;
            sprintf(str_length, "%d", length);
            printf("the length is %s \n\r",str_length);
            LCD_WRITE_STRING(str_length, LCD_PAGE6);
    
            uint8_t i, scratch;
            char array[20];
            for (i = 0; i < length; i++)
            {
                scratch = pMsg->msg.readRsp.pValue[i];
                printf ("data is %X",scratch);
                //store read value into an array
                sprintf(array,"%X",scratch);
                strcat(array_final,array);
             }
             printf("array : %s \n\r",array_final);
             taskState = TASK_DISCONNECT;
             printf("success 2 \n\r");
          }
          
          procedureInProgress = FALSE;
        }
        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 == ATT_ERROR_RSP)
          {     
            uint8 status =   pMsg->msg.errorRsp.errCode;
            LCD_WRITE_STRING_VALUE("Write Error", status, 10,
                                   LCD_PAGE6);
          }
          else
          {
        	 // taskState = TASK_READ;
            // After a successful write, display the value that was written and
            // increment value
            //LCD_WRITE_STRING_VALUE("Write sent:", charVal++, 10, LCD_PAGE4);
          }
          
          procedureInProgress = 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.
         // LCD_WRITE_STRING_VALUE("FC Violated:", pMsg->msg.flowCtrlEvt.opcode,
                 //                10, LCD_PAGE4);
        }
        else if (pMsg->method == ATT_MTU_UPDATED_EVENT)
        {   
          // MTU size updated
          //LCD_WRITE_STRING_VALUE("MTU Size:", pMsg->msg.mtuEvt.MTU, 10, LCD_PAGE4);
        }
        else if (discState != BLE_DISC_STATE_IDLE)
        {
          SimpleBLECentral_processGATTDiscEvent(pMsg);
        }
      } // else - in case a GATT message came after a connection has dropped, ignore it.
      
      // Needed only for ATT Protocol messages
      GATT_bm_free(&pMsg->msg, pMsg->method);
    }
    

    It gets stuck specifically at 
    printf("success 1 \n\r"); and disconnects from the peripheral soon after that