7 years ago.

STM32f3348-disco I2C strange behaviour

Hello,

As i mentioned in the title I have the stm32f3348-disco board. I am currently working on a small project and I want to implement some I2C devices into it as well( Light sensor, LCD display, RTC)/media/uploads/alain_kovacs2007/wp_20170428_22_03_19_rich_li.jpg I have used I2C before on Arduino without any problems, and all the devices that I want to use now have been tested and used on arduino so I know their addresses and that they are working. First, I have tried to "scan" them in an I2C scanner. I have found a few programs but they for some unknown reason found devices on all the addresses... From the 3 boards I have only managed to make the light sensor work... The RTC works quite strange whereas I can not get the LCD working at all. I am using libraries found on mbed and i have tried a few for each board, I have pull-up resistors, I have tried to change the I2C port but nothing seems to work.. Can anybody help me? Thanks I'm sorry if I didn't post this where I should have but I am quite new to this forum..

1 Answer

7 years ago.

Hi. Work only with the LCD display for now. Get it working and then move to the next I2C device, etc. Recommend to keep the I2C speed down (ie. 100khz sounds good). Study this example and use the display parameter that is suitable for your LCD module:

https://developer.mbed.org/questions/75963/Connecting-a-2004-LCD-via-I2C/

Post your results.

--

Please post your code and apply the <<code>> your code <</code>> tags for proper formatting.

Another thought is to use an older mbed library that is still supportive of your target Nucleo board. No reason to do this other than some developers have raised assorted issues and often the remedy is to roll back the mbed library. In theory, if your I2C scanner can ping one I2C device then your LCD display should also function.

So your I2C scanner code is one of the following ?

https://developer.mbed.org/users/Dominosus/code/I2C_scanner/

https://developer.mbed.org/users/netwing/code/i2c-scanner/

I have tried all those beforehand, all possible combinations but still nothing. I have 2 STM32f boards, tried the same with both but anyways, I know that they are working. Then the devices themselves are working as well. The problem I think is somewhere in the middle, meaning the IC address(I think). For example, my display is not flickering or basically not doing anything when i try to write something to it.. and as I said, I have tried almost all the combinations. I have even tried another I2C port(the board has 3 of them). Nothing seems to work. It's like the I2C port is not "started up" or something..I don't have access to a scope or a bus pirate or something like that since I am at home now but I will go to the university on monday and i will try looking at what exactly is happening there.. As about the speed, I have tried 100kHz and 400kHz... Another strange thing is that when i scan the I2C channel for the devices with an I2C scanner, it gives me some strange addresses which are consistent for example it gives me d0 and d1 for the light sensor but it's address is 0x23(it only has one address)...It does not "find" any address for the display driver though..

posted by Alain Kovacs 29 Apr 2017

The issue with I2C addresses has been explained many times before. The mbed libs use the 8bit address convention like it will be seen on the I2C SDA line, while arduino libs use the 7bit address format that is often given in the datasheets. So your arduino 0x23 address should be converted to 0x46 in the mbed code. Just leftshift by one position. Note that an mbed I2C address will never have bit0 set. That bit is reserved for indicating a read or write operation. I dont know which I2C scanner you have used, but it shows a device responding to both the read and write ping. The I2C LCD expander you show in the picture should be at 0x40 since it has a pcf8574 and 3 mounted solderbridges. The address could possibly also be at 0x4E.

posted by Wim Huiskamp 29 Apr 2017

Here is the code that i'm using. Yes, the address of the LCD expander is 0x20(read by arduino) which bitshifted is 40. I haved tried this and also 4E but still nothing appears on the LCD. I have also changed the port expander in the TextLCD_Config.h to a few of them but still nothing..

/* Hello World! for the TextLCD Enhanced Library*/

#include "mbed.h"
#include "TextLCD.h"
 
// Host PC Communication channels
Serial pc(USBTX, USBRX); // tx, rx

// I2C Communication
I2C i2c_lcd(I2C_SDA, I2C_SCL); // SDA, SCL
//I2C i2c_lcd(PB_8,PB_9); // SDA, SCL
 

// LCD instantiation 
TextLCD_I2C lcd(&i2c_lcd, 0x4E, TextLCD::LCD20x4);                  // I2C exp: I2C bus, PCF8574 Slaveaddress, LCD Type
 
