NUCLEO-C071RB not working with chThdSleepMilliseconds() Topic is solved

ChibiOS public support forum for topics related to the STMicroelectronics STM32 family of micro-controllers.

Moderator: RoccoMarco

Engemil
Posts: 37
Joined: Thu Dec 19, 2024 12:50 am
Has thanked: 13 times
Been thanked: 10 times

NUCLEO-C071RB not working with chThdSleepMilliseconds()

Post by Engemil »

- Board: NUCLEO-C071RB.
- ChibiOS version: stable-21.11.x branch (commit: 11114387c22042b4fc32095c820f31b024bef97b)
- Demo code based on: demos/STM32/RT-STM32C071RB-NUCLEO64 (from stable-21.11.x branch, commit 11114387c22042b4fc32095c820f31b024bef97b)
- IDE: VS Code w/ Cortex-Debug Extension

What is working:
- Compilation of code works
- Flash to board works
- Run code on chip works, until it gets to chThdSleepMilliseconds()
- Additionally, just to ensure that the chip have no unexpected problem; I confirm that it works with a blink script based on Arduino framework.

My example code:

Code: Select all

#include "ch.h"
#include "hal.h"

volatile uint8_t i = 0;

int main(void) {
    halInit();
    chSysInit();
    while (true) {
        palClearLine(LINE_LED_GREEN);
        chThdSleepMilliseconds(500);
        palSetLine(LINE_LED_GREEN);
        chThdSleepMilliseconds(500);
        i = i + 1;
    }
}


The system stops at line 11: chThdSleepMilliseconds(500);

In detail it seems to stop at: chThdSleepMilliseconds->chThdSleep->chThdSleepS->chSchGoSleepTimeoutS->chVTDoSetI->vt_enqueue-> ??

And somehow get stuck in repeat at that level.

If someone has more experience with the timing logic of ChibiOS and can guide me on what to check for I'd gladly investigate this more in depth.
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: NUCLEO-C071RB not working with chThdSleepMilliseconds()

Post by Giovanni »

Hi,

You should look into a possible stack overflow of some thread. Start with enabling assertions, state checker and stack checker in chconf.h.

Are you using our startup files or something different? Also, are you compiling using -O0? that uses a lot of extra stack because no optimizations.

Giovanni
Engemil
Posts: 37
Joined: Thu Dec 19, 2024 12:50 am
Has thanked: 13 times
Been thanked: 10 times

Re: NUCLEO-C071RB not working with chThdSleepMilliseconds()

Post by Engemil »

Giovanni wrote:Hi,

You should look into a possible stack overflow of some thread. Start with enabling assertions, state checker and stack checker in chconf.h.

Are you using our startup files or something different? Also, are you compiling using -O0? that uses a lot of extra stack because no optimizations.

Giovanni


Hi Giovanni!

Alright! I'll look into it and enable assertions, state checker and stack checker.

I'm using the default startup file already defined in the ChibiOS demo it is based on:

Code: Select all

include $(CHIBIOS)/os/common/startup/ARMCMx/compilers/GCC/mk/startup_stm32c0xx.mk


and I'm using the default compile optimization in the Makefile from the ChibiOS demo:

Code: Select all

USE_OPT = -O2 -ggdb -fomit-frame-pointer -falign-functions=16


The only thing I add as extra is the System View Description (.svd) file from the product page for STM32C071RB, used for debugging with Cortex Debug VS Code Extension.
Engemil
Posts: 37
Joined: Thu Dec 19, 2024 12:50 am
Has thanked: 13 times
Been thanked: 10 times

Re: NUCLEO-C071RB not working with chThdSleepMilliseconds()

Post by Engemil »

Hi Giovanni!

Info:
- Debugging without ChibiStudio, (VS Code + Cortex Debug Extension).
- I tried quickly to run this code example with ChibiStudio, but didn't manage to compile "out of the box" for me with the Virtual Machine version, so I'll use VS Code + Cortex Debug extension for now.
- I had the following debug macros activated in the chconf.h-file.

Code: Select all

#define CH_DBG_SYSTEM_STATE_CHECK           TRUE
#define CH_DBG_ENABLE_CHECKS                TRUE
#define CH_DBG_ENABLE_ASSERTS               TRUE
#define CH_DBG_ENABLE_STACK_CHECK           TRUE


Debugging result:
- No halt is triggered
- When I pressed pause to read the (ChibiOS/os/rt/src/chsys.c) ch0.dbg.panic_msg value, it was zero (0). (check attached .png file)

Please correct me if I'm reading from the wrong register. And what would be the next step?
Attachments
ch0.dbg.panic_msg = 0
ch0.dbg.panic_msg = 0
Engemil
Posts: 37
Joined: Thu Dec 19, 2024 12:50 am
Has thanked: 13 times
Been thanked: 10 times

Re: NUCLEO-C071RB not working with chThdSleepMilliseconds()

Post by Engemil »

I think there is something even more fundamental that I might have ignored. The size of the RAM and Flash of the STM32C071RB chip.

STM32C071RB has 128 Kbytes of Flash memory and 24 Kbytes RAM.

After compiling my simple code with optimization -O2, I get this:

