1 /*
2  * Copyright (c) 2016 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include "test_fifo.h"
8 
9 #define STACK_SIZE (512 + CONFIG_TEST_EXTRA_STACK_SIZE)
10 #define LIST_LEN 4
11 #define LOOPS 32
12 
13 static fdata_t data[LIST_LEN];
14 static struct k_fifo fifo;
15 static K_THREAD_STACK_DEFINE(tstack, STACK_SIZE);
16 static struct k_thread tdata;
17 static struct k_sem end_sema;
18 
tfifo_put(struct k_fifo * pfifo)19 static void tfifo_put(struct k_fifo *pfifo)
20 {
21 	/**TESTPOINT: fifo put*/
22 	for (int i = 0; i < LIST_LEN; i++) {
23 		k_fifo_put(pfifo, (void *)&data[i]);
24 	}
25 }
26 
tfifo_get(struct k_fifo * pfifo)27 static void tfifo_get(struct k_fifo *pfifo)
28 {
29 	void *rx_data;
30 
31 	/*get fifo data from "fifo_put"*/
32 	for (int i = 0; i < LIST_LEN; i++) {
33 		/**TESTPOINT: fifo get*/
34 		rx_data = k_fifo_get(pfifo, K_NO_WAIT);
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 fifo get\n");
43 	tfifo_get((struct k_fifo *)p);
44 	TC_PRINT("isr fifo put ---> ");
45 	tfifo_put((struct k_fifo *)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 fifo get\n");
51 	tfifo_get((struct k_fifo *)p1);
52 	k_sem_give(&end_sema);
53 	TC_PRINT("thread fifo put ---> ");
54 	tfifo_put((struct k_fifo *)p1);
55 	k_sem_give(&end_sema);
56 }
57 
58 /* fifo read write job */
tfifo_read_write(struct k_fifo * pfifo)59 static void tfifo_read_write(struct k_fifo *pfifo)
60 {
61 	k_sem_init(&end_sema, 0, 1);
62 	/**TESTPOINT: thread-isr-thread data passing via fifo*/
63 	k_tid_t tid = k_thread_create(&tdata, tstack, STACK_SIZE,
64 				      tThread_entry, pfifo, NULL, NULL,
65 				      K_PRIO_PREEMPT(0), 0, K_NO_WAIT);
66 
67 	TC_PRINT("main fifo put ---> ");
68 	tfifo_put(pfifo);
69 	irq_offload(tIsr_entry, pfifo);
70 	k_sem_take(&end_sema, K_FOREVER);
71 	k_sem_take(&end_sema, K_FOREVER);
72 
73 	TC_PRINT("main fifo get\n");
74 	tfifo_get(pfifo);
75 	k_thread_abort(tid);
76 	TC_PRINT("\n");
77 }
78 
79 /**
80  * @addtogroup kernel_fifo_tests
81  * @{
82  */
83 
84 /**
85  * @brief Verify zephyr fifo continuous read write in loop
86  *
87  * @details
88  * - Test Steps
89  *   -# fifo put from main thread
90  *   -# fifo read from isr
91  *   -# fifo put from isr
92  *   -# fifo get from spawn thread
93  *   -# loop above steps for LOOPs times
94  * - Expected Results
95  *   -# fifo data pass correctly and stably across contexts
96  *
97  * @see k_fifo_init(), k_fifo_put(), k_fifo_get()
98  */
ZTEST(fifo_api_1cpu,test_fifo_loop)99 ZTEST(fifo_api_1cpu, test_fifo_loop)
100 {
101 	k_fifo_init(&fifo);
102 	for (int i = 0; i < LOOPS; i++) {
103 		TC_PRINT("* Pass data by fifo in loop %d\n", i);
104 		tfifo_read_write(&fifo);
105 	}
106 }
107 /**
108  * @}
109  */
110