Skip to main content
Associate
June 13, 2024
Solved

Communication between STM32C011 and EEPROM M24C0

  • June 13, 2024
  • 5 replies
  • 6715 views

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

 

Best answer by HauDO

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

 

 


 

5 replies

Peter BENSCH
ST Technical Moderator
June 13, 2024

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.
HauDOAuthorBest answer
Associate
June 13, 2024

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

 

 


 

Senior III
June 14, 2024

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""."
Peter BENSCH
ST Technical Moderator
June 13, 2024

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.
Senior III
June 14, 2024

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""."
Karl Yamashita
Lead III
June 14, 2024

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);

 

If a reply has proven helpful, click on Accept as Solution so that it'll show at top of the post.CAN Jammer an open source CAN bus hacking toolCANableV3 Open Source
Karl Yamashita
Lead III
June 28, 2024

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.

If a reply has proven helpful, click on Accept as Solution so that it'll show at top of the post.CAN Jammer an open source CAN bus hacking toolCANableV3 Open Source
HauDOAuthor
Associate
June 28, 2024

I understand your point, and I also realize that ST made an initial transmission step for communication that is different from the requirements of M24C02. Therefore, I stopped using HAL and manually handled the bits that needed to be transmitted and received. Thank you !