code to access the AT30TSE75x temperature and E-prom device

Dependents:   AT30TSE752TST AT30TSE752TST2

Committer:
wbeaumont
Date:
Thu Mar 30 21:54:21 2017 +0000
Revision:
6:cc295531021e
Parent:
5:c783703a9e28
corrected negative temperature calculation

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wbeaumont 0:7cb648bc5c2a 1 #ifndef AT30TSE75x_H
wbeaumont 0:7cb648bc5c2a 2 #define AT30TSE75x_H
wbeaumont 0:7cb648bc5c2a 3 #include "getVersion.h"
wbeaumont 0:7cb648bc5c2a 4
wbeaumont 0:7cb648bc5c2a 5 #include "stdbool.h"
wbeaumont 0:7cb648bc5c2a 6
wbeaumont 0:7cb648bc5c2a 7
wbeaumont 0:7cb648bc5c2a 8 #include "dev_interface_def.h"
wbeaumont 0:7cb648bc5c2a 9 #include "I2CInterface.h"
wbeaumont 0:7cb648bc5c2a 10
wbeaumont 0:7cb648bc5c2a 11
wbeaumont 6:cc295531021e 12 #define VERSION_AT30TSE75x_HDR "0.95"
wbeaumont 0:7cb648bc5c2a 13
wbeaumont 0:7cb648bc5c2a 14 /** AT30TSE75x class.
wbeaumont 0:7cb648bc5c2a 15 * Used for interfacing with a AT30TSE75x temperature sensor and ee-prom
wbeaumont 0:7cb648bc5c2a 16 * For version 0.1 inital
wbeaumont 0:7cb648bc5c2a 17 * It has to be used with the https://developer.mbed.org/users/wbeaumont/code/DevInterfaces/ package
wbeaumont 0:7cb648bc5c2a 18 * This includes the "virtual" I2CInterface class that is the interface to the I2C device
wbeaumont 0:7cb648bc5c2a 19 * An implementation of the I2Cinterface class for the MBED can be found at
wbeaumont 0:7cb648bc5c2a 20 * https://developer.mbed.org/users/wbeaumont/code/I2Cinterfaces/
wbeaumont 0:7cb648bc5c2a 21 * ee-prom set / read not tested / implemented.
wbeaumont 0:7cb648bc5c2a 22 * version 0.8 : read of temperature 12 bits, fixed configuration
wbeaumont 0:7cb648bc5c2a 23 * read /write ee-prom
wbeaumont 1:c0db18a0c56b 24 * version 0.86 : added ee-prom write protect mode tested ( un-protect not tested)
wbeaumont 2:91836ad02096 25 * version 0.90 : added all set and get functions for reading the config register(s)
wbeaumont 6:cc295531021e 26 * most of the functions are not tested .
wbeaumont 6:cc295531021e 27 * version 0.95 : found error in set temperature limit registers , corrected set and get limit register
wbeaumont 6:cc295531021e 28 * function calls
wbeaumont 0:7cb648bc5c2a 29 * this code is only tested with the AT30TSE752 version ( 2 KBit version)
wbeaumont 2:91836ad02096 30 * v
wbeaumont 1:c0db18a0c56b 31 * processor board for the testing : FRDM-KL25Z
wbeaumont 0:7cb648bc5c2a 32 *
wbeaumont 0:7cb648bc5c2a 33 * (C) Wim Beaumont Universiteit Antwerpen 2016, 2017
wbeaumont 0:7cb648bc5c2a 34 *
wbeaumont 0:7cb648bc5c2a 35 */
wbeaumont 0:7cb648bc5c2a 36 class AT30TSE75x : public virtual getVersion {
wbeaumont 0:7cb648bc5c2a 37 uint8_t buffer[4];
wbeaumont 0:7cb648bc5c2a 38 int Taddr, Eaddr; // base address for temperature and eeprom
wbeaumont 0:7cb648bc5c2a 39 int Esize;
wbeaumont 2:91836ad02096 40 void set_LimitRegister(int &error ,uint8_t reg ,float temperature, int Nonvolatile);
wbeaumont 2:91836ad02096 41 float get_LimitRegister(int &error ,uint8_t reg , int Nonvolatile);
wbeaumont 2:91836ad02096 42 void CopyRegisters( int &error , uint8_t reg);
wbeaumont 2:91836ad02096 43 float convert_temperature( uint16_t datain);
wbeaumont 2:91836ad02096 44 uint16_t set_temperature(float temperature );
wbeaumont 2:91836ad02096 45 int initstatus;
wbeaumont 0:7cb648bc5c2a 46 protected:
wbeaumont 2:91836ad02096 47 struct configreg {
wbeaumont 2:91836ad02096 48 bool oneshot;
wbeaumont 2:91836ad02096 49 uint8_t resolution;
wbeaumont 2:91836ad02096 50 uint8_t faultTolQueue;
wbeaumont 2:91836ad02096 51 bool alertpol;
wbeaumont 2:91836ad02096 52 bool alarmMode;
wbeaumont 2:91836ad02096 53 bool shutdownMode;
wbeaumont 2:91836ad02096 54 bool NVregBusy;
wbeaumont 2:91836ad02096 55 bool RegisterLockDown;
wbeaumont 2:91836ad02096 56 bool RegisterLock;
wbeaumont 2:91836ad02096 57 };
wbeaumont 2:91836ad02096 58 struct configreg CregNV, Creg;
wbeaumont 0:7cb648bc5c2a 59 /** pointer to the I2C interface driver. */
wbeaumont 0:7cb648bc5c2a 60 I2CInterface* _i2c;
wbeaumont 0:7cb648bc5c2a 61 public:
wbeaumont 2:91836ad02096 62
wbeaumont 2:91836ad02096 63 enum { active_mode=0, disabled_mode=1 } shutdownmodes;
wbeaumont 2:91836ad02096 64 enum { comparator_mode=0 , interrupt_mode=1 } alarmthermostatmodes;
wbeaumont 0:7cb648bc5c2a 65 AT30TSE75x (I2CInterface* i2cinterface, int device_address_bits, int eepromsize=2);
wbeaumont 2:91836ad02096 66 int getInitStatus(void) { return initstatus;}
wbeaumont 0:7cb648bc5c2a 67 int getTaddr(){ return Taddr;};
wbeaumont 0:7cb648bc5c2a 68 int getEaddr(){ return Eaddr;};
wbeaumont 3:5944e2454e42 69 uint16_t get_temperature_register(int &error);
wbeaumont 2:91836ad02096 70 float get_temperature(int &error);
wbeaumont 2:91836ad02096 71 // write the status of the configreg structure to the register ( NV or volatile ) r
wbeaumont 2:91836ad02096 72 // @error , report error I2C error or 43 in case the register is locked permanent and writing to nonvolatile register
wbeaumont 2:91836ad02096 73 // error is 44 when register lock bit is active (in the structure) no update is done
wbeaumont 2:91836ad02096 74 // @param Nonevolatile , write to Nonvolatile register if 1 ( so no effect )
wbeaumont 2:91836ad02096 75 // write to volatile config register so effect
wbeaumont 2:91836ad02096 76 void set_config( int &error, int Nonvolatile=0 );
wbeaumont 2:91836ad02096 77 uint16_t read_config(int &error, int Nonvolatile=0 );
wbeaumont 2:91836ad02096 78 // set the one_shot bit in the (Volatile) config register and force a temperture read during shutdown mode
wbeaumont 2:91836ad02096 79 // it reads first the config register and then set the bit and writes the config register
wbeaumont 2:91836ad02096 80 // @return 0
wbeaumont 2:91836ad02096 81 void activate_oneshot(int &error );
wbeaumont 2:91836ad02096 82 // reads the resolution bits from the sconfig structure
wbeaumont 2:91836ad02096 83 // @param Nonvolatile if 1 read from the Nonvolatile register
wbeaumont 2:91836ad02096 84 // @param update if 1 reads first the register and fills the structure
wbeaumont 2:91836ad02096 85 // @param error is 0 if success else error ( I2C error in case update =1)
wbeaumont 2:91836ad02096 86 // @return the resolution bits
wbeaumont 2:91836ad02096 87 int get_resolution(int &error, int Nonvolatile=0, bool update=0 );
wbeaumont 2:91836ad02096 88 int get_FaultTollerantQueue(int &error, int Nonvolatile=0, bool update=0 );
wbeaumont 2:91836ad02096 89 int get_AlertPinPolarity(int &error, int Nonvolatile=0, bool update=0 );
wbeaumont 2:91836ad02096 90 int get_AlarmThermostateMode(int &error, int Nonvolatile=0, bool update=0 );
wbeaumont 2:91836ad02096 91 int get_ShutdownMode(int &error, int Nonvolatile=0, bool update=0 );
wbeaumont 2:91836ad02096 92 // reads the resolution bits from the sconfig structure
wbeaumont 2:91836ad02096 93 // @param Nonvolatile has to be 1 , in case of 0 no read is done and error is set to none zero
wbeaumont 2:91836ad02096 94 // @param update if 1 reads first the register and fills the structure
wbeaumont 2:91836ad02096 95 // @param error is 0 if success else error ( I2C error in case update =1)
wbeaumont 2:91836ad02096 96 // @return the Register lock status
wbeaumont 2:91836ad02096 97 int get_RegisterLock (int &error, int Nonvolatile=1, bool update=0 );
wbeaumont 2:91836ad02096 98 int get_RegisterLockdown (int &error, int Nonvolatile=1, bool update=0 );
wbeaumont 2:91836ad02096 99 // reads the config register updates the reg structure and reports the Nonvolatile busy bit
wbeaumont 2:91836ad02096 100 int get_NonevolatileBussy(int &error);
wbeaumont 2:91836ad02096 101
wbeaumont 2:91836ad02096 102 // reads the resolution bits from the sconfig structure
wbeaumont 2:91836ad02096 103 // @resolution to be set can be either bit value 0 .. 3 or 9,10, 11, 12 for bit resolution
wbeaumont 2:91836ad02096 104 // @param Nonvolatile if 1 set the Nonvolatile register
wbeaumont 2:91836ad02096 105 // @param write if 0 only the structure is updated and no write to the config register
wbeaumont 2:91836ad02096 106 // @param update if 1 reads first the register and fills the structure
wbeaumont 2:91836ad02096 107 // @param error is 0 if success else error ( I2C error in case update =1)
wbeaumont 2:91836ad02096 108 void set_resolution(int resolution , int &error, int Nonvolatile=0,bool write=0, bool update=0 );
wbeaumont 2:91836ad02096 109 void set_FaultTollerantQueue(int ftq, int &error, int Nonvolatile=0, bool write=0, bool update=0 );
wbeaumont 2:91836ad02096 110 void set_FaultTollerantQueue(char nrfaults, int &error, int Nonvolatile=0,bool write=0, bool update=0 );
wbeaumont 2:91836ad02096 111 void set_AlertPinPolarity(int pol, int &error, int Nonvolatile=0,bool write=0, bool update=0 );
wbeaumont 2:91836ad02096 112 //void set_AlarmThermostateMode(AT30TSE75x::alarmthermostatmodes mode , int &error, int Nonvolatile=0, bool update=0 ){
wbeaumont 2:91836ad02096 113 // set_AlarmThermostateMode((int) mode , error, Nonvolatile, update=0 );}
wbeaumont 2:91836ad02096 114 void set_AlarmThermostateMode(int mode , int &error, int Nonvolatile=0, bool write=0,bool update=0 );
wbeaumont 2:91836ad02096 115 //void set_ShutdownMode(AT30TSE75x::shutdownmodes mode , int &error, int Nonvolatile=0, bool update=0 ){
wbeaumont 2:91836ad02096 116 // set_ShutdownMode((int) mode , error, Nonvolatile, bool ) ;}
wbeaumont 2:91836ad02096 117 void set_ShutdownMode(int mode , int &error, int Nonvolatile=0,bool write=0, bool update=0 );
wbeaumont 2:91836ad02096 118 // set the register loc bit reads nonvolitaile config register and update the structure CregNV
wbeaumont 2:91836ad02096 119 // @param lock locks status and limits registers ( None Volitaile and volitaile) if set to 1 or unlock ( lock=0)
wbeaumont 2:91836ad02096 120 // @param error is 0 if success else error ( I2C error in case update =1)
wbeaumont 2:91836ad02096 121 void set_RegisterLock (int &error, int lock );
wbeaumont 2:91836ad02096 122 // set the register loc bit reads nonvolitaile config register and update the structure CregNV
wbeaumont 2:91836ad02096 123 // @param error reports an I2C error or wrong condition. Has to be 123 when called;
wbeaumont 2:91836ad02096 124 // use this with caution
wbeaumont 2:91836ad02096 125 // error will be 35 if #define RegisterLocDownEnable 0 in the code file
wbeaumont 2:91836ad02096 126 // error will be 36 if called with error an other value then 123
wbeaumont 2:91836ad02096 127 // most likely this function is not tested.
wbeaumont 2:91836ad02096 128 void set_RegisterLockdown (int &error, int lockpermanent );
wbeaumont 2:91836ad02096 129
wbeaumont 2:91836ad02096 130 // limit registers
wbeaumont 2:91836ad02096 131 // set the low limit temperature register
wbeaumont 2:91836ad02096 132 //@param error reports I2C error , in case the lock bit is set it error will 44 an no attempt is done to write
wbeaumont 2:91836ad02096 133 // if Nonvolatile=1 and the permanentlock bit is set error will be 43
wbeaumont 2:91836ad02096 134 //@param temperature the temperature to be set in the register
wbeaumont 2:91836ad02096 135 //@param Nonvolatile set the Nonvolataile register if set to none zero
wbeaumont 6:cc295531021e 136 void set_TLowLimitRegister(int &error , float temperture, int Nonvolatile=0) ;
wbeaumont 2:91836ad02096 137 void set_THighLimitRegister(int &error , float temperture, int Nonvolatile=0) ;
wbeaumont 5:c783703a9e28 138 float get_THighLimitRegister(int &error , int Nonvolatile=0) ;
wbeaumont 5:c783703a9e28 139 float get_TLowLimitRegister(int &error , int Nonvolatile=0) ;
wbeaumont 2:91836ad02096 140
wbeaumont 2:91836ad02096 141 // copies the Volatile registers to the NoneVolataile registers
wbeaumont 2:91836ad02096 142 // reads the volataile config register to check the NoneVolataile register status
wbeaumont 2:91836ad02096 143 // if bussy waits for 100ms and tries again ( max 300ms wait)
wbeaumont 2:91836ad02096 144 // @param error returns the I2C error ,
wbeaumont 2:91836ad02096 145 // returns 22 in case register stays busy
wbeaumont 2:91836ad02096 146 // returns 43 if lock bit is set
wbeaumont 2:91836ad02096 147 // returns 44 if lockdown bit is set
wbeaumont 2:91836ad02096 148 void CopyVolatile2NoneVolatileRegisters( int &error);
wbeaumont 2:91836ad02096 149 void CopyNonVolatile2VolatileRegisters( int &error) ;
wbeaumont 2:91836ad02096 150
wbeaumont 2:91836ad02096 151 //eeprom
wbeaumont 0:7cb648bc5c2a 152 // max lenght =16 for 1 page. If word =0
wbeaumont 0:7cb648bc5c2a 153 int read_eeprompage(char *data, uint8_t length, uint8_t word_addr, uint8_t page);
wbeaumont 0:7cb648bc5c2a 154 int write_eeprompage(char *data, uint8_t length, uint8_t word_addr, uint8_t page);
wbeaumont 0:7cb648bc5c2a 155 // read a single byte from ee prom
wbeaumont 0:7cb648bc5c2a 156 int read_eeprombyte(char &data, uint8_t word_addr, uint8_t page);
wbeaumont 0:7cb648bc5c2a 157 int write_eeprombyte(char data, uint8_t word_addr, uint8_t page);
wbeaumont 1:c0db18a0c56b 158 // set ee-prom in read only mode special voltage on A0 is needed and A1 and A2 shoud be low.
wbeaumont 1:c0db18a0c56b 159 int protect_eeprom(void);
wbeaumont 1:c0db18a0c56b 160 // set ee-prom in read only mode special voltage on A0 and A1 should be high , A2 should be low
wbeaumont 1:c0db18a0c56b 161 int unprotect_eeprom(void);
wbeaumont 1:c0db18a0c56b 162 // returns I2C err ( NACK) in case protected , else ACK ( no error =0)
wbeaumont 1:c0db18a0c56b 163 int get_eeprom_protec(void);
wbeaumont 0:7cb648bc5c2a 164
wbeaumont 0:7cb648bc5c2a 165 };
wbeaumont 0:7cb648bc5c2a 166
wbeaumont 0:7cb648bc5c2a 167 #endif