Clean up crc16.[ch]
This commit is contained in:
@@ -1,70 +1,6 @@
|
|||||||
#include "bootloader_common.h"
|
#include "bootloader_common.h"
|
||||||
#include "crc16.h"
|
#include "crc16.h"
|
||||||
|
|
||||||
#if !defined(BOOTLOADER_HOST)
|
|
||||||
#include "fsl_device_registers.h"
|
|
||||||
#include "utilities/fsl_rtos_abstraction.h"
|
|
||||||
#endif // !BOOTLOADER_HOST
|
|
||||||
|
|
||||||
#if !defined(BOOTLOADER_HOST) && FSL_FEATURE_SOC_CRC_COUNT && !defined(BL_TARGET_RAM)
|
|
||||||
#include "fsl_crc.h"
|
|
||||||
|
|
||||||
/* Table of base addresses for crc instances. */
|
|
||||||
static CRC_Type *const g_crcBase[1] = CRC_BASE_PTRS;
|
|
||||||
|
|
||||||
void crc16_init(crc16_data_t *crc16Config)
|
|
||||||
{
|
|
||||||
assert(crc16Config);
|
|
||||||
|
|
||||||
crc16Config->currentCrc = 0x0000U;
|
|
||||||
}
|
|
||||||
|
|
||||||
void crc16_update(crc16_data_t *crc16Config, const uint8_t *src, uint32_t lengthInBytes)
|
|
||||||
{
|
|
||||||
assert(crc16Config);
|
|
||||||
assert(src);
|
|
||||||
|
|
||||||
crc_config_t crcUserConfigPtr;
|
|
||||||
|
|
||||||
CRC_GetDefaultConfig(&crcUserConfigPtr);
|
|
||||||
|
|
||||||
crcUserConfigPtr.crcBits = kCrcBits16;
|
|
||||||
crcUserConfigPtr.seed = crc16Config->currentCrc;
|
|
||||||
crcUserConfigPtr.polynomial = 0x1021U;
|
|
||||||
crcUserConfigPtr.complementChecksum = false;
|
|
||||||
crcUserConfigPtr.reflectIn = false;
|
|
||||||
crcUserConfigPtr.reflectOut = false;
|
|
||||||
|
|
||||||
// Init CRC module and then run it
|
|
||||||
//! Note: We must init CRC module here, As we may seperate one crc calculation into several times
|
|
||||||
//! Note: It is better to use lock to ensure the integrity of current updating operation of crc calculation
|
|
||||||
// in case crc module is shared by multiple crc updating requests at the same time
|
|
||||||
if (lengthInBytes)
|
|
||||||
{
|
|
||||||
lock_acquire();
|
|
||||||
CRC_Init(g_crcBase[0], &crcUserConfigPtr);
|
|
||||||
CRC_WriteData(g_crcBase[0], src, lengthInBytes);
|
|
||||||
crcUserConfigPtr.seed = CRC_Get16bitResult(g_crcBase[0]);
|
|
||||||
lock_release();
|
|
||||||
}
|
|
||||||
|
|
||||||
crc16Config->currentCrc = crcUserConfigPtr.seed;
|
|
||||||
}
|
|
||||||
|
|
||||||
void crc16_finalize(crc16_data_t *crc16Config, uint16_t *hash)
|
|
||||||
{
|
|
||||||
assert(crc16Config);
|
|
||||||
assert(hash);
|
|
||||||
|
|
||||||
*hash = crc16Config->currentCrc;
|
|
||||||
|
|
||||||
// De-init CRC module when we complete a full crc calculation
|
|
||||||
CRC_Deinit(g_crcBase[0]);
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
// Code
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
void crc16_init(crc16_data_t *crc16Config)
|
void crc16_init(crc16_data_t *crc16Config)
|
||||||
{
|
{
|
||||||
assert(crc16Config);
|
assert(crc16Config);
|
||||||
@@ -107,4 +43,3 @@ void crc16_finalize(crc16_data_t *crc16Config, uint16_t *hash)
|
|||||||
|
|
||||||
*hash = crc16Config->currentCrc;
|
*hash = crc16Config->currentCrc;
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|||||||
@@ -1,56 +1,23 @@
|
|||||||
#ifndef _CRC16_H_
|
#ifndef __CRC16_H__
|
||||||
#define _CRC16_H_
|
#define __CRC16_H__
|
||||||
|
|
||||||
#include <stdint.h>
|
typedef struct Crc16Data {
|
||||||
|
uint16_t currentCrc;
|
||||||
//! @addtogroup crc16
|
|
||||||
//! @{
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
// Definitions
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
//! @brief State information for the CRC16 algorithm.
|
|
||||||
typedef struct Crc16Data
|
|
||||||
{
|
|
||||||
uint16_t currentCrc; //!< Current CRC value.
|
|
||||||
} crc16_data_t;
|
} crc16_data_t;
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
// API
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
#if __cplusplus
|
|
||||||
extern "C" {
|
|
||||||
#endif
|
|
||||||
|
|
||||||
//! @name CRC16
|
|
||||||
//@{
|
|
||||||
|
|
||||||
//! @brief Initializes the parameters of the crc function, must be called first.
|
//! @brief Initializes the parameters of the crc function, must be called first.
|
||||||
//!
|
|
||||||
//! @param crc16Config Instantiation of the data structure of type crc16_data_t.
|
//! @param crc16Config Instantiation of the data structure of type crc16_data_t.
|
||||||
void crc16_init(crc16_data_t *crc16Config);
|
void crc16_init(crc16_data_t *crc16Config);
|
||||||
|
|
||||||
//! @brief A "running" crc calculator that updates the crc value after each call.
|
//! @brief A "running" crc calculator that updates the crc value after each call.
|
||||||
//!
|
|
||||||
//! @param crc16Config Instantiation of the data structure of type crc16_data_t.
|
//! @param crc16Config Instantiation of the data structure of type crc16_data_t.
|
||||||
//! @param src Pointer to the source buffer of data.
|
//! @param src Pointer to the source buffer of data.
|
||||||
//! @param lengthInBytes The length, given in bytes (not words or long-words).
|
//! @param lengthInBytes The length, given in bytes (not words or long-words).
|
||||||
void crc16_update(crc16_data_t *crc16Config, const uint8_t *src, uint32_t lengthInBytes);
|
void crc16_update(crc16_data_t *crc16Config, const uint8_t *src, uint32_t lengthInBytes);
|
||||||
|
|
||||||
//! @brief Calculates the final crc value, padding with zeros if necessary, must be called last.
|
//! @brief Calculates the final crc value, padding with zeros if necessary, must be called last.
|
||||||
//!
|
|
||||||
//! @param crc16Config Instantiation of the data structure of type crc16_data_t.
|
//! @param crc16Config Instantiation of the data structure of type crc16_data_t.
|
||||||
//! @param hash Pointer to the value returned for the final calculated crc value.
|
//! @param hash Pointer to the value returned for the final calculated crc value.
|
||||||
void crc16_finalize(crc16_data_t *crc16Config, uint16_t *hash);
|
void crc16_finalize(crc16_data_t *crc16Config, uint16_t *hash);
|
||||||
|
|
||||||
//@}
|
|
||||||
|
|
||||||
#if __cplusplus
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
//! @}
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user