test program for the AT30TSE75X class
Dependencies: AT30TSE75x DevInterfaces I2Cinterfaces mbed
Fork of MCP4728test by
This is a program to use the AT30TSE752 with MBED . The program is used to test this device as temperature probes for the the SOLID project. It also is able to write to the ee-prom and is able to write protect the ee-prom and unprotect.
main.cpp@4:4d20444d5116, 2017-01-17 (annotated)
- Committer:
- wbeaumont
- Date:
- Tue Jan 17 18:10:31 2017 +0000
- Revision:
- 4:4d20444d5116
- Parent:
- 3:789bc6653f83
tested also unprotect function
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wbeaumont | 2:8ce5a2128381 | 1 | /** example program for the use of the AT30TSE7xx class |
wbeaumont | 1:d175631a5803 | 2 | * |
wbeaumont | 2:8ce5a2128381 | 3 | * V 0.4 : tested on the KL25z |
wbeaumont | 2:8ce5a2128381 | 4 | * tested eeprom read/ write full page / single byte |
wbeaumont | 2:8ce5a2128381 | 5 | * temperature read 12 bit resolution |
wbeaumont | 2:8ce5a2128381 | 6 | * fix chip configuration. |
wbeaumont | 2:8ce5a2128381 | 7 | * (C) Wim Beaumont Universiteit Antwerpen 2017 |
wbeaumont | 1:d175631a5803 | 8 | */ |
wbeaumont | 1:d175631a5803 | 9 | |
wbeaumont | 4:4d20444d5116 | 10 | #define AT30TSE753EXAMPLEVER "0.63" |
wbeaumont | 1:d175631a5803 | 11 | |
wbeaumont | 0:5bc0f4bd5aa0 | 12 | #include "mbed.h" |
wbeaumont | 0:5bc0f4bd5aa0 | 13 | |
wbeaumont | 0:5bc0f4bd5aa0 | 14 | #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z) |
wbeaumont | 2:8ce5a2128381 | 15 | PinName const SDA = PTE0; |
wbeaumont | 2:8ce5a2128381 | 16 | PinName const SCL = PTE1; |
wbeaumont | 0:5bc0f4bd5aa0 | 17 | #elif defined (TARGET_KL05Z) |
wbeaumont | 0:5bc0f4bd5aa0 | 18 | PinName const SDA = PTB4; |
wbeaumont | 0:5bc0f4bd5aa0 | 19 | PinName const SCL = PTB3; |
wbeaumont | 0:5bc0f4bd5aa0 | 20 | #elif defined (TARGET_K20D50M) |
wbeaumont | 0:5bc0f4bd5aa0 | 21 | PinName const SDA = PTB1; |
wbeaumont | 0:5bc0f4bd5aa0 | 22 | PinName const SCL = PTB0; |
wbeaumont | 0:5bc0f4bd5aa0 | 23 | #else |
wbeaumont | 0:5bc0f4bd5aa0 | 24 | #error TARGET NOT DEFINED |
wbeaumont | 0:5bc0f4bd5aa0 | 25 | #endif |
wbeaumont | 0:5bc0f4bd5aa0 | 26 | |
wbeaumont | 0:5bc0f4bd5aa0 | 27 | |
wbeaumont | 0:5bc0f4bd5aa0 | 28 | #include "I2C.h" |
wbeaumont | 0:5bc0f4bd5aa0 | 29 | #include "I2CInterface.h" |
wbeaumont | 2:8ce5a2128381 | 30 | #include "MBEDI2CInterface.h" |
wbeaumont | 0:5bc0f4bd5aa0 | 31 | #include "dev_interface_def.h" |
wbeaumont | 2:8ce5a2128381 | 32 | #include "AT30TSE75x.h" |
wbeaumont | 2:8ce5a2128381 | 33 | |
wbeaumont | 0:5bc0f4bd5aa0 | 34 | |
wbeaumont | 0:5bc0f4bd5aa0 | 35 | MBEDI2CInterface mbedi2c( SDA, SCL); |
wbeaumont | 0:5bc0f4bd5aa0 | 36 | MBEDI2CInterface* mbedi2cp= &mbedi2c ; |
wbeaumont | 0:5bc0f4bd5aa0 | 37 | I2CInterface* i2cdev= mbedi2cp; |
wbeaumont | 0:5bc0f4bd5aa0 | 38 | |
wbeaumont | 4:4d20444d5116 | 39 | DigitalInOut CntrA1(PTD1, PIN_INPUT , PullNone,0); |
wbeaumont | 4:4d20444d5116 | 40 | DigitalInOut CntrA2(PTD3, PIN_INPUT , PullNone,0); |
wbeaumont | 0:5bc0f4bd5aa0 | 41 | Serial pc(USBTX, USBRX); |
wbeaumont | 0:5bc0f4bd5aa0 | 42 | |
wbeaumont | 2:8ce5a2128381 | 43 | void print_buf_hex( char *data, int length){ |
wbeaumont | 2:8ce5a2128381 | 44 | int nr; |
wbeaumont | 2:8ce5a2128381 | 45 | char *ptr=data; |
wbeaumont | 2:8ce5a2128381 | 46 | for ( int lc=0; lc < length ; lc++){ |
wbeaumont | 2:8ce5a2128381 | 47 | nr= (int) *(ptr++); |
wbeaumont | 2:8ce5a2128381 | 48 | printf( "%02x ",nr); |
wbeaumont | 2:8ce5a2128381 | 49 | } |
wbeaumont | 2:8ce5a2128381 | 50 | printf("\n\r"); |
wbeaumont | 2:8ce5a2128381 | 51 | } |
wbeaumont | 2:8ce5a2128381 | 52 | |
wbeaumont | 2:8ce5a2128381 | 53 | |
wbeaumont | 3:789bc6653f83 | 54 | int main(void) { //432109876543210 \0 |
wbeaumont | 4:4d20444d5116 | 55 | char str1[16] = "SOLTEMPERAT0231"; |
wbeaumont | 3:789bc6653f83 | 56 | char str2[16] ; |
wbeaumont | 4:4d20444d5116 | 57 | char str4[16] ; |
wbeaumont | 4:4d20444d5116 | 58 | char str3[16]= "20170117...0230" ; |
wbeaumont | 3:789bc6653f83 | 59 | char resp[3]; |
wbeaumont | 0:5bc0f4bd5aa0 | 60 | // get the version of getVersion |
wbeaumont | 0:5bc0f4bd5aa0 | 61 | getVersion gv; |
wbeaumont | 2:8ce5a2128381 | 62 | int addr=0; |
wbeaumont | 3:789bc6653f83 | 63 | int i2cerr; |
wbeaumont | 3:789bc6653f83 | 64 | int cnt=0; |
wbeaumont | 3:789bc6653f83 | 65 | |
wbeaumont | 2:8ce5a2128381 | 66 | printf("AT30TSE752 example program version %s, compile date %s time %s\n\r",AT30TSE753EXAMPLEVER,__DATE__,__TIME__); |
wbeaumont | 2:8ce5a2128381 | 67 | printf("getVersion :%s\n\r ",gv.getversioninfo()); |
wbeaumont | 2:8ce5a2128381 | 68 | printf("%s\n\r",str1); |
wbeaumont | 3:789bc6653f83 | 69 | |
wbeaumont | 2:8ce5a2128381 | 70 | printf("give new addr "); scanf("%d", &addr); |
wbeaumont | 2:8ce5a2128381 | 71 | if ( addr < 0 || addr > 15) addr=0; |
wbeaumont | 2:8ce5a2128381 | 72 | fflush(stdin); |
wbeaumont | 0:5bc0f4bd5aa0 | 73 | |
wbeaumont | 2:8ce5a2128381 | 74 | AT30TSE75x tid( i2cdev ,addr); |
wbeaumont | 2:8ce5a2128381 | 75 | printf ( "AT30SE75x version :%s\n\r ",tid.getversioninfo()); |
wbeaumont | 2:8ce5a2128381 | 76 | printf( "Taddr %x , Eaddr %x subaddr %d\n\r ", tid.getTaddr(),tid.getEaddr(), addr); |
wbeaumont | 2:8ce5a2128381 | 77 | |
wbeaumont | 3:789bc6653f83 | 78 | |
wbeaumont | 3:789bc6653f83 | 79 | |
wbeaumont | 2:8ce5a2128381 | 80 | i2cerr=tid.set_config(); |
wbeaumont | 2:8ce5a2128381 | 81 | if( i2cerr) printf("config not set %d \n\r", i2cerr); |
wbeaumont | 2:8ce5a2128381 | 82 | int configrd= tid.read_config( 0, &i2cerr); |
wbeaumont | 2:8ce5a2128381 | 83 | printf( " config %x I2cerr %d \n\r", configrd,i2cerr ); |
wbeaumont | 3:789bc6653f83 | 84 | printf("write to eprom ?"); scanf("%s",resp); |
wbeaumont | 3:789bc6653f83 | 85 | int pagenr=0; |
wbeaumont | 3:789bc6653f83 | 86 | int sernr; |
wbeaumont | 3:789bc6653f83 | 87 | if(resp[0] == 'y'){ |
wbeaumont | 3:789bc6653f83 | 88 | i2cerr= tid.write_eeprompage(str1, 16, 0, (uint8_t) pagenr++); |
wbeaumont | 2:8ce5a2128381 | 89 | if(i2cerr) printf("eeprom write error %d\n\r",i2cerr); |
wbeaumont | 3:789bc6653f83 | 90 | printf("give serial nr to write "); scanf("%d",&sernr); |
wbeaumont | 3:789bc6653f83 | 91 | sprintf(str2,"%015d",sernr); |
wbeaumont | 3:789bc6653f83 | 92 | i2cerr= tid.write_eeprompage(str2, 16, 0, (uint8_t) pagenr++); |
wbeaumont | 3:789bc6653f83 | 93 | wait_ms(400); // give some time to write |
wbeaumont | 4:4d20444d5116 | 94 | i2cerr= tid.write_eeprompage(str3, 16, 0, (uint8_t) pagenr++); |
wbeaumont | 4:4d20444d5116 | 95 | float tempcor, tempref; |
wbeaumont | 4:4d20444d5116 | 96 | printf("\n\rgive temperature correction"); scanf("%f",&tempcor); |
wbeaumont | 4:4d20444d5116 | 97 | printf("\n\rgive ref temperature "); scanf("%f",&tempref); |
wbeaumont | 4:4d20444d5116 | 98 | sprintf(str4,"%8.4f %7.4f",tempcor,tempref); |
wbeaumont | 4:4d20444d5116 | 99 | i2cerr= tid.write_eeprompage(str4, 16, 0, (uint8_t) pagenr++); |
wbeaumont | 2:8ce5a2128381 | 100 | } |
wbeaumont | 2:8ce5a2128381 | 101 | wait_ms(400); // give some time to write |
wbeaumont | 3:789bc6653f83 | 102 | //strcpy(str1,"newstr1");strcp(str2,"newstr2");strcp(str3,"newstr3"); |
wbeaumont | 3:789bc6653f83 | 103 | bool fix=true; |
wbeaumont | 2:8ce5a2128381 | 104 | while(1) { |
wbeaumont | 3:789bc6653f83 | 105 | pagenr=0; |
wbeaumont | 3:789bc6653f83 | 106 | i2cerr=tid.read_eeprompage(str1, 16, 0, (uint8_t) pagenr++); |
wbeaumont | 3:789bc6653f83 | 107 | if(i2cerr) printf("eeprom read error %d \n\r",i2cerr); |
wbeaumont | 3:789bc6653f83 | 108 | |
wbeaumont | 3:789bc6653f83 | 109 | i2cerr=tid.read_eeprompage(str2, 16, 0, (uint8_t) pagenr++); |
wbeaumont | 3:789bc6653f83 | 110 | if(i2cerr) printf("eeprom read error %d \n\r",i2cerr); |
wbeaumont | 4:4d20444d5116 | 111 | i2cerr=tid.read_eeprompage(str3, 16, 0, (uint8_t) pagenr++); |
wbeaumont | 2:8ce5a2128381 | 112 | if(i2cerr) printf("eeprom read error %d \n\r",i2cerr); |
wbeaumont | 4:4d20444d5116 | 113 | i2cerr=tid.read_eeprompage(str4, 16, 0, (uint8_t) pagenr); |
wbeaumont | 4:4d20444d5116 | 114 | if(i2cerr) printf("eeprom read error %d \n\r",i2cerr); |
wbeaumont | 4:4d20444d5116 | 115 | str1[15]='\0';str2[15]='\0';str3[15]='\0';str4[15]='\0'; //make sure it ends |
wbeaumont | 4:4d20444d5116 | 116 | printf("eeprom content page %d :\n\r %s %s %s %s \n\r",pagenr,str1 ,str2 ,str3 ,str4); |
wbeaumont | 2:8ce5a2128381 | 117 | //print_buf_hex( str1,16); |
wbeaumont | 3:789bc6653f83 | 118 | if ( fix) { |
wbeaumont | 4:4d20444d5116 | 119 | printf("protect eeprom ?") ;scanf("%s",resp); |
wbeaumont | 3:789bc6653f83 | 120 | if(resp[0] == 'y'){ |
wbeaumont | 4:4d20444d5116 | 121 | printf("\n\rconnect A0 to 7 V y, t return when done \n\r");scanf("%s",resp); |
wbeaumont | 4:4d20444d5116 | 122 | CntrA1.output(); CntrA1.write(0); |
wbeaumont | 4:4d20444d5116 | 123 | CntrA2.output(); CntrA2.write(0); |
wbeaumont | 4:4d20444d5116 | 124 | i2cerr=tid.get_eeprom_protec(); |
wbeaumont | 4:4d20444d5116 | 125 | if ( i2cerr ) { printf("ee-prom write protected \n\r");} |
wbeaumont | 4:4d20444d5116 | 126 | else { printf("ee-prom NOT write protected \n\r"); } |
wbeaumont | 3:789bc6653f83 | 127 | i2cerr=tid.protect_eeprom(); |
wbeaumont | 3:789bc6653f83 | 128 | if(i2cerr) printf("eeprom protect failed %d \n\r", i2cerr); |
wbeaumont | 4:4d20444d5116 | 129 | i2cerr=tid.get_eeprom_protec(); |
wbeaumont | 4:4d20444d5116 | 130 | wait_ms(100); |
wbeaumont | 4:4d20444d5116 | 131 | if ( i2cerr ) { printf("ee-prom write protected \n\r");} |
wbeaumont | 4:4d20444d5116 | 132 | else { printf("ee-prom NOT write protected \n\r"); } |
wbeaumont | 4:4d20444d5116 | 133 | CntrA1.input(); CntrA1.mode(PullNone); |
wbeaumont | 4:4d20444d5116 | 134 | CntrA2.input(); CntrA2.mode(PullNone); |
wbeaumont | 4:4d20444d5116 | 135 | printf("disconnect A0 from 7 V y, t return when done \n\r");scanf("%s",resp); |
wbeaumont | 3:789bc6653f83 | 136 | } |
wbeaumont | 3:789bc6653f83 | 137 | // fix=false; |
wbeaumont | 4:4d20444d5116 | 138 | printf("unprotect eeprom ?") ;scanf("%s",resp); |
wbeaumont | 4:4d20444d5116 | 139 | if(resp[0] == 'y'){ |
wbeaumont | 4:4d20444d5116 | 140 | printf("\n\rconnect A0 to 7 V y, t return when done \n\r");scanf("%s",resp); |
wbeaumont | 4:4d20444d5116 | 141 | CntrA1.output(); CntrA1.write(0); |
wbeaumont | 4:4d20444d5116 | 142 | CntrA2.output(); CntrA2.write(0); |
wbeaumont | 4:4d20444d5116 | 143 | i2cerr=tid.get_eeprom_protec(); |
wbeaumont | 4:4d20444d5116 | 144 | if ( i2cerr ) { printf("ee-prom write protected \n\r");} |
wbeaumont | 4:4d20444d5116 | 145 | else { printf("ee-prom NOT write protected \n\r"); } |
wbeaumont | 4:4d20444d5116 | 146 | CntrA1.output(); CntrA1.write(1); |
wbeaumont | 4:4d20444d5116 | 147 | wait_ms(100); |
wbeaumont | 4:4d20444d5116 | 148 | i2cerr=tid.unprotect_eeprom(); |
wbeaumont | 4:4d20444d5116 | 149 | if(i2cerr) printf("eeprom protect failed %d \n\r", i2cerr); |
wbeaumont | 4:4d20444d5116 | 150 | CntrA1.output(); CntrA1.write(0); |
wbeaumont | 4:4d20444d5116 | 151 | wait_ms(100); |
wbeaumont | 4:4d20444d5116 | 152 | i2cerr=tid.get_eeprom_protec(); |
wbeaumont | 4:4d20444d5116 | 153 | if ( i2cerr ) { printf("ee-prom write protected \n\r");} |
wbeaumont | 4:4d20444d5116 | 154 | else { printf("ee-prom NOT write protected \n\r"); } |
wbeaumont | 4:4d20444d5116 | 155 | CntrA1.input(); CntrA1.mode(PullNone); |
wbeaumont | 4:4d20444d5116 | 156 | CntrA2.input(); CntrA2.mode(PullNone); |
wbeaumont | 4:4d20444d5116 | 157 | printf("disconnect A0 from 7 V y, t return when done \n\r");scanf("%s",resp); |
wbeaumont | 4:4d20444d5116 | 158 | } |
wbeaumont | 4:4d20444d5116 | 159 | |
wbeaumont | 3:789bc6653f83 | 160 | } |
wbeaumont | 2:8ce5a2128381 | 161 | char singlechar; |
wbeaumont | 2:8ce5a2128381 | 162 | i2cerr=tid.read_eeprombyte(singlechar, 3, (uint8_t) pagenr); |
wbeaumont | 2:8ce5a2128381 | 163 | if(i2cerr) printf("eeprom read error %d \n\r",i2cerr); |
wbeaumont | 2:8ce5a2128381 | 164 | printf("third char is : %c\n\r",singlechar); |
wbeaumont | 2:8ce5a2128381 | 165 | wait_ms(1000); |
wbeaumont | 2:8ce5a2128381 | 166 | float Tmp= tid.get_temperature(&i2cerr); |
wbeaumont | 2:8ce5a2128381 | 167 | printf ("%d:Temp = %f I2cerr %d \n\r",cnt++, Tmp, i2cerr); |
wbeaumont | 2:8ce5a2128381 | 168 | |
wbeaumont | 1:d175631a5803 | 169 | |
wbeaumont | 1:d175631a5803 | 170 | } |
wbeaumont | 1:d175631a5803 | 171 | |
wbeaumont | 1:d175631a5803 | 172 | |
wbeaumont | 0:5bc0f4bd5aa0 | 173 | // never reach this |
wbeaumont | 0:5bc0f4bd5aa0 | 174 | return 1; |
wbeaumont | 0:5bc0f4bd5aa0 | 175 | } |