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 | /* * 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. */ #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 u32_t data1[STACK_LEN] = { 0xAAAA, 0xBBBB, 0xCCCC, 0xDDDD }; static ZTEST_DMEM u32_t data2[STACK_LEN] = { 0x1111, 0x2222, 0x3333, 0x4444 }; static ZTEST_DMEM u32_t data_isr[STACK_LEN] = { 0xABCD, 0xABCD, 0xABCD, 0xABCD }; /* semaphore to sync threads */ static struct k_sem end_sema; /* entry of contexts */ static void tIsr_entry_push(void *p) { u32_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(void *p) { u32_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) { u32_t tmp[STACK_LEN]; u32_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, STACK_LEN), "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) { u32_t tmp[STACK_LEN]; u32_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, STACK_LEN), "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, p2); zassert_false(memcmp(data_isr, data2, STACK_LEN), "Push & Pop items does not match"); /* Push items to stack1 */ irq_offload(tIsr_entry_push, 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) { u32_t tmp[STACK_LEN]; u32_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, 0); /* 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, STACK_LEN), "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) { u32_t tmp[STACK_LEN]; u32_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, 0); 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, STACK_LEN), "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, 0); /* Push items to stack2 */ irq_offload(tIsr_entry_push, &stack2); /* Let the child thread run */ k_sem_take(&end_sema, K_FOREVER); /* Pop items from stack1 */ irq_offload(tIsr_entry_pop, &stack1); zassert_false(memcmp(data_isr, data1, STACK_LEN), "Push & Pop items does not match"); /* Clear the spawn thread to avoid side effect */ k_thread_abort(tid); } /** * @} */ /*test case main entry*/ void test_main(void) { k_thread_access_grant(k_current_get(), &stack1, &stack2, &thread_data, &end_sema, &threadstack); ztest_test_suite(test_stack_usage, ztest_user_unit_test(test_single_stack_play), ztest_user_unit_test(test_dual_stack_play), ztest_unit_test(test_isr_stack_play)); ztest_run_test_suite(test_stack_usage); } |