diff --git a/mission/freeRTOS_rust_helper.c b/mission/freeRTOS_rust_helper.c index a3d3112..eabd0dc 100644 --- a/mission/freeRTOS_rust_helper.c +++ b/mission/freeRTOS_rust_helper.c @@ -2,22 +2,39 @@ #include "semphr.h" #include "task.h" +#define NUMBER_OF_TASKS 300 +SemaphoreHandle_t taskMutex = NULL; +StaticSemaphore_t taskMutexDescriptor; +size_t nextFreeTaskDescriptor = 0; +StaticTask_t taskDescriptors[NUMBER_OF_TASKS]; -StaticTask_t uhOhdeleteThisBuffer; - -void * createTask(TaskFunction_t taskFunction, void * parameter, void *buffer, size_t buffersize){ - return xTaskCreateStatic( - taskFunction, /* The function that implements the task. */ - "rust", /* The text name assigned to the task - for debug only as it is - not used by the kernel. */ - buffersize / sizeof(StackType_t), /* The size of the stack to allocate to the task. */ - parameter, /* The parameter passed to the task - not used in this - simple case. */ - 4, /* The priority assigned to the task. */ - buffer, &uhOhdeleteThisBuffer); +// TODO check and return +void initFreeRTOSHelper() { + taskMutex = xSemaphoreCreateRecursiveMutexStatic(&taskMutexDescriptor); } +// TODO return some error code? +void *createTask(TaskFunction_t taskFunction, void *parameter, void *buffer, + size_t buffersize) { + if (xSemaphoreTakeRecursive(taskMutex, portMAX_DELAY) != pdTRUE) { + return NULL; + } + // we hold the task mutex and are now allowed to access the taskDescriptors + if (nextFreeTaskDescriptor >= + sizeof(taskDescriptors) / sizeof(*taskDescriptors)) { + return NULL; + } + + TaskHandle_t newTask = xTaskCreateStatic( + taskFunction, "rust", buffersize / sizeof(StackType_t), parameter, 4, + buffer, taskDescriptors + nextFreeTaskDescriptor); + + nextFreeTaskDescriptor++; + + xSemaphoreGiveRecursive(taskMutex); + return newTask; +} void task_delay(uint32_t milliseconds) { - vTaskDelay(pdMS_TO_TICKS(milliseconds)); + vTaskDelay(pdMS_TO_TICKS(milliseconds)); } \ No newline at end of file diff --git a/mission/main.c b/mission/main.c index 21f8ea0..6ab1dfa 100644 --- a/mission/main.c +++ b/mission/main.c @@ -124,11 +124,15 @@ extern SemaphoreHandle_t malloc_mutex; /*-----------------------------------------------------------*/ void mission(void *); +void initFreeRTOSHelper(); + int main(void) { /* Configure the hardware ready to run the demo. */ prvSetupHardware(); + initFreeRTOSHelper(); + // printf("Booting Software\n"); int taskParameters = 0; diff --git a/mission_rust/src/lib.rs b/mission_rust/src/lib.rs index ab0634e..948f291 100644 --- a/mission_rust/src/lib.rs +++ b/mission_rust/src/lib.rs @@ -1,11 +1,30 @@ #![no_std] +#[macro_export] +macro_rules! sifln { + ($(,)?) => ( + let mut stdout = Outbytes {}; + writeln!(stdout); + ); + ($($arg:tt)*) => ( + let mut stdout = Outbytes {}; + let _alwaysok = writeln!(stdout, $($arg)*); + ); +} + +#[macro_export] +macro_rules! sif { + ($($arg:tt)*) => ( + let mut stdout = Outbytes {}; + let _alwaysok = write!(stdout, $($arg)*); + ); +} + use core::panic::PanicInfo; #[panic_handler] fn panic(panic: &PanicInfo<'_>) -> ! { - let mut stdout = Outbytes {}; - let _ok = writeln!(&mut stdout, "{}", panic); + sifln!("{}", panic); //TODO: stop RTOS, exit if hosted loop {} } @@ -15,8 +34,6 @@ type TaskFunction = unsafe extern "C" fn(*mut cty::c_void); extern "C" { fn outbyte(c: cty::c_char); - //void * createTask(void (* taskFunction)( void * ), void * parameter, void *buffer, size_t buffersize){ - fn createTask( taskFunction: TaskFunction, parameter: *mut cty::c_void, @@ -31,12 +48,10 @@ extern "C" { #[no_mangle] extern "C" fn rust_main() { - let mut stdout = Outbytes {}; - - let _ok = writeln!(&mut stdout, "Rust startup"); + sifln!("Rust startup"); mission(); - let _ok = writeln!(&mut stdout, "Mission done"); + sifln!("Mission done"); } #[no_mangle] @@ -50,7 +65,7 @@ extern "C" fn task_entry(task_object: *mut cty::c_void) { } trait ExecutableObjectIF { - fn perform(&self); + fn perform(&mut self); } struct PeriodicTaskRunner<'a> { @@ -104,9 +119,8 @@ struct Handler { } impl ExecutableObjectIF for Handler { - fn perform(&self) { - let mut stdout = Outbytes {}; - let _ok = writeln!(&mut stdout, "Handler {} performs", self.id); + fn perform(&mut self) { + sifln!("Handler {} performs", self.id); } } @@ -129,16 +143,22 @@ fn panics(a: &mut [i32]) { a[4] = 3; } -fn mission() { - let mut stdout = Outbytes {}; - let _ok = writeln!(&mut stdout, "Mission enter"); + + + +fn mission() { + sifln!("Mission enter"); let mut h1 = Handler {id: 1}; + let mut h2 = Handler {id: 2}; let _t1: PeriodicTask<'_, 512> = PeriodicTask::new(&mut h1); - let _ok = writeln!(&mut stdout, "Mission done delay"); + let _t2: PeriodicTask<'_, 512> = PeriodicTask::new(&mut h2); + + sifln!("Mission done delay"); unsafe {task_delay(3000);} } +