unsigned char i2c_readreg(unsigned char theSlave, unsigned char theReg) { unsigned char b = 0; int error, cntr = 3; unsigned long flags; spin_lock_irqsave(&i2c_lock, flags); do { error = 0; i2c_start(); i2c_outbyte((theSlave & 0xfe)); if(!i2c_getack()) error = 1; i2c_dir_out(); i2c_outbyte(theReg); if(!i2c_getack()) error |= 2; i2c_delay(CLOCK_LOW_TIME); i2c_start(); i2c_outbyte(theSlave | 0x01); if(!i2c_getack()) error |= 4; b = i2c_inbyte(); i2c_sendnack(); i2c_stop(); } while(error && cntr--); spin_unlock_irqrestore(&i2c_lock, flags); return b; }
int i2c_writereg(unsigned char theSlave, unsigned char theReg, unsigned char theValue) { int error, cntr = 3; unsigned long flags; spin_lock_irqsave(&i2c_lock, flags); do { error = 0; i2c_start(); i2c_outbyte((theSlave & 0xfe)); if(!i2c_getack()) error = 1; i2c_dir_out(); i2c_outbyte(theReg); if(!i2c_getack()) error |= 2; i2c_outbyte(theValue); if(!i2c_getack()) error |= 4; i2c_stop(); } while(error && cntr--); i2c_delay(CLOCK_LOW_TIME); spin_unlock_irqrestore(&i2c_lock, flags); return -error; }
int i2c_write(unsigned char theSlave, void *data, size_t nbytes) { int error, cntr = 3; unsigned char bytes_wrote = 0; unsigned char value; unsigned long flags; spin_lock_irqsave(&i2c_lock, flags); do { error = 0; i2c_start(); i2c_outbyte((theSlave & 0xfe)); if (!i2c_getack()) error = 1; for (bytes_wrote = 0; bytes_wrote < nbytes; bytes_wrote++) { memcpy(&value, data + bytes_wrote, sizeof value); i2c_outbyte(value); if (!i2c_getack()) error |= 4; } i2c_stop(); } while (error && cntr--); i2c_delay(CLOCK_LOW_TIME); spin_unlock_irqrestore(&i2c_lock, flags); return -error; }
/*#--------------------------------------------------------------------------- *# *# FUNCTION NAME: i2c_writereg *# *# DESCRIPTION : Writes a value to an I2C device *# *#--------------------------------------------------------------------------*/ int i2c_writereg(unsigned char theSlave, unsigned char theReg, unsigned char theValue) { int error, cntr = 3; unsigned long flags; do { error = 0; /* * we don't like to be interrupted */ local_irq_save(flags); local_irq_disable(); i2c_start(); /* * send slave address */ i2c_outbyte((theSlave & 0xfe)); /* * wait for ack */ if(!i2c_getack()) error = 1; /* * now select register */ i2c_dir_out(); i2c_outbyte(theReg); /* * now it's time to wait for ack */ if(!i2c_getack()) error |= 2; /* * send register register data */ i2c_outbyte(theValue); /* * now it's time to wait for ack */ if(!i2c_getack()) error |= 4; /* * end byte stream */ i2c_stop(); /* * enable interrupt again */ local_irq_restore(flags); } while(error && cntr--); i2c_delay(CLOCK_LOW_TIME); return -error; }
void write_ext_eeprom(unsigned char id,long int address, unsigned char data){ while(!ext_eeprom_ready(id)); i2c_start(); i2c_outbyte((id|(int)(address>>7))&0xfe); i2c_outbyte(address); i2c_outbyte(data); i2c_stop(); }
void set_data (short int g_in, short int m_in, short int a_in) { i2c_stop(); i2c_start(); if (i2c_outbyte(0xd0)==0){i2c_stop();} i2c_outbyte(4); i2c_outbyte(g_in); i2c_stop(); i2c_start(); i2c_stop(); i2c_start(); if (i2c_outbyte(0xd0)==0){i2c_stop();} i2c_outbyte(5); i2c_outbyte(m_in); i2c_stop(); i2c_start(); i2c_stop(); i2c_start(); if (i2c_outbyte(0xd0)==0){i2c_stop();} i2c_outbyte(6); i2c_outbyte(a_in); i2c_stop(); i2c_start(); }
void set_ora (short int o_in, short int min_in) { i2c_stop(); i2c_start(); if (i2c_outbyte(0xd0)==0){i2c_stop();} i2c_outbyte(2); i2c_outbyte(o_in); i2c_stop(); i2c_start(); i2c_stop(); i2c_start(); if (i2c_outbyte(0xd0)==0){i2c_stop();} i2c_outbyte(1); i2c_outbyte(min_in); i2c_stop(); i2c_start(); i2c_stop(); i2c_start(); if (i2c_outbyte(0xd0)==0){i2c_stop();} i2c_outbyte(0); i2c_outbyte(0x00); i2c_stop(); i2c_start(); }
void set_data (unsigned char g_in, unsigned char m_in, unsigned char a_in) { unsigned char temp; itoa(g_in,16);//ritorna valore dentro stringa.. m ke skifo di funzione ki l'ha fatta??????? temp=atoi(stringa); i2c_stop(); i2c_start(); if (i2c_outbyte(0xd0)==0){i2c_stop();} i2c_outbyte(4); i2c_outbyte(temp); i2c_stop(); i2c_start(); itoa(m_in,16);//ritorna valore dentro stringa.. m ke skifo di funzione ki l'ha fatta??????? temp=atoi(stringa); i2c_stop(); i2c_start(); if (i2c_outbyte(0xd0)==0){i2c_stop();} i2c_outbyte(5); i2c_outbyte(temp); i2c_stop(); i2c_start(); itoa(g_in,16);//ritorna valore dentro stringa.. m ke skifo di funzione ki l'ha fatta??????? temp=atoi(stringa); i2c_stop(); i2c_start(); if (i2c_outbyte(0xd0)==0){i2c_stop();} i2c_outbyte(6); i2c_outbyte(temp); i2c_stop(); i2c_start(); }
void ds1307_init (void) { short int secondi; secondi=read_sec(); i2c_stop(); i2c_start(); if (i2c_outbyte(0xd0)==0) {i2c_stop();} i2c_outbyte(0); i2c_outbyte(secondi & 0x7F); i2c_stop(); i2c_start(); }
short int read_hour (void) { short int hours; i2c_stop(); i2c_start(); if (i2c_outbyte(0xd0)==0) {i2c_stop();} i2c_outbyte(2); i2c_start(); if (i2c_outbyte(0xd1)==0) {i2c_stop();} hours=i2c_inbyte(2); i2c_stop(); return hours; }
short int read_min (void) { short int mins; i2c_stop(); i2c_start(); if (i2c_outbyte(0xd0)==0) {i2c_stop();} i2c_outbyte(1); i2c_start(); if (i2c_outbyte(0xd1)==0) {i2c_stop();} mins=i2c_inbyte(1); i2c_stop(); return mins; }
short int read_sec (void) { short int secs; i2c_stop(); i2c_start(); if (i2c_outbyte(0xd0)==0) {i2c_stop();} i2c_outbyte(0); i2c_start(); if (i2c_outbyte(0xd1)==0) {i2c_stop();} secs=i2c_inbyte(0); i2c_stop(); return secs; }
/*#--------------------------------------------------------------------------- *# *# FUNCTION NAME: i2c_writereg *# *# DESCRIPTION : Writes a value to an I2C device *# *#--------------------------------------------------------------------------*/ int i2c_writereg(unsigned char theSlave, unsigned char theReg, unsigned char theValue) { int error, cntr = 3; unsigned long flags; spin_lock_irqsave(&i2c_lock, flags); do { error = 0; i2c_start(); /* * send slave address */ i2c_outbyte((theSlave & 0xfe)); /* * wait for ack */ if(!i2c_getack()) error = 1; /* * now select register */ i2c_dir_out(); i2c_outbyte(theReg); /* * now it's time to wait for ack */ if(!i2c_getack()) error |= 2; /* * send register register data */ i2c_outbyte(theValue); /* * now it's time to wait for ack */ if(!i2c_getack()) error |= 4; /* * end byte stream */ i2c_stop(); } while(error && cntr--); i2c_delay(CLOCK_LOW_TIME); spin_unlock_irqrestore(&i2c_lock, flags); return -error; }
short int read_daysett (void) { short int daysett; i2c_stop(); i2c_start(); if (i2c_outbyte(0xd0)==0) {i2c_stop();} i2c_outbyte(3); i2c_start(); if (i2c_outbyte(0xd1)==0) {i2c_stop();} daysett=i2c_inbyte(3); i2c_stop(); return daysett; }
short int read_day (void) { short int days; i2c_stop(); i2c_start(); if (i2c_outbyte(0xd0)==0) {i2c_stop();} i2c_outbyte(4); i2c_start(); if (i2c_outbyte(0xd1)==0) {i2c_stop();} days=i2c_inbyte(4); i2c_stop(); return days; }
short int read_year (void) { short int years; i2c_stop(); i2c_start(); if (i2c_outbyte(0xd0)==0) {i2c_stop();} i2c_outbyte(6); i2c_start(); if (i2c_outbyte(0xd1)==0) {i2c_stop();} years=i2c_inbyte(6); i2c_stop(); return years; }
//INIZIALIZZAZIONE RTC_DS1307 void ds1307_init (void) { unsigned char secondi; secondi=read_sec(); i2c_stop(); i2c_start(); if (i2c_outbyte(0xd0)==0) {i2c_stop();} i2c_outbyte(0); i2c_outbyte(secondi & 0x7F); i2c_stop(); i2c_start(); }
short int read_month (void) { short int months; i2c_stop(); i2c_start(); if (i2c_outbyte(0xd0)==0) {i2c_stop();} i2c_outbyte(5); i2c_start(); if (i2c_outbyte(0xd1)==0) {i2c_stop();} months=i2c_inbyte(5); i2c_stop(); return months; }
unsigned char read_year (void) { unsigned char years; i2c_stop(); i2c_start(); if (i2c_outbyte(0xd0)==0) {i2c_stop();} i2c_outbyte(6); i2c_start(); if (i2c_outbyte(0xd1)==0) {i2c_stop();} years=i2c_inbyte(6); i2c_stop(); return years; }
unsigned char read_month (void) { unsigned char months; i2c_stop(); i2c_start(); if (i2c_outbyte(0xd0)==0) {i2c_stop();} i2c_outbyte(5); i2c_start(); if (i2c_outbyte(0xd1)==0) {i2c_stop();} months=i2c_inbyte(5); i2c_stop(); return months; }
unsigned char read_day (void) { unsigned char days; i2c_stop(); i2c_start(); if (i2c_outbyte(0xd0)==0) {i2c_stop();} i2c_outbyte(4); i2c_start(); if (i2c_outbyte(0xd1)==0) {i2c_stop();} days=i2c_inbyte(4); i2c_stop(); return days; }
unsigned char read_sec (void) { unsigned char secs; i2c_stop(); i2c_start(); if (i2c_outbyte(0xd0)==0) {i2c_stop();} i2c_outbyte(0); i2c_start(); if (i2c_outbyte(0xd1)==0) {i2c_stop();} secs=i2c_inbyte(0); i2c_stop(); return secs; }
unsigned char read_daysett (void) { unsigned char daysett; i2c_stop(); i2c_start(); if (i2c_outbyte(0xd0)==0) {i2c_stop();} i2c_outbyte(3); i2c_start(); if (i2c_outbyte(0xd1)==0) {i2c_stop();} daysett=i2c_inbyte(3); i2c_stop(); return daysett; }
unsigned char read_min (void) { unsigned char mins; i2c_stop(); i2c_start(); if (i2c_outbyte(0xd0)==0) {i2c_stop();} i2c_outbyte(1); i2c_start(); if (i2c_outbyte(0xd1)==0) {i2c_stop();} mins=i2c_inbyte(1); i2c_stop(); return mins; }
/*#--------------------------------------------------------------------------- *# *# FUNCTION NAME: i2c_writereg *# *# DESCRIPTION : writes a value to a register of an I2C device *# *# PARAMETERS : theSlave = slave address of the I2C device *# theReg = register of the I2C device that needs to be written *# theValue = value to be written to the register *# *# RETURN : returns OR-ed result of the write action: *# 0 = Ok *# 1 = Slave_NoAck *# 2 = Reg_NoAck *# 4 = Val_NoAck *# *#--------------------------------------------------------------------------- */ int i2c_writereg( unsigned char theSlave , unsigned char theReg , unsigned char theValue ) { int error, cntr = 3; unsigned long flags; spin_lock( &i2c_lock ); do { error = 0; /* we don't like to be interrupted */ local_irq_save( flags ); i2c_start(); /* send slave address */ if ( EI2CNOACKNLD == i2c_outbyte( theSlave & 0xfe ) ) { error = 1; } /* now select register */ if ( EI2CNOACKNLD == i2c_outbyte( theReg ) ) { error |= 2; } /* send register register data */ if ( EI2CNOACKNLD == i2c_outbyte( theValue ) ) { error |= 4; } /* end byte stream */ i2c_stop(); /* enable interrupt again */ local_irq_restore( flags ); } while ( error && cntr-- ); i2c_delay( TLOW ); spin_unlock( &i2c_lock ); return ( -error ); } /* i2c_writereg */
//LETTURA PRESSIONE float PressureRead (void){ unsigned char buf[2]; int i; float pressure; i2c_stop(); i2c_start(); if (i2c_outbyte(0xF1)==0) { i2c_stop(); printf("Ricevuto NACK 1\n"); return -1; } buf[0]=i2c_inbyte(1); buf[1]=i2c_inbyte(0); i2c_stop(); printf ("Byte 1: [%03d] Byte 2 [%03d]\n",buf[0],buf[1]); // Converto in float i=buf[0]; i<<=8; i+=buf[1]; pressure=0.09375*i+762.5; return pressure; }
int i2c_read(unsigned char theSlave, void *data, size_t nbytes) { unsigned char b = 0; unsigned char bytes_read = 0; int error, cntr = 3; unsigned long flags; spin_lock_irqsave(&i2c_lock, flags); do { error = 0; memset(data, 0, nbytes); i2c_start(); i2c_outbyte((theSlave | 0x01)); if (!i2c_getack()) error = 1; for (bytes_read = 0; bytes_read < nbytes; bytes_read++) { b = i2c_inbyte(); memcpy(data + bytes_read, &b, sizeof b); if (bytes_read < (nbytes - 1)) i2c_sendack(); } i2c_sendnack(); i2c_stop(); } while (error && cntr--); spin_unlock_irqrestore(&i2c_lock, flags); return -error; }
int ext_eeprom_ready(unsigned char id){ int ack; i2c_start(); ack=i2c_outbyte(id); i2c_stop(); return ack; }
unsigned char read_hour (void) { unsigned char hours; i2c_stop(); i2c_start(); if (i2c_outbyte(0xd0)==0) {i2c_stop();} i2c_outbyte(2); i2c_start(); if (i2c_outbyte(0xd1)==0) {i2c_stop();} hours=i2c_inbyte(2); i2c_stop(); //passare da hex a decimale return hours; }
unsigned char read_ext_eeprom(unsigned char id, long int address){ unsigned char data; while(!ext_eeprom_ready(id)); i2c_start(); i2c_outbyte((id|(int)(address>>7))&0xfe); i2c_outbyte(address); i2c_start(); i2c_outbyte((id|(int)(address>>7))|1); data=i2c_inbyte(0); i2c_stop(); return data;//tra parentesi data (data) }