n32g45x_crc.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*****************************************************************************
  2. * Copyright (c) 2019, Nations Technologies Inc.
  3. *
  4. * All rights reserved.
  5. * ****************************************************************************
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. *
  10. * - Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the disclaimer below.
  12. *
  13. * Nations' name may not be used to endorse or promote products derived from
  14. * this software without specific prior written permission.
  15. *
  16. * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  19. * DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  22. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  25. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. * ****************************************************************************/
  27. /**
  28. * @file n32g45x_crc.c
  29. * @author Nations
  30. * @version v1.0.0
  31. *
  32. * @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
  33. */
  34. #include "n32g45x_crc.h"
  35. /** @addtogroup N32G45X_StdPeriph_Driver
  36. * @{
  37. */
  38. /** @addtogroup CRC
  39. * @brief CRC driver modules
  40. * @{
  41. */
  42. /** @addtogroup CRC_Private_TypesDefinitions
  43. * @{
  44. */
  45. /**
  46. * @}
  47. */
  48. /** @addtogroup CRC_Private_Defines
  49. * @{
  50. */
  51. /**
  52. * @}
  53. */
  54. /** @addtogroup CRC_Private_Macros
  55. * @{
  56. */
  57. /**
  58. * @}
  59. */
  60. /** @addtogroup CRC_Private_Variables
  61. * @{
  62. */
  63. /**
  64. * @}
  65. */
  66. /** @addtogroup CRC_Private_FunctionPrototypes
  67. * @{
  68. */
  69. /**
  70. * @}
  71. */
  72. /** @addtogroup CRC_Private_Functions
  73. * @{
  74. */
  75. /**
  76. * @brief Resets the CRC Data register (DAT).
  77. */
  78. void CRC32_ResetCrc(void)
  79. {
  80. /* Reset CRC generator */
  81. CRC->CRC32CTRL = CRC32_CTRL_RESET;
  82. }
  83. /**
  84. * @brief Computes the 32-bit CRC of a given data word(32-bit).
  85. * @param Data data word(32-bit) to compute its CRC
  86. * @return 32-bit CRC
  87. */
  88. uint32_t CRC32_CalcCrc(uint32_t Data)
  89. {
  90. CRC->CRC32DAT = Data;
  91. return (CRC->CRC32DAT);
  92. }
  93. /**
  94. * @brief Computes the 32-bit CRC of a given buffer of data word(32-bit).
  95. * @param pBuffer pointer to the buffer containing the data to be computed
  96. * @param BufferLength length of the buffer to be computed
  97. * @return 32-bit CRC
  98. */
  99. uint32_t CRC32_CalcBufCrc(uint32_t pBuffer[], uint32_t BufferLength)
  100. {
  101. uint32_t index = 0;
  102. for (index = 0; index < BufferLength; index++)
  103. {
  104. CRC->CRC32DAT = pBuffer[index];
  105. }
  106. return (CRC->CRC32DAT);
  107. }
  108. /**
  109. * @brief Returns the current CRC value.
  110. * @return 32-bit CRC
  111. */
  112. uint32_t CRC32_GetCrc(void)
  113. {
  114. return (CRC->CRC32DAT);
  115. }
  116. /**
  117. * @brief Stores a 8-bit data in the Independent Data(ID) register.
  118. * @param IDValue 8-bit value to be stored in the ID register
  119. */
  120. void CRC32_SetIDat(uint8_t IDValue)
  121. {
  122. CRC->CRC32IDAT = IDValue;
  123. }
  124. /**
  125. * @brief Returns the 8-bit data stored in the Independent Data(ID) register
  126. * @return 8-bit value of the ID register
  127. */
  128. uint8_t CRC32_GetIDat(void)
  129. {
  130. return (CRC->CRC32IDAT);
  131. }
  132. // CRC16 add
  133. void __CRC16_SetLittleEndianFmt(void)
  134. {
  135. CRC->CRC16CTRL = CRC16_CTRL_LITTLE | CRC->CRC16CTRL;
  136. }
  137. void __CRC16_SetBigEndianFmt(void)
  138. {
  139. CRC->CRC16CTRL = CRC16_CTRL_BIG & CRC->CRC16CTRL;
  140. }
  141. void __CRC16_SetCleanEnable(void)
  142. {
  143. CRC->CRC16CTRL = CRC16_CTRL_RESET | CRC->CRC16CTRL;
  144. }
  145. void __CRC16_SetCleanDisable(void)
  146. {
  147. CRC->CRC16CTRL = CRC16_CTRL_NO_RESET & CRC->CRC16CTRL;
  148. }
  149. uint16_t __CRC16_CalcCrc(uint8_t Data)
  150. {
  151. CRC->CRC16DAT = Data;
  152. return (CRC->CRC16D);
  153. }
  154. void __CRC16_SetCrc(uint8_t Data)
  155. {
  156. CRC->CRC16DAT = Data;
  157. }
  158. uint16_t __CRC16_GetCrc(void)
  159. {
  160. return (CRC->CRC16D);
  161. }
  162. void __CRC16_SetLRC(uint8_t Data)
  163. {
  164. CRC->LRC = Data;
  165. }
  166. uint8_t __CRC16_GetLRC(void)
  167. {
  168. return (CRC->LRC);
  169. }
  170. uint16_t CRC16_CalcBufCrc(uint8_t pBuffer[], uint32_t BufferLength)
  171. {
  172. uint32_t index = 0;
  173. CRC->CRC16D = 0x00;
  174. // CRC16_SetCleanEnable();
  175. for (index = 0; index < BufferLength; index++)
  176. {
  177. CRC->CRC16DAT = pBuffer[index];
  178. }
  179. return (CRC->CRC16D);
  180. }
  181. uint16_t CRC16_CalcCRC(uint8_t Data)
  182. {
  183. CRC->CRC16DAT = Data;
  184. return (CRC->CRC16D);
  185. }
  186. /**
  187. * @}
  188. */
  189. /**
  190. * @}
  191. */
  192. /**
  193. * @}
  194. */