Added reworked manager / statemachine tasks, thread implementation and new directory layout

This commit is contained in:
dario
2023-09-22 15:07:15 +02:00
parent adeec2f3f9
commit 503d874a6e
13 changed files with 652 additions and 26 deletions

99
Tacos/src/manager.cpp Normal file
View File

@@ -0,0 +1,99 @@
/*
* manager.cpp
*
* Created on: Sep 19, 2023
* Author: Dario
*/
#include <sta/tacos/manager.hpp>
#include <sta/tacos/statemachine.hpp>
namespace sta
{
namespace tacos
{
void Manager::registerThread(TacosThread thread, std::list<uint16_t> states)
{
for (uint16_t state : states)
{
STA_ASSERT(state < STA_TACOS_NUM_STATES);
threads_[state].emplace(thread);
}
}
void Manager::startThreads(uint16_t state)
{
STA_ASSERT(state < STA_TACOS_NUM_STATES);
for (TacosThread thread : threads_[state])
{
if (!thread.isRunning())
{
thread.start();
}
}
}
void Manager::stopThreads(uint16_t state)
{
uint16_t currentState = Statemachine::instance()->getCurrentState();
for (uint16_t state = 0; state < STA_TACOS_NUM_STATES; ++state)
{
if (state == currentState)
{
continue;
}
for (TacosThread thread : threads_[state])
{
// If the thread is currently running but not part of the set of threads that should be running...
if (thread.isRunning() && threads_[currentState].count(thread) == 0)
{
// ...politely request termination.
thread.requestTermination();
}
}
}
}
void Manager::updateThreads()
{
uint16_t state = Statemachine::instance()->getCurrentState();
startThreads(state);
stopThreads(state);
}
void Manager::init()
{
startThreads(Statemachine::instance()->getCurrentState());
}
void Manager::func()
{
// Wait for either the termination request or the state change flag.
uint32_t flags = osEventFlagsWait(getInstance(), STA_RTOS_THREAD_FLAG_TERMINATE, osFlagsWaitAny, osWaitForever);
if ((flags & STA_RTOS_THREAD_FLAG_TERMINATE) != 0)
{
// The loop implemented by the TacosThread class should handle termination.
return;
}
// Start all new tasks and stop all the tasks that aren't supposed to be running.
updateThreads();
}
Manager::Manager()
: TacosThread{"Manager", STA_TACOS_MANAGER_PRIORITY}
{
}
} // namespace tacos
} // namespace sta

91
Tacos/src/startup.cpp Normal file
View File

@@ -0,0 +1,91 @@
/*
* startup.cpp
*
* Created on: 22 Sep 2023
* Author: Dario
*/
#include <cmsis_os2.h>
#include <usart.h>
#include <sta/rtos/mutex.hpp>
// sta-core-specific imports.
#include <sta/devices/stm32/bus/uart.hpp>
#include <sta/debug/printing/printable_uart.hpp>
#include <sta/debug/debug.hpp>
#include <sta/lang.hpp>
// Tacos-specific includes.
#include <sta/tacos/manager.hpp>
#include <sta/tacos/statemachine.hpp>
#include <sta/tacos/startup.hpp>
// The UART mutex defined in freertos.c
extern osMutexId_t uartMutexHandle;
namespace sta
{
// Here the printable used for debugging is defined.
Printable * Debug;
namespace tacos
{
void initPrintable()
{
// 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_WEAK
void onStatemachineInit()
{}
void initStatemachine()
{
onStatemachineInit();
Statemachine::instance()->start();
}
STA_WEAK
void onManagerInit()
{}
void initManager()
{
onManagerInit();
Statemachine::instance()->start();
}
} // namespace tacos
namespace rtos
{
// Override the weak implementation of startupExtras provided in rtos2-utils.
void startupExtras(void * argument)
{
tacos::initPrintable();
tacos::initStatemachine();
tacos::initManager();
}
} // namespace rtos
} // namespace sta

View File

@@ -0,0 +1,32 @@
/*
* statemachine.cpp
*
* Created on: 21 Sep 2023
* Author: Dario
*/
#include <sta/tacos/statemachine.hpp>
namespace sta
{
namespace tacos
{
void Statemachine::init()
{
}
void Statemachine::func()
{
}
uint16_t Statemachine::getCurrentState()
{
return currentState_;
}
} // namespace tacos
} // namespace sta

86
Tacos/src/thread.cpp Normal file
View File

@@ -0,0 +1,86 @@
/*
* thread.cpp
*
* Created on: Sep 14, 2023
* Author: Dario
*/
#include <sta/tacos/thread.hpp>
#include <sta/debug/assert.hpp>
#include <functional>
#include <cstring>
namespace sta
{
namespace tacos
{
TacosThread::TacosThread(const char* name, osPriority_t prio)
: RtosThread(RtosHandle<osThreadId_t>(Handle::Deferred(&instance_))),
attribs_{ .name = name, .priority = prio }
{
}
static void entry_point(void* arg)
{
STA_ASSERT(arg != nullptr);
TacosThread* instance = reinterpret_cast<TacosThread*>(arg) ;
instance->loop();
}
void TacosThread::start()
{
STA_ASSERT(!isRunning());
instance_ = osThreadNew(entry_point, this, &attribs_);
STA_ASSERT(instance_ != NULL);
}
bool TacosThread::isRunning()
{
return instance_ != NULL;
}
osThreadId_t TacosThread::getInstance()
{
STA_ASSERT(isRunning());
return instance_;
}
const char* TacosThread::getName() const
{
return attribs_.name;
}
void TacosThread::loop()
{
init();
while ((osEventFlagsGet(instance_) & STA_RTOS_THREAD_FLAG_TERMINATE) == 0)
{
func();
}
instance_ = NULL;
osThreadExit();
}
bool TacosThread::operator==(const TacosThread& other) const
{
return std::strcmp(this->getName(), other.getName()) == 0;
}
bool TacosThread::operator<(const TacosThread& other) const
{
return std::strcmp(this->getName(), other.getName()) < 0;
}
} // namespace tacos
} // namespace sta