Beware that this post is related to an SDK in maintenance mode
More Info: Consider nRF Connect SDK for new designs
This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Is SEGGER_RTT_printf() thread safe with FreeRTOS?

Hey Guys,

I have 3 tasks running on FreeRTOS, SDK v14.0.0.  In each task, I want to use SEGGER_RTT_printf() to debug.

I currently just call SEGGER_RTT_printf() without any semaphores or mutexes, but I assume this is a bad idea.

Should I be grabbing a semaphore or mutex before using RTT and then giving it back afterwards?  Thanks.

Parents
  • Actually, you should be safe. Here is why:

    SEGGER_RTT_printf() calls into

    unsigned SEGGER_RTT_Write(unsigned BufferIndex, const void* pBuffer, unsigned NumBytes)

    file: SEGGER_RTT.c

    And here SEGGER_RTT_LOCK() is called, which is #defined to CRITICAL_REGION_ENTER() and there is an UNLOCK which is #defined to _EXIT() ...

     // This gets weary, all the sdk_config.h, macro hell.

    This gets #defined to app_util_critical_region_enter()

    If SOFTDEVICE_PRESENT is defined then sd_nvic_critical_region_enter(nested) is called which looks quite deliberate and scary. (Someone appears to either really know what they are doing or ... I'll assume they know what they are doing).

    if not defined then app_util_disable_irq() is calledwhich is #defined to the intrinsic __disable_irq();

    ---

    So bottom line: they took care of it. Just so long as you have your sdk_config.h set they way Nordic expects and did not over-write something critical. The C preprocessor is a horrible tool.

  • You're right that does seem to be the case.  I didn't bother to step that deep into the definitions initially.

    Hopefully a Nordic support member can comment and confirm this.

  • Hi,

    I agree with Nathaniel here. It should be safe both with and without the SoftDevice enabled. sd_nvic_critical_region_enter will still allow interrupts reaching the SoftDevice, but there will not be any events for the application in the critical region. This means that timing is not reliable even in critical regions, but that shouldn't be a problem for RTT since it's essentially just data being pushed.

    Best regards,
    Rune Holmgren

     

Reply
  • Hi,

    I agree with Nathaniel here. It should be safe both with and without the SoftDevice enabled. sd_nvic_critical_region_enter will still allow interrupts reaching the SoftDevice, but there will not be any events for the application in the critical region. This means that timing is not reliable even in critical regions, but that shouldn't be a problem for RTT since it's essentially just data being pushed.

    Best regards,
    Rune Holmgren

     

Children
Related