mirror of https://github.com/ARMmbed/mbed-os.git
commit
0a528ff558
|
@ -0,0 +1,114 @@
|
||||||
|
/* mbed Microcontroller Library
|
||||||
|
* Copyright (c) 2018 ARM Limited
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/** \addtogroup hal_crc_tests */
|
||||||
|
/** @{*/
|
||||||
|
|
||||||
|
#ifndef MBED_CRC_API_TESTS_H
|
||||||
|
#define MBED_CRC_API_TESTS_H
|
||||||
|
|
||||||
|
#include "device.h"
|
||||||
|
|
||||||
|
#if DEVICE_CRC
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/** Test that hal_crc_is_supported() function returns true if given polynomial/width
|
||||||
|
* is supported, false otherwise (at least one predefined polynomial/width must be supported).
|
||||||
|
*
|
||||||
|
* Given is platform with hardware CRC support.
|
||||||
|
*
|
||||||
|
* When given polynomial/width is supported.
|
||||||
|
* Then hal_crc_is_supported() function returns true.
|
||||||
|
*
|
||||||
|
* When given polynomial/width is not supported.
|
||||||
|
* Then hal_crc_is_supported() function returns false.
|
||||||
|
*
|
||||||
|
* Note:
|
||||||
|
* At least one predefined polynomial/width config must be supported.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
void crc_is_supported_test();
|
||||||
|
|
||||||
|
/** Test that CRC module can be successfully configured, fed with data and the result can
|
||||||
|
* be successfully obtained.
|
||||||
|
*
|
||||||
|
* Given is platform with hardware CRC support.
|
||||||
|
*
|
||||||
|
* When hal_crc_compute_partial_start() function is called.
|
||||||
|
* Then it configures CRC module with the given polynomial.
|
||||||
|
*
|
||||||
|
* When hal_crc_compute_partial() function is called with valid buffer and data length.
|
||||||
|
* Then it feeds CRC module with data.
|
||||||
|
*
|
||||||
|
* When hal_crc_get_result() function is called.
|
||||||
|
* Then CRC value for the given data is returned.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
void crc_calc_single_test();
|
||||||
|
|
||||||
|
/** Test that hal_crc_compute_partial() function can be call multiple times in
|
||||||
|
* succession in order to provide additional data to CRC module.
|
||||||
|
*
|
||||||
|
* Given is platform with hardware CRC support and CRC module is configured.
|
||||||
|
* When hal_crc_compute_partial() function is called multiple times.
|
||||||
|
* Then each call provides additional data to CRC module.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
void crc_calc_multi_test();
|
||||||
|
|
||||||
|
/** Test that calling hal_crc_compute_partial_start() without finalising the
|
||||||
|
* CRC calculation overrides the current configuration and partial result.
|
||||||
|
*
|
||||||
|
* Given is platform with hardware CRC support.
|
||||||
|
* When CRC module has been configured and fed with data and reconfigured (without reading the result).
|
||||||
|
* Then the configuration has been overwritten and the new data can be successfully processed.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
void crc_reconfigure_test();
|
||||||
|
|
||||||
|
/** Test that hal_crc_compute_partial() does nothing if pointer to buffer is undefined or
|
||||||
|
* data length is equal to 0.
|
||||||
|
*
|
||||||
|
* Given is platform with hardware CRC support.
|
||||||
|
* When hal_crc_compute_partial() is called with invalid parameters.
|
||||||
|
* Then no data is provided to CRC module and no exception is generated.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
void crc_compute_partial_invalid_param_test();
|
||||||
|
|
||||||
|
/** Test that hal_crc_is_supported() returns false if pointer to the config structure is undefined.
|
||||||
|
*
|
||||||
|
* Given is platform with hardware CRC support.
|
||||||
|
* When hal_crc_is_supported() is called with invalid parameter.
|
||||||
|
* Then function returns false.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
void crc_is_supported_invalid_param_test();
|
||||||
|
|
||||||
|
/**@}*/
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/**@}*/
|
|
@ -0,0 +1,281 @@
|
||||||
|
/* mbed Microcontroller Library
|
||||||
|
* Copyright (c) 2018 ARM Limited
|
||||||
|
*
|
||||||
|
* 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 "utest/utest.h"
|
||||||
|
#include "unity/unity.h"
|
||||||
|
#include "greentea-client/test_env.h"
|
||||||
|
#include "mbed.h"
|
||||||
|
#include "math.h"
|
||||||
|
#include "crc_api.h"
|
||||||
|
|
||||||
|
#if !DEVICE_CRC
|
||||||
|
#error [NOT_SUPPORTED] CRC not supported for this target
|
||||||
|
#endif
|
||||||
|
|
||||||
|
using namespace utest::v1;
|
||||||
|
|
||||||
|
#define POLY_8BIT_MAXIM 0x31
|
||||||
|
#define POLY_16BIT_MAXIM 0x8005
|
||||||
|
#define POLY_32BIT_POSIX 0x4C11DB7
|
||||||
|
|
||||||
|
#define UNSUPPORTED (-1)
|
||||||
|
#define POL_CNT (2)
|
||||||
|
|
||||||
|
const uint8_t input_data[] = "123456789";
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
const crc_mbed_config config_data;
|
||||||
|
uint32_t expected_result;
|
||||||
|
|
||||||
|
} TEST_CASE;
|
||||||
|
|
||||||
|
/* We will allocate test case array on stack since memory limits on some boards
|
||||||
|
* like NUCLEO_F070RB. */
|
||||||
|
static TEST_CASE *test_cases;
|
||||||
|
static uint32_t test_cases_size;
|
||||||
|
|
||||||
|
/* Test that hal_crc_is_supported() function returns true if given polynomial
|
||||||
|
* is supported, false otherwise (at least one polynomial from the predefined list must be supported). */
|
||||||
|
void crc_is_supported_test()
|
||||||
|
{
|
||||||
|
/* Check if at least one crc polynomial/config is supported. */
|
||||||
|
uint32_t num_of_supported_polynomials = 0;
|
||||||
|
|
||||||
|
for (unsigned int i = 0; i < (test_cases_size / sizeof(TEST_CASE)); i++) {
|
||||||
|
if (hal_crc_is_supported(&test_cases[i].config_data) == true) {
|
||||||
|
|
||||||
|
num_of_supported_polynomials++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_ASSERT(num_of_supported_polynomials > 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Test that CRC module can be successfully configured, fed with data and the result can
|
||||||
|
* be successfully obtained. */
|
||||||
|
void crc_calc_single_test()
|
||||||
|
{
|
||||||
|
for (unsigned int i = 0; i < (test_cases_size / sizeof(TEST_CASE)); i++) {
|
||||||
|
if (hal_crc_is_supported(&test_cases[i].config_data) == true) {
|
||||||
|
|
||||||
|
hal_crc_compute_partial_start(&test_cases[i].config_data);
|
||||||
|
hal_crc_compute_partial((uint8_t*) input_data, strlen((const char*) input_data));
|
||||||
|
const uint32_t crc = hal_crc_get_result();
|
||||||
|
|
||||||
|
TEST_ASSERT_EQUAL(test_cases[i].expected_result, crc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Test that hal_crc_compute_partial() function can be call multiple times in
|
||||||
|
* succession in order to provide additional data to CRC module. */
|
||||||
|
void crc_calc_multi_test()
|
||||||
|
{
|
||||||
|
for (unsigned int i = 0; i < (test_cases_size / sizeof(TEST_CASE)); i++) {
|
||||||
|
if (hal_crc_is_supported(&test_cases[i].config_data) == true) {
|
||||||
|
|
||||||
|
const uint32_t first_part_bytes = 3;
|
||||||
|
const uint32_t second_part_bytes = 1;
|
||||||
|
const uint32_t third_part_bytes = strlen((const char*) input_data) - first_part_bytes
|
||||||
|
- second_part_bytes;
|
||||||
|
|
||||||
|
hal_crc_compute_partial_start(&test_cases[i].config_data);
|
||||||
|
hal_crc_compute_partial((uint8_t*) input_data, first_part_bytes);
|
||||||
|
hal_crc_compute_partial((uint8_t*) (input_data + first_part_bytes), second_part_bytes);
|
||||||
|
hal_crc_compute_partial((uint8_t*) (input_data + first_part_bytes + second_part_bytes),
|
||||||
|
third_part_bytes);
|
||||||
|
const uint32_t crc = hal_crc_get_result();
|
||||||
|
|
||||||
|
TEST_ASSERT_EQUAL(test_cases[i].expected_result, crc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Test that calling hal_crc_compute_partial_start() without finalising the
|
||||||
|
* CRC calculation overrides the current configuration. */
|
||||||
|
void crc_reconfigure_test()
|
||||||
|
{
|
||||||
|
int pol_idx[POL_CNT] =
|
||||||
|
{ UNSUPPORTED, UNSUPPORTED };
|
||||||
|
int pol_cnt = 0;
|
||||||
|
const uint8_t dummy_input_data[] = "abcdefghijklmnopqrstuvwxyz";
|
||||||
|
|
||||||
|
/* At least one configuration must be supported. If two are supported, then
|
||||||
|
* re-initialize CRC module using different config. */
|
||||||
|
for (unsigned int i = 0; i < (test_cases_size / sizeof(TEST_CASE)); i++) {
|
||||||
|
|
||||||
|
/* Find two supported polynomials if possible. */
|
||||||
|
if (hal_crc_is_supported(&test_cases[i].config_data) == true) {
|
||||||
|
if (pol_cnt == 0) {
|
||||||
|
pol_idx[pol_cnt] = i;
|
||||||
|
pol_cnt++;
|
||||||
|
} else if (test_cases[pol_idx[0]].config_data.polynomial != test_cases[i].config_data.polynomial) {
|
||||||
|
pol_idx[pol_cnt] = i;
|
||||||
|
pol_cnt++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pol_cnt == POL_CNT) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pol_cnt = 0;
|
||||||
|
|
||||||
|
/* Init CRC module and provide some data, but do not read the result. */
|
||||||
|
hal_crc_compute_partial_start(&test_cases[pol_idx[pol_cnt]].config_data);
|
||||||
|
hal_crc_compute_partial((uint8_t*) dummy_input_data, strlen((const char*) dummy_input_data));
|
||||||
|
|
||||||
|
/* Change index only if more than one supported polynomial has been found. */
|
||||||
|
if (pol_idx[POL_CNT - 1] != UNSUPPORTED) {
|
||||||
|
pol_cnt++;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Now re-init CRC module and provide new data and check the result. */
|
||||||
|
hal_crc_compute_partial_start(&test_cases[pol_idx[pol_cnt]].config_data);
|
||||||
|
hal_crc_compute_partial((uint8_t*) input_data, strlen((const char*) input_data));
|
||||||
|
const uint32_t crc = hal_crc_get_result();
|
||||||
|
|
||||||
|
TEST_ASSERT_EQUAL(test_cases[pol_idx[pol_cnt]].expected_result, crc);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Test that hal_crc_compute_partial() does nothing if pointer to buffer is undefined or
|
||||||
|
* data length is equal to 0. */
|
||||||
|
void crc_compute_partial_invalid_param_test()
|
||||||
|
{
|
||||||
|
uint32_t crc = 0;
|
||||||
|
|
||||||
|
/* At least one polynomial must be supported. */
|
||||||
|
for (unsigned int i = 0; i < (test_cases_size / sizeof(TEST_CASE)); i++) {
|
||||||
|
if (hal_crc_is_supported(&test_cases[i].config_data) == true) {
|
||||||
|
|
||||||
|
hal_crc_compute_partial_start(&test_cases[i].config_data);
|
||||||
|
|
||||||
|
/* Call hal_crc_compute_partial() with invalid parameters. */
|
||||||
|
hal_crc_compute_partial((uint8_t*) NULL, strlen((const char*) input_data));
|
||||||
|
hal_crc_compute_partial((uint8_t*) input_data, 0);
|
||||||
|
|
||||||
|
/* Now use valid parameters. */
|
||||||
|
hal_crc_compute_partial((uint8_t*) input_data,
|
||||||
|
strlen((const char*) input_data));
|
||||||
|
|
||||||
|
crc = hal_crc_get_result();
|
||||||
|
|
||||||
|
TEST_ASSERT_EQUAL(test_cases[i].expected_result, crc);
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Test that hal_crc_is_supported() returns false if pointer to the config structure is undefined. */
|
||||||
|
void crc_is_supported_invalid_param_test()
|
||||||
|
{
|
||||||
|
TEST_ASSERT_EQUAL(false, hal_crc_is_supported(NULL));
|
||||||
|
}
|
||||||
|
|
||||||
|
Case cases[] = {
|
||||||
|
Case("test: supported polynomials.", crc_is_supported_test),
|
||||||
|
Case("test: CRC calculation - single input.", crc_calc_single_test),
|
||||||
|
Case("test: CRC calculation - multi input.", crc_calc_multi_test),
|
||||||
|
Case("test: re-configure without getting the result.", crc_reconfigure_test),
|
||||||
|
Case("test: hal_crc_compute_partial() - invalid parameters.", crc_compute_partial_invalid_param_test),
|
||||||
|
Case("test: hal_crc_is_supported() - invalid parameter.", crc_is_supported_invalid_param_test),
|
||||||
|
};
|
||||||
|
|
||||||
|
utest::v1::status_t greentea_test_setup(const size_t number_of_cases)
|
||||||
|
{
|
||||||
|
GREENTEA_SETUP(30, "default_auto");
|
||||||
|
return greentea_test_setup_handler(number_of_cases);
|
||||||
|
}
|
||||||
|
|
||||||
|
Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler);
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
TEST_CASE local_test_cases[] = {
|
||||||
|
/* Predefined polynomials. */
|
||||||
|
/* 00 */{ {POLY_7BIT_SD , 7, 0x00000000, 0x00000000, false, false}, 0xEA },
|
||||||
|
/* 01 */{ {POLY_7BIT_SD , 7, 0x0000007F, 0x00000000, false, false}, 0xA0 },
|
||||||
|
/* 02 */{ {POLY_7BIT_SD , 7, 0x0000002B, 0x00000000, false, false}, 0x74 },
|
||||||
|
/* 03 */{ {POLY_7BIT_SD , 7, 0x00000000, 0x0000007F, false, false}, 0x95 },
|
||||||
|
/* 04 */{ {POLY_7BIT_SD , 7, 0x00000000, 0x0000002B, false, false}, 0xC1 },
|
||||||
|
/* 05 */{ {POLY_7BIT_SD , 7, 0x00000000, 0x00000000, true , false}, 0xA4 },
|
||||||
|
/* 06 */{ {POLY_7BIT_SD , 7, 0x00000000, 0x00000000, false, true }, 0x57 },
|
||||||
|
|
||||||
|
/* 07 */{ {POLY_8BIT_CCITT , 8, 0x00000000, 0x00000000, false, false}, 0xF4 },
|
||||||
|
/* 08 */{ {POLY_8BIT_CCITT , 8, 0x000000FF, 0x00000000, false, false}, 0xFB },
|
||||||
|
/* 09 */{ {POLY_8BIT_CCITT , 8, 0x000000AB, 0x00000000, false, false}, 0x87 },
|
||||||
|
/* 10 */{ {POLY_8BIT_CCITT , 8, 0x00000000, 0x000000FF, false, false}, 0x0B },
|
||||||
|
/* 11 */{ {POLY_8BIT_CCITT , 8, 0x00000000, 0x000000AB, false, false}, 0x5F },
|
||||||
|
/* 12 */{ {POLY_8BIT_CCITT , 8, 0x00000000, 0x00000000, true , false}, 0x04 },
|
||||||
|
/* 13 */{ {POLY_8BIT_CCITT , 8, 0x00000000, 0x00000000, false, true }, 0x2F },
|
||||||
|
|
||||||
|
/* 14 */{ {POLY_16BIT_CCITT , 16, 0x00000000, 0x00000000, false, false}, 0x31C3 },
|
||||||
|
/* 15 */{ {POLY_16BIT_CCITT , 16, 0x0000FFFF, 0x00000000, false, false}, 0x29B1 },
|
||||||
|
/* 16 */{ {POLY_16BIT_CCITT , 16, 0x0000ABAB, 0x00000000, false, false}, 0x7D70 },
|
||||||
|
/* 17 */{ {POLY_16BIT_CCITT , 16, 0x00000000, 0x0000FFFF, false, false}, 0xCE3C },
|
||||||
|
/* 18 */{ {POLY_16BIT_CCITT , 16, 0x00000000, 0x0000ABAB, false, false}, 0x9A68 },
|
||||||
|
/* 19 */{ {POLY_16BIT_CCITT , 16, 0x00000000, 0x00000000, true , false}, 0x9184 },
|
||||||
|
/* 20 */{ {POLY_16BIT_CCITT , 16, 0x00000000, 0x00000000, false, true }, 0xC38C },
|
||||||
|
|
||||||
|
/* 21 */{ {POLY_16BIT_IBM , 16, 0x00000000, 0x00000000, false, false}, 0xFEE8 },
|
||||||
|
/* 22 */{ {POLY_16BIT_IBM , 16, 0x0000FFFF, 0x00000000, false, false}, 0xAEE7 },
|
||||||
|
/* 23 */{ {POLY_16BIT_IBM , 16, 0x0000ABAB, 0x00000000, false, false}, 0x0887 },
|
||||||
|
/* 24 */{ {POLY_16BIT_IBM , 16, 0x00000000, 0x0000FFFF, false, false}, 0x0117 },
|
||||||
|
/* 25 */{ {POLY_16BIT_IBM , 16, 0x00000000, 0x0000ABAB, false, false}, 0x5543 },
|
||||||
|
/* 26 */{ {POLY_16BIT_IBM , 16, 0x00000000, 0x00000000, true , false}, 0xBCDD },
|
||||||
|
/* 27 */{ {POLY_16BIT_IBM , 16, 0x00000000, 0x00000000, false, true }, 0x177F },
|
||||||
|
|
||||||
|
/* 28 */{ {POLY_32BIT_ANSI , 32, 0x00000000, 0x00000000, false, false}, 0x89A1897F },
|
||||||
|
/* 29 */{ {POLY_32BIT_ANSI , 32, 0xFFFFFFFF, 0x00000000, false, false}, 0x0376E6E7 },
|
||||||
|
/* 30 */{ {POLY_32BIT_ANSI , 32, 0xABABABAB, 0x00000000, false, false}, 0x871A2FAA },
|
||||||
|
/* 31 */{ {POLY_32BIT_ANSI , 32, 0x00000000, 0xFFFFFFFF, false, false}, 0x765E7680 },
|
||||||
|
/* 32 */{ {POLY_32BIT_ANSI , 32, 0x00000000, 0xABABABAB, false, false}, 0x220A22D4 },
|
||||||
|
/* 33 */{ {POLY_32BIT_ANSI , 32, 0x00000000, 0x00000000, true , false}, 0x11B4BFB4 },
|
||||||
|
/* 34 */{ {POLY_32BIT_ANSI , 32, 0x00000000, 0x00000000, false, true }, 0xFE918591 },
|
||||||
|
|
||||||
|
/* Not-predefined polynomials. */
|
||||||
|
/* 35 */{ {POLY_8BIT_MAXIM , 8, 0x00000000, 0x00000000, false, false}, 0xA2 },
|
||||||
|
/* 36 */{ {POLY_8BIT_MAXIM , 8, 0x000000FF, 0x00000000, false, false}, 0xF7 },
|
||||||
|
/* 37 */{ {POLY_8BIT_MAXIM , 8, 0x000000AB, 0x00000000, false, false}, 0x71 },
|
||||||
|
/* 38 */{ {POLY_8BIT_MAXIM , 8, 0x00000000, 0x000000FF, false, false}, 0x5D },
|
||||||
|
/* 39 */{ {POLY_8BIT_MAXIM , 8, 0x00000000, 0x000000AB, false, false}, 0x09 },
|
||||||
|
/* 40 */{ {POLY_8BIT_MAXIM , 8, 0x00000000, 0x00000000, true , false}, 0x85 },
|
||||||
|
/* 41 */{ {POLY_8BIT_MAXIM , 8, 0x00000000, 0x00000000, false, true }, 0x45 },
|
||||||
|
|
||||||
|
/* 42 */{ {POLY_16BIT_MAXIM , 16, 0x00000000, 0x00000000, false, false}, 0xFEE8 },
|
||||||
|
/* 43 */{ {POLY_16BIT_MAXIM , 16, 0x0000FFFF, 0x00000000, false, false}, 0xAEE7 },
|
||||||
|
/* 44 */{ {POLY_16BIT_MAXIM , 16, 0x0000ABAB, 0x00000000, false, false}, 0x0887 },
|
||||||
|
/* 45 */{ {POLY_16BIT_MAXIM , 16, 0x00000000, 0x0000FFFF, false, false}, 0x0117 },
|
||||||
|
/* 46 */{ {POLY_16BIT_MAXIM , 16, 0x00000000, 0x0000ABAB, false, false}, 0x5543 },
|
||||||
|
/* 47 */{ {POLY_16BIT_MAXIM , 16, 0x00000000, 0x00000000, true , false}, 0xBCDD },
|
||||||
|
/* 48 */{ {POLY_16BIT_MAXIM , 16, 0x00000000, 0x00000000, false, true }, 0x177F },
|
||||||
|
|
||||||
|
/* 49 */{ {POLY_32BIT_POSIX , 32, 0x00000000, 0x00000000, false, false}, 0x89A1897F },
|
||||||
|
/* 50 */{ {POLY_32BIT_POSIX , 32, 0xFFFFFFFF, 0x00000000, false, false}, 0x0376E6E7 },
|
||||||
|
/* 51 */{ {POLY_32BIT_POSIX , 32, 0xABABABAB, 0x00000000, false, false}, 0x871A2FAA },
|
||||||
|
/* 52 */{ {POLY_32BIT_POSIX , 32, 0x00000000, 0xFFFFFFFF, false, false}, 0x765E7680 },
|
||||||
|
/* 53 */{ {POLY_32BIT_POSIX , 32, 0x00000000, 0xABABABAB, false, false}, 0x220A22D4 },
|
||||||
|
/* 54 */{ {POLY_32BIT_POSIX , 32, 0x00000000, 0x00000000, true , false}, 0x11B4BFB4 },
|
||||||
|
/* 55 */{ {POLY_32BIT_POSIX , 32, 0x00000000, 0x00000000, false, true }, 0xFE918591 },
|
||||||
|
};
|
||||||
|
|
||||||
|
test_cases = local_test_cases;
|
||||||
|
test_cases_size = sizeof(local_test_cases);
|
||||||
|
|
||||||
|
Harness::run(specification);
|
||||||
|
}
|
|
@ -62,9 +62,64 @@ extern "C" {
|
||||||
* The Hardware CRC HAL API provides a low-level interface to the Hardware CRC
|
* The Hardware CRC HAL API provides a low-level interface to the Hardware CRC
|
||||||
* module of a target platform.
|
* module of a target platform.
|
||||||
*
|
*
|
||||||
|
* # Defined behaviour
|
||||||
|
*
|
||||||
|
* * Function hal_crc_is_supported() returns true if platform supports hardware
|
||||||
|
* CRC for the given polynomial/width - verified by test ::crc_is_supported_test.
|
||||||
|
* * Function hal_crc_is_supported() returns false if platform does not support hardware
|
||||||
|
* CRC for the given polynomial/width - verified by test ::crc_is_supported_test.
|
||||||
|
* * Function hal_crc_is_supported() returns false if given pointer to configuration
|
||||||
|
* structure is undefined (NULL) - verified by test ::crc_is_supported_invalid_param_test.
|
||||||
|
* * If CRC module does not support one of the following settings: initial_xor, final_xor
|
||||||
|
* reflect_in, reflect_out, then these operations should be handled by the driver
|
||||||
|
* - Verified by test ::crc_calc_single_test.
|
||||||
|
* * Platform which supports hardware CRC must be able to handle at least one of the predefined
|
||||||
|
* polynomial/width configurations that can be constructed in the MbedCRC class: POLY_8BIT_CCITT,
|
||||||
|
* POLY_7BIT_SD, POLY_16BIT_CCITT, POLY_16BIT_IBM, POLY_32BIT_ANSI
|
||||||
|
* - verified by test ::crc_is_supported_test, ::crc_calc_single_test.
|
||||||
|
* * Function hal_crc_compute_partial_start() configures CRC module with the given configuration
|
||||||
|
* - Verified by test ::crc_calc_single_test.
|
||||||
|
* * Calling hal_crc_compute_partial_start() without finalising the
|
||||||
|
* CRC calculation overrides the current configuration - Verified by test ::crc_reconfigure_test.
|
||||||
|
* * Function hal_crc_compute_partial() writes data to the CRC module - verified by test ::crc_calc_single_test.
|
||||||
|
* * Function hal_crc_compute_partial() can be call multiple times in succession in order to
|
||||||
|
* provide additional data to CRC module - verified by test ::crc_calc_multi_test.
|
||||||
|
* * Function hal_crc_compute_partial() does nothing if pointer to buffer is undefined or
|
||||||
|
* data length is equal to 0 - verified by test ::crc_compute_partial_invalid_param_test.
|
||||||
|
* * Function hal_crc_get_result() returns the checksum result from the CRC module
|
||||||
|
* - verified by tests ::crc_calc_single_test, ::crc_calc_multi_test, ::crc_reconfigure_test.
|
||||||
|
*
|
||||||
|
* # Undefined behaviour
|
||||||
|
*
|
||||||
|
* * Calling hal_crc_compute_partial_start() function with invalid (unsupported) polynomial.
|
||||||
|
* * Calling hal_crc_compute_partial() or hal_crc_get_result() functions before hal_crc_compute_partial_start().
|
||||||
|
* * Calling hal_crc_get_result() function multiple times.
|
||||||
|
*
|
||||||
|
* # Non-functional requirements
|
||||||
|
*
|
||||||
|
* * CRC configuration provides the following settings:
|
||||||
|
* * polynomial - CRC Polynomial,
|
||||||
|
* * width - CRC bit width,
|
||||||
|
* * initial_xor - seed value for the computation,
|
||||||
|
* * final_xor - final xor value for the computation,
|
||||||
|
* * reflect_in - reflect bits on input,
|
||||||
|
* * reflect_out - reflect bits in final result before returning.
|
||||||
|
*
|
||||||
|
* # Potential bugs
|
||||||
|
*
|
||||||
* @{
|
* @{
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \defgroup hal_crc_tests crc hal tests
|
||||||
|
* The crc HAL tests ensure driver conformance to defined behaviour.
|
||||||
|
*
|
||||||
|
* To run the crc hal tests use the command:
|
||||||
|
*
|
||||||
|
* mbed test -t <toolchain> -m <target> -n tests-mbed_hal-crc*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
/** Determine if the current platform supports hardware CRC for given polynomial
|
/** Determine if the current platform supports hardware CRC for given polynomial
|
||||||
*
|
*
|
||||||
* The purpose of this function is to inform the CRC Platform API whether the
|
* The purpose of this function is to inform the CRC Platform API whether the
|
||||||
|
|
|
@ -25,67 +25,83 @@ static uint32_t final_xor;
|
||||||
|
|
||||||
bool hal_crc_is_supported(const crc_mbed_config_t* config)
|
bool hal_crc_is_supported(const crc_mbed_config_t* config)
|
||||||
{
|
{
|
||||||
if (config == NULL) {
|
if (config == NULL) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((config->width != 32) || (config->width != 16)) {
|
if ((config->width != 32) && (config->width != 16)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void hal_crc_compute_partial_start(const crc_mbed_config_t* config)
|
void hal_crc_compute_partial_start(const crc_mbed_config_t* config)
|
||||||
{
|
{
|
||||||
if (config == NULL) {
|
if (config == NULL) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
width = (config->width == 32) ? kCrcBits32 : kCrcBits16;
|
width = (config->width == 32) ? kCrcBits32 : kCrcBits16;
|
||||||
final_xor = config->final_xor;
|
final_xor = config->final_xor;
|
||||||
|
|
||||||
crc_config_t platform_config;
|
crc_config_t platform_config;
|
||||||
platform_config.polynomial = config->polynomial;
|
platform_config.polynomial = config->polynomial;
|
||||||
platform_config.seed = config->initial_xor;
|
platform_config.seed = config->initial_xor;
|
||||||
platform_config.reflectIn = config->reflect_in;
|
platform_config.reflectIn = config->reflect_in;
|
||||||
platform_config.reflectOut = config->reflect_out;
|
platform_config.reflectOut = config->reflect_out;
|
||||||
platform_config.complementChecksum = (config->final_xor == 0xFFFFFFFFU);
|
if ((width == kCrcBits16 && config->final_xor == 0xFFFFU) ||
|
||||||
platform_config.crcBits = width;
|
(width == kCrcBits32 && config->final_xor == 0xFFFFFFFFU)) {
|
||||||
platform_config.crcResult = kCrcFinalChecksum;
|
platform_config.complementChecksum = true;
|
||||||
|
} else {
|
||||||
|
platform_config.complementChecksum = false;
|
||||||
|
}
|
||||||
|
platform_config.crcBits = width;
|
||||||
|
platform_config.crcResult = kCrcFinalChecksum;
|
||||||
|
|
||||||
CRC_Init(CRC0, &platform_config);
|
CRC_Init(CRC0, &platform_config);
|
||||||
}
|
}
|
||||||
|
|
||||||
void hal_crc_compute_partial(const uint8_t *data, const size_t size)
|
void hal_crc_compute_partial(const uint8_t *data, const size_t size)
|
||||||
{
|
{
|
||||||
if (data == NULL) {
|
if (data == NULL) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (size == 0) {
|
if (size == 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
CRC_WriteData(CRC0, data, size);
|
CRC_WriteData(CRC0, data, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t hal_crc_get_result(void)
|
uint32_t hal_crc_get_result(void)
|
||||||
{
|
{
|
||||||
if ((final_xor != 0x00000000U) && (final_xor != 0xFFFFFFFFU)) {
|
uint32_t result;
|
||||||
CRC_WriteData(CRC0, (uint8_t*)&final_xor, sizeof(final_xor));
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (width)
|
const bool manual_final_xor = ((final_xor != 0x00000000U) &&
|
||||||
{
|
((final_xor != 0xFFFFFFFFU && width == kCrcBits32) ||
|
||||||
case kCrcBits16:
|
(final_xor != 0xFFFFU && width == kCrcBits16)));
|
||||||
return CRC_Get16bitResult(CRC0);
|
|
||||||
case kCrcBits32:
|
switch (width)
|
||||||
return CRC_Get32bitResult(CRC0);
|
{
|
||||||
default:
|
case kCrcBits16:
|
||||||
MBED_ASSERT("Unhandled switch case");
|
result = CRC_Get16bitResult(CRC0);
|
||||||
return 0;
|
if (manual_final_xor) {
|
||||||
}
|
result ^= final_xor;
|
||||||
|
result &= 0xFFFF;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
case kCrcBits32:
|
||||||
|
result = CRC_Get32bitResult(CRC0);
|
||||||
|
if (manual_final_xor) {
|
||||||
|
result ^= final_xor;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
default:
|
||||||
|
MBED_ASSERT("Unhandled switch case");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // DEVICE_CRC
|
#endif // DEVICE_CRC
|
||||||
|
|
|
@ -55,7 +55,9 @@ void hal_crc_compute_partial_start(const crc_mbed_config_t* config)
|
||||||
|
|
||||||
void hal_crc_compute_partial(const uint8_t *data, const size_t size)
|
void hal_crc_compute_partial(const uint8_t *data, const size_t size)
|
||||||
{
|
{
|
||||||
HAL_CRC_Accumulate(¤t_state, (uint32_t *)data, size);
|
if (data && size) {
|
||||||
|
HAL_CRC_Accumulate(¤t_state, (uint32_t *)data, size);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t hal_crc_get_result(void)
|
uint32_t hal_crc_get_result(void)
|
||||||
|
|
Loading…
Reference in New Issue