cancel
Showing results for 
Search instead for 
Did you mean: 

How can we make an I2C interface with a sensor for STM32L412RB using baremetal programming.

SSaiy.1
Associate III

I was trying to do baremetal programming and using L412 controller. I am facing trouble with I2C.

6 REPLIES 6
Ghofrane GSOURI
ST Employee

Hello @SSaiy.1​ 

First let me thank you for posting .

To interface with an I2C sensor using the STM32L412RB microcontroller in a bare metal (no operating system) environment, you will need to follow these steps:

  1. Configure the I2C peripheral:
  • Enable the I2C clock in the RCC (Reset and Clock Control) register.
  • Configure the I2C pins (SDA and SCL) as alternate function mode in the GPIO (General Purpose Input/Output) registers.
  • Set up the I2C peripheral registers, including the control, status, and data registers, to configure the bus speed, addressing mode, and other parameters.

2.Initialize the I2C peripheral:

  • Set the I2C mode to master or slave.
  • Enable the I2C peripheral by setting the appropriate bit in the control register.
  • Set the bus speed and addressing mode in the control register.

3. Communicate with the sensor:

  • To read from the sensor, you will need to send a read command followed by the sensor's address. The sensor will then send back the requested data.
  • To write to the sensor, you will need to send a write command followed by the sensor's address and the data to be written.

Here is some sample code that demonstrates how to configure and use the I2C peripheral to read data from a sensor:

#define I2Cx_CLOCK_SPEED                   100000
 
// I2C peripheral configuration
void I2C_Init(void)
{
  // Enable I2C clock
  RCC->APB1ENR1 |= RCC_APB1ENR1_I2C1EN;
 
  // Configure I2C pins as alternate function
  GPIOB->MODER = (GPIOB->MODER & ~(GPIO_MODER_MODE8 | GPIO_MODER_MODE9)) | (GPIO_MODER_MODE8_1 | GPIO_MODER_MODE9_1);
  GPIOB->AFR[0] = (GPIOB->AFR[0] & ~(GPIO_AFRL_AFSEL8 | GPIO_AFRL_AFSEL9)) | (1 << (8 * 4)) | (1 << (9 * 4));
 
  // Configure I2C peripheral
  I2C1->CR1 &= ~I2C_CR1_PE; // Disable I2C
  I2C1->TIMINGR = (0x1 << I2C_TIMINGR_PRESC_Pos) | (0x13 << I2C_TIMINGR_SCLL_Pos) | (0xF << I2C_TIMINGR_SCLH_Pos) | (0x2 << I2C_TIMINGR_SDADEL_Pos) | (0x4 << I2C_TIMINGR_SCLDEL_Pos); // Configure bus speed and timings
  I2C1->CR1 |= I2C_CR1_PE; // Enable I2C
}
 
// Read data from I2C sensor
uint8_t I2C_Read(uint8_t address, uint8_t reg)
{
  uint8_t data;
 
  // Send start condition
  I2C1->CR2 = (I2C1->CR2 & ~I2C_CR2_SADD)

Thx

Ghofrane

SSaiy.1
Associate III

Hi @Ghofrane​ is there any git repo with sample codes for this example

Ghofrane GSOURI
ST Employee

Hello again @SSaiy.1​ 

I'm sorry, but I do not have access to any specific Git repositories that contain sample code for interfacing with an I2C sensor using the STM32L412RB microcontroller in a bare metal environment. However, there are many resources available online that may be able to help you with this task.

Here are a few suggestions for finding sample code and other resources for bare metal programming with the STM32L412RB:

  1. STM32CubeMX: STM32CubeMX is a tool that can be used to generate code for STM32 microcontrollers. It includes a library of example projects that demonstrate how to use various peripherals, including I2C. You can use STM32CubeMX to generate a starting project for your application, and then modify the code to fit your specific needs.
  2. STM32 documentation: The STM32L4 series of microcontrollers has extensive documentation available from STMicroelectronics, including reference manuals, application notes, and user guides. These documents contain detailed information about the microcontroller's peripherals, registers, and other features, and can be a useful resource for understanding how to use the microcontroller in your application.
  3. Online forums and communities: Some of these forums also have code libraries and example projects that you can use as a starting point for your own projects.

I hope these resources are helpful. Good luck with your project!

SSaiy.1
Associate III

Hi @Ghofrane GSOURI​ Thanks for the reply. I was able to initialise I2C. Could you please upload a complete read and write function examples for this controller. It would be very helpful.

S.Ma
Principal

I think ST should provide example in bare metal, for example reas write i2c eeprom memory....

SSaiy.1
Associate III

We have so much example for HAL, ST should come with baremetal example as well. Specifically for L4 series not even a youtube video tutorial is available in baremetal