Device interface library for multiple platforms including Mbed.

Dependents:   DeepCover Embedded Security in IoT MaximInterface MAXREFDES155#

Maxim Interface is a library framework focused on providing flexible and expressive hardware interfaces. Both communication interfaces such as I2C and 1-Wire and device interfaces such as DS18B20 are supported. Modern C++ concepts are used extensively while keeping compatibility with C++98/C++03 and requiring no external dependencies. The embedded-friendly design does not depend on exceptions or RTTI.

The full version of the project is hosted on GitLab: https://gitlab.com/iabenz/MaximInterface

Committer:
IanBenzMaxim
Date:
Fri Sep 27 16:19:38 2019 -0500
Revision:
9:fa054cc51fdb
Parent:
MaximInterfaceCore/crc.hpp@8:5ea891c7d1a1
Fixed file capitalization.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
IanBenzMaxim 7:9cd16581b578 1 /*******************************************************************************
IanBenzMaxim 8:5ea891c7d1a1 2 * Copyright (C) Maxim Integrated Products, Inc., All Rights Reserved.
IanBenzMaxim 7:9cd16581b578 3 *
IanBenzMaxim 7:9cd16581b578 4 * Permission is hereby granted, free of charge, to any person obtaining a
IanBenzMaxim 7:9cd16581b578 5 * copy of this software and associated documentation files (the "Software"),
IanBenzMaxim 7:9cd16581b578 6 * to deal in the Software without restriction, including without limitation
IanBenzMaxim 7:9cd16581b578 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
IanBenzMaxim 7:9cd16581b578 8 * and/or sell copies of the Software, and to permit persons to whom the
IanBenzMaxim 7:9cd16581b578 9 * Software is furnished to do so, subject to the following conditions:
IanBenzMaxim 7:9cd16581b578 10 *
IanBenzMaxim 7:9cd16581b578 11 * The above copyright notice and this permission notice shall be included
IanBenzMaxim 7:9cd16581b578 12 * in all copies or substantial portions of the Software.
IanBenzMaxim 7:9cd16581b578 13 *
IanBenzMaxim 7:9cd16581b578 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
IanBenzMaxim 7:9cd16581b578 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
IanBenzMaxim 7:9cd16581b578 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IanBenzMaxim 7:9cd16581b578 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
IanBenzMaxim 7:9cd16581b578 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
IanBenzMaxim 7:9cd16581b578 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
IanBenzMaxim 7:9cd16581b578 20 * OTHER DEALINGS IN THE SOFTWARE.
IanBenzMaxim 7:9cd16581b578 21 *
IanBenzMaxim 7:9cd16581b578 22 * Except as contained in this notice, the name of Maxim Integrated
IanBenzMaxim 7:9cd16581b578 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
IanBenzMaxim 7:9cd16581b578 24 * Products, Inc. Branding Policy.
IanBenzMaxim 7:9cd16581b578 25 *
IanBenzMaxim 7:9cd16581b578 26 * The mere transfer of this software does not imply any licenses
IanBenzMaxim 7:9cd16581b578 27 * of trade secrets, proprietary technology, copyrights, patents,
IanBenzMaxim 7:9cd16581b578 28 * trademarks, maskwork rights, or any other form of intellectual
IanBenzMaxim 7:9cd16581b578 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
IanBenzMaxim 7:9cd16581b578 30 * ownership rights.
IanBenzMaxim 7:9cd16581b578 31 *******************************************************************************/
IanBenzMaxim 7:9cd16581b578 32
IanBenzMaxim 8:5ea891c7d1a1 33 #ifndef MaximInterfaceCore_Crc_hpp
IanBenzMaxim 8:5ea891c7d1a1 34 #define MaximInterfaceCore_Crc_hpp
IanBenzMaxim 7:9cd16581b578 35
IanBenzMaxim 7:9cd16581b578 36 #include <stddef.h>
IanBenzMaxim 7:9cd16581b578 37 #include <stdint.h>
IanBenzMaxim 7:9cd16581b578 38 #include "Config.hpp"
IanBenzMaxim 7:9cd16581b578 39 #include "span.hpp"
IanBenzMaxim 7:9cd16581b578 40
IanBenzMaxim 7:9cd16581b578 41 namespace MaximInterfaceCore {
IanBenzMaxim 7:9cd16581b578 42
IanBenzMaxim 7:9cd16581b578 43 /// @brief Perform a CRC8 calculation.
IanBenzMaxim 7:9cd16581b578 44 /// @param crc Beginning state of the CRC generator.
IanBenzMaxim 7:9cd16581b578 45 /// @param data Data to pass though the CRC generator.
IanBenzMaxim 7:9cd16581b578 46 /// @returns The calculated CRC8.
IanBenzMaxim 7:9cd16581b578 47 MaximInterfaceCore_EXPORT uint_fast8_t calculateCrc8(uint_fast8_t crc,
IanBenzMaxim 7:9cd16581b578 48 uint_fast8_t data);
IanBenzMaxim 7:9cd16581b578 49
IanBenzMaxim 7:9cd16581b578 50 /// @brief
IanBenzMaxim 7:9cd16581b578 51 /// Perform a CRC8 calculation with the CRC generator initialized to zero.
IanBenzMaxim 7:9cd16581b578 52 /// @param data Data to pass though the CRC generator.
IanBenzMaxim 7:9cd16581b578 53 /// @returns The calculated CRC8.
IanBenzMaxim 7:9cd16581b578 54 inline uint_fast8_t calculateCrc8(uint_fast8_t data) {
IanBenzMaxim 7:9cd16581b578 55 return calculateCrc8(0, data);
IanBenzMaxim 7:9cd16581b578 56 }
IanBenzMaxim 7:9cd16581b578 57
IanBenzMaxim 7:9cd16581b578 58 /// @brief Perform a CRC8 calculation with variable length data.
IanBenzMaxim 7:9cd16581b578 59 /// @param crc Beginning state of the CRC generator.
IanBenzMaxim 7:9cd16581b578 60 /// @param[in] data Data array to pass through the CRC generator.
IanBenzMaxim 7:9cd16581b578 61 /// @returns The calculated CRC8.
IanBenzMaxim 7:9cd16581b578 62 MaximInterfaceCore_EXPORT uint_fast8_t
IanBenzMaxim 7:9cd16581b578 63 calculateCrc8(uint_fast8_t crc, span<const uint_least8_t> data);
IanBenzMaxim 7:9cd16581b578 64
IanBenzMaxim 7:9cd16581b578 65 /// @brief
IanBenzMaxim 7:9cd16581b578 66 /// Perform a CRC8 calculation with variable length data and the CRC generator
IanBenzMaxim 7:9cd16581b578 67 /// initialized to zero.
IanBenzMaxim 7:9cd16581b578 68 /// @param[in] data Data array to pass through the CRC generator.
IanBenzMaxim 7:9cd16581b578 69 /// @returns The calculated CRC8.
IanBenzMaxim 7:9cd16581b578 70 inline uint_fast8_t calculateCrc8(span<const uint_least8_t> data) {
IanBenzMaxim 7:9cd16581b578 71 return calculateCrc8(0, data);
IanBenzMaxim 7:9cd16581b578 72 }
IanBenzMaxim 7:9cd16581b578 73
IanBenzMaxim 7:9cd16581b578 74 /// @brief Perform a CRC16 calculation.
IanBenzMaxim 7:9cd16581b578 75 /// @param crc Beginning state of the CRC generator.
IanBenzMaxim 7:9cd16581b578 76 /// @param data Data to pass though the CRC generator.
IanBenzMaxim 7:9cd16581b578 77 /// @returns The calculated CRC16.
IanBenzMaxim 7:9cd16581b578 78 MaximInterfaceCore_EXPORT uint_fast16_t calculateCrc16(uint_fast16_t crc,
IanBenzMaxim 7:9cd16581b578 79 uint_fast8_t data);
IanBenzMaxim 7:9cd16581b578 80
IanBenzMaxim 7:9cd16581b578 81 /// @brief
IanBenzMaxim 7:9cd16581b578 82 /// Perform a CRC16 calculation with the CRC generator initialized to zero.
IanBenzMaxim 7:9cd16581b578 83 /// @param data Data to pass though the CRC generator.
IanBenzMaxim 7:9cd16581b578 84 /// @returns The calculated CRC16.
IanBenzMaxim 7:9cd16581b578 85 inline uint_fast16_t calculateCrc16(uint_fast8_t data) {
IanBenzMaxim 7:9cd16581b578 86 return calculateCrc16(0, data);
IanBenzMaxim 7:9cd16581b578 87 }
IanBenzMaxim 7:9cd16581b578 88
IanBenzMaxim 7:9cd16581b578 89 /// @brief Perform a CRC16 calculation with variable length data.
IanBenzMaxim 7:9cd16581b578 90 /// @param crc Beginning state of the CRC generator.
IanBenzMaxim 7:9cd16581b578 91 /// @param[in] data Data array to pass through the CRC generator.
IanBenzMaxim 7:9cd16581b578 92 /// @returns The calculated CRC16.
IanBenzMaxim 7:9cd16581b578 93 MaximInterfaceCore_EXPORT uint_fast16_t
IanBenzMaxim 7:9cd16581b578 94 calculateCrc16(uint_fast16_t crc, span<const uint_least8_t> data);
IanBenzMaxim 7:9cd16581b578 95
IanBenzMaxim 7:9cd16581b578 96 /// @brief
IanBenzMaxim 7:9cd16581b578 97 /// Perform a CRC16 calculation with variable length data and the CRC generator
IanBenzMaxim 7:9cd16581b578 98 /// initialized to zero.
IanBenzMaxim 7:9cd16581b578 99 /// @param[in] data Data array to pass through the CRC generator.
IanBenzMaxim 7:9cd16581b578 100 /// @returns The calculated CRC16.
IanBenzMaxim 7:9cd16581b578 101 inline uint_fast16_t calculateCrc16(span<const uint_least8_t> data) {
IanBenzMaxim 7:9cd16581b578 102 return calculateCrc16(0, data);
IanBenzMaxim 7:9cd16581b578 103 }
IanBenzMaxim 7:9cd16581b578 104
IanBenzMaxim 7:9cd16581b578 105 } // namespace MaximInterfaceCore
IanBenzMaxim 7:9cd16581b578 106
IanBenzMaxim 7:9cd16581b578 107 #endif