1-Wire® library for mbed. Complete 1-Wire library that supports our silicon masters along with a bit-bang master on the MAX32600MBED platform with one common interface for mbed. Slave support has also been included and more slaves will be added as time permits.

Dependents:   MAXREFDES131_Qt_Demo MAX32630FTHR_iButton_uSD_Logger MAX32630FTHR_DS18B20_uSD_Logger MAXREFDES130_131_Demo ... more

Superseded by MaximInterface.

Committer:
IanBenzMaxim
Date:
Sat May 14 14:27:56 2016 -0500
Revision:
76:84e6c4994e29
Parent:
75:8b627804927c
Child:
77:529edb329ee0
Move ROM commands outside of OneWireMaster to increase cohesiveness of the class. Do not use subdivide OneWire namespace since it will likely not provide value on this project.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
j3 5:ce108eeb878d 1 /******************************************************************//**
j3 5:ce108eeb878d 2 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
j3 5:ce108eeb878d 3 *
j3 5:ce108eeb878d 4 * Permission is hereby granted, free of charge, to any person obtaining a
j3 5:ce108eeb878d 5 * copy of this software and associated documentation files (the "Software"),
j3 5:ce108eeb878d 6 * to deal in the Software without restriction, including without limitation
j3 5:ce108eeb878d 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
j3 5:ce108eeb878d 8 * and/or sell copies of the Software, and to permit persons to whom the
j3 5:ce108eeb878d 9 * Software is furnished to do so, subject to the following conditions:
j3 5:ce108eeb878d 10 *
j3 5:ce108eeb878d 11 * The above copyright notice and this permission notice shall be included
j3 5:ce108eeb878d 12 * in all copies or substantial portions of the Software.
j3 5:ce108eeb878d 13 *
j3 5:ce108eeb878d 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
j3 5:ce108eeb878d 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
j3 5:ce108eeb878d 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
j3 5:ce108eeb878d 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
j3 5:ce108eeb878d 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
j3 5:ce108eeb878d 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
j3 5:ce108eeb878d 20 * OTHER DEALINGS IN THE SOFTWARE.
j3 5:ce108eeb878d 21 *
j3 5:ce108eeb878d 22 * Except as contained in this notice, the name of Maxim Integrated
j3 5:ce108eeb878d 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
j3 5:ce108eeb878d 24 * Products, Inc. Branding Policy.
j3 5:ce108eeb878d 25 *
j3 5:ce108eeb878d 26 * The mere transfer of this software does not imply any licenses
j3 5:ce108eeb878d 27 * of trade secrets, proprietary technology, copyrights, patents,
j3 5:ce108eeb878d 28 * trademarks, maskwork rights, or any other form of intellectual
j3 5:ce108eeb878d 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
j3 5:ce108eeb878d 30 * ownership rights.
j3 5:ce108eeb878d 31 **********************************************************************/
j3 5:ce108eeb878d 32
j3 15:f6cb0d906fb6 33 #include "OneWireMaster.h"
IanBenzMaxim 73:2cecc1372acc 34 #include "RomId.h"
IanBenzMaxim 27:d5aaefa252f1 35
IanBenzMaxim 76:84e6c4994e29 36 using OneWire::OneWireMaster;
j3 15:f6cb0d906fb6 37
IanBenzMaxim 75:8b627804927c 38 OneWireMaster::CmdResult OneWireMaster::OWWriteBlock(const uint8_t *sendBuf, uint8_t sendLen)
IanBenzMaxim 71:562f5c702094 39 {
IanBenzMaxim 71:562f5c702094 40 CmdResult result;
IanBenzMaxim 74:23be10c32fa3 41
IanBenzMaxim 75:8b627804927c 42 for (uint8_t idx = 0; idx < sendLen; idx++)
IanBenzMaxim 71:562f5c702094 43 {
IanBenzMaxim 75:8b627804927c 44 result = OWWriteByte(sendBuf[idx]);
IanBenzMaxim 74:23be10c32fa3 45 if (result != Success)
IanBenzMaxim 71:562f5c702094 46 {
IanBenzMaxim 71:562f5c702094 47 break;
IanBenzMaxim 71:562f5c702094 48 }
IanBenzMaxim 71:562f5c702094 49 }
IanBenzMaxim 74:23be10c32fa3 50
IanBenzMaxim 71:562f5c702094 51 return result;
IanBenzMaxim 71:562f5c702094 52 }
IanBenzMaxim 71:562f5c702094 53
IanBenzMaxim 75:8b627804927c 54 OneWireMaster::CmdResult OneWireMaster::OWReadBlock(uint8_t *recvBuf, uint8_t recvLen)
IanBenzMaxim 71:562f5c702094 55 {
IanBenzMaxim 71:562f5c702094 56 CmdResult result;
IanBenzMaxim 74:23be10c32fa3 57
IanBenzMaxim 75:8b627804927c 58 for (uint8_t idx = 0; idx < recvLen; idx++)
IanBenzMaxim 71:562f5c702094 59 {
IanBenzMaxim 75:8b627804927c 60 result = OWReadByte(recvBuf[idx]);
IanBenzMaxim 74:23be10c32fa3 61 if (result != Success)
IanBenzMaxim 71:562f5c702094 62 {
IanBenzMaxim 71:562f5c702094 63 break;
IanBenzMaxim 71:562f5c702094 64 }
IanBenzMaxim 71:562f5c702094 65 }
IanBenzMaxim 74:23be10c32fa3 66
IanBenzMaxim 71:562f5c702094 67 return result;
IanBenzMaxim 71:562f5c702094 68 }
IanBenzMaxim 71:562f5c702094 69
IanBenzMaxim 75:8b627804927c 70 OneWireMaster::CmdResult OneWireMaster::OWTriplet(SearchDirection & searchDirection, uint8_t & sbr, uint8_t & tsb)
IanBenzMaxim 32:bce180b544ed 71 {
IanBenzMaxim 32:bce180b544ed 72 CmdResult result;
IanBenzMaxim 32:bce180b544ed 73 result = OWReadBit(sbr);
IanBenzMaxim 32:bce180b544ed 74 if (result == Success)
IanBenzMaxim 74:23be10c32fa3 75 {
IanBenzMaxim 32:bce180b544ed 76 result = OWReadBit(tsb);
IanBenzMaxim 74:23be10c32fa3 77 }
IanBenzMaxim 32:bce180b544ed 78 if (result == Success)
IanBenzMaxim 32:bce180b544ed 79 {
IanBenzMaxim 57:1635f247ceae 80 if (sbr)
IanBenzMaxim 74:23be10c32fa3 81 {
IanBenzMaxim 75:8b627804927c 82 searchDirection = WriteOne;
IanBenzMaxim 74:23be10c32fa3 83 }
IanBenzMaxim 57:1635f247ceae 84 else if (tsb)
IanBenzMaxim 74:23be10c32fa3 85 {
IanBenzMaxim 75:8b627804927c 86 searchDirection = WriteZero;
IanBenzMaxim 74:23be10c32fa3 87 }
IanBenzMaxim 32:bce180b544ed 88 // else: use search_direction parameter
IanBenzMaxim 74:23be10c32fa3 89
IanBenzMaxim 75:8b627804927c 90 result = OWWriteBit((searchDirection == WriteOne) ? 1 : 0);
IanBenzMaxim 32:bce180b544ed 91 }
IanBenzMaxim 32:bce180b544ed 92 return result;
IanBenzMaxim 32:bce180b544ed 93 }
IanBenzMaxim 32:bce180b544ed 94
IanBenzMaxim 75:8b627804927c 95 uint16_t OneWireMaster::calculateCrc16(uint16_t crc16, uint16_t data)
j3 15:f6cb0d906fb6 96 {
IanBenzMaxim 75:8b627804927c 97 const uint16_t oddparity[] = { 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0 };
IanBenzMaxim 74:23be10c32fa3 98
IanBenzMaxim 75:8b627804927c 99 data = (data ^ (crc16 & 0xff)) & 0xff;
IanBenzMaxim 75:8b627804927c 100 crc16 >>= 8;
j3 17:b646b1e3970b 101
IanBenzMaxim 75:8b627804927c 102 if (oddparity[data & 0xf] ^ oddparity[data >> 4])
IanBenzMaxim 74:23be10c32fa3 103 {
IanBenzMaxim 75:8b627804927c 104 crc16 ^= 0xc001;
IanBenzMaxim 74:23be10c32fa3 105 }
j3 17:b646b1e3970b 106
IanBenzMaxim 74:23be10c32fa3 107 data <<= 6;
IanBenzMaxim 75:8b627804927c 108 crc16 ^= data;
IanBenzMaxim 74:23be10c32fa3 109 data <<= 1;
IanBenzMaxim 75:8b627804927c 110 crc16 ^= data;
j3 17:b646b1e3970b 111
IanBenzMaxim 75:8b627804927c 112 return crc16;
j3 15:f6cb0d906fb6 113 }
j3 15:f6cb0d906fb6 114
IanBenzMaxim 75:8b627804927c 115 uint16_t OneWireMaster::calculateCrc16(const uint8_t * data, size_t dataOffset, size_t dataLen, uint16_t crc)
IanBenzMaxim 74:23be10c32fa3 116 {
IanBenzMaxim 75:8b627804927c 117 for (size_t i = dataOffset; i < (dataLen + dataOffset); i++)
IanBenzMaxim 74:23be10c32fa3 118 {
IanBenzMaxim 75:8b627804927c 119 crc = calculateCrc16(crc, data[i]);
IanBenzMaxim 74:23be10c32fa3 120 }
IanBenzMaxim 74:23be10c32fa3 121 return crc;
IanBenzMaxim 48:6f9208ae280e 122 }