Hi,
I am not sure exactly what issue you are experiencing, but here is my
source code. Hope it helps.
Regards,
Jinsuk
---
Jinsuk Seo
Embedded Device Engineer
Morgan Solar Inc.
100 Symes Rd, Unit 100A Toronto, ON M6N 0A8, Canada
Phone: (416) 203-1655 | Fax: (416) 203-2805 | *[email protected]
<[email protected]>*
*NOTICE: *This message contains information that may be privileged and/or
confidential and is for the sole use of the intended recipients. If you are
not the intended recipient, you are not authorized to read, print, retain,
copy, disseminate, distribute or use any part of this message or its
attachments. If you received this message in error, please notify the
sender immediately and delete all copies of this message.
On Mon, Jun 8, 2015 at 10:58 AM, <[email protected]> wrote:
> Hi Jin,
> I am looking at this post for information and would like to ask you how to
> read using i2c in c++.
> I have the following code for write which works fine.
> txBuffer[0] = 0x2a; // location to write data
> txBuffer[1] = 0xfe ; //data
> opResult = write(i2cFD, txBuffer, 2); //write statement.
> if (opResult !=2) printf("No ACK bit!\n");
>
> I am having issues with read. I have the following code
> opResult = read(i2cFD, (char*)rxBuffer, 6);
>
> printf("Part ID rx0: %x, %x\n", (int)rxBuffer[0], 0x00);
> printf("Part ID rx1: %x, %x\n", (int)rxBuffer[1], 0x01);
> printf("Part ID rx2: %x, %x\n", (int)rxBuffer[2], 0x02);
> printf("Part ID rx3: %x, %x\n", (int)rxBuffer[3], 0x03);
> printf("Part ID rx4: %x, %x\n", (int)rxBuffer[4], 0x04);
> printf("Part ID rx5: %x, %x\n", (int)rxBuffer[5], 0x05);
>
> It prints out different values then what I expect using i2cdump
> Can you shed some light on my problem?
> Thanks big time.
>
>
> On Tuesday, November 6, 2012 at 6:26:54 PM UTC-5, Jin wrote:
>>
>> Hi all,
>>
>> I am trying to change some register settings for TPS6217 (power
>> management chip) through i2c on linux userspace.
>>
>> My hope is to configure the settings using c++. I am using the kernel
>> 3.2.33, and the i2c slave address is already binded to the driver. (seems
>> TPS65217 specific)
>>
>> I know how to use i2c drivers for general i2c, But I don know how to
>> include and use TPS65217 driver on my code. Any ideas?
>>
>> Best regards,
>>
> --
> For more options, visit http://beagleboard.org/discuss
> ---
> You received this message because you are subscribed to a topic in the
> Google Groups "BeagleBoard" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/beagleboard/d_mgCENpj8s/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> [email protected].
> For more options, visit https://groups.google.com/d/optout.
>
--
For more options, visit http://beagleboard.org/discuss
---
You received this message because you are subscribed to the Google Groups
"BeagleBoard" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.
/*
* batterygauge.cpp
* Author: Jinsuk Seo
* Date: Nov 8, 2012
*/
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <linux/i2c.h>
#include <linux/i2c-dev.h>
#include <sys/ioctl.h>
#include <stropts.h>
#include <stdio.h>
#include "batterygauge.h"
#include <iostream>
using namespace std;
#define MAX_BUS 64
#define MODE_CONFIG 0x00
#define OPERATIONAL 0x10
#define STANDBY 0x00
#define CTRL_CONFIG 0x01
#define IO_LOW 0x0C
#define IO_HIGH 0x0D
#define CHARGE_LSB 0x02
#define CHARGE_MSB 0x03
#define COUNTER_LSB 0x04
#define COUNTER_MSB 0x05
#define CURRENT_LSB 0x06
#define CURRENT_MSB 0x07
#define VOLTAGE_LSB 0x08
#define VOLTAGE_MSB 0x09
#define TEMPERATURE_LSB 0x0A
#define TEMPERATURE_MSB 0x0B
batterygauge::batterygauge(int bus, int address) {
I2CBus = bus;
I2CAddress = address;
//readFullSensorState();
}
int batterygauge::readFullSensorState()
{
char namebuf[MAX_BUS];
snprintf(namebuf, sizeof(namebuf), "/dev/i2c-%d", I2CBus);
int file;
if ((file = open(namebuf, O_RDWR)) < 0){
cout << "Failed to open STC3100 on " << namebuf << " I2C Bus" << endl;
return(1);
}
if (ioctl(file, I2C_SLAVE, I2CAddress) < 0){
cout << "I2C_SLAVE address " << I2CAddress << " failed..." << endl;
return(2);
}
int numberBytes = BATTERYGAUGE_I2C_BUFFER;
int bytesRead = read(file, this->dataBuffer, numberBytes);
if (bytesRead == -1){
cout << "Failure to read Byte Stream in readFullSensorState()" << endl;
}
close(file);
this->charge = convertcharge(CHARGE_MSB, CHARGE_LSB);
this->current = convertcurrent(CURRENT_MSB, CURRENT_LSB);
this->voltage = convertvoltage(VOLTAGE_MSB, VOLTAGE_LSB);
this->temperature = converttemperature(TEMPERATURE_MSB, TEMPERATURE_LSB);
return 0;
}
double batterygauge::convertcharge(int msb_reg_addr, int lsb_reg_addr)
{
short msb = 0x00 | dataBuffer[msb_reg_addr];
short lsb = 0x00 | dataBuffer[lsb_reg_addr];
short temp = (msb<<8) | lsb;
double output = temp;
output = 6.70 * output / 50;
return output;
}
double batterygauge::convertcurrent(int msb_reg_addr, int lsb_reg_addr)
{
short temp = dataBuffer[msb_reg_addr];
temp = (temp<<8) | dataBuffer[lsb_reg_addr];
short signedbit = temp & (1 << 13);
if (signedbit == 0)
{
// Positive
temp &= ~(1 << 14);
temp &= ~(1 << 15);
}
else
{
// Negative
temp |= 1 << 14;
temp |= 1 << 15;
}
double output = temp;
output = 11.77 * output / 50;
return output;
}
double batterygauge::convertvoltage(int msb_reg_addr, int lsb_reg_addr)
{
short temp = dataBuffer[msb_reg_addr];
temp = (temp<<8) | dataBuffer[lsb_reg_addr];
short signedbit = temp & (1 << 11);
if (signedbit == 0)
{
// Positive
temp &= 0x0FFF;
}
else
{
// Negative
temp |= 0xF000;
}
double output = temp;
output = 2.44 * output / 1000;
return output;
}
double batterygauge::converttemperature(int msb_reg_addr, int lsb_reg_addr)
{
short temp = dataBuffer[msb_reg_addr];
temp = (temp<<8) | dataBuffer[lsb_reg_addr];
short signedbit = temp & (1 << 11);
if (signedbit == 0)
{
// Positive
temp &= 0x0FFF;
}
else
{
// Negative
temp |= 0xF000;
}
double output = temp;
output = 0.125 * output;
return output;
}
int batterygauge::writeI2CDeviceByte(char address, char value)
{
char namebuf[MAX_BUS];
snprintf(namebuf, sizeof(namebuf), "/dev/i2c-%d", I2CBus);
int file;
if ((file = open(namebuf, O_RDWR)) < 0){
cout << "Failed to open batterygauge on " << namebuf << " I2C Bus" << endl;
return(1);
}
if (ioctl(file, I2C_SLAVE, I2CAddress) < 0){
cout << "I2C_SLAVE address " << I2CAddress << " failed..." << endl;
return(2);
}
char buffer[2];
buffer[0] = address;
buffer[1] = value;
if ( write(file, buffer, 2) != 2) {
cout << "Failure to write values to I2C Device address." << endl;
return(3);
}
close(file);
return 0;
}
int batterygauge::setModeConfig(MODECONFIG mode)
{
int ret_value = 0;
switch(mode)
{
case STAND_BY:
{
ret_value = writeI2CDeviceByte(MODE_CONFIG, STANDBY);
break;
}
case OPERATE:
{
ret_value = writeI2CDeviceByte(MODE_CONFIG, OPERATIONAL);
break;
}
case NOLOAD:
{
ret_value = writeI2CDeviceByte(CTRL_CONFIG, IO_HIGH);
break;
}
case LOAD:
{
ret_value = writeI2CDeviceByte(CTRL_CONFIG, IO_LOW);
break;
}
}
return ret_value;
}
MODECONFIG batterygauge::getModeConfig()
{
this->readFullSensorState();
char temp = dataBuffer[MODE_CONFIG];
short mode = temp & (1 << 4);
//cout << "The current mode config is: " << mode << endl;
this->modeConfig = (MODECONFIG) mode;
return this->modeConfig;
}
/*
* battgauge.h
* Author: Jinsuk Seo
* Date: Nov 8, 2012
*/
#ifndef BATTERYGAUGE_H_
#define BATTERYGAUGE_H_
#define BATTERYGAUGE_I2C_BUFFER 0x80
enum MODECONFIG {
STAND_BY = 0,
OPERATE = 16,
NOLOAD = 2,
LOAD = 3
};
class batterygauge {
private:
int I2CBus, I2CAddress;
char dataBuffer[BATTERYGAUGE_I2C_BUFFER];
double charge;
double current;
double voltage;
double temperature;
MODECONFIG modeConfig;
double convertcharge(int msb_addr, int lsb_addr);
double convertcurrent(int msb_addr, int lsb_addr);
double convertvoltage(int msb_addr, int lsb_addr);
double converttemperature(int msb_addr, int lsb_addr);
int writeI2CDeviceByte(char address, char value);
public:
batterygauge(int bus, int address);
int readFullSensorState();
int setModeConfig(MODECONFIG mode);
MODECONFIG getModeConfig();
double getcharge() { return charge; }
double getcurrent() { return current; }
double getvoltage() { return voltage; }
double gettemperature() { return temperature; }
};
#endif