Skip to content

Commit ccd2a32

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 83a9606 commit ccd2a32

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
@@ -186,8 +186,34 @@ class MbedCRC {
186186
{
187187
}
188188

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

192218
/** Compute CRC for the data input
193219
* Compute CRC performs the initialization, computation and collection of
@@ -842,38 +868,6 @@ const uint32_t MbedCRC<POLY_32BIT_ANSI, 32, CrcMode::TABLE>::_crc_table[MBED_CRC
842868

843869
#endif // !defined(DOXYGEN_ONLY)
844870

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

0 commit comments

Comments
 (0)