CAN-USB4 Protokoll V1
Aus iSysBus Doku
Diese Seite beschreibt die Version 1 des CAN-USB4-Protokolls.
Inhaltsverzeichnis |
Paketaufbau
Die einzelnen Datenpakete besitzen folgenden Aufbau:
| SOF | LEN | CTRL | Daten | EOF |
| 2 Byte | 1 Byte | 1 Byte | variabel | 2 Bytes |
| 0xAFFE | 0xDEAD |
Das SOF (Start Of Frame) Feld besteht aus dem Startwort 0xAFFE. Das EOF (End of Frame) Feld enthält das Wort 0xDEAD.
Das Feld LEN gibt die Anzahl der Bytes der kompletten Nachricht an, inklusive SOF und LEN. Das CTRL (Control) Feld enthält den TOF (Type Of Frame).
| CTRL | Typ-Info | Subtyp-Info | |
| 1 Byte | |||
| 000 | 00000 | Befehle | Reset |
| 00001 | Modus | ||
| 00010 | RX Fehlercounter | ||
| 00011 | TX Fehlercounter | ||
| 00100 | Initialisierung | ||
| 00101 | Taktrate | ||
| 00110 | Version | ||
| 00111 | Bufsize | ||
| 01001 | Time-Sync | ||
| 001 | xxxxx | Antworten | siehe Befehle |
| 010 | xxxxx | Standard Frame | - |
| 011 | xxxxx | Extended Frame | - |
| 100 | xxxxx | Errors | - |
Befehl
Der Befehl-Typ wird zum Steuern des Adapters vom PC aus verwendet. Das Feld CTRL setzt sich zusammen aus dem Type Of Frame (die höchstwertigen 3 Bits) sowie dem Befehltyp (die restlichen 5 Bits).
Reset
Löst einen Neustart des Adapters aus
| SOF | CTRL | CRC | |||||||
| 0x01 | TOF | RES | [CRC] | ||||||
| 0b000 | 0b00000 | ||||||||
| 1 Byte | 1 Byte | 2 Byte | |||||||
| 4 Bytes | |||||||||
| Feld | Breite | Werte | Info |
| [CRC] | 2 Byte | 0x00-0xFF (2x) | Prüfsumme (siehe CRC) |
Modus
Stellt verschiedene Modi ein
| SOF | CTRL | Daten | CRC | |||||||
| 0x01 | TOF | MOD | [VAL] | [CRC] | ||||||
| 0b000 | 0b00001 | |||||||||
| 1 Byte | 1 Byte | 1 Byte | 2 Byte | |||||||
| 5 Bytes | ||||||||||
| Feld | Breite | Werte | Info |
| [VAL] | 1 Byte | 0x00 = Abfrage, 0x01 = OFF, 0x02 = Monitor, 0x03 = Listen-Only, 0x04 = Loopback | Schaltet die verschiedenen Modi um |
RX Fehlercounter
Liefert die Anzahl der Emfpangsfehler
| SOF | CTRL | Daten | CRC | |||||||
| 0x01 | TOF | RXERR | [VAL] | [CRC] | ||||||
| 0b000 | 0b00010 | |||||||||
| 1 Byte | 1 Byte | 1 Byte | 2 Byte | |||||||
| 5 Bytes | ||||||||||
| Feld | Breite | Werte | Info |
| [VAL] | 1 Byte | 0x00-0xFF | Anzahl der Empfangsfehler (zur Abfrage auf 0x00 setzen) |
TX Fehlercounter
Liefert die Anzahl der Sendefehler
| SOF | CTRL | Daten | CRC | |||||||
| 0x01 | TOF | TXERR | [VAL] | [CRC] | ||||||
| 0b000 | 0b00011 | |||||||||
| 1 Byte | 1 Byte | 1 Byte | 2 Byte | |||||||
| 5 Bytes | ||||||||||
| Feld | Breite | Werte | Info |
| [VAL] | 1 Byte | 0x00-0xFF | Anzahl der Sendefehler (zur Abfrage auf 0x00 setzen) |
Initialisierung
Stellt Prescaler, TQ PropSeg, TQ PhSeg1, TQ PhSeg2, TQ Jump Width und die Anzahl der Samples ein
| SOF | CTRL | Daten | CRC | |||||||
| 0x01 | TOF | INIT | [BRP] | [PSEG] | [PHSEG1] | [PHSEG2] | [JPW] | [SAM] | [CRC] | |
| 0b000 | 0b00100 | |||||||||
| 1 Byte | 1 Byte | 6 Bytes | 2 Byte | |||||||
| 10 Bytes | ||||||||||
| Feld | Breite | Werte | Info |
| [BRP] | 1 Byte | 0x00-0xFF | Prescaler-Wert |
| [PSEG] | 1 Byte | 0x00-0x08 | TQ PropSeg |
| [PHSEG1] | 1 Byte | 0x01-0x08 | TQ PhSeg1 |
| [PHSEG2] | 1 Byte | 0x02-0x08 | TQ PhSeg2 (Wert muss kleiner als [PHSEG1] sein) |
| [JPW] | 1 Byte | 0x00-0x04 | TQ Jump Width |
| [SAM] | 1 Byte | 0x01 / 0x03 | Anzahl der Samples (1 bzw. 3) |
Taktrate
Liest die Taktrate des CAN-Controllers aus
| SOF | CTRL | Daten | CRC | |||||||
| 0x01 | TOF | XTAL | [OSC1] | [OSC2] | [OSC3] | [OSC4] | [CRC] | |||
| 0b000 | 0b00101 | |||||||||
| 1 Byte | 1 Byte | 4 Byte | 2 Byte | |||||||
| 8 Bytes | ||||||||||
| Feld | Breite | Werte | Info |
| [OSC1] | 1 Byte | 0x00-0xFF | Taktfrequenz, Byte 1 |
| [OSC2] | 1 Byte | 0x00-0xFF | Taktfrequenz, Byte 2 |
| [OSC3] | 1 Byte | 0x00-0xFF | Taktfrequenz, Byte 3 |
| [OSC4] | 1 Byte | 0x00-0xFF | Taktfrequenz, Byte 4 |
Der Wert wird wie folgt berechnet: Taktrate = [OSC1] + [OSC2] * 2^8 + [OSC3] * 2^16 + [OSC4] * 2^24
Zur Abfrage des aktuellen Wertes sind alle [OSCx]-Bytes auf 0x00 zu setzen
Version
Liefert die Adapterkennung, die verwendete Protokollversion sowie die Firmware-Version des Adapters
| SOF | CTRL | Daten | CRC | ||||||||
| 0x01 | TOF | VER | [ADI] | [PV] | [VER1] | [VER2] | [VER3] | [CRC] | |||
| 0b000 | 0b00110 | ||||||||||
| 1 Byte | 1 Byte | 5 Bytes | 2 Byte | ||||||||
| 9 Bytes | |||||||||||
| Feld | Breite | Werte | Info |
| [ADI] | 1 Byte | 0x00-0xFF | Adapter Identification (Hardwaretyp) |
| [PV] | 1 Byte | 0x00-0xFF | Verwendete Protokollversion |
| [VER1] | 1 Byte | 0x00-0xFF | Adapterversion, Byte 1 |
| [VER2] | 1 Byte | 0x00-0xFF | Adapterversion, Byte 2 |
| [VER3] | 1 Byte | 0x00-0xFF | Adapterversion, Byte 3 |
Die Adapterversion setzt sich wie folgt zusammen: x.y.zz = [VER3].[VER2].[VER1]
Die Versionsnummer für das Protokoll ist eine fortlaufende Nummer
Bufsize
Liest die Größe des Sende- und Empfangspuffers (vom Adapter in Richtung PC aus gesehen) aus.
| SOF | CTRL | Daten | CRC | ||
| 0x01 | TOF | BUFSIZE | [BUF_TX] | [BUF_RX] | [CRC] |
| 0b000 | 0b00111 | ||||
| 1 Byte | 1 Byte | 2 Byte | 2 Byte | 2 Byte | |
| 8 Bytes | |||||
| Feld | Breite | Werte | Info |
| [BUF_RX] | 2 Byte | 0-65535 | Puffergröße PC->Adapter in Byte |
| [BUF_TX] | 2 Byte | 0-65535 | Puffergröße Adapter->PC in Byte |
Time-Sync
Kommando, um den internen 16-Bit Zähler auf 0 zu setzen und anfangen, jede Milisekunde um 1 nach oben zu zählen. Jedes dafür vorgesehene Paket erhält damit einen Zeitstempel um die Sende- und Empfangsreihenfolge feststellen zu können. Das Paket sollte vom Adapter möglichst zeitnah ausgewertet werden.
| SOF | CTRL | CRC | |
| 0x01 | TOF | TSYN | [CRC] |
| 0b000 | 0b01001 | ||
| 1 Byte | 1 Byte | 2 Byte | |
| 4 Bytes | |||
Datenframes
Enthält die Daten eines CAN-Standard- oder Extended-Frames sowie einen Timestamp, wann das Paket vom Adapter empfangen wurde
| SOF | LEN | CTRL | Daten | EOF | |||||||
| 0xAFFE | 0x16 | TOF | [EXT] | [RTR] | [DLC] | [TSP] | [DIR] | [RES] | [CAN_ID] | [CAN_DATA] | 0xDEAD |
| 0b01 | 1 Bit | 1 Bit | 4 Bit | 1 Bit | 4 Bit | 29 Bit | |||||
| 2 Byte | 1 Byte | 1 Byte | 4 Byte | 4 Byte | 8 Byte | 2 Byte | |||||
| 22 Bytes | |||||||||||
| Feld | Breite | Werte | Info |
| [EXT] | 1 Bit | 0 / 1 | Extended Frame |
| [RTR] | 1 Bit | 0 / 1 | CAN Remote Request |
| [DLC] | 4 Bit | 0-8 | CAN Data Length |
| [TSP] | 4 Byte | 0 - 2^32-1 | Timestamp |
| [DIR] | 1 Bit | 0 / 1 | Richtungsbit, 0: Nachricht kommt vom CAN-Bus / 1: Nachricht wird auf den Bus gelegt |
| [RES] | 4 Bit | - | reserviert |
| [CAN_ID] | 29 Bit | 0 - 2^29-1 | CAN ID |
| [CAN_DATA] | 8 Byte | 0x00-0xFF | CAN Daten (Anzahl der Bytes wird durch [DLC] bestimmt) |
Hinweis: Die Anzahl der Datenbytes in [DATA] ist immer 8, die Anzahl der tatsächlichen Nutzdaten zeigt [DLC].
Error Frames
Enthält die Daten eines CAN-Frames, einen Fehlercode sowie einen Timestamp, wann das Paket vom Adapter gesendet / empfangen wurde bzw. der Fehler aufgetreten ist
| SOF | CTRL | Daten | CRC | ||||||||
| 0x01 | TOF | [RTR] | [DLC] | [TSP] | [RES] | [ERR] | [DIR] | [RES] | [CAN_ID] | [CAN_DATA] | [CRC] |
| 0b111 | 1 Bit | 4 Bit | 3 Bit | 5 Bit | 1 Bit | 2 Bit | 29 Bit | ||||
| 1 Byte | 1 Byte | 2 Byte | 1 Byte | 4 Byte | 0-8 Byte | 2 Byte | |||||
| 11-19 Bytes | |||||||||||
| Feld | Breite | Werte | Info |
| [RES] | 3 Bit | - | Reserviert für Erweiterungen |
| [ERR] | 5 Bit | 0-32 | Fehler-ID (siehe Fehlertypen) |
| [RTR] | 1 Bit | 0 / 1 | CAN Remote Request |
| [DLC] | 4 Bit | 0-8 | CAN Data Length |
| [DIR] | 1 Bit | 0 / 1 | Richtungsbit, 0: Nachricht kommt vom CAN-Bus / 1: Nachricht wird auf den Bus gelegt |
| [CAN_ID] | 4 Byte | 0-2^29 | CAN Extended ID (29 Bit) |
| [CAN_DATA] | 0-8 Byte | 0x00-0xFF | CAN Daten (Anzahl der Bytes wird durch [DLC] bestimmt) |
| [TSP] | 2 Byte | 0x00-0xFF (2x) | Timestamp |
| [CRC] | 0x00-0xFF | 0x00-0xFF (2x) | Prüfsumme (siehe CRC) |
Error Frame
| SOF | CTRL | Daten | CRC | |||||||
| 0x01 | TOF | [ERR] | [TSP] | [CRC] | ||||||
| 0b100 | 5 Bit | |||||||||
| 1 Byte | 1 Byte | 2 Byte | 2 Byte | |||||||
| 6 Bytes | ||||||||||
| Feld | Breite | Werte | Info |
| [ERR] | 5 Bit | 0-32 | Fehler-ID (siehe Fehlertypen) |
Fehlertypen
| ID | Fehler |
| 0x00 | Error Frame |
| 0x01 | Overload Frame |
| 0x02 | CRC Error |
| 0x03 | Acknowledge Error |
| 0x04 | Form Error |
| 0x05 | Bit Error |
| 0x06 | Stuff Error |
| 0x07 | RX Buffer Overflow |
| 0x08 | Bus-Off |
| 0x09 | TX Error |
| 0x0A | RX Error |
| 0x0B | TX Warn |
| 0x0C | RX Warn |
CRC
Die Prüfsumme der Pakete wird nach folgendem Algorithmus gebildet:
uint16_t calc_crc(void *data, uint16_t len) {
uint32_t sum = 0;
for (;;) {
if (len < 2) break;
sum += *((uint16_t *)data);
data += 2;
len -= 2;
}
if (len) sum += *(uint8_t *) data;
while ((len = (uint16_t) (sum >> 16)) != 0) sum = (uint16_t) sum + len;
return (uint16_t) sum ^ 0xFFFF;
}
Dabei zählen alle Bytes bis auf das Startbyte und die CRC-Bytes