]> git.sur5r.net Git - freertos/blobdiff - FreeRTOS-Labs/Source/FreeRTOS-IoT-Libraries/abstractions/platform/freertos/iot_threads_freertos.c
Add the Labs projects provided in the V10.2.1_191129 zip file.
[freertos] / FreeRTOS-Labs / Source / FreeRTOS-IoT-Libraries / abstractions / platform / freertos / iot_threads_freertos.c
diff --git a/FreeRTOS-Labs/Source/FreeRTOS-IoT-Libraries/abstractions/platform/freertos/iot_threads_freertos.c b/FreeRTOS-Labs/Source/FreeRTOS-IoT-Libraries/abstractions/platform/freertos/iot_threads_freertos.c
new file mode 100644 (file)
index 0000000..e1d8119
--- /dev/null
@@ -0,0 +1,365 @@
+/*\r
+ * Amazon FreeRTOS Platform V1.1.0\r
+ * Copyright (C) 2019 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
+ *\r
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
+ * this software and associated documentation files (the "Software"), to deal in\r
+ * the Software without restriction, including without limitation the rights to\r
+ * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
+ * the Software, and to permit persons to whom the Software is furnished to do so,\r
+ * subject to the following conditions:\r
+ *\r
+ * The above copyright notice and this permission notice shall be included in all\r
+ * copies or substantial portions of the Software.\r
+ *\r
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
+ * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
+ * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
+ * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
+ *\r
+ * http://aws.amazon.com/freertos\r
+ * http://www.FreeRTOS.org\r
+ */\r
+\r
+/**\r
+ * @file iot_threads_freertos.c\r
+ * @brief Implementation of the platform specific functions in iot_threads.h for \r
+ * FreeRTOS.\r
+ */\r
+\r
+/* The config header is always included first. */\r
+#include "iot_config.h"\r
+\r
+#include "semphr.h"\r
+\r
+/* Platform threads include. */\r
+#include "platform/iot_platform_types_freertos.h"\r
+#include "platform/iot_threads.h"\r
+#include "types/iot_platform_types.h"\r
+\r
+/* Configure logs for the functions in this file. */\r
+#ifdef IOT_LOG_LEVEL_PLATFORM\r
+    #define LIBRARY_LOG_LEVEL        IOT_LOG_LEVEL_PLATFORM\r
+#else\r
+    #ifdef IOT_LOG_LEVEL_GLOBAL\r
+        #define LIBRARY_LOG_LEVEL    IOT_LOG_LEVEL_GLOBAL\r
+    #else\r
+        #define LIBRARY_LOG_LEVEL    IOT_LOG_NONE\r
+    #endif\r
+#endif\r
+\r
+#define LIBRARY_LOG_NAME    ( "THREAD" )\r
+#include "iot_logging_setup.h"\r
+\r
+/*\r
+ * Provide default values for undefined memory allocation functions based on\r
+ * the usage of dynamic memory allocation.\r
+ */\r
+#ifndef IotThreads_Malloc\r
+    #include <stdlib.h>\r
+\r
+/**\r
+ * @brief Memory allocation. This function should have the same signature\r
+ * as [malloc](http://pubs.opengroup.org/onlinepubs/9699919799/functions/malloc.html).\r
+ */\r
+    #define IotThreads_Malloc    malloc\r
+#endif\r
+#ifndef IotThreads_Free\r
+    #include <stdlib.h>\r
+\r
+/**\r
+ * @brief Free memory. This function should have the same signature as\r
+ * [free](http://pubs.opengroup.org/onlinepubs/9699919799/functions/free.html).\r
+ */\r
+    #define IotThreads_Free    free\r
+#endif\r
+\r
+/*-----------------------------------------------------------*/\r
+\r
+static void _threadRoutineWrapper( void * pArgument )\r
+{\r
+    threadInfo_t * pThreadInfo = ( threadInfo_t * ) pArgument;\r
+\r
+    /* Run the thread routine. */\r
+    pThreadInfo->threadRoutine( pThreadInfo->pArgument );\r
+    IotThreads_Free( pThreadInfo );\r
+\r
+    vTaskDelete( NULL );\r
+}\r
+\r
+/*-----------------------------------------------------------*/\r
+\r
+bool Iot_CreateDetachedThread( IotThreadRoutine_t threadRoutine,\r
+                               void * pArgument,\r
+                               int32_t priority,\r
+                               size_t stackSize )\r
+{\r
+    bool status = true;\r
+\r
+    configASSERT( threadRoutine != NULL );\r
+\r
+    IotLogDebug( "Creating new thread." );\r
+    threadInfo_t * pThreadInfo = IotThreads_Malloc( sizeof( threadInfo_t ) );\r
+\r
+    if( pThreadInfo == NULL )\r
+    {\r
+        IotLogDebug( "Unable to allocate memory for threadRoutine %p.", threadRoutine );\r
+        status = false;\r
+    }\r
+\r
+    /* Create the FreeRTOS task that will run the thread. */\r
+    if( status )\r
+    {\r
+        pThreadInfo->threadRoutine = threadRoutine;\r
+        pThreadInfo->pArgument = pArgument;\r
+\r
+        if( xTaskCreate( _threadRoutineWrapper,\r
+                         "iot_thread",\r
+                         ( configSTACK_DEPTH_TYPE ) stackSize,\r
+                         pThreadInfo,\r
+                         priority,\r
+                         NULL ) != pdPASS )\r
+        {\r
+            /* Task creation failed. */\r
+            IotLogWarn( "Failed to create thread." );\r
+            IotThreads_Free( pThreadInfo );\r
+            status = false;\r
+        }\r
+    }\r
+\r
+    return status;\r
+}\r
+\r
+/*-----------------------------------------------------------*/\r
+\r
+bool IotMutex_Create( IotMutex_t * pNewMutex,\r
+                      bool recursive )\r
+{\r
+    _IotSystemMutex_t * internalMutex = ( _IotSystemMutex_t * ) pNewMutex;\r
+\r
+    configASSERT( internalMutex != NULL );\r
+\r
+    IotLogDebug( "Creating new mutex %p.", pNewMutex );\r
+\r
+    if( recursive )\r
+    {\r
+        ( void ) xSemaphoreCreateRecursiveMutexStatic( &internalMutex->xMutex );\r
+    }\r
+    else\r
+    {\r
+        ( void ) xSemaphoreCreateMutexStatic( &internalMutex->xMutex );\r
+    }\r
+\r
+    /* remember the type of mutex */\r
+    if( recursive )\r
+    {\r
+        internalMutex->recursive = pdTRUE;\r
+    }\r
+    else\r
+    {\r
+        internalMutex->recursive = pdFALSE;\r
+    }\r
+\r
+    return true;\r
+}\r
+\r
+/*-----------------------------------------------------------*/\r
+\r
+void IotMutex_Destroy( IotMutex_t * pMutex )\r
+{\r
+    _IotSystemMutex_t * internalMutex = ( _IotSystemMutex_t * ) pMutex;\r
+\r
+    configASSERT( internalMutex != NULL );\r
+\r
+    vSemaphoreDelete( ( SemaphoreHandle_t ) &internalMutex->xMutex );\r
+}\r
+\r
+/*-----------------------------------------------------------*/\r
+\r
+bool prIotMutexTimedLock( IotMutex_t * pMutex,\r
+                          TickType_t timeout )\r
+{\r
+    _IotSystemMutex_t * internalMutex = ( _IotSystemMutex_t * ) pMutex;\r
+    BaseType_t lockResult;\r
+\r
+    configASSERT( internalMutex != NULL );\r
+\r
+    IotLogDebug( "Locking mutex %p.", internalMutex );\r
+\r
+    /* Call the correct FreeRTOS mutex take function based on mutex type. */\r
+    if( internalMutex->recursive == pdTRUE )\r
+    {\r
+        lockResult = xSemaphoreTakeRecursive( ( SemaphoreHandle_t ) &internalMutex->xMutex, timeout );\r
+    }\r
+    else\r
+    {\r
+        lockResult = xSemaphoreTake( ( SemaphoreHandle_t ) &internalMutex->xMutex, timeout );\r
+    }\r
+\r
+    return( lockResult == pdTRUE );\r
+}\r
+\r
+/*-----------------------------------------------------------*/\r
+\r
+void IotMutex_Lock( IotMutex_t * pMutex )\r
+{\r
+    prIotMutexTimedLock( pMutex, portMAX_DELAY );\r
+}\r
+\r
+/*-----------------------------------------------------------*/\r
+\r
+bool IotMutex_TryLock( IotMutex_t * pMutex )\r
+{\r
+    return prIotMutexTimedLock( pMutex, 0 );\r
+}\r
+\r
+/*-----------------------------------------------------------*/\r
+\r
+void IotMutex_Unlock( IotMutex_t * pMutex )\r
+{\r
+    _IotSystemMutex_t * internalMutex = ( _IotSystemMutex_t * ) pMutex;\r
+\r
+    configASSERT( internalMutex != NULL );\r
+\r
+    IotLogDebug( "Unlocking mutex %p.", internalMutex );\r
+\r
+    /* Call the correct FreeRTOS mutex unlock function based on mutex type. */\r
+    if( internalMutex->recursive == pdTRUE )\r
+    {\r
+        ( void ) xSemaphoreGiveRecursive( ( SemaphoreHandle_t ) &internalMutex->xMutex );\r
+    }\r
+    else\r
+    {\r
+        ( void ) xSemaphoreGive( ( SemaphoreHandle_t ) &internalMutex->xMutex );\r
+    }\r
+}\r
+\r
+/*-----------------------------------------------------------*/\r
+\r
+bool IotSemaphore_Create( IotSemaphore_t * pNewSemaphore,\r
+                          uint32_t initialValue,\r
+                          uint32_t maxValue )\r
+{\r
+    _IotSystemSemaphore_t * internalSemaphore = ( _IotSystemSemaphore_t * ) pNewSemaphore;\r
+\r
+    configASSERT( internalSemaphore != NULL );\r
+\r
+    IotLogDebug( "Creating new semaphore %p.", pNewSemaphore );\r
+\r
+    ( void ) xSemaphoreCreateCountingStatic( maxValue, initialValue, &internalSemaphore->xSemaphore );\r
+\r
+    return true;\r
+}\r
+\r
+/*-----------------------------------------------------------*/\r
+\r
+uint32_t IotSemaphore_GetCount( IotSemaphore_t * pSemaphore )\r
+{\r
+    _IotSystemSemaphore_t * internalSemaphore = ( _IotSystemSemaphore_t * ) pSemaphore;\r
+    UBaseType_t count = 0;\r
+\r
+    configASSERT( internalSemaphore != NULL );\r
+\r
+    count = uxSemaphoreGetCount( ( SemaphoreHandle_t ) &internalSemaphore->xSemaphore );\r
+\r
+    IotLogDebug( "Semaphore %p has count %d.", pSemaphore, count );\r
+\r
+    return ( uint32_t ) count;\r
+}\r
+\r
+/*-----------------------------------------------------------*/\r
+\r
+void IotSemaphore_Destroy( IotSemaphore_t * pSemaphore )\r
+{\r
+    _IotSystemSemaphore_t * internalSemaphore = ( _IotSystemSemaphore_t * ) pSemaphore;\r
+\r
+    configASSERT( internalSemaphore != NULL );\r
+\r
+    IotLogDebug( "Destroying semaphore %p.", internalSemaphore );\r
+\r
+    vSemaphoreDelete( ( SemaphoreHandle_t ) &internalSemaphore->xSemaphore );\r
+}\r
+\r
+/*-----------------------------------------------------------*/\r
+\r
+void IotSemaphore_Wait( IotSemaphore_t * pSemaphore )\r
+{\r
+    _IotSystemSemaphore_t * internalSemaphore = ( _IotSystemSemaphore_t * ) pSemaphore;\r
+\r
+    configASSERT( internalSemaphore != NULL );\r
+\r
+    IotLogDebug( "Waiting on semaphore %p.", internalSemaphore );\r
+\r
+    /* Take the semaphore using the FreeRTOS API. */\r
+    if( xSemaphoreTake( ( SemaphoreHandle_t ) &internalSemaphore->xSemaphore,\r
+                        portMAX_DELAY ) != pdTRUE )\r
+    {\r
+        IotLogWarn( "Failed to wait on semaphore %p.",\r
+                    pSemaphore );\r
+\r
+        /* There is an assert here because during debugging we could falsely \r
+         * believe that we are waiting successfully on a semaphore. */\r
+        configASSERT( false );\r
+    }\r
+}\r
+\r
+/*-----------------------------------------------------------*/\r
+\r
+bool IotSemaphore_TryWait( IotSemaphore_t * pSemaphore )\r
+{\r
+    _IotSystemSemaphore_t * internalSemaphore = ( _IotSystemSemaphore_t * ) pSemaphore;\r
+\r
+    configASSERT( internalSemaphore != NULL );\r
+\r
+    IotLogDebug( "Attempting to wait on semaphore %p.", internalSemaphore );\r
+\r
+    return IotSemaphore_TimedWait( pSemaphore, 0 );\r
+}\r
+\r
+/*-----------------------------------------------------------*/\r
+\r
+bool IotSemaphore_TimedWait( IotSemaphore_t * pSemaphore,\r
+                             uint32_t timeoutMs )\r
+{\r
+    _IotSystemSemaphore_t * internalSemaphore = ( _IotSystemSemaphore_t * ) pSemaphore;\r
+\r
+    configASSERT( internalSemaphore != NULL );\r
+\r
+    /* Take the semaphore using the FreeRTOS API. Cast the calculation to 64 bit to avoid overflows. */\r
+    if( xSemaphoreTake( ( SemaphoreHandle_t ) &internalSemaphore->xSemaphore,\r
+                        pdMS_TO_TICKS( timeoutMs ) ) != pdTRUE )\r
+    {\r
+        /* Only warn if timeout > 0. */\r
+        if( timeoutMs > 0 )\r
+        {\r
+            IotLogWarn( "Timeout waiting on semaphore %p.",\r
+                        internalSemaphore );\r
+        }\r
+\r
+        return false;\r
+    }\r
+\r
+    return true;\r
+}\r
+\r
+/*-----------------------------------------------------------*/\r
+\r
+void IotSemaphore_Post( IotSemaphore_t * pSemaphore )\r
+{\r
+    _IotSystemSemaphore_t * internalSemaphore = ( _IotSystemSemaphore_t * ) pSemaphore;\r
+\r
+    configASSERT( internalSemaphore != NULL );\r
+\r
+    IotLogDebug( "Posting to semaphore %p.", internalSemaphore );\r
+    /* Give the semaphore using the FreeRTOS API. */\r
+    BaseType_t result = xSemaphoreGive( ( SemaphoreHandle_t ) &internalSemaphore->xSemaphore );\r
+\r
+    if( result == pdFALSE )\r
+    {\r
+        IotLogDebug( "Unable to give semaphore over maximum", internalSemaphore );\r
+    }\r
+}\r
+\r
+/*-----------------------------------------------------------*/\r