CAN implementation for standard CAN

This commit is contained in:
@CarlWachter 2024-03-08 13:55:01 +01:00
parent f0176bb2ed
commit 7147d6de83
5 changed files with 59 additions and 24 deletions

View File

@ -78,9 +78,7 @@ namespace sta
return thread_ptr;
}
bool queueCanBusMsg(const CanDataMsg & msg, uint32_t timeout);
bool queueCanBusMsg(const CanSysMsg & msg, uint32_t timeout);
bool queueCanBusMsg(CanSysMsg & msg, uint32_t timeout);
} // namespace tacos
}

View File

@ -12,6 +12,8 @@
#include <sta/config.hpp>
#include <sta/rtos/thread.hpp>
#include <sta/rtos/queue.hpp>
#include <sta/rtos/system/can_bus.hpp>
/**
* @defgroup tacos_thread TACOS Thread
@ -126,6 +128,24 @@ namespace sta
*/
void sleep(uint32_t ticks);
#ifdef STA_CAN_BUS_ENABLE
/**
* @brief Set the ID of the CAN bus this thread is associated with.
*
* @param canID
*/
void setCanID(uint32_t canID);
/**
* @brief Get the ID of the CAN bus this thread is associated with.
*
* @return can ID
*/
uint32_t getCanID();
sta::RtosQueue<CanSysMsg> CAN_queue_;
#endif // STA_CAN_BUS_ENABLE
#ifdef STA_TACOS_WATCHDOG_ENABLED
/**
* @brief This macro wraps a given statement into waiting() and heartbeat() to make the code more readable.
@ -191,6 +211,9 @@ namespace sta
bool running_;
#ifdef STA_TACOS_WATCHDOG_ENABLED
ThreadStatus status_;
#endif // STA_TACOS_WATCHDOG_ENABLED
#ifdef STA_CAN_BUS_ENABLE
uint32_t canID_;
#endif // STA_TACOS_WATCHDOG_ENABLED
bool terminate_;
};

View File

@ -3,6 +3,8 @@
#include <sta/tacos/can_bus.hpp>
#include <sta/debug/assert.hpp>
#include <sta/tacos/manager.hpp>
// TODO fix this shit
extern CAN_HandleTypeDef hcan1;
namespace sta
@ -43,18 +45,6 @@ namespace sta
}
}
if (flags & STA_RTOS_CAN_FLAG_DATA_QUEUED)
{
// Take messages from queue until empty
CanDataMsg msg;
while (CanBus::_instance->getCanBusMsg(&msg, 0))
{
canBus_.send(msg);
}
//canBus_.tx_.processFrame();
}
if (flags & STA_RTOS_CAN_FLAG_MSG_AVAIL)
{
STA_DEBUG_PRINTLN("[event] CAN INT");
@ -68,11 +58,25 @@ namespace sta
}
}
// Synchronus message check
canBus_.processRx();
CanRxHeader rxHeader; //CAN Bus Receive Header
uint8_t canRX[8] = {0,0,0,0,0,0,0,0}; //CAN Bus Receive Buffer
canBusController_->receiveFrame(CAN_RX_FIFO0, &rxHeader, canRX);
// Create a CANSysMsg from the received data
CanSysMsg sysMsg;
sysMsg.header.sid = rxHeader.id.sid;
sysMsg.header.payloadLength = rxHeader.payloadLength;
std::memcpy(sysMsg.payload, canRX, rxHeader.payloadLength);
// Append to the correct thread's queue
for (std::shared_ptr<TacosThread> thread : Manager::instance()->getActiveThreads()){
if (thread->getCanID() == rxHeader.id.sid){
thread->CAN_queue_.put(sysMsg);
break;
}
}
// Process ISOTP transmissions
canBus_.processTx();
}
bool CanBus::queueCanBusMsg(const CanDataMsg& msg, uint32_t timeout)

View File

@ -25,12 +25,8 @@ namespace sta
{
Statemachine::instance()->requestTimedStateTransition(from, to, millis, lockout);
}
bool queueCanBusMsg(const CanDataMsg & msg, uint32_t timeout){
return CanBus::instance()->queueCanBusMsg(msg, timeout);
}
bool queueCanBusMsg(const CanSysMsg & msg, uint32_t timeout){
bool queueCanBusMsg(CanSysMsg & msg, uint32_t timeout){
return CanBus::instance()->queueCanBusMsg(msg, timeout);
}
} // namespace tacos

View File

@ -27,6 +27,10 @@ namespace sta
#ifdef STA_TACOS_WATCHDOG_ENABLED
status_{ThreadStatus::STOPPED},
#endif // STA_TACOS_WATCHDOG_ENABLED
#ifdef STA_CAN_BUS_ENABLE
canID_{0},
CAN_queue_{STA_RTOS_CAN_BUS_QUEUE_LENGTH},
#endif // STA_CAN_BUS_ENABLE
terminate_{false}
{
STA_ASSERT(stack_size >= 0);
@ -165,6 +169,16 @@ namespace sta
TacosThread::~TacosThread(){}
#ifdef STA_CAN_BUS_ENABLE
void TacosThread::setCanID(uint32_t canID){
canID_ = canID;
}
uint32_t TacosThread::getCanID(){
return canID_;
}
#endif // STA_CAN_BUS_ENABLE
} // namespace tacos
} // namespace sta