Skip to content

Commit 3cbd0f6

Browse files
Add a GenericPlatformManager implementation based on CMSISOS V2 API
1 parent d1213a0 commit 3cbd0f6

11 files changed

+614
-26
lines changed

src/include/platform/ConnectivityManager.h

+4
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ namespace Internal {
5252
template <class>
5353
class GenericPlatformManagerImpl;
5454
template <class>
55+
class GenericPlatformManagerImpl_CMSISOS;
56+
template <class>
5557
class GenericPlatformManagerImpl_FreeRTOS;
5658
template <class>
5759
class GenericPlatformManagerImpl_POSIX;
@@ -249,6 +251,8 @@ class ConnectivityManager
249251
template <class>
250252
friend class Internal::GenericPlatformManagerImpl;
251253
template <class>
254+
friend class Internal::GenericPlatformManagerImpl_CMSISOS;
255+
template <class>
252256
friend class Internal::GenericPlatformManagerImpl_FreeRTOS;
253257
template <class>
254258
friend class Internal::GenericPlatformManagerImpl_POSIX;

src/include/platform/PlatformManager.h

+4
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ class GenericConfigurationManagerImpl;
5353
template <class>
5454
class GenericPlatformManagerImpl;
5555
template <class>
56+
class GenericPlatformManagerImpl_CMSISOS;
57+
template <class>
5658
class GenericPlatformManagerImpl_FreeRTOS;
5759
template <class>
5860
class GenericPlatformManagerImpl_POSIX;
@@ -261,6 +263,8 @@ class PlatformManager
261263
template <class>
262264
friend class Internal::GenericPlatformManagerImpl;
263265
template <class>
266+
friend class Internal::GenericPlatformManagerImpl_CMSISOS;
267+
template <class>
264268
friend class Internal::GenericPlatformManagerImpl_FreeRTOS;
265269
template <class>
266270
friend class Internal::GenericPlatformManagerImpl_POSIX;

src/include/platform/ThreadStackManager.h

+4
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ class GenericPlatformManagerImpl;
5555
template <class>
5656
class GenericConfigurationManagerImpl;
5757
template <class>
58+
class GenericPlatformManagerImpl_CMSISOS;
59+
template <class>
5860
class GenericPlatformManagerImpl_FreeRTOS;
5961
template <class>
6062
class GenericConnectivityManagerImpl_Thread;
@@ -168,6 +170,8 @@ class ThreadStackManager
168170
template <class>
169171
friend class Internal::GenericConfigurationManagerImpl;
170172
template <class>
173+
friend class Internal::GenericPlatformManagerImpl_CMSISOS;
174+
template <class>
171175
friend class Internal::GenericPlatformManagerImpl_FreeRTOS;
172176
template <class>
173177
friend class Internal::GenericConnectivityManagerImpl_Thread;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
/*
2+
*
3+
* Copyright (c) 2021-2024 Project CHIP Authors
4+
* All rights reserved.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
/**
20+
* @file
21+
* Provides an generic implementation of PlatformManager features
22+
* for platform using CMSISOS 2 OS Abstraction APIs.
23+
*/
24+
25+
#pragma once
26+
27+
#include <platform/CHIPDeviceConfig.h>
28+
#include <platform/internal/GenericPlatformManagerImpl.h>
29+
30+
#include <atomic>
31+
#include <cmsis_os2.h>
32+
33+
namespace chip {
34+
namespace DeviceLayer {
35+
namespace Internal {
36+
37+
/**
38+
* Provides a generic implementation of PlatformManager features for platforms using CMSISOS 2 OS Abstraction APIs
39+
*
40+
* This template contains implementations of select features from the PlatformManager abstract
41+
* interface that are suitable for use on CMSISOS-based platforms. It is intended to be inherited
42+
* (directly or indirectly) by the PlatformManagerImpl class, which also appears as the template's
43+
* ImplClass parameter.
44+
*/
45+
template <class ImplClass>
46+
class GenericPlatformManagerImpl_CMSISOS : public GenericPlatformManagerImpl<ImplClass>
47+
{
48+
49+
protected:
50+
osMutexId_t mChipStackLock = NULL;
51+
osMessageQueueId_t mChipEventQueue = NULL;
52+
osThreadId_t mEventLoopTask = NULL;
53+
bool mChipTimerActive;
54+
55+
#if defined(CHIP_DEVICE_CONFIG_ENABLE_BG_EVENT_PROCESSING) && CHIP_DEVICE_CONFIG_ENABLE_BG_EVENT_PROCESSING
56+
osMessageQueueId_t mBackgroundEventQueue = NULL;
57+
osThreadId_t mBackgroundEventLoopTask = NULL;
58+
#endif
59+
60+
// ===== Methods that implement the PlatformManager abstract interface.
61+
62+
CHIP_ERROR _InitChipStack();
63+
64+
void _LockChipStack(void);
65+
bool _TryLockChipStack(void);
66+
void _UnlockChipStack(void);
67+
68+
CHIP_ERROR _PostEvent(const ChipDeviceEvent * event);
69+
void _RunEventLoop(void);
70+
CHIP_ERROR _StartEventLoopTask(void);
71+
CHIP_ERROR _StopEventLoopTask();
72+
CHIP_ERROR _StartChipTimer(System::Clock::Timeout duration);
73+
void _Shutdown(void);
74+
75+
#if CHIP_STACK_LOCK_TRACKING_ENABLED
76+
bool _IsChipStackLockedByCurrentThread() const;
77+
#endif
78+
79+
CHIP_ERROR _PostBackgroundEvent(const ChipDeviceEvent * event);
80+
void _RunBackgroundEventLoop(void);
81+
CHIP_ERROR _StartBackgroundEventLoopTask(void);
82+
CHIP_ERROR _StopBackgroundEventLoopTask();
83+
84+
// ===== Methods available to the implementation subclass.
85+
86+
private:
87+
// ===== Private members for use by this class only.
88+
89+
inline ImplClass * Impl() { return static_cast<ImplClass *>(this); }
90+
91+
static void EventLoopTaskMain(void * pvParameter);
92+
uint32_t SyncNextChipTimerHandling();
93+
uint32_t mNextTimerBaseTime;
94+
uint32_t mNextTimerDurationTicks;
95+
std::atomic<bool> mShouldRunEventLoop;
96+
97+
#if defined(CHIP_CONFIG_CMSISOS_USE_STATIC_QUEUE) && CHIP_CONFIG_CMSISOS_USE_STATIC_QUEUE
98+
uint8_t mEventQueueBuffer[CHIP_DEVICE_CONFIG_MAX_EVENT_QUEUE_SIZE * sizeof(ChipDeviceEvent)];
99+
osMessageQueue_t mEventQueueStruct;
100+
const osMessageQueueAttr_t mEventQueueAttr = { .cb_mem = &mEventQueueStruct,
101+
.cb_size = osMessageQueueCbSize,
102+
.mq_mem = mEventQueueBuffer,
103+
.mq_size = sizeof(mEventQueueBuffer) };
104+
105+
const osMessageQueueAttr_t * mEventQueueAttrPtr = &mEventQueueAttr;
106+
#else
107+
// Nothing to configure for queues, Just use this to avoid #ifdef in the class implementation
108+
const osMessageQueueAttr_t * mEventQueueAttrPtr = nullptr;
109+
#endif // CHIP_CONFIG_CMSISOS_USE_STATIC_QUEUE
110+
111+
#if defined(CHIP_CONFIG_CMSISOS_USE_STATIC_TASK) && CHIP_CONFIG_CMSISOS_USE_STATIC_TASK
112+
uint8_t mEventLoopStack[CHIP_DEVICE_CONFIG_CHIP_TASK_STACK_SIZE];
113+
osThread_t mEventLoopTaskControlBlock;
114+
#endif // CHIP_CONFIG_CMSISOS_USE_STATIC_TASK
115+
116+
const osThreadAttr_t mEventLoopTaskAttr = {
117+
.name = CHIP_DEVICE_CONFIG_CHIP_TASK_NAME,
118+
.attr_bits = osThreadDetached,
119+
#if defined(CHIP_CONFIG_CMSISOS_USE_STATIC_TASK) && CHIP_CONFIG_CMSISOS_USE_STATIC_TASK
120+
.cb_mem = &mEventLoopTaskControlBlock,
121+
.cb_size = osThreadCbSize,
122+
.stack_mem = mEventLoopStack,
123+
#endif // CHIP_CONFIG_CMSISOS_USE_STATIC_TASK
124+
.stack_size = CHIP_DEVICE_CONFIG_CHIP_TASK_STACK_SIZE,
125+
.priority = CHIP_DEVICE_CONFIG_CHIP_TASK_PRIORITY
126+
};
127+
128+
#if defined(CHIP_CONFIG_CMSISOS_USE_STATIC_MUTEX) && CHIP_CONFIG_CMSISOS_USE_STATIC_MUTEX
129+
osMutexCbSize uint8_t mMutexControlBlock[osMutexCbSize];
130+
#endif // CHIP_CONFIG_CMSISOS_USE_STATIC_MUTEX
131+
const osMutexAttr_t mChipStackMutexAttr = {
132+
.name = "",
133+
#if defined(CHIP_CONFIG_CMSISOS_USE_STATIC_MUTEX) && CHIP_CONFIG_CMSISOS_USE_STATIC_MUTEX
134+
.cb_mem = &mMutexControlBlock,
135+
.cb_size = osMutexCbSize,
136+
#endif // CHIP_CONFIG_CMSISOS_USE_STATIC_MUTEX
137+
.attr_bits = osMutexRecursive | osMutexPrioInherit,
138+
};
139+
140+
#if defined(CHIP_DEVICE_CONFIG_ENABLE_BG_EVENT_PROCESSING) && CHIP_DEVICE_CONFIG_ENABLE_BG_EVENT_PROCESSING
141+
static void BackgroundEventLoopTaskMain(void * pvParameter);
142+
std::atomic<bool> mShouldRunBackgroundEventLoop;
143+
144+
#if defined(CHIP_CONFIG_CMSISOS_USE_STATIC_QUEUE) && CHIP_CONFIG_CMSISOS_USE_STATIC_QUEUE
145+
uint8_t mBackgroundQueueBuffer[CHIP_DEVICE_CONFIG_BG_MAX_EVENT_QUEUE_SIZE * sizeof(ChipDeviceEvent)];
146+
osMessageQueue_t mBackgroundQueueStruct;
147+
const osMessageQueueAttr_t mBgQueueAttr = { .cb_mem = &mBackgroundQueueStruct,
148+
.cb_size = osMessageQueueCbSize,
149+
.mq_mem = mBackgroundQueueBuffer,
150+
.mq_size = sizeof(mBackgroundQueueBuffer) };
151+
152+
const osMessageQueueAttr_t * mBgQueueAttrPtr = &mBgQueueAttr;
153+
#else
154+
// Nothing to configure for queues, Just use this to avoid #ifdef in the class implementation
155+
const osMessageQueueAttr_t * mBgQueueAttrPtr = nullptr;
156+
#endif // CHIP_CONFIG_CMSISOS_USE_STATIC_QUEUE
157+
158+
#if defined(CHIP_CONFIG_CMSISOS_USE_STATIC_TASK) && CHIP_CONFIG_CMSISOS_USE_STATIC_TASK
159+
uint8_t mBackgroundEventLoopStack[CHIP_DEVICE_CONFIG_BG_TASK_STACK_SIZE];
160+
osThread_t mBackgroundEventLoopTaskControlBlock;
161+
#endif // CHIP_CONFIG_CMSISOS_USE_STATIC_TASK
162+
163+
const osThreadAttr_t mBgEventLoopTaskAttr = {
164+
.name = CHIP_DEVICE_CONFIG_BG_TASK_NAME,
165+
.attr_bits = osThreadDetached,
166+
#if defined(CHIP_CONFIG_CMSISOS_USE_STATIC_TASK) && CHIP_CONFIG_CMSISOS_USE_STATIC_TASK
167+
.cb_mem = &mBackgroundEventLoopTaskControlBlock,
168+
.cb_size = osThreadCbSize,
169+
.stack_mem = mBackgroundEventLoopStack,
170+
#endif // CHIP_CONFIG_CMSISOS_USE_STATIC_TASK
171+
.stack_size = CHIP_DEVICE_CONFIG_BG_TASK_STACK_SIZE,
172+
.priority = CHIP_DEVICE_CONFIG_BG_TASK_PRIORITY
173+
};
174+
#endif // CHIP_DEVICE_CONFIG_ENABLE_BG_EVENT_PROCESSING
175+
};
176+
177+
// Instruct the compiler to instantiate the template only when explicitly told to do so.
178+
extern template class GenericPlatformManagerImpl_CMSISOS<PlatformManagerImpl>;
179+
180+
} // namespace Internal
181+
} // namespace DeviceLayer
182+
} // namespace chip

0 commit comments

Comments
 (0)