feat: add lab-rv32i-freertos-integration card
This commit is contained in:
@@ -0,0 +1,151 @@
|
||||
#ifndef FREERTOS_CPP_EVENT_GROUP_HPP
|
||||
#define FREERTOS_CPP_EVENT_GROUP_HPP
|
||||
|
||||
#include <stdint.h>
|
||||
#include <type_traits>
|
||||
|
||||
extern "C"
|
||||
{
|
||||
#include "FreeRTOS.h"
|
||||
#include "event_groups.h"
|
||||
}
|
||||
|
||||
namespace freertos
|
||||
{
|
||||
|
||||
enum class WaitMode : uint8_t
|
||||
{
|
||||
any,
|
||||
all
|
||||
};
|
||||
|
||||
enum class ClearMode : uint8_t
|
||||
{
|
||||
keep,
|
||||
clear_on_exit
|
||||
};
|
||||
|
||||
template< typename Bit >
|
||||
class EventMask final
|
||||
{
|
||||
static_assert( std::is_enum< Bit >::value,
|
||||
"EventMask requires an enum bit type" );
|
||||
|
||||
public:
|
||||
constexpr EventMask() noexcept = default;
|
||||
explicit constexpr EventMask( Bit bit ) noexcept
|
||||
: value_ { static_cast< EventBits_t >( bit ) }
|
||||
{
|
||||
}
|
||||
|
||||
[[nodiscard]] static constexpr EventMask from_raw(
|
||||
EventBits_t value ) noexcept
|
||||
{
|
||||
return EventMask { value, RawTag {} };
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr EventBits_t raw() const noexcept { return value_; }
|
||||
[[nodiscard]] constexpr bool contains( EventMask other ) const noexcept
|
||||
{
|
||||
return ( value_ & other.value_ ) == other.value_;
|
||||
}
|
||||
|
||||
friend constexpr EventMask operator|( EventMask left,
|
||||
EventMask right ) noexcept
|
||||
{
|
||||
return from_raw( left.value_ | right.value_ );
|
||||
}
|
||||
|
||||
private:
|
||||
struct RawTag {};
|
||||
constexpr EventMask( EventBits_t value, RawTag ) noexcept : value_ { value } {}
|
||||
EventBits_t value_ {};
|
||||
};
|
||||
|
||||
template< typename Bit >
|
||||
struct WaitResult final
|
||||
{
|
||||
EventMask< Bit > observed {};
|
||||
|
||||
[[nodiscard]] constexpr bool satisfied( EventMask< Bit > wanted,
|
||||
WaitMode mode ) const noexcept
|
||||
{
|
||||
const EventBits_t matched = observed.raw() & wanted.raw();
|
||||
return mode == WaitMode::all ? matched == wanted.raw() : matched != 0U;
|
||||
}
|
||||
};
|
||||
|
||||
struct StaticEventGroupStorage final
|
||||
{
|
||||
StaticEventGroup_t control {};
|
||||
};
|
||||
|
||||
template< typename Bit >
|
||||
class EventGroup final
|
||||
{
|
||||
static_assert( std::is_enum< Bit >::value,
|
||||
"EventGroup requires an enum bit type" );
|
||||
|
||||
public:
|
||||
explicit EventGroup( StaticEventGroupStorage & storage ) noexcept
|
||||
: handle_ { xEventGroupCreateStatic( &storage.control ) }
|
||||
{
|
||||
}
|
||||
|
||||
~EventGroup() noexcept
|
||||
{
|
||||
if( handle_ != nullptr ) vEventGroupDelete( handle_ );
|
||||
}
|
||||
|
||||
EventGroup( const EventGroup & ) = delete;
|
||||
EventGroup & operator=( const EventGroup & ) = delete;
|
||||
EventGroup( EventGroup && ) = delete;
|
||||
EventGroup & operator=( EventGroup && ) = delete;
|
||||
|
||||
[[nodiscard]] bool valid() const noexcept { return handle_ != nullptr; }
|
||||
|
||||
[[nodiscard]] EventMask< Bit > set( EventMask< Bit > bits ) noexcept
|
||||
{
|
||||
return EventMask< Bit >::from_raw(
|
||||
xEventGroupSetBits( handle_, bits.raw() ) );
|
||||
}
|
||||
|
||||
[[nodiscard]] EventMask< Bit > clear( EventMask< Bit > bits ) noexcept
|
||||
{
|
||||
return EventMask< Bit >::from_raw(
|
||||
xEventGroupClearBits( handle_, bits.raw() ) );
|
||||
}
|
||||
|
||||
[[nodiscard]] EventMask< Bit > bits() const noexcept
|
||||
{
|
||||
return EventMask< Bit >::from_raw( xEventGroupGetBits( handle_ ) );
|
||||
}
|
||||
|
||||
[[nodiscard]] WaitResult< Bit > wait( EventMask< Bit > wanted,
|
||||
WaitMode mode,
|
||||
ClearMode clear,
|
||||
TickType_t timeout ) noexcept
|
||||
{
|
||||
const EventBits_t result = xEventGroupWaitBits(
|
||||
handle_, wanted.raw(),
|
||||
clear == ClearMode::clear_on_exit ? pdTRUE : pdFALSE,
|
||||
mode == WaitMode::all ? pdTRUE : pdFALSE,
|
||||
timeout );
|
||||
return { EventMask< Bit >::from_raw( result ) };
|
||||
}
|
||||
|
||||
[[nodiscard]] EventGroupHandle_t native_handle() const noexcept
|
||||
{
|
||||
return handle_;
|
||||
}
|
||||
|
||||
private:
|
||||
EventGroupHandle_t handle_ {};
|
||||
};
|
||||
|
||||
static_assert( !std::is_copy_constructible<
|
||||
EventGroup< WaitMode > >::value );
|
||||
|
||||
} // namespace freertos
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,278 @@
|
||||
#ifndef FREERTOS_ISR_DRIVERS_HPP
|
||||
#define FREERTOS_ISR_DRIVERS_HPP
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <type_traits>
|
||||
|
||||
extern "C"
|
||||
{
|
||||
#include "FreeRTOS.h"
|
||||
#include "queue.h"
|
||||
#include "task.h"
|
||||
}
|
||||
|
||||
namespace freertos
|
||||
{
|
||||
|
||||
class IsrContext final
|
||||
{
|
||||
public:
|
||||
void merge_wake_request( BaseType_t local_request ) noexcept
|
||||
{
|
||||
if( local_request != pdFALSE )
|
||||
{
|
||||
higher_priority_task_woken_ = pdTRUE;
|
||||
}
|
||||
}
|
||||
|
||||
[[nodiscard]] bool wake_requested() const noexcept
|
||||
{
|
||||
return higher_priority_task_woken_ != pdFALSE;
|
||||
}
|
||||
|
||||
[[nodiscard]] std::uint32_t yield_calls() const noexcept
|
||||
{
|
||||
return yield_calls_;
|
||||
}
|
||||
|
||||
[[nodiscard]] bool yield_if_needed() noexcept
|
||||
{
|
||||
++yield_calls_;
|
||||
bool const requested = wake_requested();
|
||||
portYIELD_FROM_ISR( higher_priority_task_woken_ );
|
||||
return requested;
|
||||
}
|
||||
|
||||
private:
|
||||
BaseType_t higher_priority_task_woken_{ pdFALSE };
|
||||
std::uint32_t yield_calls_{ 0U };
|
||||
};
|
||||
|
||||
enum class IsrSendResult
|
||||
{
|
||||
queued,
|
||||
dropped_newest
|
||||
};
|
||||
|
||||
template< typename T, std::size_t Capacity >
|
||||
class StaticIsrQueue final
|
||||
{
|
||||
static_assert( Capacity > 0U );
|
||||
static_assert( std::is_trivially_copyable_v< T > );
|
||||
|
||||
public:
|
||||
StaticIsrQueue( StaticQueue_t & control, std::uint8_t * bytes ) noexcept
|
||||
: handle_( xQueueCreateStatic( static_cast< UBaseType_t >( Capacity ),
|
||||
static_cast< UBaseType_t >( sizeof( T ) ),
|
||||
bytes,
|
||||
&control ) )
|
||||
{
|
||||
}
|
||||
|
||||
StaticIsrQueue( const StaticIsrQueue & ) = delete;
|
||||
StaticIsrQueue & operator=( const StaticIsrQueue & ) = delete;
|
||||
|
||||
[[nodiscard]] bool valid() const noexcept
|
||||
{
|
||||
return handle_ != nullptr;
|
||||
}
|
||||
|
||||
[[nodiscard]] IsrSendResult send_from_isr( const T & item,
|
||||
IsrContext & context ) noexcept
|
||||
{
|
||||
BaseType_t local_wake = pdFALSE;
|
||||
BaseType_t const result = xQueueSendFromISR( handle_, &item, &local_wake );
|
||||
context.merge_wake_request( local_wake );
|
||||
return result == pdPASS ? IsrSendResult::queued
|
||||
: IsrSendResult::dropped_newest;
|
||||
}
|
||||
|
||||
[[nodiscard]] bool receive( T & item, TickType_t timeout ) noexcept
|
||||
{
|
||||
return xQueueReceive( handle_, &item, timeout ) == pdPASS;
|
||||
}
|
||||
|
||||
private:
|
||||
QueueHandle_t handle_;
|
||||
};
|
||||
|
||||
class TaskNotificationRef final
|
||||
{
|
||||
public:
|
||||
explicit constexpr TaskNotificationRef( TaskHandle_t task ) noexcept
|
||||
: task_( task )
|
||||
{
|
||||
}
|
||||
|
||||
[[nodiscard]] bool give_from_isr( IsrContext & context ) const noexcept
|
||||
{
|
||||
BaseType_t local_wake = pdFALSE;
|
||||
vTaskNotifyGiveFromISR( task_, &local_wake );
|
||||
context.merge_wake_request( local_wake );
|
||||
return true;
|
||||
}
|
||||
|
||||
private:
|
||||
TaskHandle_t task_;
|
||||
};
|
||||
|
||||
struct UartRegisters
|
||||
{
|
||||
volatile std::uint32_t status;
|
||||
volatile std::uint32_t rx_data[ 2 ];
|
||||
volatile std::uint32_t rx_count;
|
||||
volatile std::uint32_t control;
|
||||
};
|
||||
|
||||
class UartTaskView final
|
||||
{
|
||||
public:
|
||||
explicit constexpr UartTaskView( UartRegisters & registers ) noexcept
|
||||
: registers_( ®isters )
|
||||
{
|
||||
}
|
||||
|
||||
void enable_rx_irq() const noexcept { registers_->control = 1U; }
|
||||
|
||||
void inject_rx_pair_for_test( std::uint8_t first,
|
||||
std::uint8_t second ) const noexcept
|
||||
{
|
||||
registers_->rx_data[ 0 ] = first;
|
||||
registers_->rx_data[ 1 ] = second;
|
||||
registers_->rx_count = 2U;
|
||||
registers_->status = 1U;
|
||||
}
|
||||
|
||||
private:
|
||||
UartRegisters * registers_;
|
||||
};
|
||||
|
||||
class UartIsrView final
|
||||
{
|
||||
public:
|
||||
explicit constexpr UartIsrView( UartRegisters & registers ) noexcept
|
||||
: registers_( ®isters )
|
||||
{
|
||||
}
|
||||
|
||||
[[nodiscard]] bool rx_ready_from_isr() const noexcept
|
||||
{
|
||||
return ( registers_->status & 1U ) != 0U &&
|
||||
registers_->rx_count != 0U;
|
||||
}
|
||||
|
||||
[[nodiscard]] std::uint8_t read_rx_from_isr() const noexcept
|
||||
{
|
||||
configASSERT( rx_ready_from_isr() );
|
||||
std::uint8_t const byte =
|
||||
static_cast< std::uint8_t >( registers_->rx_data[ 0 ] );
|
||||
registers_->rx_data[ 0 ] = registers_->rx_data[ 1 ];
|
||||
--registers_->rx_count;
|
||||
if( registers_->rx_count == 0U )
|
||||
{
|
||||
registers_->status = 0U;
|
||||
}
|
||||
return byte;
|
||||
}
|
||||
|
||||
private:
|
||||
UartRegisters * registers_;
|
||||
};
|
||||
|
||||
class Uart final
|
||||
{
|
||||
public:
|
||||
explicit constexpr Uart( UartRegisters & registers ) noexcept
|
||||
: registers_( ®isters ) {}
|
||||
|
||||
[[nodiscard]] UartTaskView task_view() const noexcept
|
||||
{
|
||||
return UartTaskView{ *registers_ };
|
||||
}
|
||||
|
||||
[[nodiscard]] UartIsrView isr_view() const noexcept
|
||||
{
|
||||
return UartIsrView{ *registers_ };
|
||||
}
|
||||
|
||||
private:
|
||||
UartRegisters * registers_;
|
||||
};
|
||||
|
||||
struct GpioRegisters
|
||||
{
|
||||
volatile std::uint32_t direction;
|
||||
volatile std::uint32_t data;
|
||||
volatile std::uint32_t edge_status;
|
||||
};
|
||||
|
||||
class GpioTaskView final
|
||||
{
|
||||
public:
|
||||
explicit constexpr GpioTaskView( GpioRegisters & registers ) noexcept
|
||||
: registers_( ®isters ) {}
|
||||
|
||||
void configure_output( std::uint32_t mask ) const noexcept
|
||||
{
|
||||
registers_->direction |= mask;
|
||||
}
|
||||
|
||||
void write_output( std::uint32_t mask, bool high ) const noexcept
|
||||
{
|
||||
if( high ) { registers_->data |= mask; }
|
||||
else { registers_->data &= ~mask; }
|
||||
}
|
||||
|
||||
void inject_edge_for_test( std::uint32_t mask ) const noexcept
|
||||
{
|
||||
registers_->edge_status |= mask;
|
||||
}
|
||||
|
||||
private:
|
||||
GpioRegisters * registers_;
|
||||
};
|
||||
|
||||
class GpioIsrView final
|
||||
{
|
||||
public:
|
||||
explicit constexpr GpioIsrView( GpioRegisters & registers ) noexcept
|
||||
: registers_( ®isters ) {}
|
||||
|
||||
[[nodiscard]] bool edge_pending_from_isr( std::uint32_t mask ) const noexcept
|
||||
{
|
||||
return ( registers_->edge_status & mask ) != 0U;
|
||||
}
|
||||
|
||||
void clear_edge_from_isr( std::uint32_t mask ) const noexcept
|
||||
{
|
||||
registers_->edge_status &= ~mask;
|
||||
}
|
||||
|
||||
private:
|
||||
GpioRegisters * registers_;
|
||||
};
|
||||
|
||||
class GpioPin final
|
||||
{
|
||||
public:
|
||||
explicit constexpr GpioPin( GpioRegisters & registers ) noexcept
|
||||
: registers_( ®isters ) {}
|
||||
|
||||
[[nodiscard]] GpioTaskView task_view() const noexcept
|
||||
{
|
||||
return GpioTaskView{ *registers_ };
|
||||
}
|
||||
|
||||
[[nodiscard]] GpioIsrView isr_view() const noexcept
|
||||
{
|
||||
return GpioIsrView{ *registers_ };
|
||||
}
|
||||
|
||||
private:
|
||||
GpioRegisters * registers_;
|
||||
};
|
||||
|
||||
} // namespace freertos
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,132 @@
|
||||
#ifndef FREERTOS_CPP_KERNEL_HPP
|
||||
#define FREERTOS_CPP_KERNEL_HPP
|
||||
|
||||
#include <stdint.h>
|
||||
#include <type_traits>
|
||||
|
||||
extern "C"
|
||||
{
|
||||
#include "FreeRTOS.h"
|
||||
#include "task.h"
|
||||
}
|
||||
|
||||
#if defined( __GNUC__ )
|
||||
#define FREERTOS_CPP_ALWAYS_INLINE inline __attribute__( ( always_inline ) )
|
||||
#else
|
||||
#define FREERTOS_CPP_ALWAYS_INLINE inline
|
||||
#endif
|
||||
|
||||
namespace freertos
|
||||
{
|
||||
|
||||
enum class SchedulerState : uint8_t
|
||||
{
|
||||
suspended,
|
||||
not_started,
|
||||
running
|
||||
};
|
||||
|
||||
class Scheduler final
|
||||
{
|
||||
public:
|
||||
Scheduler() = delete;
|
||||
|
||||
static FREERTOS_CPP_ALWAYS_INLINE void start() noexcept
|
||||
{
|
||||
vTaskStartScheduler();
|
||||
}
|
||||
|
||||
static FREERTOS_CPP_ALWAYS_INLINE SchedulerState state() noexcept
|
||||
{
|
||||
const BaseType_t raw = xTaskGetSchedulerState();
|
||||
if( raw == taskSCHEDULER_RUNNING )
|
||||
{
|
||||
return SchedulerState::running;
|
||||
}
|
||||
if( raw == taskSCHEDULER_NOT_STARTED )
|
||||
{
|
||||
return SchedulerState::not_started;
|
||||
}
|
||||
return SchedulerState::suspended;
|
||||
}
|
||||
|
||||
static FREERTOS_CPP_ALWAYS_INLINE void suspend() noexcept
|
||||
{
|
||||
vTaskSuspendAll();
|
||||
}
|
||||
|
||||
[[nodiscard]] static FREERTOS_CPP_ALWAYS_INLINE bool resume() noexcept
|
||||
{
|
||||
return xTaskResumeAll() != pdFALSE;
|
||||
}
|
||||
};
|
||||
|
||||
namespace this_task
|
||||
{
|
||||
|
||||
FREERTOS_CPP_ALWAYS_INLINE TaskHandle_t current() noexcept
|
||||
{
|
||||
return xTaskGetCurrentTaskHandle();
|
||||
}
|
||||
|
||||
FREERTOS_CPP_ALWAYS_INLINE TickType_t tick_count() noexcept
|
||||
{
|
||||
return xTaskGetTickCount();
|
||||
}
|
||||
|
||||
FREERTOS_CPP_ALWAYS_INLINE void yield() noexcept
|
||||
{
|
||||
taskYIELD();
|
||||
}
|
||||
|
||||
} // namespace this_task
|
||||
|
||||
class CriticalSection final
|
||||
{
|
||||
public:
|
||||
FREERTOS_CPP_ALWAYS_INLINE CriticalSection() noexcept
|
||||
{
|
||||
taskENTER_CRITICAL();
|
||||
}
|
||||
|
||||
FREERTOS_CPP_ALWAYS_INLINE ~CriticalSection() noexcept
|
||||
{
|
||||
taskEXIT_CRITICAL();
|
||||
}
|
||||
|
||||
CriticalSection( const CriticalSection & ) = delete;
|
||||
CriticalSection & operator=( const CriticalSection & ) = delete;
|
||||
CriticalSection( CriticalSection && ) = delete;
|
||||
CriticalSection & operator=( CriticalSection && ) = delete;
|
||||
};
|
||||
|
||||
class SchedulerSuspendGuard final
|
||||
{
|
||||
public:
|
||||
FREERTOS_CPP_ALWAYS_INLINE SchedulerSuspendGuard() noexcept
|
||||
{
|
||||
Scheduler::suspend();
|
||||
}
|
||||
|
||||
FREERTOS_CPP_ALWAYS_INLINE ~SchedulerSuspendGuard() noexcept
|
||||
{
|
||||
( void ) Scheduler::resume();
|
||||
}
|
||||
|
||||
SchedulerSuspendGuard( const SchedulerSuspendGuard & ) = delete;
|
||||
SchedulerSuspendGuard & operator=( const SchedulerSuspendGuard & ) = delete;
|
||||
SchedulerSuspendGuard( SchedulerSuspendGuard && ) = delete;
|
||||
SchedulerSuspendGuard & operator=( SchedulerSuspendGuard && ) = delete;
|
||||
};
|
||||
|
||||
static_assert( !std::is_default_constructible< Scheduler >::value );
|
||||
static_assert( !std::is_copy_constructible< CriticalSection >::value );
|
||||
static_assert( !std::is_move_constructible< CriticalSection >::value );
|
||||
static_assert( !std::is_copy_constructible< SchedulerSuspendGuard >::value );
|
||||
static_assert( !std::is_move_constructible< SchedulerSuspendGuard >::value );
|
||||
|
||||
} // namespace freertos
|
||||
|
||||
#undef FREERTOS_CPP_ALWAYS_INLINE
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,148 @@
|
||||
#ifndef FREERTOS_CPP_QUEUE_HPP
|
||||
#define FREERTOS_CPP_QUEUE_HPP
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <type_traits>
|
||||
|
||||
extern "C"
|
||||
{
|
||||
#include "FreeRTOS.h"
|
||||
#include "queue.h"
|
||||
}
|
||||
|
||||
namespace freertos
|
||||
{
|
||||
|
||||
template< typename T >
|
||||
class Queue final
|
||||
{
|
||||
static_assert( std::is_trivially_copyable< T >::value,
|
||||
"Queue<T> requires T to be trivially copyable" );
|
||||
|
||||
public:
|
||||
explicit Queue( UBaseType_t capacity ) noexcept
|
||||
: handle_ { xQueueCreate( capacity, sizeof( T ) ) },
|
||||
capacity_ { capacity }
|
||||
{
|
||||
}
|
||||
|
||||
~Queue() noexcept
|
||||
{
|
||||
if( handle_ != nullptr )
|
||||
{
|
||||
vQueueDelete( handle_ );
|
||||
}
|
||||
}
|
||||
|
||||
Queue( const Queue & ) = delete;
|
||||
Queue & operator=( const Queue & ) = delete;
|
||||
Queue( Queue && ) = delete;
|
||||
Queue & operator=( Queue && ) = delete;
|
||||
|
||||
[[nodiscard]] bool valid() const noexcept { return handle_ != nullptr; }
|
||||
|
||||
[[nodiscard]] bool send( const T & item,
|
||||
TickType_t timeout = 0U ) noexcept
|
||||
{
|
||||
return handle_ != nullptr &&
|
||||
xQueueSend( handle_, &item, timeout ) == pdPASS;
|
||||
}
|
||||
|
||||
[[nodiscard]] bool receive( T & item,
|
||||
TickType_t timeout = 0U ) noexcept
|
||||
{
|
||||
return handle_ != nullptr &&
|
||||
xQueueReceive( handle_, &item, timeout ) == pdPASS;
|
||||
}
|
||||
|
||||
[[nodiscard]] UBaseType_t size() const noexcept
|
||||
{
|
||||
return handle_ == nullptr ? 0U : uxQueueMessagesWaiting( handle_ );
|
||||
}
|
||||
|
||||
[[nodiscard]] UBaseType_t capacity() const noexcept { return capacity_; }
|
||||
[[nodiscard]] QueueHandle_t native_handle() const noexcept { return handle_; }
|
||||
|
||||
private:
|
||||
QueueHandle_t handle_ {};
|
||||
const UBaseType_t capacity_;
|
||||
};
|
||||
|
||||
template< typename T, size_t Capacity >
|
||||
struct StaticQueueStorage final
|
||||
{
|
||||
static_assert( std::is_trivially_copyable< T >::value,
|
||||
"Queue<T> requires T to be trivially copyable" );
|
||||
static_assert( Capacity > 0U, "StaticQueue capacity must be positive" );
|
||||
|
||||
StaticQueue_t control {};
|
||||
alignas( T ) uint8_t bytes[ Capacity * sizeof( T ) ] {};
|
||||
};
|
||||
|
||||
template< typename T, size_t Capacity >
|
||||
class StaticQueue final
|
||||
{
|
||||
static_assert( std::is_trivially_copyable< T >::value,
|
||||
"Queue<T> requires T to be trivially copyable" );
|
||||
static_assert( Capacity > 0U, "StaticQueue capacity must be positive" );
|
||||
|
||||
public:
|
||||
explicit StaticQueue( StaticQueueStorage< T, Capacity > & storage ) noexcept
|
||||
: handle_ { xQueueCreateStatic(
|
||||
static_cast< UBaseType_t >( Capacity ), sizeof( T ),
|
||||
storage.bytes, &storage.control ) },
|
||||
storage_ { storage }
|
||||
{
|
||||
}
|
||||
|
||||
~StaticQueue() noexcept
|
||||
{
|
||||
if( handle_ != nullptr )
|
||||
{
|
||||
vQueueDelete( handle_ );
|
||||
}
|
||||
}
|
||||
|
||||
StaticQueue( const StaticQueue & ) = delete;
|
||||
StaticQueue & operator=( const StaticQueue & ) = delete;
|
||||
StaticQueue( StaticQueue && ) = delete;
|
||||
StaticQueue & operator=( StaticQueue && ) = delete;
|
||||
|
||||
[[nodiscard]] bool valid() const noexcept { return handle_ != nullptr; }
|
||||
|
||||
[[nodiscard]] bool send( const T & item,
|
||||
TickType_t timeout = 0U ) noexcept
|
||||
{
|
||||
return handle_ != nullptr &&
|
||||
xQueueSend( handle_, &item, timeout ) == pdPASS;
|
||||
}
|
||||
|
||||
[[nodiscard]] bool receive( T & item,
|
||||
TickType_t timeout = 0U ) noexcept
|
||||
{
|
||||
return handle_ != nullptr &&
|
||||
xQueueReceive( handle_, &item, timeout ) == pdPASS;
|
||||
}
|
||||
|
||||
[[nodiscard]] UBaseType_t size() const noexcept
|
||||
{
|
||||
return handle_ == nullptr ? 0U : uxQueueMessagesWaiting( handle_ );
|
||||
}
|
||||
|
||||
static constexpr size_t capacity() noexcept { return Capacity; }
|
||||
static constexpr size_t item_size() noexcept { return sizeof( T ); }
|
||||
[[nodiscard]] QueueHandle_t native_handle() const noexcept { return handle_; }
|
||||
[[nodiscard]] const StaticQueueStorage< T, Capacity > & storage() const noexcept
|
||||
{
|
||||
return storage_;
|
||||
}
|
||||
|
||||
private:
|
||||
QueueHandle_t handle_ {};
|
||||
StaticQueueStorage< T, Capacity > & storage_;
|
||||
};
|
||||
|
||||
} // namespace freertos
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,117 @@
|
||||
#ifndef FREERTOS_SOFTWARE_TIMER_HPP
|
||||
#define FREERTOS_SOFTWARE_TIMER_HPP
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
extern "C"
|
||||
{
|
||||
#include "FreeRTOS.h"
|
||||
#include "timers.h"
|
||||
}
|
||||
|
||||
namespace freertos
|
||||
{
|
||||
|
||||
enum class TimerPolicy
|
||||
{
|
||||
one_shot,
|
||||
periodic
|
||||
};
|
||||
|
||||
enum class TimerCommand
|
||||
{
|
||||
rejected = 0,
|
||||
accepted = 1
|
||||
};
|
||||
|
||||
template< typename Context, TimerPolicy Policy >
|
||||
class SoftwareTimer final
|
||||
{
|
||||
public:
|
||||
SoftwareTimer( StaticTimer_t & storage,
|
||||
const char * name,
|
||||
TickType_t period,
|
||||
Context & context ) noexcept
|
||||
: handle_( xTimerCreateStatic( name,
|
||||
period,
|
||||
Policy == TimerPolicy::periodic ? pdTRUE : pdFALSE,
|
||||
&context,
|
||||
&SoftwareTimer::trampoline,
|
||||
&storage ) )
|
||||
{
|
||||
}
|
||||
|
||||
SoftwareTimer( const SoftwareTimer & ) = delete;
|
||||
SoftwareTimer & operator=( const SoftwareTimer & ) = delete;
|
||||
SoftwareTimer( SoftwareTimer && ) = delete;
|
||||
SoftwareTimer & operator=( SoftwareTimer && ) = delete;
|
||||
|
||||
[[nodiscard]] bool valid() const noexcept
|
||||
{
|
||||
return handle_ != nullptr;
|
||||
}
|
||||
|
||||
[[nodiscard]] TimerCommand start( TickType_t queue_wait = 0U ) noexcept
|
||||
{
|
||||
return command_result( xTimerStart( handle_, queue_wait ) );
|
||||
}
|
||||
|
||||
[[nodiscard]] TimerCommand reset( TickType_t queue_wait = 0U ) noexcept
|
||||
{
|
||||
return command_result( xTimerReset( handle_, queue_wait ) );
|
||||
}
|
||||
|
||||
[[nodiscard]] TimerCommand change_period( TickType_t period,
|
||||
TickType_t queue_wait = 0U ) noexcept
|
||||
{
|
||||
return command_result( xTimerChangePeriod( handle_, period, queue_wait ) );
|
||||
}
|
||||
|
||||
[[nodiscard]] TimerCommand stop( TickType_t queue_wait = 0U ) noexcept
|
||||
{
|
||||
return command_result( xTimerStop( handle_, queue_wait ) );
|
||||
}
|
||||
|
||||
[[nodiscard]] bool active() const noexcept
|
||||
{
|
||||
return xTimerIsTimerActive( handle_ ) != pdFALSE;
|
||||
}
|
||||
|
||||
[[nodiscard]] TickType_t period() const noexcept
|
||||
{
|
||||
return xTimerGetPeriod( handle_ );
|
||||
}
|
||||
|
||||
[[nodiscard]] TimerHandle_t native_handle() const noexcept
|
||||
{
|
||||
return handle_;
|
||||
}
|
||||
|
||||
private:
|
||||
static TimerCommand command_result( BaseType_t result ) noexcept
|
||||
{
|
||||
return result == pdPASS ? TimerCommand::accepted : TimerCommand::rejected;
|
||||
}
|
||||
|
||||
static void trampoline( TimerHandle_t timer ) noexcept
|
||||
{
|
||||
auto * const context = static_cast< Context * >( pvTimerGetTimerID( timer ) );
|
||||
configASSERT( context != nullptr );
|
||||
context->on_timer( timer );
|
||||
}
|
||||
|
||||
TimerHandle_t handle_;
|
||||
};
|
||||
|
||||
template< typename Context >
|
||||
using OneShotTimer = SoftwareTimer< Context, TimerPolicy::one_shot >;
|
||||
|
||||
template< typename Context >
|
||||
using PeriodicTimer = SoftwareTimer< Context, TimerPolicy::periodic >;
|
||||
|
||||
static_assert( !std::is_copy_constructible_v< OneShotTimer< int > > );
|
||||
static_assert( !std::is_move_constructible_v< OneShotTimer< int > > );
|
||||
|
||||
} // namespace freertos
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,183 @@
|
||||
#ifndef FREERTOS_CPP_TASK_STORAGE_HPP
|
||||
#define FREERTOS_CPP_TASK_STORAGE_HPP
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <type_traits>
|
||||
|
||||
extern "C"
|
||||
{
|
||||
#include "FreeRTOS.h"
|
||||
#include "task.h"
|
||||
}
|
||||
|
||||
#ifndef FREERTOS_STORAGE_CHECKPOINT
|
||||
#define FREERTOS_STORAGE_CHECKPOINT( point, object ) \
|
||||
do \
|
||||
{ \
|
||||
( void ) ( point ); \
|
||||
( void ) ( object ); \
|
||||
} while( 0 )
|
||||
#endif
|
||||
|
||||
namespace freertos
|
||||
{
|
||||
|
||||
enum class StorageTaskState : uint8_t
|
||||
{
|
||||
constructed,
|
||||
ready,
|
||||
running,
|
||||
completed,
|
||||
start_failed
|
||||
};
|
||||
|
||||
template< typename Derived >
|
||||
class DynamicTask
|
||||
{
|
||||
public:
|
||||
constexpr DynamicTask( const char * name,
|
||||
configSTACK_DEPTH_TYPE stack_words,
|
||||
UBaseType_t priority ) noexcept
|
||||
: name_ { name }, stack_words_ { stack_words }, priority_ { priority }
|
||||
{
|
||||
}
|
||||
|
||||
DynamicTask( const DynamicTask & ) = delete;
|
||||
DynamicTask & operator=( const DynamicTask & ) = delete;
|
||||
DynamicTask( DynamicTask && ) = delete;
|
||||
DynamicTask & operator=( DynamicTask && ) = delete;
|
||||
|
||||
bool start() noexcept
|
||||
{
|
||||
static_assert( std::is_base_of< DynamicTask< Derived >,
|
||||
Derived >::value );
|
||||
if( state_ != StorageTaskState::constructed )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
state_ = StorageTaskState::ready;
|
||||
const BaseType_t result = xTaskCreate(
|
||||
&DynamicTask::trampoline, name_, stack_words_,
|
||||
static_cast< Derived * >( this ), priority_, &handle_ );
|
||||
if( result != pdPASS )
|
||||
{
|
||||
handle_ = nullptr;
|
||||
state_ = StorageTaskState::start_failed;
|
||||
return false;
|
||||
}
|
||||
FREERTOS_STORAGE_CHECKPOINT( 2U, this );
|
||||
return true;
|
||||
}
|
||||
|
||||
TaskHandle_t native_handle() const noexcept { return handle_; }
|
||||
StorageTaskState state() const noexcept { return state_; }
|
||||
|
||||
private:
|
||||
static void trampoline( void * context )
|
||||
{
|
||||
auto * const derived = static_cast< Derived * >( context );
|
||||
auto & base = *static_cast< DynamicTask * >( derived );
|
||||
base.state_ = StorageTaskState::running;
|
||||
FREERTOS_STORAGE_CHECKPOINT( 5U, derived );
|
||||
derived->run();
|
||||
base.state_ = StorageTaskState::completed;
|
||||
base.handle_ = nullptr;
|
||||
FREERTOS_STORAGE_CHECKPOINT( 7U, derived );
|
||||
vTaskDelete( nullptr );
|
||||
for( ;; ) {}
|
||||
}
|
||||
|
||||
TaskHandle_t handle_ {};
|
||||
const char * const name_;
|
||||
const configSTACK_DEPTH_TYPE stack_words_;
|
||||
const UBaseType_t priority_;
|
||||
volatile StorageTaskState state_ { StorageTaskState::constructed };
|
||||
};
|
||||
|
||||
template< size_t StackWords >
|
||||
struct StaticTaskStorage final
|
||||
{
|
||||
StaticTask_t tcb {};
|
||||
StackType_t stack[ StackWords ] {};
|
||||
};
|
||||
|
||||
template< typename Derived, size_t StackWords >
|
||||
class StaticTask
|
||||
{
|
||||
public:
|
||||
static_assert( StackWords > 0U, "static task needs stack storage" );
|
||||
|
||||
constexpr StaticTask( const char * name,
|
||||
UBaseType_t priority,
|
||||
StaticTaskStorage< StackWords > & storage ) noexcept
|
||||
: name_ { name }, priority_ { priority }, storage_ { storage }
|
||||
{
|
||||
}
|
||||
|
||||
StaticTask( const StaticTask & ) = delete;
|
||||
StaticTask & operator=( const StaticTask & ) = delete;
|
||||
StaticTask( StaticTask && ) = delete;
|
||||
StaticTask & operator=( StaticTask && ) = delete;
|
||||
|
||||
bool start() noexcept
|
||||
{
|
||||
static_assert( std::is_base_of< StaticTask< Derived, StackWords >,
|
||||
Derived >::value );
|
||||
if( state_ != StorageTaskState::constructed )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
state_ = StorageTaskState::ready;
|
||||
handle_ = xTaskCreateStatic(
|
||||
&StaticTask::trampoline,
|
||||
name_,
|
||||
static_cast< configSTACK_DEPTH_TYPE >( StackWords ),
|
||||
static_cast< Derived * >( this ),
|
||||
priority_,
|
||||
storage_.stack,
|
||||
&storage_.tcb );
|
||||
if( handle_ == nullptr )
|
||||
{
|
||||
state_ = StorageTaskState::start_failed;
|
||||
return false;
|
||||
}
|
||||
FREERTOS_STORAGE_CHECKPOINT( 4U, this );
|
||||
return true;
|
||||
}
|
||||
|
||||
TaskHandle_t native_handle() const noexcept { return handle_; }
|
||||
StorageTaskState state() const noexcept { return state_; }
|
||||
const StaticTask_t * tcb_storage() const noexcept { return &storage_.tcb; }
|
||||
const StackType_t * stack_storage() const noexcept { return storage_.stack; }
|
||||
static constexpr size_t stack_words() noexcept { return StackWords; }
|
||||
static constexpr size_t stack_bytes() noexcept
|
||||
{
|
||||
return StackWords * sizeof( StackType_t );
|
||||
}
|
||||
|
||||
private:
|
||||
static void trampoline( void * context )
|
||||
{
|
||||
auto * const derived = static_cast< Derived * >( context );
|
||||
auto & base = *static_cast< StaticTask * >( derived );
|
||||
base.state_ = StorageTaskState::running;
|
||||
FREERTOS_STORAGE_CHECKPOINT( 6U, derived );
|
||||
derived->run();
|
||||
base.state_ = StorageTaskState::completed;
|
||||
base.handle_ = nullptr;
|
||||
FREERTOS_STORAGE_CHECKPOINT( 8U, derived );
|
||||
vTaskDelete( nullptr );
|
||||
for( ;; ) {}
|
||||
}
|
||||
|
||||
TaskHandle_t handle_ {};
|
||||
const char * const name_;
|
||||
const UBaseType_t priority_;
|
||||
StaticTaskStorage< StackWords > & storage_;
|
||||
volatile StorageTaskState state_ { StorageTaskState::constructed };
|
||||
};
|
||||
|
||||
} // namespace freertos
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user