int main() {   
  Timer t;

  pc.printf("TextLCD Enhanced Test. Columns=%d, Rows=%d\n\r", lcd.columns(), lcd.rows());
    
  for (int row=0; row<lcd.rows(); row++) {
    int col=0;
      
    pc.printf("MemAddr(Col=%d, Row=%d)=0x%02X\n\r", col, row, lcd.getAddress(col, row));      
//    lcd.putc('-');
    lcd.putc('0' + row);      
      
    for (col=1; col<lcd.columns()-1; col++) {    
      lcd.putc('*');
    }
 
    pc.printf("MemAddr(Col=%d, Row=%d)=0x%02X\n\r", col, row, lcd.getAddress(col, row));      
    lcd.putc('+');       
  }    

// Fill screen again and time it
  t.start();

  for (int row=0; row<lcd.rows(); row++) {
    int col=0;
      
    lcd.putc('0' + row);      
      
    for (col=1; col<lcd.columns()-1; col++) {    
      lcd.putc('*');
    }
 
    lcd.putc('+');       
  }    
  t.stop();    
  pc.printf("All my hard work took %f sec\r\n", t.read());          

// Show cursor as blinking character
   lcd.setCursor(TextLCD::CurOff_BlkOn);
 
// Set and show user defined characters. A maximum of 8 UDCs are supported by the HD44780.
// They are defined by a 5x7 bitpattern. 
  lcd.printf("hi");
  lcd.setUDC(0, (char *) udc_0);  // Show |>
  lcd.putc(0);    
  lcd.setUDC(1, (char *) udc_1);  // Show <|
  lcd.putc(1);    
    
  pc.printf("Bye now\r\n");          
}

About the I2C scanner I am using this one https://developer.mbed.org/teams/V8/code/I2C_Adress_Scanner/ which is the only one that I found that at least gives me some kind of output. I have also tried to use this one https://developer.mbed.org/users/netwing/code/i2c-scanner/ but this simply outputs all the possible adresses with or without something connected and that's all..

I have just tested the two I2C scanners proposed by you and both of them give almost the same type of "response" which you can see in the picture below.. As I said before, it just "finds" all the addresses and that's all.. /media/uploads/alain_kovacs2007/capture.png

posted by Alain Kovacs 30 Apr 2017

What are the details of your LCD display? Assuming the LCD display is a 20x4 since that is the parameter you are passing to the mbed routines? Post the model of your LCD display. Since the same setup with the LCD expander and LCD is working with your Arduino, that is a good start to have working hardware.

Also, be careful with your I2C port pin defining parameters:

I2C i2c_lcd(I2C_SDA, I2C_SCL); // SDA, SCL - looks ok
//I2C i2c_lcd(PB_9,PB_8); // SDA, SCL - first is the SDA pin and then the SCL (this is reversed in your code post)

--

If you are not receiving a response (ping / ACK) from the I2C LCD expander then the focus should be on the port expander (nothing yet to do with the LCD display which will follow once the expander can be detected). Be sure you have the require pull-ups on the I2C port pins (unless these are supplied by the I2C LCD expander) - a value of 2.2k-10k should be fine to 3v3. STM32 processor I2C port pins which are defined as OD (open drain) are 5 volt tolerant so even a pull-up to 5v should be fine.

As noted by Wim here, what voltage are you using to power the I2C LCD port expander ? Check your wiring between mbed and the LCD module.

https://developer.mbed.org/questions/68849/My-module-has-address-0x27-How-I-can-con/

Quote:

