Skip to content

Commit

Permalink
tests: kernel: Add sys_irq test suites
Browse files Browse the repository at this point in the history
Add test suites to validate the sys_irq API, shared IRQs and
dynamic IRQs.

Signed-off-by: Bjarki Arge Andreasen <bjarki@arge-andreasen.me>
  • Loading branch information
bjarki-andreasen committed Jul 3, 2024
1 parent ce073d8 commit 2ab95ef
Show file tree
Hide file tree
Showing 18 changed files with 562 additions and 0 deletions.
10 changes: 10 additions & 0 deletions tests/kernel/sys_irq/dynamic_irq/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright (c) 2024 Bjarki Arge Andreasen
# SPDX-License-Identifier: Apache-2.0

cmake_minimum_required(VERSION 3.20.0)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(test_sys_irq_dynamic_irq)

target_sources(app PRIVATE
src/irq.c
)
7 changes: 7 additions & 0 deletions tests/kernel/sys_irq/dynamic_irq/boards/rak5010_nrf52840.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Copyright (c) 2024 Nordic Semiconductor
# SPDX-License-Identifier: Apache-2.0

CONFIG_GEN_ISR_TABLES=n
CONFIG_GEN_IRQ_VECTOR_TABLE=n
CONFIG_INTC=y
CONFIG_SYS_IRQ=y
46 changes: 46 additions & 0 deletions tests/kernel/sys_irq/dynamic_irq/boards/rak5010_nrf52840.overlay
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (c) 2024 Nordic Semiconductor
*
* SPDX-License-Identifier: Apache-2.0
*/

/ {
aliases {
test-intd0 = &spi1;
test-intd1 = &spi2;
};
};

&spi1 {
status = "reserved";
};

&spi2 {
status = "reserved";
};

&rtc1 {
status = "reserved";
};

&nvic {
interrupt-lines = <65>;
};

/* Console routed to IO1 and IO2 */
&pinctrl {
uart1_default: uart1_default {
group1 {
psels = <NRF_PSEL(UART_TX, 0, 20)>,
<NRF_PSEL(UART_RX, 0, 19)>;
};
};

uart1_sleep: uart1_sleep {
group1 {
psels = <NRF_PSEL(UART_TX, 0, 20)>,
<NRF_PSEL(UART_RX, 0, 19)>;
low-power-enable;
};
};
};
10 changes: 10 additions & 0 deletions tests/kernel/sys_irq/dynamic_irq/prj.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright (c) 2024 Bjarki Arge Andreasen
# SPDX-License-Identifier: Apache-2.0

CONFIG_ZTEST=y
CONFIG_ZTEST_FAIL_FAST=y
CONFIG_INTC=y
CONFIG_SYS_IRQ=y
CONFIG_SYS_IRQ_DYNAMIC=y
CONFIG_LOG=y
CONFIG_SYS_IRQ_LOG_SPURIOUS=y
121 changes: 121 additions & 0 deletions tests/kernel/sys_irq/dynamic_irq/src/irq.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
* Copyright (c) 2024 Bjarki Arge Andreasen
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <zephyr/sys/irq.h>
#include <zephyr/kernel.h>
#include <zephyr/ztest.h>

#define TEST_IRQ_MAX_DELAY K_MSEC(10)

#define INTD0_NODE DT_ALIAS(test_intd0)
#define INTD1_NODE DT_ALIAS(test_intd1)

#define INTD0_SYS_IRQN SYS_DT_IRQN(INTD0_NODE)
#define INTD1_SYS_IRQN SYS_DT_IRQN(INTD1_NODE)

#define INTD0_IRQ_FLAGS SYS_DT_IRQ_FLAGS(INTD0_NODE)
#define INTD1_IRQ_FLAGS SYS_DT_IRQ_FLAGS(INTD1_NODE)

BUILD_ASSERT(INTD0_SYS_IRQN != INTD1_SYS_IRQN, "INTDs must not share INTL");
BUILD_ASSERT(DT_NODE_HAS_STATUS(INTD0_NODE, reserved), "INTD0 must have status reserved");
BUILD_ASSERT(DT_NODE_HAS_STATUS(INTD1_NODE, reserved), "INTD1 must have status reserved");

