TACOS/src/startup.cpp

179 lines
3.9 KiB
C++

/*
* startup.cpp
*
* Created on: 22 Sep 2023
* Author: Dario
*/
/**
* @defgroup tacos TACOS Library
* @brief TACOS library
*
* @details This library contains the internal functions of TACOS. It is not intended to be used by the user, but is documented rather for anyone wanting to contribute to the project.
*/
#include <sta/config.hpp>
#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/devices/stm32/init.hpp>
#include <sta/debug/printing/printable_uart.hpp>
#include <sta/debug/debug.hpp>
#include <sta/debug/assert.hpp>
#include <sta/lang.hpp>
// rtos2-utils-specific includes.
#include <sta/rtos/system/startup.hpp>
#include <sta/rtos/system/events.hpp>
// Tacos-specific includes.
#include <sta/tacos/c_api/startup.h>
#include <sta/tacos/statemachine.hpp>
#include <sta/tacos/watchdog.hpp>
#include <sta/tacos/can_bus.hpp>
// The UART mutex defined in freertos.c
extern osMutexId_t uartMutexHandle;
/**
* @defgroup tacos_startup Startup
* @ingroup tacos
* @brief Functions that are called during startup.
*/
namespace sta
{
#ifdef STA_DEBUGGING_ENABLED
// Here the printable used for debugging is defined.
Printable * Debug;
namespace tacos
{
/**
* @brief Function that returns the UART handle used for debugging. Override it in userspace to adjust.
*
* @ingroup tacos_startup
*/
STA_WEAK
UART_HandleTypeDef * getUARThandle(){
return &STA_STM32_USART_HANDLE;
}
/**
* @brief Function that initializes the printable object given by getUARThandle().
*
* @ingroup tacos_startup
*/
void initPrintable()
{
// Initialize the mutex for UART communication.
RtosMutex * mutex = new RtosMutex("uart");
// Initialize the UART interface and printable object.
UARTSettings settings = { .mode = UARTMode::RX_TX };
STM32UART * intf_ptr = new STM32UART(getUARThandle(), settings, mutex);
Debug = new PrintableUART(intf_ptr);
}
} // namespace tacos
#endif // STA_DEBUGGING_ENABLED
namespace tacos
{
/**
* @brief Function that is called before the statemachine task is started. Override it to
* adjust the statemachine to your specifications.
*
* @ingroup tacos_startup
*/
STA_WEAK
void onStatemachineInit()
{}
void initStatemachine()
{
onStatemachineInit();
Statemachine::instance()->start();
}
#ifdef STA_TACOS_WATCHDOG_ENABLED
STA_WEAK
void onWatchdogInit()
{}
void initWatchdog()
{
onWatchdogInit();
Watchdog::instance()->start();
}
#endif // STA_TACOS_WATCHDOG_ENABLED
#ifdef STA_TACOS_CAN_BUS_ENABLED
/**
* @brief Function that is called before the Can Bus task is started. Override it to adjust
* the Can bus to your specifications.
*
* @ingroup tacos_startup
*/
STA_WEAK
void onCanBusInit()
{}
void initCanBus()
{
onCanBusInit();
CanBus::instance()->start();
}
#endif //STA_TACOS_CAN_BUS_ENABLED
void startupExtras(void * argument)
{
#ifdef STA_DEBUGGING_ENABLED
tacos::initPrintable();
#endif // STA_DEBUGGING_ENABLED
tacos::initStatemachine();
#ifdef STA_TACOS_WATCHDOG_ENABLED
tacos::initWatchdog();
#endif // STA_TACOS_WATCHDOG_ENABLED
#ifdef STA_TACOS_CAN_BUS_ENABLED
tacos::initCanBus();
#endif // STA_TACOS_CAN_BUS_ENABLED
}
} // namespace tacos
} // namespace sta
void startTACOS(void * arg)
{
STA_ASSERT_MSG(osKernelGetState() != osKernelInactive, "Cannot call startTACOS() before osKernelInitialize()");
// Initialize HAL
sta::initHAL();
// Initialize RTOS system resources
sta::rtos::initSystem();
// Call further initialization code
sta::tacos::startupExtras(arg);
// Wake threads
#ifdef STA_RTOS_SYSTEM_EVENTS_ENABLE
sta::rtos::signalStartupEvent();
#endif // STA_RTOS_SYSTEM_EVENTS_ENABLE
// Check if called from thread
if (osThreadGetId() != nullptr)
{
// Terminate current thread
osThreadExit();
}
}