code to access the AT30TSE75x temperature and E-prom device

Dependents:   AT30TSE752TST AT30TSE752TST2

Committer:
wbeaumont
Date:
Wed Jan 18 17:52:08 2017 +0000
Revision:
2:91836ad02096
Parent:
1:c0db18a0c56b
Child:
3:5944e2454e42
added methods to read and write config registers also nonvolatile registers . locking etc.  but most methods are not yet tested

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wbeaumont 0:7cb648bc5c2a 1 #include "mbed.h"
wbeaumont 0:7cb648bc5c2a 2 #include "AT30TSE75x.h"
wbeaumont 0:7cb648bc5c2a 3
wbeaumont 0:7cb648bc5c2a 4 /*
wbeaumont 0:7cb648bc5c2a 5 * info see AT30SE75x.h
wbeaumont 0:7cb648bc5c2a 6 * (C) Wim Beaumont Universiteit Antwerpen 2017
wbeaumont 0:7cb648bc5c2a 7
wbeaumont 0:7cb648bc5c2a 8 * some parts of the code are copied
wbeaumont 0:7cb648bc5c2a 9 * from https://developer.mbed.org/users/akhilpanayam/code/AT30TSE75X/file/0e430cef393b/AT30TSE75X.h
wbeaumont 0:7cb648bc5c2a 10 */
wbeaumont 0:7cb648bc5c2a 11
wbeaumont 0:7cb648bc5c2a 12
wbeaumont 2:91836ad02096 13 #define VERSION_AT30TSE75x_SRC "0.90"
wbeaumont 2:91836ad02096 14
wbeaumont 2:91836ad02096 15 #define RegisterLocDownEnable 0 // set this to one to enable permanent NV register locking , never tested with 1
wbeaumont 0:7cb648bc5c2a 16
wbeaumont 0:7cb648bc5c2a 17 #define AT30TSE75X_ADD_TEMP 0x48 /*Temperature Sensor: 0b1001xxx */
wbeaumont 0:7cb648bc5c2a 18 #define AT30TSE75X_ADD_EEPROM 0x50 /*EEPROM: 0b1010xxx */
wbeaumont 1:c0db18a0c56b 19 #define AT30TSE75X_FIX_EEPROM 0x62 /*fix EEPROM 0b01100010 ( last 0 = W) */
wbeaumont 0:7cb648bc5c2a 20 #define AT30TSE752 1
wbeaumont 0:7cb648bc5c2a 21 #define AT30TSE754 2
wbeaumont 0:7cb648bc5c2a 22 #define AT30TSE758 3
wbeaumont 0:7cb648bc5c2a 23
wbeaumont 0:7cb648bc5c2a 24 #define AT30TSE_CONFIG_RES_9_bit 0
wbeaumont 0:7cb648bc5c2a 25 #define AT30TSE_CONFIG_RES_10_bit 1
wbeaumont 0:7cb648bc5c2a 26 #define AT30TSE_CONFIG_RES_11_bit 2
wbeaumont 0:7cb648bc5c2a 27 #define AT30TSE_CONFIG_RES_12_bit 3
wbeaumont 0:7cb648bc5c2a 28
wbeaumont 0:7cb648bc5c2a 29
wbeaumont 0:7cb648bc5c2a 30 #define AT30TSE_TEMPERATURE_REG_SIZE 2
wbeaumont 0:7cb648bc5c2a 31
wbeaumont 2:91836ad02096 32 #define CopyNV2VolReg 0xB8
wbeaumont 2:91836ad02096 33 #define CopyVolV2NVReg 0x48
wbeaumont 0:7cb648bc5c2a 34
wbeaumont 0:7cb648bc5c2a 35 #define TReg 0x0 // 16 bits
wbeaumont 0:7cb648bc5c2a 36 #define ConfigReg 0x1 // 16 bits
wbeaumont 0:7cb648bc5c2a 37 #define TLowReg 0x2 // 16 bits
wbeaumont 0:7cb648bc5c2a 38 #define THighReg 0x3 // 16 bits
wbeaumont 0:7cb648bc5c2a 39 #define ConfigRegNV 0x11 // 16 bits
wbeaumont 0:7cb648bc5c2a 40 #define TLowRegNV 0x12 // 16 bits
wbeaumont 0:7cb648bc5c2a 41 #define THighRegNV 0x13 // 16 bits
wbeaumont 0:7cb648bc5c2a 42
wbeaumont 2:91836ad02096 43 /* for no all fixed values ..5432 1098 7654 3210
wbeaumont 2:91836ad02096 44 15 normal : 1 ONSHOT 0b1xxx xxxx xxxx xxxx
wbeaumont 2:91836ad02096 45 14:13 12 bit :11 RESOLUTION 0b1RRx xxxx xxxx xxxx
wbeaumont 2:91836ad02096 46 12:11 1 fault:00 FaultTQ 0b111F Fxxx xxxx xxxx
wbeaumont 2:91836ad02096 47 10 0 act low AlertPol 0b1110 0Axx xxxx xxxx
wbeaumont 2:91836ad02096 48 9 0 cmp mode 0b1110 00Cx xxxx xxxx
wbeaumont 2:91836ad02096 49 8 0 temp act SHUTDOWNM 0b1110 000S xxxx xxxx
wbeaumont 2:91836ad02096 50 7:1 dont care 0b1110 0000 0000 000x
wbeaumont 2:91836ad02096 51 0 read only 0b1110 0000 0000 0000
wbeaumont 2:91836ad02096 52 => 0xE0
wbeaumont 2:91836ad02096 53 */
wbeaumont 2:91836ad02096 54
wbeaumont 2:91836ad02096 55 // config
wbeaumont 2:91836ad02096 56 #define ONSHOT_BM 0x8000
wbeaumont 2:91836ad02096 57 #define RESOLUTION_BM 0x6000
wbeaumont 2:91836ad02096 58 #define FaultTQ_BM 0x1800
wbeaumont 2:91836ad02096 59 #define ALERTPOL_BM 0x0400
wbeaumont 2:91836ad02096 60 #define AlarmMode_BM 0x0200
wbeaumont 2:91836ad02096 61 #define SHUTDOWN_BM 0x0100
wbeaumont 2:91836ad02096 62 #define NVREGBUSY_BM 0x0001
wbeaumont 2:91836ad02096 63 #define REGLOCKDOWN_BM 0x0004
wbeaumont 2:91836ad02096 64 #define REGLOCK_BM 0x0002
wbeaumont 2:91836ad02096 65
wbeaumont 2:91836ad02096 66
wbeaumont 0:7cb648bc5c2a 67 AT30TSE75x::AT30TSE75x (I2CInterface* i2cinterface, int device_address_bits,int eepromsize):
wbeaumont 0:7cb648bc5c2a 68 getVersion( VERSION_AT30TSE75x_HDR,VERSION_AT30TSE75x_SRC, __TIME__, __DATE__),_i2c(i2cinterface) {
wbeaumont 0:7cb648bc5c2a 69 Taddr= ((device_address_bits & 0x7) | AT30TSE75X_ADD_TEMP ) & 0x7F; Taddr=Taddr<<1;
wbeaumont 0:7cb648bc5c2a 70 Eaddr= ((device_address_bits & 0x7) | AT30TSE75X_ADD_EEPROM ) & 0x7F; Eaddr=Eaddr<<1;
wbeaumont 0:7cb648bc5c2a 71
wbeaumont 0:7cb648bc5c2a 72 Esize=1;
wbeaumont 0:7cb648bc5c2a 73 if( eepromsize ==4 ) Esize=2; if( eepromsize ==8) Esize=3;
wbeaumont 2:91836ad02096 74
wbeaumont 2:91836ad02096 75 read_config(initstatus);
wbeaumont 2:91836ad02096 76 read_config(initstatus,1);
wbeaumont 2:91836ad02096 77
wbeaumont 0:7cb648bc5c2a 78 }
wbeaumont 0:7cb648bc5c2a 79
wbeaumont 0:7cb648bc5c2a 80
wbeaumont 2:91836ad02096 81 uint16_t AT30TSE75x::set_temperature(float temperature ) {
wbeaumont 2:91836ad02096 82 uint16_t td;
wbeaumont 2:91836ad02096 83 uint16_t sign= 0x8000;
wbeaumont 2:91836ad02096 84 if ( temperature < 0) temperature = -temperature;
wbeaumont 2:91836ad02096 85 else sign=0;
wbeaumont 2:91836ad02096 86 // this function is only used to calculate limits so we don't care
wbeaumont 2:91836ad02096 87 // about resolution settings, all is 12 bits
wbeaumont 2:91836ad02096 88 temperature=temperature *16;
wbeaumont 2:91836ad02096 89 td=(int)temperature;
wbeaumont 2:91836ad02096 90 td= td<<4;
wbeaumont 2:91836ad02096 91 if (sign){ td = ~td; td=td+1;}
wbeaumont 2:91836ad02096 92 td=td|sign;
wbeaumont 2:91836ad02096 93 return td;
wbeaumont 2:91836ad02096 94
wbeaumont 2:91836ad02096 95 }
wbeaumont 0:7cb648bc5c2a 96
wbeaumont 2:91836ad02096 97
wbeaumont 2:91836ad02096 98 float AT30TSE75x::convert_temperature( uint16_t datain) {
wbeaumont 2:91836ad02096 99 uint16_t data=datain;
wbeaumont 2:91836ad02096 100 float temperature;
wbeaumont 0:7cb648bc5c2a 101 int8_t sign = 1;
wbeaumont 0:7cb648bc5c2a 102
wbeaumont 0:7cb648bc5c2a 103 /* Check if negative and clear sign bit. */
wbeaumont 0:7cb648bc5c2a 104 if (data & (1 << 15)) {
wbeaumont 0:7cb648bc5c2a 105 sign *= -1;
wbeaumont 0:7cb648bc5c2a 106 data &= ~(1 << 15);
wbeaumont 0:7cb648bc5c2a 107 }
wbeaumont 0:7cb648bc5c2a 108
wbeaumont 0:7cb648bc5c2a 109 /* Convert to temperature. */
wbeaumont 2:91836ad02096 110 switch (Creg.resolution) {
wbeaumont 0:7cb648bc5c2a 111 case AT30TSE_CONFIG_RES_9_bit:
wbeaumont 0:7cb648bc5c2a 112 data = (data >> 7);
wbeaumont 0:7cb648bc5c2a 113 (temperature) = data * sign * 0.5;
wbeaumont 0:7cb648bc5c2a 114 break;
wbeaumont 0:7cb648bc5c2a 115
wbeaumont 0:7cb648bc5c2a 116 case AT30TSE_CONFIG_RES_10_bit:
wbeaumont 0:7cb648bc5c2a 117 data = (data >> 6);
wbeaumont 0:7cb648bc5c2a 118 (temperature) = data * sign * 0.25;
wbeaumont 0:7cb648bc5c2a 119 break;
wbeaumont 0:7cb648bc5c2a 120
wbeaumont 0:7cb648bc5c2a 121 case AT30TSE_CONFIG_RES_11_bit:
wbeaumont 0:7cb648bc5c2a 122 data = (data >> 5);
wbeaumont 0:7cb648bc5c2a 123 (temperature) = data * sign * 0.125;
wbeaumont 0:7cb648bc5c2a 124 break;
wbeaumont 0:7cb648bc5c2a 125
wbeaumont 0:7cb648bc5c2a 126 case AT30TSE_CONFIG_RES_12_bit:
wbeaumont 0:7cb648bc5c2a 127 data = (data >> 4);
wbeaumont 0:7cb648bc5c2a 128 (temperature) = data * sign * 0.0625;
wbeaumont 0:7cb648bc5c2a 129 break;
wbeaumont 0:7cb648bc5c2a 130
wbeaumont 0:7cb648bc5c2a 131 default:
wbeaumont 0:7cb648bc5c2a 132 break;
wbeaumont 0:7cb648bc5c2a 133 }
wbeaumont 0:7cb648bc5c2a 134 return temperature;
wbeaumont 2:91836ad02096 135
wbeaumont 2:91836ad02096 136 }
wbeaumont 2:91836ad02096 137 float AT30TSE75x::get_temperature(int &error) {
wbeaumont 2:91836ad02096 138
wbeaumont 2:91836ad02096 139
wbeaumont 2:91836ad02096 140 uint16_t data;
wbeaumont 2:91836ad02096 141 int locerr=-200;
wbeaumont 2:91836ad02096 142 buffer[0] = 0;
wbeaumont 2:91836ad02096 143 buffer[1] = 0;
wbeaumont 2:91836ad02096 144
wbeaumont 2:91836ad02096 145 locerr=_i2c->read(Taddr,(char *)buffer,AT30TSE_TEMPERATURE_REG_SIZE,false);
wbeaumont 2:91836ad02096 146 data = (buffer[0] << 8) | buffer[1];
wbeaumont 2:91836ad02096 147
wbeaumont 2:91836ad02096 148 error=locerr; // pointer !=0
wbeaumont 2:91836ad02096 149 return convert_temperature( data);
wbeaumont 0:7cb648bc5c2a 150 }
wbeaumont 0:7cb648bc5c2a 151
wbeaumont 0:7cb648bc5c2a 152
wbeaumont 2:91836ad02096 153 void AT30TSE75x::set_TlowLimitRegister(int &error , float temperture, int Nonvolatile) {
wbeaumont 2:91836ad02096 154 uint8_t reg = TLowReg ;
wbeaumont 2:91836ad02096 155 if ( Nonvolatile) reg = TLowRegNV;
wbeaumont 2:91836ad02096 156 set_LimitRegister(error ,reg , temperture, Nonvolatile);
wbeaumont 2:91836ad02096 157 }
wbeaumont 2:91836ad02096 158
wbeaumont 2:91836ad02096 159 void AT30TSE75x::set_THighLimitRegister(int &error , float temperture, int Nonvolatile) {
wbeaumont 2:91836ad02096 160 uint8_t reg = THighReg ;
wbeaumont 2:91836ad02096 161 if ( Nonvolatile) reg = THighRegNV;
wbeaumont 2:91836ad02096 162 set_LimitRegister(error ,reg , temperture, Nonvolatile);
wbeaumont 2:91836ad02096 163 }
wbeaumont 2:91836ad02096 164 void AT30TSE75x::set_LimitRegister(int &error ,uint8_t reg ,float temperature, int Nonvolatile) {
wbeaumont 2:91836ad02096 165 int locerr;
wbeaumont 2:91836ad02096 166 if( Nonvolatile && CregNV.RegisterLockDown ) { error=43; return ;}
wbeaumont 2:91836ad02096 167 if( CregNV.RegisterLock) { error=44; return; }
wbeaumont 2:91836ad02096 168 buffer[0]= reg;
wbeaumont 2:91836ad02096 169 uint16_t td=set_temperature(temperature);
wbeaumont 2:91836ad02096 170 uint16_t td_low= td & 0x0F;
wbeaumont 2:91836ad02096 171 buffer[2] = (uint8_t) td_low;
wbeaumont 2:91836ad02096 172 td = td >> 8; td= td & 0x0F;
wbeaumont 2:91836ad02096 173 buffer[1] = (uint8_t) td;
wbeaumont 2:91836ad02096 174 locerr=_i2c->write(Taddr,(char *)buffer,3,false);
wbeaumont 2:91836ad02096 175 buffer[0]= TReg;
wbeaumont 2:91836ad02096 176 locerr= locerr<<2;
wbeaumont 2:91836ad02096 177 locerr|=_i2c->write(Taddr,(char *)buffer,1,false); // make sure temp read reg is active again
wbeaumont 2:91836ad02096 178 error= locerr;
wbeaumont 2:91836ad02096 179 }
wbeaumont 2:91836ad02096 180
wbeaumont 2:91836ad02096 181 float AT30TSE75x::get_THighLimitRegister(int &error , float temperture, int Nonvolatile) {
wbeaumont 2:91836ad02096 182 uint8_t reg = THighReg ;
wbeaumont 2:91836ad02096 183 if ( Nonvolatile) reg = THighRegNV;
wbeaumont 2:91836ad02096 184 return get_LimitRegister(error ,reg , Nonvolatile);
wbeaumont 2:91836ad02096 185 }
wbeaumont 2:91836ad02096 186
wbeaumont 2:91836ad02096 187 float AT30TSE75x::get_TLowLimitRegister(int &error , float temperture, int Nonvolatile) {
wbeaumont 2:91836ad02096 188 uint8_t reg = TLowReg ;
wbeaumont 2:91836ad02096 189 if ( Nonvolatile) reg = TLowRegNV;
wbeaumont 2:91836ad02096 190 return get_LimitRegister(error ,reg , Nonvolatile);
wbeaumont 2:91836ad02096 191 }
wbeaumont 2:91836ad02096 192
wbeaumont 2:91836ad02096 193 float AT30TSE75x::get_LimitRegister(int &error ,uint8_t reg , int Nonvolatile) {
wbeaumont 2:91836ad02096 194 int locerr;
wbeaumont 2:91836ad02096 195
wbeaumont 2:91836ad02096 196 buffer[0]= reg;
wbeaumont 2:91836ad02096 197 locerr=_i2c->write(Taddr,(char *)buffer,1,false); // set reg addres to read
wbeaumont 2:91836ad02096 198 error = locerr<<2;
wbeaumont 2:91836ad02096 199 locerr = _i2c->read(Taddr,(char *)buffer,AT30TSE_TEMPERATURE_REG_SIZE,false);
wbeaumont 2:91836ad02096 200 uint16_t data = (buffer[0] << 8) | buffer[1];
wbeaumont 2:91836ad02096 201 buffer[0]= TReg;
wbeaumont 2:91836ad02096 202 locerr= locerr<<2;
wbeaumont 2:91836ad02096 203 locerr|=_i2c->write(Taddr,(char *)buffer,1,false); // make sure temp read reg is active again
wbeaumont 2:91836ad02096 204 error= locerr;
wbeaumont 2:91836ad02096 205 return convert_temperature( data);
wbeaumont 2:91836ad02096 206 }
wbeaumont 2:91836ad02096 207
wbeaumont 2:91836ad02096 208 void AT30TSE75x::CopyNonVolatile2VolatileRegisters( int &error) {
wbeaumont 2:91836ad02096 209 if ( CregNV.RegisterLock ) { error=43; return ;}
wbeaumont 2:91836ad02096 210 if ( CregNV.RegisterLockDown ) { error=44; return ;}
wbeaumont 2:91836ad02096 211 CopyRegisters( error,CopyNV2VolReg);
wbeaumont 2:91836ad02096 212 }
wbeaumont 2:91836ad02096 213
wbeaumont 2:91836ad02096 214 void AT30TSE75x::CopyVolatile2NoneVolatileRegisters( int &error) {
wbeaumont 2:91836ad02096 215 if ( CregNV.RegisterLock ) { error=43; return ;}
wbeaumont 2:91836ad02096 216 CopyRegisters( error, CopyVolV2NVReg);
wbeaumont 2:91836ad02096 217 }
wbeaumont 2:91836ad02096 218
wbeaumont 2:91836ad02096 219 void AT30TSE75x::CopyRegisters( int &error , uint8_t reg) {
wbeaumont 2:91836ad02096 220 int locerr;
wbeaumont 2:91836ad02096 221 bool busy=true;
wbeaumont 2:91836ad02096 222 int lc=3;
wbeaumont 2:91836ad02096 223 while ( busy && lc--) {
wbeaumont 2:91836ad02096 224 (void) read_config(locerr,0);
wbeaumont 2:91836ad02096 225 if ( locerr ) { error =locerr ; return;}
wbeaumont 2:91836ad02096 226 busy =Creg.NVregBusy;
wbeaumont 2:91836ad02096 227 if(busy) wait_ms(100);
wbeaumont 2:91836ad02096 228 }
wbeaumont 2:91836ad02096 229 if ( busy ) { error =22; return ;}
wbeaumont 2:91836ad02096 230
wbeaumont 2:91836ad02096 231 buffer[0]= reg;
wbeaumont 2:91836ad02096 232 locerr=_i2c->write(Taddr,(char *)buffer,1,false);
wbeaumont 2:91836ad02096 233 // not sure if this is needed
wbeaumont 2:91836ad02096 234 buffer[0]= TReg;
wbeaumont 2:91836ad02096 235 locerr= locerr<<2;
wbeaumont 2:91836ad02096 236 locerr|=_i2c->write(Taddr,(char *)buffer,1,false); // make sure temp read reg is active again
wbeaumont 2:91836ad02096 237 error= locerr;
wbeaumont 2:91836ad02096 238 }
wbeaumont 2:91836ad02096 239
wbeaumont 2:91836ad02096 240
wbeaumont 2:91836ad02096 241 uint16_t AT30TSE75x::read_config(int &error, int Nonvolatile ) {
wbeaumont 0:7cb648bc5c2a 242 uint16_t data;
wbeaumont 2:91836ad02096 243 uint16_t td;
wbeaumont 0:7cb648bc5c2a 244 int locerr;
wbeaumont 2:91836ad02096 245 if ( Nonvolatile) buffer[0]= ConfigRegNV; else buffer[0]= ConfigReg;
wbeaumont 0:7cb648bc5c2a 246 locerr=_i2c->write(Taddr,(char *)buffer,1,false);
wbeaumont 0:7cb648bc5c2a 247 locerr= locerr<<2;
wbeaumont 0:7cb648bc5c2a 248 locerr|=_i2c->read(Taddr,(char *)buffer,AT30TSE_TEMPERATURE_REG_SIZE,false);
wbeaumont 0:7cb648bc5c2a 249 locerr= locerr<<2;
wbeaumont 0:7cb648bc5c2a 250 data = (buffer[0] << 8) | buffer[1];
wbeaumont 0:7cb648bc5c2a 251 buffer[0]= TReg;
wbeaumont 2:91836ad02096 252 locerr|=_i2c->write(Taddr,(char *)buffer,1,false); // set back to read Treg
wbeaumont 2:91836ad02096 253 error=locerr;
wbeaumont 2:91836ad02096 254 if ( locerr == 0) {
wbeaumont 2:91836ad02096 255 struct configreg *lcreg;
wbeaumont 2:91836ad02096 256 if( Nonvolatile) lcreg =&CregNV; else lcreg =&Creg;
wbeaumont 2:91836ad02096 257 lcreg->oneshot =bool ( data & ONSHOT_BM); // should be 0 when reading
wbeaumont 2:91836ad02096 258 td = data & RESOLUTION_BM ; td = td >> 13; lcreg->resolution =td;
wbeaumont 2:91836ad02096 259 td = data & FaultTQ_BM ; td = td >> 11; lcreg->faultTolQueue=td;
wbeaumont 2:91836ad02096 260 lcreg->alertpol =bool ( data & ALERTPOL_BM);
wbeaumont 2:91836ad02096 261 lcreg->alarmMode =bool ( data & AlarmMode_BM);
wbeaumont 2:91836ad02096 262 lcreg->shutdownMode =bool ( data & SHUTDOWN_BM);
wbeaumont 2:91836ad02096 263 lcreg->RegisterLockDown =bool ( data & REGLOCKDOWN_BM);
wbeaumont 2:91836ad02096 264 lcreg->RegisterLock =bool ( data & REGLOCK_BM);
wbeaumont 2:91836ad02096 265 lcreg->NVregBusy =bool ( data & NVREGBUSY_BM);
wbeaumont 2:91836ad02096 266 }
wbeaumont 0:7cb648bc5c2a 267 return (int)data;
wbeaumont 0:7cb648bc5c2a 268 }
wbeaumont 0:7cb648bc5c2a 269
wbeaumont 0:7cb648bc5c2a 270
wbeaumont 2:91836ad02096 271 int AT30TSE75x::get_NonevolatileBussy(int &error){
wbeaumont 2:91836ad02096 272 uint16_t data= read_config(error, 0 ) ;
wbeaumont 2:91836ad02096 273 return (int) Creg.NVregBusy;
wbeaumont 2:91836ad02096 274 }
wbeaumont 0:7cb648bc5c2a 275
wbeaumont 2:91836ad02096 276 void AT30TSE75x::set_resolution(int resolution , int &error, int Nonvolatile,bool write, bool update ){
wbeaumont 2:91836ad02096 277 int locerr;
wbeaumont 2:91836ad02096 278 if( resolution ==12 ) resolution = AT30TSE_CONFIG_RES_12_bit;
wbeaumont 2:91836ad02096 279 if( resolution ==11 ) resolution = AT30TSE_CONFIG_RES_11_bit;
wbeaumont 2:91836ad02096 280 if( resolution ==10 ) resolution = AT30TSE_CONFIG_RES_10_bit;
wbeaumont 2:91836ad02096 281 if( resolution ==9 ) resolution = AT30TSE_CONFIG_RES_9_bit;
wbeaumont 2:91836ad02096 282 if ( resolution >=0 && resolution <=3 ) {
wbeaumont 2:91836ad02096 283 if ( update ) read_config(locerr, Nonvolatile ) ;
wbeaumont 2:91836ad02096 284 error=locerr<<2;
wbeaumont 2:91836ad02096 285 if( Nonvolatile) CregNV.resolution=resolution; else Creg.resolution=resolution;
wbeaumont 2:91836ad02096 286 if(write ) set_config(locerr, Nonvolatile ) ;
wbeaumont 2:91836ad02096 287 error |= locerr;
wbeaumont 2:91836ad02096 288 }
wbeaumont 2:91836ad02096 289 error=36; // invalid resolution
wbeaumont 2:91836ad02096 290 }
wbeaumont 2:91836ad02096 291
wbeaumont 2:91836ad02096 292 void AT30TSE75x::set_FaultTollerantQueue(int ftq, int &error, int Nonvolatile, bool write, bool update ){
wbeaumont 2:91836ad02096 293 int locerr;
wbeaumont 2:91836ad02096 294 if ( ftq >=0 && ftq <=3 ) {
wbeaumont 2:91836ad02096 295 if ( update ) read_config(locerr, Nonvolatile ) ;
wbeaumont 2:91836ad02096 296 error=locerr<<2;
wbeaumont 2:91836ad02096 297 if( Nonvolatile) CregNV.faultTolQueue=ftq; else Creg.faultTolQueue=ftq;
wbeaumont 2:91836ad02096 298 if(write ) set_config(locerr, Nonvolatile ) ;
wbeaumont 2:91836ad02096 299 error |= locerr;
wbeaumont 2:91836ad02096 300 }
wbeaumont 2:91836ad02096 301 error=36; // invalid value
wbeaumont 2:91836ad02096 302 }
wbeaumont 2:91836ad02096 303
wbeaumont 2:91836ad02096 304
wbeaumont 2:91836ad02096 305 void AT30TSE75x::set_AlertPinPolarity(int pol, int &error, int Nonvolatile,bool write, bool update ){
wbeaumont 2:91836ad02096 306 int locerr;
wbeaumont 2:91836ad02096 307 bool mode=(bool) pol;
wbeaumont 2:91836ad02096 308 if ( update ) read_config(locerr, Nonvolatile ) ;
wbeaumont 2:91836ad02096 309 error=locerr<<2;
wbeaumont 2:91836ad02096 310 if( Nonvolatile) CregNV.alertpol=mode; else Creg.alertpol=mode;
wbeaumont 2:91836ad02096 311 if(write ) set_config(locerr, Nonvolatile ) ;
wbeaumont 2:91836ad02096 312 error |= locerr;
wbeaumont 2:91836ad02096 313 }
wbeaumont 2:91836ad02096 314
wbeaumont 2:91836ad02096 315 void AT30TSE75x::set_AlarmThermostateMode(int modein, int &error, int Nonvolatile,bool write, bool update ){
wbeaumont 2:91836ad02096 316 int locerr;
wbeaumont 2:91836ad02096 317 bool mode=(bool) modein;
wbeaumont 2:91836ad02096 318 if ( update ) read_config(locerr, Nonvolatile ) ;
wbeaumont 2:91836ad02096 319 error=locerr<<2;
wbeaumont 2:91836ad02096 320 if( Nonvolatile) CregNV.alarmMode=mode; else Creg.alarmMode=mode;
wbeaumont 2:91836ad02096 321 if(write ) set_config(locerr, Nonvolatile ) ;
wbeaumont 2:91836ad02096 322 error |= locerr;
wbeaumont 2:91836ad02096 323 }
wbeaumont 2:91836ad02096 324
wbeaumont 2:91836ad02096 325
wbeaumont 2:91836ad02096 326 void AT30TSE75x::set_ShutdownMode(int modein, int &error, int Nonvolatile,bool write, bool update ){
wbeaumont 2:91836ad02096 327 int locerr;
wbeaumont 2:91836ad02096 328 bool mode=(bool) modein;
wbeaumont 2:91836ad02096 329 if ( update ) read_config(locerr, Nonvolatile ) ;
wbeaumont 2:91836ad02096 330 error=locerr<<2;
wbeaumont 2:91836ad02096 331 if( Nonvolatile) CregNV.shutdownMode=mode; else Creg.shutdownMode=mode;
wbeaumont 2:91836ad02096 332 if(write ) set_config(locerr, Nonvolatile ) ;
wbeaumont 2:91836ad02096 333 error |= locerr;
wbeaumont 2:91836ad02096 334 }
wbeaumont 2:91836ad02096 335 void AT30TSE75x::set_FaultTollerantQueue(char nrfaults, int &error, int Nonvolatile, bool write, bool update ){
wbeaumont 2:91836ad02096 336 int ftq=7;
wbeaumont 2:91836ad02096 337 if ( nrfaults == '1') ftq=0;
wbeaumont 2:91836ad02096 338 if ( nrfaults == '2') ftq=1;
wbeaumont 2:91836ad02096 339 if ( nrfaults == '4') ftq=2;
wbeaumont 2:91836ad02096 340 if ( nrfaults == '6') ftq=3;
wbeaumont 2:91836ad02096 341 set_FaultTollerantQueue( ftq,error, Nonvolatile, write, update);
wbeaumont 2:91836ad02096 342 }
wbeaumont 2:91836ad02096 343
wbeaumont 2:91836ad02096 344 void AT30TSE75x::activate_oneshot(int &error ){
wbeaumont 2:91836ad02096 345 int locerr;
wbeaumont 2:91836ad02096 346 buffer[0]= ConfigReg;
wbeaumont 2:91836ad02096 347 locerr=_i2c->write(Taddr,(char *)buffer,1,false);
wbeaumont 2:91836ad02096 348 locerr= locerr<<2;
wbeaumont 2:91836ad02096 349 locerr|=_i2c->read(Taddr,(char *)buffer,AT30TSE_TEMPERATURE_REG_SIZE,false);
wbeaumont 2:91836ad02096 350 locerr= locerr<<2;
wbeaumont 2:91836ad02096 351 buffer[0]= ConfigReg;
wbeaumont 2:91836ad02096 352 buffer[1] = 0x80 ; //set once shot MSByte
wbeaumont 2:91836ad02096 353 buffer[2] = 0 ; // volataille
wbeaumont 0:7cb648bc5c2a 354 locerr=_i2c->write(Taddr,(char *)buffer,3,false);
wbeaumont 0:7cb648bc5c2a 355 buffer[0]= TReg;
wbeaumont 2:91836ad02096 356 locerr= locerr<<2;
wbeaumont 0:7cb648bc5c2a 357 locerr|=_i2c->write(Taddr,(char *)buffer,1,false); // make sure temp read reg is active again
wbeaumont 2:91836ad02096 358 error=locerr;
wbeaumont 2:91836ad02096 359 }
wbeaumont 2:91836ad02096 360
wbeaumont 2:91836ad02096 361 void AT30TSE75x::set_RegisterLock (int &error, int lock ){
wbeaumont 2:91836ad02096 362 int locerr;
wbeaumont 2:91836ad02096 363 (void)read_config(locerr,1 );
wbeaumont 2:91836ad02096 364 error= locerr<<2;
wbeaumont 2:91836ad02096 365 CregNV.RegisterLock =lock;
wbeaumont 2:91836ad02096 366 set_config(locerr,1 );
wbeaumont 2:91836ad02096 367 error |= locerr;
wbeaumont 2:91836ad02096 368 }
wbeaumont 2:91836ad02096 369
wbeaumont 2:91836ad02096 370 void AT30TSE75x::set_RegisterLockdown (int &error, int lockpermanent ){
wbeaumont 2:91836ad02096 371 int locerr;
wbeaumont 2:91836ad02096 372 if( RegisterLocDownEnable) {
wbeaumont 2:91836ad02096 373 if ( error != 123) {error=36; return;}
wbeaumont 2:91836ad02096 374 (void)read_config(locerr,1 );
wbeaumont 2:91836ad02096 375 error= locerr<<2;
wbeaumont 2:91836ad02096 376 CregNV. RegisterLockDown = lockpermanent;
wbeaumont 2:91836ad02096 377 set_config(locerr,1 );
wbeaumont 2:91836ad02096 378 error |= locerr;
wbeaumont 2:91836ad02096 379 }
wbeaumont 2:91836ad02096 380 else { error =36 ;}
wbeaumont 2:91836ad02096 381 }
wbeaumont 2:91836ad02096 382
wbeaumont 2:91836ad02096 383
wbeaumont 2:91836ad02096 384
wbeaumont 2:91836ad02096 385 void AT30TSE75x::set_config( int &error, int Nonvolatile ) {
wbeaumont 2:91836ad02096 386 int locerr;
wbeaumont 2:91836ad02096 387 struct configreg *lcreg=&Creg;
wbeaumont 2:91836ad02096 388 if ( Creg.RegisterLockDown && Nonvolatile ) { locerr = 43; return; }
wbeaumont 2:91836ad02096 389 if ( Creg.RegisterLock ) { locerr = 44; return; }
wbeaumont 2:91836ad02096 390 if ( Nonvolatile) buffer[0]= ConfigRegNV; else buffer[0]= ConfigReg;
wbeaumont 2:91836ad02096 391 if( Nonvolatile) lcreg =&CregNV; else lcreg =&Creg;
wbeaumont 2:91836ad02096 392 uint16_t ld=0, td;
wbeaumont 2:91836ad02096 393 if ( lcreg->oneshot) ld= ONSHOT_BM; // no effect for NV reg
wbeaumont 2:91836ad02096 394 td= (lcreg->resolution) <<13; ld= ld | td;
wbeaumont 2:91836ad02096 395 td= (lcreg->faultTolQueue) <<11; ld= ld | td;
wbeaumont 2:91836ad02096 396 if ( lcreg->alertpol) ld=ld |ALERTPOL_BM;
wbeaumont 2:91836ad02096 397 if ( lcreg-> alarmMode) ld=ld |AlarmMode_BM;
wbeaumont 2:91836ad02096 398 if ( lcreg-> shutdownMode) ld=ld |SHUTDOWN_BM;
wbeaumont 2:91836ad02096 399 if ( lcreg-> RegisterLockDown) ld=ld |(REGLOCKDOWN_BM & RegisterLocDownEnable);
wbeaumont 2:91836ad02096 400 if ( lcreg-> RegisterLock) ld=ld |(REGLOCK_BM );
wbeaumont 2:91836ad02096 401 if( Nonvolatile ) ld=ld; else ld= 0xFF00 & ld;
wbeaumont 2:91836ad02096 402 buffer[2]=ld & 0xFF; ld = ld>>8;
wbeaumont 2:91836ad02096 403 buffer[1]=ld & 0xFF;
wbeaumont 2:91836ad02096 404 locerr=_i2c->write(Taddr,(char *)buffer,3,false);
wbeaumont 2:91836ad02096 405 buffer[0]= TReg;
wbeaumont 2:91836ad02096 406 locerr= locerr<<2;
wbeaumont 2:91836ad02096 407 locerr|=_i2c->write(Taddr,(char *)buffer,1,false); // make sure temp read reg is active again
wbeaumont 2:91836ad02096 408 error= locerr;
wbeaumont 0:7cb648bc5c2a 409 }
wbeaumont 0:7cb648bc5c2a 410
wbeaumont 0:7cb648bc5c2a 411
wbeaumont 0:7cb648bc5c2a 412
wbeaumont 0:7cb648bc5c2a 413
wbeaumont 0:7cb648bc5c2a 414 int AT30TSE75x::read_eeprompage(char *data, uint8_t length, uint8_t word_addr, uint8_t page) {
wbeaumont 0:7cb648bc5c2a 415 char buff[20];
wbeaumont 0:7cb648bc5c2a 416 buff[0] = (word_addr & 0x0F) | ((0x0F & page) << 4);
wbeaumont 0:7cb648bc5c2a 417
wbeaumont 0:7cb648bc5c2a 418 _i2c->write(Eaddr,buff,1,false);
wbeaumont 0:7cb648bc5c2a 419 return _i2c->read(Eaddr,data,length,false);
wbeaumont 0:7cb648bc5c2a 420 }
wbeaumont 0:7cb648bc5c2a 421
wbeaumont 0:7cb648bc5c2a 422 int AT30TSE75x::write_eeprompage(char *data, uint8_t length, uint8_t word_addr, uint8_t page)
wbeaumont 0:7cb648bc5c2a 423 {
wbeaumont 0:7cb648bc5c2a 424 char buff[length+1];
wbeaumont 0:7cb648bc5c2a 425 int buffsizep1=length+1;//buffersize+1
wbeaumont 0:7cb648bc5c2a 426 buff[0] = (word_addr & 0x0F) | ((0x0F & page) << 4);
wbeaumont 0:7cb648bc5c2a 427 for(int i = 1; i < buffsizep1; i++) {
wbeaumont 0:7cb648bc5c2a 428 buff[i] = *(data++);
wbeaumont 0:7cb648bc5c2a 429 }
wbeaumont 0:7cb648bc5c2a 430 return _i2c->write(Eaddr,buff,(length + 1),false);
wbeaumont 0:7cb648bc5c2a 431 }
wbeaumont 0:7cb648bc5c2a 432
wbeaumont 0:7cb648bc5c2a 433
wbeaumont 0:7cb648bc5c2a 434 int AT30TSE75x::read_eeprombyte(char &data, uint8_t word_addr, uint8_t page){
wbeaumont 0:7cb648bc5c2a 435 char rbuf[2];
wbeaumont 0:7cb648bc5c2a 436 int i2cresult=read_eeprompage(rbuf,1,word_addr,page);
wbeaumont 0:7cb648bc5c2a 437 data=rbuf[0];
wbeaumont 0:7cb648bc5c2a 438 return i2cresult;
wbeaumont 0:7cb648bc5c2a 439 }
wbeaumont 0:7cb648bc5c2a 440
wbeaumont 0:7cb648bc5c2a 441
wbeaumont 0:7cb648bc5c2a 442 int AT30TSE75x::write_eeprombyte(char data, uint8_t word_addr, uint8_t page){
wbeaumont 0:7cb648bc5c2a 443 char wbuf[2]; wbuf[0]=data;
wbeaumont 0:7cb648bc5c2a 444 return write_eeprompage(wbuf,1,word_addr,page);
wbeaumont 1:c0db18a0c56b 445 }
wbeaumont 1:c0db18a0c56b 446
wbeaumont 1:c0db18a0c56b 447 int AT30TSE75x::protect_eeprom(void) {
wbeaumont 1:c0db18a0c56b 448 char wbuf[2]; wbuf[0]=0;wbuf[1]=0; // don't care
wbeaumont 1:c0db18a0c56b 449 return _i2c->write(AT30TSE75X_FIX_EEPROM ,wbuf,2,false);
wbeaumont 1:c0db18a0c56b 450 }
wbeaumont 1:c0db18a0c56b 451
wbeaumont 1:c0db18a0c56b 452 int AT30TSE75x::unprotect_eeprom(void) {
wbeaumont 1:c0db18a0c56b 453 char wbuf[2]; wbuf[0]=0;wbuf[1]=0; // don't care
wbeaumont 1:c0db18a0c56b 454 return _i2c->write((0x4 | AT30TSE75X_FIX_EEPROM) ,wbuf,2,false);
wbeaumont 1:c0db18a0c56b 455 }
wbeaumont 1:c0db18a0c56b 456
wbeaumont 1:c0db18a0c56b 457 int AT30TSE75x::get_eeprom_protec(void){
wbeaumont 1:c0db18a0c56b 458 char wbuf[2];
wbeaumont 1:c0db18a0c56b 459 return _i2c->read(AT30TSE75X_FIX_EEPROM ,wbuf,1,false);
wbeaumont 1:c0db18a0c56b 460 }
wbeaumont 1:c0db18a0c56b 461