2021-12-17 12:46 AM
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:
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
*****
Solved! Go to Solution.
2021-12-17 06:56 AM
You can use HAL_I2C_EnableListen_IT and check for direction in HAL_I2C_AddrCallback and handle accordingly. Here is an example:
The example doesn't check direction, but it is trivial to add. Everything else is there.
2021-12-17 01:42 AM
Surely, the slave knows whether it is going to send or receive from the R/W bit when it is addressed?
2021-12-17 02:04 AM
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
*****
2021-12-17 05:37 AM
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.
2021-12-17 05:55 AM
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
2021-12-17 06:07 AM
2021-12-17 06:56 AM
You can use HAL_I2C_EnableListen_IT and check for direction in HAL_I2C_AddrCallback and handle accordingly. Here is an example:
The example doesn't check direction, but it is trivial to add. Everything else is there.
2021-12-19 11:51 PM
Thanks TDK, I will try it.
Best regards