Maxim Integrated / Mbed OS MAXREFDES220#

Dependencies:   USBDevice max32630fthr

Committer:
Shaun Kelsey
Date:
Thu May 24 14:45:27 2018 -0700
Revision:
6:85ac8bf9955e
Parent:
0:da5f5b56060a
Add LEDStatus and EventStats

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Shaun Kelsey 0:da5f5b56060a 1 /***************************************************************************
Shaun Kelsey 0:da5f5b56060a 2 * Copyright (C) 2017 Maxim Integrated Products, Inc., All Rights Reserved.
Shaun Kelsey 0:da5f5b56060a 3 *
Shaun Kelsey 0:da5f5b56060a 4 * Permission is hereby granted, free of charge, to any person obtaining a
Shaun Kelsey 0:da5f5b56060a 5 * copy of this software and associated documentation files (the "Software"),
Shaun Kelsey 0:da5f5b56060a 6 * to deal in the Software without restriction, including without limitation
Shaun Kelsey 0:da5f5b56060a 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
Shaun Kelsey 0:da5f5b56060a 8 * and/or sell copies of the Software, and to permit persons to whom the
Shaun Kelsey 0:da5f5b56060a 9 * Software is furnished to do so, subject to the following conditions:
Shaun Kelsey 0:da5f5b56060a 10 *
Shaun Kelsey 0:da5f5b56060a 11 * The above copyright notice and this permission notice shall be included
Shaun Kelsey 0:da5f5b56060a 12 * in all copies or substantial portions of the Software.
Shaun Kelsey 0:da5f5b56060a 13 *
Shaun Kelsey 0:da5f5b56060a 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
Shaun Kelsey 0:da5f5b56060a 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
Shaun Kelsey 0:da5f5b56060a 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
Shaun Kelsey 0:da5f5b56060a 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
Shaun Kelsey 0:da5f5b56060a 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
Shaun Kelsey 0:da5f5b56060a 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
Shaun Kelsey 0:da5f5b56060a 20 * OTHER DEALINGS IN THE SOFTWARE.
Shaun Kelsey 0:da5f5b56060a 21 *
Shaun Kelsey 0:da5f5b56060a 22 * Except as contained in this notice, the name of Maxim Integrated
Shaun Kelsey 0:da5f5b56060a 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
Shaun Kelsey 0:da5f5b56060a 24 * Products, Inc. Branding Policy.
Shaun Kelsey 0:da5f5b56060a 25 *
Shaun Kelsey 0:da5f5b56060a 26 * The mere transfer of this software does not imply any licenses
Shaun Kelsey 0:da5f5b56060a 27 * of trade secrets, proprietary technology, copyrights, patents,
Shaun Kelsey 0:da5f5b56060a 28 * trademarks, maskwork rights, or any other form of intellectual
Shaun Kelsey 0:da5f5b56060a 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
Shaun Kelsey 0:da5f5b56060a 30 * ownership rights.
Shaun Kelsey 0:da5f5b56060a 31 ****************************************************************************
Shaun Kelsey 0:da5f5b56060a 32 */
Shaun Kelsey 0:da5f5b56060a 33 #include <stdint.h>
Shaun Kelsey 0:da5f5b56060a 34
Shaun Kelsey 0:da5f5b56060a 35 /*
Shaun Kelsey 0:da5f5b56060a 36 * CRC-8:
Shaun Kelsey 0:da5f5b56060a 37 *
Shaun Kelsey 0:da5f5b56060a 38 * Width: 8
Shaun Kelsey 0:da5f5b56060a 39 * Poly: 0x07
Shaun Kelsey 0:da5f5b56060a 40 * Init: 0x00
Shaun Kelsey 0:da5f5b56060a 41 * RefIn: False
Shaun Kelsey 0:da5f5b56060a 42 * RefOut: False
Shaun Kelsey 0:da5f5b56060a 43 * XOROur: 0x00
Shaun Kelsey 0:da5f5b56060a 44 * Check: 0xF4
Shaun Kelsey 0:da5f5b56060a 45 */
Shaun Kelsey 0:da5f5b56060a 46
Shaun Kelsey 0:da5f5b56060a 47 static const uint8_t CRC8_POLY = 0x07;
Shaun Kelsey 0:da5f5b56060a 48 static bool crc8_initialized = false;
Shaun Kelsey 0:da5f5b56060a 49
Shaun Kelsey 0:da5f5b56060a 50 static uint8_t crc8_table[256];
Shaun Kelsey 0:da5f5b56060a 51
Shaun Kelsey 0:da5f5b56060a 52
Shaun Kelsey 0:da5f5b56060a 53 static void init_crc8()
Shaun Kelsey 0:da5f5b56060a 54 {
Shaun Kelsey 0:da5f5b56060a 55 uint8_t msb = 1 << 7;
Shaun Kelsey 0:da5f5b56060a 56 uint8_t bit;
Shaun Kelsey 0:da5f5b56060a 57 uint8_t rem;
Shaun Kelsey 0:da5f5b56060a 58 uint32_t byte = 0;
Shaun Kelsey 0:da5f5b56060a 59
Shaun Kelsey 0:da5f5b56060a 60 if (crc8_initialized)
Shaun Kelsey 0:da5f5b56060a 61 return;
Shaun Kelsey 0:da5f5b56060a 62
Shaun Kelsey 0:da5f5b56060a 63 while (byte < 256) {
Shaun Kelsey 0:da5f5b56060a 64 rem = byte;
Shaun Kelsey 0:da5f5b56060a 65 for (bit = 0; bit < 8; bit++) {
Shaun Kelsey 0:da5f5b56060a 66 if (rem & msb)
Shaun Kelsey 0:da5f5b56060a 67 rem = (rem << 1) ^ CRC8_POLY;
Shaun Kelsey 0:da5f5b56060a 68 else
Shaun Kelsey 0:da5f5b56060a 69 rem <<= 1;
Shaun Kelsey 0:da5f5b56060a 70 }
Shaun Kelsey 0:da5f5b56060a 71
Shaun Kelsey 0:da5f5b56060a 72 crc8_table[byte++] = rem;
Shaun Kelsey 0:da5f5b56060a 73 }
Shaun Kelsey 0:da5f5b56060a 74
Shaun Kelsey 0:da5f5b56060a 75 crc8_initialized = true;
Shaun Kelsey 0:da5f5b56060a 76 }
Shaun Kelsey 0:da5f5b56060a 77
Shaun Kelsey 0:da5f5b56060a 78 uint8_t crc8(uint8_t *data, uint32_t length)
Shaun Kelsey 0:da5f5b56060a 79 {
Shaun Kelsey 0:da5f5b56060a 80 uint8_t crc = 0x00;
Shaun Kelsey 0:da5f5b56060a 81 uint32_t msg_idx = 0;
Shaun Kelsey 0:da5f5b56060a 82
Shaun Kelsey 0:da5f5b56060a 83 if (length == 0xFFFFFFFF)
Shaun Kelsey 0:da5f5b56060a 84 return 0;
Shaun Kelsey 0:da5f5b56060a 85
Shaun Kelsey 0:da5f5b56060a 86 init_crc8();
Shaun Kelsey 0:da5f5b56060a 87
Shaun Kelsey 0:da5f5b56060a 88 while (msg_idx < length) {
Shaun Kelsey 0:da5f5b56060a 89 crc = crc8_table[crc ^ data[msg_idx++]];
Shaun Kelsey 0:da5f5b56060a 90 }
Shaun Kelsey 0:da5f5b56060a 91
Shaun Kelsey 0:da5f5b56060a 92 return crc;
Shaun Kelsey 0:da5f5b56060a 93 }