Skip to main content
KMew
Senior III
April 12, 2023
Solved

BlueNRG: BLE Initialization Never Leaves hci_init()

  • April 12, 2023
  • 2 replies
  • 2552 views

Hello,

I am creating a Bluetooth app on a custom board. I used the same hardware design as the X-NULCEO-IDB05A2 expansion board, but with the sole difference being that the Bluetooth module I'm using is the BLUENRG-M0L instead of the BLUENRG-M0A (which, to my understanding, is just an internal power supply difference and that is it).

I used the SensorBLEDemo project as the base for my code, then brought it up on a separate project to ensure it worked as intended (and it does). However, when I ran the same code on my custom board, the BlueNRG initialization code acts strangely. When the function hci_init() is called, it never leaves the function and, seemingly, gets stuck there.

I call for the BlueNRG initialization in my main.c. I've attached my initialization code below. I get stuck on Line 29. I'm unsure where to look to diagnose this, since this is a HCI premade code. Can anyone help me diagnose this?

void MX_BlueNRG_MS_Init(void)
{
 /* USER CODE BEGIN SV */
 
 /* USER CODE END SV */
 
 /* USER CODE BEGIN BlueNRG_MS_Init_PreTreatment */
	battSOC = 500;
	battVoltage = 428;
	battPower = 0;
	battMinTemp = 22;
	battMaxTemp = 24;
 /* USER CODE END BlueNRG_MS_Init_PreTreatment */
 
 /* Initialize the peripherals and the BLE Stack */
 const char *name = "COR";
 uint16_t service_handle, dev_name_char_handle, appearance_char_handle;
 
 uint8_t bdaddr_len_out;
 uint8_t hwVersion;
 uint16_t fwVersion;
 int ret;
 
 User_Init();
 
 /* Get the User Button initial state */
 //user_button_init_state = BSP_PB_GetState(BUTTON_KEY);
 
 hci_init(user_notify, NULL); // ----------> GETS STUCK HERE!!!! <------------
 
 /* get the BlueNRG HW and FW versions */
 getBlueNRGVersion(&hwVersion, &fwVersion);
 hwVersion_global = fwVersion;
 
 /*
 * Reset BlueNRG again otherwise we won't
 * be able to change its MAC address.
 * aci_hal_write_config_data() must be the first
 * command after reset otherwise it will fail.
 */
 hci_reset();
 HAL_Delay(100);
 
 PRINTF("HWver %d\nFWver %d\n", hwVersion, fwVersion);
 if (hwVersion > 0x30) { /* X-NUCLEO-IDB05A1 expansion board is used */
 bnrg_expansion_board = IDB05A1;
 }
 
 ret = aci_hal_read_config_data(CONFIG_DATA_RANDOM_ADDRESS, BDADDR_SIZE, &bdaddr_len_out, bdaddr);
 
 if (ret) {
 PRINTF("Read Static Random address failed.\n");
 }
 
 if ((bdaddr[5] & 0xC0) != 0xC0) {
 PRINTF("Static Random address not well formed.\n");
 while(1);
 }
 
 /* GATT Init */
 ret = aci_gatt_init();
 if(ret){
 PRINTF("GATT_Init failed.\n");
 }
 
 /* GAP Init */
 if (bnrg_expansion_board == IDB05A1) {
 ret = aci_gap_init_IDB05A1(GAP_PERIPHERAL_ROLE_IDB05A1, 0, 0x07, &service_handle, &dev_name_char_handle, &appearance_char_handle);
 }
 else {
 ret = aci_gap_init_IDB04A1(GAP_PERIPHERAL_ROLE_IDB04A1, &service_handle, &dev_name_char_handle, &appearance_char_handle);
 }
 if (ret != BLE_STATUS_SUCCESS) {
 PRINTF("GAP_Init failed.\n");
 }
 
 /* Update device name */
 ret = aci_gatt_update_char_value(service_handle, dev_name_char_handle, 0,
 strlen(name), (uint8_t *)name);
 if (ret) {
 PRINTF("aci_gatt_update_char_value failed.\n");
 while(1);
 }
 
 ret = aci_gap_set_auth_requirement(MITM_PROTECTION_REQUIRED,
 OOB_AUTH_DATA_ABSENT,
 NULL,
 7,
 16,
 USE_FIXED_PIN_FOR_PAIRING,
 123456,
 BONDING);
 if (ret) {
 PRINTF("aci_gap_set_authentication_requirement failed.\n");
 while(1);
 }
 
 PRINTF("BLE Stack Initialized\n");
 
 /*
 ret = Add_HWServW2ST_Service();
 if(ret == BLE_STATUS_SUCCESS) {
 PRINTF("BlueMS HW service added successfully.\n");
 } else {
 PRINTF("Error while adding BlueMS HW service: 0x%02x\r\n", ret);
 while(1);
 }
 */
 
 // Not necessary for this code
 /*
 ret = Add_SWServW2ST_Service();
 if(ret == BLE_STATUS_SUCCESS) {
 PRINTF("BlueMS SW service added successfully.\n");
 } else {
 PRINTF("Error while adding BlueMS HW service: 0x%02x\r\n", ret);
 while(1);
 }
*/
 
 ret = Add_Battery_Service();
 if(ret == BLE_STATUS_SUCCESS) {
 PRINTF("BlueMS Battery service added successfully.\n");
 } else {
 PRINTF("Error while adding BlueMS Battery service: 0x%02x\r\n", ret);
 while(1);
 }
 
 ret = Add_Inverter_Service();
 if(ret == BLE_STATUS_SUCCESS) {
 PRINTF("BlueMS Inverter service added successfully.\n");
 } else {
 PRINTF("Error while adding BlueMS Inverter service: 0x%02x\r\n", ret);
 while(1);
 }
 
 ret = Add_Fault_Service();
 if(ret == BLE_STATUS_SUCCESS) {
 PRINTF("BlueMS Fault service added successfully.\n");
 } else {
 PRINTF("Error while adding BlueMS Fault service: 0x%02x\r\n", ret);
 while(1);
 }
 
 ret = Add_Control_Service();
 if(ret == BLE_STATUS_SUCCESS) {
 PRINTF("BlueMS Control service added successfully.\n");
 } else {
 PRINTF("Error while adding BlueMS Control service: 0x%02x\r\n", ret);
 while(1);
 }
 
 
 /* Set output power level */
 ret = aci_hal_set_tx_power_level(1,4);
 
 /* USER CODE BEGIN BlueNRG_MS_Init_PostTreatment */
 
 /* USER CODE END BlueNRG_MS_Init_PostTreatment */
}

 EDIT:

