mirror of
https://git.intern.spaceteamaachen.de/ALPAKA/TACOS.git
synced 2025-06-10 16:45:59 +00:00
130 lines
3.0 KiB
C++
130 lines
3.0 KiB
C++
#ifndef INCLUDE_TACOS_CAN_BUS_HPP_
|
|
#define INCLUDE_TACOS_CAN_BUS_HPP_
|
|
|
|
#include <sta/config.hpp>
|
|
|
|
#ifdef STA_TACOS_CAN_BUS_ENABLED
|
|
|
|
#include <sta/rtos/queue.hpp>
|
|
#include <sta/rtos/system/can_bus.hpp>
|
|
#include <sta/debug/debug.hpp>
|
|
#include <sta/lang.hpp>
|
|
|
|
#include <sta/tacos/thread.hpp>
|
|
#include <sta/tacos/statemachine.hpp>
|
|
|
|
#ifndef STA_STM32_CAN_ENABLED
|
|
# error "CAN has to be enabled in the IOC first"
|
|
#endif // STA_STM32_CAN_ENABLED
|
|
|
|
/**
|
|
* @defgroup tacos_can_bus Can Bus Task
|
|
* @ingroup tacos
|
|
* @brief Can Bus task for TACOS.
|
|
*/
|
|
|
|
namespace sta
|
|
{
|
|
namespace tacos
|
|
{
|
|
/**
|
|
* @brief Can Bus implementation for Tacos.
|
|
*
|
|
* @ingroup tacos_can_bus
|
|
*/
|
|
class CanBus : public TacosThread
|
|
{
|
|
public:
|
|
|
|
CanBus();
|
|
|
|
/**
|
|
* @brief Getter function for the singleton instance.
|
|
*/
|
|
static CanBus* instance()
|
|
{
|
|
static CGuard g;
|
|
|
|
if (!_instance)
|
|
{
|
|
// Create the can bus singleton instance.
|
|
CanBus::_instance = new CanBus();
|
|
}
|
|
|
|
return _instance;
|
|
}
|
|
|
|
/**
|
|
* @brief Place system message in CAN driver TX queue.
|
|
*
|
|
* @param msg Message to transmit
|
|
* @param timeout Timeout for placing message (0 = no wait, osWaitForever = blocking)
|
|
* @return True if message was queued successfully
|
|
*/
|
|
bool queueCanBusMsg(const CanSysMsg msg, uint32_t timeout);
|
|
|
|
/**
|
|
* @brief Retrieve system message from CAN driver TX queue.
|
|
*
|
|
* @param[out] msg Destination for retrieved message
|
|
* @param timeout Timeout for retrieving message (0 = no wait, osWaitForever = blocking)
|
|
* @return True if message was retrieved successfully
|
|
*/
|
|
bool getCanBusMsg(CanSysMsg * msg, uint32_t timeout);
|
|
|
|
/**
|
|
* @brief Buffers incoming message and sets event to notify the system.
|
|
*
|
|
* @param fifo FIFO number of the received message.
|
|
*/
|
|
void canCallback(uint32_t fifo);
|
|
|
|
void init() override;
|
|
void func() override;
|
|
|
|
private:
|
|
static CanBus * _instance;
|
|
|
|
class CGuard
|
|
{
|
|
public:
|
|
~CGuard()
|
|
{
|
|
if( NULL != CanBus::_instance )
|
|
{
|
|
delete CanBus::_instance;
|
|
CanBus::_instance = NULL;
|
|
}
|
|
}
|
|
};
|
|
|
|
sta::STM32CanController * canBusController_;
|
|
|
|
RtosQueue<CanSysMsg> canBusSysQueue_;
|
|
RtosQueue<CanSysMsg> canBusRxQueue_;
|
|
|
|
AlpakaCanBus canBus_;
|
|
|
|
static RtosEvent messageEvent;
|
|
};
|
|
|
|
/**
|
|
* @brief Handle system messages received over the CAN bus. Called as soon as a message is received. If
|
|
* the message is a system message, it will be handled here. If the message is not a system message, it will be
|
|
* passed to the appropriate thread's queue.
|
|
*
|
|
* @param header The header of the received message.
|
|
* @param payload The payload of the received message.
|
|
*
|
|
* @return True if the message was a system message.
|
|
*/
|
|
STA_WEAK
|
|
bool handleSysMessage(CanMsgHeader & header, uint8_t * payload);
|
|
|
|
} /* namespace tacos */
|
|
|
|
} /* namespace sta */
|
|
|
|
#endif /* STA_TACOS_CAN_BUS_ENABLED */
|
|
#endif /* INCLUDE_TACOS_CAN_BUS_HPP_ */
|