cancel
Showing results for 
Search instead for 
Did you mean: 

BNRG2A1 Sample project fail to run. BLE_STATUS_TIMEOUT after hci_send_req command.

SChen.22
Associate

Paired the BNRG2A1 module with the STM32L476RG as recommended in the sample application. But when debugging I realised that the hciReadPktRxQueue is always empty leading to timeout.

I have made no modification to the sample application and even with the same hardware is unable to run the application. Any ideas on how to get it running?

This is the original hc_send_req that constantly timeout.

int hci_send_req(struct hci_request* r, BOOL async)
{
  uint8_t *ptr;
  uint16_t opcode = htobs(cmd_opcode_pack(r->ogf, r->ocf));
  hci_event_pckt *event_pckt;
  hci_spi_pckt *hci_hdr;
 
  tHciDataPacket * hciReadPacket = NULL;
  tListNode hciTempQueue;
  
  list_init_head(&hciTempQueue);
 
  free_event_list();
  
  send_cmd(r->ogf, r->ocf, r->clen, r->cparam);
  
  if (async)
  {
    return 0;
  }
  
  while (1) 
  {
    evt_cmd_complete  *cc;
    evt_cmd_status    *cs;
    evt_le_meta_event *me;
    uint32_t len;
    
    uint32_t tickstart = HAL_GetTick();
      
    while (1)
    {
      if ((HAL_GetTick() - tickstart) > HCI_DEFAULT_TIMEOUT_MS)
      {
        goto failed;
      }
      
      if (!list_is_empty(&hciReadPktRxQueue)) 
      {
        break;
      }
    }
    
    /* Extract packet from HCI event queue. */
    list_remove_head(&hciReadPktRxQueue, (tListNode **)&hciReadPacket);    
    
    hci_hdr = (void *)hciReadPacket->dataBuff;
 
    if (hci_hdr->type == HCI_EVENT_PKT)
    {
      event_pckt = (void *)(hci_hdr->data);
    
      ptr = hciReadPacket->dataBuff + (1 + HCI_EVENT_HDR_SIZE);
      len = hciReadPacket->data_len - (1 + HCI_EVENT_HDR_SIZE);
    
      switch (event_pckt->evt) 
      {      
      case EVT_CMD_STATUS:
        cs = (void *) ptr;
        
        if (cs->opcode != opcode)
          goto failed;
        
        if (r->event != EVT_CMD_STATUS) {
          if (cs->status) {
            goto failed;
          }
          break;
        }
 
        r->rlen = MIN(len, r->rlen);
        BLUENRG_memcpy(r->rparam, ptr, r->rlen);
        goto done;
      
      case EVT_CMD_COMPLETE:
        cc = (void *) ptr;
      
        if (cc->opcode != opcode)
          goto failed;
      
        ptr += EVT_CMD_COMPLETE_SIZE;
        len -= EVT_CMD_COMPLETE_SIZE;
      
        r->rlen = MIN(len, r->rlen);
        BLUENRG_memcpy(r->rparam, ptr, r->rlen);
        goto done;
      
      case EVT_LE_META_EVENT:
        me = (void *) ptr;
      
        if (me->subevent != r->event)
          break;
      
        len -= 1;
        r->rlen = MIN(len, r->rlen);
        BLUENRG_memcpy(r->rparam, me->data, r->rlen);
        goto done;
      
      case EVT_HARDWARE_ERROR:            
        goto failed;
      
      default:      
        break;
      }
    }
    
    /* If there are no more packets to be processed, be sure there is at list one
       packet in the pool to process the expected event.
       If no free packets are available, discard the processed event and insert it
       into the pool. */
    if (list_is_empty(&hciReadPktPool) && list_is_empty(&hciReadPktRxQueue)) {
      list_insert_tail(&hciReadPktPool, (tListNode *)hciReadPacket);
      hciReadPacket=NULL;
    }
    else {
      /* Insert the packet in a different queue. These packets will be
      inserted back in the main queue just before exiting from send_req(), so that
      these events can be processed by the application.
    */
    list_insert_tail(&hciTempQueue, (tListNode *)hciReadPacket);
      hciReadPacket=NULL;
    }
  }
  
failed: 
  if (hciReadPacket!=NULL) {
    list_insert_head(&hciReadPktPool, (tListNode *)hciReadPacket);
  }
  move_list(&hciReadPktRxQueue, &hciTempQueue);
 
  return -1;
  
done:
  /* Insert the packet back into the pool.*/
  list_insert_head(&hciReadPktPool, (tListNode *)hciReadPacket); 
  move_list(&hciReadPktRxQueue, &hciTempQueue);
 
  return 0;
}

