Skip to content

Commit a167e89

Browse files
author
Felipe Torrezan
committed
Merge branch 'felipe-iar-lib-add-crc'
2 parents 3782f7b + fa0a7fe commit a167e89

7 files changed

Lines changed: 143 additions & 4 deletions

File tree

.github/workflows/iar.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ jobs:
7878
run: ctest --test-dir build -C Release
7979
- name: CTest - Unit testing Release With Debug Info configuration
8080
run: ctest --test-dir build -C RelWithDebInfo
81-
- name: CTest - Unit testing Minimal Size With Debug Info configuration
81+
- name: CTest - Unit testing Minimal Size configuration
8282
run: ctest --test-dir build -C MinSizeRel
8383

8484
deploy:

lib/CMakeLists.txt

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ add_library(util)
22

33
# Tests
44
add_executable(hello_test)
5+
add_executable(crc16_test)
6+
add_executable(crc32_test)
57
add_executable(bm_search_test)
68
add_executable(linear_search_test)
79

@@ -13,12 +15,20 @@ target_include_directories(util PUBLIC
1315
# Sources: library
1416
target_sources(util PRIVATE
1517
bm_search.c
18+
crc16.c
19+
crc32.c
1620
linear_search.c
1721
)
1822
# Sources: unit tests
1923
target_sources(hello_test PRIVATE
2024
hello_test.c
2125
)
26+
target_sources(crc16_test PRIVATE
27+
crc16_test.c
28+
)
29+
target_sources(crc32_test PRIVATE
30+
crc32_test.c
31+
)
2232
target_sources(bm_search_test PRIVATE
2333
bm_search_test.c
2434
)
@@ -30,20 +40,24 @@ target_sources(linear_search_test PRIVATE
3040
set_target_properties(
3141
util
3242
PROPERTIES
33-
COMPILE_OPTIONS "--cpu=cortex-m4;--dlib_config=full;-e"
43+
COMPILE_OPTIONS "--cpu=cortex-m4;--dlib_config=full;-e;--no_wrap_diagnostics"
3444
)
3545
# Properties: unit testing
3646
set_target_properties(
3747
bm_search_test
48+
crc16_test
49+
crc32_test
3850
linear_search_test
3951
hello_test
4052
PROPERTIES
41-
COMPILE_OPTIONS "--cpu=cortex-m4;--dlib_config=full;-e"
53+
COMPILE_OPTIONS "--cpu=cortex-m4;--dlib_config=full;-e;--no_wrap_diagnostics"
4254
LINK_LIBRARIES util
4355
LINK_OPTIONS "--cpu=cortex-m4;--semihosting;--redirect=_Printf=_PrintfFullNoMb"
4456
)
4557

4658
# Register unit tests
4759
iar_cspysim(hello_test)
60+
iar_cspysim(crc16_test)
61+
iar_cspysim(crc32_test)
4862
iar_cspysim(bm_search_test)
4963
iar_cspysim(linear_search_test)

lib/crc16.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
Copyright (c) 2020-2025, IAR Systems AB.
3+
SPDX-License-Identifier: MIT
4+
*/
5+
6+
/*!
7+
\file crc16.c
8+
\brief Software implementation for CRC16
9+
\version 20250929
10+
*/
11+
12+
#include <stddef.h>
13+
#include <stdint.h>
14+
15+
#define CRC16_POLY 0x1021
16+
17+
uint16_t crc16(const char * data)
18+
{
19+
uint16_t crc = 0xFFFF;
20+
21+
while (*data)
22+
{
23+
crc ^= ((uint8_t)(*data++) << 8);
24+
for (int i = 0; i < 8; i++)
25+
{
26+
if (crc & 0x8000)
27+
crc = (crc << 1) ^ CRC16_POLY;
28+
else
29+
crc <<= 1;
30+
}
31+
}
32+
return crc;
33+
}

lib/crc16_test.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
Copyright (c) 2020-2025, IAR Systems AB.
3+
SPDX-License-Identifier: MIT
4+
*/
5+
6+
/*!
7+
\file crc16_test.c
8+
\brief CRC16 test module
9+
\version 20250929
10+
*/
11+
12+
#include <errno.h>
13+
#include "acutest.h"
14+
#include "util.h"
15+
16+
17+
void crc16_test()
18+
{
19+
TEST_CASE("The quick brown fox");
20+
char * message = "The quick brown fox jumps over the lazy dog";
21+
uint16_t result = crc16(message);
22+
TEST_CHECK(0x8FDD == result);
23+
}
24+
25+
TEST_LIST= {
26+
{"CRC16", crc16_test},
27+
28+
{NULL, NULL}
29+
};

lib/crc32.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
Copyright (c) 2020-2025, IAR Systems AB.
3+
SPDX-License-Identifier: MIT
4+
*/
5+
6+
/*!
7+
\file crc32.c
8+
\brief Software implementation for CRC32
9+
\version 20250929
10+
*/
11+
12+
#include <stddef.h>
13+
#include <stdint.h>
14+
15+
#define CRC32_POLY 0xEDB88320
16+
17+
uint32_t crc32(const char * data)
18+
{
19+
uint32_t crc = 0xFFFFFFFF;
20+
21+
while (*data)
22+
{
23+
crc ^= (uint8_t)(*data++);
24+
for (int i = 0; i < 8; i++)
25+
{
26+
if (crc & 1)
27+
crc = (crc >> 1) ^ CRC32_POLY;
28+
else
29+
crc >>= 1;
30+
}
31+
}
32+
return ~crc;
33+
}

lib/crc32_test.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
Copyright (c) 2020-2025, IAR Systems AB.
3+
SPDX-License-Identifier: MIT
4+
*/
5+
6+
/*!
7+
\file crc32_test.c
8+
\brief CRC32 test module
9+
\version 20250929
10+
*/
11+
12+
#include <errno.h>
13+
#include "acutest.h"
14+
#include "util.h"
15+
16+
void crc32_test()
17+
{
18+
TEST_CASE("The quick brown fox");
19+
char * message = "The quick brown fox jumps over the lazy dog";
20+
int32_t result = crc32(message);
21+
TEST_CHECK(0x414fa339 == result);
22+
}
23+
24+
TEST_LIST= {
25+
{"CRC32", crc32_test},
26+
27+
{NULL, NULL}
28+
};

lib/util.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,7 @@ typedef struct data_s {
1818

1919
const uint8_t* bm_search(const uint8_t *text, const uint8_t *pattern);
2020
uint32_t linear_search(uint32_t x, const data_t *data);
21+
uint16_t crc16(const char * data);
22+
uint32_t crc32(const char * data);
2123

22-
#endif /* __TEST_CONFIG__ */
24+
#endif /* __UTIL_H__ */

0 commit comments

Comments
 (0)