//****************************************************************** //Erasing EEPROM (filling it with databyte 0xff) //****************************************************************** unsigned char EEPROM_erase(void) { unsigned char errorStatus; unsigned int i; errorStatus = i2c_start(); if(errorStatus == 1) { i2c_stop(); return(1); } errorStatus = i2c_sendAddress(EEPROM_W); if(errorStatus == 1) { i2c_stop(); return(1); } errorStatus = i2c_sendData(0x00); if(errorStatus == 1) { i2c_stop(); return(1); } /* errorStatus = i2c_sendData(0x00); if(errorStatus == 1) { i2c_stop(); return(1); } */ for(i=0;i<256;i++) { errorStatus = i2c_sendData(0x00); if(errorStatus == 1) { i2c_stop(); return(1); } } i2c_stop(); return(0); }
int writeTo(byte _dev_address,byte address, byte val) { int a=0; a=i2c_beginTransmission(I2Ci,_dev_address,i2c_write); // start transmission to device //usart_printfm(USART1,(const int *)"Writing to this device address: %2x\n\r",_dev_address); if(a<0) return -1; a=i2c_sendData(I2Ci,address); // send register address if(a<0) return -1; a=i2c_sendData(I2Ci,val); // send value to write if(a<0) return -1; while(I2C_GetFlagStatus(I2Ci,I2C_FLAG_BUSY)!=SET); a=i2c_stopTransmission(I2Ci); // end transmission return 0; }
//****************************************************************** //Function to a character string to EEPROM //****************************************************************** unsigned char EEPROM_write(unsigned char highAddress, unsigned char lowAddress, unsigned char* string) { unsigned char errorStatus, data; errorStatus = i2c_start(); if(errorStatus == 1) { i2c_stop(); return(1); } errorStatus = i2c_sendAddress(EEPROM_W); if(errorStatus == 1) { i2c_stop(); return(1); } /* errorStatus = i2c_sendData(highAddress); if(errorStatus == 1) { i2c_stop(); return(1); } */ errorStatus = i2c_sendData(lowAddress); if(errorStatus == 1) { i2c_stop(); return(1); } while(*string) { errorStatus = i2c_sendData(*(string++)); if(errorStatus == 1) { i2c_stop(); return(1); } } i2c_stop(); return(0); }
//****************************************************************** //Function to read given number of bytes from EEPROM //****************************************************************** unsigned char EEPROM_read(unsigned char highAddress, unsigned char lowAddress, unsigned char totalChar, unsigned char* string) { unsigned char errorStatus, i, data; errorStatus = i2c_start(); if(errorStatus == 1) { i2c_stop(); return(1); } errorStatus = i2c_sendAddress(EEPROM_W); if(errorStatus == 1) { i2c_stop(); return(1); } /* errorStatus = i2c_sendData(highAddress); if(errorStatus == 1) { i2c_stop(); return(1); } */ errorStatus = i2c_sendData(lowAddress); if(errorStatus == 1) { i2c_stop(); return(1); } errorStatus = i2c_repeatStart(); if(errorStatus == 1) { i2c_stop(); return(1); } errorStatus = i2c_sendAddress(EEPROM_R); if(errorStatus == 1) { i2c_stop(); return(1); } for(i=0;i<totalChar;i++) { if(i == (totalChar-1)) //no Acknowledge after receiving the last byte *(string) = i2c_receiveData_NACK(); else *(string) = i2c_receiveData_ACK(); if(*(string++) == ERROR_CODE) { i2c_stop(); return(1); } } i2c_stop(); return(0); }
int readFrom(byte _dev_address,byte address, int num, byte _buff[]) { int a=0,i=0; int timeout; uint8_t flag1=0,flag2=0; a=i2c_beginTransmission(I2Ci,_dev_address,i2c_write); // start transmission to device if(a<0) return -1; // usart_printfm(USART1,(const int *)"Reading from this device address: %2x\n\r",_dev_address); a=i2c_sendData(I2Ci,address); // sends address to read from if(a<0) return -1; //usart_printfm(USART1,(const int *)"sent this data which is the address to read from: %2x\n\r",address); // if(a<0) // return (a-1); //i2c_stopTransmission(I2Cx); // end transmission // if(a<0) // return (a-2); a=i2c_beginTransmission(I2Ci,_dev_address,i2c_read); // start transmission to device if(a<0) return -1; //usart_printfm(USART1,(const int *)"Initiated the register read cycle: %2x\n\r",_dev_address); // if(a<0) // return a; // request 6 bytes from device if(num==1){ I2C_AcknowledgeConfig(I2Ci, DISABLE); /* Test on I2C1 EV8 and clear it */ timeout = I2C_TIMEOUT_MAX; /* Initialize timeout value */ // wait until one byte has been received while( !I2C_CheckEvent(I2Ci, I2C_EVENT_MASTER_BYTE_RECEIVED) ){ ms_delay(100); if((timeout--)==0){ flag1 = I2Ci->SR1; flag2 = I2Ci->SR2; usart_printf(USARTx,"Flag1:%04x \n\r Flag2:%04x\n\r",flag1,flag2); usart_printf(USARTx,"Failing at read nnack stage\n\r"); return -1; } } // wait until one byte has been received I2C_GenerateSTOP(I2Ci, ENABLE); // read data from I2C data register and return data byte _buff[i] = I2C_ReceiveData(I2Ci); return 0; } if(num==2) { timeout = I2C_TIMEOUT_MAX; // while(I2C_GetFlagStatus(I2Ci,I2C_FLAG_ADDR)!=SET){ // ms_delay(100); // if((timeout--)==0){ // flag1 = I2Ci->SR1; // flag2 = I2Ci->SR2; // usart_printf(USARTx,"Flag1:%04x \n\r Flag2:%04x\n\r",flag1,flag2); // usart_printf(USARTx,"Failing at reading 2 bytes stage\n\r"); // return ; // }} I2C_AcknowledgeConfig(I2Ci, DISABLE); I2C_NACKPositionConfig(I2Ci, I2C_NACKPosition_Next); timeout = I2C_TIMEOUT_MAX; while(I2C_GetFlagStatus(I2Ci,I2C_FLAG_BTF)!=SET){ ms_delay(100); if((timeout--)==0){ flag1 = I2Ci->SR1; flag2 = I2Ci->SR2; usart_printf(USARTx,"Flag1:%04x \n\r Flag2:%04x\n\r",flag1,flag2); usart_printf(USARTx,"Failing at reading 2 bytes stage2\n\r"); return -1; }} i2c_stopTransmission(I2Ci); _buff[i] = I2C_ReceiveData(I2Ci); i++; _buff[i] = I2C_ReceiveData(I2Ci); num-=2; // receive a byte //usart_printfm(USART1,(const int *)"this byte read from accel: %2x\n\r",i); return 0; } while(num>0) // device may send less than requested (abnormal) if(num==3){ //_buff[i]=I2C_read_ack(I2Cx); timeout = I2C_TIMEOUT_MAX; while(I2C_GetFlagStatus(I2Ci,I2C_FLAG_BTF)!=SET){ ms_delay(100); if((timeout--)==0){ flag1 = I2Ci->SR1; flag2 = I2Ci->SR2; usart_printf(USARTx,"Flag1:%04x \n\r Flag2:%04x\n\r",flag1,flag2); usart_printf(USARTx,"Failing at reading 2 bytes stage2\n\r"); return -1; }} I2C_AcknowledgeConfig(I2Ci, DISABLE); _buff[i]=I2C_ReceiveData(I2Ci); i++; num--; while(I2C_GetFlagStatus(I2Ci,I2C_FLAG_BTF)!=SET) { ms_delay(100); if((timeout--)==0){ flag1 = I2Ci->SR1; flag2 = I2Ci->SR2; usart_printf(USARTx,"Flag1:%04x \n\r Flag2:%04x\n\r",flag1,flag2); usart_printf(USARTx,"Failing at reading 2 bytes stage2\n\r"); return -1; }} i2c_stopTransmission(I2Ci); _buff[i]=I2C_ReceiveData(I2Ci); i++; _buff[i]=I2C_ReceiveData(I2Ci); i++; num-=2; //usart_print(USART1,"This byte read from nack: %2x\n\r",i); return 0; } else{ a=I2C_read(I2Ci,_buff[i]); if(a<0) return -1; i++; num--; } // if(i != num){ // status = ADXL345_ERROR; // error_code = ADXL345_READ_ERROR; // } //ms_delay(10); //i2c_stopTransmission(I2Cx); // end transmission //end : // usart_printf(USARTx,"Error occured !! Restarting the interface!!"); // ms_delay(5000); // return -1; return 0; }