view crc_wrapper.cpp @ 5:115cfa4a3239 default tip

Added icon upload function for OSTC 4/5 For the upload the same process as the one for the firmware update is used => CRC functionality has been copied from the ostc_pack SW
author Ideenmodellierer
date Tue, 30 Dec 2025 21:41:02 +0100
parents
children
line wrap: on
line source

#include "crc_wrapper.h"

extern "C" {
#include "crcmodel.h"
}


CrcWrapper::CrcWrapper(QObject *parent)
    : QObject(parent)
{}


void CrcWrapper::init(p_cm_t p_cm)
{
   cm_ini(p_cm);
}

void CrcWrapper::cm_next(p_cm_t p_cm, int ch)
{
    cm_nxt(p_cm, ch);
}

uint32_t CrcWrapper::CRC_CalcBlockCRC(uint32_t *buffer, uint32_t words)
{
    cm_t        crc_model;
    uint32_t      word_to_do;
    uint8_t       byte_to_do;
    int         i;

    // Values for the STM32F generator.

    crc_model.cm_width = 32;            // 32-bit CRC
    crc_model.cm_poly  = 0x04C11DB7;    // CRC-32 polynomial
    crc_model.cm_init  = 0xFFFFFFFF;    // CRC initialized to 1's
    crc_model.cm_refin = FALSE;         // CRC calculated MSB first
    crc_model.cm_refot = FALSE;         // Final result is not bit-reversed
    crc_model.cm_xorot = 0x00000000;    // Final result XOR'ed with this

    cm_ini(&crc_model);

    while (words--)
    {
        // The STM32F10x hardware does 32-bit words at a time!!!

        word_to_do = *buffer++;

        // Do all bytes in the 32-bit word.

        for (i = 0; i < sizeof(word_to_do); i++)
        {
            // We calculate a *byte* at a time. If the CRC is MSB first we
            // do the next MS byte and vica-versa.

            if (crc_model.cm_refin == FALSE)
            {
                // MSB first. Do the next MS byte.

                byte_to_do = (uint8_t) ((word_to_do & 0xFF000000) >> 24);
                word_to_do <<= 8;
            }
            else
            {
                // LSB first. Do the next LS byte.

                byte_to_do = (uint8_t) (word_to_do & 0x000000FF);
                word_to_do >>= 8;
            }

            cm_nxt(&crc_model, byte_to_do);
        }
    }

    // Return the final result.

    return (cm_crc(&crc_model));
}