stm32_crc.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright (C) 2010 by Ulrich Prinz (uprinz2@netscape.net)
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the copyright holders nor the names of
  16. * contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  25. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  26. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  27. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  29. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. *
  32. * For additional information see http://www.ethernut.de/
  33. */
  34. /*
  35. * \verbatim
  36. * $Id: stm32_crc.c 3110 2010-09-16 08:32:42Z astralix $
  37. * \endverbatim
  38. */
  39. #include <cfg/os.h>
  40. #include <cfg/clock.h>
  41. #include <cfg/arch.h>
  42. #include <cfg/arch/gpio.h>
  43. #include <arch/cm3.h>
  44. #include <arch/cm3/stm/stm32xxxx.h>
  45. /**
  46. * @brief Resets the CRC Data register (DR).
  47. * @param None
  48. * @retval : None
  49. */
  50. void CRC_ResetDR(void)
  51. {
  52. /* Reset CRC generator */
  53. CRC->CR = CRC_CR_RESET;
  54. }
  55. /**
  56. * @brief Computes the 32-bit CRC of a given data word(32-bit).
  57. * @param Data: data word(32-bit) to compute its CRC
  58. * @retval : 32-bit CRC
  59. */
  60. uint32_t CRC_CalcCRC(uint32_t Data)
  61. {
  62. CRC->DR = Data;
  63. return (CRC->DR);
  64. }
  65. /**
  66. * @brief Computes the 32-bit CRC of a given buffer of data word(32-bit).
  67. * @param pBuffer: pointer to the buffer containing the data to be
  68. * computed
  69. * @param BufferLength: length of the buffer to be computed
  70. * @retval : 32-bit CRC
  71. */
  72. uint32_t CRC_CalcBlockCRC(uint32_t pBuffer[], uint32_t BufferLength)
  73. {
  74. uint32_t index = 0;
  75. for(index = 0; index < BufferLength; index++)
  76. {
  77. CRC->DR = pBuffer[index];
  78. }
  79. return (CRC->DR);
  80. }
  81. /**
  82. * @brief Returns the current CRC value.
  83. * @param None
  84. * @retval : 32-bit CRC
  85. */
  86. uint32_t CRC_GetCRC(void)
  87. {
  88. return (CRC->DR);
  89. }
  90. /**
  91. * @brief Stores a 8-bit data in the Independent Data(ID) register.
  92. * @param IDValue: 8-bit value to be stored in the ID register
  93. * @retval : None
  94. */
  95. void CRC_SetIDRegister(uint8_t IDValue)
  96. {
  97. CRC->IDR = IDValue;
  98. }
  99. /**
  100. * @brief Returns the 8-bit data stored in the Independent Data(ID) register
  101. * @param None
  102. * @retval : 8-bit value of the ID register
  103. */
  104. uint8_t CRC_GetIDRegister(void)
  105. {
  106. return (CRC->IDR);
  107. }
  108. /**
  109. * @brief Init the CRC machine
  110. * @param None
  111. * @retval :None
  112. */
  113. void CRC_Init(void)
  114. {
  115. #if defined(MCU_STM32F1) || defined(MCU_STM32L1) || defined(MCU_STM32F3)
  116. RCC->AHBENR |= RCC_AHBENR_CRCEN;
  117. #elif defined(MCU_STM32F2) || defined(MCU_STM32F4)
  118. RCC->AHB1ENR |= RCC_AHB1ENR_CRCEN;
  119. #else
  120. #warning "Unknown STM32 family"
  121. #endif
  122. /* Reset CRC generator */
  123. CRC->CR = CRC_CR_RESET;
  124. }