Skip to main content
Visitor II
March 3, 2025
Solved

I2C detect repeated start condition

  • March 3, 2025
  • 2 replies
  • 1469 views

Hi!

I'm creating an i2c-slave using a stm32g0-series. There are two types of messages that need to be supported, a write-then-read, and a write-only.

The write-then-read starts with a write-message of a register address of the slave device (1 byte). This is followed by a repeated start condition where the master writes the data to the register address.     

The write-only starts in the same manner, but in addition to the register-address, a number of bytes (depending on the register) are also written to the slave. 

IFAIK these are fairly common ways to do reads and writes. 

The write-then-read messages work fine with the use of HAL_I2C_Slave_Seq_Receive_IT (receive one byte) and HAL_I2C_Slave_Seq_Transmit_IT (transmit n bytes). 

My problem arises when the first message (write) can either be one byte (write-then-read) or multiple bytes (write-only). The way I see it, I either have to detect if the repeated start condition has happened (one byte has been sent), or I need to expect more bytes than I potentially can receive and handle an error if fewer bytes were received... both of them don't seem possible to do.

Is there any way of handling both of these message types in the STM32 HAL library?

    This topic has been closed for replies.
    Best answer by TDK

    If transmission stops early, STOPF is handled and HAL_I2C_SlaveRxCpltCallback is called from the IRQ handler. The handle will have information on how many bytes were received. Not sure if there's a repeated start from a write to another write but it probably works similarly. If there's another start condition, it's a new transaction.

    2 replies

    Super User
    March 3, 2025

    If a multiple-byte write is possible, call HAL_I2C_Slave_Seq_Receive_IT using the largest message size expected.

    A repeated start does not generally occur if multiple bytes are being sent. I haven't seen a chip that supports of expects that.

    sigmuhaAuthor
    Visitor II
    March 5, 2025

    Thanks for the reply!

    If I call the HAL_I2C_Slave_Seq_Receive_IT to receive the maximum number of bytes, what happens if it doesn't receive that many bytes? Is an error callback called if a premature repeated start or stop condition occurs? 

    TDKAnswer
    Super User
    March 5, 2025

    If transmission stops early, STOPF is handled and HAL_I2C_SlaveRxCpltCallback is called from the IRQ handler. The handle will have information on how many bytes were received. Not sure if there's a repeated start from a write to another write but it probably works similarly. If there's another start condition, it's a new transaction.

    sigmuhaAuthor
    Visitor II
    March 6, 2025

    I see.

    If this is the case, this will work for my application.

    Thanks!