ether2ser 0.1.0
Ethernet <-> synchronous V.24 bridge firmware for RP2040 + W5500
Loading...
Searching...
No Matches
hdlc_common.c
Go to the documentation of this file.
1/*
2 * ether2ser — Ethernet <-> synchronous V.24 (RS-232/V.28) bridge
3 *
4 * File: src/protocol/hdlc_common.c
5 * Purpose: HDLC common utilities (CRC and shared helpers).
6 *
7 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Copyright (c) 2026 Florian <f.leuze@outlook.de>
10 */
11
12// Related headers
13#include "hdlc_common.h"
14
15// Standard library headers
16#include <stddef.h>
17#include <stdint.h>
18
19// Project Headers
20
21// Generated headers
22
23#define HDLC_CRC16_CCITT_INIT 0xFFFFu
24#define HDLC_CRC16_CCITT_POLY 0x1021u
25#define HDLC_CRC16_CCITT_MSB_MASK 0x8000u
26#define HDLC_CRC16_BITS_PER_BYTE 8u
27
28static uint16_t crc16_ccitt_false(const uint8_t* payload, size_t num_bytes)
29{
30 uint16_t crc16 = HDLC_CRC16_CCITT_INIT;
31 while (num_bytes--)
32 {
33 uint8_t byte = *payload++;
34 crc16 ^= (uint16_t)byte << HDLC_CRC16_BITS_PER_BYTE;
35 for (uint8_t bit = 0; bit < HDLC_CRC16_BITS_PER_BYTE; bit++)
36 {
37 crc16 = (crc16 & HDLC_CRC16_CCITT_MSB_MASK)
38 ? (uint16_t)((crc16 << 1) ^ HDLC_CRC16_CCITT_POLY)
39 : (uint16_t)(crc16 << 1);
40 }
41 }
42 return crc16 & HDLC_CRC16_CCITT_INIT;
43}
44
45uint16_t hdlc_crc16(const uint8_t* payload, size_t num_bytes)
46{
47 return crc16_ccitt_false(payload, num_bytes);
48}
#define HDLC_CRC16_CCITT_POLY
Definition hdlc_common.c:24
#define HDLC_CRC16_BITS_PER_BYTE
Definition hdlc_common.c:26
uint16_t hdlc_crc16(const uint8_t *payload, size_t num_bytes)
Compute HDLC CRC16 (FCS) over a payload.
Definition hdlc_common.c:45
#define HDLC_CRC16_CCITT_INIT
Definition hdlc_common.c:23
static uint16_t crc16_ccitt_false(const uint8_t *payload, size_t num_bytes)
Definition hdlc_common.c:28
#define HDLC_CRC16_CCITT_MSB_MASK
Definition hdlc_common.c:25