MbedCRC.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. /* mbed Microcontroller Library
  2. * Copyright (c) 2018 ARM Limited
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef MBED_CRC_API_H
  17. #define MBED_CRC_API_H
  18. #include "drivers/TableCRC.h"
  19. #include "hal/crc_api.h"
  20. #include "platform/mbed_assert.h"
  21. /* This is invalid warning from the compiler for below section of code
  22. if ((width < 8) && (NULL == _crc_table)) {
  23. p_crc = (uint32_t)(p_crc << (8 - width));
  24. }
  25. Compiler warns of the shift operation with width as it is width=(std::uint8_t),
  26. but we check for ( width < 8) before performing shift, so it should not be an issue.
  27. */
  28. #if defined ( __CC_ARM )
  29. #pragma diag_suppress 62 // Shift count is negative
  30. #elif defined ( __GNUC__ )
  31. #pragma GCC diagnostic push
  32. #pragma GCC diagnostic ignored "-Wshift-count-negative"
  33. #elif defined (__ICCARM__)
  34. #pragma diag_suppress=Pe062 // Shift count is negative
  35. #endif
  36. namespace mbed {
  37. /** \addtogroup drivers */
  38. /** @{*/
  39. /** CRC object provides CRC generation through hardware/software
  40. *
  41. * ROM polynomial tables for supported polynomials (:: crc_polynomial_t) will be used for
  42. * software CRC computation, if ROM tables are not available then CRC is computed runtime
  43. * bit by bit for all data input.
  44. *
  45. * @tparam polynomial CRC polynomial value in hex
  46. * @tparam width CRC polynomial width
  47. *
  48. * Example: Compute CRC data
  49. * @code
  50. *
  51. * #include "mbed.h"
  52. *
  53. * int main() {
  54. * MbedCRC<POLY_32BIT_ANSI, 32> ct;
  55. *
  56. * char test[] = "123456789";
  57. * uint32_t crc = 0;
  58. *
  59. * printf("\nPolynomial = 0x%lx Width = %d \n", ct.get_polynomial(), ct.get_width());
  60. *
  61. * ct.compute((void *)test, strlen((const char*)test), &crc);
  62. *
  63. * printf("The CRC of data \"123456789\" is : 0x%lx\n", crc);
  64. * return 0;
  65. * }
  66. * @endcode
  67. * Example: Compute CRC with data available in parts
  68. * @code
  69. *
  70. * #include "mbed.h"
  71. * int main() {
  72. * MbedCRC<POLY_32BIT_ANSI, 32> ct;
  73. *
  74. * char test[] = "123456789";
  75. * uint32_t crc = 0;
  76. *
  77. * printf("\nPolynomial = 0x%lx Width = %d \n", ct.get_polynomial(), ct.get_width());
  78. *
  79. * ct.compute_partial_start(&crc);
  80. * ct.compute_partial((void *)&test, 4, &crc);
  81. * ct.compute_partial((void *)&test[4], 5, &crc);
  82. * ct.compute_partial_stop(&crc);
  83. *
  84. * printf("The CRC of data \"123456789\" is : 0x%lx\n", crc);
  85. * return 0;
  86. * }
  87. * @endcode
  88. * @ingroup drivers
  89. */
  90. template <uint32_t polynomial = POLY_32BIT_ANSI, uint8_t width = 32>
  91. class MbedCRC {
  92. public:
  93. enum CrcMode { HARDWARE = 0, TABLE, BITWISE };
  94. public:
  95. typedef uint64_t crc_data_size_t;
  96. /** Lifetime of CRC object
  97. *
  98. * @param initial_xor Inital value/seed to Xor
  99. * @param final_xor Final Xor value
  100. * @param reflect_data
  101. * @param reflect_remainder
  102. * @note Default constructor without any arguments is valid only for supported CRC polynomials. :: crc_polynomial_t
  103. * MbedCRC <POLY_7BIT_SD, 7> ct; --- Valid POLY_7BIT_SD
  104. * MbedCRC <0x1021, 16> ct; --- Valid POLY_16BIT_CCITT
  105. * MbedCRC <POLY_16BIT_CCITT, 32> ct; --- Invalid, compilation error
  106. * MbedCRC <POLY_16BIT_CCITT, 32> ct (i,f,rd,rr) Consturctor can be used for not supported polynomials
  107. * MbedCRC<POLY_16BIT_CCITT, 16> sd(0, 0, false, false); Constructor can also be used for supported
  108. * polynomials with different intial/final/reflect values
  109. *
  110. */
  111. MbedCRC(uint32_t initial_xor, uint32_t final_xor, bool reflect_data, bool reflect_remainder) :
  112. _initial_value(initial_xor), _final_xor(final_xor), _reflect_data(reflect_data),
  113. _reflect_remainder(reflect_remainder), _crc_table(NULL)
  114. {
  115. mbed_crc_ctor();
  116. }
  117. MbedCRC();
  118. virtual ~MbedCRC()
  119. {
  120. // Do nothing
  121. }
  122. /** Compute CRC for the data input
  123. *
  124. * @param buffer Data bytes
  125. * @param size Size of data
  126. * @param crc CRC is the output value
  127. * @return 0 on success, negative error code on failure
  128. */
  129. int32_t compute(void *buffer, crc_data_size_t size, uint32_t *crc)
  130. {
  131. MBED_ASSERT(crc != NULL);
  132. int32_t status;
  133. if (0 != (status = compute_partial_start(crc))) {
  134. *crc = 0;
  135. return status;
  136. }
  137. if (0 != (status = compute_partial(buffer, size, crc))) {
  138. *crc = 0;
  139. return status;
  140. }
  141. if (0 != (status = compute_partial_stop(crc))) {
  142. *crc = 0;
  143. return status;
  144. }
  145. return 0;
  146. }
  147. /** Compute partial CRC for the data input.
  148. *
  149. * CRC data if not available fully, CRC can be computed in parts with available data.
  150. * Previous CRC output should be passed as argument to the current compute_partial call.
  151. * @pre: Call \ref compute_partial_start to start the partial CRC calculation.
  152. * @post: Call \ref compute_partial_stop to get the final CRC value.
  153. *
  154. * @param buffer Data bytes
  155. * @param size Size of data
  156. * @param crc CRC value is intermediate CRC value filled by API.
  157. * @return 0 on success or a negative error code on failure
  158. * @note: CRC as output in compute_partial is not final CRC value, call @ref compute_partial_stop
  159. * to get final correct CRC value.
  160. */
  161. int32_t compute_partial(void *buffer, crc_data_size_t size, uint32_t *crc)
  162. {
  163. switch (_mode) {
  164. case HARDWARE:
  165. #ifdef DEVICE_CRC
  166. hal_crc_compute_partial((uint8_t *)buffer, size);
  167. #endif // DEVICE_CRC
  168. *crc = 0;
  169. return 0;
  170. case TABLE:
  171. return table_compute_partial(buffer, size, crc);
  172. case BITWISE:
  173. return bitwise_compute_partial(buffer, size, crc);
  174. }
  175. return -1;
  176. }
  177. /** Compute partial start, indicate start of partial computation
  178. *
  179. * This API should be called before performing any partial computation
  180. * with compute_partial API.
  181. *
  182. * @param crc Initial CRC value set by the API
  183. * @return 0 on success or a negative in case of failure
  184. * @note: CRC is an out parameter and must be reused with compute_partial
  185. * and compute_partial_stop without any modifications in application.
  186. */
  187. int32_t compute_partial_start(uint32_t *crc)
  188. {
  189. MBED_ASSERT(crc != NULL);
  190. #ifdef DEVICE_CRC
  191. if (_mode == HARDWARE) {
  192. crc_mbed_config_t config;
  193. config.polynomial = polynomial;
  194. config.width = width;
  195. config.initial_xor = _initial_value;
  196. config.final_xor = _final_xor;
  197. config.reflect_in = _reflect_data;
  198. config.reflect_out = _reflect_remainder;
  199. hal_crc_compute_partial_start(&config);
  200. }
  201. #endif // DEVICE_CRC
  202. *crc = _initial_value;
  203. return 0;
  204. }
  205. /** Get the final CRC value of partial computation.
  206. *
  207. * CRC value available in partial computation is not correct CRC, as some
  208. * algorithms require remainder to be reflected and final value to be XORed
  209. * This API is used to perform final computation to get correct CRC value.
  210. *
  211. * @param crc CRC result
  212. */
  213. int32_t compute_partial_stop(uint32_t *crc)
  214. {
  215. MBED_ASSERT(crc != NULL);
  216. if (_mode == HARDWARE) {
  217. #ifdef DEVICE_CRC
  218. *crc = hal_crc_get_result();
  219. return 0;
  220. #else
  221. return -1;
  222. #endif
  223. }
  224. uint32_t p_crc = *crc;
  225. if ((width < 8) && (NULL == _crc_table)) {
  226. p_crc = (uint32_t)(p_crc << (8 - width));
  227. }
  228. *crc = (reflect_remainder(p_crc) ^ _final_xor) & get_crc_mask();
  229. return 0;
  230. }
  231. /** Get the current CRC polynomial
  232. *
  233. * @return Polynomial value
  234. */
  235. uint32_t get_polynomial(void) const
  236. {
  237. return polynomial;
  238. }
  239. /** Get the current CRC width
  240. *
  241. * @return CRC width
  242. */
  243. uint8_t get_width(void) const
  244. {
  245. return width;
  246. }
  247. private:
  248. uint32_t _initial_value;
  249. uint32_t _final_xor;
  250. bool _reflect_data;
  251. bool _reflect_remainder;
  252. uint32_t *_crc_table;
  253. CrcMode _mode;
  254. /** Get the current CRC data size
  255. *
  256. * @return CRC data size in bytes
  257. */
  258. uint8_t get_data_size(void) const
  259. {
  260. return (width <= 8 ? 1 : (width <= 16 ? 2 : 4));
  261. }
  262. /** Get the top bit of current CRC
  263. *
  264. * @return Top bit is set high for respective data width of current CRC
  265. * Top bit for CRC width less then 8 bits will be set as 8th bit.
  266. */
  267. uint32_t get_top_bit(void) const
  268. {
  269. return (width < 8 ? (1u << 7) : (uint32_t)(1ul << (width - 1)));
  270. }
  271. /** Get the CRC data mask
  272. *
  273. * @return CRC data mask is generated based on current CRC width
  274. */
  275. uint32_t get_crc_mask(void) const
  276. {
  277. return (width < 8 ? ((1u << 8) - 1) : (uint32_t)((uint64_t)(1ull << width) - 1));
  278. }
  279. /** Final value of CRC is reflected
  280. *
  281. * @param data final crc value, which should be reflected
  282. * @return Reflected CRC value
  283. */
  284. uint32_t reflect_remainder(uint32_t data) const
  285. {
  286. if (_reflect_remainder) {
  287. uint32_t reflection = 0x0;
  288. uint8_t const nBits = (width < 8 ? 8 : width);
  289. for (uint8_t bit = 0; bit < nBits; ++bit) {
  290. if (data & 0x01) {
  291. reflection |= (1 << ((nBits - 1) - bit));
  292. }
  293. data = (data >> 1);
  294. }
  295. return (reflection);
  296. } else {
  297. return data;
  298. }
  299. }
  300. /** Data bytes are reflected
  301. *
  302. * @param data value to be reflected
  303. * @return Reflected data value
  304. */
  305. uint32_t reflect_bytes(uint32_t data) const
  306. {
  307. if (_reflect_data) {
  308. uint32_t reflection = 0x0;
  309. for (uint8_t bit = 0; bit < 8; ++bit) {
  310. if (data & 0x01) {
  311. reflection |= (1 << (7 - bit));
  312. }
  313. data = (data >> 1);
  314. }
  315. return (reflection);
  316. } else {
  317. return data;
  318. }
  319. }
  320. /** Bitwise CRC computation
  321. *
  322. * @param buffer data buffer
  323. * @param size size of the data
  324. * @param crc CRC value is filled in, but the value is not the final
  325. * @return 0 on success or a negative error code on failure
  326. */
  327. int32_t bitwise_compute_partial(const void *buffer, crc_data_size_t size, uint32_t *crc) const
  328. {
  329. MBED_ASSERT(crc != NULL);
  330. MBED_ASSERT(buffer != NULL);
  331. const uint8_t *data = static_cast<const uint8_t *>(buffer);
  332. uint32_t p_crc = *crc;
  333. if (width < 8) {
  334. uint8_t data_byte;
  335. for (crc_data_size_t byte = 0; byte < size; byte++) {
  336. data_byte = reflect_bytes(data[byte]);
  337. for (uint8_t bit = 8; bit > 0; --bit) {
  338. p_crc <<= 1;
  339. if ((data_byte ^ p_crc) & get_top_bit()) {
  340. p_crc ^= polynomial;
  341. }
  342. data_byte <<= 1;
  343. }
  344. }
  345. } else {
  346. for (crc_data_size_t byte = 0; byte < size; byte++) {
  347. p_crc ^= (reflect_bytes(data[byte]) << (width - 8));
  348. // Perform modulo-2 division, a bit at a time
  349. for (uint8_t bit = 8; bit > 0; --bit) {
  350. if (p_crc & get_top_bit()) {
  351. p_crc = (p_crc << 1) ^ polynomial;
  352. } else {
  353. p_crc = (p_crc << 1);
  354. }
  355. }
  356. }
  357. }
  358. *crc = p_crc & get_crc_mask();
  359. return 0;
  360. }
  361. /** CRC computation using ROM tables
  362. *
  363. * @param buffer data buffer
  364. * @param size size of the data
  365. * @param crc CRC value is filled in, but the value is not the final
  366. * @return 0 on success or a negative error code on failure
  367. */
  368. int32_t table_compute_partial(const void *buffer, crc_data_size_t size, uint32_t *crc) const
  369. {
  370. MBED_ASSERT(crc != NULL);
  371. MBED_ASSERT(buffer != NULL);
  372. const uint8_t *data = static_cast<const uint8_t *>(buffer);
  373. uint32_t p_crc = *crc;
  374. uint8_t data_byte = 0;
  375. if (width <= 8) {
  376. uint8_t *crc_table = (uint8_t *)_crc_table;
  377. for (crc_data_size_t byte = 0; byte < size; byte++) {
  378. data_byte = reflect_bytes(data[byte]) ^ p_crc;
  379. p_crc = crc_table[data_byte];
  380. }
  381. } else if (width <= 16) {
  382. uint16_t *crc_table = (uint16_t *)_crc_table;
  383. for (crc_data_size_t byte = 0; byte < size; byte++) {
  384. data_byte = reflect_bytes(data[byte]) ^ (p_crc >> (width - 8));
  385. p_crc = crc_table[data_byte] ^ (p_crc << 8);
  386. }
  387. } else {
  388. uint32_t *crc_table = (uint32_t *)_crc_table;
  389. for (crc_data_size_t byte = 0; byte < size; byte++) {
  390. data_byte = reflect_bytes(data[byte]) ^ (p_crc >> (width - 8));
  391. p_crc = crc_table[data_byte] ^ (p_crc << 8);
  392. }
  393. }
  394. *crc = p_crc & get_crc_mask();
  395. return 0;
  396. }
  397. /** Constructor init called from all specialized cases of constructor
  398. * Note: All construtor common code should be in this function.
  399. */
  400. void mbed_crc_ctor(void)
  401. {
  402. MBED_STATIC_ASSERT(width <= 32, "Max 32-bit CRC supported");
  403. _mode = (_crc_table != NULL) ? TABLE : BITWISE;
  404. #ifdef DEVICE_CRC
  405. crc_mbed_config_t config;
  406. config.polynomial = polynomial;
  407. config.width = width;
  408. config.initial_xor = _initial_value;
  409. config.final_xor = _final_xor;
  410. config.reflect_in = _reflect_data;
  411. config.reflect_out = _reflect_remainder;
  412. if (hal_crc_is_supported(&config)) {
  413. _mode = HARDWARE;
  414. }
  415. #endif
  416. }
  417. };
  418. #if defined ( __CC_ARM )
  419. #elif defined ( __GNUC__ )
  420. #pragma GCC diagnostic pop
  421. #elif defined (__ICCARM__)
  422. #endif
  423. /** @}*/
  424. } // namespace mbed
  425. #endif