From 4782011fb816ffd3836721b78c6ba1e1bfed9e92 Mon Sep 17 00:00:00 2001 From: Ulrich Mohr Date: Tue, 14 Nov 2023 01:26:35 +0100 Subject: [PATCH] task demo (unsane) --- .../libsrc/standalone/src/outbyte.c | 2 +- mission/CMakeLists.txt | 2 +- mission/freeRTOS_rust_helper.c | 23 ++++ mission_rust/src/lib.rs | 106 +++++++++++++++++- 4 files changed, 126 insertions(+), 7 deletions(-) create mode 100644 mission/freeRTOS_rust_helper.c diff --git a/bsp_z7/ps7_cortexa9_0/libsrc/standalone/src/outbyte.c b/bsp_z7/ps7_cortexa9_0/libsrc/standalone/src/outbyte.c index c7a3b12..db60e6d 100644 --- a/bsp_z7/ps7_cortexa9_0/libsrc/standalone/src/outbyte.c +++ b/bsp_z7/ps7_cortexa9_0/libsrc/standalone/src/outbyte.c @@ -15,5 +15,5 @@ void outbyte(char c); #endif void outbyte(char c) { - //XUartPs_SendByte(STDOUT_BASEADDRESS, c); + XUartPs_SendByte(STDOUT_BASEADDRESS, c); } diff --git a/mission/CMakeLists.txt b/mission/CMakeLists.txt index 21f76d3..d53a240 100644 --- a/mission/CMakeLists.txt +++ b/mission/CMakeLists.txt @@ -1 +1 @@ -target_sources(${TARGET_NAME} PRIVATE main.c testIp.c) \ No newline at end of file +target_sources(${TARGET_NAME} PRIVATE main.c testIp.c freeRTOS_rust_helper.c) \ No newline at end of file diff --git a/mission/freeRTOS_rust_helper.c b/mission/freeRTOS_rust_helper.c new file mode 100644 index 0000000..a3d3112 --- /dev/null +++ b/mission/freeRTOS_rust_helper.c @@ -0,0 +1,23 @@ +#include "FreeRTOS.h" +#include "semphr.h" +#include "task.h" + + +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); +} + + +void task_delay(uint32_t milliseconds) { + vTaskDelay(pdMS_TO_TICKS(milliseconds)); +} \ No newline at end of file diff --git a/mission_rust/src/lib.rs b/mission_rust/src/lib.rs index aaa346e..ab0634e 100644 --- a/mission_rust/src/lib.rs +++ b/mission_rust/src/lib.rs @@ -10,13 +10,104 @@ fn panic(panic: &PanicInfo<'_>) -> ! { loop {} } +type TaskFunction = unsafe extern "C" fn(*mut cty::c_void); + extern "C" { - pub fn outbyte(c: cty::c_char); + 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, + buffer: *mut cty::c_void, + buffersize: cty::size_t, + ) -> *const cty::c_void; + + fn vTaskDelete(handle: *const cty::c_void); + + fn task_delay(milliseconds: cty::uint32_t); } #[no_mangle] -pub extern "C" fn rust_main() { +extern "C" fn rust_main() { + let mut stdout = Outbytes {}; + + let _ok = writeln!(&mut stdout, "Rust startup"); mission(); + + let _ok = writeln!(&mut stdout, "Mission done"); +} + +#[no_mangle] +extern "C" fn task_entry(task_object: *mut cty::c_void) { + let task: &mut PeriodicTaskRunner; + unsafe { + let pointer = task_object as *mut PeriodicTaskRunner; + task = &mut *pointer; + } + task.execute(); +} + +trait ExecutableObjectIF { + fn perform(&self); +} + +struct PeriodicTaskRunner<'a> { + period: u32, + task_object: &'a mut dyn ExecutableObjectIF, +} +struct PeriodicTask<'a, const STACKSIZE: usize> { + stack: [u8; STACKSIZE], + task: *const cty::c_void, + runner: PeriodicTaskRunner<'a>, +} + +impl<'a> PeriodicTaskRunner<'a> { + fn execute(&mut self) { + loop { + self.task_object.perform(); + unsafe { + task_delay(self.period); + } + } + } +} + +impl<'a, const STACKSIZE: usize> PeriodicTask<'a, STACKSIZE> { + fn new(object: &'a mut dyn ExecutableObjectIF) -> Self { + let mut instance = Self { + stack: [0; STACKSIZE], + task: 0 as *const cty::c_void, + runner: PeriodicTaskRunner { + period: 500, + task_object: object, + }, + }; + let runner_pointer: *mut cty::c_void = &mut instance.runner as *mut _ as *mut cty::c_void; + let stack_pointer: *mut cty::c_void = &mut instance.stack as *mut _ as *mut cty::c_void; + unsafe { + instance.task = createTask(task_entry, runner_pointer, stack_pointer, STACKSIZE); + } + instance + } +} + +impl<'a, const STACKSIZE: usize> Drop for PeriodicTask<'a, STACKSIZE> { + fn drop(&mut self) { + unsafe {vTaskDelete(self.task);} + } +} + +struct Handler { + id: u32 +} + +impl ExecutableObjectIF for Handler { + fn perform(&self) { + let mut stdout = Outbytes {}; + let _ok = writeln!(&mut stdout, "Handler {} performs", self.id); + } } struct Outbytes {} @@ -41,8 +132,13 @@ fn panics(a: &mut [i32]) { fn mission() { let mut stdout = Outbytes {}; - let _ok = writeln!(&mut stdout, "Rust startup"); + let _ok = writeln!(&mut stdout, "Mission enter"); - let mut array = [1, 2, 3]; - panics(&mut array); + let mut h1 = Handler {id: 1}; + + let _t1: PeriodicTask<'_, 512> = PeriodicTask::new(&mut h1); + + let _ok = writeln!(&mut stdout, "Mission done delay"); + unsafe {task_delay(3000);} + }