# FreeRTOS example for nRF9160

**URL:** <https://forums.freertos.org/t/freertos-example-for-nrf9160/14080>\
**Category:** Kernel\
**Created:** [December 17, 2021, 5:40pm UTC](https://forums.freertos.org/t/freertos-example-for-nrf9160/14080 "2021-12-17T17:40:13Z")\
**Posts on this page:** 12\
**Page:** 2

<div class="post-metadata">

**Author:** ![beethoven62](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@beethoven62](https://forums.freertos.org/u/beethoven62)\
**Post date:** [November 15, 2022, 9:31pm UTC](https://forums.freertos.org/t/freertos-example-for-nrf9160/14080/21 "2022-11-15T21:31:23Z")

</div>

I found another way to set the stack and heap sizes by using the preprocessor defines. These arm\_startup\_nrf9160.s files are generated by uVision. I suspect it comes from the device family pack that I installed for the nRF9160.

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [November 16, 2022, 4:55am UTC](https://forums.freertos.org/t/freertos-example-for-nrf9160/14080/22 "2022-11-16T04:55:20Z")

</div>

Great - so both the issues are resolved or MPU demo is still not working?

---

<div class="post-metadata">

**Author:** ![beethoven62](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@beethoven62](https://forums.freertos.org/u/beethoven62)\
**Post date:** [November 17, 2022, 9:56pm UTC](https://forums.freertos.org/t/freertos-example-for-nrf9160/14080/23 "2022-11-17T21:56:23Z")

</div>

Yes. It’s working for both demos now. I moved on to other projects using the nRF9160 and Keil uVision. I’m having issues now with _printf()_ and redirecting I/O either through one of the UARTs or using the ITM. On Segger’s Embedded Studio, it was kind of built in and all you had to do was include stdio.h. Any ideas?

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [November 18, 2022, 5:25am UTC](https://forums.freertos.org/t/freertos-example-for-nrf9160/14080/24 "2022-11-18T05:25:56Z")

</div>

Depends on the `printf` implementation but it usually calls some `write`/`put` function which you can implement to redirect output to UART.

Try to define `int _write(int file, char *ptr, int len)` function and put a breakpoint and see if it is getting called by `printf`.

---

<div class="post-metadata">

**Author:** ![beethoven62](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@beethoven62](https://forums.freertos.org/u/beethoven62)\
**Post date:** [November 18, 2022, 6:00pm UTC](https://forums.freertos.org/t/freertos-example-for-nrf9160/14080/25 "2022-11-18T18:00:10Z")

</div>

Yes, I tried that by using the RTE manager and redirecting I/O to user. This created a file, _stdout\_user.c_, where a putc() function stub could be found. This function is implemented by the user (I would send out through one of the UARTs) and is ultimately called by _printf_. However, when I do that and include stdio.h, the nonsecure boot is interrupted. It never gets to main(). I’m not sure why this is happening. If I implement this functionality by directly sending characters out of the intended UART myself, it works. As soon as I try to get uVision to help, it never gets to main().

Any ideas?

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [November 21, 2022, 5:15am UTC](https://forums.freertos.org/t/freertos-example-for-nrf9160/14080/26 "2022-11-21T05:15:50Z")

</div>

> [@beethoven62](#):
>
> However, when I do that and include stdio.h, the nonsecure boot is interrupted. It never gets to main(). I’m not sure why this is happening.

This happens even with the empty/stub definition of putc? Can you find out why the control is not reaching to main?

---

<div class="post-metadata">

**Author:** ![beethoven62](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@beethoven62](https://forums.freertos.org/u/beethoven62)\
**Post date:** [November 21, 2022, 9:44pm UTC](https://forums.freertos.org/t/freertos-example-for-nrf9160/14080/27 "2022-11-21T21:44:26Z")

</div>

Looks like it needed a little heap space from the start up file. I defined \_\_HEAP\_SIZE=128 and it started at main(). I tried to use the ITM, but I don’t see any output in the debug window. I can set up for custom user output to use the UART instead, but that will require some code. Also, the RTE doesn’t seem to set up a USART interface for the nRF9160.

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [November 22, 2022, 10:51am UTC](https://forums.freertos.org/t/freertos-example-for-nrf9160/14080/28 "2022-11-22T10:51:49Z")

</div>

> [@beethoven62](#):
>
> I defined \_\_HEAP\_SIZE=128 and it started at main().

Great. Now what happens if you try to define the put function in _stdout\_user.c_ to output on UART?

---

<div class="post-metadata">

**Author:** ![beethoven62](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@beethoven62](https://forums.freertos.org/u/beethoven62)\
**Post date:** [November 22, 2022, 5:25pm UTC](https://forums.freertos.org/t/freertos-example-for-nrf9160/14080/29 "2022-11-22T17:25:49Z")

</div>

Yes, I can get it to print out of the UART but there is a catch. The _printf()_ function works as is when outside a task (e.g. in main before the scheduler starts). If I try to execute it inside of a task, nothing is sent over the UART. If I set the privileged bit when I start a task that uses printf, then I see output from the UART. I can’t make every task that uses the printf function, privileged. How do I set this up? Here’s the code:

```auto
/**
 * @brief Implements the task which will blink the LEDs.
 *
 * @param pvParameters[in] Parameters as passed during task creation.
 */
void vStartBlinkyDemo( void )
{
    uint16_t i;
    static StackType_t xBlinkyTaskStack[configMINIMAL_STACK_SIZE] __attribute__ ( ( aligned( 32 ) ) );
    TaskParameters_t xBlinkyTaskParameters =
    {
        .pvTaskCode = prvBlinkyTask,
        .pcName = "Blinky",
        .usStackDepth = configMINIMAL_STACK_SIZE,
        .pvParameters = NULL,
        .uxPriority = tskIDLE_PRIORITY | portPRIVILEGE_BIT,
        .puxStackBuffer = xBlinkyTaskStack,
        .xRegions =
        {
            { ( void *)NRF_P0_NS, sizeof( NRF_GPIO_Type ), tskMPU_REGION_READ_WRITE | tskMPU_REGION_EXECUTE_NEVER },
            { 0, 0, 0 },
            { 0, 0, 0 },
            { 0, 0, 0 },
            { 0, 0, 0 },
            { 0, 0, 0 },
            { 0, 0, 0 },
            { 0, 0, 0 },
            { 0, 0, 0 },
            { 0, 0, 0 },
            { 0, 0, 0 },
        }
    };

```

In the meantime, I’m still interested in printing debug messages using the ITM.

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [November 23, 2022, 6:54am UTC](https://forums.freertos.org/t/freertos-example-for-nrf9160/14080/30 "2022-11-23T06:54:06Z")

</div>

> [@beethoven62](#):
>
> If I set the privileged bit when I start a task that uses printf, then I see output from the UART. I can’t make every task that uses the printf function, privileged.

You need to make UART accessible to the unprivileged task which is calling printf. You can post all the messages that you want to output to a queue and have one task which read all the messages from the queue and output them. That way only the task reading from the queue needs to call printf.

> [@beethoven62](#):
>
> In the meantime, I’m still interested in printing debug messages using the ITM.

Does this work before starting FreeRTOS?

---

<div class="post-metadata">

**Author:** ![beethoven62](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@beethoven62](https://forums.freertos.org/u/beethoven62)\
**Post date:** [November 29, 2022, 8:50pm UTC](https://forums.freertos.org/t/freertos-example-for-nrf9160/14080/31 "2022-11-29T20:50:54Z")

</div>

> You need to make UART accessible to the unprivileged task which is calling printf. You can post all the messages that you want to output to a queue and have one task which read all the messages from the queue and output them. That way only the task reading from the queue needs to call printf.

I did this and it works. I used the same code I used for SES.

> In the meantime, I’m still interested in printing debug messages using the ITM.
> 
> > Does this work before starting FreeRTOS?

It does not. The code compiles with no errors, the function that is called to use the trace modules returns with no errors. However, I see no output on the _printf (debug) window_. All instructions to set up the debugger (JLink in my case) to use the trace module (SWO pin) is set up.

Any ideas?

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [December 6, 2022, 9:52am UTC](https://forums.freertos.org/t/freertos-example-for-nrf9160/14080/32 "2022-12-06T09:52:20Z")

</div>

> [@beethoven62](#):
>
> the function that is called to use the trace modules returns with no errors.

Can you step through this function and see if it is writing to the relevant registers? If it does not work without FreeRTOS, then it is probably not related to FreeRTOS and may be a good question for Nordic.

[Previous page](https://forums.freertos.org/t/freertos-example-for-nrf9160/14080.md?page=1)
