Skip to main content
Associate
January 6, 2025
Question

STM32 HAL I2C

Good morning, I'm having trouble understanding how to solve a problem.
there is already a software developed in keil 4, but we need to change some things but now we will use keil 5 with the hal library, so far ok without problems.

my first problem was with the i2c which is ready but uses 4 eeprom 24aa1025 on my board. attached is the diagram of the board (already working but with keil 4 software without hal library).

the problem is that the programmer retired and I had to take on the job of converting the software because of a touchpanel on the “stone” which is using the HAL library,

the old code uses the command “I2C_SendData(I2C1,AddressMEM>>8);” and the new i2c library doesn't have it and I'm a bit confused about the system that has been set up.

The AddressMEM variable is working perfectly as far as the address of the bytes to be read is concerned, the problem is passing this command to the

HAL_I2C_Mem_Write(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size, uint32_t Timeout),

where it has 7 parameters, or the command to read the bytes as well.

Attached are the keil 4 i2c.c and i2c.h files.

 

Could someone help me?

This topic has been closed for replies.

8 replies

Andrew Neil
Super User
January 6, 2025

@Juniorkgtech wrote:

The AddressMEM variable is working perfectly as far as the address of the bytes to be read is concerned


Not quite sure what you mean by that?

 

@Juniorkgtech wrote:

the problem is passing this command to the

HAL_I2C_Mem_Write(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size, uint32_t Timeout),

where it has 7 parameters, or the command to read the bytes as well.


What, exactly, is the problem here?

See the documentation of the HAL_I2C_Mem_Write function - it's in the source code, and there will also be a PDF version for the particular STM32 family you're using.

I would suggest that you start by using one of the example projects, to get familiar with how the HAL I2C works ...

 

 

 

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.
Associate
January 6, 2025

and on my board I have 4 eeprom of 1024k.

and in the old software this part of the code is defined in i2c.h

#define EEPROM_MANUFACTURER_PW 131283 //(4 Bytes) Password of manufacturer

how do i read this address from the hal library?

I've been racking my brain for 2 days if you could enlighten me I'd be grateful.

 

 

Andrew Neil
Super User
January 6, 2025

It looks like your original code is based on the old Standard Peripheral Library (SPL) - yes?

Do you have a particular reason to change from that?

 


@Juniorkgtech wrote:

in the old software this part of the code is defined in i2c.h

#define EEPROM_MANUFACTURER_PW 131283 //(4 Bytes) Password of manufacturer

how do i read this address from the hal library?


What do you mean by that?

It's just a C #define - you use it just as you did previously.

 

Again, if you do want/need to move to HAL, then start by using  the example projects, to get familiar with how the HAL I2C works ...

 

You still haven't said what STM32 you're using, but here's the F0 version - with documentation:

/**
 * @brief Write an amount of data in blocking mode to a specific memory address
 * @PAram hi2c Pointer to a I2C_HandleTypeDef structure that contains
 * the configuration information for the specified I2C.
 * @PAram DevAddress Target device address: The device 7 bits address value
 * in datasheet must be shifted to the left before calling the interface
 * @PAram MemAddress Internal memory address
 * @PAram MemAddSize Size of internal memory address
 * @PAram pData Pointer to data buffer
 * @PAram Size Amount of data to be sent
 * @PAram Timeout Timeout duration
 * @retval HAL status
 */
HAL_StatusTypeDef HAL_I2C_Mem_Write( I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress,
 uint16_t MemAddSize, uint8_t *pData, uint16_t Size, uint32_t Timeout )

 

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.
Tesla DeLorean
Guru
January 6, 2025

With the HAL memory functions you can create an array of bytes for the transaction, and can specify if the address within the EPROM is 1 or 2 bytes wide. It will then do the >>8 operation for the high order byte.

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..