-
Notifications
You must be signed in to change notification settings - Fork 3k
Silicon Labs: Add support for hardware CRC #7479
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
/***************************************************************************//** | ||
* @file crc_api.c | ||
******************************************************************************* | ||
* @section License | ||
* <b>(C) Copyright 2018 Silicon Labs, http://www.silabs.com</b> | ||
******************************************************************************* | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
******************************************************************************/ | ||
|
||
#include "device.h" | ||
#include "clocking.h" | ||
#include <stdio.h> | ||
|
||
#if DEVICE_CRC | ||
#include "mbed_assert.h" | ||
#include "crc_api.h" | ||
#include "em_cmu.h" | ||
|
||
#if defined(GPCRC_PRESENT) && (GPCRC_COUNT > 0) | ||
#include "em_gpcrc.h" | ||
|
||
static bool revOutput = false; | ||
static uint32_t final_xor; | ||
|
||
bool hal_crc_is_supported(const crc_mbed_config_t *config) | ||
{ | ||
//GPCRC supports any 16-bit poly, but only the CCITT 32-bit poly | ||
if (config == NULL) { | ||
return false; | ||
} | ||
|
||
if (config->width == 16) { | ||
return true; | ||
} else if (config->width == 32) { | ||
if (config->polynomial == POLY_32BIT_ANSI) { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
void hal_crc_compute_partial_start(const crc_mbed_config_t *config) | ||
{ | ||
if (!hal_crc_is_supported(config)) { | ||
return; | ||
} | ||
|
||
CMU_ClockEnable(cmuClock_GPCRC, true); | ||
GPCRC_Reset(GPCRC); | ||
|
||
GPCRC_Init_TypeDef crc_init; | ||
|
||
crc_init.autoInit = false; | ||
crc_init.enable = true; | ||
crc_init.crcPoly = config->polynomial; | ||
|
||
// GPCRC operates on bit-reversed inputs and outputs vs the standard | ||
// defined by the mbed API. Emlib does the reversal on the poly, but | ||
// not on the initial value. | ||
if (config->width == 16) { | ||
crc_init.initValue = __RBIT(config->initial_xor) >> 16; | ||
} else { | ||
crc_init.initValue = __RBIT(config->initial_xor); | ||
} | ||
|
||
// GPCRC operates on bit-reversed inputs and outputs vs the standard | ||
// defined by the mbed API, so reflect_in/out needs to be negated. | ||
if (config->reflect_in) { | ||
crc_init.reverseByteOrder = false; | ||
crc_init.reverseBits = false; | ||
} else { | ||
crc_init.reverseByteOrder = true; | ||
crc_init.reverseBits = true; | ||
} | ||
|
||
// Disable byte mode to be able to run a faster U32 input version | ||
crc_init.enableByteMode = false; | ||
|
||
// GPCRC does not support hardware output bit-reversal, nor finalisation. | ||
// Since the mbed API does not pass the config struct when fetching the | ||
// CRC calculation result, we need to keep track of these locally. | ||
revOutput = config->reflect_out; | ||
final_xor = config->final_xor; | ||
|
||
GPCRC_Init(GPCRC, &crc_init); | ||
GPCRC_Start(GPCRC); | ||
} | ||
|
||
void hal_crc_compute_partial(const uint8_t *data, const size_t size) | ||
{ | ||
if (data == NULL || size <= 0) { | ||
return; | ||
} | ||
|
||
if (((uint32_t)data & 0x3) != 0 || size < 4) { | ||
// Unaligned or very small input, run a bytewise CRC | ||
for (size_t i = 0; i < size; i++) { | ||
GPCRC_InputU8(GPCRC, data[i]); | ||
} | ||
} else { | ||
// Aligned input, run 32-bit inputs as long as possible to make go faster. | ||
size_t i = 0; | ||
for (; i < (size & (~0x3)); i+=4) { | ||
GPCRC_InputU32(GPCRC, *((uint32_t*)(&data[i]))); | ||
} | ||
for (; i < size; i++) { | ||
GPCRC_InputU8(GPCRC, data[i]); | ||
} | ||
} | ||
} | ||
|
||
uint32_t hal_crc_get_result(void) | ||
{ | ||
uint32_t result; | ||
|
||
// GPCRC operates on bit-reversed inputs and outputs vs the standard | ||
// defined by the mbed API. | ||
if (!revOutput) { | ||
result = GPCRC_DataReadBitReversed(GPCRC); | ||
} else { | ||
result = GPCRC_DataRead(GPCRC); | ||
} | ||
|
||
GPCRC_Enable(GPCRC, false); | ||
|
||
return result ^ final_xor; | ||
} | ||
|
||
#endif //GPCRC_PRESENT | ||
#endif //DEVICE_CRC |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me, just one query / concern, GPCRC_PRESENT will always be present for all targets?
In case DEVICE_CRC is true and GPCRC_PRESENT is not defined
hal_crc_is_supported
should return false and rest of the API's should just return.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DEVICE_CRC is a target-specific definition through targets.json, while GPCRC_PRESENT is a Silicon Labs CMSIS define. If a device doesn't have a GPCRC engine, then we don't add the CRC capability to its entry in targets.json.
The reason for still having the DEVICE_CRC define is to allow people to turn off CRC acceleration if they should want to do so.