crc.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. //*****************************************************************************
  2. //
  3. // crc.c
  4. //
  5. // Driver for the CRC module.
  6. //
  7. // Copyright (C) 2014 Texas Instruments Incorporated - http://www.ti.com/
  8. //
  9. //
  10. // Redistribution and use in source and binary forms, with or without
  11. // modification, are permitted provided that the following conditions
  12. // are met:
  13. //
  14. // Redistributions of source code must retain the above copyright
  15. // notice, this list of conditions and the following disclaimer.
  16. //
  17. // Redistributions in binary form must reproduce the above copyright
  18. // notice, this list of conditions and the following disclaimer in the
  19. // documentation and/or other materials provided with the
  20. // distribution.
  21. //
  22. // Neither the name of Texas Instruments Incorporated nor the names of
  23. // its contributors may be used to endorse or promote products derived
  24. // from this software without specific prior written permission.
  25. //
  26. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  27. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  28. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  29. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  30. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  31. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  32. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  33. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  34. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  35. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  36. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  37. //
  38. //*****************************************************************************
  39. //*****************************************************************************
  40. //
  41. //! \addtogroup CRC_Cyclic_Redundancy_Check_api
  42. //! @{
  43. //
  44. //*****************************************************************************
  45. #include <stdbool.h>
  46. #include <stdint.h>
  47. #include "inc/hw_dthe.h"
  48. #include "inc/hw_memmap.h"
  49. #include "inc/hw_types.h"
  50. #include "crc.h"
  51. #include "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 EC_CRC_CFG_INIT_SEED - Initialize with seed value
  65. //! - \b EC_CRC_CFG_INIT_0 - Initialize to all '0s'
  66. //! - \b EC_CRC_CFG_INIT_1 - Initialize to all '1s'
  67. //!
  68. //! Input Data Size
  69. //! - \b EC_CRC_CFG_SIZE_8BIT - Input data size of 8 bits
  70. //! - \b EC_CRC_CFG_SIZE_32BIT - Input data size of 32 bits
  71. //!
  72. //! Post Process Reverse/Inverse
  73. //! - \b EC_CRC_CFG_RESINV - Result inverse enable
  74. //! - \b EC_CRC_CFG_OBR - Output reverse enable
  75. //!
  76. //! Input Bit Reverse
  77. //! - \b EC_CRC_CFG_IBR - Bit reverse enable
  78. //!
  79. //! Endian Control
  80. //! - \b EC_CRC_CFG_ENDIAN_SBHW - Swap byte in half-word
  81. //! - \b EC_CRC_CFG_ENDIAN_SHW - Swap half-word
  82. //!
  83. //! Operation Type
  84. //! - \b EC_CRC_CFG_TYPE_P8005 - Polynomial 0x8005
  85. //! - \b EC_CRC_CFG_TYPE_P1021 - Polynomial 0x1021
  86. //! - \b EC_CRC_CFG_TYPE_P4C11DB7 - Polynomial 0x4C11DB7
  87. //! - \b EC_CRC_CFG_TYPE_P1EDC6F41 - Polynomial 0x1EDC6F41
  88. //! - \b EC_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 == DTHE_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 + DTHE_O_CRC_CTRL) = 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 EC_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 == DTHE_BASE);
  143. //
  144. // Write the seed value to the seed register.
  145. //
  146. HWREG(ui32Base + DTHE_O_CRC_SEED) = 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 EC_CRC_CFG_SIZE_8BIT or \b EC_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 signficant
  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 == DTHE_BASE);
  174. //
  175. // Write the data
  176. //
  177. HWREG(DTHE_BASE + DTHE_O_CRC_DIN) = 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. //!
  185. //! This function reads either the unmodified CRC result or the post
  186. //! processed CRC result from the EC module. The post-processing options
  187. //! are selectable through \b EC_CRC_CFG_RESINV and \b EC_CRC_CFG_OBR
  188. //! parameters in the CRCConfigSet() function.
  189. //!
  190. //! \return The CRC result.
  191. //
  192. //*****************************************************************************
  193. uint32_t
  194. CRCResultRead(uint32_t ui32Base)
  195. {
  196. //
  197. // Check the arguments.
  198. //
  199. ASSERT(ui32Base == DTHE_BASE);
  200. //
  201. // return value.
  202. //
  203. return(HWREG(DTHE_BASE + DTHE_O_CRC_RSLT_PP));
  204. }
  205. //*****************************************************************************
  206. //
  207. //! Process data to generate a CRC with the EC module.
  208. //!
  209. //! \param ui32Base is the base address of the EC module.
  210. //! \param puiDataIn is a pointer to an array of data that is processed.
  211. //! \param ui32DataLength is the number of data items that are processed
  212. //! to produce the CRC.
  213. //! \param ui32Config the config parameter to determine the CRC mode
  214. //!
  215. //! This function processes an array of data to produce a CRC result.
  216. //! This function takes the CRC mode as the parameter.
  217. //!
  218. //! The data in the array pointed to be \e pui32DataIn is either an array
  219. //! of bytes or an array or words depending on the selection of the input
  220. //! data size options \b EC_CRC_CFG_SIZE_8BIT and
  221. //! \b EC_CRC_CFG_SIZE_32BIT.
  222. //!
  223. //! This function returns either the unmodified CRC result or the
  224. //! post- processed CRC result from the EC module. The post-processing
  225. //! options are selectable through \b EC_CRC_CFG_RESINV and
  226. //! \b EC_CRC_CFG_OBR parameters.
  227. //!
  228. //! \return The CRC result.
  229. //
  230. //*****************************************************************************
  231. uint32_t
  232. CRCDataProcess(uint32_t ui32Base, void *puiDataIn,
  233. uint32_t ui32DataLength, uint32_t ui32Config)
  234. {
  235. uint8_t *pui8DataIn;
  236. uint32_t *pui32DataIn;
  237. //
  238. // Check the arguments.
  239. //
  240. ASSERT(ui32Base == DTHE_BASE);
  241. //
  242. // See if the CRC is operating in 8-bit or 32-bit mode.
  243. //
  244. if(ui32Config & DTHE_CRC_CTRL_SIZE)
  245. {
  246. //
  247. // The CRC is operating in 8-bit mode, so create an 8-bit pointer to
  248. // the data.
  249. //
  250. pui8DataIn = (uint8_t *)puiDataIn;
  251. //
  252. // Loop through the input data.
  253. //
  254. while(ui32DataLength--)
  255. {
  256. //
  257. // Write the next data byte.
  258. //
  259. HWREG(ui32Base + DTHE_O_CRC_DIN) = *pui8DataIn++;
  260. }
  261. }
  262. else
  263. {
  264. //
  265. // The CRC is operating in 32-bit mode, so loop through the input data.
  266. //
  267. pui32DataIn = (uint32_t *)puiDataIn;
  268. while(ui32DataLength--)
  269. {
  270. //
  271. // Write the next data word.
  272. //
  273. HWREG(ui32Base + DTHE_O_CRC_DIN) = *pui32DataIn++;
  274. }
  275. }
  276. //
  277. // Return the result.
  278. //
  279. return(CRCResultRead(ui32Base));
  280. }
  281. //*****************************************************************************
  282. //
  283. // Close the Doxygen group.
  284. //! @}
  285. //
  286. //*****************************************************************************