Skip to content

Commit

Permalink
[OIS] Open IoT SDK system layer adaptation
Browse files Browse the repository at this point in the history
Add cmsis-rtos locking option.
Implement cmsis-rtos system mutex.
Open IoT SDK system layer GN build integration.

Signed-off-by: ATmobica <[email protected]>
  • Loading branch information
ATmobica committed Nov 18, 2022
1 parent b7b6582 commit ca2f463
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 8 deletions.
3 changes: 3 additions & 0 deletions src/system/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ buildconfig_header("system_buildconfig") {
chip_system_config_posix_locking = chip_system_config_locking == "posix"
chip_system_config_freertos_locking = chip_system_config_locking == "freertos"
chip_system_config_mbed_locking = chip_system_config_locking == "mbed"
chip_system_config_cmsis_rtos_locking =
chip_system_config_locking == "cmsis-rtos"
chip_system_config_no_locking = chip_system_config_locking == "none"
have_clock_gettime = chip_system_config_clock == "clock_gettime"
have_clock_settime = have_clock_gettime
Expand All @@ -82,6 +84,7 @@ buildconfig_header("system_buildconfig") {
"CHIP_SYSTEM_CONFIG_POSIX_LOCKING=${chip_system_config_posix_locking}",
"CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING=${chip_system_config_freertos_locking}",
"CHIP_SYSTEM_CONFIG_MBED_LOCKING=${chip_system_config_mbed_locking}",
"CHIP_SYSTEM_CONFIG_CMSIS_RTOS_LOCKING=${chip_system_config_cmsis_rtos_locking}",
"CHIP_SYSTEM_CONFIG_NO_LOCKING=${chip_system_config_no_locking}",
"CHIP_SYSTEM_CONFIG_PROVIDE_STATISTICS=${chip_system_config_provide_statistics}",
"HAVE_CLOCK_GETTIME=${have_clock_gettime}",
Expand Down
42 changes: 36 additions & 6 deletions src/system/SystemConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@
#define CHIP_SYSTEM_CONFIG_MBED_LOCKING INET_CONFIG_MBED_LOCKING
#endif // !defined(CHIP_SYSTEM_CONFIG_MBED_LOCKING) && defined(INET_CONFIG_MBED_LOCKING)

#if !defined(CHIP_SYSTEM_CONFIG_CMSIS_RTOS_LOCKING) && defined(INET_CONFIG_CMSIS_RTOS_LOCKING)
#define CHIP_SYSTEM_CONFIG_CMSIS_RTOS_LOCKING INET_CONFIG_CMSIS_RTOS_LOCKING
#endif // !defined(CHIP_SYSTEM_CONFIG_CMSIS_RTOS_LOCKING) && defined(INET_CONFIG_CMSIS_RTOS_LOCKING)

#if !defined(CHIP_SYSTEM_CONFIG_PACKETBUFFER_POOL_SIZE) && defined(INET_CONFIG_NUM_BUFS)
#define CHIP_SYSTEM_CONFIG_PACKETBUFFER_POOL_SIZE INET_CONFIG_NUM_BUFS
#endif // !defined(CHIP_SYSTEM_CONFIG_PACKETBUFFER_POOL_SIZE) && defined(INET_CONFIG_NUM_BUFS)
Expand Down Expand Up @@ -226,6 +230,20 @@
#define CHIP_SYSTEM_CONFIG_MBED_LOCKING 0
#endif /* CHIP_SYSTEM_CONFIG_MBED_LOCKING */

/**
* @def CHIP_SYSTEM_CONFIG_CMSIS_RTOS_LOCKING
*
* @brief
* Use CMSIS-RTOS locking.
*
* This should be generally asserted (1) for CMSIS-RTOS.
*
* However, if you are simulating an LwIP-based system atop POSIX threads and BSD sockets, this should also be deasserted (0).
*/
#ifndef CHIP_SYSTEM_CONFIG_CMSIS_RTOS_LOCKING
#define CHIP_SYSTEM_CONFIG_CMSIS_RTOS_LOCKING 0
#endif /* CHIP_SYSTEM_CONFIG_CMSIS_RTOS_LOCKING */

/**
* @def CHIP_SYSTEM_CONFIG_POOL_USE_HEAP
*
Expand All @@ -248,13 +266,13 @@
#define CHIP_SYSTEM_CONFIG_NO_LOCKING 0
#endif /* CHIP_SYSTEM_CONFIG_NO_LOCKING */

#if !(CHIP_SYSTEM_CONFIG_POSIX_LOCKING || CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING || CHIP_SYSTEM_CONFIG_MBED_LOCKING || CHIP_SYSTEM_CONFIG_NO_LOCKING)
#error "REQUIRED: CHIP_SYSTEM_CONFIG_POSIX_LOCKING || CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING || CHIP_SYSTEM_CONFIG_MBED_LOCKING || CHIP_SYSTEM_CONFIG_NO_LOCKING"
#endif // !(CHIP_SYSTEM_CONFIG_POSIX_LOCKING || CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING || CHIP_SYSTEM_CONFIG_MBED_LOCKING || CHIP_SYSTEM_CONFIG_NO_LOCKING)
#if !(CHIP_SYSTEM_CONFIG_POSIX_LOCKING || CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING || CHIP_SYSTEM_CONFIG_MBED_LOCKING || CHIP_SYSTEM_CONFIG_CMSIS_RTOS_LOCKING ||CHIP_SYSTEM_CONFIG_NO_LOCKING)
#error "REQUIRED: CHIP_SYSTEM_CONFIG_POSIX_LOCKING || CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING || CHIP_SYSTEM_CONFIG_MBED_LOCKING || CHIP_SYSTEM_CONFIG_CMSIS_RTOS_LOCKING || CHIP_SYSTEM_CONFIG_NO_LOCKING"
#endif // !(CHIP_SYSTEM_CONFIG_POSIX_LOCKING || CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING || CHIP_SYSTEM_CONFIG_MBED_LOCKING || CHIP_SYSTEM_CONFIG_NO_LOCKING || CHIP_SYSTEM_CONFIG_CMSIS_RTOS_LOCKING)

#if CHIP_SYSTEM_CONFIG_NO_LOCKING && (CHIP_SYSTEM_CONFIG_POSIX_LOCKING || CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING || CHIP_SYSTEM_CONFIG_MBED_LOCKING)
#error "FORBIDDEN: CHIP_SYSTEM_CONFIG_NO_LOCKING && (CHIP_SYSTEM_CONFIG_POSIX_LOCKING || CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING || CHIP_SYSTEM_CONFIG_MBED_LOCKING)"
#endif // CHIP_SYSTEM_CONFIG_NO_LOCKING && (CHIP_SYSTEM_CONFIG_POSIX_LOCKING || CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING || CHIP_SYSTEM_CONFIG_MBED_LOCKING)
#if CHIP_SYSTEM_CONFIG_NO_LOCKING && (CHIP_SYSTEM_CONFIG_POSIX_LOCKING || CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING || CHIP_SYSTEM_CONFIG_MBED_LOCKING || CHIP_SYSTEM_CONFIG_CMSIS_RTOS_LOCKING)
#error "FORBIDDEN: CHIP_SYSTEM_CONFIG_NO_LOCKING && (CHIP_SYSTEM_CONFIG_POSIX_LOCKING || CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING || CHIP_SYSTEM_CONFIG_MBED_LOCKING || CHIP_SYSTEM_CONFIG_CMSIS_RTOS_LOCKING)"
#endif // CHIP_SYSTEM_CONFIG_NO_LOCKING && (CHIP_SYSTEM_CONFIG_POSIX_LOCKING || CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING || CHIP_SYSTEM_CONFIG_MBED_LOCKING || CHIP_SYSTEM_CONFIG_CMSIS_RTOS_LOCKING)

#if CHIP_SYSTEM_CONFIG_POSIX_LOCKING && CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING
#error "FORBIDDEN: CHIP_SYSTEM_CONFIG_POSIX_LOCKING && CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING"
Expand All @@ -268,6 +286,18 @@
#error "FORBIDDEN: CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING && CHIP_SYSTEM_CONFIG_MBED_LOCKING"
#endif // CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING && CHIP_SYSTEM_CONFIG_MBED_LOCKING

#if CHIP_SYSTEM_CONFIG_POSIX_LOCKING && CHIP_SYSTEM_CONFIG_CMSIS_RTOS_LOCKING
#error "FORBIDDEN: CHIP_SYSTEM_CONFIG_POSIX_LOCKING && CHIP_SYSTEM_CONFIG_CMSIS_RTOS_LOCKING"
#endif // CHIP_SYSTEM_CONFIG_POSIX_LOCKING && CHIP_SYSTEM_CONFIG_CMSIS_RTOS_LOCKING

#if CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING && CHIP_SYSTEM_CONFIG_CMSIS_RTOS_LOCKING
#error "FORBIDDEN: CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING && CHIP_SYSTEM_CONFIG_CMSIS_RTOS_LOCKING"
#endif // CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING && CHIP_SYSTEM_CONFIG_CMSIS_RTOS_LOCKING

#if CHIP_SYSTEM_CONFIG_MBED_LOCKING && CHIP_SYSTEM_CONFIG_CMSIS_RTOS_LOCKING
#error "FORBIDDEN: CHIP_SYSTEM_CONFIG_MBED_LOCKING && CHIP_SYSTEM_CONFIG_CMSIS_RTOS_LOCKING"
#endif // CHIP_SYSTEM_CONFIG_MBED_LOCKING && CHIP_SYSTEM_CONFIG_CMSIS_RTOS_LOCKING

/**
* @def CHIP_SYSTEM_HEADER_RESERVE_SIZE
*
Expand Down
31 changes: 31 additions & 0 deletions src/system/SystemMutex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
// Include module header
#include <system/SystemMutex.h>

#include <lib/support/logging/CHIPLogging.h>

#if !CHIP_SYSTEM_CONFIG_NO_LOCKING

// Include system headers
Expand Down Expand Up @@ -108,6 +110,35 @@ DLL_EXPORT void Mutex::Lock(void)
}
#endif // CHIP_SYSTEM_CONFIG_FREERTOS_LOCKING

#if CHIP_SYSTEM_CONFIG_CMSIS_RTOS_LOCKING
DLL_EXPORT CHIP_ERROR Mutex::Init(Mutex & aThis)
{
aThis.mCmsisRTOSMutex = osMutexNew(NULL);
if (aThis.mCmsisRTOSMutex == NULL)
{
ChipLogError(chipSystemLayer, "osMutexNew failed");
return CHIP_ERROR_NO_MEMORY;
}
return CHIP_NO_ERROR;
}

DLL_EXPORT void Mutex::Lock(void)
{
if (mCmsisRTOSMutex && osMutexAcquire(mCmsisRTOSMutex, osWaitForever) != osOK)
{
ChipLogError(chipSystemLayer, "osMutexAcquire failed");
}
}

DLL_EXPORT void Mutex::Unlock(void)
{
if (mCmsisRTOSMutex && osMutexRelease(mCmsisRTOSMutex) != osOK)
{
ChipLogError(chipSystemLayer, "osMutexRelease failed");
}
}
#endif // CHIP_SYSTEM_CONFIG_CMSIS_RTOS_LOCKING

} // namespace System
} // namespace chip

Expand Down
8 changes: 8 additions & 0 deletions src/system/SystemMutex.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@
#include <rtos/Mutex.h>
#endif // CHIP_SYSTEM_CONFIG_MBED_LOCKING

#if CHIP_SYSTEM_CONFIG_CMSIS_RTOS_LOCKING
#include <cmsis_os2.h>
#endif // CHIP_SYSTEM_CONFIG_CMSIS_RTOS_LOCKING

namespace chip {
namespace System {

Expand Down Expand Up @@ -101,6 +105,10 @@ class DLL_EXPORT Mutex
rtos::Mutex mMbedMutex;
#endif // CHIP_SYSTEM_CONFIG_MBED_LOCKING

#if CHIP_SYSTEM_CONFIG_CMSIS_RTOS_LOCKING
osMutexId_t mCmsisRTOSMutex;
#endif // CHIP_SYSTEM_CONFIG_CMSIS_RTOS_LOCKING

Mutex(const Mutex &) = delete;
Mutex & operator=(const Mutex &) = delete;
};
Expand Down
7 changes: 5 additions & 2 deletions src/system/system.gni
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ if (chip_system_config_locking == "") {
chip_system_config_locking = "freertos"
} else if (current_os == "mbed") {
chip_system_config_locking = "mbed"
} else if (current_os == "cmsis-rtos") {
chip_system_config_locking = "cmsis-rtos"
} else if (chip_system_config_use_dispatch == false) {
chip_system_config_locking = "posix"
} else {
Expand All @@ -68,8 +70,9 @@ assert(
chip_system_config_locking == "posix" ||
chip_system_config_locking == "freertos" ||
chip_system_config_locking == "none" ||
chip_system_config_locking == "mbed",
"Please select a valid mutex implementation: posix, freertos, mbed, none")
chip_system_config_locking == "mbed" ||
chip_system_config_locking == "cmsis-rtos",
"Please select a valid mutex implementation: posix, freertos, mbed, cmsis-rtos, none")

assert(
chip_system_config_clock == "clock_gettime" ||
Expand Down

0 comments on commit ca2f463

Please sign in to comment.