feat: add lab-rv32i-freertos-semaphore-notify card

This commit is contained in:
user
2026-07-21 19:14:20 +02:00
commit 3ae96a0d06
67 changed files with 37159 additions and 0 deletions
+72
View File
@@ -0,0 +1,72 @@
#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 1
#define configUSE_TASK_NOTIFICATIONS 1
#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_xSemaphoreGetMutexHolder 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
+183
View File
@@ -0,0 +1,183 @@
#ifndef FREERTOS_CPP_SYNCHRONIZATION_HPP
#define FREERTOS_CPP_SYNCHRONIZATION_HPP
#include <stdint.h>
#include <type_traits>
extern "C"
{
#include "FreeRTOS.h"
#include "semphr.h"
#include "task.h"
}
namespace freertos
{
struct StaticSemaphoreStorage final
{
StaticSemaphore_t control {};
};
class BinarySemaphore final
{
public:
explicit BinarySemaphore( StaticSemaphoreStorage & storage ) noexcept
: handle_ { xSemaphoreCreateBinaryStatic( &storage.control ) }
{
}
~BinarySemaphore() noexcept
{
if( handle_ != nullptr ) vSemaphoreDelete( handle_ );
}
BinarySemaphore( const BinarySemaphore & ) = delete;
BinarySemaphore & operator=( const BinarySemaphore & ) = delete;
BinarySemaphore( BinarySemaphore && ) = delete;
BinarySemaphore & operator=( BinarySemaphore && ) = delete;
[[nodiscard]] bool valid() const noexcept { return handle_ != nullptr; }
[[nodiscard]] bool give() noexcept
{
return handle_ != nullptr && xSemaphoreGive( handle_ ) == pdPASS;
}
[[nodiscard]] bool take( TickType_t timeout ) noexcept
{
return handle_ != nullptr &&
xSemaphoreTake( handle_, timeout ) == pdPASS;
}
[[nodiscard]] UBaseType_t count() const noexcept
{
return handle_ == nullptr ? 0U : uxSemaphoreGetCount( handle_ );
}
[[nodiscard]] SemaphoreHandle_t native_handle() const noexcept
{
return handle_;
}
private:
SemaphoreHandle_t handle_ {};
};
template< UBaseType_t Maximum >
class CountingSemaphore final
{
static_assert( Maximum > 0U, "CountingSemaphore maximum must be positive" );
public:
CountingSemaphore( StaticSemaphoreStorage & storage,
UBaseType_t initial ) noexcept
: handle_ { initial <= Maximum ?
xSemaphoreCreateCountingStatic(
Maximum, initial, &storage.control ) : nullptr }
{
}
~CountingSemaphore() noexcept
{
if( handle_ != nullptr ) vSemaphoreDelete( handle_ );
}
CountingSemaphore( const CountingSemaphore & ) = delete;
CountingSemaphore & operator=( const CountingSemaphore & ) = delete;
CountingSemaphore( CountingSemaphore && ) = delete;
CountingSemaphore & operator=( CountingSemaphore && ) = delete;
[[nodiscard]] bool valid() const noexcept { return handle_ != nullptr; }
[[nodiscard]] bool give() noexcept
{
return handle_ != nullptr && xSemaphoreGive( handle_ ) == pdPASS;
}
[[nodiscard]] bool take( TickType_t timeout ) noexcept
{
return handle_ != nullptr &&
xSemaphoreTake( handle_, timeout ) == pdPASS;
}
[[nodiscard]] UBaseType_t count() const noexcept
{
return handle_ == nullptr ? 0U : uxSemaphoreGetCount( handle_ );
}
static constexpr UBaseType_t maximum() noexcept { return Maximum; }
[[nodiscard]] SemaphoreHandle_t native_handle() const noexcept
{
return handle_;
}
private:
SemaphoreHandle_t handle_ {};
};
enum class NotificationAction : uint8_t
{
no_action,
set_bits,
increment,
overwrite,
without_overwrite
};
class TaskNotification final
{
public:
explicit constexpr TaskNotification( TaskHandle_t target ) noexcept
: target_ { target }
{
}
[[nodiscard]] bool valid() const noexcept { return target_ != nullptr; }
[[nodiscard]] bool give() const noexcept
{
return target_ != nullptr && xTaskNotifyGive( target_ ) == pdPASS;
}
[[nodiscard]] bool notify( uint32_t value,
NotificationAction action ) const noexcept
{
return target_ != nullptr &&
xTaskNotify( target_, value, raw_action( action ) ) == pdPASS;
}
[[nodiscard]] static uint32_t take( bool clear_on_exit,
TickType_t timeout ) noexcept
{
return ulTaskNotifyTake( clear_on_exit ? pdTRUE : pdFALSE, timeout );
}
[[nodiscard]] static bool wait_value( uint32_t & value,
TickType_t timeout,
uint32_t clear_on_entry = 0U,
uint32_t clear_on_exit = UINT32_MAX ) noexcept
{
return xTaskNotifyWait( clear_on_entry, clear_on_exit,
&value, timeout ) == pdPASS;
}
[[nodiscard]] TaskHandle_t target() const noexcept { return target_; }
private:
static eNotifyAction raw_action( NotificationAction action ) noexcept
{
switch( action )
{
case NotificationAction::no_action: return eNoAction;
case NotificationAction::set_bits: return eSetBits;
case NotificationAction::increment: return eIncrement;
case NotificationAction::overwrite: return eSetValueWithOverwrite;
case NotificationAction::without_overwrite:
return eSetValueWithoutOverwrite;
}
return eNoAction;
}
TaskHandle_t target_ {};
};
static_assert( !std::is_copy_constructible< BinarySemaphore >::value );
static_assert( !std::is_move_constructible< CountingSemaphore< 2U > >::value );
static_assert( std::is_trivially_copyable< TaskNotification >::value );
} // 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
+11
View File
@@ -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
+17
View File
@@ -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