Confused about how this I2C read works - Page 1
Forum section: electronics / projects

author: Chet T16
author_rank: Frequent Contributor
timestamp: February 24, 2013, 10:39:22 pm
content: I have an i2c master which writes to a slave (controlling the display) and reads from the same slave getting the button presses.In idle mode the masters reads every 0.5 seconds or so and receives the followingQuotePressing a button causes a read instantly giving similar to belowQuoteThere is a third line in addition to SDA and SCL which is pulled low during read and write operations, i thought perhaps that the slave was pulling this low when data was available but having disconnected the master and watched this third line it is going low for three 6ms pulses every 0.5 seconds. Pressing a button has no affect on this.I have successfully managed to write to the slave but not read. I have tried to read both 2 and 5 bytes when the third line goes low. Reading 2 will give me 0x01 0x01 as above but trying 5 will just give me 0x01 0x01 0xff 0xff 0xff with button presses having no affect on the output.Any clues how the master might be telling when there is data to be read or what i could try?Thanks

author: Chet T16
author_rank: Frequent Contributor
timestamp: February 25, 2013, 01:47:09 am
content: Further testing shows that its the slave that decides when to be read from, the master holding the line low when writing and the slave doing the same for reads.Using the arduino wire library as read requests need to specify the number of bytes, i this normal practise? Should the master be able to open a request and the slave decide when to stop?

author: shebu18
author_rank: Frequent Contributor
timestamp: February 25, 2013, 11:05:58 am
content: Can you give us the model of the slave and master? Exact name. In I2C you have only 2 line, not 3. 3lines sounds more like some sort of SPI.

author: richcj10
author_rank: Unknown
timestamp: February 25, 2013, 11:06:27 am
content: It really depends on the device. There are EEPROM devices that allow consecutive reads from the device w/o requesting the data.What chip is it? That would tell forum members more information.

author: richcj10
author_rank: Unknown
timestamp: February 25, 2013, 11:07:21 am
content: Quote from: shebu18 on February 25, 2013, 11:05:58 amIt could be a interrupt....

author: Chet T16
author_rank: Frequent Contributor
timestamp: February 25, 2013, 11:21:17 am
content: I'm calling it i2c as not counting the third line it is the same and the display will work with only the data and clock connected.I don't know the models as I haven't taken them apart (these are in my car) I only have access to the wiring between them.

author: caroper
author_rank: Regular Contributor
timestamp: February 25, 2013, 02:13:07 pm
content: "I'm calling it i2c as not counting the third line it is the same and the display will work with only the data and clock connected."You can't just arbitrarily call it anything based on the number of lines, you may as well call it RS232 as that will work with only 2 lines as well.Based on your description though I would have to agree that you are using SPI not I2C, as SPI also requires Clock and a Data lines. The 3rd or more lines in SPI are Chip Select Lines when you have more than one device on the bus.Whilst electrically the same SPI, I2C and RS232 are all Software Protocols as well as a Hardware Standard.To really know what is happening you need to know what the Protocol is and therefor what Signals and Timing to expect on the lines, hence we can't help you unless you state the Protocol used.CheersChris

author: Chet T16
author_rank: Frequent Contributor
timestamp: February 25, 2013, 02:50:34 pm
content: When i sat its the same i mean the protocol is the same, aside from the 7kHz clock speed which i put down to being used because of the distance between the devices.I'll take the slave apart and see if i can find anything inside

author: Chet T16
author_rank: Frequent Contributor
timestamp: February 25, 2013, 04:23:26 pm
content: Inside the slave is some Sagem ic (AFRHEUT0200 HD6433834822H) that isn't throwing up any results on google and a few other bits - voltage reg, inverter, discretes etc

author: richcj10
author_rank: Unknown
timestamp: February 25, 2013, 07:29:42 pm
content: Is this in a car?Could it be CAN?

author: Chet T16
author_rank: Frequent Contributor
timestamp: February 25, 2013, 07:36:53 pm
content: Yes, its in a car. It is the radio (master) and the display/wheel controls(slave). I tried doing this on my old car which had a very similar setup (3 wires, 2 of which followed the i2c protocol)The car is a 2002, it has a primitive CAN bus but the radio setup is isolated. The later versions of my car did switch to CAN for the radio.

