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