LCD i2c for STM32 with ArduinoIDE.

Post Reply
Fabaum
Posts: 24
Joined: Tue Mar 17, 2020 4:10 pm

LCD i2c for STM32 with ArduinoIDE.

Post by Fabaum »

Hello friends!

I've been doing some experiments, trying to compile a simple i2c communication program with an LCD display using ArduinoIDE to work on the STM32L476RG. Again, nothing is so simple ...
In the process of compiling the program, numerous errors arise.
Researching a little more, I discovered a good example of a program for STM32, but made with STM32CubeIDE: https://controllerstech.com/i2c-lcd-in-stm32/
The author kindly provided not only the program for download but also made an explanatory video.
I had to make some minor changes, such as changing the address for sending i2c commands to the LCD display model JHD204A. As I am using a Robotdyn LCD i2c serial module, I used the PCA8574A chip datasheet for information on the addressing of the commands. So, in the "i2c-lcd.c" file, I had to change the slave address to 0x70h. Worked perfectly.
After that, I tried in many different ways to change the program to work with ArduinoIDE, but without success. I tried to reproduce the logic contained in "main.c" of the program made in STM32CubeIDE but using ArduinoIDE and including all libraries, but the errors generated are countless! With each change I make to correct them, the situation seems to get worse.
I uploaded the files, in case anyone wants to check: http://tbf.me/a/CiQu9
Could someone help me with this please? How do I make this work on ArduinoIDE?
Attachments
httprobotdyn.compubmedia0g-00004759mod-i2c-lcdphotophototop0g-00004759mod-i2c-lcd-small.jpg
httprobotdyn.compubmedia0g-00004759mod-i2c-lcdphotophototop0g-00004759mod-i2c-lcd-small.jpg (92.26 KiB) Viewed 17101 times
httprobotdyn.compubmedia0g-00004759mod-i2c-lcdphotophotobottom0g-00004759mod-i2c-lcd-small.jpg
httprobotdyn.compubmedia0g-00004759mod-i2c-lcdphotophotobottom0g-00004759mod-i2c-lcd-small.jpg (90.55 KiB) Viewed 17101 times
User avatar
Juraj
Posts: 47
Joined: Fri Jan 03, 2020 7:47 pm
Answers: 1
Location: Slovakia
Contact:

Re: LCD i2c for STM32 with ArduinoIDE.

Post by Juraj »

use an Arduino library. they use the Wire library which has the same API on all platforms
Fabaum
Posts: 24
Joined: Tue Mar 17, 2020 4:10 pm

Re: LCD i2c for STM32 with ArduinoIDE.

Post by Fabaum »

The Juraj,

Did you try to compile on your ArduinoIDE the program "Injetor_v.09" that I sent to http://tbf.me/a/CiQu9 for the STM32L476RG board?
I had tried this before, as I said, and there were numerous errors on the dependencies.
After that I tried to avoid using libraries, putting all the information in the main program and solving the syntax, but with each step more problems arose.
See the error messages I get after simply trying to compile the original program for the STM32L476RG board:

**********************************

Arduino: 1.8.12 (Windows 10), Board: "Nucleo-64, Nucleo L476RG, Mass Storage, Enabled (generic 'Serial'), None, Low/Full Speed, Smallest (-Os default), Newlib Nano (default)"

