crc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. //*****************************************************************************
  2. //
  3. // crc.c - Driver for the CRC module.
  4. //
  5. // Copyright (c) 2012-2020 Texas Instruments Incorporated. All rights reserved.
  6. // Software License Agreement
  7. //
  8. // Redistribution and use in source and binary forms, with or without
  9. // modification, are permitted provided that the following conditions
  10. // are met:
  11. //
  12. // Redistributions of source code must retain the above copyright
  13. // notice, this list of conditions and the following disclaimer.
  14. //
  15. // Redistributions in binary form must reproduce the above copyright
  16. // notice, this list of conditions and the following disclaimer in the
  17. // documentation and/or other materials provided with the
  18. // distribution.
  19. //
  20. // Neither the name of Texas Instruments Incorporated nor the names of
  21. // its contributors may be used to endorse or promote products derived
  22. // from this software without specific prior written permission.
  23. //
  24. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  27. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  28. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  29. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  30. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  31. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  32. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  33. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. //
  36. // This is part of revision 2.2.0.295 of the Tiva Peripheral Driver Library.
  37. //
  38. //*****************************************************************************
  39. //*****************************************************************************
  40. //
  41. //! \addtogroup crc_api
  42. //! @{
  43. //
  44. //*****************************************************************************
  45. #include <stdbool.h>
  46. #include <stdint.h>
  47. #include "inc/hw_ccm.h"
  48. #include "inc/hw_memmap.h"
  49. #include "inc/hw_types.h"
  50. #include "driverlib/crc.h"
  51. #include "driverlib/debug.h"
  52. //*****************************************************************************
  53. //
  54. //! Set the configuration of CRC functionality with the EC module.
  55. //!
  56. //! \param ui32Base is the base address of the EC module.
  57. //! \param ui32CRCConfig is the configuration of the CRC engine.
  58. //!
  59. //! This function configures the operation of the CRC engine within the EC
  60. //! module. The configuration is specified with the \e ui32CRCConfig argument.
  61. //! It is the logical OR of any of the following options:
  62. //!
  63. //! CRC Initialization Value
  64. //! - \b CRC_CFG_INIT_SEED - Initialize with seed value
  65. //! - \b CRC_CFG_INIT_0 - Initialize to all '0s'
  66. //! - \b CRC_CFG_INIT_1 - Initialize to all '1s'
  67. //!
  68. //! Input Data Size
  69. //! - \b CRC_CFG_SIZE_8BIT - Input data size of 8 bits
  70. //! - \b CRC_CFG_SIZE_32BIT - Input data size of 32 bits
  71. //!
  72. //! Post Process Reverse/Inverse
  73. //! - \b CRC_CFG_RESINV - Result inverse enable
  74. //! - \b CRC_CFG_OBR - Output reverse enable
  75. //!
  76. //! Input Bit Reverse
  77. //! - \b CRC_CFG_IBR - Bit reverse enable
  78. //!
  79. //! Endian Control
  80. //! - \b CRC_CFG_ENDIAN_SBHW - Swap byte in half-word
  81. //! - \b CRC_CFG_ENDIAN_SHW - Swap half-word
  82. //!
  83. //! Operation Type
  84. //! - \b CRC_CFG_TYPE_P8005 - Polynomial 0x8005
  85. //! - \b CRC_CFG_TYPE_P1021 - Polynomial 0x1021
  86. //! - \b CRC_CFG_TYPE_P4C11DB7 - Polynomial 0x4C11DB7
  87. //! - \b CRC_CFG_TYPE_P1EDC6F41 - Polynomial 0x1EDC6F41
  88. //! - \b CRC_CFG_TYPE_TCPCHKSUM - TCP checksum
  89. //!
  90. //! \return None.
  91. //
  92. //*****************************************************************************
  93. void
  94. CRCConfigSet(uint32_t ui32Base, uint32_t ui32CRCConfig)
  95. {
  96. //
  97. // Check the arguments.
  98. //
  99. ASSERT(ui32Base == CCM0_BASE);
  100. ASSERT((ui32CRCConfig & CRC_CFG_INIT_SEED) ||
  101. (ui32CRCConfig & CRC_CFG_INIT_0) ||
  102. (ui32CRCConfig & CRC_CFG_INIT_1) ||
  103. (ui32CRCConfig & CRC_CFG_SIZE_8BIT) ||
  104. (ui32CRCConfig & CRC_CFG_SIZE_32BIT) ||
  105. (ui32CRCConfig & CRC_CFG_RESINV) ||
  106. (ui32CRCConfig & CRC_CFG_OBR) ||
  107. (ui32CRCConfig & CRC_CFG_IBR) ||
  108. (ui32CRCConfig & CRC_CFG_ENDIAN_SBHW) ||
  109. (ui32CRCConfig & CRC_CFG_ENDIAN_SHW) ||
  110. (ui32CRCConfig & CRC_CFG_TYPE_P8005) ||
  111. (ui32CRCConfig & CRC_CFG_TYPE_P1021) ||
  112. (ui32CRCConfig & CRC_CFG_TYPE_P4C11DB7) ||
  113. (ui32CRCConfig & CRC_CFG_TYPE_P1EDC6F41) ||
  114. (ui32CRCConfig & CRC_CFG_TYPE_TCPCHKSUM));
  115. //
  116. // Write the control register with the configuration.
  117. //
  118. HWREG(ui32Base + CCM_O_CRCCTRL) = ui32CRCConfig;
  119. }
  120. //*****************************************************************************
  121. //
  122. //! Write the seed value for CRC operations in the EC module.
  123. //!
  124. //! \param ui32Base is the base address of the EC module.
  125. //! \param ui32Seed is the seed value.
  126. //!
  127. //! This function writes the seed value for use with CRC operations in the
  128. //! EC module. This value is the start value for CRC operations. If this
  129. //! value is not written, then the residual seed from the previous operation
  130. //! is used as the starting value.
  131. //!
  132. //! \note The seed must be written only if \b CRC_CFG_INIT_SEED is
  133. //! set with the CRCConfigSet() function.
  134. //
  135. //*****************************************************************************
  136. void
  137. CRCSeedSet(uint32_t ui32Base, uint32_t ui32Seed)
  138. {
  139. //
  140. // Check the arguments.
  141. //
  142. ASSERT(ui32Base == CCM0_BASE);
  143. //
  144. // Write the seed value to the seed register.
  145. //
  146. HWREG(ui32Base + CCM_O_CRCSEED) = ui32Seed;
  147. }
  148. //*****************************************************************************
  149. //
  150. //! Write data into the EC module for CRC operations.
  151. //!
  152. //! \param ui32Base is the base address of the EC module.
  153. //! \param ui32Data is the data to be written.
  154. //!
  155. //! This function writes either 8 or 32 bits of data into the EC module for
  156. //! CRC operations. The distinction between 8 and 32 bits of data is made
  157. //! when the \b CRC_CFG_SIZE_8BIT or \b CRC_CFG_SIZE_32BIT flag
  158. //! is set using the CRCConfigSet() function.
  159. //!
  160. //! When writing 8 bits of data, ensure the data is in the least significant
  161. //! byte position. The remaining bytes should be written with zero. For
  162. //! example, when writing 0xAB, \e ui32Data should be 0x000000AB.
  163. //!
  164. //! \return None
  165. //
  166. //*****************************************************************************
  167. void
  168. CRCDataWrite(uint32_t ui32Base, uint32_t ui32Data)
  169. {
  170. //
  171. // Check the arguments.
  172. //
  173. ASSERT(ui32Base == CCM0_BASE);
  174. //
  175. // Write the data
  176. //
  177. HWREG(ui32Base + CCM_O_CRCDIN) = ui32Data;
  178. }
  179. //*****************************************************************************
  180. //
  181. //! Reads the result of a CRC operation in the EC module.
  182. //!
  183. //! \param ui32Base is the base address of the EC module.
  184. //! \param bPPResult is \b true to read the post-processed result, or \b false
  185. //! to read the unmodified result.
  186. //!
  187. //! This function reads either the unmodified CRC result or the post
  188. //! processed CRC result from the EC module. The post-processing options
  189. //! are selectable through \b CRC_CFG_RESINV and \b CRC_CFG_OBR
  190. //! parameters in the CRCConfigSet() function.
  191. //!
  192. //! \return The CRC result.
  193. //
  194. //*****************************************************************************
  195. uint32_t
  196. CRCResultRead(uint32_t ui32Base, bool bPPResult)
  197. {
  198. //
  199. // Check the arguments.
  200. //
  201. ASSERT(ui32Base == CCM0_BASE);
  202. //
  203. // Depending on the value of bPPResult, read the appropriate register and
  204. // return value.
  205. //
  206. if(bPPResult)
  207. {
  208. return(HWREG(ui32Base + CCM_O_CRCRSLTPP));
  209. }
  210. else
  211. {
  212. return(HWREG(ui32Base + CCM_O_CRCSEED));
  213. }
  214. }
  215. //*****************************************************************************
  216. //
  217. //! Process data to generate a CRC with the EC module.
  218. //!
  219. //! \param ui32Base is the base address of the EC module.
  220. //! \param pui32DataIn is a pointer to an array of data that is processed.
  221. //! \param ui32DataLength is the number of data items that are processed
  222. //! to produce the CRC.
  223. //! \param bPPResult is \b true to read the post-processed result, or \b false
  224. //! to read the unmodified result.
  225. //!
  226. //! This function processes an array of data to produce a CRC result.
  227. //!
  228. //! The data in the array pointed to be \e pui32DataIn is either an array
  229. //! of bytes or an array or words depending on the selection of the input
  230. //! data size options \b CRC_CFG_SIZE_8BIT and
  231. //! \b CRC_CFG_SIZE_32BIT.
  232. //!
  233. //! This function returns either the unmodified CRC result or the
  234. //! post- processed CRC result from the EC module. The post-processing
  235. //! options are selectable through \b CRC_CFG_RESINV and
  236. //! \b CRC_CFG_OBR parameters.
  237. //!
  238. //! \return The CRC result.
  239. //
  240. //*****************************************************************************
  241. uint32_t
  242. CRCDataProcess(uint32_t ui32Base, uint32_t *pui32DataIn,
  243. uint32_t ui32DataLength, bool bPPResult)
  244. {
  245. uint8_t *pui8DataIn;
  246. //
  247. // Check the arguments.
  248. //
  249. ASSERT(ui32Base == CCM0_BASE);
  250. //
  251. // See if the CRC is operating in 8-bit or 32-bit mode.
  252. //
  253. if(HWREG(ui32Base + CCM_O_CRCCTRL) & CCM_CRCCTRL_SIZE)
  254. {
  255. //
  256. // The CRC is operating in 8-bit mode, so create an 8-bit pointer to
  257. // the data.
  258. //
  259. pui8DataIn = (uint8_t *)pui32DataIn;
  260. //
  261. // Loop through the input data.
  262. //
  263. while(ui32DataLength--)
  264. {
  265. //
  266. // Write the next data byte.
  267. //
  268. HWREG(ui32Base + CCM_O_CRCDIN) = *pui8DataIn++;
  269. }
  270. }
  271. else
  272. {
  273. //
  274. // The CRC is operating in 32-bit mode, so loop through the input data.
  275. //
  276. while(ui32DataLength--)
  277. {
  278. //
  279. // Write the next data word.
  280. //
  281. HWREG(ui32Base + CCM_O_CRCDIN) = *pui32DataIn++;
  282. }
  283. }
  284. //
  285. // Return the result.
  286. //
  287. return(CRCResultRead(ui32Base, bPPResult));
  288. }
  289. //*****************************************************************************
  290. //
  291. // Close the Doxygen group.
  292. //! @}
  293. //
  294. //*****************************************************************************