Skip to content

Commit a5eb541

Browse files
nzmichaelhAnas Nashif
authored andcommitted
crc: add crc16_itu_t which is used in the MMC protocol.
This is the MSB first version of crc16_ccitt. Use the same name as in Linux. Signed-off-by: Michael Hope <[email protected]>
1 parent f77c1d5 commit a5eb541

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

include/crc16.h

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ u16_t crc16(const u8_t *src, size_t len, u16_t polynomial,
5555
* the same family can be calculated by changing the seed and/or
5656
* XORing the final value. Examples include:
5757
*
58-
* - CCIITT-FALSE: seed=0xffff
5958
* - X-25 (used in PPP): seed=0xffff, xor=0xffff, residual=0xf0b8
6059
*
6160
* @note API changed in Zephyr 1.11.
@@ -68,6 +67,30 @@ u16_t crc16(const u8_t *src, size_t len, u16_t polynomial,
6867
*/
6968
u16_t crc16_ccitt(u16_t seed, const u8_t *src, size_t len);
7069

70+
/**
71+
* @brief Compute the CRC-16/XMODEM checksum of a buffer.
72+
*
73+
* The MSB first version of ITU-T Recommendation V.41 (November 1988).
74+
* Uses 0x1021 as the polynomial with no reflection.
75+
*
76+
* To calculate the CRC across non-contigious blocks use the return
77+
* value from block N-1 as the seed for block N.
78+
*
79+
* For CRC-16/XMODEM, use 0 as the initial seed. Other checksums in
80+
* the same family can be calculated by changing the seed and/or
81+
* XORing the final value. Examples include:
82+
*
83+
* - CCIITT-FALSE: seed=0xffff
84+
* - GSM: seed=0, xorout=0xffff, residue=0x1d0f
85+
*
86+
* @param seed Value to seed the CRC with
87+
* @param src Input bytes for the computation
88+
* @param len Length of the input in bytes
89+
*
90+
* @return The computed CRC16 value
91+
*/
92+
u16_t crc16_itu_t(u16_t seed, const u8_t *src, size_t len);
93+
7194
/**
7295
* @brief Compute ANSI variant of CRC 16
7396
*

lib/crc/crc16_sw.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,16 @@ u16_t crc16_ccitt(u16_t seed, const u8_t *src, size_t len)
4747

4848
return seed;
4949
}
50+
51+
u16_t crc16_itu_t(u16_t seed, const u8_t *src, size_t len)
52+
{
53+
for (; len > 0; len--) {
54+
seed = (seed >> 8) | (seed << 8);
55+
seed ^= *src++;
56+
seed ^= (seed & 0xff) >> 4;
57+
seed ^= seed << 12;
58+
seed ^= (seed & 0xff) << 5;
59+
}
60+
61+
return seed;
62+
}

tests/unit/lib/crc/main.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,14 @@ void test_crc16_ccitt_for_ppp(void)
7575
0x906e, NULL);
7676
}
7777

78+
void test_crc16_itu_t(void)
79+
{
80+
u8_t test2[] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
81+
82+
zassert_equal(crc16_itu_t(0, test2, sizeof(test2)),
83+
0x31c3, NULL);
84+
}
85+
7886
void test_crc8_ccitt(void)
7987
{
8088
u8_t test0[] = { 0 };
@@ -96,6 +104,7 @@ void test_main(void)
96104
ztest_unit_test(test_crc16_ansi),
97105
ztest_unit_test(test_crc16_ccitt),
98106
ztest_unit_test(test_crc16_ccitt_for_ppp),
107+
ztest_unit_test(test_crc16_itu_t),
99108
ztest_unit_test(test_crc8_ccitt));
100109
ztest_run_test_suite(test_crc);
101110
}

0 commit comments

Comments
 (0)