sys_crc.h 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /**
  2. * Copyright (c) 2016 - 2020 Nordic Semiconductor ASA and Luxoft Global Operations Gmbh.
  3. *
  4. * All Rights Reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. *
  10. * 1. Redistributions of source code must retain the above copyright notice, this
  11. * list of conditions and the following disclaimer.
  12. *
  13. * 2. Redistributions in binary form, except as embedded into a Nordic
  14. * Semiconductor ASA integrated circuit in a product or a software update for
  15. * such product, must reproduce the above copyright notice, this list of
  16. * conditions and the following disclaimer in the documentation and/or other
  17. * materials provided with the distribution.
  18. *
  19. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  20. * contributors may be used to endorse or promote products derived from this
  21. * software without specific prior written permission.
  22. *
  23. * 4. This software, with or without modification, must only be used with a
  24. * Nordic Semiconductor ASA integrated circuit.
  25. *
  26. * 5. Any software provided in binary form under this license must not be reverse
  27. * engineered, decompiled, modified and/or disassembled.
  28. *
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  31. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  33. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  34. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  35. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  36. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  38. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  39. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  40. *
  41. */
  42. #ifndef SYS_CRC_H_INCLUDED
  43. #define SYS_CRC_H_INCLUDED
  44. #include <stdint.h>
  45. #include <stddef.h>
  46. /** @file
  47. * This file contains declarations of the CRC computing routines and necessary macros/types.
  48. *
  49. * @defgroup sys_crc System CRC API
  50. * @ingroup sys_15_4
  51. * @{
  52. * @brief Module to declare System CRC API.
  53. * @details The CRC module implements a set of routines to compute the 16-bit CRC value for octet arrays.
  54. */
  55. /**
  56. * @brief Defines an initial value for the CRC sum.
  57. */
  58. #define SYS_CRC_INIT 0
  59. /**
  60. * @brief CRC value type. This module uses 16-bit CRC.
  61. */
  62. typedef uint16_t sys_crc_t;
  63. /**
  64. * @brief Function for computing CRC value for given data.
  65. *
  66. * @param[in] p_data Pointer to data to compute.
  67. * @param[in] length Length of data.
  68. *
  69. * @return Returns the CRC value for input data.
  70. */
  71. sys_crc_t sys_crc_calc(const uint8_t * p_data, size_t length);
  72. /**
  73. * @brief Function for updating the CRC value taking into the account the previously counted value.
  74. *
  75. * @details This function is used when input data is represented by several pieces.
  76. * Consequently, a call to this function for each piece will give a correct
  77. * total CRC value.
  78. *
  79. * @param[in] current_crc Previously counted CRC value. Should be SYS_CRC_INIT for the first piece.
  80. * @param[in] p_data Pointer to the current piece of data.
  81. * @param[in] length Length of the current piece of data.
  82. *
  83. * @return Returns the updated CRC value.
  84. */
  85. sys_crc_t sys_crc_continue(sys_crc_t current_crc, const uint8_t * p_data, size_t length);
  86. /** @} */
  87. #endif /* SYS_CRC_H_INCLUDED */