I also did a side-by-side comparison via the debugger in the hci_tl.c file. The hci_init() function is below. In my custom board, my code never goes past line 25 (if (hciContext.io.Reset) hciContext.io.Reset();).

In the Nucleo board, it does go past that line without issue.

void hci_init(void(* UserEvtRx)(void* pData), void* pConf)
{
 uint8_t index;
 
 if(UserEvtRx != NULL)
 {
 hciContext.UserEvtRx = UserEvtRx;
 }
 
 /* Initialize list heads of ready and free hci data packet queues */
 list_init_head(&hciReadPktPool);
 list_init_head(&hciReadPktRxQueue);
 
 /* Initialize TL BLE layer */
 hci_tl_lowlevel_init();
 
 /* Initialize the queue of free hci data packets */
 for (index = 0; index < HCI_READ_PACKET_NUM_MAX; index++)
 {
 list_insert_tail(&hciReadPktPool, (tListNode *)&hciReadPacketBuffer[index]);
 } 
 
 /* Initialize low level driver */
 if (hciContext.io.Init) hciContext.io.Init(NULL);
 if (hciContext.io.Reset) hciContext.io.Reset();
}

Best answer by KMew

Hello,

I have found the solution to my problem. It was two things, but both were related to the SPI configuration that the BlueNRG module uses.

Data Size: It was set to 4 bits and the BLE module uses 8 bits

Clock Baud Rate was too high. It was initially set to 70 Mbit/s. I switched it to 8.75 Mbit/s

With both changes, the BlueNRG module works as intended.

2 replies

KMew
KMewAuthorBest answer
Senior III
April 14, 2023

Hello,

I have found the solution to my problem. It was two things, but both were related to the SPI configuration that the BlueNRG module uses.

Data Size: It was set to 4 bits and the BLE module uses 8 bits

Clock Baud Rate was too high. It was initially set to 70 Mbit/s. I switched it to 8.75 Mbit/s

With both changes, the BlueNRG module works as intended.

Associate II
March 15, 2025

I had also tried the proposed solution, but the problem remained the same until I made these adjustments.
stm32l475 00.png11.png22.png

AMACH.1
Associate
April 29, 2023

Hello,I am trying to initialize my blueNRG-2N but I am not getting the expected values after a hardware reset.

I should get through datashet: [02,7F,00,00,00], [02,7F,00,06,00], and [04, FF, 03, 01, 00, 01].

The picture below shows the values I get.

Do you have any suggestions?

Thank you.


_legacyfs_online_stmicro_images_0693W00000bitZWQAY.png