cancel
Showing results for 
Search instead for 
Did you mean: 

How to differentiate Motor 1 from Motor 2 in __weak void *HALL_TIMx_CC_IRQHandler(void *pHandleVoid)?

Christophe65
Associate III
Hi everyone,
 
I'm working on a dual motor setup using an STM32F303 with the STM32 Motor Control SDK (MCSDK). I'm using FOC with Hall sensors and 3-shunt current sensing. The project is configured with MC Workbench and controlled via ST Motor Pilot.
 
I’m trying to track the position of Motor 1 using the following code inside the Hall sensor interrupt handler:
rotation_count_M1 -= pHandle->Direction * SenseMoteurM1;
 
This works fine when only one motor is connected. However, when both motors are active, the function __weak void *HALL_TIMx_CC_IRQHandler(void *pHandleVoid) is called for both motors, and the position count becomes incorrect — which makes sense, since I can't tell which motor triggered the interrupt.
I tried something like:
if (pHandle == "HALL_M1") { ... }
But of course, that doesn’t work because pHandle is a pointer, not a string.
My question is:
What is the proper way to identify whether pHandle corresponds to Motor 1 or Motor 2 inside this interrupt handler? Is there a recommended way to compare it with the actual motor handles (e.g., HALL_M1, HALL_M2) or retrieve the motor index?
Thanks in advance for your help!
3 REPLIES 3
STuser2
Senior II

Does MCSDK supported the code generation for 2 motors, or you are implementing on your own? Probably if you are implementing on your own better to look at the code MCSDK is generating for 2 motors, i think it is supporting some series of micro and boards for dual motor configuration.

Generate Code with Dual Motor on MC WorkBench - STMicroelectronics Community

 

Christophe65
Associate III
Yes, the MCSDK does support code generation for dual motor setups, and I’m using it successfully on my STM32F303 board. The configuration was done using MC Workbench, and I control the motors via ST Motor Pilot.
Both motors are running correctly — the firmware builds and executes without issue, and I can start/stop each motor independently.
However, I’m having trouble managing the position tracking for each motor. Specifically, in the __weak void *HALL_TIMx_CC_IRQHandler(void *pHandleVoid) function, I can't reliably determine whether the interrupt was triggered by Motor 1 or Motor 2. This causes incorrect updates to my position counters.
If anyone has experience with identifying the motor source from pHandleVoid or has a recommended approach for separating the logic per motor, I’d really appreciate your help!
Christophe65
Associate III

I managed to get it working with the following code:

if (pHandle->H1Pin == 8){
    rotation_count_M1 -= pHandle->Direction * SenseMoteurM1;
}

It does the job, but I don’t find it very clean.
I would have preferred to use something like a symbolic name (e.g. M1) instead of hardcoding the GPIO pin number.
If anyone knows a better way to identify the motor or Hall sensor instance without relying on the pin number, I’d really appreciate your input!