Skip to content

Commit ed687f0

Browse files
committed
theory of operation updated
1 parent 91992f0 commit ed687f0

1 file changed

Lines changed: 183 additions & 0 deletions

File tree

docs/theory_of_operation.md

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,186 @@ Controller Area Network (CAN) is a multi-master, message-oriented serial bus pro
4747

4848
## 4. CAN Frame Structures (classical CAN)
4949
### 4.1 Data Frame (standard format — 11-bit ID)
50+
51+
52+
<p align="center">
53+
<img src="./images_design/standard_frame.png"
54+
alt="Top-Level Architecture" width="600">
55+
</p>
56+
57+
58+
- SOF: Start of frame (dominant bit)
59+
60+
- Identifier: Message ID (priority)
61+
62+
- RTR: Remote Transmission Request (dominant=Data frame, recessive=Remote)
63+
64+
- IDE: Identifier Extension bit (dominant => standard)
65+
66+
- DLC: Data length code (0–8)
67+
68+
- CRC: 15-bit CRC + CRC delimiter
69+
70+
- ACK: 2-bit field (ACK slot + ACK delimiter)
71+
72+
- EOF: End-of-frame (7 recessive bits)
73+
74+
### 4.2 Extended Data Frame (29-bit ID)
75+
76+
<p align="center">
77+
<img src="./images_design/extended_frame.png"
78+
alt="Top-Level Architecture" width="600">
79+
</p>
80+
81+
Same fields but identifier split into base ID and extended ID with IDE=1 and an extended ID field present.
82+
83+
### 4.3 Remote Frame
84+
85+
Same as Data Frame but RTR set so a node requests data for the specified ID
86+
87+
### 4.4 Error & Overload Frames
88+
89+
Error Frame: Sent by any node that detects bus errors — consists of an error flag and an error delimiter.
90+
91+
Overload Frame: Used to request additional delay between frames (rare in good systems).
92+
93+
## 5. Core CAN Controller Architecture
94+
95+
A typical CAN controller (hardware IP) comprises modular blocks. Below is a recommended decomposition for documentation and RTL implementation.
96+
97+
### 5.1 Physical Interface / Transceiver
98+
99+
Handles differential signalling (CAN_H / CAN_L) and converts to single-ended RX/TX logic levels.
100+
101+
Manages bus wake, slope control (optionally), and fail-safe.
102+
103+
### 5.2 Bit Timing Unit (BTU)
104+
105+
Generates bit clocks and sample points according to configured CAN bit timing registers (prescaler, PROP, PHASE1, PHASE2).
106+
107+
Produces sample_point and sample_enable signals used by the receiver logic.
108+
109+
### 5.3 TX Logic / Arbitration Unit
110+
111+
Accepts frames from CPU or DMA into a transmit queue (FIFO).
112+
113+
Handles arbitration: outputs TX bitstream; if node loses arbitration (reads recessive when it output dominant), it withdraws and becomes a receiver.
114+
115+
Handles RTR, IDE, and frame formatting including stuffing on the transmit side.
116+
117+
### 5.4 RX Logic / Frame Assembler
118+
119+
Samples bus values at sample points; performs de-stuffing and reassembly of bits into fields.
120+
121+
Runs CRC calculation in parallel to check integrity.
122+
123+
Implements acceptance filtering to decide whether to deliver frame to host (mask+filter registers or hardware filter engine).
124+
125+
### 5.5 CRC Unit
126+
127+
Implements CRC-15 (classical CAN) generator/checker used on both transmit and receive paths.
128+
129+
### 5.6 Error Management Unit
130+
131+
Tracks transmit and receive error counters (TEC, REC) and node state: Error Active, Error Passive, Bus Off as per CAN protocol.
132+
133+
Generates Error Frames when needed and enforces retransmissions and bus-off recovery behavior.
134+
135+
### 5.7 Acceptance Filter / Mailbox System
136+
137+
One or multiple hardware mailboxes for prioritized message buffers and hardware filters for reduced CPU load.
138+
139+
Supports ID masking, full ID match, range filters, or FIFO-based buffering depending on IP complexity.
140+
141+
### 5.8 Interface to Host (CPU / Bus)
142+
143+
Register map: control/status registers, mailboxes, acceptance filters, bit timing registers, error counters, interrupt flags.
144+
145+
Common host interfaces: APB, AHB, AXI, Wishbone, custom bus.
146+
147+
### 5.9 Debug & Test Support
148+
149+
Loopback modes (internal/external), timestamping, trace buffers, test registers, and statistics counters.
150+
151+
## 6. Data Flow — Transmit Path
152+
153+
- `Frame creation`: CPU/DMA writes identifier, DLC, data bytes into transmit mailbox or FIFO, sets a request_to_send bit.
154+
155+
- `Priority selection`: If multiple mailboxes present, the TX arbiter selects the highest priority pending frame.
156+
157+
- `Start-of-transmission`: On bus idle, the TX logic issues SOF and starts serializing fields.
158+
159+
- `Arbitration`: TX unit drives each identifier bit on the bus while sampling. If a recessive bit is observed while transmitter drove dominant, that node loses arbitration and moves to listening mode — it will retry later automatically.
160+
161+
- `Frame stuffing & CRC`: Transmit side inserts stuff bits and appends CRC.
162+
163+
- `ACK handling`: After CRC delimiter, transmitter samples the ACK slot — if no node drove ACK (i.e., ACK remains recessive), transmitter marks the frame as not acknowledged (error handling).
164+
165+
- `Retransmission / Error handling`: On failure (e.g., no ACK, detected error), hardware automatically retries transmission up to configured attempts, while updating TEC/REC and possibly entering error passive/bus-off states.
166+
167+
- `Completion`: On success, TX mailbox flagged complete, interrupt raised if enabled.
168+
169+
## 7. Data Flow — Receive Path
170+
171+
- `Bit sampling & synchronization`: BTU provides sample points; the receiver samples the bus and synchronizes on SOF.
172+
173+
- `De-stuffing`: Receiver removes stuff bits inserted by transmitter (counts consecutive bits and drops inserted bits).
174+
175+
- `Field demarcation & CRC check`: Bits are collected into identifier, control, data, CRC and CRC is checked on the fly or after reception.
176+
177+
- `Acceptance filter`: If the frame matches a filter, it is moved to a RX mailbox or FIFO; otherwise discarded.
178+
179+
- `ACK generation`: If CRC OK, receiver asserts ACK by driving dominant in the ACK slot.
180+
181+
- `Host notification`: RX mailbox ready flag and interrupt triggered; CPU reads data from RX mailbox.
182+
183+
## 8. Arbitration & Determinism
184+
185+
Bitwise arbitration compares transmitted bits on the bus with the node's expected bit. Dominant wins — nodes that lose arbitration cease transmitting immediately, preserving bus time and determinism.
186+
187+
Deterministic behavior: Priority is solely identifier-based. Lower numerical ID = higher priority.
188+
189+
## Error Detection & Handling (High-level)
190+
### 9.1 Error detection mechanisms
191+
192+
Bit monitoring: Transmitting node monitors bus to detect if what it sent differs from actual bus state.
193+
194+
Bit stuffing errors: Detected if more than five identical consecutive bits appear without a stuff bit.
195+
196+
Format errors: Wrong fixed-format fields (e.g., wrong delimiter values).
197+
198+
ACK error: No receiver asserted ACK.
199+
200+
CRC error: CRC mismatch detected by receiver.
201+
202+
### 9.2 Error counters & states
203+
204+
Transmit Error Counter (TEC) and Receive Error Counter (REC) are incremented/decremented following specified rules.
205+
206+
Node states: Error Active → Error Passive → Bus Off. Bus-Off requires recovery sequence (e.g., 128 occurrences or vendor-specific behavior).
207+
208+
### 9.3 Retransmission
209+
210+
On detected transmission errors, hardware typically attempts automatic retransmission until successful or until bus-off threshold hit.
211+
212+
## 10. Timing Considerations & Bit Timing Configuration
213+
214+
- Bit time divided into: SYNC_SEG (1 Tq), PROP_SEG, PHASE_SEG1, PHASE_SEG2.
215+
216+
- Sample point typically around 70–80% of bit time for single-sample nodes, or triple-sampling near sample point for improved noise immunity.
217+
218+
- Tolerance & synchronization: Resynchronization allowed by phase segments and sample point adjustments using SJW (synchronization jump width).
219+
220+
- Configuration registers: prescaler, prop, phase1, phase2, SJW — must be set according to bus length, node count and bit rate to meet ISO timing constraints.
221+
222+
## 11. Implementation Notes for RTL / FPGA
223+
224+
Clocking: Use a high-frequency clock and a BTU to derive sample ticks; avoid metastability with proper synchronizers on async RX input.
225+
226+
FIFO Depths: Size TX/RX FIFO based on expected peak traffic and interrupt latency.
227+
228+
Hardware Filters: Implement simple mask+filter for low complexity; implement CAM or hash filters for higher performance.
229+
230+
Test modes: Provide internal loopback (no transceiver) and external loopback for validation.
231+
232+
Formal checks: Add assertions for frame format, FIFO overflow/underflow, error counter thresholds.

0 commit comments

Comments
 (0)