void I2cif::i2cWrite(QString devName, unsigned char address, QString data) { int file; char buf[200]; bool parseOk; QByteArray tmpBa = devName.toUtf8(); const char* devNameChar = tmpBa.constData(); fprintf(stderr, "writing to address %02x: ", address); /* parse QString data to buf */ QStringList bytes = data.split(" "); int i; for (i=0 ; i<bytes.length(); i++) { QString tmp = bytes.value(i); buf[i] = tmp.toInt(&parseOk, 16); if (!parseOk) { fprintf(stderr, "parsing error %d\n", i); emit i2cError(); return; } fprintf(stderr, "%02x ", buf[i]); } fprintf(stderr, "\n"); if ((file = open (devNameChar, O_RDWR)) < 0) { fprintf(stderr,"open error\n"); emit i2cError(); return; } if (ioctl(file, I2C_SLAVE, address) < 0) { close(file); fprintf(stderr,"ioctl error\n"); emit i2cError(); return; } /* Try to read 2 bytes. This is also safe for LM75 */ if (write( file, buf, bytes.length() ) != bytes.length()) { close(file); fprintf(stderr,"write error\n"); emit i2cError(); return; } close(file); fprintf(stderr,"write ok\n"); emit i2cWriteOk(); }
void I2cif::i2cRead(QString devName, unsigned char address, int count) { int file; char buf[200]; Conv conv; m_readResult = ""; emit i2cReadResultChanged(); QByteArray tmpBa = devName.toUtf8(); const char* devNameChar = tmpBa.constData(); fprintf(stderr, "reading from address %02x count %d\n", address, count); if ((file = open (devNameChar, O_RDWR)) < 0) { fprintf(stderr,"open error\n"); emit i2cError(); return; } if (ioctl(file, I2C_SLAVE, address) < 0) { close(file); fprintf(stderr,"ioctl error\n"); emit i2cError(); return; } /* Try to read 2 bytes. This is also safe for LM75 */ if (read( file, buf, count ) != count) { close(file); fprintf(stderr,"read error\n"); emit i2cError(); return; } close(file); /* copy buf to m_readResult */ int i; fprintf(stderr, "read "); for (i=0; i<count ; i++) { m_readResult = m_readResult + conv.toHex(buf[i],2) + " "; fprintf(stderr, "%02x ", buf[i]); } fprintf(stderr, "\n"); emit i2cReadResultChanged(); }
void i2cSendRepeatedStart(void) { // Send START or REPEATED START // Make sure that there is at least 1.3 microseconds between stop and start delayMicroseconds(2); pinMode(SDA,OUTPUT); // send start condition TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN); i2cWaitForComplete(); // could separate this, to allow computation while waiting if (!REPEATED_START_OK) {i2cError("Repeated Start not acked");} }
void i2cSendByte(uint8_t data) { // delay(1); pinMode(SDA,OUTPUT); while (!i2cWaitForComplete()) { i2cError("i2c not ready for sending"); delay(2); } // save data to the TWDR TWDR = data; // begin send TWCR = (1<<TWINT)|(1<<TWEN); i2cWaitForComplete(); // could separate this, to allow computation while waiting }
void i2cReceiveByte(uint8_t ackFlag) { pinMode(SDA,INPUT); // begin receive over i2c if( ackFlag ) { // ackFlag = TRUE: ACK the recevied data TWCR = (TWCR&TWCR_CMD_MASK) |BV(TWINT) |BV(TWEA); } else { // ackFlag = FALSE: NACK the recevied data TWCR = (TWCR&TWCR_CMD_MASK) |BV(TWINT); } i2cWaitForComplete(); // could separate this, to allow computation while waiting if (!READ_DATA_OK) {i2cError("Read byte not acked");} }
void i2cSendData(uint8_t data) { i2cSendByte(data); if (!WRITE_DATA_OK) {i2cError("Write byte not acked");} }
void i2cSendReadAddress(uint8_t i2c_7bit_address) { i2cSendByte((i2c_7bit_address<<1)|0x01); // read mode if(!READ_ADDRESS_OK) i2cError("Read address not acked"); }
void i2cSendWriteAddress(uint8_t i2c_7bit_address) { i2cSendByte((i2c_7bit_address<<1)); // write mode if(!WRITE_ADDRESS_OK) i2cError("Write address not acked"); }