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

/**
 * @addtogroup t_driver_uart
 * @{
 * @defgroup t_uart_mix_fifo_poll test_uart_mix_fifo_poll
 * @}
 */

#include <zephyr/drivers/uart.h>
#include <ztest.h>
#include <zephyr/drivers/counter.h>
#include <zephyr/random/rand32.h>
/* RX and TX pins have to be connected together*/

#if defined(CONFIG_BOARD_NRF52840DK_NRF52840)
#define UART_DEVICE_DEV DT_NODELABEL(uart0)
#elif defined(CONFIG_BOARD_NRF9160DK_NRF9160)
#define UART_DEVICE_DEV DT_NODELABEL(uart1)
#elif defined(CONFIG_BOARD_ATSAMD21_XPRO)
#define UART_DEVICE_DEV DT_NODELABEL(sercom1)
#elif defined(CONFIG_BOARD_ATSAMR21_XPRO)
#define UART_DEVICE_DEV DT_NODELABEL(sercom3)
#elif defined(CONFIG_BOARD_ATSAME54_XPRO)
#define UART_DEVICE_DEV DT_NODELABEL(sercom1)
#else
#define UART_DEVICE_DEV DT_CHOSEN(zephyr_console)
#endif

struct rx_source {
	int cnt;
	uint8_t prev;
};

#define BUF_SIZE 16

/* Buffer used for polling. */
static uint8_t txbuf[3][BUF_SIZE];

/* Buffer used for async or interrupt driven apis.
 * One of test configurations checks if RO buffer works with the driver.
 */
static IF_ENABLED(TEST_CONST_BUFFER, (const)) uint8_t txbuf3[16] = {
	0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
	0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f
};

struct test_data {
	const uint8_t *buf;
	volatile int cnt;
	int max;
	struct k_sem sem;
};

static struct rx_source source[4];
static struct test_data test_data[3];
static struct test_data int_async_data;

static const struct device *counter_dev;
static const struct device *uart_dev;

static bool async;
static bool int_driven;
static volatile bool async_rx_enabled;
static struct k_sem async_tx_sem;

static void int_driven_callback(const struct device *dev, void *user_data);
static void async_callback(const struct device *dev,
			   struct uart_event *evt, void *user_data);

static void process_byte(uint8_t b)
{
	int base = b >> 4;
	struct rx_source *src = &source[base];
	bool ok;

	b &= 0x0F;
	src->cnt++;

	if (src->cnt == 1) {
		src->prev = b;
		return;
	}

	ok = ((b - src->prev) == 1) || (!b && (src->prev == 0x0F));

	zassert_true(ok, "Unexpected byte received:0x%02x, prev:0x%02x",
			(base << 4) | b, (base << 4) | src->prev);
	src->prev = b;
}

static void counter_top_handler(const struct device *dev, void *user_data)
{
	static bool enable = true;
	static uint8_t async_rx_buf[4];

	if (async && !async_rx_enabled) {
		int err;

		err = uart_rx_enable(uart_dev, async_rx_buf,
				     sizeof(async_rx_buf), 1 * USEC_PER_MSEC);
		zassert_true(err >= 0, NULL);
		async_rx_enabled = true;
	} else if (int_driven) {
		if (enable) {
			uart_irq_rx_enable(uart_dev);
		} else {
			uart_irq_rx_disable(uart_dev);
		}

		enable = !enable;
	} else if (!async && !int_driven) {
		uint8_t c;

		while (uart_poll_in(uart_dev, &c) >= 0) {
			process_byte(c);
		}
	}
}

static void init_test(void)
{
	int err;
	struct counter_top_cfg top_cfg = {
		.callback = counter_top_handler,
		.user_data = NULL,
		.flags = 0
	};

	uart_dev = DEVICE_DT_GET(UART_DEVICE_DEV);
	zassert_true(device_is_ready(uart_dev), "uart device is not ready");

	if (uart_callback_set(uart_dev, async_callback, NULL) == 0) {
		async = true;
	} else {
		async = false;
		int_driven = uart_irq_tx_complete(uart_dev) >= 0;
		if (int_driven) {
			uart_irq_callback_set(uart_dev, int_driven_callback);
		}
	}

	/* Setup counter which will periodically enable/disable UART RX,
	 * Disabling RX should lead to flow control being activated.
	 */
	counter_dev = device_get_binding("TIMER_0");
	zassert_true(counter_dev != NULL, NULL);

	top_cfg.ticks = counter_us_to_ticks(counter_dev, 1000);

	err = counter_set_top_value(counter_dev, &top_cfg);
	zassert_true(err >= 0, NULL);

	err = counter_start(counter_dev);
	zassert_true(err >= 0, NULL);
}

static void rx_isr(void)
{
	uint8_t buf[64];
	int len;

	do {
		len = uart_fifo_read(uart_dev, buf, BUF_SIZE);
		for (int i = 0; i < len; i++) {
			process_byte(buf[i]);
		}
	} while (len);
}<commit_msg>tests: uart_mix_fifo_poll: Convert to use DEVICE_DT_GET<commit_after>/*
 * Copyright (c) 2019 Nordic Semiconductor ASA
 *
 * SPDX-License-Identifier: Apache-2.0
 */

