Skip to main content
Visitor
Nov 4, 2026 5:50 PM UTC
Solved

nucleo-g474re trying to set up i2c bare metal, no access to cubemx only STM32CubeIDE

I am trying to understand i2c.  I wrote a simple script to initialize my i2c and I am trying to observe SDA and SCL on an oscilloscope. 

 

I have wired the 3.3V from D13 /VREFP and have it feeding two different 5.6kohm resistors. One Resistor is connected to D15 /SCL and the other is connected to D14 /SDA.  I am using the ground on D12/ marked ground and it is hooked up to the gnd on my oscilloscope.

When I probe either D15 /SCL or D14 /SDA, it shows a flat zero volts on my oscilloscope.  When I move it to the other side of the resistor it shows 3.3 V.

Shouldn’t it have D15 sitting high or showing the pulses?

Visitor
Nov 5, 2026 3:42 PM UTC

No issue with the GPIO pins, found it, it was just the wrong memory offset for AFHR, i used the AFLH one.

16 replies

Andrew Neil
Super User
Nov 4, 2026 6:10 PM UTC

Welcome to the forum

Please see How to write your question to maximize your chances to find a solution for best results.

 

I am trying to understand i2c. 

What do you mean by that ?

  1. Understand the bus and its protocol, as defined by NXP (formerly Philips) ?
  2. Understand the I2C controller within the STM32 ?

You’ll need at least a working understanding of (1) to get to grips with (2):

NXP’s UM10204 I2C-bus specification and user manual is the definitive document defining the bus and its protocol:  https://www.nxp.com/docs/en/user-guide/UM10204.pdf

It’s a very long-established and widely-used thing - so there are plenty of learning resources; eg,

https://en.wikipedia.org/wiki/I2C

https://learn.sparkfun.com/tutorials/i2c/all

https://www.i2c-bus.org/

 

For getting to grips with (2), bare-metal register access is probably not a great place to start.

Start with one of the examples provided for your Nucleo board by ST.

 

See also Getting started with I2C in the ST Wiki:

https://wiki.st.com/stm32mcu/wiki/Getting_started_with_I2C

 

Please post a schematic of your setup - trying to describe electronic circuits in words is very difficult, and prone to errors.

Some good, clear photos of your setup would also help.

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
Visitor
Nov 4, 2026 6:52 PM UTC

I am trying to initialize the I2C and view SCL and SDA on an oscilloscope.

SDA should be high
SCL should be high or show the 100khz wave form.

Something in my code is pulling the SDA and SCL low, it seems to happen when:
 

GPIOB_MODER |= ((ALTERNATE_FUNCTION << (8 * 2)) |

(ALTERNATE_FUNCTION << (9 * 2)));


I included a photo of my test circuit.

If you know of any simple initialization functions I would love to look at it.  I didn’t see any for my board.  I do not have cubemx so I cannot use the wizard to set up the i2c.

I would love feedback on my code I attached in the previous thread to let me know if I forgot to enable something or if anything stands out.  

I can get the usart working no problem.

Andrew Neil
Super User
Nov 5, 2026 10:21 AM UTC

Something in my code is pulling the SDA and SCL low

Before assuming a code problem, have you checked the hardware?

Is there anything on the Nucleo board which could be pulling them low?

 

Can you simply toggle these pins as GPIO.

 

Again, and as others have said, start with a known-good example.

Get that working, and go from there.

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
electrical_studentAuthorBest answer
Visitor
Nov 5, 2026 3:42 PM UTC

No issue with the GPIO pins, found it, it was just the wrong memory offset for AFHR, i used the AFLH one.

Pavel A.
Nov 4, 2026 11:56 PM UTC

The I2C thingie of the STM32 must be configured properly to drive the pins. The configuration involves several steps (clocks, enables etc,)  When you write binary values to registers, you could forget some little bit.

Please use one of the ready I2C examples for your STM32 board.  These examples will be installed by CubeMX together with the HAL libraries, or you can download them from github.  Once you get an example working, modify it as you like.

 

TDK
Nov 5, 2026 12:32 AM UTC

