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
ram54288 0:a7a43371b306 18 #include "pal_rtos.h"
ram54288 0:a7a43371b306 19 #include "pal_plat_rtos.h"
ram54288 0:a7a43371b306 20
ram54288 0:a7a43371b306 21 #if PAL_UNIQUE_THREAD_PRIORITY
ram54288 0:a7a43371b306 22 //! Threads priorities array.
ram54288 0:a7a43371b306 23 uint8_t g_palThreadPriorities[PAL_MAX_NUMBER_OF_THREADS] = {0};
ram54288 0:a7a43371b306 24 #endif //PAL_UNIQUE_THREAD_PRIORITY
ram54288 0:a7a43371b306 25
ram54288 0:a7a43371b306 26 void pal_osReboot(void)
ram54288 0:a7a43371b306 27 {
ram54288 0:a7a43371b306 28 pal_plat_osReboot();
ram54288 0:a7a43371b306 29 }
ram54288 0:a7a43371b306 30
ram54288 0:a7a43371b306 31 uint32_t pal_osKernelSysTick(void)
ram54288 0:a7a43371b306 32 {
ram54288 0:a7a43371b306 33 uint32_t result;
ram54288 0:a7a43371b306 34 result = pal_plat_osKernelSysTick();
ram54288 0:a7a43371b306 35 return result;
ram54288 0:a7a43371b306 36 }
ram54288 0:a7a43371b306 37
ram54288 0:a7a43371b306 38
ram54288 0:a7a43371b306 39 uint64_t pal_osKernelSysTick64(void)
ram54288 0:a7a43371b306 40 {
ram54288 0:a7a43371b306 41
ram54288 0:a7a43371b306 42 #if PAL_RTOS_64BIT_TICK_SUPPORTED
ram54288 0:a7a43371b306 43 uint64_t result;
ram54288 0:a7a43371b306 44 result = pal_plat_osKernelSysTick64();
ram54288 0:a7a43371b306 45 return result;
ram54288 0:a7a43371b306 46 #else
ram54288 0:a7a43371b306 47 static uint64_t lastValue = 0;
ram54288 0:a7a43371b306 48 static uint64_t wraparoundsDetected = 0;
ram54288 0:a7a43371b306 49 const uint64_t one = 1;
ram54288 0:a7a43371b306 50 uint64_t tmp = pal_plat_osKernelSysTick() + (wraparoundsDetected << 32);
ram54288 0:a7a43371b306 51 if (tmp < lastValue) //erez's "wraparound algorithm" if we detect a wrap around add 1 to the higher 32 bits
ram54288 0:a7a43371b306 52 {
ram54288 0:a7a43371b306 53 tmp = tmp + (one << 32);
ram54288 0:a7a43371b306 54 wraparoundsDetected++;
ram54288 0:a7a43371b306 55 }
ram54288 0:a7a43371b306 56 lastValue = tmp;
ram54288 0:a7a43371b306 57 return (uint64_t)tmp;
ram54288 0:a7a43371b306 58 #endif
ram54288 0:a7a43371b306 59 }
ram54288 0:a7a43371b306 60
ram54288 0:a7a43371b306 61 uint64_t pal_osKernelSysTickMicroSec(uint64_t microseconds)
ram54288 0:a7a43371b306 62 {
ram54288 0:a7a43371b306 63 uint64_t result;
ram54288 0:a7a43371b306 64 result = pal_plat_osKernelSysTickMicroSec(microseconds);
ram54288 0:a7a43371b306 65 return result;
ram54288 0:a7a43371b306 66 }
ram54288 0:a7a43371b306 67
ram54288 0:a7a43371b306 68 uint64_t pal_osKernelSysMilliSecTick(uint64_t sysTicks)
ram54288 0:a7a43371b306 69 {
ram54288 0:a7a43371b306 70 uint64_t result;
ram54288 0:a7a43371b306 71 result = pal_plat_osKernelSysMilliSecTick(sysTicks);
ram54288 0:a7a43371b306 72 return result;
ram54288 0:a7a43371b306 73 }
ram54288 0:a7a43371b306 74
ram54288 0:a7a43371b306 75 uint64_t pal_osKernelSysTickFrequency(void)
ram54288 0:a7a43371b306 76 {
ram54288 0:a7a43371b306 77 uint64_t result;
ram54288 0:a7a43371b306 78 result = pal_plat_osKernelSysTickFrequency();
ram54288 0:a7a43371b306 79 return result;
ram54288 0:a7a43371b306 80 }
ram54288 0:a7a43371b306 81
ram54288 0:a7a43371b306 82 palStatus_t pal_osThreadCreate(palThreadFuncPtr function, void* funcArgument, palThreadPriority_t priority, uint32_t stackSize, uint32_t* stackPtr, palThreadLocalStore_t* store, palThreadID_t* threadID)
ram54288 0:a7a43371b306 83 {
ram54288 0:a7a43371b306 84 palStatus_t status = PAL_SUCCESS;
ram54288 0:a7a43371b306 85
ram54288 0:a7a43371b306 86 #if PAL_UNIQUE_THREAD_PRIORITY
ram54288 0:a7a43371b306 87 //! check if the priority have been used by other thread before
ram54288 0:a7a43371b306 88 if(PAL_osPriorityError == priority)
ram54288 0:a7a43371b306 89 {
ram54288 0:a7a43371b306 90 status = PAL_ERR_INVALID_ARGUMENT;
ram54288 0:a7a43371b306 91 }
ram54288 0:a7a43371b306 92
ram54288 0:a7a43371b306 93 if ((PAL_SUCCESS == status) && (g_palThreadPriorities[priority+PRIORYT_INDEX_OFFSET]))
ram54288 0:a7a43371b306 94 {
ram54288 0:a7a43371b306 95 *threadID = NULLPTR;
ram54288 0:a7a43371b306 96 status = PAL_ERR_RTOS_PRIORITY;
ram54288 0:a7a43371b306 97 }
ram54288 0:a7a43371b306 98 #endif //PAL_IGNORE_UNIQUE_THREAD_PRIORITY
ram54288 0:a7a43371b306 99
ram54288 0:a7a43371b306 100 if (PAL_SUCCESS == status)
ram54288 0:a7a43371b306 101 {
ram54288 0:a7a43371b306 102 status = pal_plat_osThreadCreate(function, funcArgument, priority, stackSize, stackPtr, store, threadID);
ram54288 0:a7a43371b306 103 }
ram54288 0:a7a43371b306 104 return status;
ram54288 0:a7a43371b306 105 }
ram54288 0:a7a43371b306 106
ram54288 0:a7a43371b306 107 palStatus_t pal_osThreadTerminate(palThreadID_t* threadID)
ram54288 0:a7a43371b306 108 {
ram54288 0:a7a43371b306 109 palStatus_t status;
ram54288 0:a7a43371b306 110 status = pal_plat_osThreadTerminate(threadID);
ram54288 0:a7a43371b306 111 return status;
ram54288 0:a7a43371b306 112 }
ram54288 0:a7a43371b306 113
ram54288 0:a7a43371b306 114 palThreadID_t pal_osThreadGetId(void)
ram54288 0:a7a43371b306 115 {
ram54288 0:a7a43371b306 116 palThreadID_t result;
ram54288 0:a7a43371b306 117 result = pal_plat_osThreadGetId();
ram54288 0:a7a43371b306 118 return result;
ram54288 0:a7a43371b306 119 }
ram54288 0:a7a43371b306 120
ram54288 0:a7a43371b306 121 void* pal_osThreadGetLocalStore(void)
ram54288 0:a7a43371b306 122 {
ram54288 0:a7a43371b306 123 void* result;
ram54288 0:a7a43371b306 124 result = pal_plat_osThreadGetLocalStore();
ram54288 0:a7a43371b306 125 return result;
ram54288 0:a7a43371b306 126 }
ram54288 0:a7a43371b306 127
ram54288 0:a7a43371b306 128 palStatus_t pal_osDelay(uint32_t milliseconds)
ram54288 0:a7a43371b306 129 {
ram54288 0:a7a43371b306 130 palStatus_t status;
ram54288 0:a7a43371b306 131 status = pal_plat_osDelay(milliseconds);
ram54288 0:a7a43371b306 132 return status;
ram54288 0:a7a43371b306 133 }
ram54288 0:a7a43371b306 134
ram54288 0:a7a43371b306 135
ram54288 0:a7a43371b306 136 palStatus_t pal_osTimerCreate(palTimerFuncPtr function, void* funcArgument, palTimerType_t timerType, palTimerID_t* timerID)
ram54288 0:a7a43371b306 137 {
ram54288 0:a7a43371b306 138 palStatus_t status;
ram54288 0:a7a43371b306 139 status = pal_plat_osTimerCreate(function, funcArgument, timerType, timerID);
ram54288 0:a7a43371b306 140 return status;
ram54288 0:a7a43371b306 141 }
ram54288 0:a7a43371b306 142
ram54288 0:a7a43371b306 143 palStatus_t pal_osTimerStart(palTimerID_t timerID, uint32_t millisec)
ram54288 0:a7a43371b306 144 {
ram54288 0:a7a43371b306 145 palStatus_t status;
ram54288 0:a7a43371b306 146 status = pal_plat_osTimerStart(timerID, millisec);
ram54288 0:a7a43371b306 147 return status;
ram54288 0:a7a43371b306 148 }
ram54288 0:a7a43371b306 149
ram54288 0:a7a43371b306 150 palStatus_t pal_osTimerStop(palTimerID_t timerID)
ram54288 0:a7a43371b306 151 {
ram54288 0:a7a43371b306 152 palStatus_t status;
ram54288 0:a7a43371b306 153 status = pal_plat_osTimerStop(timerID);
ram54288 0:a7a43371b306 154 return status;
ram54288 0:a7a43371b306 155 }
ram54288 0:a7a43371b306 156
ram54288 0:a7a43371b306 157 palStatus_t pal_osTimerDelete(palTimerID_t* timerID)
ram54288 0:a7a43371b306 158 {
ram54288 0:a7a43371b306 159 palStatus_t status;
ram54288 0:a7a43371b306 160 status = pal_plat_osTimerDelete(timerID);
ram54288 0:a7a43371b306 161 return status;
ram54288 0:a7a43371b306 162 }
ram54288 0:a7a43371b306 163
ram54288 0:a7a43371b306 164 palStatus_t pal_osMutexCreate(palMutexID_t* mutexID)
ram54288 0:a7a43371b306 165 {
ram54288 0:a7a43371b306 166 palStatus_t status;
ram54288 0:a7a43371b306 167 status = pal_plat_osMutexCreate(mutexID);
ram54288 0:a7a43371b306 168 return status;
ram54288 0:a7a43371b306 169 }
ram54288 0:a7a43371b306 170
ram54288 0:a7a43371b306 171 palStatus_t pal_osMutexWait(palMutexID_t mutexID, uint32_t millisec)
ram54288 0:a7a43371b306 172 {
ram54288 0:a7a43371b306 173 palStatus_t status;
ram54288 0:a7a43371b306 174 status = pal_plat_osMutexWait(mutexID, millisec);
ram54288 0:a7a43371b306 175 return status;
ram54288 0:a7a43371b306 176 }
ram54288 0:a7a43371b306 177
ram54288 0:a7a43371b306 178 palStatus_t pal_osMutexRelease(palMutexID_t mutexID)
ram54288 0:a7a43371b306 179 {
ram54288 0:a7a43371b306 180 palStatus_t status;
ram54288 0:a7a43371b306 181 status = pal_plat_osMutexRelease(mutexID);
ram54288 0:a7a43371b306 182 return status;
ram54288 0:a7a43371b306 183 }
ram54288 0:a7a43371b306 184
ram54288 0:a7a43371b306 185 palStatus_t pal_osMutexDelete(palMutexID_t* mutexID)
ram54288 0:a7a43371b306 186 {
ram54288 0:a7a43371b306 187 palStatus_t status;
ram54288 0:a7a43371b306 188 status = pal_plat_osMutexDelete(mutexID);
ram54288 0:a7a43371b306 189 return status;
ram54288 0:a7a43371b306 190 }
ram54288 0:a7a43371b306 191 palStatus_t pal_osSemaphoreCreate(uint32_t count, palSemaphoreID_t* semaphoreID)
ram54288 0:a7a43371b306 192 {
ram54288 0:a7a43371b306 193 palStatus_t status;
ram54288 0:a7a43371b306 194 status = pal_plat_osSemaphoreCreate(count, semaphoreID);
ram54288 0:a7a43371b306 195 return status;
ram54288 0:a7a43371b306 196 }
ram54288 0:a7a43371b306 197
ram54288 0:a7a43371b306 198 palStatus_t pal_osSemaphoreWait(palSemaphoreID_t semaphoreID, uint32_t millisec, int32_t* countersAvailable)
ram54288 0:a7a43371b306 199 {
ram54288 0:a7a43371b306 200 palStatus_t status;
ram54288 0:a7a43371b306 201 status = pal_plat_osSemaphoreWait(semaphoreID, millisec, countersAvailable);
ram54288 0:a7a43371b306 202 return status;
ram54288 0:a7a43371b306 203 }
ram54288 0:a7a43371b306 204
ram54288 0:a7a43371b306 205 palStatus_t pal_osSemaphoreRelease(palSemaphoreID_t semaphoreID)
ram54288 0:a7a43371b306 206 {
ram54288 0:a7a43371b306 207 palStatus_t status;
ram54288 0:a7a43371b306 208 status = pal_plat_osSemaphoreRelease(semaphoreID);
ram54288 0:a7a43371b306 209 return status;
ram54288 0:a7a43371b306 210 }
ram54288 0:a7a43371b306 211
ram54288 0:a7a43371b306 212 palStatus_t pal_osSemaphoreDelete(palSemaphoreID_t* semaphoreID)
ram54288 0:a7a43371b306 213 {
ram54288 0:a7a43371b306 214 palStatus_t status;
ram54288 0:a7a43371b306 215 status = pal_plat_osSemaphoreDelete(semaphoreID);
ram54288 0:a7a43371b306 216 return status;
ram54288 0:a7a43371b306 217 }
ram54288 0:a7a43371b306 218
ram54288 0:a7a43371b306 219 palStatus_t pal_osPoolCreate(uint32_t blockSize, uint32_t blockCount, palMemoryPoolID_t* memoryPoolID)
ram54288 0:a7a43371b306 220 {
ram54288 0:a7a43371b306 221 palStatus_t status;
ram54288 0:a7a43371b306 222 status = pal_plat_osPoolCreate(blockSize, blockCount, memoryPoolID);
ram54288 0:a7a43371b306 223 return status;
ram54288 0:a7a43371b306 224 }
ram54288 0:a7a43371b306 225
ram54288 0:a7a43371b306 226 void* pal_osPoolAlloc(palMemoryPoolID_t memoryPoolID)
ram54288 0:a7a43371b306 227 {
ram54288 0:a7a43371b306 228 void* result;
ram54288 0:a7a43371b306 229 result = pal_plat_osPoolAlloc(memoryPoolID);
ram54288 0:a7a43371b306 230 return result;
ram54288 0:a7a43371b306 231 }
ram54288 0:a7a43371b306 232
ram54288 0:a7a43371b306 233 void* pal_osPoolCAlloc(palMemoryPoolID_t memoryPoolID)
ram54288 0:a7a43371b306 234 {
ram54288 0:a7a43371b306 235 void* result;
ram54288 0:a7a43371b306 236 //TODO(nirson01): debug print in case of failed alloc?
ram54288 0:a7a43371b306 237 result = pal_plat_osPoolCAlloc(memoryPoolID);
ram54288 0:a7a43371b306 238 return result;
ram54288 0:a7a43371b306 239 }
ram54288 0:a7a43371b306 240
ram54288 0:a7a43371b306 241 palStatus_t pal_osPoolFree(palMemoryPoolID_t memoryPoolID, void* block)
ram54288 0:a7a43371b306 242 {
ram54288 0:a7a43371b306 243 palStatus_t status;
ram54288 0:a7a43371b306 244 //TODO(nirson01): debug print in case of failed alloc?
ram54288 0:a7a43371b306 245 status = pal_plat_osPoolFree(memoryPoolID, block);
ram54288 0:a7a43371b306 246 return status;
ram54288 0:a7a43371b306 247 }
ram54288 0:a7a43371b306 248
ram54288 0:a7a43371b306 249 palStatus_t pal_osPoolDestroy(palMemoryPoolID_t* memoryPoolID)
ram54288 0:a7a43371b306 250 {
ram54288 0:a7a43371b306 251 palStatus_t status;
ram54288 0:a7a43371b306 252 status = pal_plat_osPoolDestroy(memoryPoolID);
ram54288 0:a7a43371b306 253 return status;
ram54288 0:a7a43371b306 254 }
ram54288 0:a7a43371b306 255
ram54288 0:a7a43371b306 256 palStatus_t pal_osMessageQueueCreate(uint32_t messageQSize, palMessageQID_t* messageQID)
ram54288 0:a7a43371b306 257 {
ram54288 0:a7a43371b306 258 palStatus_t status;
ram54288 0:a7a43371b306 259 status = pal_plat_osMessageQueueCreate(messageQSize, messageQID);
ram54288 0:a7a43371b306 260 return status;
ram54288 0:a7a43371b306 261 }
ram54288 0:a7a43371b306 262
ram54288 0:a7a43371b306 263 palStatus_t pal_osMessagePut(palMessageQID_t messageQID, uint32_t info, uint32_t timeout)
ram54288 0:a7a43371b306 264 {
ram54288 0:a7a43371b306 265 palStatus_t status;
ram54288 0:a7a43371b306 266 status = pal_plat_osMessagePut(messageQID, info, timeout);
ram54288 0:a7a43371b306 267 return status;
ram54288 0:a7a43371b306 268 }
ram54288 0:a7a43371b306 269
ram54288 0:a7a43371b306 270 palStatus_t pal_osMessageGet(palMessageQID_t messageQID, uint32_t timeout, uint32_t* messageValue)
ram54288 0:a7a43371b306 271 {
ram54288 0:a7a43371b306 272 palStatus_t status;
ram54288 0:a7a43371b306 273 status = pal_plat_osMessageGet(messageQID, timeout, messageValue);
ram54288 0:a7a43371b306 274 return status;
ram54288 0:a7a43371b306 275 }
ram54288 0:a7a43371b306 276
ram54288 0:a7a43371b306 277 palStatus_t pal_osMessageQueueDestroy(palMessageQID_t* messageQID)
ram54288 0:a7a43371b306 278 {
ram54288 0:a7a43371b306 279 palStatus_t status;
ram54288 0:a7a43371b306 280 status = pal_plat_osMessageQueueDestroy(messageQID);
ram54288 0:a7a43371b306 281 return status;
ram54288 0:a7a43371b306 282 }
ram54288 0:a7a43371b306 283
ram54288 0:a7a43371b306 284 int32_t pal_osAtomicIncrement(int32_t* valuePtr, int32_t increment)
ram54288 0:a7a43371b306 285 {
ram54288 0:a7a43371b306 286 int32_t result;
ram54288 0:a7a43371b306 287 result = pal_plat_osAtomicIncrement(valuePtr, increment);
ram54288 0:a7a43371b306 288 return result;
ram54288 0:a7a43371b306 289 }
ram54288 0:a7a43371b306 290
ram54288 0:a7a43371b306 291
ram54288 0:a7a43371b306 292
ram54288 0:a7a43371b306 293 #ifdef DEBUG
ram54288 0:a7a43371b306 294 #include "stdarg.h"
ram54288 0:a7a43371b306 295 #endif
ram54288 0:a7a43371b306 296
ram54288 0:a7a43371b306 297 void dbgPrintf( const char* function, uint32_t line, const char * format, ... )
ram54288 0:a7a43371b306 298 {
ram54288 0:a7a43371b306 299 #ifdef DEBUG
ram54288 0:a7a43371b306 300 static palMutexID_t printfMutex = NULLPTR;
ram54288 0:a7a43371b306 301
ram54288 0:a7a43371b306 302 va_list args;
ram54288 0:a7a43371b306 303 if (!printfMutex)
ram54288 0:a7a43371b306 304 {
ram54288 0:a7a43371b306 305 pal_osMutexCreate(&printfMutex);
ram54288 0:a7a43371b306 306 }
ram54288 0:a7a43371b306 307 pal_osMutexWait(printfMutex, PAL_MAX_UINT32);
ram54288 0:a7a43371b306 308 #ifdef VERBOSE
ram54288 0:a7a43371b306 309 pal_plat_printf("%s:%ld\t",function,line);
ram54288 0:a7a43371b306 310 #endif
ram54288 0:a7a43371b306 311 va_start (args, format);
ram54288 0:a7a43371b306 312 pal_plat_vprintf (format, args);
ram54288 0:a7a43371b306 313 va_end (args);
ram54288 0:a7a43371b306 314 pal_osMutexRelease(printfMutex);
ram54288 0:a7a43371b306 315 #endif
ram54288 0:a7a43371b306 316 }
ram54288 0:a7a43371b306 317
ram54288 0:a7a43371b306 318
ram54288 0:a7a43371b306 319