1 /*
2  * Copyright (c) 2016 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include "test_lifo.h"
8 
9 #define STACK_SIZE (512 + CONFIG_TEST_EXTRA_STACK_SIZE)
10 #define LIST_LEN 4
11 #define LOOPS 32
12 
13 static ldata_t data[LIST_LEN];
14 static struct k_lifo lifo;
15 static K_THREAD_STACK_DEFINE(tstack, STACK_SIZE);
16 static struct k_thread tdata;
17 static struct k_sem end_sema;
18 
tlifo_put(struct k_lifo * plifo)19 static void tlifo_put(struct k_lifo *plifo)
20 {
21 	for (int i = 0; i < LIST_LEN; i++) {
22 		/**TESTPOINT: lifo put*/
23 		k_lifo_put(plifo, (void *)&data[i]);
24 	}
25 }
26 
tlifo_get(struct k_lifo * plifo)27 static void tlifo_get(struct k_lifo *plifo)
28 {
29 	void *rx_data;
30 
31 	/*get lifo data*/
32 	for (int i = LIST_LEN-1; i >= 0; i--) {
33 		/**TESTPOINT: lifo get*/
34 		rx_data = k_lifo_get(plifo, K_FOREVER);
35 		zassert_equal(rx_data, (void *)&data[i]);
36 	}
37 }
38 
39 /*entry of contexts*/
tIsr_entry(const void * p)40 static void tIsr_entry(const void *p)
41 {
42 	TC_PRINT("isr lifo get\n");
43 	tlifo_get((struct k_lifo *)p);
44 	TC_PRINT("isr lifo put ---> ");
45 	tlifo_put((struct k_lifo *)p);
46 }
47 
tThread_entry(void * p1,void * p2,void * p3)48 static void tThread_entry(void *p1, void *p2, void *p3)
49 {
50 	TC_PRINT("thread lifo get\n");
51 	tlifo_get((struct k_lifo *)p1);
52 	k_sem_give(&end_sema);
53 	TC_PRINT("thread lifo put ---> ");
54 	tlifo_put((struct k_lifo *)p1);
55 	k_sem_give(&end_sema);
56 }
57 
58 /* lifo read write job */
tlifo_read_write(struct k_lifo * plifo)59 static void tlifo_read_write(struct k_lifo *plifo)
60 {
61 	k_sem_init(&end_sema, 0, 1);
62 	/**TESTPOINT: thread-isr-thread data passing via lifo*/
63 	k_tid_t tid = k_thread_create(&tdata, tstack, STACK_SIZE,
64 		tThread_entry, plifo, NULL, NULL,
65 		K_PRIO_PREEMPT(0), 0, K_NO_WAIT);
66 
67 	TC_PRINT("main lifo put ---> ");
68 	tlifo_put(plifo);
69 	irq_offload(tIsr_entry, (const void *)plifo);
70 	k_sem_take(&end_sema, K_FOREVER);
71 	k_sem_take(&end_sema, K_FOREVER);
72 
73 	TC_PRINT("main lifo get\n");
74 	tlifo_get(plifo);
75 	k_thread_abort(tid);
76 	TC_PRINT("\n");
77 }
78 
79 /**
80  * @addtogroup kernel_lifo_tests
81  * @{
82  */
83 
84 /**
85  * @brief Verify zephyr lifo continuous read write in loop
86  *
87  * @details
88  * - Test Steps
89  *   -# lifo put from main thread
90  *   -# lifo read from isr
91  *   -# lifo put from isr
92  *   -# lifo get from spawn thread
93  *   -# loop above steps for LOOPs times
94  * - Expected Results
95  *   -# lifo data pass correctly and stably across contexts
96  *
97  * @see k_lifo_init(), k_fifo_put(), k_fifo_get()
98  */
ZTEST(lifo_loop,test_lifo_loop)99 ZTEST(lifo_loop, test_lifo_loop)
100 {
101 	k_lifo_init(&lifo);
102 	for (int i = 0; i < LOOPS; i++) {
103 		TC_PRINT("* Pass data by lifo in loop %d\n", i);
104 		tlifo_read_write(&lifo);
105 	}
106 }
107 
108 /**
109  * @}
110  */
111 
112 ZTEST_SUITE(lifo_loop, NULL, NULL,
113 		ztest_simple_1cpu_before, ztest_simple_1cpu_after, NULL);
114