Skip to content

Commit

Permalink
ETLCPP#431 CMSIS-RTOS2 support
Browse files Browse the repository at this point in the history
  • Loading branch information
jwellbelove committed Sep 28, 2021
1 parent a6d8a6d commit b9b7b9c
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 39 deletions.
7 changes: 5 additions & 2 deletions include/etl/mutex.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,12 @@ SOFTWARE.

#include "platform.h"

#if ETL_CPP11_SUPPORTED == 1 && ETL_USING_STL
#if ETL_CPP11_SUPPORTED && ETL_USING_STL
#include "mutex/mutex_std.h"
#define ETL_HAS_MUTEX 1
#elif defined(ETL_TARGET_OS_CMSIS_OS2)
#include "mutex/mutex_cmsis_os2.h"
#define ETL_HAS_MUTEX 1
#elif defined(ETL_TARGET_OS_FREERTOS)
#include "mutex/mutex_freertos.h"
#define ETL_HAS_MUTEX 1
Expand All @@ -43,7 +46,7 @@ SOFTWARE.
#elif defined(ETL_COMPILER_GCC)
#include "mutex/mutex_gcc_sync.h"
#define ETL_HAS_MUTEX 1
#elif defined(ETL_COMPILER_GCC)
#elif defined(ETL_COMPILER_CLANG)
#include "mutex/mutex_clang_sync.h"
#define ETL_HAS_MUTEX 1
#else
Expand Down
77 changes: 77 additions & 0 deletions include/etl/mutex/mutex_cmsis_os2.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com
Copyright(c) 2021 John Wellbelove
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
******************************************************************************/

#ifndef ETL_MUTEX_CMSIS_RTOS2_INCLUDED
#define ETL_MUTEX_CMSIS_RTOS2_INCLUDED

#include "../platform.h"

#include <cmsis_os2.h>

namespace etl
{
//***************************************************************************
///\ingroup mutex
///\brief This mutex class is implemented using CMSIS's RTOS2 mutexes
//***************************************************************************
class mutex
{
public:

mutex()
: id(0)
{
osMutexAttr_t attr = { "ETL", osMutexRecursive | osMutexPrioInherit | osMutexRobust, 0, 0 };
id = osMutexNew(&attr);
}

void lock()
{
osMutexAcquire(id, osWaitForever);
}

bool try_lock()
{
return osMutexAcquire(id, 0) == osOK;
}

void unlock()
{
osMutexRelease(id);
}

private:

mutex(const mutex&) ETL_DELETE;
mutex& operator=(const mutex&) ETL_DELETE;

osMutexId_t id;
};
}

#endif
75 changes: 38 additions & 37 deletions include/etl/mutex/mutex_freertos.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,44 +36,45 @@ SOFTWARE.

namespace etl
{
//***************************************************************************
///\ingroup mutex
///\brief This mutex class is implemented using FreeRTOS's mutexes
//***************************************************************************
class mutex
{
public:


mutex()
{
access = xSemaphoreCreateMutexStatic(&mutex_allocation);
}

void lock()
{
xSemaphoreTake(access, portMAX_DELAY); // portMAX_DELAY=block forever
}

bool try_lock()
{
return xSemaphoreTake(access, 0) == pdTRUE;
}

void unlock()
//***************************************************************************
///\ingroup mutex
///\brief This mutex class is implemented using FreeRTOS's mutexes
//***************************************************************************
class mutex
{
xSemaphoreGive(access);
}

private:
// Non-copyable
mutex(const mutex&);
mutex& operator=(const mutex&);
// Memory to hold the mutex
StaticSemaphore_t mutex_allocation;
// The mutex handle itself
SemaphoreHandle_t access;
};
public:

mutex()
{
access = xSemaphoreCreateMutexStatic(&mutex_allocation);
}

void lock()
{
xSemaphoreTake(access, portMAX_DELAY); // portMAX_DELAY=block forever
}

bool try_lock()
{
return xSemaphoreTake(access, 0) == pdTRUE;
}

void unlock()
{
xSemaphoreGive(access);
}

private:
// Non-copyable
mutex(const mutex&) ETL_DELETE;
mutex& operator=(const mutex&) ETL_DELETE;

// Memory to hold the mutex
StaticSemaphore_t mutex_allocation;

// The mutex handle itself
SemaphoreHandle_t access;
};
}

#endif
3 changes: 3 additions & 0 deletions include/etl/mutex/mutex_gcc_sync.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ namespace etl

private:

mutex(const mutex&) ETL_DELETE;
mutex& operator=(const mutex&) ETL_DELETE;

char flag;
};
}
Expand Down
3 changes: 3 additions & 0 deletions include/etl/mutex/mutex_std.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ namespace etl

private:

mutex(const mutex&) ETL_DELETE;
mutex& operator=(const mutex&) ETL_DELETE;

std::mutex access;
};
}
Expand Down
1 change: 1 addition & 0 deletions test/vs2019/etl.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1818,6 +1818,7 @@
<ClInclude Include="..\..\include\etl\multi_range.h" />
<ClInclude Include="..\..\include\etl\multi_vector.h" />
<ClInclude Include="..\..\include\etl\mutex\mutex_clang_sync.h" />
<ClInclude Include="..\..\include\etl\mutex\mutex_cmsis_os2.h" />
<ClInclude Include="..\..\include\etl\mutex\mutex_freertos.h" />
<ClInclude Include="..\..\include\etl\negative.h" />
<ClInclude Include="..\..\include\etl\overload.h" />
Expand Down
3 changes: 3 additions & 0 deletions test/vs2019/etl.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -1149,6 +1149,9 @@
<ClInclude Include="..\..\include\etl\multi_vector.h">
<Filter>ETL\Containers</Filter>
</ClInclude>
<ClInclude Include="..\..\include\etl\mutex\mutex_cmsis_os2.h">
<Filter>ETL\Utilities\Mutex</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\main.cpp">
Expand Down

0 comments on commit b9b7b9c

Please sign in to comment.