Files
Kettenoeler/Software/include/can_hal.h
Marcel Peterkau 98629b744d
Some checks failed
CI-Build/Kettenoeler/pipeline/head There was a failure building this commit
added Function to create CAN-Traces from WebUI
2025-08-26 23:31:35 +02:00

79 lines
2.4 KiB
C

#pragma once
#include <Arduino.h>
#include <mcp_can.h>
#include "common.h"
// ==== Board-Pin ====
// Falls nicht bereits global definiert:
#ifndef GPIO_CS_CAN
#define GPIO_CS_CAN 5
#endif
// ==== Öffentliche, einzige Instanz ====
extern MCP_CAN CAN0;
// ==== Init-Config ====
struct CanHalConfig {
uint8_t baud = CAN_500KBPS; // laut Lib
uint8_t clock = MCP_16MHZ; // 8/16 MHz je nach Quarz
uint16_t listenOnlyProbeMs = 0; // optionaler kurzer Hörtest
uint16_t modeSettleMs = 10; // Wartezeit für Mode-Set (Retry-Fenster)
};
// ==== Universeller Filter-Descriptor ====
struct CanFilter {
uint32_t id; // 11-bit oder 29-bit Roh-ID
bool ext; // false = STD(11-bit), true = EXT(29-bit)
};
// =====================
// Trace / Logging Types
// =====================
struct CanLogFrame {
uint32_t ts_ms;
uint32_t id;
bool ext;
bool rx; // true = RX, false = TX
uint8_t dlc;
uint8_t data[8];
};
using CanTraceSink = void (*)(const CanLogFrame& f);
// ==== API ====
// 1) Einmalige Hardware-Initialisierung + integrierter Loopback-Selftest.
// - begin()
// - LOOPBACK senden/echo prüfen (ohne Bus)
// - optional: ListenOnly-Probe (nur Heuristik)
// - Default: Filter/Masks weit offen, NORMAL-Mode
// Rückgabe: true = bereit; false = Fehler (kein CAN verwenden)
bool CAN_HAL_Init(const CanHalConfig& cfg);
// Ist die HAL bereit (nach Init)?
bool CAN_HAL_IsReady();
// Bestätigter Moduswechsel (CONFIG/NORMAL/LISTENONLY/LOOPBACK)
// true = erfolgreich; setzt bei Misserfolg DTC_CAN_TRANSCEIVER_FAILED
bool CAN_HAL_SetMode(uint8_t mode);
// Masken/Filter
bool CAN_HAL_SetMask(uint8_t bank, bool ext, uint32_t rawMask);
bool CAN_HAL_SetStdMask11(uint8_t bank, uint16_t mask11);
void CAN_HAL_ClearFilters();
bool CAN_HAL_AddFilter(const CanFilter& f);
bool CAN_HAL_SetFilters(const CanFilter* list, size_t count);
// Non-blocking IO
bool CAN_HAL_Read(unsigned long& id, uint8_t& len, uint8_t data[8]); // true = Frame gelesen
uint8_t CAN_HAL_Send(unsigned long id, bool ext, uint8_t len, const uint8_t* data); // CAN_OK bei Erfolg
// Diagnose/Utilities
uint8_t CAN_HAL_GetErrorFlags(); // Intern: getError()
void CAN_HAL_GetErrorCounters(uint8_t& tec, uint8_t& rec); // TX/RX Error Counter
// Trace / Sniffer
void CAN_HAL_SetTraceSink(CanTraceSink sink);
void CAN_HAL_EnableRawSniffer(bool enable);
bool CAN_HAL_IsRawSnifferEnabled();