ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
mcp251xfd_defs.h
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4#include <cstddef>
5#include <cstdint>
6
8
9namespace esphome::mcp251xfd {
10
11// Values are the oscillator frequency in MHz.
12enum class CanClock : uint8_t { CAN_CLOCK_4MHZ = 4, CAN_CLOCK_20MHZ = 20, CAN_CLOCK_40MHZ = 40 };
13
14enum class CanMode : uint8_t {
20};
21
22static constexpr uint8_t INSTRUCTION_RESET = 0x00;
23static constexpr uint8_t INSTRUCTION_WRITE = 0x20;
24static constexpr uint8_t INSTRUCTION_READ_CRC = 0xB0;
25
26static constexpr uint16_t REG_CON = 0x000;
27static constexpr uint16_t REG_NBTCFG = 0x004;
28static constexpr uint16_t REG_INT = 0x01C;
29static constexpr uint16_t REG_TREC = 0x034;
30// FIFO 1 transmits. FIFO 2 receives. TXQ and TEF are disabled.
31static constexpr uint16_t REG_TX_FIFO_CON = 0x05C;
32static constexpr uint16_t REG_TX_FIFO_STA = 0x060;
33static constexpr uint16_t REG_RX_FIFO_CON = 0x068;
34static constexpr uint16_t REG_RX_FIFO_STA = 0x06C;
35static constexpr uint16_t REG_FLTCON = 0x1D0;
36static constexpr uint16_t REG_FLTOBJ0 = 0x1F0;
37static constexpr uint16_t REG_OSC = 0xE00;
38
39static constexpr uint16_t RAM_START = 0x400;
40static constexpr uint16_t RAM_SIZE = 2048;
41// FIFO objects are two header words and the payload, no timestamp. CLASSIC uses 8-byte payloads.
42static constexpr size_t MESSAGE_HEADER_SIZE = 8;
43static constexpr size_t MESSAGE_SIZE = MESSAGE_HEADER_SIZE + 64;
44static constexpr size_t CLASSIC_MESSAGE_SIZE = MESSAGE_HEADER_SIZE + 8;
45// The CON to TDC block written in setup is the largest register write.
46static constexpr size_t MAX_REGISTER_WRITE_SIZE = 16;
47// Largest object this build sends, and largest read: the header with 8 data bytes or the rest of a payload
48static constexpr size_t MAX_MESSAGE_SIZE = MESSAGE_HEADER_SIZE + canbus::CAN_FRAME_DATA_LENGTH;
49static constexpr size_t MAX_READ_SIZE = std::max<size_t>(MESSAGE_HEADER_SIZE + canbus::CAN_MAX_DATA_LENGTH,
50 canbus::CAN_FRAME_DATA_LENGTH - canbus::CAN_MAX_DATA_LENGTH);
51static constexpr uint32_t TX_FIFO_DEPTH = 8;
52static constexpr uint32_t RX_FIFO_DEPTH = 20;
53static constexpr uint32_t CLASSIC_RX_FIFO_DEPTH = 32;
54static_assert((TX_FIFO_DEPTH + RX_FIFO_DEPTH) * MESSAGE_SIZE <= RAM_SIZE);
55static_assert((TX_FIFO_DEPTH + CLASSIC_RX_FIFO_DEPTH) * CLASSIC_MESSAGE_SIZE <= RAM_SIZE);
56
57static constexpr uint32_t CON_ISO_CRC_ENABLE = 1UL << 5;
58static constexpr uint8_t CON_MODE_MASK = 0x07;
59static constexpr uint8_t CON_MODE_SHIFT = 5;
60static constexpr uint8_t INT_SYSTEM_ERROR = 1U << 4; // SERRIF in byte 1 of INT.
61static constexpr uint8_t INT_MODE_CHANGE = 1U << 3; // MODIF in byte 0 of INT.
62static constexpr uint8_t INT_CAN_ERROR = 1U << 5; // CERRIF in byte 1 of INT.
63static constexpr uint8_t INT_INVALID_MESSAGE = 1U << 7; // IVMIF in byte 1 of INT.
64static constexpr uint8_t INT_RX_OVERFLOW = 1U << 3; // RXOVIF in byte 1 of INT.
65static constexpr uint8_t INT_RX_ENABLE = 1U << 1; // RXIE in byte 2 of INT.
66static constexpr uint32_t FIFO_PAYLOAD_8 = 0;
67static constexpr uint32_t FIFO_PAYLOAD_64 = 7UL << 29;
68static constexpr uint32_t FIFO_TX_ENABLE = 1UL << 7;
69static constexpr uint8_t FIFO_RX_NOT_EMPTY_ENABLE = 1U << 0;
70static constexpr uint8_t FIFO_RX_OVERFLOW_ENABLE = 1U << 3;
71static constexpr uint8_t FIFO_NOT_FULL_NOT_EMPTY = 1U << 0;
72static constexpr uint8_t FIFO_RX_OVERFLOW = 1U << 3;
73static constexpr uint8_t FIFO_INCREMENT = 1U << 0; // Byte 1 of FIFOCON.
74static constexpr uint8_t FIFO_TX_REQUEST = 1U << 1;
75static constexpr uint8_t FIFO_RESET = 1U << 2;
76static constexpr uint8_t FILTER_ENABLE_RX_FIFO = 0x80 | 2;
77static constexpr uint8_t OSC_PLL_ENABLE = 1U << 0;
78static constexpr uint8_t OSC_CLKODIV_10 = 3U << 5; // Reset value: CLKO is SYSCLK / 10.
79static constexpr uint8_t OSC_PLL_READY = 1U << 0; // Byte 1 of OSC.
80static constexpr uint8_t OSC_READY = 1U << 2;
81static constexpr uint8_t ERROR_RX_PASSIVE = 1U << 3; // Byte 2 of TREC.
82static constexpr uint8_t ERROR_TX_PASSIVE = 1U << 4;
83static constexpr uint8_t ERROR_BUS_OFF = 1U << 5;
84
85static constexpr uint8_t MESSAGE_DLC_MASK = 0x0F;
86static constexpr uint8_t MESSAGE_IDE = 1U << 4;
87static constexpr uint8_t MESSAGE_RTR = 1U << 5;
88static constexpr uint8_t MESSAGE_BRS = 1U << 6;
89static constexpr uint8_t MESSAGE_FDF = 1U << 7;
90static constexpr uint8_t MESSAGE_ESI = 1U << 0; // Byte 5 of the message header.
91static constexpr uint32_t EXTENDED_ID_LOW_MASK = 0x3FFFF;
92
93static constexpr uint32_t FAULT_LOG_INTERVAL_MS = 5000;
94static constexpr uint32_t MODE_RECOVERY_INTERVAL_MS = 5000;
95
96} // namespace esphome::mcp251xfd
static void uint32_t