cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F412 I2C slave that may be required to transmit or receive (non blocking with interrupt) without knowing it first?

Guidoted
Associate III

On a STM32F412 I need to have an I2C slave that may be required to transmit or receive data without knowing it first.

The problem is that we have two separate functions:

  • HAL_I2C_Slave_Receive_IT to receive data from the master, but the driver will hang if the master will try to read data
  • HAL_I2C_Slave_Transmit_IT to send data to the master, but the driver will hang if the master will try to write data

Is there anything useful in HAL or any other library, or have i to hardly write low level code by myself to do it?

Any other suggestion?

Thanks in advance

*****

1 ACCEPTED SOLUTION

Accepted Solutions
TDK
Guru

You can use HAL_I2C_EnableListen_IT and check for direction in HAL_I2C_AddrCallback and handle accordingly. Here is an example:

https://github.com/STMicroelectronics/STM32CubeF4/blob/2f3b26f16559f7af495727a98253067a31182cfc/Projects/STM32F412G-Discovery/Examples/I2C/I2C_TwoBoards_RestartComIT/readme.txt

The example doesn't check direction, but it is trivial to add. Everything else is there.

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

View solution in original post

7 REPLIES 7
Andrew Neil
Evangelist III

Surely, the slave knows whether it is going to send or receive from the R/W bit when it is addressed?

Thanks for the answer, Neil.

Probably I didn't explain well what my question is: is there any already made library (HAL will be better...) with this management already done, or I have to write from ground by myself the complete communications interrupts routines checking R/W flag, data transfers, ack, nack, stop, etc.?

Thanks

*****

Nikita91
Lead II

The functions you mention are basic and only do one thing:

if HAL_I2C_Slave_Receive_IT is used, then the master must make a transmission.

This is the problem with basic and general libraries such as HAL.

The I2C is an asymmetric protocol: the master decides what to do and the slave must respond to the request.

So if the slave has something to transmit he must wait for the master to ask for it, but at the same time he must be ready to receive a message from the master.

On the slave side, you must therefore wait for the addressing phase, then, depending on the R / W bit, do what is requested by the master.

Any good I2C library can do this: separate the addressing phase, then transmission, then stop / restart. but I don't know how to do it with HAL because I don't use it.

The I2C is complicated, and even more so on the slave side.

Thanks Nikita.

I know what is needed because I had developed succesfully I2C slave with interrupt for other MCU's (last one was MSP430 family).

I'm currently busy with other projects so it would be better for me to find something ready to use (also low level, no HAL), instead of spending hours writing all the code from ground,

Do you know where can I find something?

Thanks

TDK
Guru

You can use HAL_I2C_EnableListen_IT and check for direction in HAL_I2C_AddrCallback and handle accordingly. Here is an example:

https://github.com/STMicroelectronics/STM32CubeF4/blob/2f3b26f16559f7af495727a98253067a31182cfc/Projects/STM32F412G-Discovery/Examples/I2C/I2C_TwoBoards_RestartComIT/readme.txt

The example doesn't check direction, but it is trivial to add. Everything else is there.

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

Thanks TDK, I will try it.

Best regards