Skip to main content
Associate II
July 24, 2024
Solved

How to update BLE characteristics via timer interrupt in STM32WB55 ?

  • July 24, 2024
  • 1 reply
  • 1545 views

Hello everyone,

I'm trying to use the timer interrupt callback to modify the GATT characteristics and send out a notification (see code excerpt below). This only works once and after sending the message the timer no longer triggers and I no longer see any advertisements from my board. If I send the data via the USB-COM port, it works using the timer interrupt. So there seems to be something wrong with the IPCC but I don't know what it is and how I can fix it/get around it.  can someone help me ?

 

Many Greetings,

Andreas 

 

 

 

 

 /* Initialize all configured peripherals */
 MX_GPIO_Init();
 MX_RTC_Init();
 MX_USB_Device_Init();
 MX_TIM16_Init();
 MX_RF_Init();
 /* USER CODE BEGIN 2 */

static uint8_t cnt = 0;
const uint8_t max_cnt = 255;
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
	// Check which version of the timer triggered this callback
	if (htim == &htim16 )
	{
		if (cnt % (max_cnt - 1) == 0) cnt = 0;

		uint8_t data[1];
		memcpy(data, &cnt, sizeof(cnt));
		cnt++;
 
 // only one of the two is active at a time!

 // This part for BLE, works just once!
		// Custom_STM_App_Update_Char(CUSTOM_STM_TEMPERATUR, data);
 
 // This USB-part works allways as desired
		uint16_t data_len = (uint16_t) sizeof(data)/sizeof(uint8_t);
		CDC_Transmit_FS((uint8_t*)data, data_len);
	}
}

 

 

 

 

 

Best answer by STTwo-32

Hello @AndreasC 

In fact, you have to add a task that do this on the Sequencer that run all code tasks. So, whenever the sequencer starts, the execution will be only for tasks included on this Sequencer. For the timer, you can use a HW_Timer. More details that can be helpful are available on the AN5289 specially the part 4.5.

Best Regards.

STTwo-32

1 reply

STTwo-32
STTwo-32Best answer
Technical Moderator
August 22, 2024

Hello @AndreasC 

In fact, you have to add a task that do this on the Sequencer that run all code tasks. So, whenever the sequencer starts, the execution will be only for tasks included on this Sequencer. For the timer, you can use a HW_Timer. More details that can be helpful are available on the AN5289 specially the part 4.5.

Best Regards.

STTwo-32

AndreasCAuthor
Associate II
November 8, 2024

Hello STTwo-32,

thank u for the reply. 

 

I solved my problem just by introducing a simple flag in the interrupt routine which I evaluate in the main loop as follows:

 

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
	// Check which version of the timer triggered this callback
	if (htim == &htim16 )
	{
		if (tim16_cnt >= 5)	// for IPPC sync.
			tim16_flag = 1;
		else
			tim16_cnt++;
	}
}

while (1)
 {
	 __disable_irq();
	 if (tim16_flag){

		 BME280_cmd_res cmd_res = BME280_read_env_data(&bme280);
		 if (cmd_res != BME280_CMD_OK) {
			 HAL_GPIO_WritePin(GPIOA, GPIO_PIN_10, GPIO_PIN_SET);	// indicate error with LED on PA10
		 }

		 // convert measurement data to the appropriate units for nRF-Connect Application
		 BME280_U32_t hum_prh = (float)bme280.H/kH * 100.0;	// relative humidity in Percent
		 BME280_U32_t pres_pa= bme280.P/kP * 10;	//in Pascal

		 Custom_STM_App_Update_Char(CUSTOM_STM_TEMPERATURE, (uint8_t *)&bme280.T);
		 Custom_STM_App_Update_Char(CUSTOM_STM_HUMIDITY, (uint8_t *)&hum_prh);
		 Custom_STM_App_Update_Char(CUSTOM_STM_PRESSURE, (uint8_t *)&pres_pa);

		 tim16_flag = 0;
	 }
	 __enable_irq();

 /* USER CODE END WHILE */
 MX_APPE_Process();

 /* USER CODE BEGIN 3 */
 }

 

I was worried about race conditions (although in my case that's not a problem) and disabled the interrupts in the critical area. However, I use the BLE stack in the same area. Is it even allowed to disableoff the interrupts when the BLE stack is active, or how can you protect the critical area from interrupts without an OS when using the STM32WB?

STTwo-32
Technical Moderator
November 8, 2024

Hello @AndreasC 

Could you please give a better description of what you want to do.

Best Regards.

STTwo-32