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 2
11 /**TESTPOINT: init via K_FIFO_DEFINE*/
12 K_FIFO_DEFINE(kfifo);
13
14 struct k_fifo fifo;
15 static fdata_t data[LIST_LEN];
16 static fdata_t data_l[LIST_LEN];
17 static fdata_t data_sl[LIST_LEN];
18
19 static K_THREAD_STACK_DEFINE(tstack, STACK_SIZE);
20 static struct k_thread tdata;
21 static struct k_sem end_sema;
22
tfifo_put(struct k_fifo * pfifo)23 static void tfifo_put(struct k_fifo *pfifo)
24 {
25 for (int i = 0; i < LIST_LEN; i++) {
26 /**TESTPOINT: fifo put*/
27 k_fifo_put(pfifo, (void *)&data[i]);
28 }
29
30 /**TESTPOINT: fifo put list*/
31 static fdata_t *head = &data_l[0], *tail = &data_l[LIST_LEN - 1];
32
33 head->snode.next = (sys_snode_t *)tail;
34 tail->snode.next = NULL;
35 k_fifo_put_list(pfifo, (uint32_t *)head, (uint32_t *)tail);
36
37 /**TESTPOINT: fifo put slist*/
38 sys_slist_t slist;
39
40 sys_slist_init(&slist);
41 sys_slist_append(&slist, (sys_snode_t *)&(data_sl[0].snode));
42 sys_slist_append(&slist, (sys_snode_t *)&(data_sl[1].snode));
43 k_fifo_put_slist(pfifo, &slist);
44 }
45
tfifo_get(struct k_fifo * pfifo)46 static void tfifo_get(struct k_fifo *pfifo)
47 {
48 void *rx_data;
49
50 /*get fifo data from "fifo_put"*/
51 for (int i = 0; i < LIST_LEN; i++) {
52 /**TESTPOINT: fifo get*/
53 rx_data = k_fifo_get(pfifo, K_NO_WAIT);
54 zassert_equal(rx_data, (void *)&data[i]);
55 }
56 /*get fifo data from "fifo_put_list"*/
57 for (int i = 0; i < LIST_LEN; i++) {
58 rx_data = k_fifo_get(pfifo, K_NO_WAIT);
59 zassert_equal(rx_data, (void *)&data_l[i]);
60 }
61 /*get fifo data from "fifo_put_slist"*/
62 for (int i = 0; i < LIST_LEN; i++) {
63 rx_data = k_fifo_get(pfifo, K_NO_WAIT);
64 zassert_equal(rx_data, (void *)&data_sl[i]);
65 }
66 }
67
68 /*entry of contexts*/
tIsr_entry_put(const void * p)69 static void tIsr_entry_put(const void *p)
70 {
71 tfifo_put((struct k_fifo *)p);
72 zassert_false(k_fifo_is_empty((struct k_fifo *)p));
73 }
74
tIsr_entry_get(const void * p)75 static void tIsr_entry_get(const void *p)
76 {
77 tfifo_get((struct k_fifo *)p);
78 zassert_true(k_fifo_is_empty((struct k_fifo *)p));
79 }
80
tThread_entry(void * p1,void * p2,void * p3)81 static void tThread_entry(void *p1, void *p2, void *p3)
82 {
83 tfifo_get((struct k_fifo *)p1);
84 k_sem_give(&end_sema);
85 }
86
tfifo_thread_thread(struct k_fifo * pfifo)87 static void tfifo_thread_thread(struct k_fifo *pfifo)
88 {
89 k_sem_init(&end_sema, 0, 1);
90 /**TESTPOINT: thread-thread data passing via fifo*/
91 k_tid_t tid = k_thread_create(&tdata, tstack, STACK_SIZE,
92 tThread_entry, pfifo, NULL, NULL,
93 K_PRIO_PREEMPT(0), 0, K_NO_WAIT);
94 tfifo_put(pfifo);
95 k_sem_take(&end_sema, K_FOREVER);
96 k_thread_abort(tid);
97 }
98
tfifo_thread_isr(struct k_fifo * pfifo)99 static void tfifo_thread_isr(struct k_fifo *pfifo)
100 {
101 k_sem_init(&end_sema, 0, 1);
102 /**TESTPOINT: isr-thread data passing via fifo*/
103 irq_offload(tIsr_entry_put, (const void *)pfifo);
104 tfifo_get(pfifo);
105 }
106
tfifo_isr_thread(struct k_fifo * pfifo)107 static void tfifo_isr_thread(struct k_fifo *pfifo)
108 {
109 k_sem_init(&end_sema, 0, 1);
110 /**TESTPOINT: thread-isr data passing via fifo*/
111 tfifo_put(pfifo);
112 irq_offload(tIsr_entry_get, (const void *)pfifo);
113 }
114
tfifo_is_empty(void * p)115 static void tfifo_is_empty(void *p)
116 {
117 struct k_fifo *pfifo = (struct k_fifo *)p;
118
119 tfifo_put(&fifo);
120 /**TESTPOINT: return false when data available*/
121 zassert_false(k_fifo_is_empty(pfifo));
122
123 tfifo_get(&fifo);
124 /**TESTPOINT: return true with data unavailable*/
125 zassert_true(k_fifo_is_empty(pfifo));
126 }
127
128 /**
129 * @addtogroup kernel_fifo_tests
130 * @{
131 */
132
133 /**
134 * @brief Test thread to thread data passing via fifo
135 * @see k_fifo_init(), k_fifo_put(), k_fifo_get(), k_fifo_put_list()
136 */
ZTEST(fifo_api_1cpu,test_fifo_thread2thread)137 ZTEST(fifo_api_1cpu, test_fifo_thread2thread)
138 {
139 /**TESTPOINT: init via k_fifo_init*/
140 k_fifo_init(&fifo);
141 tfifo_thread_thread(&fifo);
142
143 /**TESTPOINT: test K_FIFO_DEFINEed fifo*/
144 tfifo_thread_thread(&kfifo);
145 }
146
147 /**
148 * @brief Test isr to thread data passing via fifo
149 * @see k_fifo_init(), k_fifo_put(), k_fifo_get()
150 */
ZTEST(fifo_api,test_fifo_thread2isr)151 ZTEST(fifo_api, test_fifo_thread2isr)
152 {
153 /**TESTPOINT: init via k_fifo_init*/
154 k_fifo_init(&fifo);
155 tfifo_thread_isr(&fifo);
156
157 /**TESTPOINT: test K_FIFO_DEFINEed fifo*/
158 tfifo_thread_isr(&kfifo);
159 }
160
161 /**
162 * @brief Test thread to isr data passing via fifo
163 * @see k_fifo_init(), k_fifo_put(), k_fifo_get()
164 */
ZTEST(fifo_api,test_fifo_isr2thread)165 ZTEST(fifo_api, test_fifo_isr2thread)
166 {
167 /**TESTPOINT: test k_fifo_init fifo*/
168 k_fifo_init(&fifo);
169 tfifo_isr_thread(&fifo);
170
171 /**TESTPOINT: test K_FIFO_DEFINE fifo*/
172 tfifo_isr_thread(&kfifo);
173 }
174
175 /**
176 * @brief Test empty fifo
177 * @see k_fifo_init(), k_fifo_is_empty(), k_fifo_put(), k_fifo_get()
178 */
ZTEST(fifo_api,test_fifo_is_empty_thread)179 ZTEST(fifo_api, test_fifo_is_empty_thread)
180 {
181 k_fifo_init(&fifo);
182 /**TESTPOINT: k_fifo_is_empty after init*/
183 zassert_true(k_fifo_is_empty(&fifo));
184
185 /**TESTPONT: check fifo is empty from thread*/
186 tfifo_is_empty(&fifo);
187 }
188
189 /**
190 * @brief Test empty fifo in interrupt context
191 * @see k_fifo_init(), fifo_is_empty(), k_fifo_put(), k_fifo_get()
192 */
ZTEST(fifo_api,test_fifo_is_empty_isr)193 ZTEST(fifo_api, test_fifo_is_empty_isr)
194 {
195 k_fifo_init(&fifo);
196 /**TESTPOINT: check fifo is empty from isr*/
197 irq_offload((irq_offload_routine_t)tfifo_is_empty, &fifo);
198 }
199 /**
200 * @}
201 */
202