Hardware Setup
I am using an ESP32S3 as an I2C master and an STM32G0 as an I2C slave. The STM32G0 is performing two tasks:
- I2C Slave Communication – Receiving periodic requests (every second) from the ESP32S3 to read dimmer register updates.
- AC Fan Control via RobotDyn Dimmer – Handling a Zero-Cross Detection (ZCD) interrupt and generating a PWM signal for the TRIAC to achieve smooth dimming.
Interrupt Details
- The ZCD interrupt is triggered every 95 microseconds to ensure precise dimming. Increasing this value results in noticeable flicker.
- The I2C interrupt occurs every second to update the slave dimmer registers (which also get updated via push button inputs on the dimmer).
- I have set the timer interrupt (ZCD) with a higher priority than the I2C interrupt to maintain dimming precision.
- DMA
Issue Faced
Whenever the STM32G0 processes an I2C interrupt, it misses a ZCD interrupt, causing a visible jerk in the dimming process. This likely happens because the I2C interrupt temporarily blocks the ZCD interrupt, leading to skipped TRIAC triggering.
Pin Configuration
ESP32S3 (I2C Master):
- I2C_SCL: GPIO 14
- I2C_SDA: GPIO 21
STM32G0 (I2C Slave + Dimmer Control):
- ZCD_IN: PB4
- TRIAC_TRIGGER: PB9
- I2C_SCL: PB6
- I2C_SDA: PB7
Attempted Solutions
- Increased ZCD interrupt priority above I2C interrupt.
- Implemented DMA for I2C transactions.
Questions
- How can I ensure that the ZCD interrupt is never missed, even when the I2C transaction is being processed?
- Are there any best practices for handling multiple high-priority interrupts (ZCD + I2C) without affecting real-time performance?
Github link for dimmer control example : https://github.com/RobotDynOfficial/RBDDimmer/blob/master/examples/SimpleToggleDimmer/SimpleToggleDimmer.ino
NOTE: The code has been ported from Arduino library to STM32 Cube IDE to control dimmer.
Any kind of help or suggestions is appreciated .