Sterling
Sterling
DIIDevHeads IoT Integration Server
Created by Sterling on 7/30/2024 in #firmware-and-baremetal
How to optimize handling of multiple inputs efficiently on STM32F4 using GCC?
Good day everyone, I am using an STM32F4 series (F429ZI) microcontroller clocked at 180 MHz, and I'm compiling my code using GCC for ARM. I have a series of 40 if statements that each check an input for an embedded board. Running through these statements is very slow.
uint32_t INPUT_ARRAY[40];
#define INPUT_X INPUT_ARRAY[0] // input 1 corresponds to the first array slot and so on, easy to call up a specific input.

void main(){
while(1) // infinite loop for embedded program
{
Read_Inputs(); // input read function

Write_Outputs(); // output write function

Logic_Test(); // This is to test out the inputs and outputs on our hardware test rig
}
}

inline void Logic_Test(void){
if ( INPUT_1 != 0){
output_3 |= some_bit // this logic could change
output_10 |= another_bit
}
if ( INPUT_2 != 0){
output_x |= some_bit
}
if ( INPUT_3 != 0){
output_x |= some_bit
}
if ( INPUT_4 != 0){
output_x |= some_bit // this logic could change
}
if ( INPUT_X != 0){
output_x |= some_bit // this logic could change
}
// ... repeated for all 40 inputs
if ( INPUT_40 != 0){
output_x |= some_bit
}
}
uint32_t INPUT_ARRAY[40];
#define INPUT_X INPUT_ARRAY[0] // input 1 corresponds to the first array slot and so on, easy to call up a specific input.

void main(){
while(1) // infinite loop for embedded program
{
Read_Inputs(); // input read function

Write_Outputs(); // output write function

Logic_Test(); // This is to test out the inputs and outputs on our hardware test rig
}
}

inline void Logic_Test(void){
if ( INPUT_1 != 0){
output_3 |= some_bit // this logic could change
output_10 |= another_bit
}
if ( INPUT_2 != 0){
output_x |= some_bit
}
if ( INPUT_3 != 0){
output_x |= some_bit
}
if ( INPUT_4 != 0){
output_x |= some_bit // this logic could change
}
if ( INPUT_X != 0){
output_x |= some_bit // this logic could change
}
// ... repeated for all 40 inputs
if ( INPUT_40 != 0){
output_x |= some_bit
}
}
The code is structured like this for inputs 1 to 40. They are not if-else-if statements. I would use a switch, but as far as I know, a switch only handles one case at a time, and multiple inputs could be on simultaneously. Is there a more efficient way to handle all the inputs without so many "if" statements?
5 replies