27 REPLIES 27

and also...I don't understand why I have to found that slave[0] must be 2....I looked for the protocol but I haven't found it.

malarab
Senior II

After reset, the BlueNRG send to mcu [02,7F,00,00,00]

then it send  [02,7F,00,06,00]

Thats why you chould have 2 on slave[0]

malarab
Senior II

After reset, the BlueNRG send to mcu [02,7F,00,00,00]

then it send  [02,7F,00,06,00]

Thats why you chould have 2 on slave[0]

Hallo, now my clk is 1Mhz (I checked with an oscilloscope), but the message I receive is 0xff 0x09 0x01 0x06 0x00...Is it correcty spi polarity to high, and clock phase 2edge? or something else to check?is it not possible to have the source code of the project inside the bluenrg2a board?thank you.

 

  SPI_AutonomousModeConfTypeDef HAL_SPI_AutonomousMode_Cfg_Struct = {0};
  HAL_StatusTypeDef ret = HAL_OK;
 
  hspi->Instance = SPI1;
  hspi->Init.Mode = SPI_MODE_MASTER;
  hspi->Init.Direction = SPI_DIRECTION_2LINES;
  hspi->Init.DataSize = SPI_DATASIZE_8BIT;
  hspi->Init.CLKPolarity = SPI_POLARITY_HIGH;
  hspi->Init.CLKPhase = SPI_PHASE_2EDGE;
  hspi->Init.NSS = SPI_NSS_SOFT;
  hspi->Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;
  hspi->Init.FirstBit = SPI_FIRSTBIT_MSB;
  hspi->Init.TIMode = SPI_TIMODE_DISABLE;
  hspi->Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  hspi->Init.CRCPolynomial = 7;
  hspi->Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
  hspi->Init.NSSPMode = SPI_NSS_PULSE_DISABLE;
  hspi->Init.NSSPolarity = SPI_NSS_POLARITY_LOW;
  hspi->Init.FifoThreshold = SPI_FIFO_THRESHOLD_01DATA;
  hspi->Init.MasterSSIdleness = SPI_MASTER_SS_IDLENESS_00CYCLE;
  hspi->Init.MasterInterDataIdleness = SPI_MASTER_INTERDATA_IDLENESS_00CYCLE;
  hspi->Init.MasterReceiverAutoSusp = SPI_MASTER_RX_AUTOSUSP_DISABLE;
  hspi->Init.MasterKeepIOState = SPI_MASTER_KEEP_IO_STATE_DISABLE;
  hspi->Init.IOSwap = SPI_IO_SWAP_DISABLE;
  hspi->Init.ReadyMasterManagement = SPI_RDY_MASTER_MANAGEMENT_INTERNALLY;
  hspi->Init.ReadyPolarity = SPI_RDY_POLARITY_HIGH;
  if (HAL_SPI_Init(hspi) != HAL_OK)
  {
  return 1;
  }
  HAL_SPI_AutonomousMode_Cfg_Struct.TriggerState = SPI_AUTO_MODE_DISABLE;
  HAL_SPI_AutonomousMode_Cfg_Struct.TriggerSelection = SPI_GRP1_GPDMA_CH0_TCF_TRG;
  HAL_SPI_AutonomousMode_Cfg_Struct.TriggerPolarity = SPI_TRIG_POLARITY_RISING;
  if (HAL_SPIEx_SetConfigAutonomousMode(&hspi1, &HAL_SPI_AutonomousMode_Cfg_Struct) != HAL_OK)
  {
    return 1;
  }

 

IVent.1
Senior

any suggestion is welcome...

try just with a simple spi config :
here is an example :

hspi1.Instance = SPI1;
hspi1.Init.Mode = SPI_MODE_MASTER;
hspi1.Init.Direction = SPI_DIRECTION_2LINES;
hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
hspi1.Init.CLKPolarity = SPI_POLARITY_HIGH;
hspi1.Init.CLKPhase = SPI_PHASE_2EDGE;
hspi1.Init.NSS = SPI_NSS_SOFT;
hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
hspi1.Init.CRCPolynomial = 7;
hspi1.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
hspi1.Init.NSSPMode = SPI_NSS_PULSE_DISABLE;
if (HAL_SPI_Init(&hspi1) != HAL_OK)
{
Error_Handler();
}

 

be sure you declare HAL_SPI_MspInit

 

you dont need any extra call

be sure that spi clock is not higher than 1MHz

 

if you are using an other spi peripheral (SPI2 ...) be sure you are not using the same spi Handle on two different peripheral