Skip to content

MbedCRC and CRC HAL revisions (6.0 redo) #11957

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 5 commits into from
Dec 3, 2019
Merged
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
72 changes: 51 additions & 21 deletions TESTS/mbed_drivers/crc/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,32 +25,62 @@ using namespace utest::v1;

void test_supported_polynomials()
{
char test[] = "123456789";
const char test[] = "123456789";
uint32_t crc;

{
MbedCRC<POLY_7BIT_SD, 7> ct;
TEST_ASSERT_EQUAL(0, ct.compute((void *)test, strlen((const char *)test), &crc));
TEST_ASSERT_EQUAL(0xEA, crc);
TEST_ASSERT_EQUAL(0, ct.compute(test, strlen(test), &crc));
TEST_ASSERT_EQUAL(0x75, crc);
}
{
MbedCRC<POLY_7BIT_SD, 7> ct(0x7F, 0, false, false);
TEST_ASSERT_EQUAL(0, ct.compute(test, strlen(test), &crc));
TEST_ASSERT_EQUAL(0x50, crc);
}
{
MbedCRC<POLY_7BIT_SD, 7> ct(0x2B, 0, false, false);
TEST_ASSERT_EQUAL(0, ct.compute(test, strlen(test), &crc));
TEST_ASSERT_EQUAL(0x3A, crc);
}
{
MbedCRC<POLY_7BIT_SD, 7> ct(0, 0x7F, false, false);
TEST_ASSERT_EQUAL(0, ct.compute(test, strlen(test), &crc));
TEST_ASSERT_EQUAL(0x0A, crc);
}
{
MbedCRC<POLY_7BIT_SD, 7> ct(0, 0x2B, false, false);
TEST_ASSERT_EQUAL(0, ct.compute(test, strlen(test), &crc));
TEST_ASSERT_EQUAL(0x5E, crc);
}
{
MbedCRC<POLY_7BIT_SD, 7> ct(0, 0, true, false);
TEST_ASSERT_EQUAL(0, ct.compute(test, strlen(test), &crc));
TEST_ASSERT_EQUAL(0x52, crc);
}
{
MbedCRC<POLY_7BIT_SD, 7> ct(0, 0, false, true);
TEST_ASSERT_EQUAL(0, ct.compute(test, strlen(test), &crc));
TEST_ASSERT_EQUAL(0x57, crc);
}
{
MbedCRC<POLY_8BIT_CCITT, 8> ct;
TEST_ASSERT_EQUAL(0, ct.compute((void *)test, strlen((const char *)test), &crc));
TEST_ASSERT_EQUAL(0, ct.compute(test, strlen(test), &crc));
TEST_ASSERT_EQUAL(0xF4, crc);
}
{
MbedCRC<POLY_16BIT_CCITT, 16> ct;
TEST_ASSERT_EQUAL(0, ct.compute((void *)test, strlen((const char *)test), &crc));
TEST_ASSERT_EQUAL(0, ct.compute(test, strlen(test), &crc));
TEST_ASSERT_EQUAL(0x29B1, crc);
}
{
MbedCRC<POLY_16BIT_IBM, 16> ct;
TEST_ASSERT_EQUAL(0, ct.compute((void *)test, strlen((const char *)test), &crc));
TEST_ASSERT_EQUAL(0, ct.compute(test, strlen(test), &crc));
TEST_ASSERT_EQUAL(0xBB3D, crc);
}
{
MbedCRC<POLY_32BIT_ANSI, 32> ct;
TEST_ASSERT_EQUAL(0, ct.compute((void *)test, strlen((const char *)test), &crc));
TEST_ASSERT_EQUAL(0, ct.compute(test, strlen(test), &crc));
TEST_ASSERT_EQUAL(0xCBF43926, crc);
}
}
Expand All @@ -62,8 +92,8 @@ void test_partial_crc()
{
MbedCRC<POLY_16BIT_CCITT, 16> ct;
TEST_ASSERT_EQUAL(0, ct.compute_partial_start(&crc));
TEST_ASSERT_EQUAL(0, ct.compute_partial((void *)&test, 4, &crc));
TEST_ASSERT_EQUAL(0, ct.compute_partial((void *)&test[4], 5, &crc));
TEST_ASSERT_EQUAL(0, ct.compute_partial(test, 4, &crc));
TEST_ASSERT_EQUAL(0, ct.compute_partial(&test[4], 5, &crc));
TEST_ASSERT_EQUAL(0, ct.compute_partial_stop(&crc));

TEST_ASSERT_EQUAL(0x29B1, crc);
Expand All @@ -81,31 +111,31 @@ void test_sd_crc()
test[2] = 0x00;
test[3] = 0x00;
test[4] = 0x00;
TEST_ASSERT_EQUAL(0, crc7.compute((void *)test, 5, &crc));
crc = (crc | 0x1) & 0xFF;
TEST_ASSERT_EQUAL(0, crc7.compute(test, 5, &crc));
crc = (crc << 1) | 0x1;
TEST_ASSERT_EQUAL(0x95, crc);

test[0] = 0x48;
test[1] = 0x00;
test[2] = 0x00;
test[3] = 0x01;
test[4] = 0xAA;
TEST_ASSERT_EQUAL(0, crc7.compute((void *)test, 5, &crc));
crc = (crc | 0x1) & 0xFF;
TEST_ASSERT_EQUAL(0, crc7.compute(test, 5, &crc));
crc = (crc << 1) | 0x1;
TEST_ASSERT_EQUAL(0x87, crc);

test[0] = 0x51;
test[1] = 0x00;
test[2] = 0x00;
test[3] = 0x00;
test[4] = 0x00;
TEST_ASSERT_EQUAL(0, crc7.compute((void *)test, 5, &crc));
crc = (crc | 0x1) & 0xFF;
TEST_ASSERT_EQUAL(0, crc7.compute(test, 5, &crc));
crc = (crc << 1) | 0x1;
TEST_ASSERT_EQUAL(0x55, crc);

MbedCRC<POLY_16BIT_CCITT, 16> crc16(0, 0, false, false);
memset(test, 0xFF, 512);
TEST_ASSERT_EQUAL(0, crc16.compute((void *)test, 512, &crc));
TEST_ASSERT_EQUAL(0, crc16.compute(test, 512, &crc));
TEST_ASSERT_EQUAL(0x7FA1, crc);
}

