Skip to main content
KiptonM
Senior III
March 30, 2022
Solved

How do you implement an I2C Slave to look like a memory?

Here is what I am trying to Implement. To make the I2C Slave look like a memory to the master.

Syntax is one of two cases.

For a Write:

<Dev Add W><MemAdd MSB><MemAdd LSB><Data0><Data1>...<Data N><Stop>

For a Read:

<Dev Add W><MemAdd MSB><MemAdd LSB><Restart Dev Add R><Data0><Data1>...<Data N><Stop>

I am on the first step. I want to use DMA because I do not want interrupts firing on every byte. My processor has to do other things like data collection, and not just mind the I2C.

I think I have to use the HAL_I2C_Slave_Seq_Receive_DMA() and HAL_I2C_Slave_Seq_Transmit_DMA() routines.

There is a XferOptions parameter, and I can not figure out what to use for the initial read (from the slave point of view.) .

It does not matter which one I use, I get an error.

I am looking for one that gracefully ends with a stop or a restart. Because in the write case, they could write 2 bytes at the address, or could write 2 more bytes at the next address. I am limiting them to a total of 8 words or 16 bytes.

In the case of a read they just write the 2 address bytes then do a restart.

Is the answer to just ignore the errors and continue? I really do not like having a product with functions throwing error interrupts.

Thanks in advance.

Kip

This topic has been closed for replies.
KnarfB
KnarfBBest answer
Super User
March 30, 2022

You may check this code, but it is using _IT not _DMA.

https://gitlab.com/stm32mcu/i2c_master_and_servant.git

The changes should be straightforward modulo HAL issues.

hth

KnarfB

9 replies

KiptonM
KiptonMAuthor
Senior III
March 30, 2022

If I tell the Slave to just read 18 bytes, the two address bytes and the maximum of 16 data bytes, and it gets a stop before the 16 bytes, it calls the HAL_I2C_ErrorCallback(). The bytes are in the buffer, but I do not know how many bytes are valid.

Tesla DeLorean
Guru
March 30, 2022

I suspect you'll have to dig that out of the DMA registers.

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
TDK
March 30, 2022

> Is the answer to just ignore the errors and continue? I really do not like having a product with functions throwing error interrupts.

NACK is classified as an error flag, even though it is used in normal communications. If the "error" is due to the master NACK to signify the end of reading data, the correct procedure would be to ignore it and return to the main loop to await the next instruction.

"If you feel a post has answered your question, please click ""Accept as Solution""."
KnarfB
KnarfBBest answer
Super User
March 30, 2022

You may check this code, but it is using _IT not _DMA.

https://gitlab.com/stm32mcu/i2c_master_and_servant.git

The changes should be straightforward modulo HAL issues.

hth

KnarfB

KiptonM
KiptonMAuthor
Senior III
March 30, 2022

Thanks. I will have to check it tonight when I get home. It looks like my work computer will not allow me to access that site.

KiptonM
KiptonMAuthor
Senior III
April 1, 2022

I got my code to work thanks to your example. Thank you.