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