Skip to content

Commit

Permalink
crc32 passed smoke test
Browse files Browse the repository at this point in the history
  • Loading branch information
RodneyDong committed Sep 22, 2024
1 parent 827acd7 commit 8121584
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 57 deletions.
12 changes: 5 additions & 7 deletions libraries/ms-common/inc/crc32.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
#pragma once

#include "FreeRTOS.h"
#include "status.h"
#include <stddef.h>
#include <stdint.h>

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

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

// Calculate CRC32 for an entire buffer
uint32_t crc32_arr(const uint8_t *buffer, size_t buffer_len);

// Incremental CRC32 calculation
uint32_t crc32_append_arr(const uint8_t *buffer, size_t buffer_len, uint32_t initial_crc);
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
}
50 changes: 0 additions & 50 deletions libraries/ms-common/src/crc32.c

This file was deleted.

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;
}

0 comments on commit 8121584

Please sign in to comment.