Skip to content

Commit 810916e

Browse files
Merge pull request #168 from Hadatko/feature/staticRPMSGApi
Allow usage of rpmsg static freertos related api
2 parents 7af8c7f + 68b32b2 commit 810916e

File tree

6 files changed

+149
-23
lines changed

6 files changed

+149
-23
lines changed

erpc_c/port/erpc_setup_extensions_freertos.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ using namespace erpc;
1616

1717
static Semaphore *s_erpc_call_in_progress = NULL;
1818
static TimerHandle_t s_erpc_call_timer_cb = NULL;
19+
#if ERPC_ALLOCATION_POLICY == ERPC_ALLOCATION_POLICY_STATIC
20+
static StaticTimer_t s_static_erpc_call_timer_cb;
21+
#endif
1922

2023
void erpc::erpc_pre_cb_default(void)
2124
{
@@ -46,16 +49,23 @@ void erpc_init_call_progress_detection_default(
4649
s_erpc_call_in_progress = new Semaphore(1);
4750
assert(s_erpc_call_in_progress && "Creating eRPC semaphore failed.");
4851

52+
#if ERPC_ALLOCATION_POLICY == ERPC_ALLOCATION_POLICY_STATIC
53+
s_erpc_call_timer_cb = xTimerCreateStatic("Erpc client call timer", waitTimeMs / portTICK_PERIOD_MS, pdFALSE, NULL,
54+
erpc_call_timer_cb, &s_static_erpc_call_timer_cb);
55+
#else
4956
s_erpc_call_timer_cb =
5057
xTimerCreate("Erpc client call timer", waitTimeMs / portTICK_PERIOD_MS, pdFALSE, NULL, erpc_call_timer_cb);
58+
#endif
5159
assert(s_erpc_call_timer_cb && "Creating eRPC timer failed.");
5260
}
5361

5462
void erpc_deinit_call_progress_detection_default(void)
5563
{
5664
if (s_erpc_call_in_progress != NULL)
5765
{
66+
#if ERPC_ALLOCATION_POLICY == ERPC_ALLOCATION_POLICY_DYNAMIC
5867
delete s_erpc_call_in_progress;
68+
#endif
5969
s_erpc_call_in_progress = NULL;
6070
}
6171

erpc_c/port/erpc_threading.h

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/*
22
* Copyright (c) 2014-2016, Freescale Semiconductor, Inc.
33
* Copyright 2016-2020 NXP
4+
* Copyright 2021 ACRIOS Systems s.r.o.
45
* All rights reserved.
56
*
67
*
@@ -70,14 +71,19 @@ class Thread
7071
public:
7172
//! @brief Unique identifier for a thread.
7273
typedef void *thread_id_t;
74+
#if ERPC_THREADS_IS(FREERTOS)
75+
typedef StackType_t *thread_stack_pointer;
76+
#else
77+
typedef void *thread_stack_pointer;
78+
#endif
7379

7480
/*!
7581
* @brief Default constructor for use with the init() method.
7682
*
7783
* If this constructor is used, the init() method must be called before the thread can be
7884
* started.
7985
*
80-
* @param name Optional name for the thread.
86+
* @param[in] name Optional name for the thread.
8187
*/
8288
Thread(const char *name = 0);
8389

@@ -86,12 +92,14 @@ class Thread
8692
*
8793
* This constructor fully initializes the thread object.
8894
*
89-
* @param entry
90-
* @param priority
91-
* @param stackSize
92-
* @param name Optional name for the thread.
95+
* @param[in] entry
96+
* @param[in] priority
97+
* @param[in] stackSize
98+
* @param[in] name Optional name for the thread.
99+
* @param[in] stackPtr Mandatory task stack pointer for static api usage.
93100
*/
94-
Thread(thread_entry_t entry, uint32_t priority = 0, uint32_t stackSize = 0, const char *name = 0);
101+
Thread(thread_entry_t entry, uint32_t priority = 0, uint32_t stackSize = 0, const char *name = 0,
102+
thread_stack_pointer stackPtr = 0);
95103

96104
/*!
97105
* @brief Destructor.
@@ -118,8 +126,9 @@ class Thread
118126
* @param[in] entry Entry function.
119127
* @param[in] priority Task priority.
120128
* @param[in] stackSize Stack size.
129+
* @param[in] stackPtr Mandatory task stack pointer for static api usage.
121130
*/
122-
void init(thread_entry_t entry, uint32_t priority = 0, uint32_t stackSize = 0);
131+
void init(thread_entry_t entry, uint32_t priority = 0, uint32_t stackSize = 0, thread_stack_pointer stackPtr = 0);
123132

124133
/*!
125134
* @brief This function starts thread execution.
@@ -152,7 +161,7 @@ class Thread
152161
return reinterpret_cast<thread_id_t>(m_thread->get_id());
153162
#elif ERPC_THREADS_IS(WIN32)
154163
return reinterpret_cast<thread_id_t>(m_thread);
155-
#elif ERPC_THREADS_IS(THREADX)
164+
#elif ERPC_THREADS_IS(THREADX)
156165
return reinterpret_cast<thread_id_t>(m_thread.tx_thread_id);
157166
#endif
158167
}
@@ -212,18 +221,22 @@ class Thread
212221
virtual void threadEntryPoint(void);
213222

214223
private:
215-
const char *m_name; /*!< Thread name. */
216-
thread_entry_t m_entry; /*!< Thread entry function. */
217-
void *m_arg; /*!< Entry parameter. */
218-
uint32_t m_stackSize; /*!< Stack size. */
219-
uint32_t m_priority; /*!< Task priority. */
224+
const char *m_name; /*!< Thread name. */
225+
thread_entry_t m_entry; /*!< Thread entry function. */
226+
void *m_arg; /*!< Entry parameter. */
227+
uint32_t m_stackSize; /*!< Stack size. */
228+
uint32_t m_priority; /*!< Task priority. */
229+
thread_stack_pointer m_stackPtr; /*!< Task pointer. */
220230
#if ERPC_THREADS_IS(PTHREADS)
221231
static pthread_key_t s_threadObjectKey; /*!< Thread key. */
222232
pthread_t m_thread; /*!< Current thread. */
223233
#elif ERPC_THREADS_IS(FREERTOS)
224-
TaskHandle_t m_task; /*!< Current task. */
225-
Thread *m_next; /*!< Pointer to next Thread. */
226-
static Thread *s_first; /*!< Pointer to first Thread. */
234+
TaskHandle_t m_task; /*!< Current task. */
235+
Thread *m_next; /*!< Pointer to next Thread. */
236+
static Thread *s_first; /*!< Pointer to first Thread. */
237+
#if ERPC_ALLOCATION_POLICY == ERPC_ALLOCATION_POLICY_STATIC
238+
StaticTask_t m_staticTask; /*!< Hold static task data. */
239+
#endif
227240
#elif ERPC_THREADS_IS(ZEPHYR)
228241
struct k_thread m_thread; /*!< Current thread. */
229242
k_thread_stack_t *m_stack; /*!< Pointer to stack. */
@@ -238,7 +251,7 @@ class Thread
238251
static Thread *s_first; /*!< Pointer to first Thread. */
239252
static CRITICAL_SECTION m_critical_section;
240253
static BOOL m_critical_section_inited;
241-
#elif ERPC_THREADS_IS(THREADX)
254+
#elif ERPC_THREADS_IS(THREADX)
242255
TX_THREAD m_thread; /*!< Underlying Thread instance */
243256
Thread *m_next; /*!< Pointer to next Thread. */
244257
static Thread *s_first; /*!< Pointer to first Thread. */
@@ -397,7 +410,8 @@ class Mutex
397410
#if ERPC_THREADS_IS(PTHREADS)
398411
pthread_mutex_t m_mutex; /*!< Mutex.*/
399412
#elif ERPC_THREADS_IS(FREERTOS)
400-
SemaphoreHandle_t m_mutex; /*!< Mutex.*/
413+
SemaphoreHandle_t m_mutex; /*!< Mutex.*/
414+
StaticSemaphore_t m_staticQueue; /*!< Static queue. */
401415
#elif ERPC_THREADS_IS(ZEPHYR)
402416
struct k_mutex m_mutex; /*!< Mutex.*/
403417
#elif ERPC_THREADS_IS(MBED)
@@ -484,7 +498,8 @@ class Semaphore
484498
until some predicate on shared data is satisfied. */
485499
Mutex m_mutex; /*!< Mutext. */
486500
#elif ERPC_THREADS_IS(FREERTOS)
487-
SemaphoreHandle_t m_sem; /*!< Semaphore. */
501+
SemaphoreHandle_t m_sem; /*!< Semaphore. */
502+
StaticSemaphore_t m_staticQueue; /*!< Static queue. */
488503
#elif ERPC_THREADS_IS(ZEPHYR)
489504
struct k_sem m_sem; /*!< Semaphore. */
490505
#elif ERPC_THREADS_IS(MBED)

erpc_c/port/erpc_threading_freertos.cpp

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ Thread::Thread(const char *name)
3838
{
3939
}
4040

41-
Thread::Thread(thread_entry_t entry, uint32_t priority, uint32_t stackSize, const char *name)
41+
Thread::Thread(thread_entry_t entry, uint32_t priority, uint32_t stackSize, const char *name,
42+
thread_stack_pointer stackPtr)
4243
: m_name(name)
4344
, m_entry(entry)
4445
, m_arg(0)
@@ -51,26 +52,50 @@ Thread::Thread(thread_entry_t entry, uint32_t priority, uint32_t stackSize, cons
5152

5253
Thread::~Thread(void) {}
5354

54-
void Thread::init(thread_entry_t entry, uint32_t priority, uint32_t stackSize)
55+
void Thread::init(thread_entry_t entry, uint32_t priority, uint32_t stackSize, thread_stack_pointer stackPtr)
5556
{
5657
m_entry = entry;
5758
m_stackSize = stackSize;
5859
m_priority = priority;
60+
m_stackPtr = stackPtr;
5961
}
6062

6163
void Thread::start(void *arg)
6264
{
6365
m_arg = arg;
66+
bool taskCreated = false;
6467

6568
// Enter a critical section to disable preemptive scheduling until we add the newly
6669
// created thread to the linked list. This prevents a race condition if the new thread is
6770
// higher priority than the current thread, and the new thread calls getCurrenThread(),
6871
// which will scan the linked list.
6972
taskENTER_CRITICAL();
7073

71-
if (pdPASS == xTaskCreate(threadEntryPointStub, (m_name ? m_name : "task"),
74+
#if ERPC_ALLOCATION_POLICY == ERPC_ALLOCATION_POLICY_STATIC
75+
if (m_stackPtr != NULL)
76+
{
77+
m_task =
78+
xTaskCreateStatic(threadEntryPointStub, (m_name ? m_name : "task"),
7279
((m_stackSize + sizeof(uint32_t) - 1) / sizeof(uint32_t)), // Round up number of words.
73-
this, m_priority, &m_task))
80+
this, m_priority, m_stackPtr, &m_staticTask);
81+
taskCreated = true;
82+
}
83+
#endif
84+
85+
#if configSUPPORT_DYNAMIC_ALLOCATION
86+
if (m_stackPtr == NULL)
87+
{
88+
if (pdPASS ==
89+
xTaskCreate(threadEntryPointStub, (m_name ? m_name : "task"),
90+
((m_stackSize + sizeof(uint32_t) - 1) / sizeof(uint32_t)), // Round up number of words.
91+
this, m_priority, &m_task))
92+
{
93+
taskCreated = true;
94+
}
95+
}
96+
#endif
97+
98+
if (taskCreated)
7499
{
75100
// Link in this thread to the list.
76101
if (NULL != s_first)
@@ -180,7 +205,13 @@ void Thread::threadEntryPointStub(void *arg)
180205
Mutex::Mutex(void)
181206
: m_mutex(0)
182207
{
208+
#if ERPC_ALLOCATION_POLICY == ERPC_ALLOCATION_POLICY_STATIC
209+
m_mutex = xSemaphoreCreateRecursiveMutexStatic(&m_staticQueue);
210+
#elif configSUPPORT_DYNAMIC_ALLOCATION
183211
m_mutex = xSemaphoreCreateRecursiveMutex();
212+
#else
213+
#error "Allocation method didn't match"
214+
#endif
184215
}
185216

186217
Mutex::~Mutex(void)
@@ -208,7 +239,13 @@ Semaphore::Semaphore(int count)
208239
: m_sem(0)
209240
{
210241
// Set max count to highest signed int.
242+
#if ERPC_ALLOCATION_POLICY == ERPC_ALLOCATION_POLICY_STATIC
243+
m_sem = xSemaphoreCreateCountingStatic(0x7fffffff, count, &m_staticQueue);
244+
#elif configSUPPORT_DYNAMIC_ALLOCATION
211245
m_sem = xSemaphoreCreateCounting(0x7fffffff, count);
246+
#else
247+
#error "Allocation method didn't match"
248+
#endif
212249
}
213250

214251
Semaphore::~Semaphore(void)

erpc_c/transports/erpc_rpmsg_lite_base_transport.h

100644100755
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/*
22
* Copyright (c) 2015-2016, Freescale Semiconductor, Inc.
33
* Copyright 2016 NXP
4+
* Copyright 2021 ACRIOS Systems s.r.o.
45
* All rights reserved.
56
*
67
*
@@ -11,8 +12,17 @@
1112
#define _EMBEDDED_RPC__RPMSG_LITE_BASE_TRANSPORT_H_
1213

1314
#include "erpc_transport.h"
15+
#include "erpc_config_internal.h"
1416

1517
#include "rpmsg_lite.h"
18+
#include "rpmsg_env_specific.h"
19+
20+
#if ERPC_ALLOCATION_POLICY == ERPC_ALLOCATION_POLICY_STATIC
21+
#ifndef RL_USE_STATIC_API
22+
#warning "RPMSG is not set to use static allocation"
23+
#endif
24+
#endif
25+
1626

1727
/*!
1828
* @addtogroup rpmsg_lite_transport
@@ -54,6 +64,12 @@ class RPMsgBaseTransport : public Transport
5464
protected:
5565
static struct rpmsg_lite_instance *s_rpmsg; /*!< Pointer to instance of RPMSG lite. */
5666
static uint8_t s_initialized; /*!< Represent information if the rpmsg-lite was initialized. */
67+
#if RL_USE_STATIC_API
68+
struct rpmsg_lite_instance m_static_context;
69+
struct rpmsg_lite_ept_static_context m_ept_context;
70+
rpmsg_static_queue_ctxt m_queue_context;
71+
uint8_t m_queue_stack[RL_ENV_QUEUE_STATIC_STORAGE_SIZE];
72+
#endif
5773
};
5874

5975
} // namespace erpc

erpc_c/transports/erpc_rpmsg_lite_rtos_transport.cpp

100644100755
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,23 @@ erpc_status_t RPMsgRTOSTransport::init(uint32_t src_addr, uint32_t dst_addr, voi
7878

7979
if (0U == s_initialized)
8080
{
81+
#if RL_USE_STATIC_API
82+
s_rpmsg = rpmsg_lite_master_init(base_address, length, rpmsg_link_id, RL_NO_FLAGS, &m_static_context);
83+
#else
8184
s_rpmsg = rpmsg_lite_master_init(base_address, length, rpmsg_link_id, RL_NO_FLAGS);
85+
#endif
8286
if (s_rpmsg == RL_NULL)
8387
{
8488
status = kErpcStatus_InitFailed;
8589
}
8690

8791
if (status == kErpcStatus_Success)
8892
{
93+
#if RL_USE_STATIC_API
94+
m_rpmsg_queue = rpmsg_queue_create(s_rpmsg, m_queue_stack, &m_queue_context);
95+
#else
8996
m_rpmsg_queue = rpmsg_queue_create(s_rpmsg);
97+
#endif
9098
if (m_rpmsg_queue == RL_NULL)
9199
{
92100
status = kErpcStatus_InitFailed;
@@ -95,7 +103,11 @@ erpc_status_t RPMsgRTOSTransport::init(uint32_t src_addr, uint32_t dst_addr, voi
95103

96104
if (status == kErpcStatus_Success)
97105
{
106+
#if RL_USE_STATIC_API
107+
m_rpmsg_ept = rpmsg_lite_create_ept(s_rpmsg, src_addr, rpmsg_queue_rx_cb, m_rpmsg_queue, &m_ept_context);
108+
#else
98109
m_rpmsg_ept = rpmsg_lite_create_ept(s_rpmsg, src_addr, rpmsg_queue_rx_cb, m_rpmsg_queue);
110+
#endif
99111
if (m_rpmsg_ept == RL_NULL)
100112
{
101113
status = kErpcStatus_InitFailed;
@@ -137,7 +149,11 @@ erpc_status_t RPMsgRTOSTransport::init(uint32_t src_addr, uint32_t dst_addr, voi
137149

138150
if (0U == s_initialized)
139151
{
152+
#if RL_USE_STATIC_API
153+
s_rpmsg = rpmsg_lite_remote_init(base_address, rpmsg_link_id, RL_NO_FLAGS, &m_static_context);
154+
#else
140155
s_rpmsg = rpmsg_lite_remote_init(base_address, rpmsg_link_id, RL_NO_FLAGS);
156+
#endif
141157
if (s_rpmsg == RL_NULL)
142158
{
143159
status = kErpcStatus_InitFailed;
@@ -155,7 +171,11 @@ erpc_status_t RPMsgRTOSTransport::init(uint32_t src_addr, uint32_t dst_addr, voi
155171
{
156172
}
157173

174+
#if RL_USE_STATIC_API
175+
m_rpmsg_queue = rpmsg_queue_create(s_rpmsg, m_queue_stack, &m_queue_context);
176+
#else
158177
m_rpmsg_queue = rpmsg_queue_create(s_rpmsg);
178+
#endif
159179
if (m_rpmsg_queue == RL_NULL)
160180
{
161181
status = kErpcStatus_InitFailed;
@@ -164,7 +184,11 @@ erpc_status_t RPMsgRTOSTransport::init(uint32_t src_addr, uint32_t dst_addr, voi
164184

165185
if (status == kErpcStatus_Success)
166186
{
187+
#if RL_USE_STATIC_API
188+
m_rpmsg_ept = rpmsg_lite_create_ept(s_rpmsg, src_addr, rpmsg_queue_rx_cb, m_rpmsg_queue, &m_ept_context);
189+
#else
167190
m_rpmsg_ept = rpmsg_lite_create_ept(s_rpmsg, src_addr, rpmsg_queue_rx_cb, m_rpmsg_queue);
191+
#endif
168192
if (m_rpmsg_ept == RL_NULL)
169193
{
170194
status = kErpcStatus_InitFailed;

0 commit comments

Comments
 (0)