0
mirror of https://github.com/OneOfEleven/uv-k5-firmware-custom.git synced 2025-06-20 15:08:37 +03:00

Initial commit

This commit is contained in:
OneOfEleven
2023-09-09 08:03:56 +01:00
parent 92305117f1
commit 54441e27d9
3388 changed files with 582553 additions and 0 deletions

View File

@ -0,0 +1,89 @@
/* Copyright (c) 2012 mbed.org */
#ifndef MAIL_H
#define MAIL_H
#include <stdint.h>
#include <string.h>
#include "cmsis_os.h"
namespace rtos {
/*! 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.
\tparam T data type of a single message element.
\tparam queue_sz maximum number of messages in queue.
*/
template<typename T, uint32_t queue_sz>
class Mail {
public:
/*! Create and Initialise Mail queue. */
Mail() {
#ifdef CMSIS_OS_RTX
memset(_mail_q, 0, sizeof(_mail_q));
_mail_p[0] = _mail_q;
memset(_mail_m, 0, sizeof(_mail_m));
_mail_p[1] = _mail_m;
_mail_def.pool = _mail_p;
_mail_def.queue_sz = queue_sz;
_mail_def.item_sz = sizeof(T);
#endif
_mail_id = osMailCreate(&_mail_def, NULL);
}
/*! Allocate a memory block of type T
\param millisec timeout value or 0 in case of no time-out. (default: 0).
\return pointer to memory block that can be filled with mail or NULL in case error.
*/
T* alloc(uint32_t millisec=0) {
return (T*)osMailAlloc(_mail_id, millisec);
}
/*! Allocate a memory block of type T and set memory block to zero.
\param millisec timeout value or 0 in case of no time-out. (default: 0).
\return pointer to memory block that can be filled with mail or NULL in case error.
*/
T* calloc(uint32_t millisec=0) {
return (T*)osMailCAlloc(_mail_id, millisec);
}
/*! 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.
*/
osStatus put(T *mptr) {
return osMailPut(_mail_id, (void*)mptr);
}
/*! 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.
*/
osEvent get(uint32_t millisec=osWaitForever) {
return osMailGet(_mail_id, millisec);
}
/*! Free a memory block from a mail.
\param mptr pointer to the memory block that was obtained with Mail::get.
\return status code that indicates the execution status of the function.
*/
osStatus free(T *mptr) {
return osMailFree(_mail_id, (void*)mptr);
}
private:
osMailQId _mail_id;
osMailQDef_t _mail_def;
#ifdef CMSIS_OS_RTX
uint32_t _mail_q[4+(queue_sz)];
uint32_t _mail_m[3+((sizeof(T)+3)/4)*(queue_sz)];
void *_mail_p[2];
#endif
};
}
#endif

View File

@ -0,0 +1,62 @@
/* Copyright (c) 2012 mbed.org */
#ifndef MEMORYPOOL_H
#define MEMORYPOOL_H
#include <stdint.h>
#include <string.h>
#include "cmsis_os.h"
namespace rtos {
/*! Define and manage fixed-size memory pools of objects of a given type.
\tparam T data type of a single object (element).
\tparam queue_sz maximum number of objects (elements) in the memory pool.
*/
template<typename T, uint32_t pool_sz>
class MemoryPool {
public:
/*! Create and Initialize a memory pool. */
MemoryPool() {
#ifdef CMSIS_OS_RTX
memset(_pool_m, 0, sizeof(_pool_m));
_pool_def.pool = _pool_m;
_pool_def.pool_sz = pool_sz;
_pool_def.item_sz = sizeof(T);
#endif
_pool_id = osPoolCreate(&_pool_def);
}
/*! Allocate a memory block of type T from a memory pool.
\return address of the allocated memory block or NULL in case of no memory available.
*/
T* alloc(void) {
return (T*)osPoolAlloc(_pool_id);
}
/*! Allocate a memory block of type T from a memory pool and set memory block to zero.
\return address of the allocated memory block or NULL in case of no memory available.
*/
T* calloc(void) {
return (T*)osPoolCAlloc(_pool_id);
}
/*! Return an allocated memory block back to a specific memory pool.
\param address of the allocated memory block that is returned to the memory pool.
\return status code that indicates the execution status of the function.
*/
osStatus free(T *block) {
return osPoolFree(_pool_id, (void*)block);
}
private:
osPoolId _pool_id;
osPoolDef_t _pool_def;
#ifdef CMSIS_OS_RTX
uint32_t _pool_m[3+((sizeof(T)+3)/4)*(pool_sz)];
#endif
};
}
#endif

View File

@ -0,0 +1,31 @@
#include "Mutex.h"
#include <string.h>
//#include "error.h"
namespace rtos {
Mutex::Mutex() {
#ifdef CMSIS_OS_RTX
memset(_mutex_data, 0, sizeof(_mutex_data));
_osMutexDef.mutex = _mutex_data;
#endif
_osMutexId = osMutexCreate(&_osMutexDef);
if (_osMutexId == NULL) {
// error("Error initializing the mutex object\n");
}
}
osStatus Mutex::lock(uint32_t millisec) {
return osMutexWait(_osMutexId, millisec);
}
bool Mutex::trylock() {
return (osMutexWait(_osMutexId, 0) == osOK);
}
osStatus Mutex::unlock() {
return osMutexRelease(_osMutexId);
}
}

