fsl_crc.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * The Clear BSD License
  3. * Copyright (c) 2015-2016, Freescale Semiconductor, Inc.
  4. * Copyright 2016-2017 NXP
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without modification,
  8. * are permitted (subject to the limitations in the disclaimer below) provided
  9. * that the following conditions are met:
  10. *
  11. * o Redistributions of source code must retain the above copyright notice, this list
  12. * of conditions and the following disclaimer.
  13. *
  14. * o Redistributions in binary form must reproduce the above copyright notice, this
  15. * list of conditions and the following disclaimer in the documentation and/or
  16. * other materials provided with the distribution.
  17. *
  18. * o Neither the name of the copyright holder nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  24. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  25. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  26. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  27. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  28. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  29. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  30. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  32. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include "fsl_crc.h"
  35. /*******************************************************************************
  36. * Definitions
  37. ******************************************************************************/
  38. /* Component ID definition, used by tools. */
  39. #ifndef FSL_COMPONENT_ID
  40. #define FSL_COMPONENT_ID "platform.drivers.lpc_crc"
  41. #endif
  42. #if defined(CRC_DRIVER_USE_CRC16_CCITT_FALSE_AS_DEFAULT) && CRC_DRIVER_USE_CRC16_CCITT_FALSE_AS_DEFAULT
  43. /* @brief Default user configuration structure for CRC-CCITT */
  44. #define CRC_DRIVER_DEFAULT_POLYNOMIAL kCRC_Polynomial_CRC_CCITT
  45. /*< CRC-CCIT polynomial x^16 + x^12 + x^5 + x^0 */
  46. #define CRC_DRIVER_DEFAULT_REVERSE_IN false
  47. /*< Default is no bit reverse */
  48. #define CRC_DRIVER_DEFAULT_COMPLEMENT_IN false
  49. /*< Default is without complement of written data */
  50. #define CRC_DRIVER_DEFAULT_REVERSE_OUT false
  51. /*< Default is no bit reverse */
  52. #define CRC_DRIVER_DEFAULT_COMPLEMENT_OUT false
  53. /*< Default is without complement of CRC data register read data */
  54. #define CRC_DRIVER_DEFAULT_SEED 0xFFFFU
  55. /*< Default initial checksum */
  56. #endif /* CRC_DRIVER_USE_CRC16_CCITT_FALSE_AS_DEFAULT */
  57. /*******************************************************************************
  58. * Code
  59. ******************************************************************************/
  60. void CRC_Init(CRC_Type *base, const crc_config_t *config)
  61. {
  62. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  63. /* enable clock to CRC */
  64. CLOCK_EnableClock(kCLOCK_Crc);
  65. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  66. /* configure CRC module and write the seed */
  67. base->MODE = 0 | CRC_MODE_CRC_POLY(config->polynomial) | CRC_MODE_BIT_RVS_WR(config->reverseIn) |
  68. CRC_MODE_CMPL_WR(config->complementIn) | CRC_MODE_BIT_RVS_SUM(config->reverseOut) |
  69. CRC_MODE_CMPL_SUM(config->complementOut);
  70. base->SEED = config->seed;
  71. }
  72. void CRC_GetDefaultConfig(crc_config_t *config)
  73. {
  74. static const crc_config_t default_config = {CRC_DRIVER_DEFAULT_POLYNOMIAL, CRC_DRIVER_DEFAULT_REVERSE_IN,
  75. CRC_DRIVER_DEFAULT_COMPLEMENT_IN, CRC_DRIVER_DEFAULT_REVERSE_OUT,
  76. CRC_DRIVER_DEFAULT_COMPLEMENT_OUT, CRC_DRIVER_DEFAULT_SEED};
  77. *config = default_config;
  78. }
  79. void CRC_Reset(CRC_Type *base)
  80. {
  81. crc_config_t config;
  82. CRC_GetDefaultConfig(&config);
  83. CRC_Init(base, &config);
  84. }
  85. void CRC_GetConfig(CRC_Type *base, crc_config_t *config)
  86. {
  87. /* extract CRC mode settings */
  88. uint32_t mode = base->MODE;
  89. config->polynomial = (crc_polynomial_t)((mode & CRC_MODE_CRC_POLY_MASK) >> CRC_MODE_CRC_POLY_SHIFT);
  90. config->reverseIn = (bool)(mode & CRC_MODE_BIT_RVS_WR_MASK);
  91. config->complementIn = (bool)(mode & CRC_MODE_CMPL_WR_MASK);
  92. config->reverseOut = (bool)(mode & CRC_MODE_BIT_RVS_SUM_MASK);
  93. config->complementOut = (bool)(mode & CRC_MODE_CMPL_SUM_MASK);
  94. /* reset CRC sum bit reverse and 1's complement setting, so its value can be used as a seed */
  95. base->MODE = mode & ~((1U << CRC_MODE_BIT_RVS_SUM_SHIFT) | (1U << CRC_MODE_CMPL_SUM_SHIFT));
  96. /* now we can obtain intermediate raw CRC sum value */
  97. config->seed = base->SUM;
  98. /* restore original CRC sum bit reverse and 1's complement setting */
  99. base->MODE = mode;
  100. }
  101. void CRC_WriteData(CRC_Type *base, const uint8_t *data, size_t dataSize)
  102. {
  103. const uint32_t *data32;
  104. /* 8-bit reads and writes till source address is aligned 4 bytes */
  105. while ((dataSize) && ((uint32_t)data & 3U))
  106. {
  107. *((__O uint8_t *)&(base->WR_DATA)) = *data;
  108. data++;
  109. dataSize--;
  110. }
  111. /* use 32-bit reads and writes as long as possible */
  112. data32 = (const uint32_t *)data;
  113. while (dataSize >= sizeof(uint32_t))
  114. {
  115. *((__O uint32_t *)&(base->WR_DATA)) = *data32;
  116. data32++;
  117. dataSize -= sizeof(uint32_t);
  118. }
  119. data = (const uint8_t *)data32;
  120. /* 8-bit reads and writes till end of data buffer */
  121. while (dataSize)
  122. {
  123. *((__O uint8_t *)&(base->WR_DATA)) = *data;
  124. data++;
  125. dataSize--;
  126. }
  127. }