Trying to use UART2 witn custom nrf9151 board non secure

Hi all,

I'm trying to use uart2 on my custom nrf9151 board, but I'm stuck. As I understand, I use i2c1 so I can't use uart1 and i2c1in parallel, so here is why I wnt to use uart2. 

Here is my configufration: I use P0.23 and P0.24 in my dtsi file

	uart2_default: uart2_default {
		group1 {
			psels = <NRF_PSEL(UART_TX, 0, 23)>,
				<NRF_PSEL(UART_RX, 0, 24)>;
		};
	};

	uart2_sleep: uart2_sleep {
		group1 {
			psels = <NRF_PSEL(UART_TX, 0, 23)>,
				<NRF_PSEL(UART_RX, 0, 24)>;
			low-power-enable;
		};
	};

And in overlay file

&uart2 {
    status = "okay";
    current-speed = <115200>;
    pinctrl-0 = <&uart2_default>;
    pinctrl-1 = <&uart2_sleep>;
    pinctrl-names = "default", "sleep";
};

and I've added CONFIG_UART_ASYNC_API=y in my prj.conf. So after all thaht setup I've write uart_async.c file (inspired by devacedemy here)

#define UART_NODE DT_NODELABEL(uart2)
#define RX_BUF_SIZE 64
#define RX_TIMEOUT 100

static const struct device *uart_dev;
static uint8_t rx_buf[RX_BUF_SIZE];

struct tx_msg {
	uint8_t data[64];
	size_t len;
};


static void uart_cb(const struct device *dev, struct uart_event *evt, void *user_data) {
	switch (evt->type) {

	case UART_TX_DONE:
		LOG_DBG("TX done (%d bytes)", evt->data.tx.len);
		break;

	case UART_TX_ABORTED:
		LOG_ERR("TX aborted");
		break;

	case UART_RX_RDY:
		if (evt->data.rx.len > 0) {
			LOG_INF("RX");
			LOG_HEXDUMP_INF(&evt->data.rx.buf[evt->data.rx.offset],
			                evt->data.rx.len,
			                "RX Data");

			for (size_t i = evt->data.rx.offset; i < evt->data.rx.offset + evt->data.rx.len; i++) {
				if (rx_buf[i] == '\n') {
					LOG_INF("End of message detected");
				}
			}
		}
		break;

	case UART_RX_DISABLED:
		LOG_WRN("RX disabled, re-enabling");
		uart_rx_enable(dev, rx_buf, sizeof(rx_buf), RX_TIMEOUT);
		break;

	case UART_RX_BUF_REQUEST:
		// RX buffer management not used in this basic example
		break;

	case UART_RX_BUF_RELEASED:
		// RX buffer management not used in this basic example
		break;

	case UART_RX_STOPPED:
		LOG_ERR("RX stopped due to error %d", evt->data.rx_stop.reason);
		break;

	default:
		break;
	}
}

int uart_init(void) {
	uart_dev = DEVICE_DT_GET(UART_NODE);
	if (!device_is_ready(uart_dev)) {
		LOG_ERR("UART device not ready");
		return -ENODEV;
	}

	const struct uart_config uart_cfg = {
		.baudrate = 115200,
		.parity = UART_CFG_PARITY_NONE,
		.stop_bits = UART_CFG_STOP_BITS_1,
		.data_bits = UART_CFG_DATA_BITS_8,
		.flow_ctrl = UART_CFG_FLOW_CTRL_NONE
	};

	int ret = uart_configure(uart_dev, &uart_cfg);

	if (ret == -ENOSYS) {
		return -ENOSYS;
	}


	ret = uart_rx_enable(uart_dev, rx_buf, sizeof(rx_buf), RX_TIMEOUT);
	if (ret) {
		LOG_ERR("Failed to enable UART RX (%d)", ret);
		return ret;
	}

	LOG_INF("UART initialized");
	return 0;
}

int uart_send_data(const uint8_t *data, size_t len) {
	int ret = uart_tx(uart_dev, data, len, SYS_FOREVER_MS);
	if (ret) {
		LOG_ERR("UART TX failed (%d)", ret);
		return ret;
	}
	return 0;
}

and my main

	uart_init();


	static uint8_t tx_buf[] = {
		"nRF Connect SDK Fundamentals Course\r\n"
	};

	uart_send_data(tx_buf, sizeof(tx_buf));
	....

Everything build but when my board boot, I receive my tx data but after thaht my board seem to be freeze nothing happen. If I boot my board without cp21 connected to P0.23/24 my board boot and run normaly, but if I try to send data through my cp21 i receive nothing...

Is there other configuration I missed in non secure to use uart2? It's seem RX pin to somthing, if I unplung it from mycp2102 my board boot normally

  • I was able to enable CONFIG_UART_2_ASYNC with CONFIG_SHLL=y if I also disabled CONFIG_UART_2_INTERRUPT_DRIVEN. Did you try the same? 

    Jamal_nRF said:
    With that I had to trade uart_set_callback() --which seems to be only supported for ASYNC API???

    Yes, you're right. Please refer to the "UART async driver API" documentation which describes the difference between these.

  • Hi Vidar,

    Re: I was able to enable CONFIG_UART_2_ASYNC with CONFIG_SHLL=y if I also disabled CONFIG_UART_2_INTERRUPT_DRIVEN. Did you try the same? 

    I will give that a try and let you know shortly...

    As a matter of fact, I encountered a very similar situation on the Serial LTE Modem (SLM) application

    So I will try it there as well...

    Thanks

  • Hello Vidar,

    I tried your suggestion for the UART configuration, here is my complete prj.conf content

    it seems the CONFIG_UART_2_ASYNC is getting forced to n despite being set to y

    # CONFIG_UART_ASYNC_API=y
    CONFIG_UART_2_ASYNC=y
    CONFIG_UART_2_INTERRUPT_DRIVEN=n
    # CONFIG_UART_INTERRUPT_DRIVEN=y
    CONFIG_UART_USE_RUNTIME_CONFIGURE=y

    # Modem library
    CONFIG_NRF_MODEM_LIB=y
    CONFIG_AT_MONITOR=y
    CONFIG_LTE_LINK_CONTROL=y
    CONFIG_AT_HOST_LIBRARY=y

    # Enable debug and assertions.
    CONFIG_DEBUG=y
    CONFIG_ASSERT=y

    # Logging configurations.
    CONFIG_LOG=y
    CONFIG_LOG_MODE_IMMEDIATE=y
    CONFIG_LOG_BACKEND_SHOW_COLOR=y
    CONFIG_LOG_INFO_COLOR_GREEN=y

    # Shell and console support.
    CONFIG_SHELL=y
    CONFIG_SHELL_LOG_LEVEL_INF=y
    CONFIG_SHELL_STACK_SIZE=3072

    still getting a failure on the uart_callback_set call, although the error code is -134 instead of -88

    [00:00:00.732,635] <inf> main: uart_configure (0)
    [00:00:00.738,037] <err> main: uart_callback_set (-134)

  • -ENOTSUP (-134) is returned when CONFIG_UART_ASYNC_API is not enabled.
  • OK, after restoring both configuration,

    the callback set works, and the UART2 communication works as well

    However, the shell / help command on the main console / uart0 does not seem to work

    Did I miss another configuration?

    Thanks,

    CONFIG_UART_ASYNC_API=y
    CONFIG_UART_2_ASYNC=y
Related