Zephyr Project API 3.7.0
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
main.c File Reference
#include <stdlib.h>
#include <zephyr/ztest.h>
#include <zephyr/kernel_structs.h>
#include <zephyr/arch/cpu.h>
#include <zephyr/irq_offload.h>
#include <zephyr/sys_clock.h>

Data Structures

struct  ISR_INFO
 
struct  timeout_order
 

Macros

#define THREAD_STACKSIZE   (512 + CONFIG_TEST_EXTRA_STACK_SIZE)
 
#define THREAD_STACKSIZE2   (384 + CONFIG_TEST_EXTRA_STACK_SIZE)
 
#define THREAD_PRIORITY   4
 
#define THREAD_SELF_CMD   0
 
#define EXEC_CTX_TYPE_CMD   1
 
#define UNKNOWN_COMMAND   -1
 
#define INVALID_BEHAVIOUR   -2
 
#define TICK_IRQ   (z_sys_timer_irq_for_test)
 
#define HAS_POWERSAVE_INSTRUCTION
 
#define NUM_TIMEOUT_THREADS   ARRAY_SIZE(timeouts)
 

Typedefs

typedef int(* disable_int_func) (int)
 
typedef void(* enable_int_func) (int)
 

Functions

static K_THREAD_STACK_DEFINE (thread_stack1,(512+CONFIG_TEST_EXTRA_STACK_SIZE))
 
static K_THREAD_STACK_DEFINE (thread_stack2,(512+CONFIG_TEST_EXTRA_STACK_SIZE))
 
static K_THREAD_STACK_DEFINE (thread_stack3,(512+CONFIG_TEST_EXTRA_STACK_SIZE))
 
static void isr_handler (const void *data)
 Handler to perform various actions from within an ISR context.
 
static void isr_handler_trigger (void)
 
static void kernel_init_objects (void)
 Initialize kernel objects.
 
int irq_lock_wrapper (int unused)
 A wrapper for irq_lock()
 
void irq_unlock_wrapper (int imask)
 A wrapper for irq_unlock()
 
int irq_disable_wrapper (int irq)
 A wrapper for irq_disable()
 
void irq_enable_wrapper (int irq)
 A wrapper for irq_enable()
 
 ZTEST (context_cpu_idle, test_cpu_idle_atomic)
 Test cpu idle function.
 
 ZTEST (context_cpu_idle, test_cpu_idle)
 Test cpu idle function.
 
 ZTEST (context, test_interrupts)
 Test routines for disabling and enabling interrupts.
 
 ZTEST (context_one_cpu, test_timer_interrupts)
 Test routines for disabling and enabling interrupts (disable timer)
 
 ZTEST (context, test_ctx_thread)
 Test some context routines.
 
static void thread_helper (void *arg1, void *arg2, void *arg3)
 Entry point to the thread's helper.
 
static void k_yield_entry (void *arg0, void *arg1, void *arg2)
 Entry point to thread started by another thread.
 
static void kernel_thread_entry (void *_thread_id, void *arg1, void *arg2)
 
static K_THREAD_STACK_ARRAY_DEFINE (timeout_stacks, ARRAY_SIZE(timeouts),(384+CONFIG_TEST_EXTRA_STACK_SIZE))
 
static void busy_wait_thread (void *mseconds, void *arg2, void *arg3)
 
static void thread_sleep (void *delta, void *arg2, void *arg3)
 
static void delayed_thread (void *num, void *arg2, void *arg3)
 
 ZTEST (context_one_cpu, test_busy_wait)
 Test timeouts.
 
 ZTEST (context_one_cpu, test_k_sleep)
 Test timeouts.
 
 ZTEST (context_one_cpu, test_k_yield)
 Test the k_yield() routine.
 
 ZTEST (context_one_cpu, test_thread)
 Test kernel thread creation.
 
static void * context_setup (void)
 
 ZTEST_SUITE (context_cpu_idle, NULL, context_setup, NULL, NULL, NULL)
 
 ZTEST_SUITE (context, NULL, context_setup, NULL, NULL, NULL)
 
 ZTEST_SUITE (context_one_cpu, NULL, context_setup, ztest_simple_1cpu_before, ztest_simple_1cpu_after, NULL)
 

Variables

static struct k_sem sem_thread
 
static struct k_timer timer
 
static struct k_sem reply_timeout
 
struct k_fifo timeout_order_fifo
 
static int thread_evidence
 
static struct k_thread thread_data1
 
