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?
4 Replies
te0
te05mo ago
maybe a polling task which sets an event group according to the pin?
Sterling
Sterling5mo ago
Thanks for this @te0 , but could you please elaborate on how I might implement a polling task that sets an event group according to the pin? More like, how would this approach improve the performance compared to the current series of if statements?
Sterling
Sterling5mo ago
Perhaps a code explaining it would be most appreciated 🙏 @te0
te0
te05mo ago
EXTI_enable_pins();

void gpio_isr(void* pvArg)
{
gpio_t pin_num = (gpio_t) pvArg;
xEventGroupSetBitsFromISR( xEventGroup,1 << pin_num);
}

void gpio_task(void* Arg)
{
while(1)
{
xEventGroupWaitBits(xEventGroup,xEventAnyBit , pdTRUE, pdFALSE, portMAX_DELAY);
process_gpio();
}

vTaskDelete(NULL);
}
EXTI_enable_pins();

void gpio_isr(void* pvArg)
{
gpio_t pin_num = (gpio_t) pvArg;
xEventGroupSetBitsFromISR( xEventGroup,1 << pin_num);
}

void gpio_task(void* Arg)
{
while(1)
{
xEventGroupWaitBits(xEventGroup,xEventAnyBit , pdTRUE, pdFALSE, portMAX_DELAY);
process_gpio();
}

vTaskDelete(NULL);
}
Want results from more Discord servers?
Add your server