static K_SEM_DEFINE(irq0_sem, 0, 1);
static K_SEM_DEFINE(irq1_sem, 0, 1);

static uint32_t irq0_data;
static uint32_t irq1_data;
static struct sys_irq irq0;
static struct sys_irq irq1;

static int irq0_handler(const void *data)
{
zassert_equal(data, &irq0_data);
k_sem_give(&irq0_sem);
return SYS_IRQ_HANDLED;
}

static int irq1_handler(const void *data)
{
zassert_equal(data, &irq1_data);
k_sem_give(&irq1_sem);
return SYS_IRQ_HANDLED;
}

ZTEST(sys_irq_irq, test__enable_then_req_then_trigger__irq)
{
zassert_ok(sys_irq_enable(INTD0_SYS_IRQN));
zassert_ok(sys_irq_enable(INTD1_SYS_IRQN));
zassert_ok(sys_irq_request(INTD0_SYS_IRQN, &irq0, irq0_handler, &irq0_data));
zassert_ok(sys_irq_request(INTD1_SYS_IRQN, &irq1, irq1_handler, &irq1_data));
zassert_ok(sys_irq_trigger(INTD0_SYS_IRQN));
zassert_equal(k_sem_take(&irq0_sem, TEST_IRQ_MAX_DELAY), 0);
zassert_ok(sys_irq_trigger(INTD1_SYS_IRQN));
zassert_equal(k_sem_take(&irq1_sem, TEST_IRQ_MAX_DELAY), 0);
}

ZTEST(sys_irq_irq, test__req_then_trigger__no_irq)
{
zassert_ok(sys_irq_request(INTD0_SYS_IRQN, &irq0, irq0_handler, &irq0_data));
zassert_ok(sys_irq_request(INTD1_SYS_IRQN, &irq1, irq1_handler, &irq1_data));
zassert_ok(sys_irq_trigger(INTD0_SYS_IRQN));
zassert_equal(k_sem_take(&irq0_sem, TEST_IRQ_MAX_DELAY), -EAGAIN);
zassert_ok(sys_irq_trigger(INTD1_SYS_IRQN));
zassert_equal(k_sem_take(&irq1_sem, TEST_IRQ_MAX_DELAY), -EAGAIN);
}

ZTEST(sys_irq_irq, test__trigger_then_req__no_irq)
{
zassert_ok(sys_irq_trigger(INTD0_SYS_IRQN));
zassert_ok(sys_irq_trigger(INTD1_SYS_IRQN));
zassert_ok(sys_irq_request(INTD0_SYS_IRQN, &irq0, irq0_handler, &irq0_data));
zassert_equal(k_sem_take(&irq0_sem, TEST_IRQ_MAX_DELAY), -EAGAIN);
zassert_ok(sys_irq_request(INTD1_SYS_IRQN, &irq1, irq1_handler, &irq1_data));
zassert_equal(k_sem_take(&irq1_sem, TEST_IRQ_MAX_DELAY), -EAGAIN);
}

ZTEST(sys_irq_irq, test__trigger_then_req_then_enable__no_irq)
{
zassert_ok(sys_irq_trigger(INTD0_SYS_IRQN));
zassert_ok(sys_irq_trigger(INTD1_SYS_IRQN));
zassert_ok(sys_irq_request(INTD0_SYS_IRQN, &irq0, irq0_handler, &irq0_data));
zassert_equal(k_sem_take(&irq0_sem, TEST_IRQ_MAX_DELAY), -EAGAIN);
zassert_ok(sys_irq_request(INTD1_SYS_IRQN, &irq1, irq1_handler, &irq1_data));
zassert_equal(k_sem_take(&irq1_sem, TEST_IRQ_MAX_DELAY), -EAGAIN);
zassert_ok(sys_irq_enable(INTD0_SYS_IRQN));
zassert_equal(k_sem_take(&irq0_sem, TEST_IRQ_MAX_DELAY), 0);
zassert_ok(sys_irq_enable(INTD1_SYS_IRQN));
zassert_equal(k_sem_take(&irq1_sem, TEST_IRQ_MAX_DELAY), 0);
}

