Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
8 years, 10 months ago.
I2C.h and mbed.h >> classes >> I2C dont match?
Hey everyone,
I am a little confused using the I2C library from mbed.h. I posted in this same forum about the lmp91000 and using I2C with it and keep getting a NACK from my i2c.read(). So I went and looked at the library which I imported mbed.h into my project went to classes down to I2C and saw this part
int read ( int address, char * data, int length, bool repeated = false )
Read from an I2C slave.
Performs a complete read transaction. The bottom bit of the address is forced to 1 to indicate a read.
Parameters: address 8-bit I2C slave address [ addr | 1 ] data Pointer to the byte-array to read data in to length Number of bytes to read repeated Repeated start, true - don't send stop at end
Returns: 0 on success (ack), non-0 on failure (nack)
it shows the 0 on sucess and non-0 on failure which I keep getting 1 on my return from i2c.read so I assume its not working correctly. But then I look at the actual i2c.h file from here
https://developer.mbed.org/users/mbed_official/code/mbed/file/25aea2a3f4e3/I2C.h
and it shows this
int read(int address, char *data, int length, bool repeated = false);
/ Read a single byte from the I2C bus
- @param ack indicates if the byte is to be acknowledged (1 = acknowledge)
- @returns
- the byte read
- /
enum Acknowledge { NoACK = 0, ACK = 1 };
Which is completely opposite. So I am not sure which one is correct?
Thanks
1 Answer
8 years, 10 months ago.
I think you've mixed up the comments as they are correct. For write & read using this style function, it says:
* @returns * 0 on success (ack), * non-0 on failure (nack) */ int write(int address, const char *data, int length, bool repeated = false);
However, for a single byte write you have a different return value
* @returns * '1' if an ACK was received, * '0' otherwise */ int write(int data);
O okay I see,thank you for clearing that up. Unfortunately, I still am not able to read from the LMP91000 eval board. I used the example code for writing/reading and I know that I am using the correct address for the LMP since it is listed on the data sheet. I still am getting a return value of 1 and I can't seem to find anything wrong with the code I am using. Here is what I have: see below post
Thanks
posted by 10 Oct 2016Sorry, I think we are misunderstanding it each other. Here is my code, I am using the first write function you posted which 0 is success and non-0 is fail. Which I keep receiving 1 which is a failure and the data value stored in status register is 0 and should be 1 on bootup. I am not using the single byte write function.
#include "mbed.h" I2C i2c(p10, p8); char data[1] = {0}; char ptrAddr [1]; int addr = 0x90; //shifted from 0x48 7bit to 8bit addr int returnVal = 0; Serial pc(USBTX, USBRX); int main() { pc.baud(9600); wait(5); i2c.frequency(100000); ptrAddr[0] = 0; while(1) { returnVal = i2c.write(addr, ptrAddr, 1,true); //Write to register to read from pc.printf("It is :%x\r\n", returnVal); i2c.read(addr, data, 1,false); //read actual data from register pc.printf("the value is :%x\r\n", data[0]); wait(1); } }
This crazy forum won't let the same person answer twice!! I was not familiar with the LMP91000 but it seems it's a TI development board. In the user manual it states "The I2C bus requires two 10kohm pull-up resistors (R1, R2);" Did you install those on the LMP board? I think your failure code is because you're not getting an ACK from the chip. If there is no pullup then it can't ack...just some thoughts...
posted by Bill Bellis 10 Oct 2016Yes, It is a TI eval board but essentially I am just using it as a breakout board. Not using the ADC on it or anything just the LMP91000 chip that sits on the board. I do have the 10k pullups on the SDA/SCL and its very strange that I keep getting a NACK from the write. I have tried literally a million different combinations of code using the i2c library and all the examples seem to match up with what I have. Like I said above I have the tmp102 sensor able to communicate through ble just fine over i2c using the same library and almost identical code. The very weird thing is I have tried to just wire up the LMP91000 in place of the tmp102 to see if the code that I know works with ble would work with the LMP and no dice. I am not sure if its something with the red bear ble nano or the serial and i2c running at the same time or the LMP. I might try out the arduino i2c library with wire.h just to confirm whats actually causing the issue but I have heard mixed issues with it on the red bear forums.
posted by Alexandra Fleitas 10 Oct 2016Ok, other questions. You've checked the VREF pin to make sure the voltage is getting to the LMP91000? Voltage across C1 on the TI board seems correct? You've got the MENB jumper at 2-3?
posted by Bill Bellis 11 Oct 2016Checked VREF its 2.5V as listed. Also to clarify the pinsetup is VDD on board, GND1 on board, SDA/SCL connected to pin 11/12, MENB 2-3 jumpered. VDD sitting at 3.3V. Not sure what is causing the failure to write, it seems like the chip is not booting up correctly.
posted by Alexandra Fleitas 11 Oct 2016