This commit is contained in:
2024-08-05 20:57:09 +08:00
commit 46d9ee7795
3020 changed files with 1725767 additions and 0 deletions

View File

@@ -0,0 +1,8 @@
from building import *
cwd = GetCurrentDir()
src = Glob('*.cpp')
CPPPATH = [cwd]
group = DefineGroup('CPP', src, depend=['RT_USING_CPP_WRAPPER'], CPPPATH=CPPPATH)
Return('group')

View File

@@ -0,0 +1,44 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
*/
#include "cxx_mutex.h"
using namespace rtthread;
Mutex::Mutex(const char *name)
{
rt_mutex_init(&mID, name, RT_IPC_FLAG_PRIO);
}
bool Mutex::lock(int32_t millisec)
{
rt_int32_t tick;
if (millisec < 0)
tick = -1;
else
tick = rt_tick_from_millisecond(millisec);
return rt_mutex_take(&mID, tick) == RT_EOK;
}
bool Mutex::trylock()
{
return lock(0);
}
void Mutex::unlock()
{
rt_mutex_release(&mID);
}
Mutex::~Mutex()
{
rt_mutex_detach(&mID);
}

View File

@@ -0,0 +1,39 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
*/
#include "cxx_semaphore.h"
using namespace rtthread;
Semaphore::Semaphore(const char *name, int32_t count)
{
rt_sem_init(&mID, name, count, RT_IPC_FLAG_FIFO);
}
bool Semaphore::wait(int32_t millisec)
{
rt_int32_t tick;
if (millisec < 0)
tick = -1;
else
tick = rt_tick_from_millisecond(millisec);
return rt_sem_take(&mID, tick) == RT_EOK;
}
void Semaphore::release(void)
{
rt_sem_release(&mID);
}
Semaphore::~Semaphore()
{
rt_sem_detach(&mID);
}

View File

@@ -0,0 +1,117 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
*/
#include "cxx_thread.h"
using namespace rtthread;
Thread::Thread(rt_uint32_t stack_size,
rt_uint8_t priority,
rt_uint32_t tick,
const char *name)
: _entry(RT_NULL), _param(RT_NULL), started(false)
{
rt_event_init(&_event, name, 0);
_thread = rt_thread_create(name,
(thread_func_t)func,
this,
stack_size,
priority,
tick);
}
Thread::Thread(void (*entry)(void *p),
void *p,
rt_uint32_t stack_size,
rt_uint8_t priority,
rt_uint32_t tick,
const char *name)
: _entry(entry), _param(p), started(false)
{
rt_event_init(&_event, name, 0);
_thread = rt_thread_create(name,
(thread_func_t)func,
this,
stack_size,
priority,
tick);
}
Thread::~Thread()
{
rt_event_detach(&_event);
rt_thread_delete(_thread);
}
bool Thread::start()
{
if (rt_thread_startup(_thread) == RT_EOK)
{
started = true;
}
return started;
}
void Thread::sleep(int32_t millisec)
{
rt_int32_t tick;
if (millisec < 0)
tick = 1;
else
tick = rt_tick_from_millisecond(millisec);
rt_thread_delay(tick);
}
void Thread::func(Thread *pThis)
{
if (pThis->_entry != RT_NULL)
{
pThis->_entry(pThis->_param);
}
else
{
pThis->run(pThis->_param);
}
rt_event_send(&pThis->_event, 1);
}
void Thread::run(void *parameter)
{
/* please overload this method */
}
rt_err_t Thread::wait(int32_t millisec)
{
return join(millisec);
}
rt_err_t Thread::join(int32_t millisec)
{
if (started)
{
rt_int32_t tick;
if (millisec < 0)
tick = -1;
else
tick = rt_tick_from_millisecond(millisec);
return rt_event_recv(&_event, 1, RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, tick, RT_NULL);
}
else
{
return -RT_ENOSYS;
}
}

View File

@@ -0,0 +1,28 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2016/10/1 Bernard The first version
*/
#pragma once
#include <stdint.h>
#include <string.h>
namespace rtthread {
class Lock
{
public:
Lock(Mutex& mutex) : m(mutex) {m.lock();}
~Lock() {m.unlock();}
protected:
Mutex &m;
};
}

View File

@@ -0,0 +1,81 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2016/10/1 Bernard The first version
*/
#pragma once
#include <stdint.h>
#include <string.h>
#include <rtthread.h>
namespace rtthread {
/**
* The Mail class allow to control, send, receive, or wait for mail.
* A mail is a memory block that is send to a thread or interrupt service routine.
* @param T data type of a single message element.
* @param queue_sz maximum number of messages in queue.
*/
template<typename T, uint32_t queue_sz>
class Mail {
public:
/** Create and Initialise Mail queue. */
Mail(const char *name = "")
{
rt_mb_init(&mID, name, mPool, queue_sz, RT_IPC_FLAG_FIFO);
}
~Mail()
{
rt_mb_detach(&mID);
}
/** Put a mail in the queue.
@param mptr memory block previously allocated with Mail::alloc or Mail::calloc.
@return status code that indicates the execution status of the function.
*/
bool put(T *mptr, int32_t millisec = 0)
{
rt_int32_t tick;
if (millisec < 0)
tick = -1;
else
tick = rt_tick_from_millisecond(millisec);
return rt_mb_send_wait(&mID, (rt_ubase_t)mptr, tick) == RT_EOK;
}
/** Get a mail from a queue.
@param millisec timeout value or 0 in case of no time-out. (default: osWaitForever).
@return event that contains mail information or error code.
*/
T* get(int32_t millisec = -1)
{
T *t = NULL;
rt_int32_t tick;
if (millisec < 0)
tick = -1;
else
tick = rt_tick_from_millisecond(millisec);
rt_mb_recv(&mID, &t, tick);
return t;
}
private:
struct rt_mailbox mID;
T* mPool[queue_sz];
};
}