author: richcj10
author_rank: Unknown
timestamp: March 01, 2013, 07:08:12 am
content: It is probably CAN. It may not be at CAN voltages. That means it is not using transceivers. Do you have a scope? Logic analyzer?

author: AndreasF
author_rank: Frequent Contributor
timestamp: March 01, 2013, 07:20:32 am
content: Quote from: Chet T16 on February 25, 2013, 01:47:09 amThat doesn't really sound like 'slave' behaviour (unless I misunderstood your description). You can have multiple masters in an I2C bus, if what you have is indeed an I2C protocol.

author: Chet T16
author_rank: Frequent Contributor
timestamp: March 01, 2013, 10:24:04 am
content: Quote from: richcj10 on March 01, 2013, 07:08:12 amYes, both. 5v logic levels and the logic analyser decodes it as i2c. Writing works fine using a standard i2c library (only modified to use the 7kHz clock)

author: Chet T16
author_rank: Frequent Contributor
timestamp: March 01, 2013, 10:31:36 am
content: Quote from: AndreasF on March 01, 2013, 07:20:32 amWhen i posted originally i wasn't aware that it was the slave (i'll stick with that term for the minute!) that was pulling the third line low. It does appear the the reads are initiated from the master following the i2c protocol but the timing of the reads is determined by the slave.The thing i find unusual is the varying number of bytes sent from the slave as it was my understanding that the master determined the amount read whereas here it would have no prior knowledge of whether it had five bytes to read. I don't think this will matter for me reading button presses anyway but i am curious.

author: AndreasF
author_rank: Frequent Contributor
timestamp: March 01, 2013, 10:44:19 am
content: Well, the first byte that's being sent (by the slave) seems to indicate the number of bytes that are available to read (from the slave), so all the master needs to do is send the slave address in read mode, wait for the first byte from the slave, and continue to read according to this first byte.

author: Chet T16
author_rank: Frequent Contributor
timestamp: March 01, 2013, 11:05:49 am
content: Having looked into the library I think i see where I've gotten mixed up. With the arduino you start a read specifying the address and number of bytes. These are then read from the buffer. I was mistaken in thinking the master sent the number of bytes to the slave.Yes the first byte returned will give me an indication of whether I have a button press to read.

author: free_electron
author_rank: Super Contributor
timestamp: March 01, 2013, 10:36:19 pm
content: if they use 3 wire it could be C-Bus... that is almost i2c...

author: ve7xen
author_rank: Super Contributor
timestamp: March 02, 2013, 06:16:19 am
content: Is this maybe an (ab)use ofI2C clock stretching? The master could be initiating the read immediately after having received some data, with the slave stretching the clock until an event happens, when it sends the requested data to the master. This feature is designed to allow the slave to throttle commands if the master is sending them too quickly for it to handle, but I suppose it could be used as a crude form of interrupt as well.I'm not sure if the AVR TWI peripheral supports clock stretching (check the manual, this is an oft-omitted feature), but I'd bet the Arduino bit-banged library does not. I believe Peter Fleury's bitbanged I2C library does, however.

author: amyk
author_rank: Super Contributor
timestamp: March 02, 2013, 07:15:59 am
content: Quote from: Chet T16 on February 25, 2013, 04:23:26 pmI think you misread the part# as that is probably the HD6433834S- Hitachi (now Renesas) H8 microcontroller. Thedatasheetdoes not explicitly mention CAN or I2C but there is a UART peripheral capable of different transfer modes. Or they could be bit-banging I2C/CAN...