In file included from C:\Users\Fabio\AppData\Local\Arduino15\packages\STM32\hardware\stm32\1.8.0\system/Drivers/CMSIS/Device/ST/STM32L4xx/Include/stm32l4xx.h:148,
from C:\Users\Fabio\AppData\Local\Arduino15\packages\STM32\hardware\stm32\1.8.0\cores\arduino/stm32/stm32_def.h:48,
from C:\Users\Fabio\AppData\Local\Arduino15\packages\STM32\hardware\stm32\1.8.0\cores\arduino/stm32/clock.h:43,
from C:\Users\Fabio\AppData\Local\Arduino15\packages\STM32\hardware\stm32\1.8.0\cores\arduino/wiring_time.h:23,
from C:\Users\Fabio\AppData\Local\Arduino15\packages\STM32\hardware\stm32\1.8.0\cores\arduino/wiring.h:38,
from C:\Users\Fabio\AppData\Local\Arduino15\packages\STM32\hardware\stm32\1.8.0\cores\arduino/Arduino.h:32,
from sketch\Injetor_v.09.ino.cpp:1:
C:\Users\Fabio\AppData\Local\Arduino15\packages\STM32\hardware\stm32\1.8.0\system/Drivers/CMSIS/Device/ST/STM32L4xx/Include/stm32l476xx.h:1318:29: error: expected identifier before '(' token
1318 | #define LCD ((LCD_TypeDef *) LCD_BASE)
| ^
C:\Users\Fabio\Documents\Arduino\libraries\NewLiquidCrystal_lib/LCD.h:199:7: note: in expansion of macro 'LCD'
199 | class LCD : public Print
| ^~~
C:\Users\Fabio\AppData\Local\Arduino15\packages\STM32\hardware\stm32\1.8.0\system/Drivers/CMSIS/Device/ST/STM32L4xx/Include/stm32l476xx.h:1318:43: error: expected ')' before '*' token
1318 | #define LCD ((LCD_TypeDef *) LCD_BASE)
| ~ ^
C:\Users\Fabio\Documents\Arduino\libraries\NewLiquidCrystal_lib/LCD.h:199:7: note: in expansion of macro 'LCD'
199 | class LCD : public Print
| ^~~
C:\Users\Fabio\AppData\Local\Arduino15\packages\STM32\hardware\stm32\1.8.0\system/Drivers/CMSIS/Device/ST/STM32L4xx/Include/stm32l476xx.h:1318:43: error: expected ')' before '*' token
1318 | #define LCD ((LCD_TypeDef *) LCD_BASE)
| ~ ^
C:\Users\Fabio\Documents\Arduino\libraries\NewLiquidCrystal_lib/LCD.h:199:7: note: in expansion of macro 'LCD'
199 | class LCD : public Print
| ^~~
C:\Users\Fabio\AppData\Local\Arduino15\packages\STM32\hardware\stm32\1.8.0\system/Drivers/CMSIS/Device/ST/STM32L4xx/Include/stm32l476xx.h:1318:29: error: expected class-name before '(' token
1318 | #define LCD ((LCD_TypeDef *) LCD_BASE)
| ^
C:\Users\Fabio\Documents\Arduino\libraries\NewLiquidCrystal_lib/LiquidCrystal_I2C.h:50:34: note: in expansion of macro 'LCD'
50 | class LiquidCrystal_I2C : public LCD
| ^~~
C:\Users\Fabio\AppData\Local\Arduino15\packages\STM32\hardware\stm32\1.8.0\system/Drivers/CMSIS/Device/ST/STM32L4xx/Include/stm32l476xx.h:1318:29: error: expected '{' before '(' token
1318 | #define LCD ((LCD_TypeDef *) LCD_BASE)
| ^
C:\Users\Fabio\Documents\Arduino\libraries\NewLiquidCrystal_lib/LiquidCrystal_I2C.h:50:34: note: in expansion of macro 'LCD'
50 | class LiquidCrystal_I2C : public LCD
| ^~~
C:\Users\Fabio\AppData\Local\Arduino15\packages\STM32\hardware\stm32\1.8.0\system/Drivers/CMSIS/Device/ST/STM32L4xx/Include/stm32l476xx.h:1318:43: error: expected ')' before '*' token
1318 | #define LCD ((LCD_TypeDef *) LCD_BASE)
| ~ ^
C:\Users\Fabio\Documents\Arduino\libraries\NewLiquidCrystal_lib/LiquidCrystal_I2C.h:50:34: note: in expansion of macro 'LCD'
50 | class LiquidCrystal_I2C : public LCD
| ^~~
C:\Users\Fabio\AppData\Local\Arduino15\packages\STM32\hardware\stm32\1.8.0\system/Drivers/CMSIS/Device/ST/STM32L4xx/Include/stm32l476xx.h:1318:43: error: expected ')' before '*' token
1318 | #define LCD ((LCD_TypeDef *) LCD_BASE)
| ~ ^
C:\Users\Fabio\Documents\Arduino\libraries\NewLiquidCrystal_lib/LiquidCrystal_I2C.h:50:34: note: in expansion of macro 'LCD'
50 | class LiquidCrystal_I2C : public LCD
| ^~~
Injetor_v.09:4:22: error: variable 'LiquidCrystal_I2C lcd' has initializer but incomplete type
4 | LiquidCrystal_I2C lcd(0x38, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address
| ^
exit status 1
variable 'LiquidCrystal_I2C lcd' has initializer but incomplete type

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

*****************************
I've tried several different things, but it feels like I'm at a dead end ...
Would any of the friends have a solution to this problem? Or an LCD i2c library that works well for AVRs and when compiled for the STM32L476RG on ArduinoIDE does not present conflicts?
Thanks for your attention...
fredbox
Posts: 125
Joined: Thu Dec 19, 2019 3:05 am
Answers: 2

Re: LCD i2c for STM32 with ArduinoIDE.

Post by fredbox »

Juraj wrote: Mon Mar 23, 2020 6:07 am by Juraj » Mon Mar 23, 2020 1:07 am
use an Arduino library. they use the Wire library which has the same API on all platforms
The "hello world" example from https://github.com/johnrickman/LiquidCrystal_I2C compiles without error for the STM32L476RG board.

Code: Select all

Sketch uses 14116 bytes (1%) of program storage space. Maximum is 1048576 bytes.
Global variables use 824 bytes (0%) of dynamic memory, leaving 97480 bytes for local variables. Maximum is 98304 bytes.
User avatar
fpiSTM
Posts: 1738
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: LCD i2c for STM32 with ArduinoIDE.

Post by fpiSTM »

First thing to test is to build one of the library default example.
So I've installed the latest NewLiquidCrystal v 1.5.1 (https://bitbucket.org/fmalpartida/new-l ... downloads/)
Then trying to build the the default example HelloWorld_i2c.

And get similar issue than you.
In fact the issue is STM32L476 has an LCD peripheral and it is defined in the CMSIS device:
https://raw.githubusercontent.com/stm32 ... 32l476xx.h

Code: Select all

#define LCD                 ((LCD_TypeDef *) LCD_BASE)
like the library defined a class LCD there is an issue as it try to expand the class definition with the LCD definition which is not possible ;)

