CAN w/o isotp

This commit is contained in:
CarlWachter 2024-04-30 15:27:01 +02:00
parent a6704f4c45
commit 21e50a5ed1
2 changed files with 3 additions and 165 deletions

View File

@ -29,8 +29,6 @@
#include <sta/bus/can/controller.hpp>
#include <sta/devices/stm32/can.hpp>
#include <sta/rtos/c_api/can_msg.h>
#include <sta/proto/isotp/transmitter.hpp>
#include <sta/proto/isotp/receiver.hpp>
#include <sta/time.hpp>
#include <cstdint>
@ -78,10 +76,6 @@
* @brief CAN system message in queue.
*/
#define STA_RTOS_CAN_FLAG_SYS_QUEUED 0x1U << 3
/**
* @brief Show ISOTP statistics.
*/
#define STA_RTOS_CAN_FLAG_SHOW_STATS 0x1U << 4
/**
* @brief CAN SID bits used for system messages.
@ -126,14 +120,11 @@ namespace sta
class AlpakaCanBus
{
public:
using SysMsgHandler = void (*)(const CanRxHeader &, const uint8_t *);
using DataMsgHandler = void (*)(const IsotpMessage &);
static const uint8_t FIFO_SYS = 0;
static const uint8_t FIFO_DATA = 1;
public:
AlpakaCanBus(CanController * controller, TimeMsFn timeMs, SysMsgHandler sysMsgHandler, DataMsgHandler dataMsgHandler);
AlpakaCanBus(CanController * controller, TimeMsFn timeMs);
/**
@ -143,51 +134,14 @@ namespace sta
*/
void send(const CanSysMsg & msg);
/**
* @brief Send data message.
*
* @param msg Message
*/
void send(const CanDataMsg & msg);
/**
* @brief Process transmissions.
*
* Call regularly to advance transmission.
*/
void processTx();
/**
* @brief Process received CAN messages.
*/
void processRx();
/**
* @brief Display ISOTP TX/RX statistics.
*/
void showStatistics();
private:
/**
* @brief Configure CAN filters.
*/
void setupSubscriptions();
/**
* @brief Handle received data message CAN frames.
*
* @param header CAN frame header
* @param payload Payload buffer
*/
void receiveDataFrame(const CanRxHeader & header, const uint8_t * payload);
private:
CanController * controller_;
IsotpTransmitter tx_;
IsotpReceiver rx_;
SysMsgHandler handleSysMsg_;
DataMsgHandler handleDataMsg_;
};
} // namespace sta

View File

@ -10,8 +10,6 @@
#include <sta/debug/debug.hpp>
#include <sta/bus/can/subscribable.hpp>
#include <sta/lang.hpp>
#include <sta/proto/isotp/transmitter.hpp>
#include <sta/proto/isotp/receiver.hpp>
#include <sta/rtos/defs.hpp>
#include <sta/rtos/system/events.hpp>
#include <sta/devices/stm32/hal.hpp>
@ -62,12 +60,9 @@ namespace debug
namespace sta
{
AlpakaCanBus::AlpakaCanBus(CanController * controller, TimeMsFn timeMs, SysMsgHandler sysMsgHandler, DataMsgHandler dataMsgHandler)
: controller_{controller}, tx_{controller, timeMs}, rx_{controller, timeMs}, handleSysMsg_{sysMsgHandler}, handleDataMsg_{dataMsgHandler}
AlpakaCanBus::AlpakaCanBus(CanController * controller, TimeMsFn timeMs)
: controller_{controller}
{
STA_ASSERT(handleSysMsg_ != nullptr);
STA_ASSERT(handleDataMsg_ != nullptr);
setupSubscriptions();
}
@ -83,91 +78,6 @@ namespace sta
controller_->sendFrame(header, msg.payload);
}
void AlpakaCanBus::send(const CanDataMsg & msg)
{
CanFrameId frameID;
frameID.format = static_cast<CanIdFormat>(msg.header.format);
frameID.sid = msg.header.sid;
frameID.eid = msg.header.eid;
// Start transmission via ISO-TP
tx_.send(frameID, msg.payload, msg.header.payloadLength);
}
void AlpakaCanBus::processTx()
{
tx_.process();
}
void AlpakaCanBus::processRx()
{
for (auto fifo : controller_->getPendingRxFifos())
{
CanRxHeader header;
uint8_t payload[STA_RTOS_CAN_BUS_MAX_PAYLOAD_SIZE];
if (controller_->receiveFrame(fifo, &header, payload))
{
//debug::displayFrameUART(frame);
// Forward frame to callback
switch (fifo)
{
case FIFO_SYS:
handleSysMsg_(header, payload);
break;
case FIFO_DATA:
receiveDataFrame(header, payload);
break;
default:
STA_ASSERT(false);
}
}
}
}
void AlpakaCanBus::showStatistics()
{
STA_DEBUG_PRINTLN();
STA_DEBUG_PRINTLN("# ######################");
STA_DEBUG_PRINTLN("# ## ISOTP statistics ##");
STA_DEBUG_PRINTLN("# ######################");
STA_DEBUG_PRINTLN("#");
const auto & txStats = tx_.stats();
STA_DEBUG_PRINTLN("# Transmitter");
STA_DEBUG_PRINT("# messages: ");
STA_DEBUG_PRINTLN(txStats.messages);
STA_DEBUG_PRINT("# blocks: ");
STA_DEBUG_PRINTLN(txStats.blocks);
STA_DEBUG_PRINT("# frames: ");
STA_DEBUG_PRINTLN(txStats.frames);
STA_DEBUG_PRINT("# timeouts: ");
STA_DEBUG_PRINTLN(txStats.timeouts);
STA_DEBUG_PRINTLN("#");
const auto & rxStats = rx_.stats();
STA_DEBUG_PRINTLN("# Receiver");
STA_DEBUG_PRINT("# messages: ");
STA_DEBUG_PRINTLN(rxStats.messages);
STA_DEBUG_PRINT("# blocks: ");
STA_DEBUG_PRINTLN(rxStats.blocks);
STA_DEBUG_PRINT("# frames: ");
STA_DEBUG_PRINTLN(rxStats.frames);
STA_DEBUG_PRINT("# timeouts: ");
STA_DEBUG_PRINTLN(rxStats.timeouts);
STA_DEBUG_PRINT("# flow control errors: ");
STA_DEBUG_PRINTLN(rxStats.flowErrors);
STA_DEBUG_PRINT("# overflows: ");
STA_DEBUG_PRINTLN(rxStats.overflows);
STA_DEBUG_PRINTLN();
}
void AlpakaCanBus::setupSubscriptions()
{
// Make sure to receive all messages
@ -192,32 +102,6 @@ namespace sta
filter.fifo = FIFO_DATA;
controller_->configureFilter(FIFO_DATA, filter, true);
}
void AlpakaCanBus::receiveDataFrame(const CanRxHeader & header, const uint8_t * payload)
{
// Write frame payload to DebugSerial
STA_DEBUG_PRINTLN("[event] RX data frame");
debug::printPayloadHex(payload, header.payloadLength);
// Process RX frame
auto handle = rx_.processFrame(header, payload);
if (handle != IsotpReceiver::INVALID_HANDLE)
{
// Get message if completed
IsotpMessage msg;
if (rx_.getMessage(handle, &msg))
{
handleDataMsg_(msg);
}
// Handle FC responses
rx_.processFC(handle);
}
// Process TX frame
tx_.processFrame(header, payload);
}
} // namespace sta
#endif // STA_CAN_BUS_ENABLE