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