Which LCD module type (and controller) are you using? Are you using 5V or 3V3 supply (note that some controllers dont work on 3V3 supply voltage. The I2C will however work on 3V3.

see here:

https://developer.mbed.org/users/mbed_official/code/mbed-dev/file/default/targets/TARGET_STM/TARGET_STM32F3/TARGET_STM32F334x8/TARGET_DISCO_F334C8/PinNames.h

-

see here:

https://developer.mbed.org/questions/75963/Connecting-a-2004-LCD-via-I2C/

Do you have the port expander table as noted above ? Consider to right mouse click on the LCD library and select update.

Quote:

Try rightclick on the TextLCD lib in your project and select ''update'' to get the most recent one. Then modify the list to enable LCM1602 and disable all others. The TextLCD_Config.h file should be revision v09.

--

Just found this which may be also your problem:

https://developer.mbed.org/questions/77809/Anyone-got-the-STM32F103RB-working-with-/

Perhaps you should try the same and consider to use the mbed library 132.

Quote:

The problem seems to be introduced in the updated I2C libs for the STM devices. I noticed a failure on the first write operation after a reset. There are also issues with read operations. I have raised a bug report on github.

The lib that you use for the PCF8574 detects the write error and then blocks all further communications.

A quick fix could be to replace mbed rev 140 in your program and select revision 132 instead. That version still seems to work OK. Select the mbed lib in your program filetree, then select revisions in the window on the right hand side and select revisions, switch to the desired revision and recompile

-

On the RTC hookup, you noted earlier that all of your devices have pull-up resistors. This should not be required. That is, only a single device (ideally the I2C master = mbed board) should have local pull-up resistors to +3v3 or even +5v. The value of the I2C pull-ups is not critical but recommend 2k2 to 10k range. Respectively, remove the pull-ups from the other I2C slave devices. Need to watch you I2C bus speed (100khz is ok) and also you wiring lengths. Then test again. If you do not have one already, consider to pickup a low cost I2C bus analyzer which will serve you well to review the I2C bus traffic.

For example:

http://www.ebay.com/itm/24MHz-8CH-USB-Logic-Analyzer-24MHz-8-Channel-Compatible-to-Saleae-ARM-FPGA-M100-/272337085235?hash=item3f688e2333:g:RNcAAOSwPCVYBD2b

or better yet:

http://www.ebay.com/itm/New-Beagle-I2C-SPI-Protocol-Analyzer-/222495861210?hash=item33cdc969da:g:xZYAAOSw~OVWymki

We have both and others in the lab and they are very valuable for such I2C debugging.

-

Just checked the datasheet for the RTC by Maxim. The MAX speed is 100khz for this I2C slave device. Consider to slow this down to 10khz and try again. Post your results.

    // Set the frequency to 10kHz @ line 19-20
    m_rtc->frequency(10000);

-

See my comments above. The MAX speed for this RTC IC by Maxim is 100 khz. Slow it down to a lower value and test again to see if the results are more solid.

From your posted photos, it appear your RTC module is this one ?

http://www.dx.com/p/i2c-rtc-ds1307-24c32-real-time-clock-module-for-arduino-blue-149493

If yes then you already have onboard 3k3 pull-up on the I2C lines so do not place any additional pull-up resistors onto the SDA nor onto the SCL lines. Also, this module is required to be powered from 5v0 which is fine as the STM Nucleo I2C lines are 5v0 tolerant so it is safe to use. See that there is an onboard I2C memory device on this module so that can allow for non-volatile storage of values when you are ready and have perfected this I2C interfacing.

posted by Sanjiv Bhatia 30 Apr 2017

Yes, you are right about the code.. That is from yesterday when I was testing everything and I actually did that on purpose in case that was the problem so I just swapped the pins, who knows, right?

As about my hardware I have 2 LCD expanders actually. 1. 16x2 Display (1602A model) which has attached an LCD expander with the PCF8574AT chip and address is 0x3F(read by arduino) 2. 20x4 Display (QC2004A model) and LCD expander PCF8574T and chip address set to 0x20(also read by arduino

Both of them work on arduino without any problems with or without the LCD expander.

Here is the picture of both of them /media/uploads/alain_kovacs2007/wp_20170430_17_11_08_rich_li.jpg

Everything is connected to 5V from the STM board so I don't think that's the problem.. Though, I have tested them with 3v3 on arduino and they did work as well.

I am also using 2x10k pull-up resistors but all the boards that I have(RTC, LCD expander and Light sensor) have pull-ups as well..

posted by Alain Kovacs 30 Apr 2017

So, I actually got lucky yesterday evening, just didn't have time to update you!! :D

Let's sum up with the LCD expander. My libraries were updated and ok, the same with the config file(v9). Everything was set-up correctly but the only problem were the I2c pins. I was using I2C_SDA and I2C_SCL which also have some LED's attached on the board and I think that this was the problem. I switched to PA_14(SDA), PA_15(SCL) and it just worked.. The thing that also done was to disconnect the LCD from the vcc after every code upload to the board because i read somewhere that the controller or the LCD might be stuck because of a bad command.

Now the only thing that I have to figure out is the RTC which again acts strange..I have the correct address(D0) but it doesn't look like working, read or write.. Though I didn't have much time to play with it.. I will probably do that today or tomorrow. I will keep you updated!

Thank you for your help! Keep up the good work ;)

posted by Alain Kovacs 01 May 2017

So, yet again I have some problems with the Tiny RTC. I am using the code down below and this library https://developer.mbed.org/users/leihen/code/RTC-DS1307/ It doesn't work.. What I mean by not working? I upload the code, I get the menu but it doesn't always accept commands, sometimes I have to "send" a command a few times. When I press read for example i get something like this: "Performing read operation The current time is : 32:157:85 The current date is : Saturday, 124/157/2165" If i type write, it asks for the date and time(doesn't accept commands always) and when I finally input something it gives me another menu and when I try to read it it gives me another strange time and date like the one up. The thing is that that time and date is somewhat random.. I have activated the debug file but I only get info's like "Successfully read 7 registers from RTC" "Writing back start/stop register value" "Start/stop register value successfully written"

Yet again, I have tested it with arduino and everything works...The address read by arduino is 0x68, which bitshifted is 0xD0, exactly like in the code.. Am I missing something?

L.E. I can not post the whole code since it says it's too long but here's the link to the program that I am using, of course, the I2C pins are Rtc_Ds1307 rtc(PA_14, PA_15);

https://developer.mbed.org/users/leihen/code/Rtc_Ds1307_Sample/

posted by Alain Kovacs 02 May 2017

Anybody? anything?

posted by Alain Kovacs 07 May 2017