Expand All @@ -115,12 +145,12 @@ void test_any_polynomial()
uint32_t crc;
{
MbedCRC<0x3D65, 16> ct(0x0, 0xFFFF, 0, 0);
TEST_ASSERT_EQUAL(0, ct.compute((void *)test, strlen((const char *)test), &crc));
TEST_ASSERT_EQUAL(0, ct.compute(test, strlen(test), &crc));
TEST_ASSERT_EQUAL(0xC2B7, crc);
}
{
MbedCRC<0x1EDC6F41, 32> ct(0xFFFFFFFF, 0xFFFFFFFF, 1, 1);
TEST_ASSERT_EQUAL(0, ct.compute((void *)test, strlen((const char *)test), &crc));
TEST_ASSERT_EQUAL(0, ct.compute(test, strlen(test), &crc));
TEST_ASSERT_EQUAL(0xE3069283, crc);
}
}
Expand All @@ -130,7 +160,7 @@ void test_thread(void)
char test[] = "123456789";
uint32_t crc;
MbedCRC<POLY_32BIT_ANSI, 32> ct;
TEST_ASSERT_EQUAL(0, ct.compute((void *)test, strlen((const char *)test), &crc));
TEST_ASSERT_EQUAL(0, ct.compute(test, strlen(test), &crc));
TEST_ASSERT_EQUAL(0xCBF43926, crc);
}

Expand All @@ -143,11 +173,11 @@ void test_thread_safety()
MbedCRC<POLY_16BIT_IBM, 16> ct;

