C code and C++ library, driver software for Maxim Integrated DS1775, DS75 thermometer and thermostat temperature sensor. Code supports continuous or shut-down/standby, hysteresis, alarm limits, comparator or interrupt mode, fault filtering, and active low/high. Compact 5-pin SOT23 packaging

Dependents:   DS1775_Digital_Thermostat_Temperature

Committer:
phonemacro
Date:
Wed Apr 10 18:07:50 2019 +0000
Revision:
20:cf75ff9e5560
Parent:
18:69436cb7b19e
remove extra waits in conversion

Who changed what in which revision?

UserRevisionLine numberNew contents of line
phonemacro 8:b2d4c71268aa 1 /*******************************************************************************
phonemacro 8:b2d4c71268aa 2 * Copyright (C) 2019 Maxim Integrated Products, Inc., All Rights Reserved.
phonemacro 8:b2d4c71268aa 3 *
phonemacro 8:b2d4c71268aa 4 * Permission is hereby granted, free of charge, to any person obtaining a
phonemacro 8:b2d4c71268aa 5 * copy of this software and associated documentation files (the "Software"),
phonemacro 8:b2d4c71268aa 6 * to deal in the Software without restriction, including without limitation
phonemacro 8:b2d4c71268aa 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
phonemacro 8:b2d4c71268aa 8 * and/or sell copies of the Software, and to permit persons to whom the
phonemacro 8:b2d4c71268aa 9 * Software is furnished to do so, subject to the following conditions:
phonemacro 8:b2d4c71268aa 10 *
phonemacro 8:b2d4c71268aa 11 * The above copyright notice and this permission notice shall be included
phonemacro 8:b2d4c71268aa 12 * in all copies or substantial portions of the Software.
phonemacro 8:b2d4c71268aa 13 *
phonemacro 8:b2d4c71268aa 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
phonemacro 8:b2d4c71268aa 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
phonemacro 8:b2d4c71268aa 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
phonemacro 8:b2d4c71268aa 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
phonemacro 8:b2d4c71268aa 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
phonemacro 8:b2d4c71268aa 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
phonemacro 8:b2d4c71268aa 20 * OTHER DEALINGS IN THE SOFTWARE.
phonemacro 8:b2d4c71268aa 21 *
phonemacro 8:b2d4c71268aa 22 * Except as contained in this notice, the name of Maxim Integrated
phonemacro 8:b2d4c71268aa 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
phonemacro 8:b2d4c71268aa 24 * Products, Inc. Branding Policy.
phonemacro 8:b2d4c71268aa 25 *
phonemacro 8:b2d4c71268aa 26 * The mere transfer of this software does not imply any licenses
phonemacro 8:b2d4c71268aa 27 * of trade secrets, proprietary technology, copyrights, patents,
phonemacro 8:b2d4c71268aa 28 * trademarks, maskwork rights, or any other form of intellectual
phonemacro 8:b2d4c71268aa 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
phonemacro 8:b2d4c71268aa 30 * ownership rights.
phonemacro 8:b2d4c71268aa 31 *******************************************************************************
phonemacro 8:b2d4c71268aa 32 */
phonemacro 8:b2d4c71268aa 33 #include "ds1775.h"
phonemacro 8:b2d4c71268aa 34 #include "ds1775_cpp.h"
phonemacro 8:b2d4c71268aa 35 #include "mbed.h"
phonemacro 8:b2d4c71268aa 36 // #include "USBSerial.h"
phonemacro 8:b2d4c71268aa 37
phonemacro 8:b2d4c71268aa 38 /******************************************************************************
phonemacro 8:b2d4c71268aa 39 * C++ version for DS1775 driver *
phonemacro 8:b2d4c71268aa 40 ******************************************************************************
phonemacro 8:b2d4c71268aa 41 */
phonemacro 8:b2d4c71268aa 42
phonemacro 8:b2d4c71268aa 43 /******************************************************************************/
phonemacro 8:b2d4c71268aa 44 DS1775::DS1775(I2C &i2c_bus, uint8_t slave_address):
phonemacro 8:b2d4c71268aa 45 m_i2c(i2c_bus),
phonemacro 8:b2d4c71268aa 46 m_write_address(slave_address <<1),
phonemacro 8:b2d4c71268aa 47 m_read_address ((slave_address << 1) | 1)
phonemacro 8:b2d4c71268aa 48 {
phonemacro 8:b2d4c71268aa 49 }
phonemacro 8:b2d4c71268aa 50
phonemacro 8:b2d4c71268aa 51 /******************************************************************************/
phonemacro 8:b2d4c71268aa 52 DS1775::~DS1775(void)
phonemacro 8:b2d4c71268aa 53 {
phonemacro 8:b2d4c71268aa 54 /** empty block */
phonemacro 8:b2d4c71268aa 55 }
phonemacro 8:b2d4c71268aa 56
phonemacro 8:b2d4c71268aa 57 /******************************************************************************/
phonemacro 9:315236fb3c6a 58 int DS1775::read_cfg_reg(uint8_t *value)
phonemacro 9:315236fb3c6a 59 {
phonemacro 9:315236fb3c6a 60 int32_t ret;
phonemacro 9:315236fb3c6a 61 char data[1] = {0};
phonemacro 9:315236fb3c6a 62 char reg = DS1775_REG_CONFIGURATION;
phonemacro 9:315236fb3c6a 63
phonemacro 9:315236fb3c6a 64 /* write to the Register Select, true is for repeated start */
phonemacro 9:315236fb3c6a 65 ret = m_i2c.write(m_write_address, &reg, 1, true);
phonemacro 9:315236fb3c6a 66 if (ret == 0) {
phonemacro 9:315236fb3c6a 67 ret = m_i2c.read(m_read_address, data, 1, false);
phonemacro 9:315236fb3c6a 68 if (ret == 0) {
phonemacro 9:315236fb3c6a 69 *value = data[0];
phonemacro 9:315236fb3c6a 70 return DS1775_NO_ERROR;
phonemacro 9:315236fb3c6a 71 } else {
phonemacro 9:315236fb3c6a 72 printf(
phonemacro 9:315236fb3c6a 73 "%s: failed to read data: ret: %d\r\n", __func__, ret);
phonemacro 9:315236fb3c6a 74 }
phonemacro 9:315236fb3c6a 75 } else {
phonemacro 9:315236fb3c6a 76 printf("%s: failed to write to Register Select: ret: %d\r\n",
phonemacro 9:315236fb3c6a 77 __func__, ret);
phonemacro 9:315236fb3c6a 78 }
phonemacro 9:315236fb3c6a 79 return DS1775_ERROR;
phonemacro 9:315236fb3c6a 80 }
phonemacro 9:315236fb3c6a 81
phonemacro 9:315236fb3c6a 82 /******************************************************************************/
phonemacro 15:449134e4b43f 83 int DS1775::read_reg16(int16_t *value, char reg)
phonemacro 8:b2d4c71268aa 84 {
phonemacro 8:b2d4c71268aa 85 int32_t ret;
phonemacro 8:b2d4c71268aa 86 char data[2] = {0, 0};
phonemacro 8:b2d4c71268aa 87 ds1775_raw_data tmp;
phonemacro 8:b2d4c71268aa 88
phonemacro 10:03645de9c017 89 if (reg == DS1775_REG_TEMPERATURE ||
phonemacro 14:c74a5b4d6715 90 reg == DS1775_REG_THYST_LOW_TRIP || reg == DS1775_REG_TOS_HIGH_TRIP) {
phonemacro 9:315236fb3c6a 91 /* write to the Register Select, true is for repeated start */
phonemacro 8:b2d4c71268aa 92 ret = m_i2c.write(m_write_address, &reg, 1, true);
phonemacro 8:b2d4c71268aa 93 /* read the two bytes of data */
phonemacro 8:b2d4c71268aa 94 if (ret == 0) {
phonemacro 8:b2d4c71268aa 95 ret = m_i2c.read(m_read_address, data, 2, false);
phonemacro 8:b2d4c71268aa 96 if (ret == 0) {
phonemacro 8:b2d4c71268aa 97 tmp.msb = data[0];
phonemacro 8:b2d4c71268aa 98 tmp.lsb = data[1];
phonemacro 10:03645de9c017 99 *value = tmp.swrd;
phonemacro 8:b2d4c71268aa 100 return DS1775_NO_ERROR;
phonemacro 8:b2d4c71268aa 101 } else {
phonemacro 8:b2d4c71268aa 102 printf(
phonemacro 8:b2d4c71268aa 103 "%s: failed to read data: ret: %d\r\n", __func__, ret);
phonemacro 8:b2d4c71268aa 104 }
phonemacro 8:b2d4c71268aa 105 } else {
phonemacro 8:b2d4c71268aa 106 printf("%s: failed to write to Register Select: ret: %d\r\n",
phonemacro 8:b2d4c71268aa 107 __func__, ret);
phonemacro 8:b2d4c71268aa 108 }
phonemacro 8:b2d4c71268aa 109 } else {
phonemacro 8:b2d4c71268aa 110 printf("%s: register address is not correct: register: %d\r\n",
phonemacro 8:b2d4c71268aa 111 __func__, reg);
phonemacro 8:b2d4c71268aa 112 }
phonemacro 8:b2d4c71268aa 113 return DS1775_ERROR;
phonemacro 8:b2d4c71268aa 114 }
phonemacro 8:b2d4c71268aa 115
phonemacro 8:b2d4c71268aa 116 /******************************************************************************/
phonemacro 8:b2d4c71268aa 117 float DS1775::read_reg_as_temperature(uint8_t reg)
phonemacro 8:b2d4c71268aa 118 {
phonemacro 8:b2d4c71268aa 119 ds1775_raw_data tmp;
phonemacro 8:b2d4c71268aa 120 float temperature;
phonemacro 8:b2d4c71268aa 121 if (reg == DS1775_REG_TEMPERATURE ||
phonemacro 8:b2d4c71268aa 122 reg == DS1775_REG_THYST_LOW_TRIP || reg == DS1775_REG_TOS_HIGH_TRIP) {
phonemacro 15:449134e4b43f 123 read_reg16(&tmp.swrd, reg);
phonemacro 14:c74a5b4d6715 124 temperature = (float)tmp.swrd; /* values are 2's complement */
phonemacro 10:03645de9c017 125 temperature *= DS1775_CF_LSB;
phonemacro 8:b2d4c71268aa 126 return temperature;
phonemacro 8:b2d4c71268aa 127 } else {
phonemacro 8:b2d4c71268aa 128 printf("%s: register is invalid, %d r\n", __func__, reg);
phonemacro 8:b2d4c71268aa 129 return 0;
phonemacro 8:b2d4c71268aa 130 }
phonemacro 8:b2d4c71268aa 131 }
phonemacro 8:b2d4c71268aa 132
phonemacro 8:b2d4c71268aa 133 /******************************************************************************/
phonemacro 15:449134e4b43f 134 int DS1775::write_reg16(int16_t value, char reg)
phonemacro 8:b2d4c71268aa 135 {
phonemacro 8:b2d4c71268aa 136 int32_t ret;
phonemacro 8:b2d4c71268aa 137 char cmd[3];
phonemacro 8:b2d4c71268aa 138 ds1775_raw_data tmp;
phonemacro 8:b2d4c71268aa 139
phonemacro 14:c74a5b4d6715 140 if (reg >= DS1775_REG_THYST_LOW_TRIP && reg <= DS1775_REG_MAX) {
phonemacro 8:b2d4c71268aa 141 cmd[0] = reg;
phonemacro 10:03645de9c017 142 tmp.swrd = value;
phonemacro 8:b2d4c71268aa 143 cmd[1] = tmp.msb;
phonemacro 8:b2d4c71268aa 144 cmd[2] = tmp.lsb;
phonemacro 8:b2d4c71268aa 145 ret = m_i2c.write(m_write_address, cmd, 3, false);
phonemacro 8:b2d4c71268aa 146 if (ret == 0) {
phonemacro 9:315236fb3c6a 147 return DS1775_NO_ERROR;
phonemacro 9:315236fb3c6a 148 } else {
phonemacro 9:315236fb3c6a 149 printf("%s: I2C write error %d\r\n",__func__, ret);
phonemacro 9:315236fb3c6a 150 return DS1775_ERROR;
phonemacro 9:315236fb3c6a 151 }
phonemacro 9:315236fb3c6a 152 } else {
phonemacro 9:315236fb3c6a 153 printf("%s: register value invalid %x\r\n",__func__, reg);
phonemacro 9:315236fb3c6a 154 return DS1775_ERROR;
phonemacro 9:315236fb3c6a 155 }
phonemacro 9:315236fb3c6a 156 }
phonemacro 9:315236fb3c6a 157
phonemacro 17:ca86c0b37244 158
phonemacro 9:315236fb3c6a 159 /******************************************************************************/
phonemacro 17:ca86c0b37244 160 int DS1775::write_cfg_reg(uint8_t cfg)
phonemacro 9:315236fb3c6a 161 {
phonemacro 9:315236fb3c6a 162 int32_t ret;
phonemacro 9:315236fb3c6a 163 char cmd[2];
phonemacro 9:315236fb3c6a 164
phonemacro 17:ca86c0b37244 165 cmd[0] = DS1775_REG_CONFIGURATION;
phonemacro 17:ca86c0b37244 166 cmd[1] = cfg;
phonemacro 17:ca86c0b37244 167 ret = m_i2c.write(m_write_address, cmd, 2, false);
phonemacro 17:ca86c0b37244 168 if (ret == 0) {
phonemacro 17:ca86c0b37244 169 return DS1775_NO_ERROR;
phonemacro 8:b2d4c71268aa 170 } else {
phonemacro 17:ca86c0b37244 171 printf("%s: I2C write error %d\r\n",__func__, ret);
phonemacro 8:b2d4c71268aa 172 return DS1775_ERROR;
phonemacro 8:b2d4c71268aa 173 }
phonemacro 8:b2d4c71268aa 174 }
phonemacro 8:b2d4c71268aa 175
phonemacro 8:b2d4c71268aa 176 /******************************************************************************/
phonemacro 18:69436cb7b19e 177 int DS1775::write_trip_low_thyst(float temperature)
phonemacro 8:b2d4c71268aa 178 {
phonemacro 8:b2d4c71268aa 179 ds1775_raw_data raw;
phonemacro 10:03645de9c017 180 temperature /= DS1775_CF_LSB;
phonemacro 10:03645de9c017 181 raw.swrd = int16_t(temperature);
phonemacro 15:449134e4b43f 182 return write_reg16(raw.swrd, DS1775_REG_THYST_LOW_TRIP);
phonemacro 8:b2d4c71268aa 183 }
phonemacro 8:b2d4c71268aa 184
phonemacro 8:b2d4c71268aa 185 /******************************************************************************/
phonemacro 18:69436cb7b19e 186 int DS1775::write_trip_high_tos(float temperature)
phonemacro 8:b2d4c71268aa 187 {
phonemacro 8:b2d4c71268aa 188 ds1775_raw_data raw;
phonemacro 10:03645de9c017 189 temperature /= DS1775_CF_LSB;
phonemacro 10:03645de9c017 190 raw.swrd = int16_t(temperature);
phonemacro 15:449134e4b43f 191 return write_reg16(raw.uwrd, DS1775_REG_TOS_HIGH_TRIP);
phonemacro 8:b2d4c71268aa 192 }
phonemacro 8:b2d4c71268aa 193
phonemacro 8:b2d4c71268aa 194 /******************************************************************************/
phonemacro 8:b2d4c71268aa 195 float DS1775::celsius_to_fahrenheit(float temp_c)
phonemacro 8:b2d4c71268aa 196 {
phonemacro 8:b2d4c71268aa 197 float temp_f;
phonemacro 8:b2d4c71268aa 198 temp_f = ((temp_c * 9)/5) + 32;
phonemacro 8:b2d4c71268aa 199 return temp_f;
phonemacro 8:b2d4c71268aa 200 }