Skip to content

Commit 63af794

Browse files
committed
MbedCRC: improve default constructors
Original default constructor implementation wasn't quite working properly; it didn't cope with the new "mode_limit" parameter. Change mechanism so that this now works: MbedCRC<POLY32_BIT_ANSI_CRC, 32, CrcMode::TABLE> crc;
1 parent bbc7ab6 commit 63af794

File tree

2 files changed

+51
-34
lines changed

2 files changed

+51
-34
lines changed

TESTS/mbed_drivers/crc/main.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,28 @@ void test_partial_crc()
143143
}
144144
}
145145

146+
void test_mode_limit()
147+
{
148+
const char test[] = "123456789";
149+
uint32_t crc;
150+
151+
{
152+
MbedCRC<POLY_32BIT_ANSI, 32, CrcMode::BITWISE> ct;
153+
TEST_ASSERT_EQUAL(0, ct.compute(test, strlen(test), &crc));
154+
TEST_ASSERT_EQUAL(0xCBF43926, crc);
155+
}
156+
{
157+
MbedCRC<POLY_32BIT_ANSI, 32, CrcMode::TABLE> ct;
158+
TEST_ASSERT_EQUAL(0, ct.compute(test, strlen(test), &crc));
159+
TEST_ASSERT_EQUAL(0xCBF43926, crc);
160+
}
161+
{
162+
MbedCRC<POLY_32BIT_ANSI, 32, CrcMode::HARDWARE> ct;
163+
TEST_ASSERT_EQUAL(0, ct.compute(test, strlen(test), &crc));
164+
TEST_ASSERT_EQUAL(0xCBF43926, crc);
165+
}
166+
}
167+
146168
void test_sd_crc()
147169
{
148170
MbedCRC<POLY_7BIT_SD, 7> crc7;
@@ -232,6 +254,7 @@ void test_thread_safety()
232254
Case cases[] = {
233255
Case("Test supported polynomials", test_supported_polynomials),
234256
Case("Test partial CRC", test_partial_crc),
257+
Case("Test mode-limited CRC", test_mode_limit),
235258
Case("Test SD CRC polynomials", test_sd_crc),
236259
#if defined(MBED_CONF_RTOS_PRESENT)
237260
Case("Test thread safety", test_thread_safety),

drivers/MbedCRC.h

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,34 @@ class MbedCRC {
181181
{
182182
}
183183

184-
MSTD_CONSTEXPR_FN_14
185-
MbedCRC();
184+
/* Default values for different types of polynomials
185+
*/
186+
// *INDENT-OFF*
187+
template<uint32_t poly = polynomial, mstd::enable_if_t<poly == POLY_32BIT_ANSI && width == 32, int> = 0>
188+
MSTD_CONSTEXPR_FN_14 MbedCRC() : MbedCRC(0xFFFFFFFF, 0xFFFFFFFF, true, true)
189+
{
190+
}
191+
192+
template<uint32_t poly = polynomial, mstd::enable_if_t<poly == POLY_16BIT_IBM && width == 16, int> = 0>
193+
MSTD_CONSTEXPR_FN_14 MbedCRC() : MbedCRC(0, 0, true, true)
194+
{
195+
}
196+
197+
template<uint32_t poly = polynomial, mstd::enable_if_t<poly == POLY_16BIT_CCITT && width == 16, int> = 0>
198+
MSTD_CONSTEXPR_FN_14 MbedCRC() : MbedCRC(0xFFFF, 0, false, false)
199+
{
200+
}
201+
202+
template<uint32_t poly = polynomial, mstd::enable_if_t<poly == POLY_7BIT_SD && width == 7, int> = 0>
203+
MSTD_CONSTEXPR_FN_14 MbedCRC() : MbedCRC(0, 0, false, false)
204+
{
205+
}
206+
207+
template<uint32_t poly = polynomial, mstd::enable_if_t<poly == POLY_8BIT_CCITT && width == 8, int> = 0>
208+
MSTD_CONSTEXPR_FN_14 MbedCRC() : MbedCRC(0, 0, false, false)
209+
{
210+
}
211+
// *INDENT-ON*
186212

187213
/** Compute CRC for the data input
188214
* Compute CRC performs the initialization, computation and collection of
@@ -837,38 +863,6 @@ const uint32_t MbedCRC<POLY_32BIT_ANSI, 32, CrcMode::TABLE>::_crc_table[MBED_CRC
837863

838864
#endif // !defined(DOXYGEN_ONLY)
839865

840-
/* Default values for different types of polynomials
841-
*/
842-
template<>
843-
inline MSTD_CONSTEXPR_FN_14
844-
MbedCRC<POLY_32BIT_ANSI, 32>::MbedCRC() : MbedCRC(0xFFFFFFFF, 0xFFFFFFFF, true, true)
845-
{
846-
}
847-
848-
template<>
849-
inline MSTD_CONSTEXPR_FN_14
850-
MbedCRC<POLY_16BIT_IBM, 16>::MbedCRC() : MbedCRC(0, 0, true, true)
851-
{
852-
}
853-
854-
template<>
855-
inline MSTD_CONSTEXPR_FN_14
856-
MbedCRC<POLY_16BIT_CCITT, 16>::MbedCRC() : MbedCRC(0xFFFF, 0, false, false)
857-
{
858-
}
859-
860-
template<>
861-
inline MSTD_CONSTEXPR_FN_14
862-
MbedCRC<POLY_7BIT_SD, 7>::MbedCRC(): MbedCRC(0, 0, false, false)
863-
{
864-
}
865-
866-
template<>
867-
inline MSTD_CONSTEXPR_FN_14
868-
MbedCRC<POLY_8BIT_CCITT, 8>::MbedCRC(): MbedCRC(0, 0, false, false)
869-
{
870-
}
871-
872866
/** @}*/
873867
/** @}*/
874868

0 commit comments

Comments
 (0)