Compare commits
2 Commits
8e96588829
...
c335211ef8
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c335211ef8 | ||
|
|
3dbd04a3eb |
120
Threads/Inc/threads_thread.hpp
Normal file
120
Threads/Inc/threads_thread.hpp
Normal file
@ -0,0 +1,120 @@
|
||||
/*
|
||||
* tasks_task.hpp
|
||||
*
|
||||
* Created on: Mar 18, 2021
|
||||
* Author: erki
|
||||
*/
|
||||
|
||||
#ifndef SKULLC_THREADS_THREAD_HPP_
|
||||
#define SKULLC_THREADS_THREAD_HPP_
|
||||
|
||||
#include <cmsis_os.h>
|
||||
|
||||
#include <freertos_os2.h>
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <tuple>
|
||||
|
||||
#include <peripherals_utility.hpp>
|
||||
|
||||
namespace Threads
|
||||
{
|
||||
|
||||
template<typename CRTP>
|
||||
class Thread
|
||||
{
|
||||
public:
|
||||
Thread(const osThreadAttr_t attrs)
|
||||
: attributes(attrs)
|
||||
{
|
||||
auto f = [](void* d) {
|
||||
CRTP* dd = static_cast<CRTP*>(d);
|
||||
(*dd)();
|
||||
};
|
||||
|
||||
thread_id = osThreadNew(f, this, &attributes);
|
||||
assert(thread_id != nullptr);
|
||||
}
|
||||
|
||||
Thread(const char* name, const osPriority_t priority, const std::uint32_t stack_size)
|
||||
: Thread(constructThreadAttrs_(name, priority, stack_size))
|
||||
{}
|
||||
|
||||
Thread(const Thread&) = delete;
|
||||
Thread(Thread&&) = delete;
|
||||
|
||||
osThreadId_t thread_id;
|
||||
osThreadAttr_t attributes;
|
||||
|
||||
constexpr TaskHandle_t taskHandle() const
|
||||
{
|
||||
return static_cast<TaskHandle_t>(thread_id);
|
||||
}
|
||||
|
||||
[[nodiscard]] BaseType_t notify(const long value, const eNotifyAction action)
|
||||
{
|
||||
return xTaskNotify(taskHandle(), value, action);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @returns Tuple. First member indicating the return value of the underlying notify
|
||||
* function, the second member indicating whether a higher priority task was awoken.
|
||||
*
|
||||
*/
|
||||
[[nodiscard]] std::pair<BaseType_t, BaseType_t> notifyFromIsr(const long value, const eNotifyAction action)
|
||||
{
|
||||
BaseType_t was_notified = pdFALSE;
|
||||
|
||||
const BaseType_t rval = xTaskNotifyFromISR(taskHandle(), value, action, &was_notified);
|
||||
|
||||
return {rval, was_notified};
|
||||
}
|
||||
|
||||
void yield()
|
||||
{
|
||||
osThreadYield();
|
||||
}
|
||||
|
||||
void sleep(const unsigned long delay)
|
||||
{
|
||||
osDelay(delay);
|
||||
}
|
||||
|
||||
void sleepUntil(const unsigned long deadline)
|
||||
{
|
||||
osDelayUntil(deadline);
|
||||
}
|
||||
|
||||
protected:
|
||||
bool notifyWait(const TickType_t delay) const
|
||||
{
|
||||
return xTaskNotifyWait(0, std::numeric_limits<unsigned long>::max(), nullptr, delay);
|
||||
}
|
||||
|
||||
std::pair<bool, unsigned long> notifyWait(const TickType_t delay, const unsigned long set_on_entry, const unsigned long set_on_exit) const
|
||||
{
|
||||
unsigned long notification_value = 0;
|
||||
const bool rval = xTaskNotifyWait(set_on_entry, set_on_exit, ¬ification_value, delay);
|
||||
|
||||
return {rval, notification_value};
|
||||
}
|
||||
|
||||
private:
|
||||
osThreadAttr_t constructThreadAttrs_(const char* name, const osPriority_t priority, const std::uint32_t stack_size)
|
||||
{
|
||||
auto attrs = Peripherals::zeroInitialized<osThreadAttr_t>();
|
||||
|
||||
attrs.name = name;
|
||||
attrs.priority = priority;
|
||||
attrs.stack_size = stack_size;
|
||||
|
||||
return attrs;
|
||||
}
|
||||
};
|
||||
|
||||
}// namespace Threads
|
||||
|
||||
|
||||
#endif /* SKULLC_THREADS_THREAD_HPP_ */
|
||||
57
Utility/Inc/utility_staticpointer.hpp
Normal file
57
Utility/Inc/utility_staticpointer.hpp
Normal file
@ -0,0 +1,57 @@
|
||||
/*
|
||||
* utility_staticpointer.hpp
|
||||
*
|
||||
* Created on: Jun 8, 2021
|
||||
* Author: erki
|
||||
*/
|
||||
|
||||
#ifndef SKULLC_UTILITY_STATICPOINTER_HPP_
|
||||
#define SKULLC_UTILITY_STATICPOINTER_HPP_
|
||||
|
||||
#include <utility>
|
||||
|
||||
namespace Utility
|
||||
{
|
||||
|
||||
template<typename T>
|
||||
struct StaticPointer
|
||||
{
|
||||
using value_type = T;
|
||||
|
||||
alignas(value_type) unsigned char storage[sizeof(value_type)];
|
||||
|
||||
template<class... Args>
|
||||
value_type& setup(Args&&... args)
|
||||
{
|
||||
initialized_ = true;
|
||||
return *(new (storage) value_type(std::forward<Args>(args)...));
|
||||
}
|
||||
|
||||
value_type& operator*() const
|
||||
{
|
||||
return reinterpret_cast<T*>(storage);
|
||||
}
|
||||
|
||||
value_type* operator->() const noexcept
|
||||
{
|
||||
return reinterpret_cast<T*>(storage);
|
||||
}
|
||||
|
||||
constexpr explicit operator bool() const
|
||||
{
|
||||
return isInitialized();
|
||||
}
|
||||
|
||||
constexpr bool isInitialized() const
|
||||
{
|
||||
return initialized_;
|
||||
}
|
||||
|
||||
private:
|
||||
bool initialized_ = false;
|
||||
};
|
||||
|
||||
}// namespace Utility
|
||||
|
||||
|
||||
#endif /* SKULLC_UTILITY_STATICPOINTER_HPP_ */
|
||||
Loading…
x
Reference in New Issue
Block a user