diff --git a/.cproject b/.cproject index 528a81e..178b7ac 100644 --- a/.cproject +++ b/.cproject @@ -101,12 +101,12 @@ - - - - - + + + + + @@ -200,10 +200,10 @@ - - + + diff --git a/.settings/stm32cubeide.project.prefs b/.settings/stm32cubeide.project.prefs index 96364d8..0ad0f8c 100644 --- a/.settings/stm32cubeide.project.prefs +++ b/.settings/stm32cubeide.project.prefs @@ -1,5 +1,5 @@ 635E684B79701B039C64EA45C3F84D30=C96BA6CC9F20E1205A6EBDFF40205165 66BE74F758C12D739921AEA421D593D3=4 8DF89ED150041C4CBC7CB9A9CAA90856=D6E44E0C9E8538D2672E3627575EB9A7 -DC22A860405A8BF2F2C095E5B6529F12=B7A8998BB86F8064A3F4829332693759 +DC22A860405A8BF2F2C095E5B6529F12=D6E44E0C9E8538D2672E3627575EB9A7 eclipse.preferences.version=1 diff --git a/App/Inc/tasks/dummy.hpp b/App/Inc/tasks/dummy.hpp new file mode 100644 index 0000000..aea9df6 --- /dev/null +++ b/App/Inc/tasks/dummy.hpp @@ -0,0 +1,25 @@ +/* + * dummy.hpp + * + * Created on: 22 Sep 2023 + * Author: Dario + */ + +#ifndef INC_TASKS_DUMMY_HPP_ +#define INC_TASKS_DUMMY_HPP_ + +#include + +namespace demo +{ + class DummyTask : public sta::tacos::TacosThread { + public: + DummyTask(const char* name); + + void init() override; + + void func() override; + }; +} // namespace demo + +#endif /* INC_TASKS_DUMMY_HPP_ */ diff --git a/App/Inc/tasks/statemachine.hpp b/App/Inc/tasks/statemachine.hpp deleted file mode 100644 index 0adc089..0000000 --- a/App/Inc/tasks/statemachine.hpp +++ /dev/null @@ -1,45 +0,0 @@ -/* - * statemachine.hpp - * - * Created on: Sep 6, 2023 - * Author: Dario - */ - -#ifndef INC_TASKS_STATEMACHINE_HPP_ -#define INC_TASKS_STATEMACHINE_HPP_ - -// Two flags for the state change event. TODO: HOW TO SET THE VALUES? -#define TACOS_STATE_CHG_FORCED 0x01 -#define TACOS_STATE_CHG_TIMEOUT 0x02 -#define TACOS_STATE_CHG_NATURAL 0x03 -#define TACOS_STATE_CHG_ALL TACOS_STATE_CHG_FORCED | TACOS_STATE_CHG_TIMEOUT | TACOS_STATE_CHG_NATURAL - - -// The event for signaling state changes to other tasks -osEventFlagsId_t stateChangeEvent_id; - - -// The states used for this demo -enum tacos_states_t -{ - init, - started, - flying, - landed -}; - - -namespace tacos -{ - class StateMachine - { - public: - - - private: - - }; -} - - -#endif /* INC_TASKS_STATEMACHINE_HPP_ */ diff --git a/App/Src/startup.cpp b/App/Src/startup.cpp index 18cc29e..1cd7e5b 100644 --- a/App/Src/startup.cpp +++ b/App/Src/startup.cpp @@ -5,38 +5,31 @@ * Author: Dario */ -#include -#include -#include -#include -#include +#include #include -// The UART mutex defined in freertos.c -extern osMutexId_t uartMutexHandle; +#include +#include namespace sta { - // Here the printable used for debugging is defined. - Printable * Debug; - - namespace rtos + namespace tacos { - // Override the weak implementation of startupExtras provided in rtos2-utils. - void startupExtras(void * argument) + void onStatemachineInit() { - // Initialize the mutex for UART communication. - RtosMutex * mutex = new RtosMutex(&uartMutexHandle); - - // Initialize the UART interface and printable object. - UARTSettings settings = { .mode = UARTMode::RX_TX }; - STM32UART * intf_ptr = new STM32UART(&huart2, settings, mutex); - Debug = new PrintableUART(intf_ptr); - - STA_DEBUG_PRINTLN("UART SUCCESSFULLY INITIALIZED"); + STA_DEBUG_PRINTLN("Starting statemachine task!"); } - } // namespace rtos + + void onManagerInit() + { + STA_DEBUG_PRINTLN("Starting manager task!"); + + Manager::instance()->registerThread(demo::DummyTask("A"), {0, 1}); + Manager::instance()->registerThread(demo::DummyTask("B"), {1, 2}); + Manager::instance()->registerThread(demo::DummyTask("C"), {2, 3}); + } + } // namespace tacos } // namespace sta diff --git a/App/Src/tasks/dummy.cpp b/App/Src/tasks/dummy.cpp new file mode 100644 index 0000000..90bb5f2 --- /dev/null +++ b/App/Src/tasks/dummy.cpp @@ -0,0 +1,34 @@ +/* + * dummy.cpp + * + * Created on: 22 Sep 2023 + * Author: Dario + */ + +#include +#include + +#include + + +namespace demo +{ + DummyTask::DummyTask(const char* name) + : TacosThread(name, osPriorityNormal) + { + + } + + void DummyTask::init() + { + STA_DEBUG_PRINTLN("Initialized dummy task!"); + } + + void DummyTask::func() + { + STA_DEBUG_PRINT("Executing "); + STA_DEBUG_PRINTLN(this->getName()); + } +} // namespace demo + + diff --git a/App/Src/tasks/manager.cpp b/App/Src/tasks/manager.cpp deleted file mode 100644 index 8a286b0..0000000 --- a/App/Src/tasks/manager.cpp +++ /dev/null @@ -1,152 +0,0 @@ -/* - * manager.cpp - * - * Created on: Aug 30, 2023 - * Author: Dario - */ - - -#include -#include - -#include -#include - -#include -#include - - -#include - -#include - - -namespace tacos -{ - - // The data structure representing a TACOS task. - struct tacos_task_t - { - // The code to be executed for this task. - osThreadFunc_t func; - - // The attributes for the task. - osThreadAttr_t attribs; - - // A list of currently running instances of this task. - std::list running; - - // A list of states for which this task should be running. - std::list states; - }; - - -} // namespace tacos - - -// The current state defined somewhere else. -extern tacos_states_t currentState; - - -extern "C" -{ - void managerTask(void *) - { - STA_DEBUG_PRINTLN("INITIALIZED MANAGER TASK"); - - while (true) - { - // Wait until the state machine triggers an event signaling a state-change. - uint32_t flags = osEventFlagsWait(stateChangeEvent_id, STATE_CHANGED_MSK, osFlagsWaitAll, osWaitForever); - - - } - } -} - - - - - - - - -std::list tasks; - - - -void dummyInit(void *) -{ - while (true) - { - STA_DEBUG_PRINTLN("INIT STATE"); - } - - osThreadExit(); -} - -void dummyStarted(void *) -{ - while (true) - { - STA_DEBUG_PRINTLN("STARTED STATE"); - } - - osThreadExit(); -} - -void dummyFlying(void *) -{ - while (true) - { - STA_DEBUG_PRINTLN("FLYING STATE"); - } - - osThreadExit(); -} - -void dummyLanded(void *) -{ - while (true) - { - STA_DEBUG_PRINTLN("LANDED STATE"); - } - - osThreadExit(); -} - - -extern "C" void startManagerTask(void *) -{ - STA_DEBUG_PRINTLN("INITIALIZED MANAGER TASK"); - - while (true) - { - // Wait until the state machine triggers an event signaling a state-change. - osEventFlagsWait(stateChangeEvent_id, STATE_CHANGED_MSK, osFlagsWaitAll, osWaitForever); - - for (tacos_task_t task : tasks) - { - // Check if this task is supposed to be running for this state. If not, kill all instances, else create the desired number of instances. - if (std::find(task.states.begin(), task.states.end(), currentState) == task.states.end()) - { - for (osThreadId_t instance : task.running) - { - osThreadTerminate(instance); - } - } - else - { - if (!task.running.empty()) - { - osThreadDef (task.attribs.name, task.attribs.priority, 1, 0); - - osThreadCreate(osTh, argument); - } - } - } - } - - osThreadExit(); -} - diff --git a/App/Src/tasks/output.cpp b/App/Src/tasks/output.cpp deleted file mode 100644 index 8c9854b..0000000 --- a/App/Src/tasks/output.cpp +++ /dev/null @@ -1,25 +0,0 @@ -/* - * manager.cpp - * - * Created on: Aug 31, 2023 - * Author: Carl - */ - -#include -#include -#include - -#include - - -extern "C" void outputTask(void *) -{ - while (true) - { - STA_DEBUG_PRINTLN("OUTPUT TASK RUNNING"); - - osThreadYield(); - } - - osThreadExit(); -} diff --git a/App/Src/tasks/statemachine.cpp b/App/Src/tasks/statemachine.cpp deleted file mode 100644 index e06e9c4..0000000 --- a/App/Src/tasks/statemachine.cpp +++ /dev/null @@ -1,107 +0,0 @@ -/* - * state_machine.cpp - * - * Created on: Sep 4, 2023 - * Author: Dario - */ - -#include - -#include -#include - - -tacos_states_t currentState; - -osTimerId_t lockout; -osTimerId_t failsafe; - - -void lockoutCallback(void* arg) -{ - -} - -void failsafeCallback(void* arg) -{ - changeState(TACOS_STATE_CHG_TIMEOUT); -} - - - -bool checkStateChangeConditions() -{ - // Only use the timers in this demo. - return false; -} - - -void changeState(uint8_t flag) -{ - // Stop the old timers. - osTimerStop(lockout); - osTimerStop(failsafe); - - // Set the event flags to signal other tasks that a state change has occured. Reset the flags immediately. - osEventFlagsSet(stateChangeEvent_id, flag); - osEventFlagsClear(stateChangeEvent_id, TACOS_STATE_CHG_ALL); - - uint32_t lockoutCycles = 0; - uint32_t failsafeCycles = 0; - - switch (currentState) { - case tacos_states_t::init: - currentState = tacos_states_t::started; - - lockoutCycles = 5000; - failsafeCycles = 6000; - - case tacos_states_t::started: - currentState = tacos_states_t::flying; - - lockoutCycles = 1000; - failsafeCycles = 2000; - - case tacos_states_t::flying: - currentState = tacos_states_t::landed; - - lockoutCycles = 5000; - failsafeCycles = 6000; - - break; - default: - break; - } - - // Restart the timers. - osTimerStart(lockout, lockoutCycles); - osTimerStart(failsafe, failsafeCycles); -} - - -extern "C" void startStateMachine(void*) -{ - // Initialize the stateChange event. - stateChangeEvent_id = osEventFlagsNew(NULL); - STA_ASSERT_MSG(stateChangeEvent_id != NULL, "Failed to initialize state change event!"); - - // The timers for catching errors. - lockout = osTimerNew(lockoutCallback, osTimerOnce, NULL, NULL); - failsafe = osTimerNew(failsafeCallback, osTimerOnce, NULL, NULL); - - // Check if the initialization of the timers was successful. - STA_ASSERT_MSG(lockout != 0, "Failed to initialize lockout timer"); - STA_ASSERT_MSG(failsafe != 0, "Failed to initialize failsafe timer"); - - while (true) - { - if (checkStateChangeConditions()) - { - changeState(TACOS_STATE_CHG_NATURAL); - } - } - - osThreadExit(); -} - - diff --git a/App/Src/tasks/watchdog.cpp b/App/Src/watchdog.cpp similarity index 53% rename from App/Src/tasks/watchdog.cpp rename to App/Src/watchdog.cpp index e785bce..a8b8eb1 100644 --- a/App/Src/tasks/watchdog.cpp +++ b/App/Src/watchdog.cpp @@ -12,13 +12,9 @@ namespace sta { namespace rtos { - // Implementation of the watchdog event handler. - void watchdogEventHandler(void *, uint32_t flags) + void watchdogEventHandler(void * arg, uint32_t flags) { - if (flags & STA_WATCHDOG_FLAG_HEARTBEAT) - { - STA_DEBUG_PRINTLN("PING!"); - } + STA_DEBUG_PRINTLN("Watchdog is doing stuff?"); } } } // namespace sta diff --git a/Core/Src/freertos.c b/Core/Src/freertos.c index 8f3a4e1..2cc7d3e 100644 --- a/Core/Src/freertos.c +++ b/Core/Src/freertos.c @@ -29,7 +29,6 @@ /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ -typedef StaticTask_t osStaticThreadDef_t; typedef StaticSemaphore_t osStaticMutexDef_t; /* USER CODE BEGIN PTD */ @@ -56,19 +55,6 @@ const osThreadAttr_t defaultTask_attributes = { .stack_size = 128 * 4, .priority = (osPriority_t) osPriorityNormal, }; - -/* Definitions for managerTask */ -osThreadId_t managerTaskHandle; -uint32_t managerTaskBuffer[ 128 ]; -osStaticThreadDef_t managerTaskControlBlock; -const osThreadAttr_t managerTask_attributes = { - .name = "managerTask", - .cb_mem = &managerTaskControlBlock, - .cb_size = sizeof(managerTaskControlBlock), - .stack_mem = &managerTaskBuffer[0], - .stack_size = sizeof(managerTaskBuffer), - .priority = (osPriority_t) osPriorityLow, -}; /* Definitions for uartMutex */ osMutexId_t uartMutexHandle; osStaticMutexDef_t uartMutex_cb; @@ -84,7 +70,6 @@ const osMutexAttr_t uartMutex_attributes = { /* USER CODE END FunctionPrototypes */ void StartDefaultTask(void *argument); -extern void startManagerTask(void *argument); void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */ @@ -133,9 +118,6 @@ void MX_FREERTOS_Init(void) { /* creation of defaultTask */ defaultTaskHandle = osThreadNew(StartDefaultTask, NULL, &defaultTask_attributes); - /* creation of managerTask */ - managerTaskHandle = osThreadNew(startManagerTask, NULL, &managerTask_attributes); - /* USER CODE BEGIN RTOS_THREADS */ /* add threads, ... */ /* USER CODE END RTOS_THREADS */ diff --git a/TACOS-Demo.ioc b/TACOS-Demo.ioc index 2b3194c..d7c0de1 100644 --- a/TACOS-Demo.ioc +++ b/TACOS-Demo.ioc @@ -5,7 +5,7 @@ CAD.provider= FREERTOS.FootprintOK=true FREERTOS.IPParameters=Tasks01,configUSE_NEWLIB_REENTRANT,configRECORD_STACK_HIGH_ADDRESS,configCHECK_FOR_STACK_OVERFLOW,Mutexes01,FootprintOK FREERTOS.Mutexes01=uartMutex,Static,uartMutex_cb -FREERTOS.Tasks01=defaultTask,24,128,StartDefaultTask,Default,NULL,Dynamic,NULL,NULL;managerTask,8,128,startManagerTask,As external,NULL,Static,managerTaskBuffer,managerTaskControlBlock +FREERTOS.Tasks01=defaultTask,24,128,StartDefaultTask,Default,NULL,Dynamic,NULL,NULL FREERTOS.configCHECK_FOR_STACK_OVERFLOW=1 FREERTOS.configRECORD_STACK_HIGH_ADDRESS=1 FREERTOS.configUSE_NEWLIB_REENTRANT=1 diff --git a/Tacos/include/sta/tacos/statemachine.hpp b/Tacos/include/sta/tacos/statemachine.hpp index 363776b..0a90231 100644 --- a/Tacos/include/sta/tacos/statemachine.hpp +++ b/Tacos/include/sta/tacos/statemachine.hpp @@ -42,6 +42,8 @@ namespace sta uint16_t getCurrentState(); private: + static Statemachine * _instance; + class CGuard { public: @@ -61,8 +63,6 @@ namespace sta ~Statemachine() {} - static Statemachine * _instance; - uint16_t currentState_; }; } // namespace tacos