I'm using I2C in a project on STM32, and i was wondering what's the difference between HAL_I2C_Master_Transmit and HAL_I2C_mem_write, and the difference between HAL_I2C_Master_Receive and HAL_I2C_mem_read.
1 Answer 1
The MEM functios can directly read and write devices that have register address based access. They write the register addresss before reading or writing the register data. Most chips are like this. The non-MEM functions just do simple reads and writes.
-
5\$\begingroup\$ Adding on to your description: The non-MEM functions send start bit, slave I2C address, data, and stop bit. The MEM functions send start bit, slave I2C address, slave internal mem address, repeat start bit, data, and stop bit. \$\endgroup\$kkrambo– kkrambo2020年02月07日 15:43:16 +00:00Commented Feb 7, 2020 at 15:43
-
\$\begingroup\$ @kkrambo is there ever a situation where you'd have to use the MEM functions instead of the non-MEM functions, or vice versa? \$\endgroup\$Ed Peguillan III– Ed Peguillan III2024年08月29日 02:04:21 +00:00Commented Aug 29, 2024 at 2:04
-
\$\begingroup\$ @EdPeguillanIII Interestingly put question. No you never have to use the MEM functions, if you use other functions in a more complex way to achieve the same protocol as MEM functions already do. It simply makes your life easier as 95% of the time you anyway can simply use the MEM functions and might not need the other functions, but 5% of the time you can't use the MEM functions. \$\endgroup\$Justme– Justme2024年08月29日 04:02:26 +00:00Commented Aug 29, 2024 at 4:02
-
\$\begingroup\$ @EdPeguillanIII In my experience, most I2C devices have internal registers at addresses. And for these you have to write the internal address and repeat-start before reading or writing the register value. Use the MEM functions for this type of transfer. Can you do a repeat-start with the non-MEM functions? (I doubt it.) However, if there is an I2C device without internally addressed registers then you would probably use the non-MEM functions to read/write to that device. Read the device's datasheet to see how to read/write data and then use the functions that do likewise. \$\endgroup\$kkrambo– kkrambo2024年08月29日 08:50:21 +00:00Commented Aug 29, 2024 at 8:50
-
\$\begingroup\$ @kkrambo (1) Most but definitely not all are register-based (2) You don't need restart when writing, unless the chip is very exceptional. Some chips refuse the write if you do send a restart, but that's a bit rare (3) Yes the HAL provides also non-MEM functions that allow you to do any types of transactions in multiple parts, which is what the MEM functions use internally, but of course the basic read and write functions don't do anything special. \$\endgroup\$Justme– Justme2024年08月29日 10:37:34 +00:00Commented Aug 29, 2024 at 10:37