cancel
Showing results for 
Search instead for 
Did you mean: 

Communication between STM32C011 and EEPROM M24C0

HauDO
Associate II

Chào bạn,

Tôi đã thiết lập kết nối theo sơ đồ và tôi đã tải mã trong tệp đính kèm nhưng tôi không thể đọc dữ liệu từ EEPROM. Địa chỉ M24C02 của tôi được đặt thành 0xA0.

Ai đó có thể giúp tôi xác định vấn đề? Cảm ơn rất nhiều.

 

HauDO_1-1718248224550.png

 

1 ACCEPTED SOLUTION

Accepted Solutions

Dear Peter,

I have already replaced the resistors R1 and R2 with values of 4.6K, but it still doesn't work. I think the issue might be with the software. I am using the "HAL_I2C_Mem_Write" and "HAL_I2C_Mem_Read" functions from the HAL library.

My code looks like this, but I still can't communicate with the M24C02HauDO_0-1718266257022.png

 

 


 

View solution in original post

10 REPLIES 10
Peter BENSCH
ST Employee

Firstly, please increase R1 and R2 to 3.3k-4.7k and connect them to VDD (+3.3V).

Regards
/Peter

In order 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.

Dear Peter,

I have already replaced the resistors R1 and R2 with values of 4.6K, but it still doesn't work. I think the issue might be with the software. I am using the "HAL_I2C_Mem_Write" and "HAL_I2C_Mem_Read" functions from the HAL library.

My code looks like this, but I still can't communicate with the M24C02HauDO_0-1718266257022.png

 

 


 

Peter BENSCH
ST Employee

Please check again whether you have also connected R2 to VDD, as it is still connected to GND in your schematics.

After a quick look at your main.c, I noticed that you consistently ignore the return values of the HAL calls. Please bear in mind that unforeseen things can happen when controlling external devices, especially EEPROMs, e.g. a write process that takes a long time.

Perhaps a look at X-CUBE-EEPRMA1 is helpful, where you can see how this is implemented.

Regards
/Peter

 

In order 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.
Techn
Senior III

Check the i2c status

while(HAL_I2C_IsDeviceReady(&hi2c1, EEPROM_ADDR,10, HAL_MAX_DELAY));

set the i2c frequency as 100khz

Please note that the page size is only 16 bytes, so when you want to write 100 bytes, the page changes.... after each page write, you have to give the write delay or poll the status.

So first you can try to do a byte write and read with 0x55 and see how it happens. it takes 5 ms to write complete

btw, Why do you have different EEPROM address  as CALIB_EEPROM_WR and RD?

If you feel a post has answered your question, please click "Accept as Solution".

you can try this snippet and see if you get back what is written into location zero..

while(HAL_I2C_IsDeviceReady(&hi2c1, EEPROM_ADDR,10, HAL_MAX_DELAY));
uint8_t Wvalue = 25;
HAL_I2C_Mem_Write(&hi2c1, EEPROM_ADDR, 0, 1,&Wvalue , 1, 1000);
HAL_Delay(10);
while(HAL_I2C_IsDeviceReady(&hi2c1, EEPROM_ADDR,10, HAL_MAX_DELAY));
uint8_t Rvalue = 0;
HAL_I2C_Mem_Read(&hi2c1, EEPROM_ADDR, 0, 1,&Rvalue , 1, 1000);

If you feel a post has answered your question, please click "Accept as Solution".
Karl Yamashita
Lead III

You say the slave address is 0xA0. But how are CALIB_EEPROM_WR and CALIB_EEPROM_RD defined?

 

You should only have one define. The HAL driver will automatically set the read bit, but it does require the slave address be shifted left by 1 like so...

#define CALIB_EEPROM (0xA0 << 1)

HAL_I2C_Mem_Write(&hi2c1, CALIB_EEPROM, 100, 4, dataWrite, 4, 1000);

 

Tips and Tricks with TimerCallback https://www.youtube.com/@eebykarl
If you find my solution useful, please click the Accept as Solution so others see the solution.

I increased the pull-up resistors to 4.6k Ohm on R1 and R2, but it still didn't work due to my code. After reviewing the I2C communication code, I was able to successfully read and write data to the EEPROM. Thank you very much.

I am now able to read and write data successfully. My program is a bit lengthy, but it is functioning stable. CALIB_EEPROM_WR = 0xA0, CALIB_EEPROM_RD = 0xA1. Thank you very much

HauDO_0-1719545199708.pngHauDO_1-1719545228144.png

 

Karl Yamashita
Lead III

I2C uses 7 bit addressing. So technically the slave address is 0x50 but ST doesn't indicate that, which is poor documentation.

You have to left shift the slave address 0x50 by one and the value is 0xA0 before you call the HAL routine, but still it's not the actual slave address. ST half baked their I2C routines. Most if not all other MCU manufacturers will use 0x50 and automatically shift it in code to become 0xA0 and set bit 0 if it's a read. 

So you only need to use 0xA0 for reading or writing. The HAL driver automatically sets bit 0 when you do a read, so the value the HAL driver sends on the bus is 0xA1 

That's why @Techn and I are asking why there are two slave addresses when there should only be one.

Tips and Tricks with TimerCallback https://www.youtube.com/@eebykarl
If you find my solution useful, please click the Accept as Solution so others see the solution.