This could not be fixed in the core as is is a CMSIS standard device definition.

So if you want to use this library you will have to modify it to fix that.
I've added this to LCD.h after the last #include:

Code: Select all

#ifdef LCD
#undef LCD
#endif
And it allows to build.
Fabaum
Posts: 24
Joined: Tue Mar 17, 2020 4:10 pm

Re: LCD i2c for STM32 with ArduinoIDE.

Post by Fabaum »

fpiSTM wrote: Mon Mar 23, 2020 4:55 pm So if you want to use this library you will have to modify it to fix that.
I've added this to LCD.h after the last #include:

Code: Select all

#ifdef LCD
#undef LCD
#endif
And it allows to build.
Hi fpiSTM,

The library you referred to is the same library I was already using to compile the program for Arduino Uno.
I made the modifications you suggested, and tested it with the sample program I put in TransferBigFiles:

Code: Select all

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x38, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Set the LCD I2C address

 void setup() 
{ 
    lcd.begin(20,4); 
    lcd.setCursor(3,0);
    lcd.print("Hello YouTube!");
    lcd.setCursor(8,1);
    lcd.print("****");
    lcd.setCursor(0,2);
    lcd.print("This is a demo text");
    lcd.setCursor(8,3);
    lcd.print("****");
 }
 
 void loop()
{
}
Worked perfectly! Thanks a lot for the help!
Fabaum
Posts: 24
Joined: Tue Mar 17, 2020 4:10 pm

Re: LCD i2c for STM32 with ArduinoIDE.

Post by Fabaum »

fredbox wrote: Mon Mar 23, 2020 4:54 pm The "hello world" example from https://github.com/johnrickman/LiquidCrystal_I2C compiles without error for the STM32L476RG board.

Code: Select all

Sketch uses 14116 bytes (1%) of program storage space. Maximum is 1048576 bytes.
Global variables use 824 bytes (0%) of dynamic memory, leaving 97480 bytes for local variables. Maximum is 98304 bytes.
This worked as well, with a litlle modification of the adress of the LCD to 0x38, but some error occurs during the compilation.
These are the messages:

WARNING: library LiquidCrystal_I2C-master claims to run on avr architecture(s) and may be incompatible with your current board which runs on stm32 architecture(s).
Sketch uses 15460 bytes (1%) of program storage space. Maximum is 1048576 bytes.
Global variables use 848 bytes (0%) of dynamic memory, leaving 97456 bytes for local variables. Maximum is 98304 bytes.
1 arquivo(s) copiado(s)
Upload complete on NODE_L476RG (F:)

Thank you for your help!
Attachments
Erro.png
Erro.png (86.29 KiB) Viewed 17048 times
User avatar
fpiSTM
Posts: 1738
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: LCD i2c for STM32 with ArduinoIDE.

Post by fpiSTM »

The warning it not important, it is just to warn you the library is not marked as compatible for the arch you used, in our case stm32.
Each Arduino library has a library.properties file with a "architectures=" field. This one specify which arch the library support.

See https://arduino.github.io/arduino-cli/l ... ile-format

In your case:
https://github.com/johnrickman/LiquidCr ... perties#L9

If you want avoid this warning add ",stm32" after avr
Fabaum
Posts: 24
Joined: Tue Mar 17, 2020 4:10 pm

Re: LCD i2c for STM32 with ArduinoIDE.

Post by Fabaum »

Thanks for the clarification !!!
Post Reply

Return to “STM32L4 based boards”