Mayank Gupta / Mbed OS pelion-example-frdm

Dependencies:   FXAS21002 FXOS8700Q

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers pal_rtos.c Source File

pal_rtos.c

00001 /*******************************************************************************
00002  * Copyright 2016-2018 ARM Ltd.
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  *******************************************************************************/
00016 
00017 #include "pal.h"
00018 #include "pal_plat_rtos.h"
00019 
00020 #include <stdlib.h>
00021 
00022 #define TRACE_GROUP "PAL"
00023 
00024 PAL_PRIVATE bool palRTOSInitialized = false;
00025 
00026 palStatus_t pal_RTOSInitialize(void* opaqueContext)
00027 {
00028     palStatus_t status = PAL_SUCCESS;
00029     if (palRTOSInitialized)
00030     {
00031         return status;
00032     }
00033 
00034     status = pal_plat_RTOSInitialize(opaqueContext);
00035     if (PAL_SUCCESS == status)
00036     {
00037         palRTOSInitialized = true;
00038     }
00039     else
00040     {
00041         PAL_LOG_ERR("pal_RTOSInitialize: pal_plat_RTOSInitialize failed, status=%" PRIx32 "\n", status);
00042     }
00043     return status;
00044 }
00045 
00046 palStatus_t pal_RTOSDestroy(void)
00047 {
00048     palStatus_t status = PAL_ERR_NOT_INITIALIZED ;
00049     if (!palRTOSInitialized)
00050     {
00051         return status;
00052     }
00053 
00054     status = pal_plat_RTOSDestroy();
00055     if (PAL_SUCCESS != status)
00056     {
00057         PAL_LOG_ERR("pal_RTOSDestroy: pal_plat_RTOSDestroy failed, status=%" PRIx32 "\n", status);
00058     }
00059     palRTOSInitialized = false;
00060     return status;
00061 }
00062 
00063 void pal_osReboot(void)
00064 {
00065 #if defined(PAL_USE_APPLICATION_REBOOT) && (PAL_USE_APPLICATION_REBOOT == 1)
00066     PAL_LOG_INFO("pal_osReboot (app)\n");
00067     pal_plat_osApplicationReboot();
00068 #else
00069     PAL_LOG_INFO("pal_osReboot (os)\n");
00070     pal_plat_osReboot();
00071 #endif
00072 }
00073 
00074 uint64_t pal_osKernelSysTick(void)
00075 {
00076     static uint64_t lastValue = 0;
00077     static uint64_t wraparoundsDetected = 0;
00078     const uint64_t one = 1;
00079     uint64_t tics = pal_plat_osKernelSysTick();
00080     uint64_t tmp = tics + (wraparoundsDetected << 32);
00081 
00082     if (tmp < lastValue) //erez's "wraparound algorithm" if we detect a wrap around add 1 to the higher 32 bits
00083     {
00084         tmp = tmp + (one << 32);
00085         wraparoundsDetected++;
00086     }
00087     lastValue = tmp;
00088     return (uint64_t)tmp;
00089 }
00090 
00091 uint64_t pal_osKernelSysTickMicroSec(uint64_t microseconds)
00092 {
00093     uint64_t result;
00094     result = pal_plat_osKernelSysTickMicroSec(microseconds);
00095     return result;
00096 }
00097 
00098 uint64_t pal_osKernelSysMilliSecTick(uint64_t sysTicks)
00099 {
00100     uint64_t result = 0;
00101     uint64_t osTickFreq = pal_plat_osKernelSysTickFrequency();
00102     if ((sysTicks) && (osTickFreq)) // > 0
00103     {
00104         result = (uint64_t)((sysTicks) * PAL_TICK_TO_MILLI_FACTOR / osTickFreq); //convert ticks per second to milliseconds
00105     }
00106 
00107     return result;
00108 }
00109 
00110 uint64_t pal_osKernelSysTickFrequency(void)
00111 {
00112     uint64_t result;
00113     result = pal_plat_osKernelSysTickFrequency();
00114     return result;
00115 }
00116 
00117 palStatus_t pal_osThreadCreateWithAlloc(palThreadFuncPtr function, void* funcArgument, palThreadPriority_t priority, uint32_t stackSize, palThreadLocalStore_t* store, palThreadID_t* threadID)
00118 {
00119     PAL_VALIDATE_ARGUMENTS((NULL == function) || (PAL_osPrioritylast < priority) || (PAL_osPriorityError == priority) || (0 == stackSize) || (NULL == threadID));
00120     if (store)
00121     {
00122         PAL_LOG_ERR("thread storage in not supported\n");
00123         return PAL_ERR_NOT_SUPPORTED ;
00124     }
00125     palStatus_t status = pal_plat_osThreadCreate(function, funcArgument, priority, stackSize, threadID);
00126     return status;
00127 }
00128 
00129 palStatus_t pal_osThreadTerminate(palThreadID_t* threadID)
00130 {
00131     PAL_VALIDATE_ARGUMENTS ((NULL == threadID) || (PAL_INVALID_THREAD == *threadID));
00132     palStatus_t status = pal_plat_osThreadTerminate(threadID);
00133     return status;
00134 }
00135 
00136 palThreadID_t pal_osThreadGetId(void)
00137 {
00138     palThreadID_t threadID = pal_plat_osThreadGetId();
00139     return threadID;
00140 }
00141 
00142 palStatus_t pal_osDelay(uint32_t milliseconds)
00143 {
00144     palStatus_t status;
00145     status = pal_plat_osDelay(milliseconds);
00146     return status;
00147 }
00148 
00149 palStatus_t pal_osTimerCreate(palTimerFuncPtr function, void* funcArgument, palTimerType_t timerType, palTimerID_t* timerID)
00150 {
00151     PAL_VALIDATE_ARGUMENTS(NULL == timerID || NULL == function);
00152     palStatus_t status;
00153     status = pal_plat_osTimerCreate(function, funcArgument, timerType, timerID);
00154     return status;
00155 }
00156 
00157 palStatus_t pal_osTimerStart(palTimerID_t timerID, uint32_t millisec)
00158 {
00159     PAL_VALIDATE_ARGUMENTS (NULLPTR == timerID);
00160     palStatus_t status;
00161     if (0 == millisec)
00162     {
00163         return PAL_ERR_RTOS_VALUE ;
00164     }
00165     status = pal_plat_osTimerStart(timerID, millisec);
00166     return status;
00167 }
00168 
00169 palStatus_t pal_osTimerStop(palTimerID_t timerID)
00170 {
00171     PAL_VALIDATE_ARGUMENTS(NULLPTR == timerID);
00172     palStatus_t status;
00173     status = pal_plat_osTimerStop(timerID);
00174     return status;
00175 }
00176 
00177 palStatus_t pal_osTimerDelete(palTimerID_t* timerID)
00178 {
00179     PAL_VALIDATE_ARGUMENTS(NULL == timerID || NULLPTR == *timerID);
00180     palStatus_t status;
00181     status = pal_plat_osTimerDelete(timerID);
00182     return status;
00183 }
00184 
00185 palStatus_t pal_osMutexCreate(palMutexID_t* mutexID)
00186 {
00187     PAL_VALIDATE_ARGUMENTS(NULL == mutexID);
00188     palStatus_t status;
00189     status = pal_plat_osMutexCreate(mutexID);
00190     return status;
00191 }
00192 
00193 palStatus_t pal_osMutexWait(palMutexID_t mutexID, uint32_t millisec)
00194 {
00195     PAL_VALIDATE_ARGUMENTS((NULLPTR == mutexID));
00196     palStatus_t status;
00197     status = pal_plat_osMutexWait(mutexID, millisec);
00198     return status;
00199 }
00200 
00201 palStatus_t pal_osMutexRelease(palMutexID_t mutexID)
00202 {
00203     PAL_VALIDATE_ARGUMENTS(NULLPTR == mutexID);
00204     palStatus_t status;
00205     status = pal_plat_osMutexRelease(mutexID);
00206     return status;
00207 }
00208 
00209 palStatus_t pal_osMutexDelete(palMutexID_t* mutexID)
00210 {
00211     PAL_VALIDATE_ARGUMENTS(NULL == mutexID || NULLPTR == *mutexID);
00212     palStatus_t status;
00213     status = pal_plat_osMutexDelete(mutexID);
00214     return status;
00215 }
00216 
00217 palStatus_t pal_osSemaphoreCreate(uint32_t count, palSemaphoreID_t* semaphoreID)
00218 {
00219     PAL_VALIDATE_ARGUMENTS(NULL == semaphoreID);
00220     palStatus_t status;
00221     status = pal_plat_osSemaphoreCreate(count, semaphoreID);
00222     return status;
00223 }
00224 
00225 palStatus_t pal_osSemaphoreWait(palSemaphoreID_t semaphoreID, uint32_t millisec,  int32_t* countersAvailable)
00226 {
00227     PAL_VALIDATE_ARGUMENTS(NULLPTR == semaphoreID);
00228     palStatus_t status;
00229     status = pal_plat_osSemaphoreWait(semaphoreID, millisec, countersAvailable);
00230     return status;
00231 }
00232 
00233 palStatus_t pal_osSemaphoreRelease(palSemaphoreID_t semaphoreID)
00234 {
00235     PAL_VALIDATE_ARGUMENTS(NULLPTR == semaphoreID);
00236     palStatus_t status;
00237     status = pal_plat_osSemaphoreRelease(semaphoreID);
00238     return status;
00239 }
00240 
00241 palStatus_t pal_osSemaphoreDelete(palSemaphoreID_t* semaphoreID)
00242 {
00243     PAL_VALIDATE_ARGUMENTS(NULL == semaphoreID || NULLPTR == *semaphoreID);
00244     palStatus_t status;
00245     status = pal_plat_osSemaphoreDelete(semaphoreID);
00246     return status;
00247 }
00248 
00249 
00250 int32_t pal_osAtomicIncrement(int32_t* valuePtr, int32_t increment)
00251 {
00252     PAL_VALIDATE_ARGUMENTS(NULL == valuePtr);
00253     int32_t result;
00254     result = pal_plat_osAtomicIncrement(valuePtr, increment);
00255     return result;
00256 }
00257