2025-12-18 4:31 AM - edited 2025-12-18 4:32 AM
Hi there! Apologies if this is not the right place to be asking this question, but I'm deep in the weeds writing my own motor controller from scratch, and have a question about timing:
My setup uses the STSpin233 with three-shunt low-side current sensing, and an stm32f4 microcontroller. I am using freeRTOS, and have a single task dedicated to motor control. (I have no other tasks yet, but intend to have some other tasks for UI management and audio processing later).
At the moment, the way I've structured things is:
I am running my PWM at 20kHz (so, a complete center-aligned PWM period is 50us). I can measure the time that passes between when my ADC is triggered and when the conversion completes (5us). And the time it takes my control code to run is somewhat reasonable (there's room to optimize, but that's not my concern just yet).
What I'm noticing is that there seems to be a nearly 18us delay between when I release my semaphore from the ISR and when my main while() loop sees it and begins to do work. It's pretty consistent per PWM period. That seems excessively long to me, and I'm curious how to mitigate it.
Consulting various AI tools, they all suggest that what I'm seeing is a "context switch penalty", and is expected. One suggestion is switching away from semaphores to vTaskNotifyGiveFromISR(), which is apparently a lighter-weight mechanism for doing this handoff (I need to reread the FreeRTOS manual to fully understand this).
But, another suggestion is to run my control loop entirely within my ISR that fires when the ADC is complete. This is bewildering to me, and I'm having a hard time wrapping my head around this idea - I've had it drilled into me that one is never meant to spend lots of time in an ISR, which is why I thought to do the semaphore thing I'm doing. Maybe this isn't the right way to manage this though?
I'm wondering if I could get the opinion of a human (rather than relying on AI advice) on this?
Thank you so much!
2025-12-18 8:34 AM
Hello @sb_st,
You can refer to the motor control software development kit (MCSDK) implementation with the MCSDK tool, available here.
Timings and scheduling are described in the Current sampling in three-shunt topology using two A/D converters chapter of the documentation, accessible through MCSDK Workbench tool > About > Documentations > Documentation > User manual tab > Current sampling link.