Page 1 of 1

Using I2C causes issues with MCPWM Capture.

Posted: Fri Jan 26, 2024 1:51 pm
by Ravenholem
Hello everyone!
So while working on a project I came across something I can't seem to explain.
The Scope of work is as follows:
I retrieve data from a sensor over I2C. (I use this library https://github.com/UncleRus/esp-idf-lib to help make things easier)
Using MCPWM Capture 0, I capture a PWM input signal on Negative Edge and get its period to calculate frequency.
I take that Frequency value and modify it as needed and output it using LEDC at 50% duty.

The strange behavior I am experiencing:
I setup and initialize the MCPWM capture and LEDC.
I setup and internalize I2C using the library.
The MCPWM and LEDC part of the code works perfect. I capture a 5khz frequency and I output expected 5khz frequency.

If I start using the I2C bus to do anything say Read from a sensor. I start seeing random captures of 23khz on my MCPWM Capture.
Looking at the PWM input with a scope and triggering a pin every time a capture happens shows that MCPWM triggers on the Falling Edge of my PWM then again on the next Rising edge. This should NOT be happening. The MCPWM is set up to only trigger on falling edges so that I can calculate the period. If I stop sending any data through I2C this issue goes away and the MCPWM captures as normal.

My input PWM signal has a sloped knee shaped rising edge and a sharp falling edge.

In the attached picture you can see what I am describing.
The "Purple" trace is the PWM signal at the chip pin.
The "Yellow" trace is the MCPWM Capture unit ISR toggling a PIN every time it fires.
As you can see in the picture it will sporadically fire twice and this is the issue causing all my trouble.