Zeroday Hong / mbed-dev

Fork of mbed-dev by mbed official

Committer:
<>
Date:
Wed Jan 04 16:58:05 2017 +0000
Revision:
154:37f96f9d4de2
This updates the lib to the mbed lib v133

Who changed what in which revision?

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