A metronome using the FRDM K64F board

Committer:
ram54288
Date:
Sun May 14 18:40:18 2017 +0000
Revision:
0:a7a43371b306
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ram54288 0:a7a43371b306 1 /*
ram54288 0:a7a43371b306 2 * Copyright (c) 2016 ARM Limited. All rights reserved.
ram54288 0:a7a43371b306 3 * SPDX-License-Identifier: Apache-2.0
ram54288 0:a7a43371b306 4 * Licensed under the Apache License, Version 2.0 (the License); you may
ram54288 0:a7a43371b306 5 * not use this file except in compliance with the License.
ram54288 0:a7a43371b306 6 * You may obtain a copy of the License at
ram54288 0:a7a43371b306 7 *
ram54288 0:a7a43371b306 8 * http://www.apache.org/licenses/LICENSE-2.0
ram54288 0:a7a43371b306 9 *
ram54288 0:a7a43371b306 10 * Unless required by applicable law or agreed to in writing, software
ram54288 0:a7a43371b306 11 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
ram54288 0:a7a43371b306 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ram54288 0:a7a43371b306 13 * See the License for the specific language governing permissions and
ram54288 0:a7a43371b306 14 * limitations under the License.
ram54288 0:a7a43371b306 15 */
ram54288 0:a7a43371b306 16
ram54288 0:a7a43371b306 17 #include "pal_rtos_test_utils.h"
ram54288 0:a7a43371b306 18 #include "pal_rtos.h"
ram54288 0:a7a43371b306 19 #include "unity_fixture.h"
ram54288 0:a7a43371b306 20
ram54288 0:a7a43371b306 21 #include "pal.h"
ram54288 0:a7a43371b306 22
ram54288 0:a7a43371b306 23 threadsArgument_t threadsArg;
ram54288 0:a7a43371b306 24 timerArgument_t timerArgs;
ram54288 0:a7a43371b306 25
ram54288 0:a7a43371b306 26 void palThreadFunc1(void const *argument)
ram54288 0:a7a43371b306 27 {
ram54288 0:a7a43371b306 28 palThreadID_t threadID = 10;
ram54288 0:a7a43371b306 29 uint32_t* threadStorage = NULL;
ram54288 0:a7a43371b306 30 threadsArgument_t *tmp = (threadsArgument_t*)argument;
ram54288 0:a7a43371b306 31 #ifdef MUTEX_UNITY_TEST
ram54288 0:a7a43371b306 32 palStatus_t status = PAL_SUCCESS;
ram54288 0:a7a43371b306 33 TEST_PRINTF("palThreadFunc1::before mutex\n");
ram54288 0:a7a43371b306 34 status = pal_osMutexWait(mutex1, 100);
ram54288 0:a7a43371b306 35 TEST_PRINTF("palThreadFunc1::after mutex: 0x%08x\n", status);
ram54288 0:a7a43371b306 36 TEST_PRINTF("palThreadFunc1::after mutex (expected): 0x%08x\n", PAL_ERR_RTOS_TIMEOUT);
ram54288 0:a7a43371b306 37 TEST_ASSERT_EQUAL(PAL_ERR_RTOS_TIMEOUT, status);
ram54288 0:a7a43371b306 38 return; // for Mutex scenario, this should end here
ram54288 0:a7a43371b306 39 #endif //MUTEX_UNITY_TEST
ram54288 0:a7a43371b306 40
ram54288 0:a7a43371b306 41 tmp->arg1 = 10;
ram54288 0:a7a43371b306 42
ram54288 0:a7a43371b306 43 threadID = pal_osThreadGetId();
ram54288 0:a7a43371b306 44 TEST_PRINTF("palThreadFunc1::Thread ID is %d\n", threadID);
ram54288 0:a7a43371b306 45
ram54288 0:a7a43371b306 46 threadStorage = pal_osThreadGetLocalStore();
ram54288 0:a7a43371b306 47 if (threadStorage == g_threadStorage)
ram54288 0:a7a43371b306 48 {
ram54288 0:a7a43371b306 49 TEST_PRINTF("Thread storage updated as expected\n");
ram54288 0:a7a43371b306 50 }
ram54288 0:a7a43371b306 51 TEST_ASSERT_EQUAL(threadStorage, g_threadStorage);
ram54288 0:a7a43371b306 52 #ifdef MUTEX_UNITY_TEST
ram54288 0:a7a43371b306 53 status = pal_osMutexRelease(mutex1);
ram54288 0:a7a43371b306 54 TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
ram54288 0:a7a43371b306 55 #endif //MUTEX_UNITY_TEST
ram54288 0:a7a43371b306 56 TEST_PRINTF("palThreadFunc1::STAAAAM\n");
ram54288 0:a7a43371b306 57
ram54288 0:a7a43371b306 58 }
ram54288 0:a7a43371b306 59
ram54288 0:a7a43371b306 60 void palThreadFunc2(void const *argument)
ram54288 0:a7a43371b306 61 {
ram54288 0:a7a43371b306 62
ram54288 0:a7a43371b306 63 palThreadID_t threadID = 10;
ram54288 0:a7a43371b306 64 threadsArgument_t *tmp = (threadsArgument_t*)argument;
ram54288 0:a7a43371b306 65 #ifdef MUTEX_UNITY_TEST
ram54288 0:a7a43371b306 66 palStatus_t status = PAL_SUCCESS;
ram54288 0:a7a43371b306 67 TEST_PRINTF("palThreadFunc2::before mutex\n");
ram54288 0:a7a43371b306 68 status = pal_osMutexWait(mutex2, 300);
ram54288 0:a7a43371b306 69 TEST_PRINTF("palThreadFunc2::after mutex: 0x%08x\n", status);
ram54288 0:a7a43371b306 70 TEST_PRINTF("palThreadFunc2::after mutex (expected): 0x%08x\n", PAL_SUCCESS);
ram54288 0:a7a43371b306 71 TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
ram54288 0:a7a43371b306 72 #endif //MUTEX_UNITY_TEST
ram54288 0:a7a43371b306 73
ram54288 0:a7a43371b306 74 tmp->arg2 = 20;
ram54288 0:a7a43371b306 75
ram54288 0:a7a43371b306 76 threadID = pal_osThreadGetId();
ram54288 0:a7a43371b306 77 TEST_PRINTF("palThreadFunc2::Thread ID is %d\n", threadID);
ram54288 0:a7a43371b306 78 #ifdef MUTEX_UNITY_TEST
ram54288 0:a7a43371b306 79 status = pal_osMutexRelease(mutex2);
ram54288 0:a7a43371b306 80 TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
ram54288 0:a7a43371b306 81 #endif //MUTEX_UNITY_TEST
ram54288 0:a7a43371b306 82 TEST_PRINTF("palThreadFunc2::STAAAAM\n");
ram54288 0:a7a43371b306 83 }
ram54288 0:a7a43371b306 84
ram54288 0:a7a43371b306 85 void palThreadFunc3(void const *argument)
ram54288 0:a7a43371b306 86 {
ram54288 0:a7a43371b306 87
ram54288 0:a7a43371b306 88 palThreadID_t threadID = 10;
ram54288 0:a7a43371b306 89 threadsArgument_t *tmp = (threadsArgument_t*)argument;
ram54288 0:a7a43371b306 90
ram54288 0:a7a43371b306 91 #ifdef SEMAPHORE_UNITY_TEST
ram54288 0:a7a43371b306 92 palStatus_t status = PAL_SUCCESS;
ram54288 0:a7a43371b306 93 uint32_t semaphoresAvailable = 10;
ram54288 0:a7a43371b306 94 status = pal_osSemaphoreWait(semaphore1, 200, &semaphoresAvailable);
ram54288 0:a7a43371b306 95
ram54288 0:a7a43371b306 96 if (PAL_SUCCESS == status)
ram54288 0:a7a43371b306 97 {
ram54288 0:a7a43371b306 98 TEST_PRINTF("palThreadFunc3::semaphoresAvailable: %d\n", semaphoresAvailable);
ram54288 0:a7a43371b306 99 TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
ram54288 0:a7a43371b306 100 }
ram54288 0:a7a43371b306 101 else if(PAL_ERR_RTOS_TIMEOUT == status)
ram54288 0:a7a43371b306 102 {
ram54288 0:a7a43371b306 103 TEST_PRINTF("palThreadFunc3::semaphoresAvailable: %d\n", semaphoresAvailable);
ram54288 0:a7a43371b306 104 TEST_PRINTF("palThreadFunc3::status: 0x%08x\n", status);
ram54288 0:a7a43371b306 105 TEST_PRINTF("palThreadFunc3::failed to get Semaphore as expected\n", status);
ram54288 0:a7a43371b306 106 TEST_ASSERT_EQUAL(PAL_ERR_RTOS_TIMEOUT, status);
ram54288 0:a7a43371b306 107 return;
ram54288 0:a7a43371b306 108 }
ram54288 0:a7a43371b306 109 pal_osDelay(6000);
ram54288 0:a7a43371b306 110 #endif //SEMAPHORE_UNITY_TEST
ram54288 0:a7a43371b306 111 tmp->arg3 = 30;
ram54288 0:a7a43371b306 112 threadID = pal_osThreadGetId();
ram54288 0:a7a43371b306 113 TEST_PRINTF("palThreadFunc3::Thread ID is %d\n", threadID);
ram54288 0:a7a43371b306 114
ram54288 0:a7a43371b306 115 #ifdef SEMAPHORE_UNITY_TEST
ram54288 0:a7a43371b306 116 status = pal_osSemaphoreRelease(semaphore1);
ram54288 0:a7a43371b306 117 TEST_PRINTF("palThreadFunc3::pal_osSemaphoreRelease res: 0x%08x\n", status);
ram54288 0:a7a43371b306 118 TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
ram54288 0:a7a43371b306 119 #endif //SEMAPHORE_UNITY_TEST
ram54288 0:a7a43371b306 120 TEST_PRINTF("palThreadFunc3::STAAAAM\n");
ram54288 0:a7a43371b306 121 }
ram54288 0:a7a43371b306 122
ram54288 0:a7a43371b306 123 void palThreadFunc4(void const *argument)
ram54288 0:a7a43371b306 124 {
ram54288 0:a7a43371b306 125 palThreadID_t threadID = 10;
ram54288 0:a7a43371b306 126 threadsArgument_t *tmp = (threadsArgument_t*)argument;
ram54288 0:a7a43371b306 127 #ifdef MUTEX_UNITY_TEST
ram54288 0:a7a43371b306 128 palStatus_t status = PAL_SUCCESS;
ram54288 0:a7a43371b306 129 TEST_PRINTF("palThreadFunc4::before mutex\n");
ram54288 0:a7a43371b306 130 status = pal_osMutexWait(mutex1, 200);
ram54288 0:a7a43371b306 131 TEST_PRINTF("palThreadFunc4::after mutex: 0x%08x\n", status);
ram54288 0:a7a43371b306 132 TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
ram54288 0:a7a43371b306 133 pal_osDelay(3500); //wait 3.5 seconds to make sure that the next thread arrive to this point
ram54288 0:a7a43371b306 134 #endif //MUTEX_UNITY_TEST
ram54288 0:a7a43371b306 135
ram54288 0:a7a43371b306 136
ram54288 0:a7a43371b306 137 tmp->arg4 = 40;
ram54288 0:a7a43371b306 138
ram54288 0:a7a43371b306 139 threadID = pal_osThreadGetId();
ram54288 0:a7a43371b306 140 TEST_PRINTF("Thread ID is %d\n", threadID);
ram54288 0:a7a43371b306 141
ram54288 0:a7a43371b306 142
ram54288 0:a7a43371b306 143
ram54288 0:a7a43371b306 144 #ifdef MUTEX_UNITY_TEST
ram54288 0:a7a43371b306 145 status = pal_osMutexRelease(mutex1);
ram54288 0:a7a43371b306 146 TEST_PRINTF("palThreadFunc4::after release mutex: 0x%08x\n", status);
ram54288 0:a7a43371b306 147 TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
ram54288 0:a7a43371b306 148 #endif //MUTEX_UNITY_TEST
ram54288 0:a7a43371b306 149 TEST_PRINTF("palThreadFunc4::STAAAAM\n");
ram54288 0:a7a43371b306 150 }
ram54288 0:a7a43371b306 151
ram54288 0:a7a43371b306 152 void palThreadFunc5(void const *argument)
ram54288 0:a7a43371b306 153 {
ram54288 0:a7a43371b306 154 palThreadID_t threadID = 10;
ram54288 0:a7a43371b306 155 threadsArgument_t *tmp = (threadsArgument_t*)argument;
ram54288 0:a7a43371b306 156 #ifdef MUTEX_UNITY_TEST
ram54288 0:a7a43371b306 157 palStatus_t status = PAL_SUCCESS;
ram54288 0:a7a43371b306 158 TEST_PRINTF("palThreadFunc5::before mutex\n");
ram54288 0:a7a43371b306 159 status = pal_osMutexWait(mutex1, 4500);
ram54288 0:a7a43371b306 160 TEST_PRINTF("palThreadFunc5::after mutex: 0x%08x\n", status);
ram54288 0:a7a43371b306 161 TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
ram54288 0:a7a43371b306 162 #endif //MUTEX_UNITY_TEST
ram54288 0:a7a43371b306 163 tmp->arg5 = 50;
ram54288 0:a7a43371b306 164
ram54288 0:a7a43371b306 165 threadID = pal_osThreadGetId();
ram54288 0:a7a43371b306 166 TEST_PRINTF("Thread ID is %d\n", threadID);
ram54288 0:a7a43371b306 167 #ifdef MUTEX_UNITY_TEST
ram54288 0:a7a43371b306 168 status = pal_osMutexRelease(mutex1);
ram54288 0:a7a43371b306 169 TEST_PRINTF("palThreadFunc5::after release mutex: 0x%08x\n", status);
ram54288 0:a7a43371b306 170 TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
ram54288 0:a7a43371b306 171 #endif //MUTEX_UNITY_TEST
ram54288 0:a7a43371b306 172 TEST_PRINTF("palThreadFunc5::STAAAAM\n");
ram54288 0:a7a43371b306 173 }
ram54288 0:a7a43371b306 174
ram54288 0:a7a43371b306 175 void palThreadFunc6(void const *argument)
ram54288 0:a7a43371b306 176 {
ram54288 0:a7a43371b306 177 palThreadID_t threadID = 10;
ram54288 0:a7a43371b306 178 threadsArgument_t *tmp = (threadsArgument_t*)argument;
ram54288 0:a7a43371b306 179 #ifdef SEMAPHORE_UNITY_TEST
ram54288 0:a7a43371b306 180 palStatus_t status = PAL_SUCCESS;
ram54288 0:a7a43371b306 181 uint32_t semaphoresAvailable = 10;
ram54288 0:a7a43371b306 182 status = pal_osSemaphoreWait(123456, 200, &semaphoresAvailable); //MUST fail, since there is no semaphore with ID=3
ram54288 0:a7a43371b306 183 TEST_PRINTF("palThreadFunc6::semaphoresAvailable: %d\n", semaphoresAvailable);
ram54288 0:a7a43371b306 184 TEST_ASSERT_EQUAL(PAL_ERR_RTOS_PARAMETER, status);
ram54288 0:a7a43371b306 185 return;
ram54288 0:a7a43371b306 186 #endif //SEMAPHORE_UNITY_TEST
ram54288 0:a7a43371b306 187 tmp->arg6 = 60;
ram54288 0:a7a43371b306 188
ram54288 0:a7a43371b306 189 threadID = pal_osThreadGetId();
ram54288 0:a7a43371b306 190 TEST_PRINTF("Thread ID is %d\n", threadID);
ram54288 0:a7a43371b306 191 #ifdef SEMAPHORE_UNITY_TEST
ram54288 0:a7a43371b306 192 status = pal_osSemaphoreRelease(123456);
ram54288 0:a7a43371b306 193 TEST_PRINTF("palThreadFunc6::pal_osSemaphoreRelease res: 0x%08x\n", status);
ram54288 0:a7a43371b306 194 TEST_ASSERT_EQUAL(PAL_ERR_RTOS_PARAMETER, status);
ram54288 0:a7a43371b306 195 #endif //SEMAPHORE_UNITY_TEST
ram54288 0:a7a43371b306 196 TEST_PRINTF("palThreadFunc6::STAAAAM\n");
ram54288 0:a7a43371b306 197 }
ram54288 0:a7a43371b306 198
ram54288 0:a7a43371b306 199
ram54288 0:a7a43371b306 200 void palTimerFunc1(void const *argument)
ram54288 0:a7a43371b306 201 {
ram54288 0:a7a43371b306 202 g_timerArgs.ticksInFunc1 = pal_osKernelSysTick();
ram54288 0:a7a43371b306 203 TEST_PRINTF("ticks in palTimerFunc1: 0 - %d\n", g_timerArgs.ticksInFunc1);
ram54288 0:a7a43371b306 204 TEST_PRINTF("Once Timer function was called\n");
ram54288 0:a7a43371b306 205 }
ram54288 0:a7a43371b306 206
ram54288 0:a7a43371b306 207 void palTimerFunc2(void const *argument)
ram54288 0:a7a43371b306 208 {
ram54288 0:a7a43371b306 209 g_timerArgs.ticksInFunc2 = pal_osKernelSysTick();
ram54288 0:a7a43371b306 210 TEST_PRINTF("ticks in palTimerFunc2: 0 - %d\n", g_timerArgs.ticksInFunc2);
ram54288 0:a7a43371b306 211 TEST_PRINTF("Periodic Timer function was called\n");
ram54288 0:a7a43371b306 212 }
ram54288 0:a7a43371b306 213
ram54288 0:a7a43371b306 214 void palThreadFuncCustom1(void const *argument)
ram54288 0:a7a43371b306 215 {
ram54288 0:a7a43371b306 216 TEST_PRINTF("palThreadFuncCustom1 was called\n");
ram54288 0:a7a43371b306 217 }
ram54288 0:a7a43371b306 218
ram54288 0:a7a43371b306 219 void palThreadFuncCustom2(void const *argument)
ram54288 0:a7a43371b306 220 {
ram54288 0:a7a43371b306 221 TEST_PRINTF("palThreadFuncCustom2 was called\n");
ram54288 0:a7a43371b306 222 }
ram54288 0:a7a43371b306 223
ram54288 0:a7a43371b306 224 void palThreadFuncCustom3(void const *argument)
ram54288 0:a7a43371b306 225 {
ram54288 0:a7a43371b306 226 TEST_PRINTF("palThreadFuncCustom3 was called\n");
ram54288 0:a7a43371b306 227 }
ram54288 0:a7a43371b306 228
ram54288 0:a7a43371b306 229 void palThreadFuncCustom4(void const *argument)
ram54288 0:a7a43371b306 230 {
ram54288 0:a7a43371b306 231 TEST_PRINTF("palThreadFuncCustom4 was called\n");
ram54288 0:a7a43371b306 232 }
ram54288 0:a7a43371b306 233
ram54288 0:a7a43371b306 234 void palRunThreads()
ram54288 0:a7a43371b306 235 {
ram54288 0:a7a43371b306 236 palStatus_t status = PAL_SUCCESS;
ram54288 0:a7a43371b306 237 palThreadID_t threadID1 = NULLPTR;
ram54288 0:a7a43371b306 238 palThreadID_t threadID2 = NULLPTR;
ram54288 0:a7a43371b306 239 palThreadID_t threadID3 = NULLPTR;
ram54288 0:a7a43371b306 240 palThreadID_t threadID4 = NULLPTR;
ram54288 0:a7a43371b306 241 palThreadID_t threadID5 = NULLPTR;
ram54288 0:a7a43371b306 242 palThreadID_t threadID6 = NULLPTR;
ram54288 0:a7a43371b306 243
ram54288 0:a7a43371b306 244 uint32_t *stack1 = malloc(THREAD_STACK_SIZE);
ram54288 0:a7a43371b306 245 uint32_t *stack2 = malloc(THREAD_STACK_SIZE);
ram54288 0:a7a43371b306 246 uint32_t *stack3 = malloc(THREAD_STACK_SIZE);
ram54288 0:a7a43371b306 247 uint32_t *stack4 = malloc(THREAD_STACK_SIZE);
ram54288 0:a7a43371b306 248 uint32_t *stack5 = malloc(THREAD_STACK_SIZE);
ram54288 0:a7a43371b306 249 uint32_t *stack6 = malloc(THREAD_STACK_SIZE);
ram54288 0:a7a43371b306 250
ram54288 0:a7a43371b306 251 status = pal_init(NULL);
ram54288 0:a7a43371b306 252 TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
ram54288 0:a7a43371b306 253
ram54288 0:a7a43371b306 254 status = pal_osThreadCreate(palThreadFunc1, &g_threadsArg, PAL_osPriorityIdle, THREAD_STACK_SIZE, stack1, (palThreadLocalStore_t *)g_threadStorage, &threadID1);
ram54288 0:a7a43371b306 255 TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
ram54288 0:a7a43371b306 256
ram54288 0:a7a43371b306 257 status = pal_osThreadCreate(palThreadFunc2, &g_threadsArg, PAL_osPriorityLow, THREAD_STACK_SIZE, stack2, NULL, &threadID2);
ram54288 0:a7a43371b306 258 TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
ram54288 0:a7a43371b306 259
ram54288 0:a7a43371b306 260 status = pal_osThreadCreate(palThreadFunc3, &g_threadsArg, PAL_osPriorityNormal, THREAD_STACK_SIZE, stack3, NULL, &threadID3);
ram54288 0:a7a43371b306 261 TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
ram54288 0:a7a43371b306 262
ram54288 0:a7a43371b306 263 status = pal_osThreadCreate(palThreadFunc4, &g_threadsArg, PAL_osPriorityBelowNormal, THREAD_STACK_SIZE, stack4, NULL, &threadID4);
ram54288 0:a7a43371b306 264 TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
ram54288 0:a7a43371b306 265
ram54288 0:a7a43371b306 266 status = pal_osThreadCreate(palThreadFunc5, &g_threadsArg, PAL_osPriorityAboveNormal, THREAD_STACK_SIZE, stack5, NULL, &threadID5);
ram54288 0:a7a43371b306 267 TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
ram54288 0:a7a43371b306 268
ram54288 0:a7a43371b306 269 status = pal_osThreadCreate(palThreadFunc6, &g_threadsArg, PAL_osPriorityHigh, THREAD_STACK_SIZE, stack6, NULL, &threadID6);
ram54288 0:a7a43371b306 270 TEST_ASSERT_EQUAL(PAL_SUCCESS, status);
ram54288 0:a7a43371b306 271
ram54288 0:a7a43371b306 272
ram54288 0:a7a43371b306 273 free(stack1);
ram54288 0:a7a43371b306 274 free(stack2);
ram54288 0:a7a43371b306 275 free(stack3);
ram54288 0:a7a43371b306 276 free(stack4);
ram54288 0:a7a43371b306 277 free(stack5);
ram54288 0:a7a43371b306 278 free(stack6);
ram54288 0:a7a43371b306 279 }