Extended MaximInterface

Dependents:   mbed_DS28EC20_GPIO

Committer:
IanBenzMaxim
Date:
Wed Jan 23 13:11:04 2019 -0600
Revision:
6:a8c83a2e6fa4
Parent:
0:f77ad7f72d04
Updated to version 1.6 and removed platform files that are incompatible with mbed.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
IanBenzMaxim 0:f77ad7f72d04 1 /*******************************************************************************
IanBenzMaxim 0:f77ad7f72d04 2 * Copyright (C) 2017 Maxim Integrated Products, Inc., All Rights Reserved.
IanBenzMaxim 0:f77ad7f72d04 3 *
IanBenzMaxim 0:f77ad7f72d04 4 * Permission is hereby granted, free of charge, to any person obtaining a
IanBenzMaxim 0:f77ad7f72d04 5 * copy of this software and associated documentation files (the "Software"),
IanBenzMaxim 0:f77ad7f72d04 6 * to deal in the Software without restriction, including without limitation
IanBenzMaxim 0:f77ad7f72d04 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
IanBenzMaxim 0:f77ad7f72d04 8 * and/or sell copies of the Software, and to permit persons to whom the
IanBenzMaxim 0:f77ad7f72d04 9 * Software is furnished to do so, subject to the following conditions:
IanBenzMaxim 0:f77ad7f72d04 10 *
IanBenzMaxim 0:f77ad7f72d04 11 * The above copyright notice and this permission notice shall be included
IanBenzMaxim 0:f77ad7f72d04 12 * in all copies or substantial portions of the Software.
IanBenzMaxim 0:f77ad7f72d04 13 *
IanBenzMaxim 0:f77ad7f72d04 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
IanBenzMaxim 0:f77ad7f72d04 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
IanBenzMaxim 0:f77ad7f72d04 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IanBenzMaxim 0:f77ad7f72d04 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
IanBenzMaxim 0:f77ad7f72d04 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
IanBenzMaxim 0:f77ad7f72d04 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
IanBenzMaxim 0:f77ad7f72d04 20 * OTHER DEALINGS IN THE SOFTWARE.
IanBenzMaxim 0:f77ad7f72d04 21 *
IanBenzMaxim 0:f77ad7f72d04 22 * Except as contained in this notice, the name of Maxim Integrated
IanBenzMaxim 0:f77ad7f72d04 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
IanBenzMaxim 0:f77ad7f72d04 24 * Products, Inc. Branding Policy.
IanBenzMaxim 0:f77ad7f72d04 25 *
IanBenzMaxim 0:f77ad7f72d04 26 * The mere transfer of this software does not imply any licenses
IanBenzMaxim 0:f77ad7f72d04 27 * of trade secrets, proprietary technology, copyrights, patents,
IanBenzMaxim 0:f77ad7f72d04 28 * trademarks, maskwork rights, or any other form of intellectual
IanBenzMaxim 0:f77ad7f72d04 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
IanBenzMaxim 0:f77ad7f72d04 30 * ownership rights.
IanBenzMaxim 0:f77ad7f72d04 31 *******************************************************************************/
IanBenzMaxim 0:f77ad7f72d04 32
IanBenzMaxim 0:f77ad7f72d04 33 #include <MaximInterface/Links/OneWireMaster.hpp>
IanBenzMaxim 0:f77ad7f72d04 34 #include <MaximInterface/Utilities/Error.hpp>
IanBenzMaxim 0:f77ad7f72d04 35 #include "DS1920.hpp"
IanBenzMaxim 0:f77ad7f72d04 36
IanBenzMaxim 0:f77ad7f72d04 37 namespace MaximInterface {
IanBenzMaxim 0:f77ad7f72d04 38
IanBenzMaxim 6:a8c83a2e6fa4 39 error_code DS1920::writeScratchpad(uint_least8_t th, uint_least8_t tl) {
IanBenzMaxim 0:f77ad7f72d04 40 error_code result = selectRom(*master);
IanBenzMaxim 0:f77ad7f72d04 41 if (!result) {
IanBenzMaxim 6:a8c83a2e6fa4 42 const uint_least8_t sendBlock[] = {0x4E, th, tl};
IanBenzMaxim 6:a8c83a2e6fa4 43 result = master->writeBlock(sendBlock);
IanBenzMaxim 0:f77ad7f72d04 44 }
IanBenzMaxim 0:f77ad7f72d04 45 return result;
IanBenzMaxim 0:f77ad7f72d04 46 }
IanBenzMaxim 0:f77ad7f72d04 47
IanBenzMaxim 6:a8c83a2e6fa4 48 error_code DS1920::readScratchpad(Scratchpad::span scratchpad) {
IanBenzMaxim 0:f77ad7f72d04 49 error_code result = selectRom(*master);
IanBenzMaxim 0:f77ad7f72d04 50 if (!result) {
IanBenzMaxim 6:a8c83a2e6fa4 51 result = master->writeByte(0xBE);
IanBenzMaxim 0:f77ad7f72d04 52 if (!result) {
IanBenzMaxim 6:a8c83a2e6fa4 53 result = master->readBlock(scratchpad);
IanBenzMaxim 0:f77ad7f72d04 54 if (!result) {
IanBenzMaxim 0:f77ad7f72d04 55 uint_least8_t receivedCrc;
IanBenzMaxim 0:f77ad7f72d04 56 result = master->readByte(receivedCrc);
IanBenzMaxim 6:a8c83a2e6fa4 57 if (!result && (receivedCrc != calculateCrc8(scratchpad))) {
IanBenzMaxim 0:f77ad7f72d04 58 result = make_error_code(CrcError);
IanBenzMaxim 0:f77ad7f72d04 59 }
IanBenzMaxim 0:f77ad7f72d04 60 }
IanBenzMaxim 0:f77ad7f72d04 61 }
IanBenzMaxim 0:f77ad7f72d04 62 }
IanBenzMaxim 0:f77ad7f72d04 63 return result;
IanBenzMaxim 0:f77ad7f72d04 64 }
IanBenzMaxim 0:f77ad7f72d04 65
IanBenzMaxim 6:a8c83a2e6fa4 66 error_code DS1920::copyScratchpad() {
IanBenzMaxim 0:f77ad7f72d04 67 error_code result = selectRom(*master);
IanBenzMaxim 0:f77ad7f72d04 68 if (!result) {
IanBenzMaxim 6:a8c83a2e6fa4 69 result = master->writeByteSetLevel(0x48, OneWireMaster::StrongLevel);
IanBenzMaxim 0:f77ad7f72d04 70 if (!result) {
IanBenzMaxim 6:a8c83a2e6fa4 71 sleep->invoke(10);
IanBenzMaxim 0:f77ad7f72d04 72 result = master->setLevel(OneWireMaster::NormalLevel);
IanBenzMaxim 0:f77ad7f72d04 73 }
IanBenzMaxim 0:f77ad7f72d04 74 }
IanBenzMaxim 0:f77ad7f72d04 75 return result;
IanBenzMaxim 0:f77ad7f72d04 76 }
IanBenzMaxim 0:f77ad7f72d04 77
IanBenzMaxim 6:a8c83a2e6fa4 78 error_code DS1920::convertTemperature() {
IanBenzMaxim 0:f77ad7f72d04 79 error_code result = selectRom(*master);
IanBenzMaxim 0:f77ad7f72d04 80 if (!result) {
IanBenzMaxim 6:a8c83a2e6fa4 81 result = master->writeByteSetLevel(0x44, OneWireMaster::StrongLevel);
IanBenzMaxim 0:f77ad7f72d04 82 if (!result) {
IanBenzMaxim 6:a8c83a2e6fa4 83 sleep->invoke(750);
IanBenzMaxim 0:f77ad7f72d04 84 result = master->setLevel(OneWireMaster::NormalLevel);
IanBenzMaxim 0:f77ad7f72d04 85 }
IanBenzMaxim 0:f77ad7f72d04 86 }
IanBenzMaxim 0:f77ad7f72d04 87 return result;
IanBenzMaxim 0:f77ad7f72d04 88 }
IanBenzMaxim 0:f77ad7f72d04 89
IanBenzMaxim 6:a8c83a2e6fa4 90 error_code DS1920::recallEeprom() {
IanBenzMaxim 0:f77ad7f72d04 91 error_code result = selectRom(*master);
IanBenzMaxim 0:f77ad7f72d04 92 if (!result) {
IanBenzMaxim 6:a8c83a2e6fa4 93 result = master->writeByte(0xB8);
IanBenzMaxim 0:f77ad7f72d04 94 }
IanBenzMaxim 0:f77ad7f72d04 95 return result;
IanBenzMaxim 0:f77ad7f72d04 96 }
IanBenzMaxim 0:f77ad7f72d04 97
IanBenzMaxim 0:f77ad7f72d04 98 const error_category & DS1920::errorCategory() {
IanBenzMaxim 0:f77ad7f72d04 99 static class : public error_category {
IanBenzMaxim 0:f77ad7f72d04 100 public:
IanBenzMaxim 0:f77ad7f72d04 101 virtual const char * name() const { return "DS1920"; }
IanBenzMaxim 0:f77ad7f72d04 102
IanBenzMaxim 0:f77ad7f72d04 103 virtual std::string message(int condition) const {
IanBenzMaxim 0:f77ad7f72d04 104 switch (condition) {
IanBenzMaxim 0:f77ad7f72d04 105 case CrcError:
IanBenzMaxim 0:f77ad7f72d04 106 return "CRC Error";
IanBenzMaxim 0:f77ad7f72d04 107
IanBenzMaxim 0:f77ad7f72d04 108 case DataError:
IanBenzMaxim 0:f77ad7f72d04 109 return "Data Error";
IanBenzMaxim 0:f77ad7f72d04 110
IanBenzMaxim 0:f77ad7f72d04 111 default:
IanBenzMaxim 0:f77ad7f72d04 112 return defaultErrorMessage(condition);
IanBenzMaxim 0:f77ad7f72d04 113 }
IanBenzMaxim 0:f77ad7f72d04 114 }
IanBenzMaxim 0:f77ad7f72d04 115 } instance;
IanBenzMaxim 0:f77ad7f72d04 116 return instance;
IanBenzMaxim 0:f77ad7f72d04 117 }
IanBenzMaxim 0:f77ad7f72d04 118
IanBenzMaxim 0:f77ad7f72d04 119 error_code readTemperature(DS1920 & ds1920, int & temperature) {
IanBenzMaxim 0:f77ad7f72d04 120 error_code result = ds1920.convertTemperature();
IanBenzMaxim 0:f77ad7f72d04 121 if (result) {
IanBenzMaxim 0:f77ad7f72d04 122 return result;
IanBenzMaxim 0:f77ad7f72d04 123 }
IanBenzMaxim 6:a8c83a2e6fa4 124 DS1920::Scratchpad::array scratchpad;
IanBenzMaxim 0:f77ad7f72d04 125 result = ds1920.readScratchpad(scratchpad);
IanBenzMaxim 0:f77ad7f72d04 126 if (result) {
IanBenzMaxim 0:f77ad7f72d04 127 return result;
IanBenzMaxim 0:f77ad7f72d04 128 }
IanBenzMaxim 0:f77ad7f72d04 129
IanBenzMaxim 0:f77ad7f72d04 130 unsigned int tempData =
IanBenzMaxim 0:f77ad7f72d04 131 (static_cast<unsigned int>(scratchpad[1]) << 8) | scratchpad[0];
IanBenzMaxim 0:f77ad7f72d04 132 const unsigned int signMask = 0xFF00;
IanBenzMaxim 0:f77ad7f72d04 133 if ((tempData & signMask) == signMask) {
IanBenzMaxim 0:f77ad7f72d04 134 temperature = -0x100;
IanBenzMaxim 0:f77ad7f72d04 135 tempData &= ~signMask;
IanBenzMaxim 0:f77ad7f72d04 136 } else if ((tempData & signMask) == 0) {
IanBenzMaxim 0:f77ad7f72d04 137 temperature = 0;
IanBenzMaxim 0:f77ad7f72d04 138 } else {
IanBenzMaxim 0:f77ad7f72d04 139 return make_error_code(DS1920::DataError);
IanBenzMaxim 0:f77ad7f72d04 140 }
IanBenzMaxim 0:f77ad7f72d04 141 temperature += static_cast<int>(tempData);
IanBenzMaxim 0:f77ad7f72d04 142 return error_code();
IanBenzMaxim 0:f77ad7f72d04 143 }
IanBenzMaxim 0:f77ad7f72d04 144
IanBenzMaxim 0:f77ad7f72d04 145 } // namespace MaximInterface