code to access the AT30TSE75x temperature and E-prom device
Dependents: AT30TSE752TST AT30TSE752TST2
AT30TSE75x.cpp@4:8189e5cb4459, 2017-03-15 (annotated)
- Committer:
- wbeaumont
- Date:
- Wed Mar 15 17:15:53 2017 +0000
- Revision:
- 4:8189e5cb4459
- Parent:
- 3:5944e2454e42
- Child:
- 5:c783703a9e28
removed mbed.h from cpp file ( mbed specific)
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 | 4:8189e5cb4459 | 14 | #define VERSION_AT30TSE75x_SRC "0.91" |
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 | 2:91836ad02096 | 85 | if ( temperature < 0) temperature = -temperature; |
wbeaumont | 2:91836ad02096 | 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 | 2:91836ad02096 | 91 | td= td<<4; |
wbeaumont | 2:91836ad02096 | 92 | if (sign){ td = ~td; td=td+1;} |
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 | 0:7cb648bc5c2a | 102 | int8_t 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 | 0:7cb648bc5c2a | 107 | data &= ~(1 << 15); |
wbeaumont | 0:7cb648bc5c2a | 108 | } |
wbeaumont | 0:7cb648bc5c2a | 109 | |
wbeaumont | 0:7cb648bc5c2a | 110 | /* Convert to temperature. */ |
wbeaumont | 2:91836ad02096 | 111 | switch (Creg.resolution) { |
wbeaumont | 0:7cb648bc5c2a | 112 | case AT30TSE_CONFIG_RES_9_bit: |
wbeaumont | 0:7cb648bc5c2a | 113 | data = (data >> 7); |
wbeaumont | 0:7cb648bc5c2a | 114 | (temperature) = data * sign * 0.5; |
wbeaumont | 0:7cb648bc5c2a | 115 | break; |
wbeaumont | 0:7cb648bc5c2a | 116 | |
wbeaumont | 0:7cb648bc5c2a | 117 | case AT30TSE_CONFIG_RES_10_bit: |
wbeaumont | 0:7cb648bc5c2a | 118 | data = (data >> 6); |
wbeaumont | 0:7cb648bc5c2a | 119 | (temperature) = data * sign * 0.25; |
wbeaumont | 0:7cb648bc5c2a | 120 | break; |
wbeaumont | 0:7cb648bc5c2a | 121 | |
wbeaumont | 0:7cb648bc5c2a | 122 | case AT30TSE_CONFIG_RES_11_bit: |
wbeaumont | 0:7cb648bc5c2a | 123 | data = (data >> 5); |
wbeaumont | 0:7cb648bc5c2a | 124 | (temperature) = data * sign * 0.125; |
wbeaumont | 0:7cb648bc5c2a | 125 | break; |
wbeaumont | 0:7cb648bc5c2a | 126 | |
wbeaumont | 0:7cb648bc5c2a | 127 | case AT30TSE_CONFIG_RES_12_bit: |
wbeaumont | 0:7cb648bc5c2a | 128 | data = (data >> 4); |
wbeaumont | 0:7cb648bc5c2a | 129 | (temperature) = data * sign * 0.0625; |
wbeaumont | 0:7cb648bc5c2a | 130 | break; |
wbeaumont | 0:7cb648bc5c2a | 131 | |
wbeaumont | 0:7cb648bc5c2a | 132 | default: |
wbeaumont | 0:7cb648bc5c2a | 133 | break; |
wbeaumont | 0:7cb648bc5c2a | 134 | } |
wbeaumont | 0:7cb648bc5c2a | 135 | return temperature; |
wbeaumont | 2:91836ad02096 | 136 | |
wbeaumont | 2:91836ad02096 | 137 | } |
wbeaumont | 3:5944e2454e42 | 138 | |
wbeaumont | 2:91836ad02096 | 139 | float AT30TSE75x::get_temperature(int &error) { |
wbeaumont | 3:5944e2454e42 | 140 | return convert_temperature( get_temperature_register(error)); |
wbeaumont | 3:5944e2454e42 | 141 | } |
wbeaumont | 3:5944e2454e42 | 142 | |
wbeaumont | 3:5944e2454e42 | 143 | uint16_t AT30TSE75x::get_temperature_register(int &error){ |
wbeaumont | 2:91836ad02096 | 144 | |
wbeaumont | 2:91836ad02096 | 145 | uint16_t data; |
wbeaumont | 2:91836ad02096 | 146 | int locerr=-200; |
wbeaumont | 2:91836ad02096 | 147 | buffer[0] = 0; |
wbeaumont | 2:91836ad02096 | 148 | buffer[1] = 0; |
wbeaumont | 2:91836ad02096 | 149 | |
wbeaumont | 2:91836ad02096 | 150 | locerr=_i2c->read(Taddr,(char *)buffer,AT30TSE_TEMPERATURE_REG_SIZE,false); |
wbeaumont | 2:91836ad02096 | 151 | data = (buffer[0] << 8) | buffer[1]; |
wbeaumont | 2:91836ad02096 | 152 | |
wbeaumont | 2:91836ad02096 | 153 | error=locerr; // pointer !=0 |
wbeaumont | 3:5944e2454e42 | 154 | return data; |
wbeaumont | 0:7cb648bc5c2a | 155 | } |
wbeaumont | 0:7cb648bc5c2a | 156 | |
wbeaumont | 0:7cb648bc5c2a | 157 | |
wbeaumont | 2:91836ad02096 | 158 | void AT30TSE75x::set_TlowLimitRegister(int &error , float temperture, int Nonvolatile) { |
wbeaumont | 2:91836ad02096 | 159 | uint8_t reg = TLowReg ; |
wbeaumont | 2:91836ad02096 | 160 | if ( Nonvolatile) reg = TLowRegNV; |
wbeaumont | 2:91836ad02096 | 161 | set_LimitRegister(error ,reg , temperture, Nonvolatile); |
wbeaumont | 2:91836ad02096 | 162 | } |
wbeaumont | 2:91836ad02096 | 163 | |
wbeaumont | 2:91836ad02096 | 164 | void AT30TSE75x::set_THighLimitRegister(int &error , float temperture, int Nonvolatile) { |
wbeaumont | 2:91836ad02096 | 165 | uint8_t reg = THighReg ; |
wbeaumont | 2:91836ad02096 | 166 | if ( Nonvolatile) reg = THighRegNV; |
wbeaumont | 2:91836ad02096 | 167 | set_LimitRegister(error ,reg , temperture, Nonvolatile); |
wbeaumont | 2:91836ad02096 | 168 | } |
wbeaumont | 2:91836ad02096 | 169 | void AT30TSE75x::set_LimitRegister(int &error ,uint8_t reg ,float temperature, int Nonvolatile) { |
wbeaumont | 2:91836ad02096 | 170 | int locerr; |
wbeaumont | 2:91836ad02096 | 171 | if( Nonvolatile && CregNV.RegisterLockDown ) { error=43; return ;} |
wbeaumont | 2:91836ad02096 | 172 | if( CregNV.RegisterLock) { error=44; return; } |
wbeaumont | 2:91836ad02096 | 173 | buffer[0]= reg; |
wbeaumont | 2:91836ad02096 | 174 | uint16_t td=set_temperature(temperature); |
wbeaumont | 2:91836ad02096 | 175 | uint16_t td_low= td & 0x0F; |
wbeaumont | 2:91836ad02096 | 176 | buffer[2] = (uint8_t) td_low; |
wbeaumont | 2:91836ad02096 | 177 | td = td >> 8; td= td & 0x0F; |
wbeaumont | 2:91836ad02096 | 178 | buffer[1] = (uint8_t) td; |
wbeaumont | 2:91836ad02096 | 179 | locerr=_i2c->write(Taddr,(char *)buffer,3,false); |
wbeaumont | 2:91836ad02096 | 180 | buffer[0]= TReg; |
wbeaumont | 2:91836ad02096 | 181 | locerr= locerr<<2; |
wbeaumont | 2:91836ad02096 | 182 | locerr|=_i2c->write(Taddr,(char *)buffer,1,false); // make sure temp read reg is active again |
wbeaumont | 2:91836ad02096 | 183 | error= locerr; |
wbeaumont | 2:91836ad02096 | 184 | } |
wbeaumont | 2:91836ad02096 | 185 | |
wbeaumont | 2:91836ad02096 | 186 | float AT30TSE75x::get_THighLimitRegister(int &error , float temperture, int Nonvolatile) { |
wbeaumont | 2:91836ad02096 | 187 | uint8_t reg = THighReg ; |
wbeaumont | 2:91836ad02096 | 188 | if ( Nonvolatile) reg = THighRegNV; |
wbeaumont | 2:91836ad02096 | 189 | return get_LimitRegister(error ,reg , Nonvolatile); |
wbeaumont | 2:91836ad02096 | 190 | } |
wbeaumont | 2:91836ad02096 | 191 | |
wbeaumont | 2:91836ad02096 | 192 | float AT30TSE75x::get_TLowLimitRegister(int &error , float temperture, int Nonvolatile) { |
wbeaumont | 2:91836ad02096 | 193 | uint8_t reg = TLowReg ; |
wbeaumont | 2:91836ad02096 | 194 | if ( Nonvolatile) reg = TLowRegNV; |
wbeaumont | 2:91836ad02096 | 195 | return get_LimitRegister(error ,reg , Nonvolatile); |
wbeaumont | 2:91836ad02096 | 196 | } |
wbeaumont | 2:91836ad02096 | 197 | |
wbeaumont | 2:91836ad02096 | 198 | float AT30TSE75x::get_LimitRegister(int &error ,uint8_t reg , int Nonvolatile) { |
wbeaumont | 2:91836ad02096 | 199 | int locerr; |
wbeaumont | 2:91836ad02096 | 200 | |
wbeaumont | 2:91836ad02096 | 201 | buffer[0]= reg; |
wbeaumont | 2:91836ad02096 | 202 | locerr=_i2c->write(Taddr,(char *)buffer,1,false); // set reg addres to read |
wbeaumont | 2:91836ad02096 | 203 | error = locerr<<2; |
wbeaumont | 2:91836ad02096 | 204 | locerr = _i2c->read(Taddr,(char *)buffer,AT30TSE_TEMPERATURE_REG_SIZE,false); |
wbeaumont | 2:91836ad02096 | 205 | uint16_t data = (buffer[0] << 8) | buffer[1]; |
wbeaumont | 2:91836ad02096 | 206 | buffer[0]= TReg; |
wbeaumont | 2:91836ad02096 | 207 | locerr= locerr<<2; |
wbeaumont | 2:91836ad02096 | 208 | locerr|=_i2c->write(Taddr,(char *)buffer,1,false); // make sure temp read reg is active again |
wbeaumont | 2:91836ad02096 | 209 | error= locerr; |
wbeaumont | 2:91836ad02096 | 210 | return convert_temperature( data); |
wbeaumont | 2:91836ad02096 | 211 | } |
wbeaumont | 2:91836ad02096 | 212 | |
wbeaumont | 2:91836ad02096 | 213 | void AT30TSE75x::CopyNonVolatile2VolatileRegisters( int &error) { |
wbeaumont | 2:91836ad02096 | 214 | if ( CregNV.RegisterLock ) { error=43; return ;} |
wbeaumont | 2:91836ad02096 | 215 | if ( CregNV.RegisterLockDown ) { error=44; return ;} |
wbeaumont | 2:91836ad02096 | 216 | CopyRegisters( error,CopyNV2VolReg); |
wbeaumont | 2:91836ad02096 | 217 | } |
wbeaumont | 2:91836ad02096 | 218 | |
wbeaumont | 2:91836ad02096 | 219 | void AT30TSE75x::CopyVolatile2NoneVolatileRegisters( int &error) { |
wbeaumont | 2:91836ad02096 | 220 | if ( CregNV.RegisterLock ) { error=43; return ;} |
wbeaumont | 2:91836ad02096 | 221 | CopyRegisters( error, CopyVolV2NVReg); |
wbeaumont | 2:91836ad02096 | 222 | } |
wbeaumont | 2:91836ad02096 | 223 | |
wbeaumont | 2:91836ad02096 | 224 | void AT30TSE75x::CopyRegisters( int &error , uint8_t reg) { |
wbeaumont | 2:91836ad02096 | 225 | int locerr; |
wbeaumont | 2:91836ad02096 | 226 | bool busy=true; |
wbeaumont | 2:91836ad02096 | 227 | int lc=3; |
wbeaumont | 2:91836ad02096 | 228 | while ( busy && lc--) { |
wbeaumont | 2:91836ad02096 | 229 | (void) read_config(locerr,0); |
wbeaumont | 2:91836ad02096 | 230 | if ( locerr ) { error =locerr ; return;} |
wbeaumont | 2:91836ad02096 | 231 | busy =Creg.NVregBusy; |
wbeaumont | 4:8189e5cb4459 | 232 | if(busy) _i2c->wait_for_ms(100); |
wbeaumont | 2:91836ad02096 | 233 | } |
wbeaumont | 2:91836ad02096 | 234 | if ( busy ) { error =22; return ;} |
wbeaumont | 2:91836ad02096 | 235 | |
wbeaumont | 2:91836ad02096 | 236 | buffer[0]= reg; |
wbeaumont | 2:91836ad02096 | 237 | locerr=_i2c->write(Taddr,(char *)buffer,1,false); |
wbeaumont | 2:91836ad02096 | 238 | // not sure if this is needed |
wbeaumont | 2:91836ad02096 | 239 | buffer[0]= TReg; |
wbeaumont | 2:91836ad02096 | 240 | locerr= locerr<<2; |
wbeaumont | 2:91836ad02096 | 241 | locerr|=_i2c->write(Taddr,(char *)buffer,1,false); // make sure temp read reg is active again |
wbeaumont | 2:91836ad02096 | 242 | error= locerr; |
wbeaumont | 2:91836ad02096 | 243 | } |
wbeaumont | 2:91836ad02096 | 244 | |
wbeaumont | 2:91836ad02096 | 245 | |
wbeaumont | 2:91836ad02096 | 246 | uint16_t AT30TSE75x::read_config(int &error, int Nonvolatile ) { |
wbeaumont | 0:7cb648bc5c2a | 247 | uint16_t data; |
wbeaumont | 2:91836ad02096 | 248 | uint16_t td; |
wbeaumont | 0:7cb648bc5c2a | 249 | int locerr; |
wbeaumont | 2:91836ad02096 | 250 | if ( Nonvolatile) buffer[0]= ConfigRegNV; else buffer[0]= ConfigReg; |
wbeaumont | 0:7cb648bc5c2a | 251 | locerr=_i2c->write(Taddr,(char *)buffer,1,false); |
wbeaumont | 0:7cb648bc5c2a | 252 | locerr= locerr<<2; |
wbeaumont | 0:7cb648bc5c2a | 253 | locerr|=_i2c->read(Taddr,(char *)buffer,AT30TSE_TEMPERATURE_REG_SIZE,false); |
wbeaumont | 0:7cb648bc5c2a | 254 | locerr= locerr<<2; |
wbeaumont | 0:7cb648bc5c2a | 255 | data = (buffer[0] << 8) | buffer[1]; |
wbeaumont | 0:7cb648bc5c2a | 256 | buffer[0]= TReg; |
wbeaumont | 2:91836ad02096 | 257 | locerr|=_i2c->write(Taddr,(char *)buffer,1,false); // set back to read Treg |
wbeaumont | 2:91836ad02096 | 258 | error=locerr; |
wbeaumont | 2:91836ad02096 | 259 | if ( locerr == 0) { |
wbeaumont | 2:91836ad02096 | 260 | struct configreg *lcreg; |
wbeaumont | 2:91836ad02096 | 261 | if( Nonvolatile) lcreg =&CregNV; else lcreg =&Creg; |
wbeaumont | 2:91836ad02096 | 262 | lcreg->oneshot =bool ( data & ONSHOT_BM); // should be 0 when reading |
wbeaumont | 2:91836ad02096 | 263 | td = data & RESOLUTION_BM ; td = td >> 13; lcreg->resolution =td; |
wbeaumont | 2:91836ad02096 | 264 | td = data & FaultTQ_BM ; td = td >> 11; lcreg->faultTolQueue=td; |
wbeaumont | 2:91836ad02096 | 265 | lcreg->alertpol =bool ( data & ALERTPOL_BM); |
wbeaumont | 2:91836ad02096 | 266 | lcreg->alarmMode =bool ( data & AlarmMode_BM); |
wbeaumont | 2:91836ad02096 | 267 | lcreg->shutdownMode =bool ( data & SHUTDOWN_BM); |
wbeaumont | 2:91836ad02096 | 268 | lcreg->RegisterLockDown =bool ( data & REGLOCKDOWN_BM); |
wbeaumont | 2:91836ad02096 | 269 | lcreg->RegisterLock =bool ( data & REGLOCK_BM); |
wbeaumont | 2:91836ad02096 | 270 | lcreg->NVregBusy =bool ( data & NVREGBUSY_BM); |
wbeaumont | 2:91836ad02096 | 271 | } |
wbeaumont | 0:7cb648bc5c2a | 272 | return (int)data; |
wbeaumont | 0:7cb648bc5c2a | 273 | } |
wbeaumont | 0:7cb648bc5c2a | 274 | |
wbeaumont | 0:7cb648bc5c2a | 275 | |
wbeaumont | 2:91836ad02096 | 276 | int AT30TSE75x::get_NonevolatileBussy(int &error){ |
wbeaumont | 2:91836ad02096 | 277 | uint16_t data= read_config(error, 0 ) ; |
wbeaumont | 2:91836ad02096 | 278 | return (int) Creg.NVregBusy; |
wbeaumont | 2:91836ad02096 | 279 | } |
wbeaumont | 0:7cb648bc5c2a | 280 | |
wbeaumont | 2:91836ad02096 | 281 | void AT30TSE75x::set_resolution(int resolution , int &error, int Nonvolatile,bool write, bool update ){ |
wbeaumont | 2:91836ad02096 | 282 | int locerr; |
wbeaumont | 2:91836ad02096 | 283 | if( resolution ==12 ) resolution = AT30TSE_CONFIG_RES_12_bit; |
wbeaumont | 2:91836ad02096 | 284 | if( resolution ==11 ) resolution = AT30TSE_CONFIG_RES_11_bit; |
wbeaumont | 2:91836ad02096 | 285 | if( resolution ==10 ) resolution = AT30TSE_CONFIG_RES_10_bit; |
wbeaumont | 2:91836ad02096 | 286 | if( resolution ==9 ) resolution = AT30TSE_CONFIG_RES_9_bit; |
wbeaumont | 2:91836ad02096 | 287 | if ( resolution >=0 && resolution <=3 ) { |
wbeaumont | 2:91836ad02096 | 288 | if ( update ) read_config(locerr, Nonvolatile ) ; |
wbeaumont | 2:91836ad02096 | 289 | error=locerr<<2; |
wbeaumont | 2:91836ad02096 | 290 | if( Nonvolatile) CregNV.resolution=resolution; else Creg.resolution=resolution; |
wbeaumont | 2:91836ad02096 | 291 | if(write ) set_config(locerr, Nonvolatile ) ; |
wbeaumont | 2:91836ad02096 | 292 | error |= locerr; |
wbeaumont | 2:91836ad02096 | 293 | } |
wbeaumont | 2:91836ad02096 | 294 | error=36; // invalid resolution |
wbeaumont | 2:91836ad02096 | 295 | } |
wbeaumont | 2:91836ad02096 | 296 | |
wbeaumont | 2:91836ad02096 | 297 | void AT30TSE75x::set_FaultTollerantQueue(int ftq, int &error, int Nonvolatile, bool write, bool update ){ |
wbeaumont | 2:91836ad02096 | 298 | int locerr; |
wbeaumont | 2:91836ad02096 | 299 | if ( ftq >=0 && ftq <=3 ) { |
wbeaumont | 2:91836ad02096 | 300 | if ( update ) read_config(locerr, Nonvolatile ) ; |
wbeaumont | 2:91836ad02096 | 301 | error=locerr<<2; |
wbeaumont | 2:91836ad02096 | 302 | if( Nonvolatile) CregNV.faultTolQueue=ftq; else Creg.faultTolQueue=ftq; |
wbeaumont | 2:91836ad02096 | 303 | if(write ) set_config(locerr, Nonvolatile ) ; |
wbeaumont | 2:91836ad02096 | 304 | error |= locerr; |
wbeaumont | 2:91836ad02096 | 305 | } |
wbeaumont | 2:91836ad02096 | 306 | error=36; // invalid value |
wbeaumont | 2:91836ad02096 | 307 | } |
wbeaumont | 2:91836ad02096 | 308 | |
wbeaumont | 2:91836ad02096 | 309 | |
wbeaumont | 2:91836ad02096 | 310 | void AT30TSE75x::set_AlertPinPolarity(int pol, int &error, int Nonvolatile,bool write, bool update ){ |
wbeaumont | 2:91836ad02096 | 311 | int locerr; |
wbeaumont | 2:91836ad02096 | 312 | bool mode=(bool) pol; |
wbeaumont | 2:91836ad02096 | 313 | if ( update ) read_config(locerr, Nonvolatile ) ; |
wbeaumont | 2:91836ad02096 | 314 | error=locerr<<2; |
wbeaumont | 2:91836ad02096 | 315 | if( Nonvolatile) CregNV.alertpol=mode; else Creg.alertpol=mode; |
wbeaumont | 2:91836ad02096 | 316 | if(write ) set_config(locerr, Nonvolatile ) ; |
wbeaumont | 2:91836ad02096 | 317 | error |= locerr; |
wbeaumont | 2:91836ad02096 | 318 | } |
wbeaumont | 2:91836ad02096 | 319 | |
wbeaumont | 2:91836ad02096 | 320 | void AT30TSE75x::set_AlarmThermostateMode(int modein, int &error, int Nonvolatile,bool write, bool update ){ |
wbeaumont | 2:91836ad02096 | 321 | int locerr; |
wbeaumont | 2:91836ad02096 | 322 | bool mode=(bool) modein; |
wbeaumont | 2:91836ad02096 | 323 | if ( update ) read_config(locerr, Nonvolatile ) ; |
wbeaumont | 2:91836ad02096 | 324 | error=locerr<<2; |
wbeaumont | 2:91836ad02096 | 325 | if( Nonvolatile) CregNV.alarmMode=mode; else Creg.alarmMode=mode; |
wbeaumont | 2:91836ad02096 | 326 | if(write ) set_config(locerr, Nonvolatile ) ; |
wbeaumont | 2:91836ad02096 | 327 | error |= locerr; |
wbeaumont | 2:91836ad02096 | 328 | } |
wbeaumont | 2:91836ad02096 | 329 | |
wbeaumont | 2:91836ad02096 | 330 | |
wbeaumont | 2:91836ad02096 | 331 | void AT30TSE75x::set_ShutdownMode(int modein, int &error, int Nonvolatile,bool write, bool update ){ |
wbeaumont | 2:91836ad02096 | 332 | int locerr; |
wbeaumont | 2:91836ad02096 | 333 | bool mode=(bool) modein; |
wbeaumont | 2:91836ad02096 | 334 | if ( update ) read_config(locerr, Nonvolatile ) ; |
wbeaumont | 2:91836ad02096 | 335 | error=locerr<<2; |
wbeaumont | 2:91836ad02096 | 336 | if( Nonvolatile) CregNV.shutdownMode=mode; else Creg.shutdownMode=mode; |
wbeaumont | 2:91836ad02096 | 337 | if(write ) set_config(locerr, Nonvolatile ) ; |
wbeaumont | 2:91836ad02096 | 338 | error |= locerr; |
wbeaumont | 2:91836ad02096 | 339 | } |
wbeaumont | 2:91836ad02096 | 340 | void AT30TSE75x::set_FaultTollerantQueue(char nrfaults, int &error, int Nonvolatile, bool write, bool update ){ |
wbeaumont | 2:91836ad02096 | 341 | int ftq=7; |
wbeaumont | 2:91836ad02096 | 342 | if ( nrfaults == '1') ftq=0; |
wbeaumont | 2:91836ad02096 | 343 | if ( nrfaults == '2') ftq=1; |
wbeaumont | 2:91836ad02096 | 344 | if ( nrfaults == '4') ftq=2; |
wbeaumont | 2:91836ad02096 | 345 | if ( nrfaults == '6') ftq=3; |
wbeaumont | 2:91836ad02096 | 346 | set_FaultTollerantQueue( ftq,error, Nonvolatile, write, update); |
wbeaumont | 2:91836ad02096 | 347 | } |
wbeaumont | 2:91836ad02096 | 348 | |
wbeaumont | 2:91836ad02096 | 349 | void AT30TSE75x::activate_oneshot(int &error ){ |
wbeaumont | 2:91836ad02096 | 350 | int locerr; |
wbeaumont | 2:91836ad02096 | 351 | buffer[0]= ConfigReg; |
wbeaumont | 2:91836ad02096 | 352 | locerr=_i2c->write(Taddr,(char *)buffer,1,false); |
wbeaumont | 2:91836ad02096 | 353 | locerr= locerr<<2; |
wbeaumont | 2:91836ad02096 | 354 | locerr|=_i2c->read(Taddr,(char *)buffer,AT30TSE_TEMPERATURE_REG_SIZE,false); |
wbeaumont | 2:91836ad02096 | 355 | locerr= locerr<<2; |
wbeaumont | 2:91836ad02096 | 356 | buffer[0]= ConfigReg; |
wbeaumont | 2:91836ad02096 | 357 | buffer[1] = 0x80 ; //set once shot MSByte |
wbeaumont | 2:91836ad02096 | 358 | buffer[2] = 0 ; // volataille |
wbeaumont | 0:7cb648bc5c2a | 359 | locerr=_i2c->write(Taddr,(char *)buffer,3,false); |
wbeaumont | 0:7cb648bc5c2a | 360 | buffer[0]= TReg; |
wbeaumont | 2:91836ad02096 | 361 | locerr= locerr<<2; |
wbeaumont | 0:7cb648bc5c2a | 362 | locerr|=_i2c->write(Taddr,(char *)buffer,1,false); // make sure temp read reg is active again |
wbeaumont | 2:91836ad02096 | 363 | error=locerr; |
wbeaumont | 2:91836ad02096 | 364 | } |
wbeaumont | 2:91836ad02096 | 365 | |
wbeaumont | 2:91836ad02096 | 366 | void AT30TSE75x::set_RegisterLock (int &error, int lock ){ |
wbeaumont | 2:91836ad02096 | 367 | int locerr; |
wbeaumont | 2:91836ad02096 | 368 | (void)read_config(locerr,1 ); |
wbeaumont | 2:91836ad02096 | 369 | error= locerr<<2; |
wbeaumont | 2:91836ad02096 | 370 | CregNV.RegisterLock =lock; |
wbeaumont | 2:91836ad02096 | 371 | set_config(locerr,1 ); |
wbeaumont | 2:91836ad02096 | 372 | error |= locerr; |
wbeaumont | 2:91836ad02096 | 373 | } |
wbeaumont | 2:91836ad02096 | 374 | |
wbeaumont | 2:91836ad02096 | 375 | void AT30TSE75x::set_RegisterLockdown (int &error, int lockpermanent ){ |
wbeaumont | 2:91836ad02096 | 376 | int locerr; |
wbeaumont | 2:91836ad02096 | 377 | if( RegisterLocDownEnable) { |
wbeaumont | 2:91836ad02096 | 378 | if ( error != 123) {error=36; return;} |
wbeaumont | 2:91836ad02096 | 379 | (void)read_config(locerr,1 ); |
wbeaumont | 2:91836ad02096 | 380 | error= locerr<<2; |
wbeaumont | 2:91836ad02096 | 381 | CregNV. RegisterLockDown = lockpermanent; |
wbeaumont | 2:91836ad02096 | 382 | set_config(locerr,1 ); |
wbeaumont | 2:91836ad02096 | 383 | error |= locerr; |
wbeaumont | 2:91836ad02096 | 384 | } |
wbeaumont | 2:91836ad02096 | 385 | else { error =36 ;} |
wbeaumont | 2:91836ad02096 | 386 | } |
wbeaumont | 2:91836ad02096 | 387 | |
wbeaumont | 2:91836ad02096 | 388 | |
wbeaumont | 2:91836ad02096 | 389 | |
wbeaumont | 2:91836ad02096 | 390 | void AT30TSE75x::set_config( int &error, int Nonvolatile ) { |
wbeaumont | 2:91836ad02096 | 391 | int locerr; |
wbeaumont | 2:91836ad02096 | 392 | struct configreg *lcreg=&Creg; |
wbeaumont | 2:91836ad02096 | 393 | if ( Creg.RegisterLockDown && Nonvolatile ) { locerr = 43; return; } |
wbeaumont | 2:91836ad02096 | 394 | if ( Creg.RegisterLock ) { locerr = 44; return; } |
wbeaumont | 2:91836ad02096 | 395 | if ( Nonvolatile) buffer[0]= ConfigRegNV; else buffer[0]= ConfigReg; |
wbeaumont | 2:91836ad02096 | 396 | if( Nonvolatile) lcreg =&CregNV; else lcreg =&Creg; |
wbeaumont | 2:91836ad02096 | 397 | uint16_t ld=0, td; |
wbeaumont | 2:91836ad02096 | 398 | if ( lcreg->oneshot) ld= ONSHOT_BM; // no effect for NV reg |
wbeaumont | 2:91836ad02096 | 399 | td= (lcreg->resolution) <<13; ld= ld | td; |
wbeaumont | 2:91836ad02096 | 400 | td= (lcreg->faultTolQueue) <<11; ld= ld | td; |
wbeaumont | 2:91836ad02096 | 401 | if ( lcreg->alertpol) ld=ld |ALERTPOL_BM; |
wbeaumont | 2:91836ad02096 | 402 | if ( lcreg-> alarmMode) ld=ld |AlarmMode_BM; |
wbeaumont | 2:91836ad02096 | 403 | if ( lcreg-> shutdownMode) ld=ld |SHUTDOWN_BM; |
wbeaumont | 2:91836ad02096 | 404 | if ( lcreg-> RegisterLockDown) ld=ld |(REGLOCKDOWN_BM & RegisterLocDownEnable); |
wbeaumont | 2:91836ad02096 | 405 | if ( lcreg-> RegisterLock) ld=ld |(REGLOCK_BM ); |
wbeaumont | 2:91836ad02096 | 406 | if( Nonvolatile ) ld=ld; else ld= 0xFF00 & ld; |
wbeaumont | 2:91836ad02096 | 407 | buffer[2]=ld & 0xFF; ld = ld>>8; |
wbeaumont | 2:91836ad02096 | 408 | buffer[1]=ld & 0xFF; |
wbeaumont | 2:91836ad02096 | 409 | locerr=_i2c->write(Taddr,(char *)buffer,3,false); |
wbeaumont | 2:91836ad02096 | 410 | buffer[0]= TReg; |
wbeaumont | 2:91836ad02096 | 411 | locerr= locerr<<2; |
wbeaumont | 2:91836ad02096 | 412 | locerr|=_i2c->write(Taddr,(char *)buffer,1,false); // make sure temp read reg is active again |
wbeaumont | 2:91836ad02096 | 413 | error= locerr; |
wbeaumont | 0:7cb648bc5c2a | 414 | } |
wbeaumont | 0:7cb648bc5c2a | 415 | |
wbeaumont | 0:7cb648bc5c2a | 416 | |
wbeaumont | 0:7cb648bc5c2a | 417 | |
wbeaumont | 0:7cb648bc5c2a | 418 | |
wbeaumont | 0:7cb648bc5c2a | 419 | int AT30TSE75x::read_eeprompage(char *data, uint8_t length, uint8_t word_addr, uint8_t page) { |
wbeaumont | 0:7cb648bc5c2a | 420 | char buff[20]; |
wbeaumont | 0:7cb648bc5c2a | 421 | buff[0] = (word_addr & 0x0F) | ((0x0F & page) << 4); |
wbeaumont | 0:7cb648bc5c2a | 422 | |
wbeaumont | 0:7cb648bc5c2a | 423 | _i2c->write(Eaddr,buff,1,false); |
wbeaumont | 0:7cb648bc5c2a | 424 | return _i2c->read(Eaddr,data,length,false); |
wbeaumont | 0:7cb648bc5c2a | 425 | } |
wbeaumont | 0:7cb648bc5c2a | 426 | |
wbeaumont | 0:7cb648bc5c2a | 427 | int AT30TSE75x::write_eeprompage(char *data, uint8_t length, uint8_t word_addr, uint8_t page) |
wbeaumont | 0:7cb648bc5c2a | 428 | { |
wbeaumont | 0:7cb648bc5c2a | 429 | char buff[length+1]; |
wbeaumont | 0:7cb648bc5c2a | 430 | int buffsizep1=length+1;//buffersize+1 |
wbeaumont | 0:7cb648bc5c2a | 431 | buff[0] = (word_addr & 0x0F) | ((0x0F & page) << 4); |
wbeaumont | 0:7cb648bc5c2a | 432 | for(int i = 1; i < buffsizep1; i++) { |
wbeaumont | 0:7cb648bc5c2a | 433 | buff[i] = *(data++); |
wbeaumont | 0:7cb648bc5c2a | 434 | } |
wbeaumont | 0:7cb648bc5c2a | 435 | return _i2c->write(Eaddr,buff,(length + 1),false); |
wbeaumont | 0:7cb648bc5c2a | 436 | } |
wbeaumont | 0:7cb648bc5c2a | 437 | |
wbeaumont | 0:7cb648bc5c2a | 438 | |
wbeaumont | 0:7cb648bc5c2a | 439 | int AT30TSE75x::read_eeprombyte(char &data, uint8_t word_addr, uint8_t page){ |
wbeaumont | 0:7cb648bc5c2a | 440 | char rbuf[2]; |
wbeaumont | 0:7cb648bc5c2a | 441 | int i2cresult=read_eeprompage(rbuf,1,word_addr,page); |
wbeaumont | 0:7cb648bc5c2a | 442 | data=rbuf[0]; |
wbeaumont | 0:7cb648bc5c2a | 443 | return i2cresult; |
wbeaumont | 0:7cb648bc5c2a | 444 | } |
wbeaumont | 0:7cb648bc5c2a | 445 | |
wbeaumont | 0:7cb648bc5c2a | 446 | |
wbeaumont | 0:7cb648bc5c2a | 447 | int AT30TSE75x::write_eeprombyte(char data, uint8_t word_addr, uint8_t page){ |
wbeaumont | 0:7cb648bc5c2a | 448 | char wbuf[2]; wbuf[0]=data; |
wbeaumont | 0:7cb648bc5c2a | 449 | return write_eeprompage(wbuf,1,word_addr,page); |
wbeaumont | 1:c0db18a0c56b | 450 | } |
wbeaumont | 1:c0db18a0c56b | 451 | |
wbeaumont | 1:c0db18a0c56b | 452 | int AT30TSE75x::protect_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(AT30TSE75X_FIX_EEPROM ,wbuf,2,false); |
wbeaumont | 1:c0db18a0c56b | 455 | } |
wbeaumont | 1:c0db18a0c56b | 456 | |
wbeaumont | 1:c0db18a0c56b | 457 | int AT30TSE75x::unprotect_eeprom(void) { |
wbeaumont | 1:c0db18a0c56b | 458 | char wbuf[2]; wbuf[0]=0;wbuf[1]=0; // don't care |
wbeaumont | 1:c0db18a0c56b | 459 | return _i2c->write((0x4 | AT30TSE75X_FIX_EEPROM) ,wbuf,2,false); |
wbeaumont | 1:c0db18a0c56b | 460 | } |
wbeaumont | 1:c0db18a0c56b | 461 | |
wbeaumont | 1:c0db18a0c56b | 462 | int AT30TSE75x::get_eeprom_protec(void){ |
wbeaumont | 1:c0db18a0c56b | 463 | char wbuf[2]; |
wbeaumont | 1:c0db18a0c56b | 464 | return _i2c->read(AT30TSE75X_FIX_EEPROM ,wbuf,1,false); |
wbeaumont | 1:c0db18a0c56b | 465 | } |
wbeaumont | 1:c0db18a0c56b | 466 |