This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

printk on USB port nrf dongle

I have working zigbee switch example for nrf52840 dongle and I want to add print. Am I using correct code changes? please suggest, Thank you.

printk is not giving any printouts. I can see the "Zephyr USB Console Sample" in available ports in windows.

I took reference from the zephyr/samples/subsys/usb/console example. 

nrf52840dongle_nrf52840.overlay

/*
 * Copyright (c) 2021 Nordic Semiconductor ASA
 *
 * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
 */

 / {
	chosen {
		zephyr,entropy = &rng;
		zephyr,console = &cdc_acm_uart0;
	};

	pwmleds {
		pwm_led3: pwm_led_3 {
			pwms = <&pwm0 12>;
		};
	};

	buttons {
		compatible = "gpio-keys";
		rst_button0: rst_button_0 {
			gpios = <&gpio0 19 (GPIO_PULL_UP | GPIO_ACTIVE_LOW)>;
			label = "Reset button";
		};
	};

	aliases {
		rst0 = &rst_button0;
	};
};


&zephyr_udc0 {
	compatible = "nordic,nrf-usbd";
	status = "okay";
	label = "USBD";
	cdc_acm_uart0: cdc_acm_uart0 {
		compatible = "zephyr,cdc-acm-uart";
		label = "CDC_ACM_0";
	};
};

prj.conf

#
# Copyright (c) 2020 Nordic Semiconductor ASA
#
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
#

#CONFIG_NCS_SAMPLES_DEFAULTS=y
CONFIG_USB_DEVICE_STACK=y
CONFIG_USB_DEVICE_PRODUCT="Zephyr USB console"


#CONFIG_UART_INTERRUPT_DRIVEN=y
CONFIG_SERIAL=y
CONFIG_GPIO=y

# Make sure printk is not printing to the UART console
CONFIG_CONSOLE=y
CONFIG_UART_CONSOLE=y
#CONFIG_UART_LINE_CTRL=y

CONFIG_HEAP_MEM_POOL_SIZE=2048

CONFIG_ZIGBEE=y
CONFIG_ZIGBEE_APP_UTILS=y
CONFIG_ZIGBEE_ROLE_END_DEVICE=y

# Enable DK LED and Buttons library
CONFIG_DK_LIBRARY=y

# This example requires more workqueue stack
CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=2048

# Enable nRF ECB driver
CONFIG_CRYPTO=y
CONFIG_CRYPTO_NRF_ECB=y
CONFIG_CRYPTO_INIT_PRIORITY=80

# Cryptocell is not supported through CSPRNG driver API: NCSDK-4813
CONFIG_ENTROPY_CC3XX=n

# Enable API for powering down unused RAM parts
CONFIG_RAM_POWER_DOWN_LIBRARY=y

#Networking
CONFIG_NET_IPV6_MLD=n
CONFIG_NET_IPV6_NBR_CACHE=n
CONFIG_NET_IPV6_RA_RDNSS=n
CONFIG_NET_IP_ADDR_CHECK=n
CONFIG_NET_UDP=n

kconfig

config USB_DEVICE_PID
	default USB_PID_CONSOLE_SAMPLE
source "Kconfig.zephyr"

& addition in main.c

BUILD_ASSERT(DT_NODE_HAS_COMPAT(DT_CHOSEN(zephyr_console), zephyr_cdc_acm_uart),
	     "Console device is not ACM CDC UART device");

// In main
const struct device *dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_console));
uint32_t dtr = 0;

if (usb_enable(NULL)) {
	return;
}

/* Poll if the DTR flag was set */
while (!dtr) {
	uart_line_ctrl_get(dev, UART_LINE_CTRL_DTR, &dtr);
	/* Give CPU resources to low priority threads. */
	k_sleep(K_MSEC(100));
}
printk("hello world");
	     

  • Hi, 

    I use your nrf52840dongle_nrf52840.overlay, this prj.conf

    #
    # Copyright (c) 2020 Nordic Semiconductor ASA
    #
    # SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
    #
    
    CONFIG_NCS_SAMPLES_DEFAULTS=y
    
    CONFIG_UART_INTERRUPT_DRIVEN=y
    CONFIG_SERIAL=y
    CONFIG_GPIO=y
    
    # Make sure printk is not printing to the UART console
    CONFIG_CONSOLE=y
    CONFIG_UART_CONSOLE=y
    
    
    CONFIG_HEAP_MEM_POOL_SIZE=2048
    
    CONFIG_ZIGBEE=y
    CONFIG_ZIGBEE_APP_UTILS=y
    CONFIG_ZIGBEE_ROLE_END_DEVICE=y
    
    # Enable DK LED and Buttons library
    CONFIG_DK_LIBRARY=y
    
    # This example requires more workqueue stack
    CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=2048
    
    # Enable nRF ECB driver
    CONFIG_CRYPTO=y
    CONFIG_CRYPTO_NRF_ECB=y
    CONFIG_CRYPTO_INIT_PRIORITY=80
    
    # Cryptocell is not supported through CSPRNG driver API: NCSDK-4813
    CONFIG_ENTROPY_CC3XX=n
    
    # Enable API for powering down unused RAM parts
    CONFIG_RAM_POWER_DOWN_LIBRARY=y
    
    #Networking
    CONFIG_NET_IPV6_MLD=n
    CONFIG_NET_IPV6_NBR_CACHE=n
    CONFIG_NET_IPV6_RA_RDNSS=n
    CONFIG_NET_IP_ADDR_CHECK=n
    CONFIG_NET_UDP=n
    
    ##############
    
    CONFIG_USB_DEVICE_STACK=y
    CONFIG_USB_DEVICE_DRIVER=y
    CONFIG_USB_DEVICE_PRODUCT="Zephyr USB console sample"
    CONFIG_USB_DEVICE_MANUFACTURER="Nordic Semiconductor ASA"
    CONFIG_USB_DEVICE_VID=0x1915
    CONFIG_USB_DEVICE_PID=0x0100
    
    CONFIG_UART_LINE_CTRL=y
    CONFIG_USB_CDC_ACM_LOG_LEVEL_OFF=y
    
    # Since the default LOG_MODE_MINIMAL is synchronous, it will mess with the USB
    # driver/subsys. Use the deferred mode instead.
    CONFIG_LOG_MODE_DEFERRED=y
     

    and adding the following in main.c

    #include <sys/printk.h>
    #include <usb/usb_device.h>
    #include <drivers/uart.h>
    
    BUILD_ASSERT(DT_NODE_HAS_COMPAT(DT_CHOSEN(zephyr_console), zephyr_cdc_acm_uart),
    	     "Console device is not ACM CDC UART device");
    
    void main(void)
    {
    	const struct device *dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_console));
    	uint32_t dtr = 0;
    
    	if (usb_enable(NULL)) {
    		return;
    	}
    
    	/* Poll if the DTR flag was set */
    	while (!dtr) {
    		uart_line_ctrl_get(dev, UART_LINE_CTRL_DTR, &dtr);
    		/* Give CPU resources to low priority threads. */
    		k_sleep(K_MSEC(100));
    	}
    
    	int console_init(void);
    	printk("hello world");
    
    //----------------------------
    	LOG_INF("Starting ZBOSS Light Switch example");

    It can get both logging and printk as

    Here is the project: light_switch_zigbee_log_NCSv1.9.7z

    Regards,
    Amanda

Related