DS7505 C, C++ driver source code: The DS7505 low-voltage (1.7V to 3.7V) digital thermometer and thermostat provides 9 to 12-bit digital temperature readings over a -55°C to +125°C range with +-0.5°C accuracy in the 0°C (32°F) to +70°C (158°F) range.
Dependents: DS7505_Temperature_Sensor_Low_Power
Revision 25:93287f99d225, committed 2019-04-27
- Comitter:
- phonemacro
- Date:
- Sat Apr 27 09:50:48 2019 +0000
- Parent:
- 24:b4fdbbe79036
- Commit message:
- Initial Commit
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ds7505.h Sat Apr 27 09:50:48 2019 +0000 @@ -0,0 +1,103 @@ +/******************************************************************************* +* Copyright (C) 2019 Maxim Integrated Products, Inc., All Rights Reserved. +* +* Permission is hereby granted, free of charge, to any person obtaining a +* copy of this software and associated documentation files (the "Software"), +* to deal in the Software without restriction, including without limitation +* the rights to use, copy, modify, merge, publish, distribute, sublicense, +* and/or sell copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included +* in all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +* IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES +* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +* OTHER DEALINGS IN THE SOFTWARE. +* +* Except as contained in this notice, the name of Maxim Integrated +* Products, Inc. shall not be used except as stated in the Maxim Integrated +* Products, Inc. Branding Policy. +* +* The mere transfer of this software does not imply any licenses +* of trade secrets, proprietary technology, copyrights, patents, +* trademarks, maskwork rights, or any other form of intellectual +* property whatsoever. Maxim Integrated Products, Inc. retains all +* ownership rights. +******************************************************************************* +* @file DS7505.h +******************************************************************************* +*/ +#ifndef DS7505_H +#define DS7505_H +#include "mbed.h" + +#define DS7505_NO_ERROR 0 +#define DS7505_ERROR -1 + +#define DS7505_REG_TEMPERATURE 0X00 +#define DS7505_REG_CONFIGURATION 0X01 +#define DS7505_REG_THYST_LOW_TRIP 0X02 +#define DS7505_REG_TOS_HIGH_TRIP 0X03 +#define DS7505_REG_MAX 0X03 + +#define WAIT_MARGIN (0.0000) +#define DS7505_WAIT_CONV_TIME (.050+WAIT_MARGIN) + +#define DS7505_CFG_CONTINUOUS (0X00 << 0) +#define DS7505_CFG_SHUTDOWN (0X01 << 0) + +#define DS7505_CFG_COMPARATOR_MODE (0X00 << 1) +#define DS7505_CFG_INTERRUPT_MODE (0X01 << 1) + +#define DS7505_CFG_OS_POLARITY_ACT_LOW (0x00 << 2) +#define DS7505_CFG_OS_POLARITY_ACT_HIGH (0x01 << 2) + +#define DS7505_CFG_FAULT_FILTER_1 (0x00 << 3) +#define DS7505_CFG_FAULT_FILTER_2 (0x01 << 3) +#define DS7505_CFG_FAULT_FILTER_4 (0x02 << 3) +#define DS7505_CFG_FAULT_FILTER_6 (0x03 << 3) +#define DS7505_CFG_RESOLUTION_9BIT (0x00 << 5) +#define DS7505_CFG_RESOLUTION_10BIT (0x00 << 5) +#define DS7505_CFG_RESOLUTION_11BIT (0x00 << 5) +#define DS7505_CFG_RESOLUTION_12BIT (0x00 << 5) +#define DS7505_CFG_NV_BUSY (0X01 << 7) + +#define WAIT_MARGIN (0.000) +#define DS7505_WAIT_CONV_TIME_9BIT (0.025+WAIT_MARGIN) +#define DS7505_WAIT_CONV_TIME_10BIT (0.050+2*WAIT_MARGIN) +#define DS7505_WAIT_CONV_TIME_11BIT (0.100+4*WAIT_MARGIN) +#define DS7505_WAIT_CONV_TIME_12BIT (0.200+8*WAIT_MARGIN) + +#define DS7505_I2C_SLAVE_ADR_00 (0x90 >> 1) // code uses the 7 bit address +#define DS7505_I2C_SLAVE_ADR_01 (0x92 >> 1) +#define DS7505_I2C_SLAVE_ADR_02 (0x94 >> 1) +#define DS7505_I2C_SLAVE_ADR_03 (0x96 >> 1) +#define DS7505_I2C_SLAVE_ADR_04 (0x98 >> 1) +#define DS7505_I2C_SLAVE_ADR_05 (0x9A >> 1) +#define DS7505_I2C_SLAVE_ADR_06 (0x9C >> 1) +#define DS7505_I2C_SLAVE_ADR_07 (0x9E >> 1) + +#define DS7505_CF_LSB (0.00390625F) + +/** @union ds7505_raw_data + * @brief union data structure for byte word manipulations + */ +union ds7505_raw_data { + struct { + uint8_t lsb; + uint8_t msb; + }; + struct { + uint16_t magnitude_bits:15; + uint16_t sign_bit:1; + }; + uint16_t uwrd; + int16_t swrd; +}; + +#endif/* MAX31875_H */ \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ds7505_c.cpp Sat Apr 27 09:50:48 2019 +0000 @@ -0,0 +1,197 @@ +/******************************************************************************* +* Copyright (C) 2019 Maxim Integrated Products, Inc., All Rights Reserved. +* +* Permission is hereby granted, free of charge, to any person obtaining a +* copy of this software and associated documentation files (the "Software"), +* to deal in the Software without restriction, including without limitation +* the rights to use, copy, modify, merge, publish, distribute, sublicense, +* and/or sell copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included +* in all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +* IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES +* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +* OTHER DEALINGS IN THE SOFTWARE. +* +* Except as contained in this notice, the name of Maxim Integrated +* Products, Inc. shall not be used except as stated in the Maxim Integrated +* Products, Inc. Branding Policy. +* +* The mere transfer of this software does not imply any licenses +* of trade secrets, proprietary technology, copyrights, patents, +* trademarks, maskwork rights, or any other form of intellectual +* property whatsoever. Maxim Integrated Products, Inc. retains all +* ownership rights. +******************************************************************************* +*/ +#include "ds7505.h" +#include "ds7505_c.h" +#include "mbed.h" +// #include "USBSerial.h" + + +/******************************************************************************/ +/* C version for DS7505 driver */ +/******************************************************************************/ + +/* @var ds7505_write_address, ds7505_read_address + * @brief I2C address + */ +uint8_t ds7505_write_address, ds7505_read_address; + +int ds7505_init(uint8_t slave_address) +{ + ds7505_write_address = (slave_address <<1); + ds7505_read_address = ((slave_address << 1) | 1); + return DS7505_NO_ERROR; +} + +/******************************************************************************/ +int ds7505_read_cfg_reg(uint8_t *value, I2C &i2c_bus) +{ + int32_t ret; + char data[1] = {0}; + char reg = DS7505_REG_CONFIGURATION; + + /* write to the Register Select, true is for repeated start */ + ret = i2c_bus.write(ds7505_write_address, ®, 1, true); + if (ret == 0) { + ret = i2c_bus.read(ds7505_read_address, data, 1, false); + if (ret == 0) { + *value = data[0]; + return DS7505_NO_ERROR; + } else { + printf("%s: failed to read data: ret: %d\r\n", __func__, ret); + } + } else { + printf("%s: failed to write to Register Select: ret: %d\r\n", + __func__, ret); + } + return DS7505_ERROR; +} + + +/******************************************************************************/ +int ds7505_read_reg16(int16_t *value, char reg, I2C &i2c_bus) +{ + int32_t ret; + char data[2] = {0, 0}; + ds7505_raw_data tmp; + + if (reg == DS7505_REG_TEMPERATURE || + reg == DS7505_REG_THYST_LOW_TRIP || reg == DS7505_REG_TOS_HIGH_TRIP) { + /* write to the Register Select, true is for repeated start */ + ret = i2c_bus.write(ds7505_write_address, ®, 1, true); + /* read the two bytes of data */ + if (ret == 0) { + ret = i2c_bus.read(ds7505_read_address, data, 2, false); + if (ret == 0) { + tmp.msb = data[0]; + tmp.lsb = data[1]; + *value = tmp.swrd; + return DS7505_NO_ERROR; + } else { + printf( + "%s: failed to read data: ret: %d\r\n", __func__, ret); + } + } else { + printf("%s: failed to write to Register Select: ret: %d\r\n", + __func__, ret); + } + } else { + printf("%s: register address is not correct: register: %d\r\n", + __func__, reg); + } + return DS7505_ERROR; +} + +float ds7505_read_reg_as_temperature(uint8_t reg, I2C &i2c_bus) +{ + ds7505_raw_data tmp; + float temperature; + if (reg == DS7505_REG_TEMPERATURE || + reg == DS7505_REG_THYST_LOW_TRIP || reg == DS7505_REG_TOS_HIGH_TRIP) { + ds7505_read_reg16(&tmp.swrd, reg, i2c_bus); + temperature = (float)tmp.swrd; /* values are 2's complement */ + temperature *= DS7505_CF_LSB; + return temperature; + } else { + printf("%s: register is invalid, %d r\n", __func__, reg); + return 0; + } +} + + +/******************************************************************************/ +int ds7505_write_reg16(int16_t value, char reg, I2C &i2c_bus) +{ + int32_t ret; + char cmd[3]; + ds7505_raw_data tmp; + + if (reg >= DS7505_REG_THYST_LOW_TRIP && reg <= DS7505_REG_MAX) { + cmd[0] = reg; + tmp.swrd = value; + cmd[1] = tmp.msb; + cmd[2] = tmp.lsb; + ret = i2c_bus.write(ds7505_write_address, cmd, 3, false); + if (ret == 0) { + return DS7505_NO_ERROR; + } else { + printf("%s: I2C write error %d\r\n",__func__, ret); + return DS7505_ERROR; + } + } else { + printf("%s: register value invalid %x\r\n",__func__, reg); + return DS7505_ERROR; + } +} + + +int ds7505_write_cfg_reg(uint8_t cfg, I2C &i2c_bus) +{ + int32_t ret; + char cmd[2]; + + cmd[0] = DS7505_REG_CONFIGURATION; + cmd[1] = cfg; + ret = i2c_bus.write(ds7505_write_address, cmd, 2, false); + if (ret == 0) { + return DS7505_NO_ERROR; + } else { + printf("%s: I2C write error %d\r\n",__func__, ret); + return DS7505_ERROR; + } +} + + +int ds7505_write_trip_low_thyst(float temperature, I2C &i2c_bus) +{ + ds7505_raw_data raw; + temperature /= DS7505_CF_LSB; + raw.swrd = int16_t(temperature); + return ds7505_write_reg16(raw.swrd, DS7505_REG_THYST_LOW_TRIP, i2c_bus); +} + + +int ds7505_write_trip_high_tos(float temperature, I2C &i2c_bus) +{ + ds7505_raw_data raw; + temperature /= DS7505_CF_LSB; + raw.swrd = int16_t(temperature); + return ds7505_write_reg16(raw.swrd, DS7505_REG_TOS_HIGH_TRIP, i2c_bus); +} + + +float ds7505_celsius_to_fahrenheit(float temp_c) +{ + float temp_f; + temp_f = ((temp_c * 9)/5) + 32; + return temp_f; +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ds7505_c.h Sat Apr 27 09:50:48 2019 +0000 @@ -0,0 +1,58 @@ +/******************************************************************************* +* Copyright (C) 2019 Maxim Integrated Products, Inc., All Rights Reserved. +* +* Permission is hereby granted, free of charge, to any person obtaining a +* copy of this software and associated documentation files (the "Software"), +* to deal in the Software without restriction, including without limitation +* the rights to use, copy, modify, merge, publish, distribute, sublicense, +* and/or sell copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included +* in all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +* IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES +* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +* OTHER DEALINGS IN THE SOFTWARE. +* +* Except as contained in this notice, the name of Maxim Integrated +* Products, Inc. shall not be used except as stated in the Maxim Integrated +* Products, Inc. Branding Policy. +* +* The mere transfer of this software does not imply any licenses +* of trade secrets, proprietary technology, copyrights, patents, +* trademarks, maskwork rights, or any other form of intellectual +* property whatsoever. Maxim Integrated Products, Inc. retains all +* ownership rights. +******************************************************************************* +* @file ds7505_c.h +******************************************************************************* +*/ +#ifndef DS7505_C_H +#define DS7505_C_H +#include "mbed.h" +/*****************************************************************************/ +/* C header for DS7505 driver */ +/*****************************************************************************/ + +int ds7505_init(uint8_t slaveAddress); + +int ds7505_read_cfg_reg(uint8_t *value, I2C &i2c_bus); + +int ds7505_read_reg16(int16_t *value, char reg, I2C &i2c_bus); + +float ds7505_read_reg_as_temperature(uint8_t reg, I2C &i2c_bus); + +int ds7505_write_cfg_reg(uint8_t cfg, I2C &i2c_bus); + +int ds7505_write_trip_low_thyst(float temperature, I2C &i2c_bus); + +int ds7505_write_trip_high_tos(float temperature, I2C &i2c_bus); + +float ds7505_celsius_to_fahrenheit(float temp_c); + +#endif/* DS7505_C_H */ \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ds7505_cpp.cpp Sat Apr 27 09:50:48 2019 +0000 @@ -0,0 +1,200 @@ +/******************************************************************************* +* Copyright (C) 2019 Maxim Integrated Products, Inc., All Rights Reserved. +* +* Permission is hereby granted, free of charge, to any person obtaining a +* copy of this software and associated documentation files (the "Software"), +* to deal in the Software without restriction, including without limitation +* the rights to use, copy, modify, merge, publish, distribute, sublicense, +* and/or sell copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included +* in all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +* IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES +* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +* OTHER DEALINGS IN THE SOFTWARE. +* +* Except as contained in this notice, the name of Maxim Integrated +* Products, Inc. shall not be used except as stated in the Maxim Integrated +* Products, Inc. Branding Policy. +* +* The mere transfer of this software does not imply any licenses +* of trade secrets, proprietary technology, copyrights, patents, +* trademarks, maskwork rights, or any other form of intellectual +* property whatsoever. Maxim Integrated Products, Inc. retains all +* ownership rights. +******************************************************************************* +*/ +#include "ds7505.h" +#include "ds7505_cpp.h" +#include "mbed.h" +// #include "USBSerial.h" + +/****************************************************************************** + * C++ version for DS7505 driver * + ****************************************************************************** + */ + +/******************************************************************************/ +DS7505::DS7505(I2C &i2c_bus, uint8_t slave_address): +m_i2c(i2c_bus), +m_write_address(slave_address <<1), +m_read_address ((slave_address << 1) | 1) +{ +} + +/******************************************************************************/ +DS7505::~DS7505(void) +{ + /** empty block */ +} + +/******************************************************************************/ +int DS7505::read_cfg_reg(uint8_t *value) +{ + int32_t ret; + char data[1] = {0}; + char reg = DS7505_REG_CONFIGURATION; + + /* write to the Register Select, true is for repeated start */ + ret = m_i2c.write(m_write_address, ®, 1, true); + if (ret == 0) { + ret = m_i2c.read(m_read_address, data, 1, false); + if (ret == 0) { + *value = data[0]; + return DS7505_NO_ERROR; + } else { + printf( + "%s: failed to read data: ret: %d\r\n", __func__, ret); + } + } else { + printf("%s: failed to write to Register Select: ret: %d\r\n", + __func__, ret); + } + return DS7505_ERROR; +} + +/******************************************************************************/ +int DS7505::read_reg16(int16_t *value, char reg) +{ + int32_t ret; + char data[2] = {0, 0}; + ds7505_raw_data tmp; + + if (reg == DS7505_REG_TEMPERATURE || + reg == DS7505_REG_THYST_LOW_TRIP || reg == DS7505_REG_TOS_HIGH_TRIP) { + /* write to the Register Select, true is for repeated start */ + ret = m_i2c.write(m_write_address, ®, 1, true); + /* read the two bytes of data */ + if (ret == 0) { + ret = m_i2c.read(m_read_address, data, 2, false); + if (ret == 0) { + tmp.msb = data[0]; + tmp.lsb = data[1]; + *value = tmp.swrd; + return DS7505_NO_ERROR; + } else { + printf( + "%s: failed to read data: ret: %d\r\n", __func__, ret); + } + } else { + printf("%s: failed to write to Register Select: ret: %d\r\n", + __func__, ret); + } + } else { + printf("%s: register address is not correct: register: %d\r\n", + __func__, reg); + } + return DS7505_ERROR; +} + +/******************************************************************************/ +float DS7505::read_reg_as_temperature(uint8_t reg) +{ + ds7505_raw_data tmp; + float temperature; + if (reg == DS7505_REG_TEMPERATURE || + reg == DS7505_REG_THYST_LOW_TRIP || reg == DS7505_REG_TOS_HIGH_TRIP) { + read_reg16(&tmp.swrd, reg); + temperature = (float)tmp.swrd; /* values are 2's complement */ + temperature *= DS7505_CF_LSB; + return temperature; + } else { + printf("%s: register is invalid, %d r\n", __func__, reg); + return 0; + } +} + +/******************************************************************************/ +int DS7505::write_reg16(int16_t value, char reg) +{ + int32_t ret; + char cmd[3]; + ds7505_raw_data tmp; + + if (reg >= DS7505_REG_THYST_LOW_TRIP && reg <= DS7505_REG_MAX) { + cmd[0] = reg; + tmp.swrd = value; + cmd[1] = tmp.msb; + cmd[2] = tmp.lsb; + ret = m_i2c.write(m_write_address, cmd, 3, false); + if (ret == 0) { + return DS7505_NO_ERROR; + } else { + printf("%s: I2C write error %d\r\n",__func__, ret); + return DS7505_ERROR; + } + } else { + printf("%s: register value invalid %x\r\n",__func__, reg); + return DS7505_ERROR; + } +} + + +/******************************************************************************/ +int DS7505::write_cfg_reg(uint8_t cfg) +{ + int32_t ret; + char cmd[2]; + + cmd[0] = DS7505_REG_CONFIGURATION; + cmd[1] = cfg; + ret = m_i2c.write(m_write_address, cmd, 2, false); + if (ret == 0) { + return DS7505_NO_ERROR; + } else { + printf("%s: I2C write error %d\r\n",__func__, ret); + return DS7505_ERROR; + } +} + +/******************************************************************************/ +int DS7505::write_trip_low_thyst(float temperature) +{ + ds7505_raw_data raw; + temperature /= DS7505_CF_LSB; + raw.swrd = int16_t(temperature); + return write_reg16(raw.swrd, DS7505_REG_THYST_LOW_TRIP); +} + +/******************************************************************************/ +int DS7505::write_trip_high_tos(float temperature) +{ + ds7505_raw_data raw; + temperature /= DS7505_CF_LSB; + raw.swrd = int16_t(temperature); + return write_reg16(raw.uwrd, DS7505_REG_TOS_HIGH_TRIP); +} + +/******************************************************************************/ +float DS7505::celsius_to_fahrenheit(float temp_c) +{ + float temp_f; + temp_f = ((temp_c * 9)/5) + 32; + return temp_f; +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ds7505_cpp.h Sat Apr 27 09:50:48 2019 +0000 @@ -0,0 +1,180 @@ +/******************************************************************************* +* Copyright (C) 2019 Maxim Integrated Products, Inc., All Rights Reserved. +* +* Permission is hereby granted, free of charge, to any person obtaining a +* copy of this software and associated documentation files (the "Software"), +* to deal in the Software without restriction, including without limitation +* the rights to use, copy, modify, merge, publish, distribute, sublicense, +* and/or sell copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included +* in all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +* IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES +* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +* OTHER DEALINGS IN THE SOFTWARE. +* +* Except as contained in this notice, the name of Maxim Integrated +* Products, Inc. shall not be used except as stated in the Maxim Integrated +* Products, Inc. Branding Policy. +* +* The mere transfer of this software does not imply any licenses +* of trade secrets, proprietary technology, copyrights, patents, +* trademarks, maskwork rights, or any other form of intellectual +* property whatsoever. Maxim Integrated Products, Inc. retains all +* ownership rights. +******************************************************************************* +* @file ds7505_cpp.h +******************************************************************************* +*/ +#ifndef DS7505_CPP_H +#define DS7505_CPP_H +#include "mbed.h" + + +/** + * @brief Digital thermometer, thermostat, temperature sensor. + * @version 1.0000.0000 + * + * @details The DS7505 low-voltage (1.7V to 3.7V) digital thermometer + * and thermostat provides 9-, 10-, 11-, or 12-bit digital + * temperature readings over a -55°C to +125°C range with + * ±0.5°C accuracy over a -0°C to +70°C range. The 9-bit resolution mode + * is software compatible with the LM75. + * Available in 8-pin SO (1.27 mm pitch) or uMAX (0.65 mm pitch) packages. + * Operating temperature: -55°C to +125°C (-67°F to +257°). + * VDD: 1.7V to 3.7V. + * + * @code + * #include "mbed.h" + * #include "max32630fthr.h" + * #include "ds7505.h" + * #include "USBSerial.h" + * MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3); + * I2C i2cBus(P3_4, P3_5); + * int main() + * { + * float temperature; + * DigitalOut rLED(LED1, LED_OFF); + * DigitalOut gLED(LED2, LED_OFF); + * DigitalOut bLED(LED3, LED_OFF); + * gLED = LED_ON; + * DS7505 temp_sensor(i2cBus, DS7505_I2C_SLAVE_ADR_00); + * i2cBus.frequency(400000); + * temperature = + * temp_sensor.read_reg_as_temperature(DS7505_REG_TEMPERATURE); + * printf("Temperature = %3.4f Celsius, %3.4f Fahrenheit\r\n", + * temperature, temp_sensor.celsius_to_fahrenheit(temperature)); + * } + * @endcode + */ + +class DS7505 +{ + public: + + /**********************************************************//** + * @brief Constructor for DS7505 Class. + * + * @details Allows user to use existing I2C object + * + * On Entry: + * @param[in] i2c_bus - pointer to existing I2C object + * @param[in] i2c_adrs - 7-bit slave address of DS7505 + * + * On Exit: + * + * @return None + **************************************************************/ + DS7505(I2C &i2c_bus, uint8_t slave_address); + + /**********************************************************//** + * @brief Default destructor for DS7505 Class. + * + * @details Destroys I2C object if owner + * + * On Entry: + * + * On Exit: + * + * @return None + **************************************************************/ + ~DS7505(); + + /** + * @brief Read register of device at slave address + * @param[out] value - Read data on success + * @return 0 on success, negative number on failure + */ + int read_cfg_reg(uint8_t *value); + + /** + * @brief Read register of device at slave address + * @param[out] value - Read data on success + * @param reg - Register address + * @return 0 on success, negative number on failure + */ + int read_reg16(int16_t *value, char reg); + + /** + * @brief Reads the temperature registers + * @param reg - the address of the temperature register + * @return temperature in degrees Celsius + */ + float read_reg_as_temperature(uint8_t reg); + + /** + * @brief Writes to the configuration register + * @param cfg - configurate word + * @return 0 on success, negative number on failure + */ + int write_cfg_reg(uint8_t cfg); + + /** + * @brief Writes to the THYST register + * @param temperature - the temperature in Celsius degrees + * @return 0 on success, negative number on failure + */ + int write_trip_low_thyst(float temperature); + + /** + * @brief Writes to the TOS register + * @param temperature - the temperature in Celsius degrees + * @return 0 on success, negative number on failure + */ + int write_trip_high_tos(float temperature); + + /** + * @brief Converts Celsius degrees to Fahrenheit + * @param temp_c - the temperature in Celsius degrees + * @return temperature in Celsius degrees + */ + float celsius_to_fahrenheit(float temp_c); + +protected: + /** + * @brief Write a value to a register + * @param value - value to write to the register + * @param reg - register address + * @return 0 on success, negative number on failure + */ + int write_reg16(int16_t value, char reg); + +private: + /** @var m_i2c + * @brief I2C object + */ + I2C &m_i2c; + /** @var m_write_address, m_read_address + * @brief I2C address + */ + uint8_t m_write_address, m_read_address; + +}; + +#endif/* DS7505_CPP_H */ \ No newline at end of file
--- a/max31725.h Wed Apr 17 21:28:29 2019 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,152 +0,0 @@ -/******************************************************************************* -* Copyright (C) 2019 Maxim Integrated Products, Inc., All Rights Reserved. -* -* Permission is hereby granted, free of charge, to any person obtaining a -* copy of this software and associated documentation files (the "Software"), -* to deal in the Software without restriction, including without limitation -* the rights to use, copy, modify, merge, publish, distribute, sublicense, -* and/or sell copies of the Software, and to permit persons to whom the -* Software is furnished to do so, subject to the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES -* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, -* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -* OTHER DEALINGS IN THE SOFTWARE. -* -* Except as contained in this notice, the name of Maxim Integrated -* Products, Inc. shall not be used except as stated in the Maxim Integrated -* Products, Inc. Branding Policy. -* -* The mere transfer of this software does not imply any licenses -* of trade secrets, proprietary technology, copyrights, patents, -* trademarks, maskwork rights, or any other form of intellectual -* property whatsoever. Maxim Integrated Products, Inc. retains all -* ownership rights. -******************************************************************************* -* @file MAX31725.h -******************************************************************************* -*/ -#ifndef MAX31725_H -#define MAX31725_H -#include "mbed.h" - -#define MAX31725_NO_ERROR 0 -#define MAX31725_ERROR -1 - -#define MAX31725_REG_TEMPERATURE 0X00 -#define MAX31725_REG_CONFIGURATION 0X01 -#define MAX31725_REG_THYST_LOW_TRIP 0X02 -#define MAX31725_REG_TOS_HIGH_TRIP 0X03 -#define MAX31725_REG_MAX 0X03 - -#define WAIT_MARGIN (0.0000) -#define MAX31725_WAIT_CONV_TIME (.050+WAIT_MARGIN) - -#define MAX31725_CFG_CONTINUOUS (0X00 << 0) -#define MAX31725_CFG_SHUTDOWN (0X01 << 0) - -#define MAX31725_CFG_COMPARATOR_MODE (0X00 << 1) -#define MAX31725_CFG_INTERRUPT_MODE (0X01 << 1) - -#define MAX31725_CFG_OS_POLARITY_ACT_LOW (0x00 << 2) -#define MAX31725_CFG_OS_POLARITY_ACT_HIGH (0x01 << 2) - -#define MAX31725_CFG_FAULT_FILTER_1 (0x00 << 3) -#define MAX31725_CFG_FAULT_FILTER_2 (0x01 << 3) -#define MAX31725_CFG_FAULT_FILTER_4 (0x02 << 3) -#define MAX31725_CFG_FAULT_FILTER_6 (0x03 << 3) - -#define MAX31725_CFG_NORMAL_FORMAT (0x00 << 5) -#define MAX31725_CFG_EXTENDED_FORMAT (0x01 << 5) /* add 64°C to temperature */ -#define MAX31725_EXTENDED_FORMAT_OFFSET (64.0f) -#define MAX31725_CFG_TIMEOUT_ENABLE (0X00 << 6) -#define MAX31725_CFG_TIMEOUT_DISABLE (0X01 << 6) -#define MAX31725_CFG_ONE_SHOT_START (0X01 << 7) - - /* A1 A2 A3 */ -#define MAX31725_I2C_SLAVE_ADR_00 (0x90 >> 1) /* GND GND GND */ -#define MAX31725_I2C_SLAVE_ADR_01 (0x92 >> 1) /* GND GND VDD */ -#define MAX31725_I2C_SLAVE_ADR_02 (0x82 >> 1) /* GND GND SCL */ -#define MAX31725_I2C_SLAVE_ADR_03 (0x80 >> 1) /* GND GND SDA */ -#define MAX31725_I2C_SLAVE_ADR_04 (0x94 >> 1) /* GND VDD GND */ - -#define MAX31725_I2C_SLAVE_ADR_05 (0x96 >> 1) /* GND VDD VDD */ -#define MAX31725_I2C_SLAVE_ADR_06 (0x86 >> 1) /* GND VDD SCL */ -#define MAX31725_I2C_SLAVE_ADR_07 (0x84 >> 1) /* GND VDD SDA */ -#define MAX31725_I2C_SLAVE_ADR_08 (0xB4 >> 1) /* GND SCL GND */ -#define MAX31725_I2C_SLAVE_ADR_09 (0xB6 >> 1) /* GND SCL VDD */ - -#define MAX31725_I2C_SLAVE_ADR_10 (0xA6 >> 1) /* GND SCL SCL */ -#define MAX31725_I2C_SLAVE_ADR_11 (0xA4 >> 1) /* GND SCL SDA */ -#define MAX31725_I2C_SLAVE_ADR_12 (0xB0 >> 1) /* GND SDA GND */ -#define MAX31725_I2C_SLAVE_ADR_13 (0xB2 >> 1) /* GND SDA VDD */ -#define MAX31725_I2C_SLAVE_ADR_14 (0xA2 >> 1) /* GND SDA SCL */ - -#define MAX31725_I2C_SLAVE_ADR_15 (0xA0 >> 1) /* GND SDA SDA */ -#define MAX31725_I2C_SLAVE_ADR_16 (0x98 >> 1) /* VDD GND GND */ -#define MAX31725_I2C_SLAVE_ADR_17 (0x9A >> 1) /* VDD GND VDD */ -#define MAX31725_I2C_SLAVE_ADR_18 (0x8A >> 1) /* VDD GND SCL */ -#define MAX31725_I2C_SLAVE_ADR_19 (0x88 >> 1) /* VDD GND SDA */ - -#define MAX31725_I2C_SLAVE_ADR_20 (0x9C >> 1) /* VDD VDD GND */ -#define MAX31725_I2C_SLAVE_ADR_21 (0x9E >> 1) /* VDD VDD VDD */ -#define MAX31725_I2C_SLAVE_ADR_22 (0x8E >> 1) /* VDD VDD SCL */ -#define MAX31725_I2C_SLAVE_ADR_23 (0x8C >> 1) /* VDD VDD SDA */ -#define MAX31725_I2C_SLAVE_ADR_24 (0xBC >> 1) /* VDD SCL GND */ - -#define MAX31725_I2C_SLAVE_ADR_25 (0xBE >> 1) /* VDD SCL VDD */ -#define MAX31725_I2C_SLAVE_ADR_26 (0xAE >> 1) /* VDD SCL SCL */ -#define MAX31725_I2C_SLAVE_ADR_27 (0xAC >> 1) /* VDD SCL SDA */ -#define MAX31725_I2C_SLAVE_ADR_28 (0xB8 >> 1) /* VDD SDA GND */ -#define MAX31725_I2C_SLAVE_ADR_29 (0xBA >> 1) /* VDD SDA VDD */ - -#define MAX31725_I2C_SLAVE_ADR_30 (0xAA >> 1) /* VDD SDA SCL */ -#define MAX31725_I2C_SLAVE_ADR_31 (0xA8 >> 1) /* VDD SDA SDA */ - - -#define MAX31726_I2C_SLAVE_ADR_00 (0x98 >> 1) /* GND GND */ -#define MAX31726_I2C_SLAVE_ADR_01 (0x9A >> 1) /* GND VDD */ -#define MAX31726_I2C_SLAVE_ADR_02 (0x8A >> 1) /* GND SCL */ -#define MAX31726_I2C_SLAVE_ADR_03 (0x88 >> 1) /* GND SDA */ -#define MAX31726_I2C_SLAVE_ADR_04 (0x9C >> 1) /* VDD GND */ - -#define MAX31726_I2C_SLAVE_ADR_05 (0x9E >> 1) /* VDD VDD */ -#define MAX31726_I2C_SLAVE_ADR_06 (0x8E >> 1) /* VDD SCL */ -#define MAX31726_I2C_SLAVE_ADR_07 (0x8C >> 1) /* VDD SDA */ -#define MAX31726_I2C_SLAVE_ADR_08 (0xBC >> 1) /* SCL GND */ -#define MAX31726_I2C_SLAVE_ADR_09 (0xBE >> 1) /* SCL VDD */ - -#define MAX31726_I2C_SLAVE_ADR_10 (0xAE >> 1) /* SCL SCL */ -#define MAX31726_I2C_SLAVE_ADR_11 (0xAC >> 1) /* SCL SDA */ -#define MAX31726_I2C_SLAVE_ADR_12 (0xB8 >> 1) /* SDA GND */ -#define MAX31726_I2C_SLAVE_ADR_13 (0xBA >> 1) /* SDA VDD */ -#define MAX31726_I2C_SLAVE_ADR_14 (0xAA >> 1) /* SDA SCL */ - -#define MAX31726_I2C_SLAVE_ADR_15 (0xA8 >> 1) /* SDA SDA */ - - -#define MAX31725_CF_LSB (0.00390625F) - -/** @union max31725_raw_data - * @brief union data structure for byte word manipulations - */ -union max31725_raw_data { - struct { - uint8_t lsb; - uint8_t msb; - }; - struct { - uint16_t magnitude_bits:15; - uint16_t sign_bit:1; - }; - uint16_t uwrd; - int16_t swrd; -}; - -#endif/* MAX31875_H */ \ No newline at end of file
--- a/max31725_c.cpp Wed Apr 17 21:28:29 2019 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,209 +0,0 @@ -/******************************************************************************* -* Copyright (C) 2019 Maxim Integrated Products, Inc., All Rights Reserved. -* -* Permission is hereby granted, free of charge, to any person obtaining a -* copy of this software and associated documentation files (the "Software"), -* to deal in the Software without restriction, including without limitation -* the rights to use, copy, modify, merge, publish, distribute, sublicense, -* and/or sell copies of the Software, and to permit persons to whom the -* Software is furnished to do so, subject to the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES -* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, -* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -* OTHER DEALINGS IN THE SOFTWARE. -* -* Except as contained in this notice, the name of Maxim Integrated -* Products, Inc. shall not be used except as stated in the Maxim Integrated -* Products, Inc. Branding Policy. -* -* The mere transfer of this software does not imply any licenses -* of trade secrets, proprietary technology, copyrights, patents, -* trademarks, maskwork rights, or any other form of intellectual -* property whatsoever. Maxim Integrated Products, Inc. retains all -* ownership rights. -******************************************************************************* -*/ -#include "max31725.h" -#include "max31725_c.h" -#include "mbed.h" -// #include "USBSerial.h" - - -/******************************************************************************/ -/* C version for MAX31725 driver */ -/******************************************************************************/ - -/* @var max31725_write_address, max31725_read_address - * @brief I2C address - */ -uint8_t max31725_write_address, max31725_read_address; - -/* @var max31875_extended_format - * @brief Extended format, add 64°C to the temperature reading - */ -uint32_t max31725_extended_format; - -int max31725_init(uint8_t slave_address) -{ - max31725_write_address = (slave_address <<1); - max31725_read_address = ((slave_address << 1) | 1); - max31725_extended_format = 0; - return MAX31725_NO_ERROR; -} - -/******************************************************************************/ -int max31725_read_cfg_reg(uint8_t *value, I2C &i2c_bus) -{ - int32_t ret; - char data[1] = {0}; - char reg = MAX31725_REG_CONFIGURATION; - - /* write to the Register Select, true is for repeated start */ - ret = i2c_bus.write(max31725_write_address, ®, 1, true); - if (ret == 0) { - ret = i2c_bus.read(max31725_read_address, data, 1, false); - if (ret == 0) { - *value = data[0]; - return MAX31725_NO_ERROR; - } else { - printf("%s: failed to read data: ret: %d\r\n", __func__, ret); - } - } else { - printf("%s: failed to write to Register Select: ret: %d\r\n", - __func__, ret); - } - return MAX31725_ERROR; -} - - -/******************************************************************************/ -int max31725_read_reg16(int16_t *value, char reg, I2C &i2c_bus) -{ - int32_t ret; - char data[2] = {0, 0}; - max31725_raw_data tmp; - - if (reg == MAX31725_REG_TEMPERATURE || - reg == MAX31725_REG_THYST_LOW_TRIP || reg == MAX31725_REG_TOS_HIGH_TRIP) { - /* write to the Register Select, true is for repeated start */ - ret = i2c_bus.write(max31725_write_address, ®, 1, true); - /* read the two bytes of data */ - if (ret == 0) { - ret = i2c_bus.read(max31725_read_address, data, 2, false); - if (ret == 0) { - tmp.msb = data[0]; - tmp.lsb = data[1]; - *value = tmp.swrd; - return MAX31725_NO_ERROR; - } else { - printf( - "%s: failed to read data: ret: %d\r\n", __func__, ret); - } - } else { - printf("%s: failed to write to Register Select: ret: %d\r\n", - __func__, ret); - } - } else { - printf("%s: register address is not correct: register: %d\r\n", - __func__, reg); - } - return MAX31725_ERROR; -} - -float max31725_read_reg_as_temperature(uint8_t reg, I2C &i2c_bus) -{ - max31725_raw_data tmp; - float temperature; - if (reg == MAX31725_REG_TEMPERATURE || - reg == MAX31725_REG_THYST_LOW_TRIP || reg == MAX31725_REG_TOS_HIGH_TRIP) { - max31725_read_reg16(&tmp.swrd, reg, i2c_bus); - temperature = (float)tmp.swrd; /* values are 2's complement */ - temperature *= MAX31725_CF_LSB; - if (reg == MAX31725_REG_TEMPERATURE && max31725_extended_format) - temperature += MAX31725_EXTENDED_FORMAT_OFFSET; - return temperature; - } else { - printf("%s: register is invalid, %d r\n", __func__, reg); - return 0; - } -} - - -/******************************************************************************/ -int max31725_write_reg16(int16_t value, char reg, I2C &i2c_bus) -{ - int32_t ret; - char cmd[3]; - max31725_raw_data tmp; - - if (reg >= MAX31725_REG_THYST_LOW_TRIP && reg <= MAX31725_REG_MAX) { - cmd[0] = reg; - tmp.swrd = value; - cmd[1] = tmp.msb; - cmd[2] = tmp.lsb; - ret = i2c_bus.write(max31725_write_address, cmd, 3, false); - if (ret == 0) { - return MAX31725_NO_ERROR; - } else { - printf("%s: I2C write error %d\r\n",__func__, ret); - return MAX31725_ERROR; - } - } else { - printf("%s: register value invalid %x\r\n",__func__, reg); - return MAX31725_ERROR; - } -} - - -int max31725_write_cfg_reg(uint8_t cfg, I2C &i2c_bus) -{ - int32_t ret; - char cmd[2]; - - cmd[0] = MAX31725_REG_CONFIGURATION; - cmd[1] = cfg; - ret = i2c_bus.write(max31725_write_address, cmd, 2, false); - if (ret == 0) { - max31725_extended_format = 0; - if (cfg & MAX31725_CFG_EXTENDED_FORMAT) - max31725_extended_format = 1; - - return MAX31725_NO_ERROR; - } else { - printf("%s: I2C write error %d\r\n",__func__, ret); - return MAX31725_ERROR; - } -} - - -int max31725_write_trip_low_thyst(float temperature, I2C &i2c_bus) -{ - max31725_raw_data raw; - temperature /= MAX31725_CF_LSB; - raw.swrd = int16_t(temperature); - return max31725_write_reg16(raw.swrd, MAX31725_REG_THYST_LOW_TRIP, i2c_bus); -} - - -int max31725_write_trip_high_tos(float temperature, I2C &i2c_bus) -{ - max31725_raw_data raw; - temperature /= MAX31725_CF_LSB; - raw.swrd = int16_t(temperature); - return max31725_write_reg16(raw.swrd, MAX31725_REG_TOS_HIGH_TRIP, i2c_bus); -} - - -float max31725_celsius_to_fahrenheit(float temp_c) -{ - float temp_f; - temp_f = ((temp_c * 9)/5) + 32; - return temp_f; -} \ No newline at end of file
--- a/max31725_c.h Wed Apr 17 21:28:29 2019 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,58 +0,0 @@ -/******************************************************************************* -* Copyright (C) 2019 Maxim Integrated Products, Inc., All Rights Reserved. -* -* Permission is hereby granted, free of charge, to any person obtaining a -* copy of this software and associated documentation files (the "Software"), -* to deal in the Software without restriction, including without limitation -* the rights to use, copy, modify, merge, publish, distribute, sublicense, -* and/or sell copies of the Software, and to permit persons to whom the -* Software is furnished to do so, subject to the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES -* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, -* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -* OTHER DEALINGS IN THE SOFTWARE. -* -* Except as contained in this notice, the name of Maxim Integrated -* Products, Inc. shall not be used except as stated in the Maxim Integrated -* Products, Inc. Branding Policy. -* -* The mere transfer of this software does not imply any licenses -* of trade secrets, proprietary technology, copyrights, patents, -* trademarks, maskwork rights, or any other form of intellectual -* property whatsoever. Maxim Integrated Products, Inc. retains all -* ownership rights. -******************************************************************************* -* @file max31725_c.h -******************************************************************************* -*/ -#ifndef MAX31725_C_H -#define MAX31725_C_H -#include "mbed.h" -/*****************************************************************************/ -/* C header for MAX31725 driver */ -/*****************************************************************************/ - -int max31725_init(uint8_t slaveAddress); - -int max31725_read_cfg_reg(uint8_t *value, I2C &i2c_bus); - -int max31725_read_reg16(int16_t *value, char reg, I2C &i2c_bus); - -float max31725_read_reg_as_temperature(uint8_t reg, I2C &i2c_bus); - -int max31725_write_cfg_reg(uint8_t cfg, I2C &i2c_bus); - -int max31725_write_trip_low_thyst(float temperature, I2C &i2c_bus); - -int max31725_write_trip_high_tos(float temperature, I2C &i2c_bus); - -float max31725_celsius_to_fahrenheit(float temp_c); - -#endif/* MAX31725_C_H */ \ No newline at end of file
--- a/max31725_cpp.h Wed Apr 17 21:28:29 2019 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,185 +0,0 @@ -/******************************************************************************* -* Copyright (C) 2019 Maxim Integrated Products, Inc., All Rights Reserved. -* -* Permission is hereby granted, free of charge, to any person obtaining a -* copy of this software and associated documentation files (the "Software"), -* to deal in the Software without restriction, including without limitation -* the rights to use, copy, modify, merge, publish, distribute, sublicense, -* and/or sell copies of the Software, and to permit persons to whom the -* Software is furnished to do so, subject to the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES -* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, -* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -* OTHER DEALINGS IN THE SOFTWARE. -* -* Except as contained in this notice, the name of Maxim Integrated -* Products, Inc. shall not be used except as stated in the Maxim Integrated -* Products, Inc. Branding Policy. -* -* The mere transfer of this software does not imply any licenses -* of trade secrets, proprietary technology, copyrights, patents, -* trademarks, maskwork rights, or any other form of intellectual -* property whatsoever. Maxim Integrated Products, Inc. retains all -* ownership rights. -******************************************************************************* -* @file max31725_cpp.h -******************************************************************************* -*/ -#ifndef MAX31725_CPP_H -#define MAX31725_CPP_H -#include "mbed.h" - - -/** - * @brief Digital thermometer, thermostat, temperature sensor. - * @version 1.0000.0002 - * - * @details The MAX31725, MAX31726 temperature sensors - * provides accurate temperature measurements. - * Extended format allows for high temperature readings up to 150°C. - * The MAX31725 can operate in a low powered mode by utilizing - * the shutdown and one-shot mode. - * 8-pin TQDFN 3x3 mm package - * Accuracy is +-0.5°C from -40°C to +105°C (-40°F to +221°). - * Operating temperature: -55°C to +150°C (-67°F to +302°). - * VDD: 2.5V to 3.7V. - * - * @code - * #include "mbed.h" - * #include "max32630fthr.h" - * #include "max31725.h" - * #include "USBSerial.h" - * MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3); - * I2C i2cBus(P3_4, P3_5); - * int main() - * { - * float temperature; - * DigitalOut rLED(LED1, LED_OFF); - * DigitalOut gLED(LED2, LED_OFF); - * DigitalOut bLED(LED3, LED_OFF); - * gLED = LED_ON; - * MAX31725 temp_sensor(i2cBus, MAX31725_I2C_SLAVE_ADR_00); - * i2cBus.frequency(400000); - * temperature = - * temp_sensor.read_reg_as_temperature(MAX31725_REG_TEMPERATURE); - * printf("Temperature = %3.4f Celsius, %3.4f Fahrenheit\r\n", - * temperature, temp_sensor.celsius_to_fahrenheit(temperature)); - * } - * @endcode - */ - -class MAX31725 -{ - public: - - /**********************************************************//** - * @brief Constructor for MAX31725 Class. - * - * @details Allows user to use existing I2C object - * - * On Entry: - * @param[in] i2c_bus - pointer to existing I2C object - * @param[in] i2c_adrs - 7-bit slave address of MAX31725 - * - * On Exit: - * - * @return None - **************************************************************/ - MAX31725(I2C &i2c_bus, uint8_t slave_address); - - /**********************************************************//** - * @brief Default destructor for MAX31725 Class. - * - * @details Destroys I2C object if owner - * - * On Entry: - * - * On Exit: - * - * @return None - **************************************************************/ - ~MAX31725(); - - /** - * @brief Read register of device at slave address - * @param[out] value - Read data on success - * @return 0 on success, negative number on failure - */ - int read_cfg_reg(uint8_t *value); - - /** - * @brief Read register of device at slave address - * @param[out] value - Read data on success - * @param reg - Register address - * @return 0 on success, negative number on failure - */ - int read_reg16(int16_t *value, char reg); - - /** - * @brief Reads the temperature registers - * @param reg - the address of the temperature register - * @return temperature in degrees Celsius - */ - float read_reg_as_temperature(uint8_t reg); - - /** - * @brief Writes to the configuration register - * @param cfg - configurate word - * @return 0 on success, negative number on failure - */ - int write_cfg_reg(uint8_t cfg); - - /** - * @brief Writes to the THYST register - * @param temperature - the temperature in Celsius degrees - * @return 0 on success, negative number on failure - */ - int write_trip_low_thyst(float temperature); - - /** - * @brief Writes to the TOS register - * @param temperature - the temperature in Celsius degrees - * @return 0 on success, negative number on failure - */ - int write_trip_high_tos(float temperature); - - /** - * @brief Converts Celsius degrees to Fahrenheit - * @param temp_c - the temperature in Celsius degrees - * @return temperature in Celsius degrees - */ - float celsius_to_fahrenheit(float temp_c); - -protected: - /** - * @brief Write a value to a register - * @param value - value to write to the register - * @param reg - register address - * @return 0 on success, negative number on failure - */ - int write_reg16(int16_t value, char reg); - -private: - /** @var m_i2c - * @brief I2C object - */ - I2C &m_i2c; - /** @var m_write_address, m_read_address - * @brief I2C address - */ - uint8_t m_write_address, m_read_address; - /** @var m_extended_format - * @brief Extended format, add 64°C to the temperature reading - */ - uint32_t max31725_extended_format; - -}; - -#endif/* MAX31725_CPP_H */ \ No newline at end of file
--- a/max317275_cpp.cpp Wed Apr 17 21:28:29 2019 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,208 +0,0 @@ -/******************************************************************************* -* Copyright (C) 2019 Maxim Integrated Products, Inc., All Rights Reserved. -* -* Permission is hereby granted, free of charge, to any person obtaining a -* copy of this software and associated documentation files (the "Software"), -* to deal in the Software without restriction, including without limitation -* the rights to use, copy, modify, merge, publish, distribute, sublicense, -* and/or sell copies of the Software, and to permit persons to whom the -* Software is furnished to do so, subject to the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES -* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, -* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -* OTHER DEALINGS IN THE SOFTWARE. -* -* Except as contained in this notice, the name of Maxim Integrated -* Products, Inc. shall not be used except as stated in the Maxim Integrated -* Products, Inc. Branding Policy. -* -* The mere transfer of this software does not imply any licenses -* of trade secrets, proprietary technology, copyrights, patents, -* trademarks, maskwork rights, or any other form of intellectual -* property whatsoever. Maxim Integrated Products, Inc. retains all -* ownership rights. -******************************************************************************* -*/ -#include "max31725.h" -#include "max31725_cpp.h" -#include "mbed.h" -// #include "USBSerial.h" - -/****************************************************************************** - * C++ version for MAX31725 driver * - ****************************************************************************** - */ - -/******************************************************************************/ -MAX31725::MAX31725(I2C &i2c_bus, uint8_t slave_address): -m_i2c(i2c_bus), -m_write_address(slave_address <<1), -m_read_address ((slave_address << 1) | 1) -{ - max31725_extended_format = 0; -} - -/******************************************************************************/ -MAX31725::~MAX31725(void) -{ - /** empty block */ -} - -/******************************************************************************/ -int MAX31725::read_cfg_reg(uint8_t *value) -{ - int32_t ret; - char data[1] = {0}; - char reg = MAX31725_REG_CONFIGURATION; - - /* write to the Register Select, true is for repeated start */ - ret = m_i2c.write(m_write_address, ®, 1, true); - if (ret == 0) { - ret = m_i2c.read(m_read_address, data, 1, false); - if (ret == 0) { - *value = data[0]; - return MAX31725_NO_ERROR; - } else { - printf( - "%s: failed to read data: ret: %d\r\n", __func__, ret); - } - } else { - printf("%s: failed to write to Register Select: ret: %d\r\n", - __func__, ret); - } - return MAX31725_ERROR; -} - -/******************************************************************************/ -int MAX31725::read_reg16(int16_t *value, char reg) -{ - int32_t ret; - char data[2] = {0, 0}; - max31725_raw_data tmp; - - if (reg == MAX31725_REG_TEMPERATURE || - reg == MAX31725_REG_THYST_LOW_TRIP || reg == MAX31725_REG_TOS_HIGH_TRIP) { - /* write to the Register Select, true is for repeated start */ - ret = m_i2c.write(m_write_address, ®, 1, true); - /* read the two bytes of data */ - if (ret == 0) { - ret = m_i2c.read(m_read_address, data, 2, false); - if (ret == 0) { - tmp.msb = data[0]; - tmp.lsb = data[1]; - *value = tmp.swrd; - return MAX31725_NO_ERROR; - } else { - printf( - "%s: failed to read data: ret: %d\r\n", __func__, ret); - } - } else { - printf("%s: failed to write to Register Select: ret: %d\r\n", - __func__, ret); - } - } else { - printf("%s: register address is not correct: register: %d\r\n", - __func__, reg); - } - return MAX31725_ERROR; -} - -/******************************************************************************/ -float MAX31725::read_reg_as_temperature(uint8_t reg) -{ - max31725_raw_data tmp; - float temperature; - if (reg == MAX31725_REG_TEMPERATURE || - reg == MAX31725_REG_THYST_LOW_TRIP || reg == MAX31725_REG_TOS_HIGH_TRIP) { - read_reg16(&tmp.swrd, reg); - temperature = (float)tmp.swrd; /* values are 2's complement */ - temperature *= MAX31725_CF_LSB; - if (reg == MAX31725_REG_TEMPERATURE && max31725_extended_format) - temperature += MAX31725_EXTENDED_FORMAT_OFFSET; - - return temperature; - } else { - printf("%s: register is invalid, %d r\n", __func__, reg); - return 0; - } -} - -/******************************************************************************/ -int MAX31725::write_reg16(int16_t value, char reg) -{ - int32_t ret; - char cmd[3]; - max31725_raw_data tmp; - - if (reg >= MAX31725_REG_THYST_LOW_TRIP && reg <= MAX31725_REG_MAX) { - cmd[0] = reg; - tmp.swrd = value; - cmd[1] = tmp.msb; - cmd[2] = tmp.lsb; - ret = m_i2c.write(m_write_address, cmd, 3, false); - if (ret == 0) { - return MAX31725_NO_ERROR; - } else { - printf("%s: I2C write error %d\r\n",__func__, ret); - return MAX31725_ERROR; - } - } else { - printf("%s: register value invalid %x\r\n",__func__, reg); - return MAX31725_ERROR; - } -} - - -/******************************************************************************/ -int MAX31725::write_cfg_reg(uint8_t cfg) -{ - int32_t ret; - char cmd[2]; - - cmd[0] = MAX31725_REG_CONFIGURATION; - cmd[1] = cfg; - ret = m_i2c.write(m_write_address, cmd, 2, false); - if (ret == 0) { - max31725_extended_format = 0; - if (cfg & MAX31725_CFG_EXTENDED_FORMAT) - max31725_extended_format = 1; - - return MAX31725_NO_ERROR; - } else { - printf("%s: I2C write error %d\r\n",__func__, ret); - return MAX31725_ERROR; - } -} - -/******************************************************************************/ -int MAX31725::write_trip_low_thyst(float temperature) -{ - max31725_raw_data raw; - temperature /= MAX31725_CF_LSB; - raw.swrd = int16_t(temperature); - return write_reg16(raw.swrd, MAX31725_REG_THYST_LOW_TRIP); -} - -/******************************************************************************/ -int MAX31725::write_trip_high_tos(float temperature) -{ - max31725_raw_data raw; - temperature /= MAX31725_CF_LSB; - raw.swrd = int16_t(temperature); - return write_reg16(raw.uwrd, MAX31725_REG_TOS_HIGH_TRIP); -} - -/******************************************************************************/ -float MAX31725::celsius_to_fahrenheit(float temp_c) -{ - float temp_f; - temp_f = ((temp_c * 9)/5) + 32; - return temp_f; -} \ No newline at end of file