code to access the AT30TSE75x temperature and E-prom device

Dependents:   AT30TSE752TST AT30TSE752TST2

Committer:
wbeaumont
Date:
Tue Jan 17 13:43:08 2017 +0000
Revision:
1:c0db18a0c56b
Parent:
0:7cb648bc5c2a
Child:
2:91836ad02096
ee-prom write protect

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wbeaumont 0:7cb648bc5c2a 1 #include "mbed.h"
wbeaumont 0:7cb648bc5c2a 2 #include "AT30TSE75x.h"
wbeaumont 0:7cb648bc5c2a 3
wbeaumont 0:7cb648bc5c2a 4 /*
wbeaumont 0:7cb648bc5c2a 5 * info see AT30SE75x.h
wbeaumont 0:7cb648bc5c2a 6 * (C) Wim Beaumont Universiteit Antwerpen 2017
wbeaumont 0:7cb648bc5c2a 7
wbeaumont 0:7cb648bc5c2a 8 * some parts of the code are copied
wbeaumont 0:7cb648bc5c2a 9 * from https://developer.mbed.org/users/akhilpanayam/code/AT30TSE75X/file/0e430cef393b/AT30TSE75X.h
wbeaumont 0:7cb648bc5c2a 10 */
wbeaumont 0:7cb648bc5c2a 11
wbeaumont 0:7cb648bc5c2a 12
wbeaumont 1:c0db18a0c56b 13 #define VERSION_AT30TSE75x_SRC "0.86"
wbeaumont 0:7cb648bc5c2a 14
wbeaumont 0:7cb648bc5c2a 15 #define AT30TSE75X_ADD_TEMP 0x48 /*Temperature Sensor: 0b1001xxx */
wbeaumont 0:7cb648bc5c2a 16 #define AT30TSE75X_ADD_EEPROM 0x50 /*EEPROM: 0b1010xxx */
wbeaumont 1:c0db18a0c56b 17 #define AT30TSE75X_FIX_EEPROM 0x62 /*fix EEPROM 0b01100010 ( last 0 = W) */
wbeaumont 0:7cb648bc5c2a 18 #define AT30TSE752 1
wbeaumont 0:7cb648bc5c2a 19 #define AT30TSE754 2
wbeaumont 0:7cb648bc5c2a 20 #define AT30TSE758 3
wbeaumont 0:7cb648bc5c2a 21
wbeaumont 0:7cb648bc5c2a 22 #define AT30TSE_CONFIG_RES_9_bit 0
wbeaumont 0:7cb648bc5c2a 23 #define AT30TSE_CONFIG_RES_10_bit 1
wbeaumont 0:7cb648bc5c2a 24 #define AT30TSE_CONFIG_RES_11_bit 2
wbeaumont 0:7cb648bc5c2a 25 #define AT30TSE_CONFIG_RES_12_bit 3
wbeaumont 0:7cb648bc5c2a 26
wbeaumont 0:7cb648bc5c2a 27
wbeaumont 0:7cb648bc5c2a 28 #define AT30TSE_TEMPERATURE_REG_SIZE 2
wbeaumont 0:7cb648bc5c2a 29
wbeaumont 0:7cb648bc5c2a 30
wbeaumont 0:7cb648bc5c2a 31 #define TReg 0x0 // 16 bits
wbeaumont 0:7cb648bc5c2a 32 #define ConfigReg 0x1 // 16 bits
wbeaumont 0:7cb648bc5c2a 33 #define TLowReg 0x2 // 16 bits
wbeaumont 0:7cb648bc5c2a 34 #define THighReg 0x3 // 16 bits
wbeaumont 0:7cb648bc5c2a 35 #define ConfigRegNV 0x11 // 16 bits
wbeaumont 0:7cb648bc5c2a 36 #define TLowRegNV 0x12 // 16 bits
wbeaumont 0:7cb648bc5c2a 37 #define THighRegNV 0x13 // 16 bits
wbeaumont 0:7cb648bc5c2a 38
wbeaumont 0:7cb648bc5c2a 39 AT30TSE75x::AT30TSE75x (I2CInterface* i2cinterface, int device_address_bits,int eepromsize):
wbeaumont 0:7cb648bc5c2a 40 getVersion( VERSION_AT30TSE75x_HDR,VERSION_AT30TSE75x_SRC, __TIME__, __DATE__),_i2c(i2cinterface) {
wbeaumont 0:7cb648bc5c2a 41 Taddr= ((device_address_bits & 0x7) | AT30TSE75X_ADD_TEMP ) & 0x7F; Taddr=Taddr<<1;
wbeaumont 0:7cb648bc5c2a 42 Eaddr= ((device_address_bits & 0x7) | AT30TSE75X_ADD_EEPROM ) & 0x7F; Eaddr=Eaddr<<1;
wbeaumont 0:7cb648bc5c2a 43
wbeaumont 0:7cb648bc5c2a 44 Esize=1;
wbeaumont 0:7cb648bc5c2a 45 if( eepromsize ==4 ) Esize=2; if( eepromsize ==8) Esize=3;
wbeaumont 0:7cb648bc5c2a 46 resolution=AT30TSE_CONFIG_RES_12_bit;
wbeaumont 0:7cb648bc5c2a 47 }
wbeaumont 0:7cb648bc5c2a 48
wbeaumont 0:7cb648bc5c2a 49
wbeaumont 0:7cb648bc5c2a 50
wbeaumont 0:7cb648bc5c2a 51
wbeaumont 0:7cb648bc5c2a 52 float AT30TSE75x::get_temperature(int *error) {
wbeaumont 0:7cb648bc5c2a 53
wbeaumont 0:7cb648bc5c2a 54
wbeaumont 0:7cb648bc5c2a 55 float temperature;
wbeaumont 0:7cb648bc5c2a 56 uint16_t data;
wbeaumont 0:7cb648bc5c2a 57 int locerr=-200;
wbeaumont 0:7cb648bc5c2a 58 buffer[0] = 0;
wbeaumont 0:7cb648bc5c2a 59 buffer[1] = 0;
wbeaumont 0:7cb648bc5c2a 60
wbeaumont 0:7cb648bc5c2a 61 locerr=_i2c->read(Taddr,(char *)buffer,AT30TSE_TEMPERATURE_REG_SIZE,false);
wbeaumont 0:7cb648bc5c2a 62 if ( error ) *error=locerr; // pointer !=0
wbeaumont 0:7cb648bc5c2a 63
wbeaumont 0:7cb648bc5c2a 64 data = (buffer[0] << 8) | buffer[1];
wbeaumont 0:7cb648bc5c2a 65 int8_t sign = 1;
wbeaumont 0:7cb648bc5c2a 66
wbeaumont 0:7cb648bc5c2a 67 /* Check if negative and clear sign bit. */
wbeaumont 0:7cb648bc5c2a 68 if (data & (1 << 15)) {
wbeaumont 0:7cb648bc5c2a 69 sign *= -1;
wbeaumont 0:7cb648bc5c2a 70 data &= ~(1 << 15);
wbeaumont 0:7cb648bc5c2a 71 }
wbeaumont 0:7cb648bc5c2a 72
wbeaumont 0:7cb648bc5c2a 73 /* Convert to temperature. */
wbeaumont 0:7cb648bc5c2a 74 switch (resolution) {
wbeaumont 0:7cb648bc5c2a 75 case AT30TSE_CONFIG_RES_9_bit:
wbeaumont 0:7cb648bc5c2a 76 data = (data >> 7);
wbeaumont 0:7cb648bc5c2a 77 (temperature) = data * sign * 0.5;
wbeaumont 0:7cb648bc5c2a 78 break;
wbeaumont 0:7cb648bc5c2a 79
wbeaumont 0:7cb648bc5c2a 80 case AT30TSE_CONFIG_RES_10_bit:
wbeaumont 0:7cb648bc5c2a 81 data = (data >> 6);
wbeaumont 0:7cb648bc5c2a 82 (temperature) = data * sign * 0.25;
wbeaumont 0:7cb648bc5c2a 83 break;
wbeaumont 0:7cb648bc5c2a 84
wbeaumont 0:7cb648bc5c2a 85 case AT30TSE_CONFIG_RES_11_bit:
wbeaumont 0:7cb648bc5c2a 86 data = (data >> 5);
wbeaumont 0:7cb648bc5c2a 87 (temperature) = data * sign * 0.125;
wbeaumont 0:7cb648bc5c2a 88 break;
wbeaumont 0:7cb648bc5c2a 89
wbeaumont 0:7cb648bc5c2a 90 case AT30TSE_CONFIG_RES_12_bit:
wbeaumont 0:7cb648bc5c2a 91 data = (data >> 4);
wbeaumont 0:7cb648bc5c2a 92 (temperature) = data * sign * 0.0625;
wbeaumont 0:7cb648bc5c2a 93 break;
wbeaumont 0:7cb648bc5c2a 94
wbeaumont 0:7cb648bc5c2a 95 default:
wbeaumont 0:7cb648bc5c2a 96 break;
wbeaumont 0:7cb648bc5c2a 97 }
wbeaumont 0:7cb648bc5c2a 98 return temperature;
wbeaumont 0:7cb648bc5c2a 99 }
wbeaumont 0:7cb648bc5c2a 100
wbeaumont 0:7cb648bc5c2a 101
wbeaumont 0:7cb648bc5c2a 102 int AT30TSE75x::read_config( int Nonvolatille,int *error ) {
wbeaumont 0:7cb648bc5c2a 103 uint16_t data;
wbeaumont 0:7cb648bc5c2a 104 int locerr;
wbeaumont 0:7cb648bc5c2a 105 buffer[0]= ConfigReg; // set pointer to config reg nv
wbeaumont 0:7cb648bc5c2a 106 locerr=_i2c->write(Taddr,(char *)buffer,1,false);
wbeaumont 0:7cb648bc5c2a 107 locerr= locerr<<2;
wbeaumont 0:7cb648bc5c2a 108 locerr|=_i2c->read(Taddr,(char *)buffer,AT30TSE_TEMPERATURE_REG_SIZE,false);
wbeaumont 0:7cb648bc5c2a 109 locerr= locerr<<2;
wbeaumont 0:7cb648bc5c2a 110 data = (buffer[0] << 8) | buffer[1];
wbeaumont 0:7cb648bc5c2a 111 buffer[0]= TReg;
wbeaumont 0:7cb648bc5c2a 112 locerr|=_i2c->write(Taddr,(char *)buffer,1,false);
wbeaumont 0:7cb648bc5c2a 113 if ( error ) *error=locerr; // pointer !=0
wbeaumont 0:7cb648bc5c2a 114 return (int)data;
wbeaumont 0:7cb648bc5c2a 115 }
wbeaumont 0:7cb648bc5c2a 116
wbeaumont 0:7cb648bc5c2a 117
wbeaumont 0:7cb648bc5c2a 118 int AT30TSE75x::set_config( int Nonvolatille ) {
wbeaumont 0:7cb648bc5c2a 119 int locerr;
wbeaumont 0:7cb648bc5c2a 120 buffer[0]= ConfigReg; // set pointer to config reg nv
wbeaumont 0:7cb648bc5c2a 121
wbeaumont 0:7cb648bc5c2a 122
wbeaumont 0:7cb648bc5c2a 123
wbeaumont 0:7cb648bc5c2a 124 /* for no all fixed values
wbeaumont 0:7cb648bc5c2a 125 15 normal : 1 0b1xxx xxxx xxxx xxxx
wbeaumont 0:7cb648bc5c2a 126 14:13 12 bit :11 0b111x xxxx xxxx xxxx
wbeaumont 0:7cb648bc5c2a 127 12:11 1 fault:00 0b1110 0xxx xxxx xxxx
wbeaumont 0:7cb648bc5c2a 128 10 0 act low 0b1110 00xx xxxx xxxx
wbeaumont 0:7cb648bc5c2a 129 9 0 cmp mode 0b1110 000x xxxx xxxx
wbeaumont 0:7cb648bc5c2a 130 8 0 temp act 0b1110 0000 xxxx xxxx
wbeaumont 0:7cb648bc5c2a 131 7:1 dont care 0b1110 0000 0000 000x
wbeaumont 0:7cb648bc5c2a 132 0 read only 0b1110 0000 0000 0000
wbeaumont 0:7cb648bc5c2a 133 => 0xE0
wbeaumont 0:7cb648bc5c2a 134 */
wbeaumont 0:7cb648bc5c2a 135 buffer[1]=0xE0; buffer[2]=0x00;
wbeaumont 0:7cb648bc5c2a 136 locerr=_i2c->write(Taddr,(char *)buffer,3,false);
wbeaumont 0:7cb648bc5c2a 137 buffer[0]= TReg;
wbeaumont 0:7cb648bc5c2a 138 locerr= locerr<<2;
wbeaumont 0:7cb648bc5c2a 139 locerr|=_i2c->write(Taddr,(char *)buffer,1,false); // make sure temp read reg is active again
wbeaumont 0:7cb648bc5c2a 140 return locerr;
wbeaumont 0:7cb648bc5c2a 141 }
wbeaumont 0:7cb648bc5c2a 142
wbeaumont 0:7cb648bc5c2a 143
wbeaumont 0:7cb648bc5c2a 144
wbeaumont 0:7cb648bc5c2a 145
wbeaumont 0:7cb648bc5c2a 146 int AT30TSE75x::read_eeprompage(char *data, uint8_t length, uint8_t word_addr, uint8_t page) {
wbeaumont 0:7cb648bc5c2a 147 char buff[20];
wbeaumont 0:7cb648bc5c2a 148 buff[0] = (word_addr & 0x0F) | ((0x0F & page) << 4);
wbeaumont 0:7cb648bc5c2a 149
wbeaumont 0:7cb648bc5c2a 150 _i2c->write(Eaddr,buff,1,false);
wbeaumont 0:7cb648bc5c2a 151 return _i2c->read(Eaddr,data,length,false);
wbeaumont 0:7cb648bc5c2a 152 }
wbeaumont 0:7cb648bc5c2a 153
wbeaumont 0:7cb648bc5c2a 154 int AT30TSE75x::write_eeprompage(char *data, uint8_t length, uint8_t word_addr, uint8_t page)
wbeaumont 0:7cb648bc5c2a 155 {
wbeaumont 0:7cb648bc5c2a 156 char buff[length+1];
wbeaumont 0:7cb648bc5c2a 157 int buffsizep1=length+1;//buffersize+1
wbeaumont 0:7cb648bc5c2a 158 buff[0] = (word_addr & 0x0F) | ((0x0F & page) << 4);
wbeaumont 0:7cb648bc5c2a 159 for(int i = 1; i < buffsizep1; i++) {
wbeaumont 0:7cb648bc5c2a 160 buff[i] = *(data++);
wbeaumont 0:7cb648bc5c2a 161 }
wbeaumont 0:7cb648bc5c2a 162 return _i2c->write(Eaddr,buff,(length + 1),false);
wbeaumont 0:7cb648bc5c2a 163 }
wbeaumont 0:7cb648bc5c2a 164
wbeaumont 0:7cb648bc5c2a 165
wbeaumont 0:7cb648bc5c2a 166 int AT30TSE75x::read_eeprombyte(char &data, uint8_t word_addr, uint8_t page){
wbeaumont 0:7cb648bc5c2a 167 char rbuf[2];
wbeaumont 0:7cb648bc5c2a 168 int i2cresult=read_eeprompage(rbuf,1,word_addr,page);
wbeaumont 0:7cb648bc5c2a 169 data=rbuf[0];
wbeaumont 0:7cb648bc5c2a 170 return i2cresult;
wbeaumont 0:7cb648bc5c2a 171 }
wbeaumont 0:7cb648bc5c2a 172
wbeaumont 0:7cb648bc5c2a 173
wbeaumont 0:7cb648bc5c2a 174 int AT30TSE75x::write_eeprombyte(char data, uint8_t word_addr, uint8_t page){
wbeaumont 0:7cb648bc5c2a 175 char wbuf[2]; wbuf[0]=data;
wbeaumont 0:7cb648bc5c2a 176 return write_eeprompage(wbuf,1,word_addr,page);
wbeaumont 1:c0db18a0c56b 177 }
wbeaumont 1:c0db18a0c56b 178
wbeaumont 1:c0db18a0c56b 179 int AT30TSE75x::protect_eeprom(void) {
wbeaumont 1:c0db18a0c56b 180 char wbuf[2]; wbuf[0]=0;wbuf[1]=0; // don't care
wbeaumont 1:c0db18a0c56b 181 return _i2c->write(AT30TSE75X_FIX_EEPROM ,wbuf,2,false);
wbeaumont 1:c0db18a0c56b 182 }
wbeaumont 1:c0db18a0c56b 183
wbeaumont 1:c0db18a0c56b 184 int AT30TSE75x::unprotect_eeprom(void) {
wbeaumont 1:c0db18a0c56b 185 char wbuf[2]; wbuf[0]=0;wbuf[1]=0; // don't care
wbeaumont 1:c0db18a0c56b 186 return _i2c->write((0x4 | AT30TSE75X_FIX_EEPROM) ,wbuf,2,false);
wbeaumont 1:c0db18a0c56b 187 }
wbeaumont 1:c0db18a0c56b 188
wbeaumont 1:c0db18a0c56b 189 int AT30TSE75x::get_eeprom_protec(void){
wbeaumont 1:c0db18a0c56b 190 char wbuf[2];
wbeaumont 1:c0db18a0c56b 191 return _i2c->read(AT30TSE75X_FIX_EEPROM ,wbuf,1,false);
wbeaumont 1:c0db18a0c56b 192 }
wbeaumont 1:c0db18a0c56b 193