Repostiory containing DAPLink source code with Reset Pin workaround for HANI_IOT board.

Upstream: https://github.com/ARMmbed/DAPLink

Committer:
Pawel Zarembski
Date:
Tue Apr 07 12:55:42 2020 +0200
Revision:
0:01f31e923fe2
hani: DAPLink with reset workaround

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Pawel Zarembski 0:01f31e923fe2 1 /**********************************************************************
Pawel Zarembski 0:01f31e923fe2 2 *
Pawel Zarembski 0:01f31e923fe2 3 * Filename: crc.c
Pawel Zarembski 0:01f31e923fe2 4 *
Pawel Zarembski 0:01f31e923fe2 5 * Description: Slow and fast implementations of the CRC standards.
Pawel Zarembski 0:01f31e923fe2 6 *
Pawel Zarembski 0:01f31e923fe2 7 * Notes: The parameters for each supported CRC standard are
Pawel Zarembski 0:01f31e923fe2 8 * defined in the header file crc.h. The implementations
Pawel Zarembski 0:01f31e923fe2 9 * here should stand up to further additions to that list.
Pawel Zarembski 0:01f31e923fe2 10 *
Pawel Zarembski 0:01f31e923fe2 11 *
Pawel Zarembski 0:01f31e923fe2 12 * Copyright (c) 2000 by Michael Barr. This software is placed into
Pawel Zarembski 0:01f31e923fe2 13 * the public domain and may be used for any purpose. However, this
Pawel Zarembski 0:01f31e923fe2 14 * notice must not be changed or removed and no warranty is either
Pawel Zarembski 0:01f31e923fe2 15 * expressed or implied by its publication or distribution.
Pawel Zarembski 0:01f31e923fe2 16 **********************************************************************/
Pawel Zarembski 0:01f31e923fe2 17
Pawel Zarembski 0:01f31e923fe2 18 /**
Pawel Zarembski 0:01f31e923fe2 19 * @file crc32.c
Pawel Zarembski 0:01f31e923fe2 20 * @brief Implementation of crc.h
Pawel Zarembski 0:01f31e923fe2 21 */
Pawel Zarembski 0:01f31e923fe2 22
Pawel Zarembski 0:01f31e923fe2 23 #include "crc.h"
Pawel Zarembski 0:01f31e923fe2 24
Pawel Zarembski 0:01f31e923fe2 25 #define FALSE 0
Pawel Zarembski 0:01f31e923fe2 26 #define TRUE !FALSE
Pawel Zarembski 0:01f31e923fe2 27
Pawel Zarembski 0:01f31e923fe2 28 typedef unsigned long crc;
Pawel Zarembski 0:01f31e923fe2 29
Pawel Zarembski 0:01f31e923fe2 30 #define CRC_NAME "CRC-32"
Pawel Zarembski 0:01f31e923fe2 31 #define POLYNOMIAL 0x04C11DB7
Pawel Zarembski 0:01f31e923fe2 32 #define INITIAL_REMAINDER 0xFFFFFFFF
Pawel Zarembski 0:01f31e923fe2 33 #define FINAL_XOR_VALUE 0xFFFFFFFF
Pawel Zarembski 0:01f31e923fe2 34 #define REFLECT_DATA TRUE
Pawel Zarembski 0:01f31e923fe2 35 #define REFLECT_REMAINDER TRUE
Pawel Zarembski 0:01f31e923fe2 36 #define CHECK_VALUE 0xCBF43926
Pawel Zarembski 0:01f31e923fe2 37
Pawel Zarembski 0:01f31e923fe2 38 /*
Pawel Zarembski 0:01f31e923fe2 39 * Derive parameters from the standard-specific parameters in crc.h.
Pawel Zarembski 0:01f31e923fe2 40 */
Pawel Zarembski 0:01f31e923fe2 41 #define WIDTH (8 * sizeof(crc))
Pawel Zarembski 0:01f31e923fe2 42 #define TOPBIT (1U << (WIDTH - 1))
Pawel Zarembski 0:01f31e923fe2 43
Pawel Zarembski 0:01f31e923fe2 44 #if (REFLECT_DATA == TRUE)
Pawel Zarembski 0:01f31e923fe2 45 #undef REFLECT_DATA
Pawel Zarembski 0:01f31e923fe2 46 #define REFLECT_DATA(X) ((unsigned char) reflect((X), 8))
Pawel Zarembski 0:01f31e923fe2 47 #else
Pawel Zarembski 0:01f31e923fe2 48 #undef REFLECT_DATA
Pawel Zarembski 0:01f31e923fe2 49 #define REFLECT_DATA(X) (X)
Pawel Zarembski 0:01f31e923fe2 50 #endif
Pawel Zarembski 0:01f31e923fe2 51
Pawel Zarembski 0:01f31e923fe2 52 #if (REFLECT_REMAINDER == TRUE)
Pawel Zarembski 0:01f31e923fe2 53 #undef REFLECT_REMAINDER
Pawel Zarembski 0:01f31e923fe2 54 #define REFLECT_REMAINDER(X) ((crc) reflect((X), WIDTH))
Pawel Zarembski 0:01f31e923fe2 55 #else
Pawel Zarembski 0:01f31e923fe2 56 #undef REFLECT_REMAINDER
Pawel Zarembski 0:01f31e923fe2 57 #define REFLECT_REMAINDER(X) (X)
Pawel Zarembski 0:01f31e923fe2 58 #endif
Pawel Zarembski 0:01f31e923fe2 59
Pawel Zarembski 0:01f31e923fe2 60
Pawel Zarembski 0:01f31e923fe2 61 /*********************************************************************
Pawel Zarembski 0:01f31e923fe2 62 *
Pawel Zarembski 0:01f31e923fe2 63 * Function: reflect()
Pawel Zarembski 0:01f31e923fe2 64 *
Pawel Zarembski 0:01f31e923fe2 65 * Description: Reorder the bits of a binary sequence, by reflecting
Pawel Zarembski 0:01f31e923fe2 66 * them about the middle position.
Pawel Zarembski 0:01f31e923fe2 67 *
Pawel Zarembski 0:01f31e923fe2 68 * Notes: No checking is done that nBits <= 32.
Pawel Zarembski 0:01f31e923fe2 69 *
Pawel Zarembski 0:01f31e923fe2 70 * Returns: The reflection of the original data.
Pawel Zarembski 0:01f31e923fe2 71 *
Pawel Zarembski 0:01f31e923fe2 72 *********************************************************************/
Pawel Zarembski 0:01f31e923fe2 73 static unsigned long
Pawel Zarembski 0:01f31e923fe2 74 reflect(unsigned long data, unsigned char nBits)
Pawel Zarembski 0:01f31e923fe2 75 {
Pawel Zarembski 0:01f31e923fe2 76 unsigned long reflection = 0x00000000;
Pawel Zarembski 0:01f31e923fe2 77 unsigned char bit;
Pawel Zarembski 0:01f31e923fe2 78
Pawel Zarembski 0:01f31e923fe2 79 /*
Pawel Zarembski 0:01f31e923fe2 80 * Reflect the data about the center bit.
Pawel Zarembski 0:01f31e923fe2 81 */
Pawel Zarembski 0:01f31e923fe2 82 for (bit = 0; bit < nBits; ++bit) {
Pawel Zarembski 0:01f31e923fe2 83 /*
Pawel Zarembski 0:01f31e923fe2 84 * If the LSB bit is set, set the reflection of it.
Pawel Zarembski 0:01f31e923fe2 85 */
Pawel Zarembski 0:01f31e923fe2 86 if (data & 0x01) {
Pawel Zarembski 0:01f31e923fe2 87 reflection |= (1 << ((nBits - 1) - bit));
Pawel Zarembski 0:01f31e923fe2 88 }
Pawel Zarembski 0:01f31e923fe2 89
Pawel Zarembski 0:01f31e923fe2 90 data = (data >> 1);
Pawel Zarembski 0:01f31e923fe2 91 }
Pawel Zarembski 0:01f31e923fe2 92
Pawel Zarembski 0:01f31e923fe2 93 return (reflection);
Pawel Zarembski 0:01f31e923fe2 94 } /* reflect() */
Pawel Zarembski 0:01f31e923fe2 95
Pawel Zarembski 0:01f31e923fe2 96
Pawel Zarembski 0:01f31e923fe2 97 /*********************************************************************
Pawel Zarembski 0:01f31e923fe2 98 *
Pawel Zarembski 0:01f31e923fe2 99 * Function: crc32()
Pawel Zarembski 0:01f31e923fe2 100 *
Pawel Zarembski 0:01f31e923fe2 101 * Description: Compute the CRC of a given message.
Pawel Zarembski 0:01f31e923fe2 102 *
Pawel Zarembski 0:01f31e923fe2 103 * Notes:
Pawel Zarembski 0:01f31e923fe2 104 *
Pawel Zarembski 0:01f31e923fe2 105 * Returns: The CRC of the message.
Pawel Zarembski 0:01f31e923fe2 106 *
Pawel Zarembski 0:01f31e923fe2 107 *********************************************************************/
Pawel Zarembski 0:01f31e923fe2 108 uint32_t
Pawel Zarembski 0:01f31e923fe2 109 crc32(const void *data, int nBytes)
Pawel Zarembski 0:01f31e923fe2 110 {
Pawel Zarembski 0:01f31e923fe2 111 crc remainder = INITIAL_REMAINDER;
Pawel Zarembski 0:01f31e923fe2 112 int byte;
Pawel Zarembski 0:01f31e923fe2 113 unsigned char bit;
Pawel Zarembski 0:01f31e923fe2 114 unsigned char const *message = data;
Pawel Zarembski 0:01f31e923fe2 115
Pawel Zarembski 0:01f31e923fe2 116 /*
Pawel Zarembski 0:01f31e923fe2 117 * Perform modulo-2 division, a byte at a time.
Pawel Zarembski 0:01f31e923fe2 118 */
Pawel Zarembski 0:01f31e923fe2 119 for (byte = 0; byte < nBytes; ++byte) {
Pawel Zarembski 0:01f31e923fe2 120 /*
Pawel Zarembski 0:01f31e923fe2 121 * Bring the next byte into the remainder.
Pawel Zarembski 0:01f31e923fe2 122 */
Pawel Zarembski 0:01f31e923fe2 123 remainder ^= (REFLECT_DATA(message[byte]) << (WIDTH - 8));
Pawel Zarembski 0:01f31e923fe2 124
Pawel Zarembski 0:01f31e923fe2 125 /*
Pawel Zarembski 0:01f31e923fe2 126 * Perform modulo-2 division, a bit at a time.
Pawel Zarembski 0:01f31e923fe2 127 */
Pawel Zarembski 0:01f31e923fe2 128 for (bit = 8; bit > 0; --bit) {
Pawel Zarembski 0:01f31e923fe2 129 /*
Pawel Zarembski 0:01f31e923fe2 130 * Try to divide the current data bit.
Pawel Zarembski 0:01f31e923fe2 131 */
Pawel Zarembski 0:01f31e923fe2 132 if (remainder & TOPBIT) {
Pawel Zarembski 0:01f31e923fe2 133 remainder = (remainder << 1) ^ POLYNOMIAL;
Pawel Zarembski 0:01f31e923fe2 134 } else {
Pawel Zarembski 0:01f31e923fe2 135 remainder = (remainder << 1);
Pawel Zarembski 0:01f31e923fe2 136 }
Pawel Zarembski 0:01f31e923fe2 137 }
Pawel Zarembski 0:01f31e923fe2 138 }
Pawel Zarembski 0:01f31e923fe2 139
Pawel Zarembski 0:01f31e923fe2 140 /*
Pawel Zarembski 0:01f31e923fe2 141 * The final remainder is the CRC result.
Pawel Zarembski 0:01f31e923fe2 142 */
Pawel Zarembski 0:01f31e923fe2 143 return (REFLECT_REMAINDER(remainder) ^ FINAL_XOR_VALUE);
Pawel Zarembski 0:01f31e923fe2 144 } /* crc32() */
Pawel Zarembski 0:01f31e923fe2 145
Pawel Zarembski 0:01f31e923fe2 146 /*********************************************************************
Pawel Zarembski 0:01f31e923fe2 147 *
Pawel Zarembski 0:01f31e923fe2 148 * Function: crc32_continue()
Pawel Zarembski 0:01f31e923fe2 149 *
Pawel Zarembski 0:01f31e923fe2 150 * Description: Compute the CRC of a given message.
Pawel Zarembski 0:01f31e923fe2 151 *
Pawel Zarembski 0:01f31e923fe2 152 * Notes:
Pawel Zarembski 0:01f31e923fe2 153 *
Pawel Zarembski 0:01f31e923fe2 154 * Returns: The CRC of the message.
Pawel Zarembski 0:01f31e923fe2 155 *
Pawel Zarembski 0:01f31e923fe2 156 *********************************************************************/
Pawel Zarembski 0:01f31e923fe2 157 uint32_t
Pawel Zarembski 0:01f31e923fe2 158 crc32_continue(uint32_t prev_crc, const void *data, int nBytes)
Pawel Zarembski 0:01f31e923fe2 159 {
Pawel Zarembski 0:01f31e923fe2 160 crc remainder = REFLECT_REMAINDER(prev_crc ^ FINAL_XOR_VALUE);
Pawel Zarembski 0:01f31e923fe2 161 int byte;
Pawel Zarembski 0:01f31e923fe2 162 unsigned char bit;
Pawel Zarembski 0:01f31e923fe2 163 unsigned char const *message = data;
Pawel Zarembski 0:01f31e923fe2 164
Pawel Zarembski 0:01f31e923fe2 165 /*
Pawel Zarembski 0:01f31e923fe2 166 * Perform modulo-2 division, a byte at a time.
Pawel Zarembski 0:01f31e923fe2 167 */
Pawel Zarembski 0:01f31e923fe2 168 for (byte = 0; byte < nBytes; ++byte) {
Pawel Zarembski 0:01f31e923fe2 169 /*
Pawel Zarembski 0:01f31e923fe2 170 * Bring the next byte into the remainder.
Pawel Zarembski 0:01f31e923fe2 171 */
Pawel Zarembski 0:01f31e923fe2 172 remainder ^= (REFLECT_DATA(message[byte]) << (WIDTH - 8));
Pawel Zarembski 0:01f31e923fe2 173
Pawel Zarembski 0:01f31e923fe2 174 /*
Pawel Zarembski 0:01f31e923fe2 175 * Perform modulo-2 division, a bit at a time.
Pawel Zarembski 0:01f31e923fe2 176 */
Pawel Zarembski 0:01f31e923fe2 177 for (bit = 8; bit > 0; --bit) {
Pawel Zarembski 0:01f31e923fe2 178 /*
Pawel Zarembski 0:01f31e923fe2 179 * Try to divide the current data bit.
Pawel Zarembski 0:01f31e923fe2 180 */
Pawel Zarembski 0:01f31e923fe2 181 if (remainder & TOPBIT) {
Pawel Zarembski 0:01f31e923fe2 182 remainder = (remainder << 1) ^ POLYNOMIAL;
Pawel Zarembski 0:01f31e923fe2 183 } else {
Pawel Zarembski 0:01f31e923fe2 184 remainder = (remainder << 1);
Pawel Zarembski 0:01f31e923fe2 185 }
Pawel Zarembski 0:01f31e923fe2 186 }
Pawel Zarembski 0:01f31e923fe2 187 }
Pawel Zarembski 0:01f31e923fe2 188
Pawel Zarembski 0:01f31e923fe2 189 /*
Pawel Zarembski 0:01f31e923fe2 190 * The final remainder is the CRC result.
Pawel Zarembski 0:01f31e923fe2 191 */
Pawel Zarembski 0:01f31e923fe2 192 return (REFLECT_REMAINDER(remainder) ^ FINAL_XOR_VALUE);
Pawel Zarembski 0:01f31e923fe2 193 } /* crc32_continue() */