View File

@ -0,0 +1,43 @@
/* Copyright (c) 2012 mbed.org */
#ifndef MUTEX_H
#define MUTEX_H
#include <stdint.h>
#include "cmsis_os.h"
namespace rtos {
/*! 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();
/*! Wait until a Mutex becomes available.
\param millisec timeout value or 0 in case of no time-out. (default: osWaitForever)
\return status code that indicates the execution status of the function.
*/
osStatus lock(uint32_t millisec=osWaitForever);
/*! 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
\return status code that indicates the execution status of the function.
*/
osStatus unlock();
private:
osMutexId _osMutexId;
osMutexDef_t _osMutexDef;
#ifdef CMSIS_OS_RTX
int32_t _mutex_data[3];
#endif
};
}
#endif

View File

@ -0,0 +1,61 @@
/* Copyright (c) 2012 mbed.org */
#ifndef QUEUE_H
#define QUEUE_H
#include <stdint.h>
#include <string.h>
#include "cmsis_os.h"
#include "error.h"
namespace rtos {
/*! 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.
\tparam T data type of a single message element.
\tparam 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() {
#ifdef CMSIS_OS_RTX
memset(_queue_q, 0, sizeof(_queue_q));
_queue_def.pool = _queue_q;
_queue_def.queue_sz = queue_sz;
#endif
_queue_id = osMessageCreate(&_queue_def, NULL);
if (_queue_id == NULL) {
error("Error initialising the queue object\n");
}
}
/*! 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.
*/
osStatus put(T* data, uint32_t millisec=0) {
return osMessagePut(_queue_id, (uint32_t)data, millisec);
}
/*! 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 event information that includes the message and the status code.
*/
osEvent get(uint32_t millisec=osWaitForever) {
return osMessageGet(_queue_id, millisec);
}
private:
osMessageQId _queue_id;
osMessageQDef_t _queue_def;
#ifdef CMSIS_OS_RTX
uint32_t _queue_q[4+(queue_sz)];
#endif
};
}
#endif

View File

@ -0,0 +1,28 @@
#include "RtosTimer.h"
#include <string.h>
#include "cmsis_os.h"
//#include "error.h"
namespace rtos {
RtosTimer::RtosTimer(void (*periodic_task)(void const *argument), os_timer_type type, void *argument) {
#ifdef CMSIS_OS_RTX
_timer.ptimer = periodic_task;
memset(_timer_data, 0, sizeof(_timer_data));
_timer.timer = _timer_data;
#endif
_timer_id = osTimerCreate(&_timer, type, argument);
}
osStatus RtosTimer::start(uint32_t millisec) {
return osTimerStart(_timer_id, millisec);
}
osStatus RtosTimer::stop(void) {
return osTimerStop(_timer_id);
}
}

View File

@ -0,0 +1,49 @@
/* Copyright (c) 2012 mbed.org */
#ifndef TIMER_H
#define TIMER_H
#include <stdint.h>
#include "cmsis_os.h"
namespace rtos {
/*! The RtosTimer class allow creating and and controlling of timer functions in the system.
A timer function is called when a time period expires whereby both on-shot and
periodic timers are possible. A timer can be started, restarted, or stopped.
Timers are handled in the thread osTimerThread.
Callback functions run under control of this thread and may use CMSIS-RTOS API calls.
*/
class RtosTimer {
public:
/*! Create and Start timer.
\param task name of the timer call back function.
\param type osTimerOnce for one-shot or osTimerPeriodic for periodic behaviour. (default: osTimerPeriodic)
\param argument argument to the timer call back function. (default: NULL)
*/
RtosTimer(void (*task)(void const *argument),
os_timer_type type=osTimerPeriodic,
void *argument=NULL);
/*! Stop the timer.
\return status code that indicates the execution status of the function.
*/
osStatus stop(void);
/*! start a timer.
\param millisec time delay value of the timer.
\return status code that indicates the execution status of the function.
*/
osStatus start(uint32_t millisec);
private:
osTimerId _timer_id;
osTimerDef_t _timer;
#ifdef CMSIS_OS_RTX
uint32_t _timer_data[5];
#endif
};
}
#endif

View File

@ -0,0 +1,24 @@
#include "Semaphore.h"
#include <string.h>
//#include "error.h"
namespace rtos {
Semaphore::Semaphore(int32_t count) {
#ifdef CMSIS_OS_RTX
memset(_semaphore_data, 0, sizeof(_semaphore_data));
_osSemaphoreDef.semaphore = _semaphore_data;
#endif
_osSemaphoreId = osSemaphoreCreate(&_osSemaphoreDef, count);
}
int32_t Semaphore::wait(uint32_t millisec) {
return osSemaphoreWait(_osSemaphoreId, millisec);
}
osStatus Semaphore::release(void) {
return osSemaphoreRelease(_osSemaphoreId);
}
}

