Problems with device reset and RTT terminal output not showing after flashing

EDIT: I thought I had enabled RTT terminal output in this proejct, which I had not. Obviously this will have an impact on my problems sorrounding RTT terminal output. So I'm currently adding RTT terminal output and will revisit this thread.

Hi,

I will try to explain my problems as best as I can. It is somewhat intermittent but occuring more often than not. Also I'm a newcomer to Nordic NRF SDK so my understanding is not helping much out either.

My hardware setup is a custom board using NRF52810 connected via SWD header P20 on a NRF52832 DK. The custom board is self powered using a CR2032 coin cell. I experience the same issues using the DK MCU directly.

Software setup is NRF Connect SDK version 2.6.1 on VS Code. My code is a modified example of the lis2dh12 sample code with some added GPIO (sensor readout not working at the moemnt, will probably post about this when my setup is behaving).

Problem(s):

When I flash my board, it seems that sometimes it doesn't reset. I have to re-insert the coin cell. For the DK I could simply push the reset button "IF BOOT/RESET". Using the DK MCU, sometimes I could simply wait for a "slow boot" and it would eventually boot without me intervening. 

When the board resets (which I can see from that LEDs starts blinking) I don't always get any RTT terminal output. Usually I don't. I have some printf's and log output which occur during boot when things act normal (from my perspective):
SEGGER J-Link V7.96l - Real time terminal output
SEGGER J-Link (unknown) V1.0, SN=682410030
Process: JLink.exe
Could not get LIS2DH device
[00:00:00.912,170] <err> lis2dh: Failed to read chip id.
*** Booting nRF Connect SDK 3758bcbfa5cd ***

This is the output when nothing is printed:
SEGGER J-Link V7.96l - Real time terminal output
SEGGER J-Link (unknown) V1.0, SN=682410030
Process: JLink.exe

I have no idea which part of the project/code might be contributing to this behavior so I'm not including source files other than main.c at this moment.

main.c

/*
 * Copyright (c) 2019 Nordic Semiconductor ASA
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include <stdio.h>
#include <zephyr/kernel.h>
#include <zephyr/device.h>
#include <zephyr/drivers/sensor.h>
#include <zephyr/drivers/gpio.h>
#include <zephyr/sys/util.h>
#include <inttypes.h>

#define LED0_NODE DT_NODELABEL(led_b)
#define ADDR_NODE DT_NODELABEL(lis2dh12_addr)
#define CS_NODE DT_NODELABEL(lis2dh12_cs)

static void fetch_and_display(const struct device *sensor)
{
	static unsigned int count;
	struct sensor_value accel[3];
	struct sensor_value temperature;
	const char *overrun = "";
	int rc = sensor_sample_fetch(sensor);

	++count;
	if (rc == -EBADMSG) {
		/* Sample overrun.  Ignore in polled mode. */
		if (IS_ENABLED(CONFIG_LIS2DH_TRIGGER)) {
			overrun = "[OVERRUN] ";
		}
		rc = 0;
	}
	if (rc == 0) {
		rc = sensor_channel_get(sensor,
					SENSOR_CHAN_ACCEL_XYZ,
					accel);
	}
	if (rc < 0) {
		printf("ERROR: Update failed: %d\n", rc);
	} else {
		printf("#%u @ %u ms: %sx %f , y %f , z %f",
		       count, k_uptime_get_32(), overrun,
		       sensor_value_to_double(&accel[0]),
		       sensor_value_to_double(&accel[1]),
		       sensor_value_to_double(&accel[2]));
	}

	if (IS_ENABLED(CONFIG_LIS2DH_MEASURE_TEMPERATURE)) {
		if (rc == 0) {
			rc = sensor_channel_get(sensor, SENSOR_CHAN_DIE_TEMP, &temperature);
			if (rc < 0) {
				printf("\nERROR: Unable to read temperature:%d\n", rc);
			} else {
				printf(", t %f\n", sensor_value_to_double(&temperature));
			}
		}

	} else {
		printf("\n");
	}
}

#ifdef CONFIG_LIS2DH_TRIGGER
static void trigger_handler(const struct device *dev,
			    const struct sensor_trigger *trig)
{
	fetch_and_display(dev);
}
#endif

int main(void)
{
	int ret;
	static const struct gpio_dt_spec led = GPIO_DT_SPEC_GET(LED0_NODE, gpios);
	static const struct gpio_dt_spec addr = GPIO_DT_SPEC_GET(ADDR_NODE, gpios);
	static const struct gpio_dt_spec cs = GPIO_DT_SPEC_GET(CS_NODE, gpios);
	const struct device *const sensor = DEVICE_DT_GET_ANY(st_lis2dh);
	
	if (!gpio_is_ready_dt(&led)) {
		return 0;
	}

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

	printf("Kommer hit\n");

	gpio_pin_set_dt(&cs, 1); //Puts LIS2DH12 CS high, means I2C mode.
	gpio_pin_set_dt(&addr, 1); //Sets address pin high, means address = 0x19 (probably)

	k_sleep(K_MSEC(2000));

	gpio_pin_set_dt(&led, 1); //Turn on blue LED


	if (sensor == NULL) {
		printf("No device found\n");
		return 0;
	}
	if (!device_is_ready(sensor)) {
		printf("Device %s is not ready\n", sensor->name);
		return 0;
	}

#if CONFIG_LIS2DH_TRIGGER
	{
		struct sensor_trigger trig;
		int rc;

		trig.type = SENSOR_TRIG_DATA_READY;
		trig.chan = SENSOR_CHAN_ACCEL_XYZ;

		if (IS_ENABLED(CONFIG_LIS2DH_ODR_RUNTIME)) {
			struct sensor_value odr = {
				.val1 = 1,
			};

			rc = sensor_attr_set(sensor, trig.chan,
					     SENSOR_ATTR_SAMPLING_FREQUENCY,
					     &odr);
			if (rc != 0) {
				printf("Failed to set odr: %d\n", rc);
				return 0;
			}
			printf("Sampling at %u Hz\n", odr.val1);
		}

		rc = sensor_trigger_set(sensor, &trig, trigger_handler);
		if (rc != 0) {
			printf("Failed to set trigger: %d\n", rc);
			return 0;
		}

		printf("Waiting for triggers\n");
		while (true) {
			k_sleep(K_MSEC(2000));
		}
	}
#else /* CONFIG_LIS2DH_TRIGGER */
	printf("Polling at 0.5 Hz\n");
	while (true) {
		fetch_and_display(sensor);
		k_sleep(K_MSEC(2000));
	}
#endif /* CONFIG_LIS2DH_TRIGGER */
}

Related