MAXREFDES143#: DeepCover Embedded Security in IoT Authenticated Sensing & Notification

Dependencies:   MaximInterface mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DS7505.cpp Source File

DS7505.cpp

00001 /*******************************************************************************
00002 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
00003 *
00004 * Permission is hereby granted, free of charge, to any person obtaining a
00005 * copy of this software and associated documentation files (the "Software"),
00006 * to deal in the Software without restriction, including without limitation
00007 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
00008 * and/or sell copies of the Software, and to permit persons to whom the
00009 * Software is furnished to do so, subject to the following conditions:
00010 *
00011 * The above copyright notice and this permission notice shall be included
00012 * in all copies or substantial portions of the Software.
00013 *
00014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00015 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00016 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00017 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
00018 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
00019 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00020 * OTHER DEALINGS IN THE SOFTWARE.
00021 *
00022 * Except as contained in this notice, the name of Maxim Integrated
00023 * Products, Inc. shall not be used except as stated in the Maxim Integrated
00024 * Products, Inc. Branding Policy.
00025 *
00026 * The mere transfer of this software does not imply any licenses
00027 * of trade secrets, proprietary technology, copyrights, patents,
00028 * trademarks, maskwork rights, or any other form of intellectual
00029 * property whatsoever. Maxim Integrated Products, Inc. retains all
00030 * ownership rights.
00031 *******************************************************************************/
00032 
00033 #include <I2C.h>
00034 #include <wait_api.h>
00035 #include "DS7505.hpp"
00036 
00037 static const int I2C_Write_Ok = 1;
00038 static const uint8_t DS7505_Config_SD_Bit = 0x01; // Enable shutdown mode
00039 
00040 DS7505::DS7505 (mbed::I2C & I2C_interface, uint8_t I2C_address)
00041     : m_current_config(Config_9b_Res, true), m_I2C_interface(I2C_interface),
00042       m_I2C_address(I2C_address) {}
00043 
00044 uint8_t DS7505::get_measure_delay_ms(Config_Resolution resolution) {
00045   uint8_t measure_delay_ms;
00046 
00047   switch (resolution) {
00048   case Config_9b_Res:
00049     measure_delay_ms = 25;
00050     break;
00051   case Config_10b_Res:
00052     measure_delay_ms = 50;
00053     break;
00054   case Config_11b_Res:
00055     measure_delay_ms = 100;
00056     break;
00057   case Config_12b_Res:
00058     measure_delay_ms = 200;
00059     break;
00060   default:
00061     measure_delay_ms = 0;
00062     break;
00063   }
00064 
00065   return measure_delay_ms;
00066 }
00067 
00068 bool DS7505::read_temp_sensor_data(uint16_t & sensor_data) const {
00069   bool result;
00070   uint8_t upperByte, lowerByte;
00071   int sub_res;
00072 
00073   sensor_data = 0;
00074   m_I2C_interface.start();
00075   sub_res = m_I2C_interface.write(m_I2C_address | 1);
00076   if (sub_res == I2C_Write_Ok) {
00077     upperByte = m_I2C_interface.read(mbed::I2C::ACK);
00078     lowerByte = m_I2C_interface.read(mbed::I2C::NoACK);
00079   }
00080   m_I2C_interface.stop();
00081   if (sub_res == I2C_Write_Ok) {
00082     sensor_data = ((((uint16_t)upperByte) << 8) | lowerByte);
00083     result = true;
00084   } else {
00085     // Handle hardware malfunction
00086     result = false;
00087   }
00088 
00089   return result;
00090 }
00091 
00092 bool DS7505::set_register_pointer(Register pointer_reg) const {
00093   int res;
00094 
00095   m_I2C_interface.start();
00096   res = m_I2C_interface.write(m_I2C_address);
00097   if (res == I2C_Write_Ok) {
00098     res = m_I2C_interface.write(pointer_reg);
00099   }
00100   m_I2C_interface.stop();
00101 
00102   return (res == I2C_Write_Ok);
00103 }
00104 
00105 bool DS7505::write_register(Register write_reg, uint8_t write_val) const {
00106   bool res;
00107 
00108   m_I2C_interface.start();
00109   res = m_I2C_interface.write(m_I2C_address);
00110   if (res == I2C_Write_Ok) {
00111     res = m_I2C_interface.write(write_reg);
00112     if (res == I2C_Write_Ok)
00113       res = m_I2C_interface.write(write_val);
00114   }
00115   m_I2C_interface.stop();
00116 
00117   return (res == I2C_Write_Ok);
00118 }
00119 
00120 bool DS7505::write_current_config() const {
00121   uint8_t DS7505_Config_Val = m_current_config.resolution;
00122   if (m_current_config.enable_shutdown_mode)
00123     DS7505_Config_Val |= DS7505_Config_SD_Bit;
00124   return write_register(Configuration_Reg, DS7505_Config_Val);
00125 }
00126 
00127 DS7505::Result DS7505::set_resolution(uint8_t resolution) {
00128   switch (resolution) {
00129   case 1:
00130     m_current_config.resolution = Config_9b_Res;
00131     break;
00132   case 2:
00133     m_current_config.resolution = Config_10b_Res;
00134     break;
00135   case 3:
00136     m_current_config.resolution = Config_11b_Res;
00137     break;
00138   case 4:
00139     m_current_config.resolution = Config_12b_Res;
00140     break;
00141   default:
00142     return Out_of_Range;
00143   }
00144 
00145   // Write DS7505 configuration
00146   if (!write_current_config()) {
00147     // Handle hardware malfunction
00148     return Hardware_Failure;
00149   }
00150 
00151   // Set pointer to temperature register
00152   if (!set_register_pointer(Temperature_Reg)) {
00153     // Handle hardware malfunction
00154     return Hardware_Failure;
00155   }
00156 
00157   return Success;
00158 }
00159 
00160 DS7505::Result DS7505::read_temp_sensor(uint16_t & sensor_data) const {
00161   bool res;
00162 
00163   if (m_current_config.enable_shutdown_mode) {
00164     // Disable shutdown mode
00165     m_current_config.enable_shutdown_mode = false;
00166     res = write_current_config();
00167     if (!res)
00168       return Hardware_Failure;
00169 
00170     // DS7505 measures temperature
00171 
00172     // Enable shutdown mode
00173     m_current_config.enable_shutdown_mode = true;
00174     res = write_current_config();
00175     if (!res)
00176       return Hardware_Failure;
00177 
00178     // Set pointer to temperature register
00179     res = set_register_pointer(Temperature_Reg);
00180     if (!res)
00181       return Hardware_Failure;
00182 
00183     // Sleep for maximum time needed for sample
00184     wait_ms(get_measure_delay_ms(m_current_config.resolution));
00185   }
00186   // else: shutdown mode disabled
00187   //    DS7505 is constantly measuring temperature
00188 
00189   // Read temperature from sensor
00190   if (!read_temp_sensor_data(sensor_data)) {
00191     return Hardware_Failure;
00192   }
00193 
00194   return Success;
00195 }
00196 
00197 DS7505::Result DS7505::read_current_temp(int16_t & temperature) const {
00198   uint16_t sensor_data;
00199   Result result;
00200 
00201   result = read_temp_sensor(sensor_data);
00202   if (result == Success) {
00203     // Convert temperature to have an exponent of 10^-2
00204     temperature = ((int8_t)(sensor_data >> 8)) * 100;
00205     if (sensor_data & 0x0080)
00206       temperature += 50; // 0.5
00207     if (sensor_data & 0x0040)
00208       temperature += 25; // 0.25
00209     if (sensor_data & 0x0020)
00210       temperature += 13; // 0.125
00211     if (sensor_data & 0x0010)
00212       temperature += 6; // 0.0625
00213   }
00214   return result;
00215 }
00216 
00217 DS7505::Result DS7505::read_current_temp(double & temperature) const {
00218   uint16_t sensor_data;
00219   Result result;
00220 
00221   result = read_temp_sensor(sensor_data);
00222   if (result == Success) {
00223     // Convert sensor data to floating-point temperature
00224     temperature = ((int8_t)(sensor_data >> 8));
00225     if (sensor_data & 0x0080)
00226       temperature += 0.5;
00227     if (sensor_data & 0x0040)
00228       temperature += 0.25;
00229     if (sensor_data & 0x0020)
00230       temperature += 0.125;
00231     if (sensor_data & 0x0010)
00232       temperature += 0.0625;
00233   }
00234   return result;
00235 }
00236 
00237 DS7505::Result DS7505::read_current_temp(int8_t & temperature) const {
00238   uint16_t sensor_data;
00239   Result result;
00240 
00241   result = read_temp_sensor(sensor_data);
00242   if (result == Success) {
00243     // Convert sensor data to integer temperature
00244     temperature = ((int8_t)(sensor_data >> 8));
00245   }
00246   return result;
00247 }