osalDbgAssert() may fail under some circumstances

This forum is dedicated to feedback, discussions about ongoing or future developments, ideas and suggestions regarding the ChibiOS projects are welcome. This forum is NOT for support.
User avatar
Cesare
Posts: 36
Joined: Tue Jul 11, 2017 11:51 am
Location: Milan, Italy
Has thanked: 3 times
Been thanked: 3 times

osalDbgAssert() may fail under some circumstances

Postby Cesare » Mon May 28, 2018 11:37 am

Good morning to everyone
I simply would like to report an issue that I am experiencing in these days.
I am developing on STM32F151 and ChibiOs is configured with:
#define CH_DBG_ENABLE_ASSERTS TRUE

When I start the timer TIM4, gpt_lld_start(GPTDriver *gptp) is executed until the prescaler is calculated:

Code: Select all

  /* Prescaler value calculation.*/
  psc = (uint16_t)((gptp->clock / gptp->config->frequency) - 1);
  osalDbgAssert(((uint32_t)(psc + 1) * gptp->config->frequency) == gptp->clock, "invalid frequency");

In my case gptp->clock = 4194000 Hz and gptp->config->frequency = 10000 Hz then in theory psc = (4194000 / 10000) -1 = 418.4 but the preprocessor rounds it to 418.
When osalDbgAssert() is called to assert, the confirmation between psc and gptp->config->frequency fails because the result is:
(418+1)* 10000 = 4190000 Hz instead of 4194000 Hz.

This is the effect of the preprocessor thinking with integers instead of floating point.

I had a similar problem in the past with my own code and I solved converting those variables/defines into floating point number just for the preprocessor phase, but I am not sure if doing this in ChibiOs might be a good idea due to cosmetic and legibility reasons.

apmorton
Posts: 36
Joined: Fri Sep 29, 2017 10:26 am
Been thanked: 16 times

Re: osalDbgAssert() may fail under some circumstances

Postby apmorton » Mon May 28, 2018 1:22 pm

In fact, this assert is failing by design.
The PSC value is going to be an integer itself, not a float.

ChibiOS is raising an assert to inform you that the frequency you requested is not possible given the clock speed of the peripheral.

The code "works" when the assert is disabled - except that the frequency will not be the requested 10000 Hz.
In your example the frequency would be 4194000/(418 + 1) = 10009.5465394 Hz

User avatar
Giovanni
Site Admin
Posts: 14444
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1074 times
Been thanked: 921 times
Contact:

Re: osalDbgAssert() may fail under some circumstances

Postby Giovanni » Mon May 28, 2018 3:01 pm

Hi,

Assertions are there to signal that there is a potential problem, in you case a baud rate that cannot be obtained using integer divisors.

What you could do is to specify the actual baud rate (4190000) so the assertion is not triggered.

Giovanni

User avatar
Cesare
Posts: 36
Joined: Tue Jul 11, 2017 11:51 am
Location: Milan, Italy
Has thanked: 3 times
Been thanked: 3 times

Re: osalDbgAssert() may fail under some circumstances

Postby Cesare » Mon May 28, 2018 3:22 pm

@apmorton
At the end, thinking well, you are right.
The assert fails because can't get the precise required frequency.
Ok, I will change the PLL frequency in order to match PSC and the timer frequency.

@Giovanni
Yes I also did that at the beginning.

Thank u.


Return to “Development and Feedback”

Who is online

Users browsing this forum: No registered users and 9 guests