cancel
Showing results for 
Search instead for 
Did you mean: 

HAL_I2C_Mem_Write is not working properly

Glebiys
Associate

Hello all,

Must use I/O expander (PCA9534PW) with STM32F334K8T6.

Code:

HAL_I2C_Mem_Write(&hi2c1, 0x20<<1, 0x03, I2C_MEMADD_SIZE_8BIT, 0x00, 1, 15000);

All code (CubeMX generated): https://pastebin.com/GKNxHSut

The usual HAL_I2C_Transmit is not suitable, since here it is necessary to select the module settings register (as well as with an external EEPROM).

1) 0x03 - configuration register

2) 0x00 - set all 8 pins to exit mode

When the code is run, the expander reacts, but not as it should. Instead of setting all pins to output mode, he puts one or more (I can see this by the LEDs on the expander outputs, since they are high by default). Having tried various combinations of the configuration register, for example 0x99 - all pins are in output mode. Although 0x99 shouldn't.

Identical and with further on / off pins - what I enter - does not match at the outputs.

My guess is that there is bit shifting going on here and the shifted combination arrives at the device.

What options did I check:

HAL_I2C_Mem_Write(&hi2c1, 0x20<<1, 0x01, I2C_MEMADD_SIZE_8BIT, 0x98, 1, 15000);
HAL_I2C_Mem_Write(&hi2c1, 0x20<<1, 0x03, I2C_MEMADD_SIZE_8BIT, 0x1<<1, 1, 300);
HAL_I2C_Mem_Write(&hi2c1, 0x20<<1, 0x03, I2C_MEMADD_SIZE_8BIT, 0xbf<<1, 1, 300);
HAL_I2C_Mem_Write(&hi2c1, 0x20<<1, 0x01, I2C_MEMADD_SIZE_8BIT, 0x72, 1, 1000);
HAL_I2C_Mem_Write(&hi2c1, 0x20<<1, 0x03, I2C_MEMADD_SIZE_8BIT, 0x0, 1, HAL_I2C_TIMEOUT_MAX);
HAL_I2C_Mem_Write(&hi2c1, 0x20<<1, 0x03, 1, 0xff<<1, 1, 10000);
HAL_I2C_Mem_Write(&hi2c1, 0x20<<1, 0x03, 1, 0x0, 1, 10000);
HAL_I2C_Mem_Write(&hi2c1, 0x20<<1, 0x01, 1, 0x1, 1, 10000);

Also several times changed combinations of sizes of the penultimate parameter of the function.

The expander is working properly, checked through the Arduino. Entering identical register values ​​on STM - the behavior is different.

Question: What could be the error and how to fix it?

Thank you!

Best regards,

Alex

1 ACCEPTED SOLUTION

Accepted Solutions
Glebiys
Associate

UPD.

The error was that I was directly writing register data to the function.

And there a pointer was needed.

Make a pointer - the problem was solved. Now it works!

The reason is my inattention.

uint8_t dataBuffer[1];
dataBuffer[0] = 0x0;
HAL_I2C_Mem_Write(&hi2c1, 0x20<<1, 0x03, I2C_MEMADD_SIZE_8BIT, dataBuffer, 1, 15000);

View solution in original post

1 REPLY 1
Glebiys
Associate

UPD.

The error was that I was directly writing register data to the function.

And there a pointer was needed.

Make a pointer - the problem was solved. Now it works!

The reason is my inattention.

uint8_t dataBuffer[1];
dataBuffer[0] = 0x0;
HAL_I2C_Mem_Write(&hi2c1, 0x20<<1, 0x03, I2C_MEMADD_SIZE_8BIT, dataBuffer, 1, 15000);