main
John Lauer software/: Pico firmware (500 ksps two-channel ADC over DMA, SYNC on GPIO15, framed delta-packed USB stream), smi-dash… e09b832
// SPDX-License-Identifier: MIT
#include "smi_frame.h"

uint16_t smi_crc16(const uint8_t *p, size_t n, uint16_t crc) {
    // CRC-16/CCITT-FALSE: poly 0x1021, init 0xFFFF, no reflection, no xorout.
    for (size_t i = 0; i < n; i++) {
        crc ^= (uint16_t)p[i] << 8;
        for (int b = 0; b < 8; b++) crc = (crc & 0x8000) ? (uint16_t)((crc << 1) ^ 0x1021) : (uint16_t)(crc << 1);
    }
    return crc;
}

static inline uint32_t zigzag(int32_t d) { return ((uint32_t)d << 1) ^ (uint32_t)(d >> 31); }
static inline int32_t unzigzag(uint32_t z) { return (int32_t)(z >> 1) ^ -(int32_t)(z & 1); }

size_t smi_pack_block(const uint16_t *s, size_t stride, uint8_t *dst) {
    uint32_t zz[SMI_BLOCK - 1];
    uint32_t all = 0;
    uint16_t prev = s[0] & 0x0FFF;
    for (int i = 1; i < SMI_BLOCK; i++) {
        uint16_t v = s[i * stride] & 0x0FFF;
        zz[i - 1] = zigzag((int32_t)v - (int32_t)prev);
        all |= zz[i - 1];
        prev = v;
    }
    unsigned w = 0;
    while (w < 32 && (all >> w)) w++;
    unsigned code = w;
    if (w > 12) {
        code = SMI_RAW_CODE;
        w = 12;
        for (int i = 1; i < SMI_BLOCK; i++) zz[i - 1] = s[i * stride] & 0x0FFF;
    }
    smi_put_u16(dst, (uint16_t)((s[0] & 0x0FFF) | (code << 12)));
    size_t o = 2;
    if (w == 0) return o;
    uint32_t acc = 0;
    unsigned nb = 0;
    for (int i = 0; i < SMI_BLOCK - 1; i++) {
        acc |= zz[i] << nb;
        nb += w;
        while (nb >= 8) { dst[o++] = (uint8_t)acc; acc >>= 8; nb -= 8; }
    }
    if (nb) dst[o++] = (uint8_t)acc;
    return o;
}

size_t smi_unpack_block(const uint8_t *src, size_t avail, uint16_t *out) {
    if (avail < 2) return 0;
    uint16_t head = (uint16_t)(src[0] | (src[1] << 8));
    unsigned code = head >> 12;
    out[0] = head & 0x0FFF;
    if (code > 12 && code != SMI_RAW_CODE) return 0;
    unsigned w = code == SMI_RAW_CODE ? 12 : code;
    size_t need = 2 + (63 * w + 7) / 8;
    if (avail < need) return 0;
    uint32_t acc = 0;
    unsigned nb = 0;
    size_t o = 2;
    int32_t prev = out[0];
    for (int i = 1; i < SMI_BLOCK; i++) {
        uint32_t z = 0;
        if (w) {
            while (nb < w) { acc |= (uint32_t)src[o++] << nb; nb += 8; }
            z = acc & ((1u << w) - 1);
            acc >>= w;
            nb -= w;
        }
        if (code == SMI_RAW_CODE) {
            out[i] = (uint16_t)z;
        } else {
            prev += unzigzag(z);
            if (prev < 0 || prev > 4095) return 0;
            out[i] = (uint16_t)prev;
        }
    }
    return need;
}

void smi_begin_frame(uint8_t *dst, uint8_t type, uint16_t payload_len, uint16_t seq) {
    dst[0] = SMI_SYNC0;
    dst[1] = SMI_SYNC1;
    dst[2] = type;
    dst[3] = 0;
    smi_put_u16(dst + 4, payload_len);
    smi_put_u16(dst + 6, seq);
}

size_t smi_finish_frame(uint8_t *frame, uint16_t payload_len) {
    size_t end = SMI_HDR_LEN + payload_len;
    uint16_t crc = smi_crc16(frame + 2, end - 2, 0xFFFF);
    smi_put_u16(frame + end, crc);
    return end + 2;
}