Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Second commit for crc32 #303

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions libraries/ms-common/inc/crc32.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once

#include <stddef.h>
#include <stdint.h>

#include "FreeRTOS.h"
#include "status.h"

// Initialize CRC calculation (no use for STM32F1)
StatusCode crc_init(void);

// Calculate CRC32 for an entire buffer
uint32_t crc_calculate(const uint32_t *buffer, size_t buffer_len);
29 changes: 29 additions & 0 deletions libraries/ms-common/src/arm/crc32.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "crc32.h" // Include the header file for CRC32 function declarations

#include "log.h" // Include the logging utility, possibly used for debugging or status messages
#include "misc.h"
#include "stm32f10x.h" // Include the STM32F1 series specific header file for hardware definitions
#include "stm32f10x_crc.h" // Include the CRC library header

// Initialization function for CRC
StatusCode crc_init(void) {
// Enable the CRC peripheral clock
RCC->AHBENR |= RCC_AHBENR_CRCEN;

// Reset CRC Data Register
CRC_ResetDR();

return STATUS_CODE_OK; // Return OK status
}

// Function to calculate CRC32 for a given buffer, with an option to append to an existing CRC
uint32_t crc_calculate(const uint32_t *buffer, size_t buffer_len) {
CRC_ResetDR(); // Reset the CRC Data Register

// Process each byte in the buffer
for (size_t i = 0; i < buffer_len; i++) {
CRC_CalcCRC(buffer[i]); // Calculate CRC for each byte
}

return ~CRC_GetCRC(); // Return the inverted final CRC value
}
41 changes: 41 additions & 0 deletions libraries/ms-common/src/x86/crc32.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include "crc32.h" // Include the header file for CRC32 function declarations

#include "log.h" // Include the logging utility, possibly used for debugging or status messages
#include "misc.h"
#include "stm32f10x.h" // Include the STM32F1 series specific header file for hardware definitions
#include "stm32f10x_crc.h" // Include the CRC library header

// Define the CRC32 polynomial and initial value
#define CRC32_POLYNOMIAL \
0x04C11DB7 // Standard CRC32 polynomial (used in many protocols and formats)
#define CRC32_INIT_VALUE 0xFFFFFFFF // Initial value used to start the CRC calculation

static uint32_t crc32_calculate_byte(uint32_t crc, uint32_t data) {
crc ^= data; // XOR the word directly into the CRC value

// Perform the polynomial division one bit at a time for each of the 32 bits
for (int i = 0; i < 32; i++) {
if (crc & 0x80000000) { // If the top bit is set (MSB is 1)
crc = (crc << 1) ^ CRC32_POLYNOMIAL; // Shift left and XOR with the polynomial
} else {
crc <<= 1; // Just shift left if the top bit is not set
}
}
return crc; // Return the updated CRC value after processing the word
}

uint32_t crc_calculate(const uint32_t *buffer, size_t buffer_len) {
uint32_t crc = ~0; // Initialize CRC with the inverted initial value (standard practice)

// Process each byte in the buffer
for (size_t i = 0; i < buffer_len; i++) {
crc = crc32_calculate_byte(crc, buffer[i]); // Update CRC for each byte
}

return ~crc; // Return the inverted CRC32 value (final CRC value needs to be inverted)
}

// no use for x86
StatusCode crc_init(void) {
return STATUS_CODE_OK;
}
Loading