static struct k_thread thread_data2
 
static struct k_thread thread_data3
 
static ISR_INFO isr_info
 
struct timeout_order timeouts []
 
static struct k_thread timeout_threads [ARRAY_SIZE(timeouts)]
 

Macro Definition Documentation

◆ EXEC_CTX_TYPE_CMD

#define EXEC_CTX_TYPE_CMD   1

◆ HAS_POWERSAVE_INSTRUCTION

#define HAS_POWERSAVE_INSTRUCTION

◆ INVALID_BEHAVIOUR

#define INVALID_BEHAVIOUR   -2

◆ NUM_TIMEOUT_THREADS

#define NUM_TIMEOUT_THREADS   ARRAY_SIZE(timeouts)

◆ THREAD_PRIORITY

#define THREAD_PRIORITY   4

◆ THREAD_SELF_CMD

#define THREAD_SELF_CMD   0

◆ THREAD_STACKSIZE

#define THREAD_STACKSIZE   (512 + CONFIG_TEST_EXTRA_STACK_SIZE)

◆ THREAD_STACKSIZE2

#define THREAD_STACKSIZE2   (384 + CONFIG_TEST_EXTRA_STACK_SIZE)

◆ TICK_IRQ

#define TICK_IRQ   (z_sys_timer_irq_for_test)

◆ UNKNOWN_COMMAND

#define UNKNOWN_COMMAND   -1

Typedef Documentation

◆ disable_int_func

typedef int(* disable_int_func) (int)

◆ enable_int_func

typedef void(* enable_int_func) (int)

Function Documentation

◆ busy_wait_thread()

static void busy_wait_thread ( void *  mseconds,
void *  arg2,
void *  arg3 
)
static

◆ context_setup()

static void * context_setup ( void  )
static

◆ delayed_thread()

static void delayed_thread ( void *  num,
void *  arg2,
void *  arg3 
)
static

◆ irq_disable_wrapper()

int irq_disable_wrapper ( int  irq)

A wrapper for irq_disable()

Returns
irq

◆ irq_enable_wrapper()

void irq_enable_wrapper ( int  irq)

A wrapper for irq_enable()

◆ irq_lock_wrapper()

int irq_lock_wrapper ( int  unused)

A wrapper for irq_lock()

Returns
irq_lock() return value

◆ irq_unlock_wrapper()

void irq_unlock_wrapper ( int  imask)

A wrapper for irq_unlock()

◆ isr_handler()

static void isr_handler ( const void *  data)
static

Handler to perform various actions from within an ISR context.

This routine is the ISR handler for isr_handler_trigger(). It performs the command requested in <isr_info.command>.

◆ isr_handler_trigger()

static void isr_handler_trigger ( void  )
static

◆ K_THREAD_STACK_ARRAY_DEFINE()

