test program for the AT30TSE75X class

Dependencies:   AT30TSE75x DevInterfaces I2Cinterfaces mbed

Fork of MCP4728test by wimbeaumont Project

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.

Committer:
wbeaumont
Date:
Tue Jan 17 13:44:20 2017 +0000
Revision:
3:789bc6653f83
Parent:
2:8ce5a2128381
Child:
4:4d20444d5116
write TSENSOR to ee-prom, write protect ee-prom

Who changed what in which revision?

UserRevisionLine numberNew 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 3:789bc6653f83 10 #define AT30TSE753EXAMPLEVER "0.56"
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 0:5bc0f4bd5aa0 39 Serial pc(USBTX, USBRX);
wbeaumont 0:5bc0f4bd5aa0 40
wbeaumont 2:8ce5a2128381 41 void print_buf_hex( char *data, int length){
wbeaumont 2:8ce5a2128381 42 int nr;
wbeaumont 2:8ce5a2128381 43 char *ptr=data;
wbeaumont 2:8ce5a2128381 44 for ( int lc=0; lc < length ; lc++){
wbeaumont 2:8ce5a2128381 45 nr= (int) *(ptr++);
wbeaumont 2:8ce5a2128381 46 printf( "%02x ",nr);
wbeaumont 2:8ce5a2128381 47 }
wbeaumont 2:8ce5a2128381 48 printf("\n\r");
wbeaumont 2:8ce5a2128381 49 }
wbeaumont 2:8ce5a2128381 50
wbeaumont 2:8ce5a2128381 51
wbeaumont 3:789bc6653f83 52 int main(void) { //432109876543210 \0
wbeaumont 3:789bc6653f83 53 char str1[16] = "SOLTEMPERAT0230";
wbeaumont 3:789bc6653f83 54 char str2[16] ;
wbeaumont 3:789bc6653f83 55 char str3[16]= "20170116...0230" ;
wbeaumont 3:789bc6653f83 56 char resp[3];
wbeaumont 0:5bc0f4bd5aa0 57 // get the version of getVersion
wbeaumont 0:5bc0f4bd5aa0 58 getVersion gv;
wbeaumont 2:8ce5a2128381 59 int addr=0;
wbeaumont 3:789bc6653f83 60 int i2cerr;
wbeaumont 3:789bc6653f83 61 int cnt=0;
wbeaumont 3:789bc6653f83 62
wbeaumont 2:8ce5a2128381 63 printf("AT30TSE752 example program version %s, compile date %s time %s\n\r",AT30TSE753EXAMPLEVER,__DATE__,__TIME__);
wbeaumont 2:8ce5a2128381 64 printf("getVersion :%s\n\r ",gv.getversioninfo());
wbeaumont 2:8ce5a2128381 65 printf("%s\n\r",str1);
wbeaumont 3:789bc6653f83 66
wbeaumont 2:8ce5a2128381 67 printf("give new addr "); scanf("%d", &addr);
wbeaumont 2:8ce5a2128381 68 if ( addr < 0 || addr > 15) addr=0;
wbeaumont 2:8ce5a2128381 69 fflush(stdin);
wbeaumont 0:5bc0f4bd5aa0 70
wbeaumont 2:8ce5a2128381 71 AT30TSE75x tid( i2cdev ,addr);
wbeaumont 2:8ce5a2128381 72 printf ( "AT30SE75x version :%s\n\r ",tid.getversioninfo());
wbeaumont 2:8ce5a2128381 73 printf( "Taddr %x , Eaddr %x subaddr %d\n\r ", tid.getTaddr(),tid.getEaddr(), addr);
wbeaumont 3:789bc6653f83 74 i2cerr=tid.get_eeprom_protec();
wbeaumont 3:789bc6653f83 75 if ( i2cerr ) { printf("ee-prom write protected \n\r");}
wbeaumont 3:789bc6653f83 76 else { printf("ee-prom NOT write protected \n\r"); }
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 3:789bc6653f83 94 i2cerr= tid.write_eeprompage(str3, 16, 0, (uint8_t) pagenr);
wbeaumont 2:8ce5a2128381 95 }
wbeaumont 2:8ce5a2128381 96 wait_ms(400); // give some time to write
wbeaumont 3:789bc6653f83 97 //strcpy(str1,"newstr1");strcp(str2,"newstr2");strcp(str3,"newstr3");
wbeaumont 3:789bc6653f83 98 bool fix=true;
wbeaumont 2:8ce5a2128381 99 while(1) {
wbeaumont 3:789bc6653f83 100 pagenr=0;
wbeaumont 3:789bc6653f83 101 i2cerr=tid.read_eeprompage(str1, 16, 0, (uint8_t) pagenr++);
wbeaumont 3:789bc6653f83 102 if(i2cerr) printf("eeprom read error %d \n\r",i2cerr);
wbeaumont 3:789bc6653f83 103
wbeaumont 3:789bc6653f83 104 i2cerr=tid.read_eeprompage(str2, 16, 0, (uint8_t) pagenr++);
wbeaumont 3:789bc6653f83 105 if(i2cerr) printf("eeprom read error %d \n\r",i2cerr);
wbeaumont 3:789bc6653f83 106 i2cerr=tid.read_eeprompage(str3, 16, 0, (uint8_t) pagenr);
wbeaumont 2:8ce5a2128381 107 if(i2cerr) printf("eeprom read error %d \n\r",i2cerr);
wbeaumont 3:789bc6653f83 108 str1[15]='\0';str2[15]='\0';str3[15]='\0'; //make sure it ends
wbeaumont 3:789bc6653f83 109 printf("eeprom content page %d :\n\r %s %s %s \n\r",pagenr,str1 ,str2 ,str3 );
wbeaumont 2:8ce5a2128381 110 //print_buf_hex( str1,16);
wbeaumont 3:789bc6653f83 111 if ( fix) {
wbeaumont 3:789bc6653f83 112 printf("protect eeprom ?") ; scanf("%s",resp);
wbeaumont 3:789bc6653f83 113 if(resp[0] == 'y'){
wbeaumont 3:789bc6653f83 114 i2cerr=tid.protect_eeprom();
wbeaumont 3:789bc6653f83 115 if(i2cerr) printf("eeprom protect failed %d \n\r", i2cerr);
wbeaumont 3:789bc6653f83 116 }
wbeaumont 3:789bc6653f83 117 // fix=false;
wbeaumont 3:789bc6653f83 118 }
wbeaumont 2:8ce5a2128381 119 char singlechar;
wbeaumont 2:8ce5a2128381 120 i2cerr=tid.read_eeprombyte(singlechar, 3, (uint8_t) pagenr);
wbeaumont 2:8ce5a2128381 121 if(i2cerr) printf("eeprom read error %d \n\r",i2cerr);
wbeaumont 2:8ce5a2128381 122 printf("third char is : %c\n\r",singlechar);
wbeaumont 2:8ce5a2128381 123 wait_ms(1000);
wbeaumont 2:8ce5a2128381 124 float Tmp= tid.get_temperature(&i2cerr);
wbeaumont 2:8ce5a2128381 125 printf ("%d:Temp = %f I2cerr %d \n\r",cnt++, Tmp, i2cerr);
wbeaumont 2:8ce5a2128381 126
wbeaumont 1:d175631a5803 127
wbeaumont 1:d175631a5803 128 }
wbeaumont 1:d175631a5803 129
wbeaumont 1:d175631a5803 130
wbeaumont 0:5bc0f4bd5aa0 131 // never reach this
wbeaumont 0:5bc0f4bd5aa0 132 return 1;
wbeaumont 0:5bc0f4bd5aa0 133 }