Code: Select all

   text    data     bss     dec     hex filename
   6760       0   24576   31336    7a68 build/ch.elf
Creating build/fw_al1_mb1.list


Isn't bss only in RAM?, so in other words, ChibiOS/RT is too big for STM32C071RB?

If this is true, the RT-STM32C071RB-NUCLEO64 demo, should probably be changed to a NIL example. I'll give ChibiOS/NIL a try.

Please verify if I'm wrong or correct about this :)
Engemil
Posts: 37
Joined: Thu Dec 19, 2024 12:50 am
Has thanked: 13 times
Been thanked: 10 times

Re: NUCLEO-C071RB not working with chThdSleepMilliseconds()

Post by Engemil »

I just tried to copy ChibiOS/demos/STM32/NIL-STM32C031C6-NUCLEO64/ demo (branch stable_21.11.x), with the following changes.

Makefile (changes only at the shown sections)

Code: Select all

# Stack size to be allocated to the Cortex-M process stack. This stack is
# the stack used by the main() thread.
ifeq ($(USE_PROCESS_STACKSIZE),)
  #USE_PROCESS_STACKSIZE = 0x400
  USE_PROCESS_STACKSIZE = 0x100
endif
...

# Licensing files.
include $(CHIBIOS)/os/license/license.mk
# Startup files.
include $(CHIBIOS)/os/common/startup/ARMCMx/compilers/GCC/mk/startup_stm32c0xx.mk
# HAL-OSAL files (optional).
include $(CHIBIOS)/os/hal/hal.mk
include $(CHIBIOS)/os/hal/ports/STM32/STM32C0xx/platform.mk
include $(CHIBIOS)/os/hal/boards/ST_NUCLEO64_C071RB/board.mk
include $(CHIBIOS)/os/hal/osal/rt-nil/osal.mk
# RTOS files (optional).
##include $(CHIBIOS)/os/rt/rt.mk
include $(CHIBIOS)/os/nil/nil.mk
include $(CHIBIOS)/os/common/ports/ARMv6-M/compilers/GCC/mk/port.mk
# Auto-build files in ./source recursively.
include $(CHIBIOS)/tools/mk/autobuild.mk
# Other files (optional).
include $(CHIBIOS)/os/test/test.mk
##include $(CHIBIOS)/test/rt/rt_test.mk
include $(CHIBIOS)/test/nil/nil_test.mk
##include $(CHIBIOS)/test/oslib/oslib_test.mk


chconf.h: Copied directly from the NIL demo.

mcuconf.h No changes (for STM32C071RB)

halconf.h Seems to be the exact same

main.c

Code: Select all

#include "ch.h"
#include "hal.h"

volatile uint8_t i = 0;


/*
 * Blinker thread #1.
 */
static THD_WORKING_AREA(waThread1, 128);
static THD_FUNCTION(Thread1, arg) {

    (void)arg;

    while (true) {
        while (true) {
            palClearLine(LINE_LED_GREEN);
            chThdSleepMilliseconds(500);
            palSetLine(LINE_LED_GREEN);
            chThdSleepMilliseconds(500);
            i = i + 1;
        }
    }
}

/*
 * Threads creation table, one entry per thread.
 */
THD_TABLE_BEGIN
    THD_TABLE_THREAD(0, "blinker1",     waThread1,       Thread1,      NULL)
THD_TABLE_END

int main(void) {
    halInit();
    chSysInit();
    while (true) {
       // blank
    }
}



Results


- Not working
- Compilation size

Code: Select all

   text    data     bss     dec     hex filename
   4836       0   24576   29412    72e4 build/ch.elf
Creating build/ch.list
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: NUCLEO-C071RB not working with chThdSleepMilliseconds()

Post by Giovanni »

Hi,

Don't trust those sizes, the tool gets confused. If you want to see real sizes it is better to inspect the .map file or the .dmp file.

I need to check if I have a C071 somewhere.

If you want to reduce the code size you can remove the test code from the demo, the whole suite is huge, RT itself is few KBs.

Giovanni
Engemil
Posts: 37
Joined: Thu Dec 19, 2024 12:50 am
Has thanked: 13 times
Been thanked: 10 times

Re: NUCLEO-C071RB not working with chThdSleepMilliseconds()

Post by Engemil »

Thank you for the clarification!

I hope you find what might cause the system to hang, let me know if there is anything else I can look into.

As a question on the side, It seems that heap size is defined by the remaining "left-over" memory in RAM? If so, the yeah, the simple demo would only require 2.9KB of the RAM
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: NUCLEO-C071RB not working with chThdSleepMilliseconds()

Post by Giovanni »

Hi,

Correct, the default heap is all the unused RAM on simple MCUs.

I don't have a C071, will test again on a C031.

Giovanni
Engemil
Posts: 37
Joined: Thu Dec 19, 2024 12:50 am
Has thanked: 13 times
Been thanked: 10 times

Re: NUCLEO-C071RB not working with chThdSleepMilliseconds()

Post by Engemil »

Hi Giovanni!

Have you been able to replicate the bug? or you are debugging it?

I'll probably start comparing the behaviour between a Nucleo-F401RE to the Nucleo-C071RB, next week to see how they behave differently.


-Emil
Post Reply