How to control delays in non-blocking ways?
I am using H563ZI to develop a motor control project. I have several motors in my project. Motor control commands will be sent to the board with ethernet cable(I have finished this part). Once the board get the command, it should then make the motor with given index run for given period of time, and then stop the motor. I created a function that toggles the GPIO pins and calls HAL_Delay to control the period. After the delay, the function then toggle the GPIO pins back. The problem is that when one motor is in the HAL_Delay, the whole process is blocked. Is there any way that I can turn on several motors and stop them after their given period individually? For example, if [200, 300] is given, motor0 and motor1 should be turned on at (at least nearly) the same time. Motor0 should be turned off after 200ms, and motor1 should be turned off after 300ms.
I guess one idea is to use ThreadX, which I also used for the Ethernet part (https://github.com/STMicroelectronics/STM32CubeH5/tree/main/Projects/NUCLEO-H563ZI/Applications/NetXDuo/Nx_UDP_Echo_Client). However, I have at least 14 motors, should I create a thread for each motor? Or is there a better way to implement this?
Here is my function to control the motors now, which leads to blocking issue.
void stopMotor(int motor_idx) {
HAL_GPIO_WritePin(motors[motor_idx].IN1.GPIO_Port,
motors[motor_idx].IN1.GPIO_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(motors[motor_idx].IN2.GPIO_Port,
motors[motor_idx].IN2.GPIO_Pin, GPIO_PIN_RESET);
}
void controlMotor(int motor_idx, uint32_t duration, int direction) {
GPIO_PinState in1_state = direction == 0 ? GPIO_PIN_RESET : GPIO_PIN_SET;
GPIO_PinState in2_state = direction == 0 ? GPIO_PIN_SET : GPIO_PIN_RESET;
HAL_GPIO_WritePin(motors[motor_idx].IN1.GPIO_Port,
motors[motor_idx].IN1.GPIO_Pin, in1_state);
HAL_GPIO_WritePin(motors[motor_idx].IN2.GPIO_Port,
motors[motor_idx].IN2.GPIO_Pin, in2_state);
HAL_Delay(duration);
stopMotor(motor_idx);
}