![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Color Oled(SSD1331) connect to STMicroelectronics Nucleo-F466
mbed-os/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_K82F/drivers/fsl_crc.c@0:8fdf9a60065b, 2018-10-10 (annotated)
- Committer:
- kadonotakashi
- Date:
- Wed Oct 10 00:33:53 2018 +0000
- Revision:
- 0:8fdf9a60065b
how to make mbed librry
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kadonotakashi | 0:8fdf9a60065b | 1 | /* |
kadonotakashi | 0:8fdf9a60065b | 2 | * Copyright (c) 2015-2016, Freescale Semiconductor, Inc. |
kadonotakashi | 0:8fdf9a60065b | 3 | * All rights reserved. |
kadonotakashi | 0:8fdf9a60065b | 4 | * |
kadonotakashi | 0:8fdf9a60065b | 5 | * Redistribution and use in source and binary forms, with or without modification, |
kadonotakashi | 0:8fdf9a60065b | 6 | * are permitted provided that the following conditions are met: |
kadonotakashi | 0:8fdf9a60065b | 7 | * |
kadonotakashi | 0:8fdf9a60065b | 8 | * o Redistributions of source code must retain the above copyright notice, this list |
kadonotakashi | 0:8fdf9a60065b | 9 | * of conditions and the following disclaimer. |
kadonotakashi | 0:8fdf9a60065b | 10 | * |
kadonotakashi | 0:8fdf9a60065b | 11 | * o Redistributions in binary form must reproduce the above copyright notice, this |
kadonotakashi | 0:8fdf9a60065b | 12 | * list of conditions and the following disclaimer in the documentation and/or |
kadonotakashi | 0:8fdf9a60065b | 13 | * other materials provided with the distribution. |
kadonotakashi | 0:8fdf9a60065b | 14 | * |
kadonotakashi | 0:8fdf9a60065b | 15 | * o Neither the name of Freescale Semiconductor, Inc. nor the names of its |
kadonotakashi | 0:8fdf9a60065b | 16 | * contributors may be used to endorse or promote products derived from this |
kadonotakashi | 0:8fdf9a60065b | 17 | * software without specific prior written permission. |
kadonotakashi | 0:8fdf9a60065b | 18 | * |
kadonotakashi | 0:8fdf9a60065b | 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
kadonotakashi | 0:8fdf9a60065b | 20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
kadonotakashi | 0:8fdf9a60065b | 21 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
kadonotakashi | 0:8fdf9a60065b | 22 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR |
kadonotakashi | 0:8fdf9a60065b | 23 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
kadonotakashi | 0:8fdf9a60065b | 24 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
kadonotakashi | 0:8fdf9a60065b | 25 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
kadonotakashi | 0:8fdf9a60065b | 26 | * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
kadonotakashi | 0:8fdf9a60065b | 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
kadonotakashi | 0:8fdf9a60065b | 28 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
kadonotakashi | 0:8fdf9a60065b | 29 | */ |
kadonotakashi | 0:8fdf9a60065b | 30 | #include "fsl_crc.h" |
kadonotakashi | 0:8fdf9a60065b | 31 | |
kadonotakashi | 0:8fdf9a60065b | 32 | /******************************************************************************* |
kadonotakashi | 0:8fdf9a60065b | 33 | * Definitions |
kadonotakashi | 0:8fdf9a60065b | 34 | ******************************************************************************/ |
kadonotakashi | 0:8fdf9a60065b | 35 | /*! @internal @brief Has data register with name CRC. */ |
kadonotakashi | 0:8fdf9a60065b | 36 | #if defined(FSL_FEATURE_CRC_HAS_CRC_REG) && FSL_FEATURE_CRC_HAS_CRC_REG |
kadonotakashi | 0:8fdf9a60065b | 37 | #define DATA CRC |
kadonotakashi | 0:8fdf9a60065b | 38 | #define DATALL CRCLL |
kadonotakashi | 0:8fdf9a60065b | 39 | #endif |
kadonotakashi | 0:8fdf9a60065b | 40 | |
kadonotakashi | 0:8fdf9a60065b | 41 | #if defined(CRC_DRIVER_USE_CRC16_CCIT_FALSE_AS_DEFAULT) && CRC_DRIVER_USE_CRC16_CCIT_FALSE_AS_DEFAULT |
kadonotakashi | 0:8fdf9a60065b | 42 | /* @brief Default user configuration structure for CRC-16-CCITT */ |
kadonotakashi | 0:8fdf9a60065b | 43 | #define CRC_DRIVER_DEFAULT_POLYNOMIAL 0x1021U |
kadonotakashi | 0:8fdf9a60065b | 44 | /*< CRC-16-CCIT polynomial x**16 + x**12 + x**5 + x**0 */ |
kadonotakashi | 0:8fdf9a60065b | 45 | #define CRC_DRIVER_DEFAULT_SEED 0xFFFFU |
kadonotakashi | 0:8fdf9a60065b | 46 | /*< Default initial checksum */ |
kadonotakashi | 0:8fdf9a60065b | 47 | #define CRC_DRIVER_DEFAULT_REFLECT_IN false |
kadonotakashi | 0:8fdf9a60065b | 48 | /*< Default is no transpose */ |
kadonotakashi | 0:8fdf9a60065b | 49 | #define CRC_DRIVER_DEFAULT_REFLECT_OUT false |
kadonotakashi | 0:8fdf9a60065b | 50 | /*< Default is transpose bytes */ |
kadonotakashi | 0:8fdf9a60065b | 51 | #define CRC_DRIVER_DEFAULT_COMPLEMENT_CHECKSUM false |
kadonotakashi | 0:8fdf9a60065b | 52 | /*< Default is without complement of CRC data register read data */ |
kadonotakashi | 0:8fdf9a60065b | 53 | #define CRC_DRIVER_DEFAULT_CRC_BITS kCrcBits16 |
kadonotakashi | 0:8fdf9a60065b | 54 | /*< Default is 16-bit CRC protocol */ |
kadonotakashi | 0:8fdf9a60065b | 55 | #define CRC_DRIVER_DEFAULT_CRC_RESULT kCrcFinalChecksum |
kadonotakashi | 0:8fdf9a60065b | 56 | /*< Default is resutl type is final checksum */ |
kadonotakashi | 0:8fdf9a60065b | 57 | #endif /* CRC_DRIVER_USE_CRC16_CCIT_FALSE_AS_DEFAULT */ |
kadonotakashi | 0:8fdf9a60065b | 58 | |
kadonotakashi | 0:8fdf9a60065b | 59 | /*! @brief CRC type of transpose of read write data */ |
kadonotakashi | 0:8fdf9a60065b | 60 | typedef enum _crc_transpose_type |
kadonotakashi | 0:8fdf9a60065b | 61 | { |
kadonotakashi | 0:8fdf9a60065b | 62 | kCrcTransposeNone = 0U, /*! No transpose */ |
kadonotakashi | 0:8fdf9a60065b | 63 | kCrcTransposeBits = 1U, /*! Tranpose bits in bytes */ |
kadonotakashi | 0:8fdf9a60065b | 64 | kCrcTransposeBitsAndBytes = 2U, /*! Transpose bytes and bits in bytes */ |
kadonotakashi | 0:8fdf9a60065b | 65 | kCrcTransposeBytes = 3U, /*! Transpose bytes */ |
kadonotakashi | 0:8fdf9a60065b | 66 | } crc_transpose_type_t; |
kadonotakashi | 0:8fdf9a60065b | 67 | |
kadonotakashi | 0:8fdf9a60065b | 68 | /*! |
kadonotakashi | 0:8fdf9a60065b | 69 | * @brief CRC module configuration. |
kadonotakashi | 0:8fdf9a60065b | 70 | * |
kadonotakashi | 0:8fdf9a60065b | 71 | * This structure holds the configuration for the CRC module. |
kadonotakashi | 0:8fdf9a60065b | 72 | */ |
kadonotakashi | 0:8fdf9a60065b | 73 | typedef struct _crc_module_config |
kadonotakashi | 0:8fdf9a60065b | 74 | { |
kadonotakashi | 0:8fdf9a60065b | 75 | uint32_t polynomial; /*!< CRC Polynomial, MSBit first.@n |
kadonotakashi | 0:8fdf9a60065b | 76 | Example polynomial: 0x1021 = 1_0000_0010_0001 = x^12+x^5+1 */ |
kadonotakashi | 0:8fdf9a60065b | 77 | uint32_t seed; /*!< Starting checksum value */ |
kadonotakashi | 0:8fdf9a60065b | 78 | crc_transpose_type_t readTranspose; /*!< Type of transpose when reading CRC result. */ |
kadonotakashi | 0:8fdf9a60065b | 79 | crc_transpose_type_t writeTranspose; /*!< Type of transpose when writing CRC input data. */ |
kadonotakashi | 0:8fdf9a60065b | 80 | bool complementChecksum; /*!< True if the result shall be complement of the actual checksum. */ |
kadonotakashi | 0:8fdf9a60065b | 81 | crc_bits_t crcBits; /*!< Selects 16- or 32- bit CRC protocol. */ |
kadonotakashi | 0:8fdf9a60065b | 82 | } crc_module_config_t; |
kadonotakashi | 0:8fdf9a60065b | 83 | |
kadonotakashi | 0:8fdf9a60065b | 84 | /******************************************************************************* |
kadonotakashi | 0:8fdf9a60065b | 85 | * Code |
kadonotakashi | 0:8fdf9a60065b | 86 | ******************************************************************************/ |
kadonotakashi | 0:8fdf9a60065b | 87 | |
kadonotakashi | 0:8fdf9a60065b | 88 | /*! |
kadonotakashi | 0:8fdf9a60065b | 89 | * @brief Returns transpose type for CRC protocol reflect in parameter. |
kadonotakashi | 0:8fdf9a60065b | 90 | * |
kadonotakashi | 0:8fdf9a60065b | 91 | * This functions helps to set writeTranspose member of crc_config_t structure. Reflect in is CRC protocol parameter. |
kadonotakashi | 0:8fdf9a60065b | 92 | * |
kadonotakashi | 0:8fdf9a60065b | 93 | * @param enable True or false for the selected CRC protocol Reflect In (refin) parameter. |
kadonotakashi | 0:8fdf9a60065b | 94 | */ |
kadonotakashi | 0:8fdf9a60065b | 95 | static inline crc_transpose_type_t CRC_GetTransposeTypeFromReflectIn(bool enable) |
kadonotakashi | 0:8fdf9a60065b | 96 | { |
kadonotakashi | 0:8fdf9a60065b | 97 | return ((enable) ? kCrcTransposeBitsAndBytes : kCrcTransposeBytes); |
kadonotakashi | 0:8fdf9a60065b | 98 | } |
kadonotakashi | 0:8fdf9a60065b | 99 | |
kadonotakashi | 0:8fdf9a60065b | 100 | /*! |
kadonotakashi | 0:8fdf9a60065b | 101 | * @brief Returns transpose type for CRC protocol reflect out parameter. |
kadonotakashi | 0:8fdf9a60065b | 102 | * |
kadonotakashi | 0:8fdf9a60065b | 103 | * This functions helps to set readTranspose member of crc_config_t structure. Reflect out is CRC protocol parameter. |
kadonotakashi | 0:8fdf9a60065b | 104 | * |
kadonotakashi | 0:8fdf9a60065b | 105 | * @param enable True or false for the selected CRC protocol Reflect Out (refout) parameter. |
kadonotakashi | 0:8fdf9a60065b | 106 | */ |
kadonotakashi | 0:8fdf9a60065b | 107 | static inline crc_transpose_type_t CRC_GetTransposeTypeFromReflectOut(bool enable) |
kadonotakashi | 0:8fdf9a60065b | 108 | { |
kadonotakashi | 0:8fdf9a60065b | 109 | return ((enable) ? kCrcTransposeBitsAndBytes : kCrcTransposeNone); |
kadonotakashi | 0:8fdf9a60065b | 110 | } |
kadonotakashi | 0:8fdf9a60065b | 111 | |
kadonotakashi | 0:8fdf9a60065b | 112 | /*! |
kadonotakashi | 0:8fdf9a60065b | 113 | * @brief Starts checksum computation. |
kadonotakashi | 0:8fdf9a60065b | 114 | * |
kadonotakashi | 0:8fdf9a60065b | 115 | * Configures the CRC module for the specified CRC protocol. @n |
kadonotakashi | 0:8fdf9a60065b | 116 | * Starts the checksum computation by writing the seed value |
kadonotakashi | 0:8fdf9a60065b | 117 | * |
kadonotakashi | 0:8fdf9a60065b | 118 | * @param base CRC peripheral address. |
kadonotakashi | 0:8fdf9a60065b | 119 | * @param config Pointer to protocol configuration structure. |
kadonotakashi | 0:8fdf9a60065b | 120 | */ |
kadonotakashi | 0:8fdf9a60065b | 121 | static void CRC_ConfigureAndStart(CRC_Type *base, const crc_module_config_t *config) |
kadonotakashi | 0:8fdf9a60065b | 122 | { |
kadonotakashi | 0:8fdf9a60065b | 123 | uint32_t crcControl; |
kadonotakashi | 0:8fdf9a60065b | 124 | |
kadonotakashi | 0:8fdf9a60065b | 125 | /* pre-compute value for CRC control registger based on user configuraton without WAS field */ |
kadonotakashi | 0:8fdf9a60065b | 126 | crcControl = 0 | CRC_CTRL_TOT(config->writeTranspose) | CRC_CTRL_TOTR(config->readTranspose) | |
kadonotakashi | 0:8fdf9a60065b | 127 | CRC_CTRL_FXOR(config->complementChecksum) | CRC_CTRL_TCRC(config->crcBits); |
kadonotakashi | 0:8fdf9a60065b | 128 | |
kadonotakashi | 0:8fdf9a60065b | 129 | /* make sure the control register is clear - WAS is deasserted, and protocol is set */ |
kadonotakashi | 0:8fdf9a60065b | 130 | base->CTRL = crcControl; |
kadonotakashi | 0:8fdf9a60065b | 131 | |
kadonotakashi | 0:8fdf9a60065b | 132 | /* write polynomial register */ |
kadonotakashi | 0:8fdf9a60065b | 133 | base->GPOLY = config->polynomial; |
kadonotakashi | 0:8fdf9a60065b | 134 | |
kadonotakashi | 0:8fdf9a60065b | 135 | /* write pre-computed control register value along with WAS to start checksum computation */ |
kadonotakashi | 0:8fdf9a60065b | 136 | base->CTRL = crcControl | CRC_CTRL_WAS(true); |
kadonotakashi | 0:8fdf9a60065b | 137 | |
kadonotakashi | 0:8fdf9a60065b | 138 | /* write seed (initial checksum) */ |
kadonotakashi | 0:8fdf9a60065b | 139 | base->DATA = config->seed; |
kadonotakashi | 0:8fdf9a60065b | 140 | |
kadonotakashi | 0:8fdf9a60065b | 141 | /* deassert WAS by writing pre-computed CRC control register value */ |
kadonotakashi | 0:8fdf9a60065b | 142 | base->CTRL = crcControl; |
kadonotakashi | 0:8fdf9a60065b | 143 | } |
kadonotakashi | 0:8fdf9a60065b | 144 | |
kadonotakashi | 0:8fdf9a60065b | 145 | /*! |
kadonotakashi | 0:8fdf9a60065b | 146 | * @brief Starts final checksum computation. |
kadonotakashi | 0:8fdf9a60065b | 147 | * |
kadonotakashi | 0:8fdf9a60065b | 148 | * Configures the CRC module for the specified CRC protocol. @n |
kadonotakashi | 0:8fdf9a60065b | 149 | * Starts final checksum computation by writing the seed value. |
kadonotakashi | 0:8fdf9a60065b | 150 | * @note CRC_Get16bitResult() or CRC_Get32bitResult() return final checksum |
kadonotakashi | 0:8fdf9a60065b | 151 | * (output reflection and xor functions are applied). |
kadonotakashi | 0:8fdf9a60065b | 152 | * |
kadonotakashi | 0:8fdf9a60065b | 153 | * @param base CRC peripheral address. |
kadonotakashi | 0:8fdf9a60065b | 154 | * @param protocolConfig Pointer to protocol configuration structure. |
kadonotakashi | 0:8fdf9a60065b | 155 | */ |
kadonotakashi | 0:8fdf9a60065b | 156 | static void CRC_SetProtocolConfig(CRC_Type *base, const crc_config_t *protocolConfig) |
kadonotakashi | 0:8fdf9a60065b | 157 | { |
kadonotakashi | 0:8fdf9a60065b | 158 | crc_module_config_t moduleConfig; |
kadonotakashi | 0:8fdf9a60065b | 159 | /* convert protocol to CRC peripheral module configuration, prepare for final checksum */ |
kadonotakashi | 0:8fdf9a60065b | 160 | moduleConfig.polynomial = protocolConfig->polynomial; |
kadonotakashi | 0:8fdf9a60065b | 161 | moduleConfig.seed = protocolConfig->seed; |
kadonotakashi | 0:8fdf9a60065b | 162 | moduleConfig.readTranspose = CRC_GetTransposeTypeFromReflectOut(protocolConfig->reflectOut); |
kadonotakashi | 0:8fdf9a60065b | 163 | moduleConfig.writeTranspose = CRC_GetTransposeTypeFromReflectIn(protocolConfig->reflectIn); |
kadonotakashi | 0:8fdf9a60065b | 164 | moduleConfig.complementChecksum = protocolConfig->complementChecksum; |
kadonotakashi | 0:8fdf9a60065b | 165 | moduleConfig.crcBits = protocolConfig->crcBits; |
kadonotakashi | 0:8fdf9a60065b | 166 | |
kadonotakashi | 0:8fdf9a60065b | 167 | CRC_ConfigureAndStart(base, &moduleConfig); |
kadonotakashi | 0:8fdf9a60065b | 168 | } |
kadonotakashi | 0:8fdf9a60065b | 169 | |
kadonotakashi | 0:8fdf9a60065b | 170 | /*! |
kadonotakashi | 0:8fdf9a60065b | 171 | * @brief Starts intermediate checksum computation. |
kadonotakashi | 0:8fdf9a60065b | 172 | * |
kadonotakashi | 0:8fdf9a60065b | 173 | * Configures the CRC module for the specified CRC protocol. @n |
kadonotakashi | 0:8fdf9a60065b | 174 | * Starts intermediate checksum computation by writing the seed value. |
kadonotakashi | 0:8fdf9a60065b | 175 | * @note CRC_Get16bitResult() or CRC_Get32bitResult() return intermediate checksum (raw data register value). |
kadonotakashi | 0:8fdf9a60065b | 176 | * |
kadonotakashi | 0:8fdf9a60065b | 177 | * @param base CRC peripheral address. |
kadonotakashi | 0:8fdf9a60065b | 178 | * @param protocolConfig Pointer to protocol configuration structure. |
kadonotakashi | 0:8fdf9a60065b | 179 | */ |
kadonotakashi | 0:8fdf9a60065b | 180 | static void CRC_SetRawProtocolConfig(CRC_Type *base, const crc_config_t *protocolConfig) |
kadonotakashi | 0:8fdf9a60065b | 181 | { |
kadonotakashi | 0:8fdf9a60065b | 182 | crc_module_config_t moduleConfig; |
kadonotakashi | 0:8fdf9a60065b | 183 | /* convert protocol to CRC peripheral module configuration, prepare for intermediate checksum */ |
kadonotakashi | 0:8fdf9a60065b | 184 | moduleConfig.polynomial = protocolConfig->polynomial; |
kadonotakashi | 0:8fdf9a60065b | 185 | moduleConfig.seed = protocolConfig->seed; |
kadonotakashi | 0:8fdf9a60065b | 186 | moduleConfig.readTranspose = |
kadonotakashi | 0:8fdf9a60065b | 187 | kCrcTransposeNone; /* intermediate checksum does no transpose of data register read value */ |
kadonotakashi | 0:8fdf9a60065b | 188 | moduleConfig.writeTranspose = CRC_GetTransposeTypeFromReflectIn(protocolConfig->reflectIn); |
kadonotakashi | 0:8fdf9a60065b | 189 | moduleConfig.complementChecksum = false; /* intermediate checksum does no xor of data register read value */ |
kadonotakashi | 0:8fdf9a60065b | 190 | moduleConfig.crcBits = protocolConfig->crcBits; |
kadonotakashi | 0:8fdf9a60065b | 191 | |
kadonotakashi | 0:8fdf9a60065b | 192 | CRC_ConfigureAndStart(base, &moduleConfig); |
kadonotakashi | 0:8fdf9a60065b | 193 | } |
kadonotakashi | 0:8fdf9a60065b | 194 | |
kadonotakashi | 0:8fdf9a60065b | 195 | void CRC_Init(CRC_Type *base, const crc_config_t *config) |
kadonotakashi | 0:8fdf9a60065b | 196 | { |
kadonotakashi | 0:8fdf9a60065b | 197 | #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) |
kadonotakashi | 0:8fdf9a60065b | 198 | /* ungate clock */ |
kadonotakashi | 0:8fdf9a60065b | 199 | CLOCK_EnableClock(kCLOCK_Crc0); |
kadonotakashi | 0:8fdf9a60065b | 200 | #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */ |
kadonotakashi | 0:8fdf9a60065b | 201 | /* configure CRC module and write the seed */ |
kadonotakashi | 0:8fdf9a60065b | 202 | if (config->crcResult == kCrcFinalChecksum) |
kadonotakashi | 0:8fdf9a60065b | 203 | { |
kadonotakashi | 0:8fdf9a60065b | 204 | CRC_SetProtocolConfig(base, config); |
kadonotakashi | 0:8fdf9a60065b | 205 | } |
kadonotakashi | 0:8fdf9a60065b | 206 | else |
kadonotakashi | 0:8fdf9a60065b | 207 | { |
kadonotakashi | 0:8fdf9a60065b | 208 | CRC_SetRawProtocolConfig(base, config); |
kadonotakashi | 0:8fdf9a60065b | 209 | } |
kadonotakashi | 0:8fdf9a60065b | 210 | } |
kadonotakashi | 0:8fdf9a60065b | 211 | |
kadonotakashi | 0:8fdf9a60065b | 212 | void CRC_GetDefaultConfig(crc_config_t *config) |
kadonotakashi | 0:8fdf9a60065b | 213 | { |
kadonotakashi | 0:8fdf9a60065b | 214 | static const crc_config_t crc16ccit = { |
kadonotakashi | 0:8fdf9a60065b | 215 | CRC_DRIVER_DEFAULT_POLYNOMIAL, CRC_DRIVER_DEFAULT_SEED, |
kadonotakashi | 0:8fdf9a60065b | 216 | CRC_DRIVER_DEFAULT_REFLECT_IN, CRC_DRIVER_DEFAULT_REFLECT_OUT, |
kadonotakashi | 0:8fdf9a60065b | 217 | CRC_DRIVER_DEFAULT_COMPLEMENT_CHECKSUM, CRC_DRIVER_DEFAULT_CRC_BITS, |
kadonotakashi | 0:8fdf9a60065b | 218 | CRC_DRIVER_DEFAULT_CRC_RESULT, |
kadonotakashi | 0:8fdf9a60065b | 219 | }; |
kadonotakashi | 0:8fdf9a60065b | 220 | |
kadonotakashi | 0:8fdf9a60065b | 221 | *config = crc16ccit; |
kadonotakashi | 0:8fdf9a60065b | 222 | } |
kadonotakashi | 0:8fdf9a60065b | 223 | |
kadonotakashi | 0:8fdf9a60065b | 224 | void CRC_WriteData(CRC_Type *base, const uint8_t *data, size_t dataSize) |
kadonotakashi | 0:8fdf9a60065b | 225 | { |
kadonotakashi | 0:8fdf9a60065b | 226 | const uint32_t *data32; |
kadonotakashi | 0:8fdf9a60065b | 227 | |
kadonotakashi | 0:8fdf9a60065b | 228 | /* 8-bit reads and writes till source address is aligned 4 bytes */ |
kadonotakashi | 0:8fdf9a60065b | 229 | while ((dataSize) && ((uint32_t)data & 3U)) |
kadonotakashi | 0:8fdf9a60065b | 230 | { |
kadonotakashi | 0:8fdf9a60065b | 231 | base->ACCESS8BIT.DATALL = *data; |
kadonotakashi | 0:8fdf9a60065b | 232 | data++; |
kadonotakashi | 0:8fdf9a60065b | 233 | dataSize--; |
kadonotakashi | 0:8fdf9a60065b | 234 | } |
kadonotakashi | 0:8fdf9a60065b | 235 | |
kadonotakashi | 0:8fdf9a60065b | 236 | /* use 32-bit reads and writes as long as possible */ |
kadonotakashi | 0:8fdf9a60065b | 237 | data32 = (const uint32_t *)data; |
kadonotakashi | 0:8fdf9a60065b | 238 | while (dataSize >= sizeof(uint32_t)) |
kadonotakashi | 0:8fdf9a60065b | 239 | { |
kadonotakashi | 0:8fdf9a60065b | 240 | base->DATA = *data32; |
kadonotakashi | 0:8fdf9a60065b | 241 | data32++; |
kadonotakashi | 0:8fdf9a60065b | 242 | dataSize -= sizeof(uint32_t); |
kadonotakashi | 0:8fdf9a60065b | 243 | } |
kadonotakashi | 0:8fdf9a60065b | 244 | |
kadonotakashi | 0:8fdf9a60065b | 245 | data = (const uint8_t *)data32; |
kadonotakashi | 0:8fdf9a60065b | 246 | |
kadonotakashi | 0:8fdf9a60065b | 247 | /* 8-bit reads and writes till end of data buffer */ |
kadonotakashi | 0:8fdf9a60065b | 248 | while (dataSize) |
kadonotakashi | 0:8fdf9a60065b | 249 | { |
kadonotakashi | 0:8fdf9a60065b | 250 | base->ACCESS8BIT.DATALL = *data; |
kadonotakashi | 0:8fdf9a60065b | 251 | data++; |
kadonotakashi | 0:8fdf9a60065b | 252 | dataSize--; |
kadonotakashi | 0:8fdf9a60065b | 253 | } |
kadonotakashi | 0:8fdf9a60065b | 254 | } |
kadonotakashi | 0:8fdf9a60065b | 255 | |
kadonotakashi | 0:8fdf9a60065b | 256 | uint32_t CRC_Get32bitResult(CRC_Type *base) |
kadonotakashi | 0:8fdf9a60065b | 257 | { |
kadonotakashi | 0:8fdf9a60065b | 258 | return base->DATA; |
kadonotakashi | 0:8fdf9a60065b | 259 | } |
kadonotakashi | 0:8fdf9a60065b | 260 | |
kadonotakashi | 0:8fdf9a60065b | 261 | uint16_t CRC_Get16bitResult(CRC_Type *base) |
kadonotakashi | 0:8fdf9a60065b | 262 | { |
kadonotakashi | 0:8fdf9a60065b | 263 | uint32_t retval; |
kadonotakashi | 0:8fdf9a60065b | 264 | uint32_t totr; /* type of transpose read bitfield */ |
kadonotakashi | 0:8fdf9a60065b | 265 | |
kadonotakashi | 0:8fdf9a60065b | 266 | retval = base->DATA; |
kadonotakashi | 0:8fdf9a60065b | 267 | totr = (base->CTRL & CRC_CTRL_TOTR_MASK) >> CRC_CTRL_TOTR_SHIFT; |
kadonotakashi | 0:8fdf9a60065b | 268 | |
kadonotakashi | 0:8fdf9a60065b | 269 | /* check transpose type to get 16-bit out of 32-bit register */ |
kadonotakashi | 0:8fdf9a60065b | 270 | if (totr >= 2U) |
kadonotakashi | 0:8fdf9a60065b | 271 | { |
kadonotakashi | 0:8fdf9a60065b | 272 | /* transpose of bytes for read is set, the result CRC is in CRC_DATA[HU:HL] */ |
kadonotakashi | 0:8fdf9a60065b | 273 | retval &= 0xFFFF0000U; |
kadonotakashi | 0:8fdf9a60065b | 274 | retval = retval >> 16U; |
kadonotakashi | 0:8fdf9a60065b | 275 | } |
kadonotakashi | 0:8fdf9a60065b | 276 | else |
kadonotakashi | 0:8fdf9a60065b | 277 | { |
kadonotakashi | 0:8fdf9a60065b | 278 | /* no transpose of bytes for read, the result CRC is in CRC_DATA[LU:LL] */ |
kadonotakashi | 0:8fdf9a60065b | 279 | retval &= 0x0000FFFFU; |
kadonotakashi | 0:8fdf9a60065b | 280 | } |
kadonotakashi | 0:8fdf9a60065b | 281 | return (uint16_t)retval; |
kadonotakashi | 0:8fdf9a60065b | 282 | } |