7
\$\begingroup\$

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.

asked Feb 7, 2020 at 11:01
\$\endgroup\$

1 Answer 1

9
\$\begingroup\$

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.

answered Feb 7, 2020 at 11:57
\$\endgroup\$
7
  • 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\$ Commented 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\$ Commented 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\$ Commented 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\$ Commented 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\$ Commented Aug 29, 2024 at 10:37

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.