2022-12-30 4:17 AM
I was trying to do baremetal programming and using L412 controller. I am facing trouble with I2C.
2022-12-30 5:13 AM
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:
2.Initialize the I2C peripheral:
3. Communicate with the sensor:
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
To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
2022-12-30 5:42 AM
Hi @Ghofrane is there any git repo with sample codes for this example
2022-12-30 5:51 AM
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:
I hope these resources are helpful. Good luck with your project!
To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
2022-12-30 9:34 AM
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.
2022-12-31 12:22 AM
I think ST should provide example in bare metal, for example reas write i2c eeprom memory....
2023-01-03 2:44 AM
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