feat: add lab-rv32i-freertos-static-task card
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
#ifndef FREERTOS_CONFIG_H
|
||||
#define FREERTOS_CONFIG_H
|
||||
|
||||
/* Hazard3 testbench: mtime advances once per simulated cycle. */
|
||||
#define configCPU_CLOCK_HZ ( 1000000UL )
|
||||
#define configTICK_RATE_HZ ( 1000U )
|
||||
#define configMTIME_BASE_ADDRESS ( 0xC0000100UL )
|
||||
#define configMTIMECMP_BASE_ADDRESS ( 0xC0000108UL )
|
||||
|
||||
#define configUSE_PREEMPTION 1
|
||||
#define configUSE_TIME_SLICING 1
|
||||
#define configNUMBER_OF_CORES 1
|
||||
#define configUSE_PORT_OPTIMISED_TASK_SELECTION 0
|
||||
#define configUSE_IDLE_HOOK 0
|
||||
#define configUSE_TICK_HOOK 0
|
||||
#define configMAX_PRIORITIES 5
|
||||
#define configMINIMAL_STACK_SIZE 128
|
||||
#define configMAX_TASK_NAME_LEN 12
|
||||
#define configTICK_TYPE_WIDTH_IN_BITS TICK_TYPE_WIDTH_32_BITS
|
||||
#define configIDLE_SHOULD_YIELD 1
|
||||
|
||||
#define configSUPPORT_DYNAMIC_ALLOCATION 1
|
||||
#define configSUPPORT_STATIC_ALLOCATION 1
|
||||
#define configKERNEL_PROVIDED_STATIC_MEMORY 1
|
||||
#define configTOTAL_HEAP_SIZE ( 16U * 1024U )
|
||||
#define configAPPLICATION_ALLOCATED_HEAP 1
|
||||
#define configUSE_MALLOC_FAILED_HOOK 1
|
||||
#define configHEAP_CLEAR_MEMORY_ON_FREE 0
|
||||
|
||||
#define configUSE_TIMERS 0
|
||||
#define configUSE_MUTEXES 0
|
||||
#define configUSE_RECURSIVE_MUTEXES 0
|
||||
#define configUSE_COUNTING_SEMAPHORES 0
|
||||
#define configUSE_TASK_NOTIFICATIONS 0
|
||||
#define configUSE_TRACE_FACILITY 1
|
||||
#define configUSE_STATS_FORMATTING_FUNCTIONS 0
|
||||
#define configGENERATE_RUN_TIME_STATS 0
|
||||
#define configUSE_CO_ROUTINES 0
|
||||
#define configUSE_NEWLIB_REENTRANT 0
|
||||
#define configUSE_POSIX_ERRNO 0
|
||||
#define configCHECK_FOR_STACK_OVERFLOW 0
|
||||
#define configRECORD_STACK_HIGH_ADDRESS 1
|
||||
|
||||
#define INCLUDE_vTaskDelay 1
|
||||
#define INCLUDE_vTaskDelayUntil 1
|
||||
#define INCLUDE_vTaskDelete 1
|
||||
#define INCLUDE_vTaskSuspend 0
|
||||
#define INCLUDE_eTaskGetState 1
|
||||
#define INCLUDE_uxTaskPriorityGet 1
|
||||
#define INCLUDE_vTaskPrioritySet 1
|
||||
#define INCLUDE_xTaskGetSchedulerState 1
|
||||
#define INCLUDE_uxTaskGetStackHighWaterMark 1
|
||||
|
||||
/* Hazard3 is RV32I: no floating-point or vector register file to save. */
|
||||
#define configENABLE_FPU 0
|
||||
#define configENABLE_VPU 0
|
||||
|
||||
/* Required by the pinned RISC-V port. */
|
||||
#define configISR_STACK_SIZE_WORDS 128
|
||||
|
||||
#ifndef __ASSEMBLER__
|
||||
#ifdef __cplusplus
|
||||
extern "C" void lab_assert_fail( const char * file, int line );
|
||||
#else
|
||||
void lab_assert_fail( const char * file, int line );
|
||||
#endif
|
||||
#define configASSERT( condition ) do { if( !( condition ) ) { lab_assert_fail( __FILE__, __LINE__ ); } } while( 0 )
|
||||
#else
|
||||
#define configASSERT( condition )
|
||||
#endif
|
||||
#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
|
||||
@@ -0,0 +1,15 @@
|
||||
#ifndef FREERTOS_RISC_V_CHIP_SPECIFIC_EXTENSIONS_H
|
||||
#define FREERTOS_RISC_V_CHIP_SPECIFIC_EXTENSIONS_H
|
||||
|
||||
/* Hazard3 adds no architectural registers to the base RV32I context. */
|
||||
#define portasmHAS_MTIME 1
|
||||
#define portasmHAS_SIFIVE_CLINT 0
|
||||
#define portasmADDITIONAL_CONTEXT_SIZE 0
|
||||
|
||||
.macro portasmSAVE_ADDITIONAL_REGISTERS
|
||||
.endm
|
||||
|
||||
.macro portasmRESTORE_ADDITIONAL_REGISTERS
|
||||
.endm
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
#ifndef LAB_FREESTANDING_STDLIB_H
|
||||
#define LAB_FREESTANDING_STDLIB_H
|
||||
|
||||
/*
|
||||
* The selected FreeRTOS sources include <stdlib.h> for standard types, but the
|
||||
* configuration used by this card does not call hosted stdlib functions.
|
||||
* GCC's freestanding headers provide size_t through <stddef.h>.
|
||||
*/
|
||||
#include <stddef.h>
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,17 @@
|
||||
#ifndef LAB_FREESTANDING_STRING_H
|
||||
#define LAB_FREESTANDING_STRING_H
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
void * memcpy( void * destination, const void * source, size_t count );
|
||||
void * memset( void * destination, int value, size_t count );
|
||||
size_t strlen( const char * text );
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user