author: Chet T16
author_rank: Frequent Contributor
timestamp: March 02, 2013, 07:55:43 pm
content: I done some more testing, after a write the slave will pull the third line low waiting for a read. If it doesn't get one it will release the line and try twice more. If i initiate a read when its pulled low i can read the data, either the 2 or 5 bytes depending on whether the buttons are pressed. Strangely although reading them fine the arduino is is not sending an ACK for the last byte which causes the slave to try to send everything again (twice more, as above) so if i read a button press i will actually see it three times.Quote from: amyk on March 02, 2013, 07:15:59 amI didn't take the display apart again (it has one of thos super thin ribbon cable things attaching the LCD that i don't want to break) but the package seems to match up. Good spot!

author: Chet T16
author_rank: Frequent Contributor
timestamp: March 02, 2013, 10:14:24 pm
content: Some logic analyser screen grabsHere is a read with the original master and slaveAnd here is one with me as master

author: amyk
author_rank: Super Contributor
timestamp: March 03, 2013, 01:51:36 pm
content: Since this might not really be I2C you may be violating some timing requirement.

author: richcj10
author_rank: Unknown
timestamp: March 03, 2013, 08:44:18 pm
content: Some devices don't care or want to see the NAK.Other IC's really care about seeing the ACK.Try that first.

author: TerminalJack505
author_rank: Super Contributor
timestamp: March 03, 2013, 11:06:21 pm
content: The Arduino library will send a NACK when it has received the last byte. This is normal. It's supposed to tell the slave that the master is done reading.You might be able to emulate the behavior you are seeing by calling Wire.requestFrom() with aquantityparameter value that is larger than the number of bytes you actually expect.If that doesn't work AND you actually need that behavior then you might just try bit-banging the I2C bus yourself.The attached ZIP has the code from a simple little project I did with an ATtiny2313, which has no native I2C support. I'm pretty sure the code for bit-banging the I2C bus was taken pretty much as-is from an Atmel app note. I likely just wrapped their code in a C++ class.

author: Chet T16
author_rank: Frequent Contributor
timestamp: March 04, 2013, 12:02:35 am
content: Ok well that makes perfect sense then. I will try requesting 6 bytes (max is normally 5) and see if that satisfies the slave. Aside from it causing the slave to reattempt a send (i.e. pull the line low to start a read) there is no negative affect on what i'm trying to do. Once i have completed a read i can ignore the others in code or pull it low myself which should stop it.At this point i don't see a need to bit-bang. There is a couple of software i2c libraries about - i have been able to write to the display using an Attiny85

author: man-x86
author_rank: Newbie
timestamp: February 12, 2018, 08:44:59 pm
content: It seems that I'm very late on this topic, but there's not much information available on this kind of hardware. I'd like to thankChet T16for showing that it is possible to write some user text on the LCD.I also have aTuner Listradio, and it required some reverse engineering to understand how the display works:https://imgur.com/gallery/AlMldAnd here's an obvious proof that it worked:I still have to figure out how to read the data from buttons under the steering wheel, but that shouldn't be too hard once the this step has been made.

author: Chet T16
author_rank: Frequent Contributor
timestamp: February 12, 2018, 09:01:04 pm
content: Quote from: man-x86 on February 12, 2018, 08:44:59 pmGood to see this still doing the rounds.From what I recall to read the buttons you must do an i2c read from the display and the first byte tells you if there are any further bytes to be read. If it's non zero the following bytes tell you what button is pressed. Using the arduino ide you must send the number of bytes to read first but as the length varies I sent a fixed value, 5 I think, and discarded the data if the first byte was zero.I may still have access to my old notesSent from my SM-G925F using Tapatalk

author: Chet T16
author_rank: Frequent Contributor
timestamp: February 12, 2018, 09:02:58 pm
content: Actually I see I covered some of this above now that I reread!Sent from my SM-G925F using Tapatalk

author: Chet T16
author_rank: Frequent Contributor
timestamp: February 12, 2018, 09:08:22 pm
content: From my notes:Code:Volume UpPress:0x470x010x100x490x100x000x06Volume UpHold:0x470x010x100x490x100x000x86Volume DownPress:0x470x010x100x490x100x000x08Volume DownHold:0x470x010x100x490x100x000x88PausePress:0x470x010x100x490x100x000x0APauseHold:0x470x010x100x490x100x010x0ASource RightPress:0x470x010x100x490x100x000x02Source RightHold:0x470x010x100x490x100x010x02Source LeftPress:0x470x010x100x490x100x000x04Source LeftHold:0x470x010x100x490x100x010x04Bottom ButtonPress:0x470x010x100x490x100x000x00Bottom ButtonHold:0x470x010x100x490x100x010x00Scroll Up:0x470x010x100x490x100x040x82Scroll Down:0x470x010x100x490x100x040x02

author: man-x86
author_rank: Newbie
timestamp: February 12, 2018, 09:15:54 pm
content: Quote from: Chet T16 on February 12, 2018, 09:01:04 pmI think I'll figure out by myself during the next rainy weekend, reverse engineering is still plenty of fun.I see that you also used the Arduino IDE. Did you also had to tweak the Wire library (TWBR = 0xff and TWSR = 0x01) for the unusually slow bitrate?Quote from: Chet T16 on February 12, 2018, 09:08:22 pmWow, thanks a lot.

author: Chet T16
author_rank: Frequent Contributor
timestamp: February 12, 2018, 09:27:45 pm
content: For some reason I copied the wire library and made a new version to suit the 7kHz speed, not sure why i done that!I just found this in a text file called notes.txt, may be of use to youCode:When idle - radio writes 0x01 0x11Reads controls - if no input it receives 0x01 0x01

author: man-x86
author_rank: Newbie
timestamp: February 12, 2018, 09:49:27 pm
content: I fixed it in a terribly ugly way for the 1st tests, by calling a function after every call to the Wire library (at least every call that I suspect to overwrite those registers).void conf() {TWBR = 0xff;TWSR = 0x01;//TWCR &= ~(1<<TWEA); // ignore ACK, bad idea}I already noticed the constant {0x01, 0x11} writes and {0x01, 0x01} reads, but thanks for adding that.

author: man-x86
author_rank: Newbie
timestamp: February 25, 2018, 05:58:44 pm
content: Interesting, I noticed different values for the controls on my car:Code:* Source R: 0x04, 0x82, 0x91, 0x00, 0x01 (press)* Source R: 0x04, 0x82, 0x91, 0x00, 0x81 (hold), but sending 0x41 works as well* Source L: 0x04, 0x82, 0x91, 0x00, 0x02 (press)* Source L: 0x04, 0x82, 0x91, 0x00, 0x82 (hold), but sending 0x42 works as well** Volume +: 0x04, 0x82, 0x91, 0x00, 0x03 (press)* Volume +: 0x04, 0x82, 0x91, 0x00, 0x43 (hold)* Volume -: 0x04, 0x82, 0x91, 0x00, 0x04 (press)* Volume -: 0x04, 0x82, 0x91, 0x00, 0x44 (hold)** Pause : 0x04, 0x82, 0x91, 0x00, 0x05 (press) apparently, no hold feature* OK : 0x04, 0x82, 0x91, 0x00, 0x00 (press)* OK : 0x04, 0x82, 0x91, 0x00, 0x40 (hold)** Wheel UP: 0x04, 0x82, 0x91, 0x01, 0x41* Wheel DN: 0x04, 0x82, 0x91, 0x01, 0x01I didn't check if the values from your car work on my radio, but I assume it would be compatible.I can manage to capture the keypresses with an MCU although it's a bit messy (it's not the goal anyways).My final goal is replace the display by a raspi (media center + navigation), and keep the original steering wheel buttons to control the raspi and the original radio (only the volume/pause + forcing the source on AUX).I managed to go a bit further by controlling the radio from the MCU, but now I got most of the things working, I'll definitely have to rewrite the code properly (I'm adding nops to time properly the MRQ line).I noticed that some other cars (Renault Laguna 1, Scenic 1) were sold with an early GPS navigation system (Carminat). Thankfully, they reused the same layout on the other cars of that era (Megane 1, perhaps the Clio 2 as well), even though there was no option for a factory navigation system. So that gives plenty of room under the console/dashboard for a raspi + a 5" touchscreen to fit.

author: bson
author_rank: Unknown
timestamp: February 26, 2018, 08:58:19 am
content: Quote from: Chet T16 on February 25, 2013, 04:23:26 pmThis is probably some variant of the Renesas H8 series uC.

author: ptijoss
author_rank: Newbie
timestamp: June 03, 2019, 08:04:42 am
content: hi everybody,i have a problem ! I have read your topic but it's impossible for me to send a frames for radio.However I did everything that you have say !!please HELP !!!