View File

@@ -0,0 +1,46 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2016/10/1 Bernard The first version
*/
#pragma once
#include <stdint.h>
#include <rtthread.h>
namespace rtthread {
/** The Mutex class is used to synchronise the execution of threads.
This is for example used to protect access to a shared resource.
*/
class Mutex {
public:
/** Create and Initialize a Mutex object */
Mutex(const char *name = "mutex");
~Mutex();
/** Wait until a Mutex becomes available.
@param millisec timeout value or 0 in case of no time-out. (default: WaitForever)
@return true if the mutex was acquired, false otherwise.
*/
bool lock(int32_t millisec = -1);
/** Try to lock the mutex, and return immediately
@return true if the mutex was acquired, false otherwise.
*/
bool trylock();
/** Unlock the mutex that has previously been locked by the same thread
*/
void unlock();
private:
struct rt_mutex mID;
};
}

View File

@@ -0,0 +1,74 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2016/10/1 Bernard The first version
*/
#pragma once
#include <stdint.h>
#include <string.h>
#include <rtthread.h>
namespace rtthread {
/**
* The Queue class allow to control, send, receive, or wait for messages.
* A message can be a integer or pointer value to a certain type T that is send
* to a thread or interrupt service routine.
* @param T data type of a single message element.
* @param queue_sz maximum number of messages in queue.
*/
template<typename T, uint32_t queue_sz>
class Queue
{
public:
/** Create and initialise a message Queue. */
Queue()
{
rt_mq_init(&mID, "mq", mPool, sizeof(T), sizeof(mPool), RT_IPC_FLAG_FIFO);
};
~Queue()
{
rt_mq_detach(&mID);
};
/** Put a message in a Queue.
@param data message pointer.
@param millisec timeout value or 0 in case of no time-out. (default: 0)
@return status code that indicates the execution status of the function.
*/
rt_err_t put(T& data, int32_t millisec = 0)
{
return rt_mq_send(&mID, &data, sizeof(data));
}
/** Get a message or Wait for a message from a Queue.
@param millisec timeout value or 0 in case of no time-out. (default: osWaitForever).
@return bool .
*/
bool get(T& data, int32_t millisec = WAIT_FOREVER)
{
rt_int32_t tick;
if (millisec < 0)
tick = -1;
else
tick = rt_tick_from_millisecond(millisec);
return rt_mq_recv(&mID, &data, sizeof(data), tick) >= 0;
}
private:
struct rt_messagequeue mID;
char mPool[(sizeof(void *) + RT_ALIGN(sizeof(T), RT_ALIGN_SIZE)) * queue_sz];
};
}

View File

@@ -0,0 +1,42 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2016/10/1 Bernard The first version
*/
#pragma once
#include <stdint.h>
#include <rtthread.h>
namespace rtthread {
/** The Semaphore class is used to manage and protect access to a set of shared resources. */
class Semaphore
{
public:
/** Create and Initialize a Semaphore object used for managing resources.
@param number of available resources; maximum index value is (count-1).
*/
Semaphore(const char *name = "sem", int32_t count = 0);
~Semaphore();
/** Wait until a Semaphore resource becomes available.
@param millisec timeout value or 0 in case of no time-out.
@return true on success.
*/
bool wait(int32_t millisec = -1);
/** Release a Semaphore resource that was obtain with Semaphore::wait.
*/
void release(void);
private:
struct rt_semaphore mID;
};
}

View File

@@ -0,0 +1,67 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2016/10/1 Bernard The first version
*/
#pragma once
#include <stdint.h>
#include <rtthread.h>
namespace rtthread
{
/** The Thread class allow defining, creating, and controlling thread functions in the system. */
class Thread
{
public:
typedef void (*thread_func_t)(void *param);
/** Allocate a new thread without starting execution
@param priority initial priority of the thread function. (default: osPriorityNormal).
@param stack_size stack size (in bytes) requirements for the thread function. (default: DEFAULT_STACK_SIZE).
@param stack_pointer pointer to the stack area to be used by this thread (default: NULL).
*/
Thread(rt_uint32_t stack_size = 2048,
rt_uint8_t priority = (RT_THREAD_PRIORITY_MAX * 2) / 3,
rt_uint32_t tick = 20,
const char *name = "th");
Thread(void (*entry)(void *p),
void *p = RT_NULL,
rt_uint32_t stack_size = 2048,
rt_uint8_t priority = (RT_THREAD_PRIORITY_MAX * 2) / 3,
rt_uint32_t tick = 20,
const char *name = "th");
virtual ~Thread();
bool start();
static void sleep(int32_t millisec);
rt_err_t wait(int32_t millisec);
rt_err_t join(int32_t millisec = -1);
protected:
virtual void run(void *parameter);
private:
static void func(Thread *pThis);
rt_thread_t _thread;
thread_func_t _entry;
void *_param;
/* event for thread join */
struct rt_event _event;
bool started;
};
}