|
| 1 | +/* |
| 2 | + * Copyright (c) 2022 Synopsys |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +/* |
| 8 | + * @file |
| 9 | + * @brief complex number multiplication portion of DSP sharing test |
| 10 | + * |
| 11 | + * @ingroup kernel_dspsharing_tests |
| 12 | + * |
| 13 | + * This module is used for the DSP sharing test, and supplements the basic |
| 14 | + * load/store test by incorporating two additional threads that utilize the |
| 15 | + * DSP unit. |
| 16 | + * |
| 17 | + * Testing utilizes a pair of tasks that independently compute complex vector |
| 18 | + * dot product. The lower priority task is regularly preempted by the higher |
| 19 | + * priority task, thereby testing whether DSP context information is properly |
| 20 | + * preserved. |
| 21 | + * |
| 22 | + * A reference value of computed result is computed once at the start of the |
| 23 | + * test. All subsequent computations must produce the same value, otherwise |
| 24 | + * an error has occurred. |
| 25 | + */ |
| 26 | + |
| 27 | +#include <ztest.h> |
| 28 | +#include "fxarc.h" |
| 29 | +#include "dsp_context.h" |
| 30 | +#include "test_common.h" |
| 31 | + |
| 32 | +/* stored in XY memory, need AGU_SHARING */ |
| 33 | +#define DATA_ATTR __xy __attribute__((section(".Xdata"))) |
| 34 | +static DATA_ATTR const cq15_t cq15_a[3] = {{0x20, 10}, {0x10, 20}, {4, 30}}; |
| 35 | +static DATA_ATTR const cq15_t cq15_b[3] = {{0x20, 11}, {0x10, 21}, {5, 31}}; |
| 36 | + |
| 37 | +static volatile short reference_result; |
| 38 | + |
| 39 | +static volatile unsigned int calc_low_count; |
| 40 | +static volatile unsigned int calc_high_count; |
| 41 | + |
| 42 | +/* Indicates that the load/store test exited */ |
| 43 | +static volatile bool test_exited; |
| 44 | + |
| 45 | +/* Semaphore for signaling end of test */ |
| 46 | +static K_SEM_DEFINE(test_exit_sem, 0, 1); |
| 47 | + |
| 48 | +/** |
| 49 | + * @brief Entry point for the low priority compute task |
| 50 | + * |
| 51 | + * @ingroup kernel_dspsharing_tests |
| 52 | + */ |
| 53 | +static void calculate_low(void) |
| 54 | +{ |
| 55 | + volatile short res[2]; |
| 56 | + /* Loop until the test finishes, or an error is detected. */ |
| 57 | + for (calc_low_count = 0; !test_exited; calc_low_count++) { |
| 58 | + |
| 59 | + v2accum32_t acc = {0, 0}; |
| 60 | + |
| 61 | + for (int i = 0; i < 3; i++) { |
| 62 | + acc = fx_v2a32_cmac_cq15(acc, cq15_a[i], cq15_b[i]); |
| 63 | + } |
| 64 | + /* cast reult from v2accum32_ to short type */ |
| 65 | + res[0] = fx_q15_cast_asl_rnd_a32(fx_get_v2a32(acc, 0), 15); |
| 66 | + res[1] = fx_q15_cast_asl_rnd_a32(fx_get_v2a32(acc, 1), 15); |
| 67 | + |
| 68 | + if (reference_result == 0) { |
| 69 | + reference_result = res[0]; |
| 70 | + } else if (reference_result != res[0]) { |
| 71 | + printf("Computed result %d, reference result %d\n", |
| 72 | + res[0], reference_result); |
| 73 | + } |
| 74 | + |
| 75 | + zassert_equal(reference_result, res[0], |
| 76 | + "complex product computation error"); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +/** |
| 81 | + * @brief Entry point for the high priority compute task |
| 82 | + * |
| 83 | + * @ingroup kernel_dspsharing_tests |
| 84 | + */ |
| 85 | +static void calculate_high(void) |
| 86 | +{ |
| 87 | + volatile short res[2]; |
| 88 | + /* Run the test until the specified maximum test count is reached */ |
| 89 | + for (calc_high_count = 0; calc_high_count <= MAX_TESTS; |
| 90 | + calc_high_count++) { |
| 91 | + |
| 92 | + v2accum32_t acc = {0, 0}; |
| 93 | + |
| 94 | + for (int i = 0; i < 3; i++) { |
| 95 | + acc = fx_v2a32_cmac_cq15(acc, cq15_a[i], cq15_b[i]); |
| 96 | + } |
| 97 | + |
| 98 | + /* |
| 99 | + * Relinquish the processor for the remainder of the current |
| 100 | + * system clock tick, so that lower priority threads get a |
| 101 | + * chance to run. |
| 102 | + * |
| 103 | + * This exercises the ability of the kernel to restore the |
| 104 | + * DSP state of a low priority thread _and_ the ability of the |
| 105 | + * kernel to provide a "clean" DSP state to this thread |
| 106 | + * once the sleep ends. |
| 107 | + */ |
| 108 | + k_sleep(K_MSEC(10)); |
| 109 | + |
| 110 | + res[0] = fx_q15_cast_asl_rnd_a32(fx_get_v2a32(acc, 0), 15); |
| 111 | + res[1] = fx_q15_cast_asl_rnd_a32(fx_get_v2a32(acc, 1), 15); |
| 112 | + |
| 113 | + if (reference_result == 0) { |
| 114 | + reference_result = res[0]; |
| 115 | + } else if (reference_result != res[0]) { |
| 116 | + printf("Computed result %d, reference result %d\n", |
| 117 | + res[0], reference_result); |
| 118 | + } |
| 119 | + |
| 120 | + zassert_equal(reference_result, res[0], |
| 121 | + "complex product computation error"); |
| 122 | + |
| 123 | + /* Periodically issue progress report */ |
| 124 | + if ((calc_high_count % 100) == 50) { |
| 125 | + printf("complex product calculation OK after %u (high) " |
| 126 | + "+" |
| 127 | + " %u (low) tests (computed %d)\n", |
| 128 | + calc_high_count, calc_low_count, res[0]); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + /* Signal end of test */ |
| 133 | + test_exited = true; |
| 134 | + k_sem_give(&test_exit_sem); |
| 135 | +} |
| 136 | + |
| 137 | +K_THREAD_DEFINE(cal_low, THREAD_STACK_SIZE, calculate_low, NULL, NULL, NULL, |
| 138 | + THREAD_LOW_PRIORITY, K_DSP_REGS | K_AGU_REGS, K_TICKS_FOREVER); |
| 139 | + |
| 140 | +K_THREAD_DEFINE(cal_high, THREAD_STACK_SIZE, calculate_high, NULL, NULL, NULL, |
| 141 | + THREAD_HIGH_PRIORITY, K_DSP_REGS | K_AGU_REGS, K_TICKS_FOREVER); |
| 142 | + |
| 143 | +void test_calculation(void) |
| 144 | +{ |
| 145 | + /* Initialise test states */ |
| 146 | + test_exited = false; |
| 147 | + k_sem_reset(&test_exit_sem); |
| 148 | + |
| 149 | + /* Start test threads */ |
| 150 | + k_thread_start(cal_low); |
| 151 | + k_thread_start(cal_high); |
| 152 | + |
| 153 | + /* Wait for test threads to exit */ |
| 154 | + k_sem_take(&test_exit_sem, K_FOREVER); |
| 155 | +} |
0 commit comments