STM32F407 I2C master: BTF flag often fails to set with multi-byte TX
I am using an STM32F407 MCU as an I2C master, my code built on the LL API of the STM32F4Cube IDE (version 1.25.0). I find that quite often, i.e. always within a handful of bytes, the BTF flag fails to set when I am doing a multi-byte TX. Single-byte TXs always work, it is only multi-byte TXs that suffer from this problem. I do not appear to receive a NACK from the far end (i.e. I2C_FLAG_AF is not set while I'm waiting for BTF to set), just not a "set-BTF".
What reasons might there be for this?
My code follows the pattern of stm32f4xx_hal_i2c.c as closely as possible and can be found here, where this is the send() function that gets stuck. Below is a sample log of a problem case (you can match the log points with the code), a millisecond timestamp on the left: you can see that the BTF flag is normally set almost immediately (within the millisecond resolution of my timestamp) until suddenly it is not, even after waiting for 10 milliseconds.
24876: EVENT_i2c1: TXing this many bytes 8
24876: EVENT_i2c10: TX sent a byte because TXE is set
24876: EVENT_i2c8: TX BTF flag set after waiting, all is good with the world
24876: EVENT_i2c10: TX sent a byte because TXE is set
24876: EVENT_i2c8: TX BTF flag set after waiting, all is good with the world
24876: EVENT_i2c10: TX sent a byte because TXE is set
24876: EVENT_i2c8: TX BTF flag set after waiting, all is good with the world
24876: EVENT_i2c10: TX sent a byte because TXE is set
24876: EVENT_i2c8: TX BTF flag set after waiting, all is good with the world
24876: EVENT_i2c10: TX sent a byte because TXE is set
24876: EVENT_i2c8: TX BTF flag set after waiting, all is good with the world
24876: EVENT_i2c10: TX sent a byte because TXE is set
24888:* EVENT_i2c4: TX wait for BTF flag failed to set