Printk statement not printing properly

I have just started programming the using the nrf52840DK_nrf52840. I downloaded the toolchain and sdk using the VS code extension and created a new application from the blinky sample. The light is blinking properly, im able to change the time between blinks and it works just fine. The problem is that the printk statements are printing garbled outputs. I thought it is a baud rate issue so I checked it for all baud rates from 9600 to 115200, i also tried with flow control on and off. Im using the recommended settings of 8 data bits, 1 stop bit and none Parity. At first i didnt modify the blinky code at all. But after reading https://academy.nordicsemi.com/courses/nrf-connect-sdk-fundamentals/lessons/lesson-3-printing-messages-to-console-and-logging/topic/printk-function/ I added the statement 

#include <zephyr/sys/printk.h>
but there was still no difference. I also modified the proj.conf file and added
CONFIG_UART_CONSOLE=y
CONFIG_CONSOLE=y
CONFIG_LOG=y
still no change. I also tried using the logging functions like LOG_INF (after adding required header files and initializing it) but it also give me the same type of garbled output.

Can anyone please tell me how to resolve it. (I'm using Toolchain and connect sdk version 2.6.1) 

Print statement:  

printk("LED state: %s\r\n", led_state ? "ON" : "OFF");

Output: 

D t`: DFS0002 0
X Z hF>MxN`` ( j
LEDc@`::6:0.50(0 "
J1 48 0 H FNS m


E@ t`:F
Z 2"4048&\ S0i<hn> ^ideD: NFANZ
LE R00&2"0$8\ S <Hn> q^iDae:LNHN
R020&244

i tried viewing in vscode, arduino IDE, and tera term all give similar outputs Pls help me i need the logging functionality for my upcoming project

  • HI Kevin,

    Could you try using CONFIG_LOG_PRINTK=y

    Regards,

    Priyanka

  • I have tried it. I dont know if it is beacuse of that but output seems slightly more readable

    LED stte:LED stte:LED stte:LED stte:LED stte:LED stte:HLED stte:LED stWLED stte:HLED stte:@

    But it is still not proper.

    proj.conf

    CONFIG_GPIO=y
    CONFIG_LOG_PRINTK=y
    CONFIG_CONSOLE=y
    CONFIG_UART_CONSOLE=y
    if i also enable CONFIG_LOG=y I get output 
    LED s@te: `
    LED s@te@PLED s`te:@p
    LED s@te:HP
    LED s`te:@ p@LED s@te:@
    here is the code:
    /*
     * Copyright (c) 2016 Intel Corporation
     *
     * SPDX-License-Identifier: Apache-2.0
     */

    #include <zephyr/kernel.h>
    #include <zephyr/drivers/gpio.h>
    #include <zephyr/sys/printk.h>

    /* 1000 msec = 1 sec */
    #define SLEEP_TIME_MS   1000

    /* The devicetree node identifier for the "led0" alias. */
    #define LED0_NODE DT_ALIAS(led0)

    /*
     * A build error on this line means your board is unsupported.
     * See the sample documentation for information on how to fix this.
     */
    static const struct gpio_dt_spec led = GPIO_DT_SPEC_GET(LED0_NODE, gpios);

    int main(void)
    {
        int ret;

        if (!gpio_is_ready_dt(&led)) {
            return 0;
        }

        ret = gpio_pin_configure_dt(&led, GPIO_OUTPUT_ACTIVE);
        if (ret < 0) {
            return 0;
        }

        bool led_state = true;
       
        while (1) {
            ret = gpio_pin_toggle_dt(&led);
            if (ret < 0) {
                return 0;
            }
            led_state = !led_state;
            printk("LED state: %s\r\n", led_state ? "ON" : "OFF");
            k_msleep(SLEEP_TIME_MS);
        }
        return 0;
    }
    I am also using the default device tree file nrf52840dk_nrf52840.dts that comes with the sdk without any overlays. I am using NRF serial terminal to see the output with 115200 8n1 Flowcontrol:off. Is there any msg length limitation that is causing messages to overlap?
  • Hi Kevin,

    Which pins are you using for UART tx and rx? Please verify that you use the correct pin numbers. Could you try adding the following to prj.conf:

    CONFIG_LOG_DEFAULT_LEVEL=4
    CONFIG_LOG_MODE_DEFERRED=n 

    You could also try to explicitly set the baud rate by

    CONFIG_UART_CONSOLE_BAUDRATE=115200

    If you still have the issue, please try to run a simple hello world sample and we can check if you get a proper output.

    -Priyanka

  • Im pretty sure Im missing something here. Im just using the USB cable connected to the DK to see this, I'm not using any pins. I am connecting to the com port and just viewing the data in NRF connect serial terminal. The hello world example was not printing anything i thought it might be too fast and so i put it in a while loop and got output: 

    Hello `rlHello hrlHello `rl Hello `rld Hello `rld

    code:

    /*
     * Copyright (c) 2012-2014 Wind River Systems, Inc.
     *
     * SPDX-License-Identifier: Apache-2.0
     */

    #include <stdio.h>
    #include <zephyr/kernel.h>

    int main(void)
    {
         while (true) {
            // Your code here, e.g., printing or processing
           
        printf("Hello World! %s\n", CONFIG_BOARD);

            // Sleep for 1 second (1000 milliseconds)
            k_sleep(K_MSEC(1000));
        }
        return 0;
    }
    P.S: Unknown symbol CONFIG_UART_CONSOLE_BAUDRATE
  • Hi Kevin,

    Let's take a look step by step. Please tell me which SDK version are you working with? Is it 2.7.0?

    Which DK revision are you using? You can find the details on the white sticker on the board.

    Did you build the default hello world sample in the SDK without making any changes to it? If not, please try this.

    What is you nRF Command Line Tools version?

    -Priyanka

Related