crc.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /******************************************************************************
  2. * @brief Cyclic redundancy check (CRC) source code.
  3. *
  4. ******************************************************************************/
  5. #include "common.h"
  6. #include "crc.h"
  7. /******************************************************************************
  8. * Global variables
  9. ******************************************************************************/
  10. /******************************************************************************
  11. * Constants and macros
  12. ******************************************************************************/
  13. /******************************************************************************
  14. * Local types
  15. ******************************************************************************/
  16. /******************************************************************************
  17. * Local function prototypes
  18. ******************************************************************************/
  19. /******************************************************************************
  20. * Local variables
  21. ******************************************************************************/
  22. /******************************************************************************
  23. * Local functions
  24. ******************************************************************************/
  25. /******************************************************************************
  26. * Global functions
  27. ******************************************************************************/
  28. /******************************************************************************
  29. * define CRC APIs
  30. *
  31. *//*! @addtogroup crc_api_list
  32. * @{
  33. *******************************************************************************/
  34. /*****************************************************************************//*!
  35. *
  36. * @brief initialize CRC with poly per control parameters
  37. *
  38. * @param[in] pConfig point to configuration.
  39. *
  40. * @return none
  41. *
  42. * @ Pass/ Fail criteria: none
  43. *****************************************************************************/
  44. void CRC_Init(CRC_ConfigType *pConfig)
  45. {
  46. uint32_t u32Sc ;
  47. u32Sc = 0;
  48. SIM->SCGC |= SIM_SCGC_CRC_MASK;
  49. u32Sc |= ((pConfig->bWidth & 0x01)<<24);
  50. u32Sc |= CRC_CTRL_TOTR(pConfig->bTransposeReadType & 0x03);
  51. u32Sc |= CRC_CTRL_TOT(pConfig->bTransposeWriteType & 0x03);
  52. if (pConfig->bFinalXOR)
  53. {
  54. u32Sc |= CRC_CTRL_FXOR_MASK;
  55. }
  56. CRC0->CTRL = u32Sc;
  57. if ( pConfig->bWidth )
  58. {
  59. CRC0->GPOLY = pConfig->u32PolyData;
  60. }
  61. else
  62. {
  63. CRC0->GPOLY_ACCESS16BIT.GPOLYL = pConfig->u32PolyData; /*!< only 16-bit write allowed */
  64. }
  65. }
  66. /*****************************************************************************//*!
  67. *
  68. * @brief crc module 16-bit mode calculation.
  69. *
  70. * @param[in] seed
  71. * @param[in] msg poiont to message buffer
  72. * @param[in] sizeBytes size of message
  73. *
  74. * @return data_out convertion result
  75. *
  76. * @ Pass/ Fail criteria: none
  77. *****************************************************************************/
  78. uint32_t CRC_Cal16(uint32_t seed, uint8_t *msg, uint32_t sizeBytes)
  79. {
  80. uint32_t ctrl_reg,data_out,data_in;
  81. uint8_t *pCRCBytes;
  82. uint32_t sizeWords;
  83. uint32_t i,j;
  84. /* Input seed, Set WaS=1 */
  85. ctrl_reg = CRC0->CTRL;
  86. CRC0->CTRL = ctrl_reg | CRC_CTRL_WAS_MASK;
  87. CRC0->ACCESS16BIT.DATAL = seed;
  88. /*Input data, Set WaS=0*/
  89. CRC0->CTRL = ctrl_reg & 0xFD000000;
  90. /*Wait for calculation completion*/
  91. sizeWords = sizeBytes>>1;
  92. j = 0;
  93. for(i=0;i<sizeWords;i++){
  94. data_in = (msg[j] << 8) | (msg[j+1]);
  95. j += 2;
  96. CRC0->ACCESS16BIT.DATAL =data_in;
  97. }
  98. if (j<sizeBytes)
  99. {
  100. pCRCBytes = (uint8_t*)&CRC0->ACCESS8BIT.DATALL;
  101. *pCRCBytes++ = msg[j];
  102. }
  103. data_out=CRC0->ACCESS16BIT.DATAL;
  104. return(data_out);
  105. }
  106. /*****************************************************************************//*!
  107. *
  108. * @brief crc module 32-bit mode calculation.
  109. *
  110. * @param[in] seed
  111. * @param[in] msg poiont to message buffer
  112. * @param[in] sizeBytes size of message
  113. *
  114. * @return data_out convertion result
  115. *
  116. * @ Pass/ Fail criteria: none
  117. *****************************************************************************/
  118. uint32_t CRC_Cal32(uint32_t seed, uint8_t *msg, uint32_t sizeBytes)
  119. {
  120. uint32_t ctrl_reg,data_out,data_in;
  121. uint32_t sizeDwords;
  122. uint8_t *pCRCBytes;
  123. uint32_t i,j;
  124. /*Input seed, Set WaS=1*/
  125. ctrl_reg = CRC0->CTRL;
  126. CRC0->CTRL = ctrl_reg | 0x02000000;
  127. CRC0->DATA = seed;
  128. /*Input data, Set WaS=0*/
  129. CRC0->CTRL = ctrl_reg & 0xFD000000;
  130. /*Wait for calculation completion*/
  131. sizeDwords = sizeBytes>>2;
  132. j = 0;
  133. for(i=0;i<sizeDwords;i++)
  134. {
  135. data_in = ((msg[j] << 24) | (msg[j+1] << 16) | (msg[j+2] << 8) | msg[j+3]);
  136. j += 4;
  137. CRC0->DATA = data_in;
  138. }
  139. if (j<sizeBytes)
  140. {
  141. pCRCBytes = (uint8_t*)&CRC0->ACCESS8BIT.DATALL;
  142. #if defined(BYTE_ENABLES_1_2_4_8)
  143. /*write single byte*/
  144. for(;j<sizeBytes;j++)
  145. {
  146. *pCRCBytes++ = msg[j];
  147. }
  148. #elif defined(BYTE_ENABLES_3_6_C)
  149. /*write two bytes*/
  150. data_in = 0;
  151. i = 0;
  152. for(;j<sizeBytes;j++)
  153. {
  154. data_in = (data_in <<8) | msg[j];
  155. i++;
  156. if (i==2)
  157. {
  158. i = 0;
  159. CRC0->ACCESS16BIT.DATAL = data_in;
  160. }
  161. }
  162. if (i==1)
  163. {
  164. CRC0->ACCESS8BIT.DATALL = data_in; /*!< write last byte */
  165. }
  166. #elif defined(BYTE_ENABLES_7_E)
  167. /*!< write three bytes */
  168. data_in = 0;
  169. i = 0;
  170. for(;j<sizeBytes;j++)
  171. {
  172. data_in = (data_in <<8) | msg[j];
  173. i++;
  174. if (i==3)
  175. {
  176. i = 0;
  177. /*write first char*/
  178. CRC0->ACCESS8BIT.DATAHL = (data_in>>16) & 0xff; /*!< write low byte of high word */
  179. /*write last two chars*/
  180. CRC0->ACCESS16BIT.DATAL = data_in & 0x00ffff; /*!< write low word */
  181. }
  182. }
  183. if ( i == 2)
  184. {
  185. CRC0->ACCESS16BIT.DATAL = (data_in); /*!< write last 2 bytes */
  186. }
  187. else if (i == 1)
  188. {
  189. CRC0->ACCESS8BIT.DATALL = data_in; /*!< write last byte */
  190. }
  191. #else /*!< write low byte only */
  192. for(;j<sizeBytes;j++)
  193. {
  194. *pCRCBytes = msg[j];
  195. }
  196. #endif
  197. }
  198. data_out=CRC0->DATA;
  199. return(data_out);
  200. }
  201. /*****************************************************************************//*!
  202. *
  203. * @brief de-initialize crc module, reset crc register.
  204. *
  205. * @param none
  206. *
  207. * @return none
  208. *
  209. * @ Pass/ Fail criteria: none
  210. *****************************************************************************/
  211. void CRC_DeInit(void)
  212. {
  213. CRC0->CTRL = 0x3000000; /*!< prepare for write 32-bit seed*/
  214. CRC0->DATA = 0xFFFFFFFF;/*!< write 32-bit seed to data register*/
  215. while(!(CRC0->DATA == 0xFFFFFFFF));
  216. CRC0->GPOLY = 0x00001021;
  217. CRC0->CTRL = 0; /*!< reset ctrl register*/
  218. SIM->SCGC &= ~SIM_SCGC_CRC_MASK;
  219. }
  220. /*! @} End of crc_api_list */