Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 | /* * Copyright (c) 2018 Intel Corporation * * SPDX-License-Identifier: Apache-2.0 */ /* * @file * @brief Use stack API's in different scenarios * * This module tests following three basic scenarios: * * Scenario #1 * Test thread enters items into a stack, starts the Child thread and * waits for a semaphore. Child thread extracts all items from the stack * and enters some items back into the stack. Child thread gives the * semaphore for Test thread to continue. Once the control is returned * back to Test thread, it extracts all items from the stack. * * Scenario #2 * Test thread enters an item into stack2, starts a Child thread and * extract an item from stack1 once the item is there. The child thread * will extract an item from stack2 once the item is there and and enter * an item to stack1. The flow of control goes from Test thread to Child * thread and so forth. * * Scenario #3 * Tests the ISR interfaces. Test thread pushes items into stack2 and gives * control to the Child thread. Child thread pops items from stack2 and then * pushes items into stack1. Child thread gives back control to the Test thread * and Test thread pops the items from stack1. * All the Push and Pop operations happen in ISR Context. */ /** * @brief Tests for Kernel stack objects * @defgroup kernel_stack_tests Stacks * @ingroup all_tests * @{ * @} */ #include <ztest.h> #include <irq_offload.h> #define TSTACK_SIZE (512 + CONFIG_TEST_EXTRA_STACKSIZE) #define STACK_LEN 4 /* stack objects used in this test */ K_STACK_DEFINE(stack1, STACK_LEN); K_STACK_DEFINE(stack2, STACK_LEN); /* thread info * */ K_THREAD_STACK_DEFINE(threadstack, TSTACK_SIZE); struct k_thread thread_data; /* Data pushed to stack */ static ZTEST_DMEM stack_data_t data1[STACK_LEN] = { 0xAAAA, 0xBBBB, 0xCCCC, 0xDDDD }; static ZTEST_DMEM stack_data_t data2[STACK_LEN] = { 0x1111, 0x2222, 0x3333, 0x4444 }; static ZTEST_DMEM stack_data_t data_isr[STACK_LEN] = { 0xABCD, 0xABCD, 0xABCD, 0xABCD }; /* semaphore to sync threads */ static struct k_sem end_sema; K_HEAP_DEFINE(test_pool, 128 * 3); extern struct k_stack kstack; extern struct k_stack stack; extern struct k_sem end_sema; extern void test_stack_thread2thread(void); extern void test_stack_thread2isr(void); extern void test_stack_pop_fail(void); extern void test_stack_alloc_thread2thread(void); extern void test_stack_pop_can_wait(void); extern void test_stack_cleanup_error(void); extern void test_stack_push_full(void); #ifdef CONFIG_USERSPACE extern void test_stack_user_thread2thread(void); extern void test_stack_user_pop_fail(void); extern void test_stack_user_init_null(void); extern void test_stack_user_init_invalid_value(void); extern void test_stack_user_push_null(void); extern void test_stack_user_pop_null(void); extern void test_stack_user_pop_permission(void); #else #define dummy_test(_name) \ static void _name(void) \ { \ ztest_test_skip(); \ } dummy_test(test_stack_user_thread2thread); dummy_test(test_stack_user_pop_fail); dummy_test(test_stack_user_init_null); dummy_test(test_stack_user_init_invalid_value); dummy_test(test_stack_user_push_null); dummy_test(test_stack_user_pop_null); dummy_test(test_stack_user_pop_permission); #endif /* CONFIG_USERSPACE */ /* entry of contexts */ static void tIsr_entry_push(const void *p) { uint32_t i; /* Push items to stack */ for (i = 0U; i < STACK_LEN; i++) { k_stack_push((struct k_stack *)p, data_isr[i]); } } static void tIsr_entry_pop(const void *p) { uint32_t i; /* Pop items from stack */ for (i = 0U; i < STACK_LEN; i++) { if (p == &stack1) { k_stack_pop((struct k_stack *)p, &data1[i], K_NO_WAIT); } else { k_stack_pop((struct k_stack *)p, &data2[i], K_NO_WAIT); } } } static void thread_entry_fn_single(void *p1, void *p2, void *p3) { stack_data_t tmp[STACK_LEN]; uint32_t i; /* Pop items from stack */ for (i = STACK_LEN; i; i--) { k_stack_pop((struct k_stack *)p1, &tmp[i - 1], K_NO_WAIT); } zassert_false(memcmp(tmp, data1, sizeof(tmp)), "Push & Pop items does not match"); /* Push items from stack */ for (i = 0U; i < STACK_LEN; i++) { k_stack_push((struct k_stack *)p1, data2[i]); } /* Give control back to Test thread */ k_sem_give(&end_sema); } static void thread_entry_fn_dual(void *p1, void *p2, void *p3) { stack_data_t tmp[STACK_LEN]; uint32_t i; for (i = 0U; i < STACK_LEN; i++) { /* Pop items from stack2 */ k_stack_pop(p2, &tmp[i], K_FOREVER); /* Push items to stack1 */ k_stack_push(p1, data1[i]); } zassert_false(memcmp(tmp, data2, sizeof(tmp)), "Push & Pop items does not match"); } static void thread_entry_fn_isr(void *p1, void *p2, void *p3) { /* Pop items from stack2 */ irq_offload(tIsr_entry_pop, (const void *)p2); zassert_false(memcmp(data_isr, data2, sizeof(data_isr)), "Push & Pop items does not match"); /* Push items to stack1 */ irq_offload(tIsr_entry_push, (const void *)p1); /* Give control back to Test thread */ k_sem_give(&end_sema); } /** * @addtogroup kernel_stack_tests * @{ */ /** * @brief Verify data passing between threads using single stack * @see k_stack_push(), #K_STACK_DEFINE(x), k_stack_pop() */ static void test_single_stack_play(void) { stack_data_t tmp[STACK_LEN]; uint32_t i; /* Init kernel objects */ k_sem_init(&end_sema, 0, 1); /* Push items to stack */ for (i = 0U; i < STACK_LEN; i++) { k_stack_push(&stack1, data1[i]); } k_tid_t tid = k_thread_create(&thread_data, threadstack, TSTACK_SIZE, thread_entry_fn_single, &stack1, NULL, NULL, K_PRIO_PREEMPT(0), K_USER | K_INHERIT_PERMS, K_NO_WAIT); /* Let the child thread run */ k_sem_take(&end_sema, K_FOREVER); /* Pop items from stack */ for (i = STACK_LEN; i; i--) { k_stack_pop(&stack1, &tmp[i - 1], K_NO_WAIT); } zassert_false(memcmp(tmp, data2, sizeof(tmp)), "Push & Pop items does not match"); /* Clear the spawn thread to avoid side effect */ k_thread_abort(tid); } /** * @brief Verify data passing between threads using dual stack * @see k_stack_push(), #K_STACK_DEFINE(x), k_stack_pop() */ static void test_dual_stack_play(void) { stack_data_t tmp[STACK_LEN]; uint32_t i; k_tid_t tid = k_thread_create(&thread_data, threadstack, TSTACK_SIZE, thread_entry_fn_dual, &stack1, &stack2, NULL, K_PRIO_PREEMPT(0), K_USER | K_INHERIT_PERMS, K_NO_WAIT); for (i = 0U; i < STACK_LEN; i++) { /* Push items to stack2 */ k_stack_push(&stack2, data2[i]); /* Pop items from stack1 */ k_stack_pop(&stack1, &tmp[i], K_FOREVER); } zassert_false(memcmp(tmp, data1, sizeof(tmp)), "Push & Pop items does not match"); /* Clear the spawn thread to avoid side effect */ k_thread_abort(tid); } /** * @brief Verify data passing between thread and ISR * @see k_stack_push(), #K_STACK_DEFINE(x), k_stack_pop() */ static void test_isr_stack_play(void) { /* Init kernel objects */ k_sem_init(&end_sema, 0, 1); k_tid_t tid = k_thread_create(&thread_data, threadstack, TSTACK_SIZE, thread_entry_fn_isr, &stack1, &stack2, NULL, K_PRIO_PREEMPT(0), K_INHERIT_PERMS, K_NO_WAIT); /* Push items to stack2 */ irq_offload(tIsr_entry_push, (const void *)&stack2); /* Let the child thread run */ k_sem_take(&end_sema, K_FOREVER); /* Pop items from stack1 */ irq_offload(tIsr_entry_pop, (const void *)&stack1); zassert_false(memcmp(data_isr, data1, sizeof(data_isr)), "Push & Pop items does not match"); /* Clear the spawn thread to avoid side effect */ k_thread_abort(tid); } /* the thread entry */ void thread_entry_wait(void *p1, void *p2, void *p3) { stack_data_t *txdata = p3; k_stack_push(p1, *(txdata + 2)); k_stack_push(p1, *(txdata + 3)); } /** * @brief Test that the stack pop can be waited * if no item availablle * * @details Create and initialize a new stack * Set two timeout parameters to indicate * the maximum amount of time the thread will wait. * * @ingroup kernel_stack_tests * * @see k_stack_push(), #K_STACK_DEFINE(x), k_stack_pop() */ void test_stack_pop_can_wait(void) { struct k_stack stack3; stack_data_t tx_data[STACK_LEN] = { 0xaa, 0xbb, 0xcc, 0xdd }; stack_data_t rx_data[STACK_LEN] = { 0 }; k_stack_alloc_init(&stack3, 2); k_tid_t tid = k_thread_create(&thread_data, threadstack, TSTACK_SIZE, thread_entry_wait, &stack3, NULL, tx_data, K_PRIO_PREEMPT(0), 0, K_NO_WAIT); for (int i = 0; i < 2; i++) { k_stack_push(&stack3, tx_data[i]); } for (int i = 0; i < 3; i++) { k_stack_pop(&stack3, &rx_data[i], K_FOREVER); } zassert_true(rx_data[2] == tx_data[2], "wait foreve and pop failed\n"); k_stack_pop(&stack3, &rx_data[3], K_MSEC(50)); zassert_true(rx_data[3] == tx_data[3], "Wait maxmum time pop failed\n"); /* Clear the spawn thread to avoid side effect */ k_thread_abort(tid); /*free the buffer allocated*/ k_stack_cleanup(&stack3); } /** * @} */ extern struct k_stack threadstack1; extern struct k_thread thread_data1; extern struct k_sem end_sema1; /*test case main entry*/ void test_main(void) { k_thread_access_grant(k_current_get(), &stack1, &stack2, &thread_data, &end_sema, &threadstack, &kstack, &stack, &thread_data1, &end_sema1, &threadstack1); k_thread_heap_assign(k_current_get(), &test_pool); ztest_test_suite(test_stack_usage, ztest_unit_test(test_stack_thread2thread), ztest_user_unit_test(test_stack_user_thread2thread), ztest_unit_test(test_stack_thread2isr), ztest_unit_test(test_stack_pop_fail), ztest_unit_test(test_stack_cleanup_error), ztest_unit_test(test_stack_push_full), ztest_user_unit_test(test_stack_user_pop_fail), ztest_user_unit_test(test_stack_user_init_null), ztest_user_unit_test(test_stack_user_init_invalid_value), ztest_user_unit_test(test_stack_user_push_null), ztest_user_unit_test(test_stack_user_pop_null), ztest_user_unit_test(test_stack_user_pop_permission), ztest_unit_test(test_stack_alloc_thread2thread), ztest_user_unit_test(test_single_stack_play), ztest_1cpu_user_unit_test(test_dual_stack_play), ztest_1cpu_unit_test(test_isr_stack_play), ztest_unit_test(test_stack_pop_can_wait)); ztest_run_test_suite(test_stack_usage); } |