Something in my code is pulling the SDA and SCL low, it seems to happen when:

Don’t set register to intermediate values, only write once. HAL does this correctly, perhaps look at that code for an example. Configure AF register setting before setting that pin to AF mode.

Using standard CMSIS headers will avoid errors in register settings and make your code more readable by yourself and especially others. Nobody wants to sort through random redefinitions and check each one. That is why we have CMSIS headers.

"If you feel a post has answered your question, please click ""Accept as Solution""."
Visitor II
Nov 5, 2026 2:11 AM UTC

One thing I would check is the GPIO configuration sequence. On the STM32G474RE, PB8 and PB9 are commonly used for I2C1 SCL and SDA, respectively, with AF4. Make sure the correct alternate function is selected in GPIOx_AFR before switching the pins to alternate-function mode.

Also, I2C uses open-drain outputs, so both SDA and SCL need pull-up resistors to 3.3V. Your 5.6k resistors should generally be fine for a basic test.

I would also verify that the GPIOB clock and I2C peripheral clock are enabled, the I2C timing register (TIMINGR) is configured correctly, and the I2C peripheral is enabled through CR1.

One useful test is to configure the pins as open-drain GPIO outputs first and check whether both lines stay high through the external pull-ups. Then move to alternate-function mode and check the signals again. This can help narrow down whether the issue is in GPIO configuration or the I2C peripheral initialization.

If possible, share the complete initialization code, especially the GPIO AFR, MODER, OTYPER, RCC, and I2C TIMINGR settings. That would make it much easier to spot the exact problem.

Visitor
Nov 5, 2026 3:48 PM UTC

main.c had the complete code in there.

It was just the wrong memory offset for AFHR, fixed it and it works.  I am able to generate I2C signals and observe them on my oscilloscope.

gbm
Super User
Nov 5, 2026 6:53 AM UTC

And why don’t you simply download and use CubeMX to compare your code and register values with the CubeMX-generated stuff?

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice
mƎALLEm
ST Technical Moderator
Nov 5, 2026 9:00 AM UTC

And why don’t you simply download and use CubeMX to compare your code and register values with the CubeMX-generated stuff?

Exactly. 

​@electrical_student , before going ahead with direct access to the registers, the first step is at least to validate your hardware and make something simple and functional. Then you can move on with direct access to the registers either by inspiring the register sequence call from the HAL.

To give better visibility on the answered topics, please click "Best answer" on the reply which solved your issue or answered your question.
Visitor
Nov 5, 2026 3:44 PM UTC

I cannot get cubeMX, due to a security issue or policy or whatever is IT told me.  It was a simple mistake in my memory offset for the AFHR

waclawek.jan
Super User
Nov 5, 2026 10:58 AM UTC

Something in my code is pulling the SDA and SCL low, it seems to happen when:
 

GPIOB_MODER |= ((ALTERNATE_FUNCTION << (8 * 2)) |

(ALTERNATE_FUNCTION << (9 * 2)));

 

There’s no such line in the source you’ve provided above. I see similar which is before setting AFR, which may explain why lines are pulled down at that moment; but they should be released by the time GPIO is fully set up. But it’s difficult to guess everything what happens just from sources; that’s why we have debugging facilities.

Read out and check/post content of GPIO and I2C registers.

Style: don’t write defines for hardware, use the ST-provided CMSIS-mandated device headers.

JW

Visitor
Nov 5, 2026 3:40 PM UTC

found it, it was the wrong memory address for the AFHR.

 

Got it working.  There is a security issue with cubeMX and we aren’t able to use it.  

mƎALLEm
ST Technical Moderator
Nov 5, 2026 3:44 PM UTC

There is a security issue with cubeMX and we aren’t able to use it.  

You can still use HAL without the need of CubeMx. Get one of the available examples and adapt it according to your needs:

https://github.com/STMicroelectronics/STM32CubeG4/tree/master/Projects/NUCLEO-G474RE/Examples/I2C

I’m marking your post as you provided the solution and confirmed it’s working.

To give better visibility on the answered topics, please click "Best answer" on the reply which solved your issue or answered your question.