reworked CAN Stack

This commit is contained in:
2025-08-24 21:49:09 +02:00
parent 3e69485696
commit c6d65f50bf
11 changed files with 721 additions and 447 deletions

View File

@@ -0,0 +1,59 @@
#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)
};
// ==== 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 (nutzen MCP-APIs)
uint8_t CAN_HAL_GetErrorFlags(); // Intern: getError()
void CAN_HAL_GetErrorCounters(uint8_t& tec, uint8_t& rec); // TX/RX Error Counter