View File

@ -0,0 +1,38 @@
/* Copyright (c) 2012 mbed.org */
#ifndef SEMAPHORE_H
#define SEMAPHORE_H
#include <stdint.h>
#include "cmsis_os.h"
namespace rtos {
/*! 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(int32_t count);
/*! Wait until a Semaphore resource becomes available.
\param millisec timeout value or 0 in case of no time-out. (default: osWaitForever).
\return number of available tokens, or -1 in case of incorrect parameters
*/
int32_t wait(uint32_t millisec=osWaitForever);
/*! Release a Semaphore resource that was obtain with Semaphore::wait.
\return status code that indicates the execution status of the function.
*/
osStatus release(void);
private:
osSemaphoreId _osSemaphoreId;
osSemaphoreDef_t _osSemaphoreDef;
#ifdef CMSIS_OS_RTX
uint32_t _semaphore_data[2];
#endif
};
}
#endif

View File

@ -0,0 +1,51 @@
#include "Thread.h"
namespace rtos {
Thread::Thread(void (*task)(void const *argument),
void *argument,
osPriority priority,
uint32_t stacksize) {
// The actual fields of os_thread_def are implementation specific in every CMSIS-RTOS
#ifdef CMSIS_OS_RTX
_thread_def.pthread = task;
_thread_def.tpriority = priority;
_thread_def.instances = 1;
_thread_def.stacksize = stacksize;
#endif
_tid = osThreadCreate(&_thread_def, argument);
}
osStatus Thread::terminate() {
return osThreadTerminate(_tid);
}
osStatus Thread::set_priority(osPriority priority) {
return osThreadSetPriority(_tid, priority);
}
osPriority Thread::get_priority() {
return osThreadGetPriority(_tid);
}
int32_t Thread::signal_set(int32_t signals) {
return osSignalSet(_tid, signals);
}
osEvent Thread::signal_wait(int32_t signals, uint32_t millisec) {
return osSignalWait(signals, millisec);
}
osStatus Thread::wait(uint32_t millisec) {
return osDelay(millisec);
}
osStatus Thread::yield() {
return osThreadYield();
}
osThreadId Thread::gettid() {
return osThreadGetId();
}
}

View File

@ -0,0 +1,78 @@
/* Copyright (c) 2012 mbed.org */
#ifndef THREAD_H
#define THREAD_H
#include <stdint.h>
#include "cmsis_os.h"
#define DEFAULT_STACK_SIZE 0x1000
namespace rtos {
/*! The Thread class allow defining, creating, and controlling thread functions in the system. */
class Thread {
public:
/*! Create a new thread, and start it executing the specified function.
\param task function to be executed by this thread.
\param argument pointer that is passed to the thread function as start argument. (default: NULL).
\param priority initial priority of the thread function. (default: osPriorityNormal).
\param stacksz stack size (in bytes) requirements for the thread function. (default: DEFAULT_STACK_SIZE).
*/
Thread(void (*task)(void const *argument),
void *argument=NULL,
osPriority priority=osPriorityNormal,
uint32_t stacksize=DEFAULT_STACK_SIZE);
/*! Terminate execution of a thread and remove it from Active Threads
\return status code that indicates the execution status of the function.
*/
osStatus terminate();
/*! Set priority of an active thread
\param priority new priority value for the thread function.
\return status code that indicates the execution status of the function.
*/
osStatus set_priority(osPriority priority);
/*! Get priority of an active thread
\ return current priority value of the thread function.
*/
osPriority get_priority();
/*! Set the specified Signal Flags of an active thread.
\param signals specifies the signal flags of the thread that should be set.
\return previous signal flags of the specified thread or 0x80000000 in case of incorrect parameters.
*/
int32_t signal_set(int32_t signals);
/*! Wait for one or more Signal Flags to become signaled for the current RUNNING thread.
\param signals wait until all specified signal flags set or 0 for any single signal flag.
\param millisec timeout value or 0 in case of no time-out. (default: osWaitForever).
\return event flag information or error code.
*/
static osEvent signal_wait(int32_t signals, uint32_t millisec=osWaitForever);
/*! Wait for a specified time period in millisec:
\param millisec time delay value
\return status code that indicates the execution status of the function.
*/
static osStatus wait(uint32_t millisec);
/*! Pass control to next thread that is in state READY.
\return status code that indicates the execution status of the function.
*/
static osStatus yield();
/*! Get the thread id of the current running thread.
\return thread ID for reference by other functions or NULL in case of error.
*/
static osThreadId gettid();
private:
osThreadId _tid;
osThreadDef_t _thread_def;
};
}
#endif

View File

@ -0,0 +1,17 @@
/* mbed Microcontroller Library
* Copyright (c) 2006-2012 ARM Limited. All rights reserved.
*/
#ifndef RTOS_H
#define RTOS_H
#include "Thread.h"
#include "Mutex.h"
#include "RtosTimer.h"
#include "Semaphore.h"
#include "Mail.h"
#include "MemoryPool.h"
#include "Queue.h"
using namespace rtos;
#endif