static K_THREAD_STACK_ARRAY_DEFINE ( timeout_stacks  ,
ARRAY_SIZE(timeouts ,
(384+CONFIG_TEST_EXTRA_STACK_SIZE)   
)
static

◆ K_THREAD_STACK_DEFINE() [1/3]

static K_THREAD_STACK_DEFINE ( thread_stack1  ,
(512+CONFIG_TEST_EXTRA_STACK_SIZE)   
)
static

◆ K_THREAD_STACK_DEFINE() [2/3]

static K_THREAD_STACK_DEFINE ( thread_stack2  ,
(512+CONFIG_TEST_EXTRA_STACK_SIZE)   
)
static

◆ K_THREAD_STACK_DEFINE() [3/3]

static K_THREAD_STACK_DEFINE ( thread_stack3  ,
(512+CONFIG_TEST_EXTRA_STACK_SIZE)   
)
static

◆ k_yield_entry()

static void k_yield_entry ( void *  arg0,
void *  arg1,
void *  arg2 
)
static

Entry point to thread started by another thread.

This routine is the entry point to the thread started by the thread.

◆ kernel_init_objects()

static void kernel_init_objects ( void  )
static

Initialize kernel objects.

This routine initializes the kernel objects used in this module's tests.

◆ kernel_thread_entry()

static void kernel_thread_entry ( void *  _thread_id,
void *  arg1,
void *  arg2 
)
static

◆ thread_helper()

static void thread_helper ( void *  arg1,
void *  arg2,
void *  arg3 
)
static

Entry point to the thread's helper.

This routine is the entry point to the thread's helper thread. It is used to help test the behavior of the k_yield() routine.

Parameters
arg1unused
arg2unused
arg3unused

◆ thread_sleep()

static void thread_sleep ( void *  delta,
void *  arg2,
void *  arg3 
)
static

◆ ZTEST() [1/9]

ZTEST ( context  ,
test_ctx_thread   
)

Test some context routines.

Test Objective:

  • Thread context handles derived from context switches must be able to be restored upon interrupt exit

Testing techniques

  • Functional and black box testing
  • Interface testing

Prerequisite Condition:

  • N/A

Input Specifications:

  • N/A

Test Procedure:

  1. Set priority of current thread to 0 as a preemptible thread
  2. Trap to interrupt context, get thread id of the interrupted thread and pass back to that thread.
  3. Return to thread context and make sure this context is interrupted by comparing its thread ID and the thread ID passed by isr.
  4. Pass command to isr to check whether the isr is executed in interrupt context
  5. When return to thread context, check the return value of command.

Expected Test Result:

  • Thread context restored upon interrupt exit

Pass/Fail criteria:

  • Success if context of thread restored correctly, failure otherwise.

Assumptions and Constraints

  • N/A
See also
k_current_get(), k_is_in_isr()

◆ ZTEST() [2/9]

ZTEST ( context  ,
test_interrupts   
)

Test routines for disabling and enabling interrupts.

Test Objective:

  • To verify kernel architecture layer shall provide a mechanism to selectively disable and enable specific numeric interrupts.
  • This routine tests the routines for disabling and enabling interrupts. These include irq_lock() and irq_unlock().

Testing techniques:

  • Interface testing, function and black box testing, dynamic analysis and testing

Prerequisite Conditions:

  • CONFIG_TICKLESS_KERNEL is not set.

Input Specifications:

  • N/A

Test Procedure:

  1. Do action to align to a tick boundary.
  2. Left shift 4 bits for the value of counts.
  3. Call irq_lock() and restore its return value to imask.
  4. Call sys_clock_tick_get_32() and store its return value to tick.
  5. Repeat counts of calling sys_clock_tick_get_32().
  6. Call sys_clock_tick_get_32() and store its return value to tick2.
  7. Call irq_unlock() with parameter imask.
  8. Check if tick is equal to tick2.
  9. Repeat counts of calling sys_clock_tick_get_32().
  10. Call sys_clock_tick_get_32() and store its return value to tick2.
  11. Check if tick is NOT equal to tick2.

Expected Test Result:

  • The ticks shall not increase while interrupt locked.

Pass/Fail Criteria:

  • Successful if check points in test procedure are all passed, otherwise failure.

Assumptions and Constraints:

  • N/A
See also
irq_lock(), irq_unlock()

◆ ZTEST() [3/9]

ZTEST ( context_cpu_idle  ,
test_cpu_idle   
)

Test cpu idle function.

Test Objective:

  • The kernel architecture provide an idle function to be run when the system has no work for the current CPU
  • This routine tests the k_cpu_idle() routine

Testing techniques

  • Functional and black box testing
  • Interface testing

Prerequisite Condition:

  • HAS_POWERSAVE_INSTRUCTION is set

Input Specifications:

  • N/A

Test Procedure:

  1. Record system time before cpu enters idle state
  2. Enter cpu idle state by k_cpu_idle()
  3. Record system time after cpu idle state is interrupted
  4. Compare the two system time values.

Expected Test Result:

  • cpu enters idle state for a given time

Pass/Fail criteria:

  • Success if the cpu enters idle state, failure otherwise.

Assumptions and Constraints

  • N/A
See also
k_cpu_idle()

◆ ZTEST() [4/9]

ZTEST ( context_cpu_idle  ,
test_cpu_idle_atomic   
)

Test cpu idle function.

Test Objective:

  • The kernel architecture provide an idle function to be run when the system has no work for the current CPU
  • This routine tests the k_cpu_atomic_idle() routine

Testing techniques

  • Functional and black box testing
  • Interface testing

Prerequisite Condition:

  • HAS_POWERSAVE_INSTRUCTION is set

Input Specifications:

  • N/A

Test Procedure:

  1. Record system time before cpu enters idle state
  2. Enter cpu idle state by k_cpu_atomic_idle()
  3. Record system time after cpu idle state is interrupted
  4. Compare the two system time values.

Expected Test Result:

  • cpu enters idle state for a given time

Pass/Fail criteria:

  • Success if the cpu enters idle state, failure otherwise.

Assumptions and Constraints

  • N/A
See also
k_cpu_atomic_idle()

◆ ZTEST() [5/9]

ZTEST ( context_one_cpu  ,
test_busy_wait   
)

Test timeouts.

See also
k_busy_wait(), k_sleep()

◆ ZTEST() [6/9]

ZTEST ( context_one_cpu  ,
test_k_sleep   
)

Test timeouts.

See also
k_sleep()

◆ ZTEST() [7/9]

ZTEST ( context_one_cpu  ,
test_k_yield   
)

Test the k_yield() routine.

Tests the k_yield() routine. It starts another thread (thus also testing k_thread_create()) and checks that behavior of k_yield() against the a higher priority thread, a lower priority thread, and another thread of equal priority.

See also
k_yield()

◆ ZTEST() [8/9]

ZTEST ( context_one_cpu  ,
test_thread   
)

Test kernel thread creation.

See also
k_thread_create

◆ ZTEST() [9/9]

ZTEST ( context_one_cpu  ,
test_timer_interrupts   
)

Test routines for disabling and enabling interrupts (disable timer)

Test Objective:

  • To verify the kernel architecture layer shall provide a mechanism to simultaneously mask all local CPU interrupts and return the previous mask state for restoration.
  • This routine tests the routines for disabling and enabling interrupts. These include irq_disable() and irq_enable().

Testing techniques:

  • Interface testing, function and black box testing, dynamic analysis and testing

Prerequisite Conditions:

  • TICK_IRQ is defined.

Input Specifications:

  • N/A

Test Procedure:

  1. Do action to align to a tick boundary.
  2. Left shift 4 bit for the value of counts.
  3. Call irq_disable() and restore its return value to imask.
  4. Call sys_clock_tick_get_32() and store its return value to tick.
  5. Repeat counts of calling sys_clock_tick_get_32().
  6. Call sys_clock_tick_get_32() and store its return value to tick2.
  7. Call irq_enable() with parameter imask.
  8. Check if tick is equal to tick2.
  9. Repeat counts of calling sys_clock_tick_get_32().
  10. Call sys_clock_tick_get_32() and store its return value to tick2.
  11. Check if tick is NOT equal to tick2.

Expected Test Result:

  • The ticks shall not increase while interrupt locked.

Pass/Fail Criteria:

  • Successful if check points in test procedure are all passed, otherwise failure.

Assumptions and Constraints:

  • Note that this test works by disabling the timer interrupt directly, without any interaction with the timer driver or timeout subsystem. NOT ALL ARCHITECTURES will latch and deliver a timer interrupt that arrives while the interrupt is disabled, which means that the timeout list will become corrupted (because it contains items that should have expired in the past). Any use of kernel timeouts after completion of this test is disallowed. RUN THIS TEST LAST IN THE SUITE.
See also
irq_disable(), irq_enable()

◆ ZTEST_SUITE() [1/3]

ZTEST_SUITE ( context  ,
NULL  ,
context_setup  ,
NULL  ,
NULL  ,
NULL   
)

◆ ZTEST_SUITE() [2/3]

ZTEST_SUITE ( context_cpu_idle  ,
NULL  ,
context_setup  ,
NULL  ,
NULL  ,
NULL   
)

◆ ZTEST_SUITE() [3/3]

ZTEST_SUITE ( context_one_cpu  ,
NULL  ,
context_setup  ,
ztest_simple_1cpu_before  ,
ztest_simple_1cpu_after  ,
NULL   
)

Variable Documentation

◆ isr_info

ISR_INFO isr_info
static

◆ reply_timeout

struct k_sem reply_timeout
static

◆ sem_thread

struct k_sem sem_thread
static

◆ thread_data1

struct k_thread thread_data1
static

◆ thread_data2

struct k_thread thread_data2
static

◆ thread_data3

struct k_thread thread_data3
static

◆ thread_evidence

int thread_evidence
static

◆ timeout_order_fifo

struct k_fifo timeout_order_fifo

◆ timeout_threads

struct k_thread timeout_threads[ARRAY_SIZE(timeouts)]
static

◆ timeouts

struct timeout_order timeouts[]
Initial value:
= {
{ 0, 1000, 2, 0 },
{ 0, 1500, 4, 1 },
{ 0, 500, 0, 2 },
{ 0, 750, 1, 3 },
{ 0, 1750, 5, 4 },
{ 0, 2000, 6, 5 },
{ 0, 1250, 3, 6 },
}

◆ timer

struct k_timer timer
static