/**
 * @addtogroup t_driver_uart
 * @{
 * @defgroup t_uart_mix_fifo_poll test_uart_mix_fifo_poll
 * @}
 */

#include <zephyr/drivers/uart.h>
#include <ztest.h>
#include <zephyr/drivers/counter.h>
#include <zephyr/random/rand32.h>
/* RX and TX pins have to be connected together*/

#if defined(CONFIG_BOARD_NRF52840DK_NRF52840)
#define UART_DEVICE_DEV DT_NODELABEL(uart0)
#elif defined(CONFIG_BOARD_NRF9160DK_NRF9160)
#define UART_DEVICE_DEV DT_NODELABEL(uart1)
#elif defined(CONFIG_BOARD_ATSAMD21_XPRO)
#define UART_DEVICE_DEV DT_NODELABEL(sercom1)
#elif defined(CONFIG_BOARD_ATSAMR21_XPRO)
#define UART_DEVICE_DEV DT_NODELABEL(sercom3)
#elif defined(CONFIG_BOARD_ATSAME54_XPRO)
#define UART_DEVICE_DEV DT_NODELABEL(sercom1)
#else
#define UART_DEVICE_DEV DT_CHOSEN(zephyr_console)
#endif

struct rx_source {
	int cnt;
	uint8_t prev;
};

#define BUF_SIZE 16

/* Buffer used for polling. */
static uint8_t txbuf[3][BUF_SIZE];

/* Buffer used for async or interrupt driven apis.
 * One of test configurations checks if RO buffer works with the driver.
 */
static IF_ENABLED(TEST_CONST_BUFFER, (const)) uint8_t txbuf3[16] = {
	0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
	0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f
};

struct test_data {
	const uint8_t *buf;
	volatile int cnt;
	int max;
	struct k_sem sem;
};

static struct rx_source source[4];
static struct test_data test_data[3];
static struct test_data int_async_data;

static const struct device *counter_dev;
static const struct device *uart_dev;

static bool async;
static bool int_driven;
static volatile bool async_rx_enabled;
static struct k_sem async_tx_sem;

static void int_driven_callback(const struct device *dev, void *user_data);
static void async_callback(const struct device *dev,
			   struct uart_event *evt, void *user_data);

static void process_byte(uint8_t b)
{
	int base = b >> 4;
	struct rx_source *src = &source[base];
	bool ok;

	b &= 0x0F;
	src->cnt++;

	if (src->cnt == 1) {
		src->prev = b;
		return;
	}

	ok = ((b - src->prev) == 1) || (!b && (src->prev == 0x0F));

	zassert_true(ok, "Unexpected byte received:0x%02x, prev:0x%02x",
			(base << 4) | b, (base << 4) | src->prev);
	src->prev = b;
}

static void counter_top_handler(const struct device *dev, void *user_data)
{
	static bool enable = true;
	static uint8_t async_rx_buf[4];

	if (async && !async_rx_enabled) {
		int err;

		err = uart_rx_enable(uart_dev, async_rx_buf,
				     sizeof(async_rx_buf), 1 * USEC_PER_MSEC);
		zassert_true(err >= 0, NULL);
		async_rx_enabled = true;
	} else if (int_driven) {
		if (enable) {
			uart_irq_rx_enable(uart_dev);
		} else {
			uart_irq_rx_disable(uart_dev);
		}

		enable = !enable;
	} else if (!async && !int_driven) {
		uint8_t c;

		while (uart_poll_in(uart_dev, &c) >= 0) {
			process_byte(c);
		}
	}
}

static void init_test(void)
{
	int err;
	struct counter_top_cfg top_cfg = {
		.callback = counter_top_handler,
		.user_data = NULL,
		.flags = 0
	};

	uart_dev = DEVICE_DT_GET(UART_DEVICE_DEV);
	zassert_true(device_is_ready(uart_dev), "uart device is not ready");

	if (uart_callback_set(uart_dev, async_callback, NULL) == 0) {
		async = true;
	} else {
		async = false;
		int_driven = uart_irq_tx_complete(uart_dev) >= 0;
		if (int_driven) {
			uart_irq_callback_set(uart_dev, int_driven_callback);
		}
	}

	/* Setup counter which will periodically enable/disable UART RX,
	 * Disabling RX should lead to flow control being activated.
	 */
	counter_dev = DEVICE_DT_GET(DT_NODELABEL(timer0));
	zassert_true(device_is_ready(counter_dev), NULL);

	top_cfg.ticks = counter_us_to_ticks(counter_dev, 1000);

	err = counter_set_top_value(counter_dev, &top_cfg);
	zassert_true(err >= 0, NULL);

	err = counter_start(counter_dev);
	zassert_true(err >= 0, NULL);
}

static void rx_isr(void)
{
	uint8_t buf[64];
	int len;

	do {
		len = uart_fifo_read(uart_dev, buf, BUF_SIZE);
		for (int i = 0; i < len; i++) {
			process_byte(buf[i]);
		}
	} while (len);
}
{'fraction_non_alphanumeric': 0.11975021427696829, 'fraction_numerical': 0.031100771397085832, 'mean_word_length': 4.825962910128388, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5034483', 'n_tokens_mistral': 3511, 'n_tokens_neox': 3242, 'n_words': 760}