void *test_init(void)
{
zassert_ok(sys_irq_configure(INTD0_SYS_IRQN, INTD0_IRQ_FLAGS));
zassert_ok(sys_irq_configure(INTD1_SYS_IRQN, INTD1_IRQ_FLAGS));
return NULL;
}

void test_before(void *f)
{
ARG_UNUSED(f);

k_sem_reset(&irq0_sem);
k_sem_reset(&irq1_sem);
}

void test_after(void *f)
{
ARG_UNUSED(f);

zassert(sys_irq_disable(INTD0_SYS_IRQN) > -1, "retval must be positive");
zassert(sys_irq_disable(INTD1_SYS_IRQN) > -1, "retval must be positive");
zassert(sys_irq_clear(INTD0_SYS_IRQN) > -1, "retval must be positive");
zassert(sys_irq_clear(INTD1_SYS_IRQN) > -1, "retval must be positive");
zassert_ok(sys_irq_release(INTD0_SYS_IRQN, &irq0));
zassert_ok(sys_irq_release(INTD1_SYS_IRQN, &irq1));
}

ZTEST_SUITE(sys_irq_irq, NULL, test_init, test_before, test_after, NULL);
4 changes: 4 additions & 0 deletions tests/kernel/sys_irq/dynamic_irq/testcase.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
tests:
kernel.sys_irq.dynamic_irq:
platform_allow:
- rak5010/nrf52840
10 changes: 10 additions & 0 deletions tests/kernel/sys_irq/irq/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright (c) 2024 Bjarki Arge Andreasen
# SPDX-License-Identifier: Apache-2.0

cmake_minimum_required(VERSION 3.20.0)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(test_sys_irq_irq)

target_sources(app PRIVATE
src/irq.c
)
7 changes: 7 additions & 0 deletions tests/kernel/sys_irq/irq/boards/rak5010_nrf52840.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Copyright (c) 2024 Nordic Semiconductor
# SPDX-License-Identifier: Apache-2.0

CONFIG_GEN_ISR_TABLES=n
CONFIG_GEN_IRQ_VECTOR_TABLE=n
CONFIG_INTC=y
CONFIG_SYS_IRQ=y
46 changes: 46 additions & 0 deletions tests/kernel/sys_irq/irq/boards/rak5010_nrf52840.overlay
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (c) 2024 Nordic Semiconductor
*
* SPDX-License-Identifier: Apache-2.0
*/

/ {
aliases {
test-intd0 = &spi1;
test-intd1 = &spi2;
};
};

&spi1 {
status = "reserved";
};

&spi2 {
status = "reserved";
};

&rtc1 {
status = "reserved";
};

&nvic {
interrupt-lines = <65>;
};

/* Console routed to IO1 and IO2 */
&pinctrl {
uart1_default: uart1_default {
group1 {
psels = <NRF_PSEL(UART_TX, 0, 20)>,
<NRF_PSEL(UART_RX, 0, 19)>;
};
};

uart1_sleep: uart1_sleep {
group1 {
psels = <NRF_PSEL(UART_TX, 0, 20)>,
<NRF_PSEL(UART_RX, 0, 19)>;
low-power-enable;
};
};
};
9 changes: 9 additions & 0 deletions tests/kernel/sys_irq/irq/prj.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Copyright (c) 2024 Bjarki Arge Andreasen
# SPDX-License-Identifier: Apache-2.0

CONFIG_ZTEST=y
CONFIG_ZTEST_FAIL_FAST=y
CONFIG_INTC=y
CONFIG_SYS_IRQ=y
CONFIG_LOG=y
CONFIG_SYS_IRQ_LOG_SPURIOUS=y
Loading

0 comments on commit 2ab95ef

Please sign in to comment.