TEST_ASSERT_EQUAL(0, ct.compute_partial_start(&crc));
TEST_ASSERT_EQUAL(0, ct.compute_partial((void *)&test, 4, &crc));
TEST_ASSERT_EQUAL(0, ct.compute_partial(test, 4, &crc));

Thread t1(osPriorityNormal1, 380);
t1.start(callback(test_thread));
TEST_ASSERT_EQUAL(0, ct.compute_partial((void *)&test[4], 5, &crc));
TEST_ASSERT_EQUAL(0, ct.compute_partial(&test[4], 5, &crc));
TEST_ASSERT_EQUAL(0, ct.compute_partial_stop(&crc));
TEST_ASSERT_EQUAL(0xBB3D, crc);

Expand Down
29 changes: 11 additions & 18 deletions TESTS/mbed_hal/crc/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ void crc_is_supported_test()
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) {
if (HAL_CRC_IS_SUPPORTED(test_cases[i].config_data.polynomial, test_cases[i].config_data.width)) {

num_of_supported_polynomials++;
}
Expand All @@ -68,7 +68,7 @@ void crc_is_supported_test()
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) {
if (HAL_CRC_IS_SUPPORTED(test_cases[i].config_data.polynomial, test_cases[i].config_data.width)) {

hal_crc_compute_partial_start(&test_cases[i].config_data);
hal_crc_compute_partial((uint8_t *) input_data, strlen((const char *) input_data));
Expand All @@ -84,7 +84,7 @@ void crc_calc_single_test()
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) {
if (HAL_CRC_IS_SUPPORTED(test_cases[i].config_data.polynomial, test_cases[i].config_data.width)) {

const uint32_t first_part_bytes = 3;
const uint32_t second_part_bytes = 1;
Expand Down Expand Up @@ -117,7 +117,7 @@ void crc_reconfigure_test()
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 (HAL_CRC_IS_SUPPORTED(test_cases[i].config_data.polynomial, test_cases[i].config_data.width)) {
if (pol_cnt == 0) {
pol_idx[pol_cnt] = i;
pol_cnt++;
Expand Down Expand Up @@ -159,7 +159,7 @@ void crc_compute_partial_invalid_param_test()

/* 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) {
if (HAL_CRC_IS_SUPPORTED(test_cases[i].config_data.polynomial, test_cases[i].config_data.width)) {

hal_crc_compute_partial_start(&test_cases[i].config_data);

Expand All @@ -180,19 +180,12 @@ void crc_compute_partial_invalid_param_test()
}
}

/* 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)
Expand All @@ -208,12 +201,12 @@ int main()
// *INDENT-OFF*
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 },
/* 00 */{ {POLY_7BIT_SD , 7, 0x00000000, 0x00000000, false, false}, 0x75 },
/* 01 */{ {POLY_7BIT_SD , 7, 0x0000007F, 0x00000000, false, false}, 0x50 },
/* 02 */{ {POLY_7BIT_SD , 7, 0x0000002B, 0x00000000, false, false}, 0x3A },
/* 03 */{ {POLY_7BIT_SD , 7, 0x00000000, 0x0000007F, false, false}, 0x0A },
/* 04 */{ {POLY_7BIT_SD , 7, 0x00000000, 0x0000002B, false, false}, 0x5E },
/* 05 */{ {POLY_7BIT_SD , 7, 0x00000000, 0x00000000, true , false}, 0x52 },
/* 06 */{ {POLY_7BIT_SD , 7, 0x00000000, 0x00000000, false, true }, 0x57 },

/* 07 */{ {POLY_8BIT_CCITT , 8, 0x00000000, 0x00000000, false, false}, 0xF4 },
Expand Down
2 changes: 1 addition & 1 deletion UNITTESTS/empty_baseline/unittest.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ set(unittest-test-sources
empty_baseline/empty_baseline.cpp
)

set(DEVICE_FLAGS "-DDEVICE_ANALOGIN -DDEVICE_ANALOGOUT -DDEVICE_CAN -DDEVICE_CRC -DDEVICE_ETHERNET -DDEVICE_FLASH -DDEVICE_I2C -DDEVICE_I2CSLAVE -DDEVICE_I2C_ASYNCH -DDEVICE_INTERRUPTIN -DDEVICE_LPTICKER -DDEVICE_PORTIN -DDEVICE_PORTINOUT -DDEVICE_PORTOUT -DDEVICE_PWMOUT -DDEVICE_QSPI -DDEVICE_SERIAL -DDEVICE_SERIAL_ASYNCH -DDEVICE_SERIAL_FC -DDEVICE_SPI -DDEVICE_SPISLAVE -DDEVICE_SPI_ASYNCH -DDEVICE_FLASH -DCOMPONENT_FLASHIAP")
set(DEVICE_FLAGS "-DDEVICE_ANALOGIN -DDEVICE_ANALOGOUT -DDEVICE_CAN -DDEVICE_ETHERNET -DDEVICE_FLASH -DDEVICE_I2C -DDEVICE_I2CSLAVE -DDEVICE_I2C_ASYNCH -DDEVICE_INTERRUPTIN -DDEVICE_LPTICKER -DDEVICE_PORTIN -DDEVICE_PORTINOUT -DDEVICE_PORTOUT -DDEVICE_PWMOUT -DDEVICE_QSPI -DDEVICE_SERIAL -DDEVICE_SERIAL_ASYNCH -DDEVICE_SERIAL_FC -DDEVICE_SPI -DDEVICE_SPISLAVE -DDEVICE_SPI_ASYNCH -DDEVICE_FLASH -DCOMPONENT_FLASHIAP")
set(CONF_FLAGS "-DMBED_CONF_PLATFORM_CTHUNK_COUNT_MAX=10 -DMBED_CONF_DATAFLASH_SPI_FREQ=1 -DMBED_CONF_FLASHIAP_BLOCK_DEVICE_BASE_ADDRESS=0 -DMBED_CONF_FLASHIAP_BLOCK_DEVICE_SIZE=0 -DMBED_CONF_QSPIF_QSPI_FREQ=1 -DMBED_CONF_QSPIF_QSPI_MIN_READ_SIZE=1 -DMBED_CONF_QSPIF_QSPI_MIN_PROG_SIZE=1 -DMBED_LFS_READ_SIZE=64 -DMBED_LFS_PROG_SIZE=64 -DMBED_LFS_BLOCK_SIZE=512 -DMBED_LFS_LOOKAHEAD=512 -DFLASHIAP_APP_ROM_END_ADDR=0x80000 -DMBED_CONF_STORAGE_TDB_INTERNAL_INTERNAL_SIZE=1024 -DMBED_CONF_STORAGE_TDB_INTERNAL_INTERNAL_BASE_ADDRESS=0x80000 -DMBED_CONF_STORAGE_STORAGE_TYPE=default")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${DEVICE_FLAGS} ${CONF_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${DEVICE_FLAGS} ${CONF_FLAGS}")
16 changes: 16 additions & 0 deletions UNITTESTS/target_h/cmsis.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,19 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef MBED_CMSIS_H
#define MBED_CMSIS_H

#include <stdint.h>
static inline uint32_t __RBIT(uint32_t x)
{
x = ((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1);
x = ((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2);
x = ((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4);
x = ((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8);
x = (x >> 16) | (x << 16);
return x;
}

#endif
24 changes: 14 additions & 10 deletions components/storage/blockdevice/COMPONENT_SD/SDBlockDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ const uint32_t SDBlockDevice::_block_size = BLOCK_SIZE_HC;
#if MBED_CONF_SD_CRC_ENABLED
SDBlockDevice::SDBlockDevice(PinName mosi, PinName miso, PinName sclk, PinName cs, uint64_t hz, bool crc_on)
: _sectors(0), _spi(mosi, miso, sclk), _cs(cs), _is_initialized(0),
_init_ref_count(0), _crc_on(crc_on), _crc16(0, 0, false, false)
_init_ref_count(0), _crc_on(crc_on)
#else
SDBlockDevice::SDBlockDevice(PinName mosi, PinName miso, PinName sclk, PinName cs, uint64_t hz, bool crc_on)
: _sectors(0), _spi(mosi, miso, sclk), _cs(cs), _is_initialized(0),
Expand Down Expand Up @@ -678,10 +678,11 @@ uint8_t SDBlockDevice::_cmd_spi(SDBlockDevice::cmdSupported cmd, uint32_t arg)
cmdPacket[4] = (arg >> 0);

#if MBED_CONF_SD_CRC_ENABLED
uint32_t crc;
if (_crc_on) {
_crc7.compute((void *)cmdPacket, 5, &crc);
cmdPacket[5] = (char)(crc | 0x01);
MbedCRC<POLY_7BIT_SD, 7> crc7;
uint32_t crc;
crc7.compute(cmdPacket, 5, &crc);
cmdPacket[5] = ((uint8_t) crc << 1) | 0x01;
} else
#endif
{
Expand Down Expand Up @@ -899,12 +900,13 @@ int SDBlockDevice::_read_bytes(uint8_t *buffer, uint32_t length)

#if MBED_CONF_SD_CRC_ENABLED
if (_crc_on) {
mbed::MbedCRC<POLY_16BIT_CCITT, 16> crc16(0, 0, false, false);
uint32_t crc_result;
// Compute and verify checksum
_crc16.compute((void *)buffer, length, &crc_result);
if ((uint16_t)crc_result != crc) {
debug_if(SD_DBG, "_read_bytes: Invalid CRC received 0x%" PRIx16 " result of computation 0x%" PRIx16 "\n",
crc, (uint16_t)crc_result);
crc16.compute(buffer, length, &crc_result);
if (crc_result != crc) {
debug_if(SD_DBG, "_read_bytes: Invalid CRC received 0x%" PRIx16 " result of computation 0x%" PRIx32 "\n",
crc, crc_result);
_deselect();
return SD_BLOCK_DEVICE_ERROR_CRC;
}
Expand Down Expand Up @@ -934,9 +936,10 @@ int SDBlockDevice::_read(uint8_t *buffer, uint32_t length)

#if MBED_CONF_SD_CRC_ENABLED
if (_crc_on) {
mbed::MbedCRC<POLY_16BIT_CCITT, 16> crc16(0, 0, false, false);
uint32_t crc_result;
// Compute and verify checksum
_crc16.compute((void *)buffer, length, &crc_result);
crc16.compute((void *)buffer, length, &crc_result);
if ((uint16_t)crc_result != crc) {
debug_if(SD_DBG, "_read_bytes: Invalid CRC received 0x%" PRIx16 " result of computation 0x%" PRIx16 "\n",
crc, (uint16_t)crc_result);
Expand All @@ -962,8 +965,9 @@ uint8_t SDBlockDevice::_write(const uint8_t *buffer, uint8_t token, uint32_t len

#if MBED_CONF_SD_CRC_ENABLED
if (_crc_on) {
mbed::MbedCRC<POLY_16BIT_CCITT, 16> crc16(0, 0, false, false);
// Compute CRC
_crc16.compute((void *)buffer, length, &crc);
crc16.compute(buffer, length, &crc);
}
#endif

Expand Down
2 changes: 0 additions & 2 deletions components/storage/blockdevice/COMPONENT_SD/SDBlockDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,6 @@ class SDBlockDevice : public mbed::BlockDevice {

#if MBED_CONF_SD_CRC_ENABLED
bool _crc_on;
mbed::MbedCRC<POLY_7BIT_SD, 7> _crc7;
mbed::MbedCRC<POLY_16BIT_CCITT, 16> _crc16;
#endif
};

Expand Down
Loading