Skip to main content
Visitor II
June 19, 2025
Question

Freertos semaphore initialised wrong in CubeMX

  • June 19, 2025
  • 1 reply
  • 273 views

CubeMX always generates binary semaphores with an initial count of 1 (full) in CMSIS-RTOS2, even if the user wants the semaphore to start depleted (count = 0). There is no option to set the initial count to depleted during configuration, but when generating the code, it will be with initial count of 1.

Taha_0-1750349263021.png

/* creation of myBinarySem01 */
 myBinarySem01Handle = osSemaphoreNew(1, 1, &myBinarySem01_attributes);

 /* creation of myBinarySem02 */
 myBinarySem02Handle = osSemaphoreNew(1, 1, &myBinarySem02_attributes);

only way I solve it is acquiring the semaphore after creating it

* USER CODE BEGIN RTOS_SEMAPHORES */
 /* add semaphores, ... */
 osSemaphoreAcquire(myBinarySem02Handle, 10);

 /* USER CODE END RTOS_SEMAPHORES */

 

1 reply

Andrew Neil
Super User
June 19, 2025
A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.