Stefan Scholz / ETL
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers crc8_ccitt.h Source File

crc8_ccitt.h

Go to the documentation of this file.
00001 ///\file
00002 
00003 /******************************************************************************
00004 The MIT License(MIT)
00005 
00006 Embedded Template Library.
00007 https://github.com/ETLCPP/etl
00008 http://www.etlcpp.com
00009 
00010 Copyright(c) 2014 jwellbelove
00011 
00012 Permission is hereby granted, free of charge, to any person obtaining a copy
00013 of this software and associated documentation files(the "Software"), to deal
00014 in the Software without restriction, including without limitation the rights
00015 to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
00016 copies of the Software, and to permit persons to whom the Software is
00017 furnished to do so, subject to the following conditions :
00018 
00019 The above copyright notice and this permission notice shall be included in all
00020 copies or substantial portions of the Software.
00021 
00022 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00023 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00024 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
00025 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00026 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00027 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
00028 SOFTWARE.
00029 ******************************************************************************/
00030 
00031 #ifndef __ETL_CRC8_CCITT__
00032 #define __ETL_CRC8_CCITT__
00033 
00034 #include <stdint.h>
00035 #include <iterator>
00036 
00037 #include "platform.h "
00038 #include "frame_check_sequence.h "
00039 
00040 #if defined(ETL_COMPILER_KEIL)
00041 #pragma diag_suppress 1300
00042 #endif
00043 
00044 ///\defgroup crc8_ccitt 8 bit CRC calculation
00045 ///\ingroup crc
00046 
00047 namespace etl
00048 {
00049   //***************************************************************************
00050   /// CRC8 table
00051   /// \ingroup crc8_ccitt
00052   //***************************************************************************
00053   extern const uint8_t CRC8_CCITT[];
00054 
00055   //***************************************************************************
00056   /// CRC8 CCITT policy.
00057   /// Calculates CRC8 CCITT using polynomial 0x07.
00058   //***************************************************************************
00059   struct crc_policy_8_ccitt
00060   {
00061     typedef uint8_t value_type;
00062 
00063     inline uint8_t initial() const
00064     {
00065       return 0;
00066     }
00067 
00068     inline uint8_t add(uint8_t crc, uint8_t value) const
00069     {
00070       return  CRC8_CCITT[crc ^ value];
00071     }
00072 
00073     inline uint8_t final(uint8_t crc) const
00074     {
00075       return crc;
00076     }
00077   };
00078 
00079   //*************************************************************************
00080   /// CRC8 CCITT
00081   //*************************************************************************
00082   class crc8_ccitt : public etl::frame_check_sequence<etl::crc_policy_8_ccitt>
00083   {
00084   public:
00085 
00086     //*************************************************************************
00087     /// Default constructor.
00088     //*************************************************************************
00089     crc8_ccitt()
00090     {
00091       this->reset();
00092     }
00093 
00094     //*************************************************************************
00095     /// Constructor from range.
00096     /// \param begin Start of the range.
00097     /// \param end   End of the range.
00098     //*************************************************************************
00099     template<typename TIterator>
00100     crc8_ccitt(TIterator begin, const TIterator end)
00101     {
00102       this->reset();
00103       this->add(begin, end);
00104     }
00105   };
00106 }
00107 
00108 #endif
00109