From 053fcabc634cbbea056c657001e3684f3cf1eb38 Mon Sep 17 00:00:00 2001 From: Klagarge Date: Mon, 26 Feb 2024 14:54:31 +0100 Subject: [PATCH] finish lab --- main.c | 78 +++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 63 insertions(+), 15 deletions(-) diff --git a/main.c b/main.c index d18fa21..f2000ad 100644 --- a/main.c +++ b/main.c @@ -6,27 +6,68 @@ #include CMSIS_device_header #include "cmsis_os2.h" #include "EventRecorder.h" + +const osThreadAttr_t thread1_attr = { + .priority = osPriorityNormal, // Set initial thread priority to high + .name = "BLOCKER" +}; + +const osThreadAttr_t thread2_attr = { + .priority = osPriorityAboveNormal4, + .name = "RESUMER" +}; + +const osThreadAttr_t thread3_attr = { + .priority = osPriorityAboveNormal, + .name = "DUMMY_WAITER" +}; + +const osThreadAttr_t thread4_attr = { + .priority = osPriorityLow, + .name = "DO_NOTHING" +}; + + /*---------------------------------------------------------------------------- * Application main thread *---------------------------------------------------------------------------*/ -__NO_RETURN static void app_main (void *argument) { - (void)argument; - // ... - for (;;) {} +__NO_RETURN static void Thread_1 (void *args) { + osThreadId_t* id = (osThreadId_t*)args; + osStatus_t status; + for (;;) { + osThreadState_t state = osThreadGetState(*id); + if(state != osThreadBlocked) { // Thread run + osThreadSuspend(*id); + } + status = osDelay(50); + } } -void task1(void *args) { - (void)args; - for(;;) {} +__NO_RETURN static void Thread_2(void *args) { + osThreadId_t* id = (osThreadId_t*)args; + osStatus_t status; + for(;;) { + osThreadState_t state = osThreadGetState(*id); + if(state == osThreadBlocked) { // Thread suspended + osThreadResume(*id); + } + status = osDelay(20); + } } -__NO_RETURN static void task2(void *args) { +__NO_RETURN static void Thread_3(void *args) { (void)args; - for(;;) {} + uint32_t i = 0; + osStatus_t status; + for(;;) { + i = (i+1) % 10; + status = osDelay(i); + } } -__NO_RETURN static void task3(void *args) { +__NO_RETURN static void Thread_4(void *args) { (void)args; + osStatus_t status; for(;;) {} } @@ -35,12 +76,19 @@ int main (void) { // System Initialization SystemCoreClockUpdate(); EventRecorderInitialize(EventRecordAll, 1U); + + osThreadId_t thread1; + osThreadId_t thread2; + osThreadId_t thread3; + osThreadId_t thread4; osKernelInitialize(); // Initialize CMSIS-RTOS - thread1 = osThreadNew(app_main, NULL, NULL); // Create application main thread - osThreadNew(task1, NULL, NULL); - osThreadNew(task2, NULL, NULL); - osThreadNew(task3, NULL, NULL); - osKernelStart(); // Start thread execution + + thread1 = osThreadNew(Thread_1, &thread4, &thread1_attr); + thread2 = osThreadNew(Thread_2, &thread4, &thread2_attr); + thread3 = osThreadNew(Thread_3, NULL, &thread3_attr); + thread4 = osThreadNew(Thread_4, NULL, &thread4_attr); + + osKernelStart(); // Start thread execution for (;;) {} }