lpc_crc.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /**********************************************************************
  2. * $Id$ lpc_crc.c 2011-06-02
  3. *//**
  4. * @file lpc_crc.c
  5. * @brief Contains all functions support for CRC firmware library on
  6. * LPC
  7. * @version 1.0
  8. * @date 02. June. 2011
  9. * @author NXP MCU SW Application Team
  10. *
  11. * Copyright(C) 2011, NXP Semiconductor
  12. * All rights reserved.
  13. *
  14. ***********************************************************************
  15. * Software that is described herein is for illustrative purposes only
  16. * which provides customers with programming information regarding the
  17. * products. This software is supplied "AS IS" without any warranties.
  18. * NXP Semiconductors assumes no responsibility or liability for the
  19. * use of the software, conveys no license or title under any patent,
  20. * copyright, or mask work right to the product. NXP Semiconductors
  21. * reserves the right to make changes in the software without
  22. * notification. NXP Semiconductors also make no representation or
  23. * warranty that such application will be suitable for the specified
  24. * use without further testing or modification.
  25. * Permission to use, copy, modify, and distribute this software and its
  26. * documentation is hereby granted, under NXP Semiconductors'
  27. * relevant copyright in the software, without fee, provided that it
  28. * is used in conjunction with NXP Semiconductors microcontrollers. This
  29. * copyright, permission, and disclaimer notice must appear in all copies of
  30. * this code.
  31. **********************************************************************/
  32. #ifdef __BUILD_WITH_EXAMPLE__
  33. #include "lpc_libcfg.h"
  34. #else
  35. #include "lpc_libcfg_default.h"
  36. #endif /* __BUILD_WITH_EXAMPLE__ */
  37. #ifdef _CRC
  38. /* Includes ------------------------------------------------------------------- */
  39. #include "lpc_crc.h"
  40. #include "lpc_clkpwr.h"
  41. /* Private Variables ----------------------------------------------------------- */
  42. volatile CRC_Type crc_cur_type;
  43. /* Public Functions ----------------------------------------------------------- */
  44. /** @addtogroup CRC_Public_Functions
  45. * @{
  46. */
  47. /*********************************************************************//**
  48. * @brief Initialize CRC operation
  49. * @param[in] CRCType CRC standard type, should be:
  50. * - CRC_POLY_CRCCCITT: CRC-CCITT polynomial
  51. * - CRC_POLY_CRC16: CRC-16 polynomial
  52. * - CRC_POLY_CRC32: CRC-32 polynomial
  53. * @return None
  54. **********************************************************************/
  55. void CRC_Init(CRC_Type CRCType)
  56. {
  57. if(CRCType == CRC_POLY_CRCCCITT)
  58. {
  59. LPC_CRC->MODE = 0x00;
  60. LPC_CRC->SEED = 0xFFFF;
  61. crc_cur_type = CRC_POLY_CRCCCITT;
  62. }
  63. else if(CRCType == CRC_POLY_CRC16)
  64. {
  65. LPC_CRC->MODE = 0x15;
  66. LPC_CRC->SEED = 0x0000;
  67. crc_cur_type = CRC_POLY_CRC16;
  68. }
  69. else if(CRCType == CRC_POLY_CRC32)
  70. {
  71. LPC_CRC->MODE = 0x36;
  72. LPC_CRC->SEED = 0xFFFFFFFF;
  73. crc_cur_type = CRC_POLY_CRC32;
  74. }
  75. else
  76. {
  77. //Invalid input parameter
  78. while(1);//error loop
  79. }
  80. }
  81. /*********************************************************************//**
  82. * @brief CRC reset
  83. * @param[in] None
  84. * @return None
  85. **********************************************************************/
  86. void CRC_Reset(void)
  87. {
  88. if(crc_cur_type == CRC_POLY_CRCCCITT)
  89. LPC_CRC->SEED = 0xFFFF;
  90. else if (crc_cur_type == CRC_POLY_CRC16)
  91. LPC_CRC->SEED = 0x0000;
  92. else if (crc_cur_type == CRC_POLY_CRC32)
  93. LPC_CRC->SEED = 0xFFFFFFFF;
  94. }
  95. /*********************************************************************//**
  96. * @brief CRC data checksum calculation
  97. * @param[in] data input data
  98. * @return data checksum result
  99. **********************************************************************/
  100. uint32_t CRC_CalcDataChecksum(uint32_t data, CRC_WR_SIZE SizeType)
  101. {
  102. if(SizeType == CRC_WR_8BIT)
  103. LPC_CRC->WR_DATA_BYTE.DATA = (uint8_t)data;
  104. else if(SizeType == CRC_WR_16BIT)
  105. LPC_CRC->WR_DATA_WORD.DATA = (uint16_t)data;
  106. else
  107. LPC_CRC->WR_DATA_DWORD.DATA = data;
  108. return(LPC_CRC->SUM);
  109. }
  110. /*********************************************************************//**
  111. * @brief CRC block data checksum calculation
  112. * @param[in] blockdata pointer to block input data
  113. * @param[in] blocksize size of block data
  114. * @param[in] SizeType size of data width per write, should be:
  115. - CRC_WR_8BIT : 8-bit write
  116. - CRC_WR_16BIT: 16-bit write
  117. - CRC_WR_32BIT: 32-bit write
  118. * @return block data checksum result
  119. **********************************************************************/
  120. uint32_t CRC_CalcBlockChecksum(void *block_data, uint32_t block_size, CRC_WR_SIZE data_size)
  121. {
  122. uint8_t *data = (uint8_t*) block_data;
  123. while(block_size !=0) {
  124. switch(data_size) {
  125. case CRC_WR_8BIT:
  126. {
  127. uint8_t *tmp = data;
  128. LPC_CRC->WR_DATA_BYTE.DATA = *tmp;
  129. data++;
  130. }
  131. break;
  132. case CRC_WR_16BIT:
  133. {
  134. uint16_t *tmp = (uint16_t*)data;
  135. LPC_CRC->WR_DATA_WORD.DATA = *tmp;
  136. data +=2;
  137. }
  138. break;
  139. default:
  140. {
  141. uint32_t *tmp = (uint32_t*)data;
  142. LPC_CRC->WR_DATA_DWORD.DATA = *tmp;
  143. data += 4;
  144. }
  145. break;
  146. }
  147. block_size--;
  148. }
  149. return(LPC_CRC->SUM);
  150. }
  151. /**
  152. * @}
  153. */
  154. #endif /*_CRC*/
  155. /**
  156. * @}
  157. */
  158. /* --------------------------------- End Of File ------------------------------ */