Overhead of triggers

Hi,

I have a question regarding the overhead of trigger_high() and trigger_low() functions in the following code snippet.

Could you please help me understand how much overhead these functions introduce in terms of execution time?

If I place a pair of trigger_high() and trigger_low() outside cmp() as follows and call trigger.get_trigger_times(), I get back a list of [1148].

bool cmp(const char* s1, const char* s2)
{ 
    if ( (s1 == NULL) || (s2 == NULL) ) {
        return false;
    }
    
    int i = 0;
    for (i = 0; s1[i] != '\0' && s2[i] != '\0'; i++) {
        if (s1[i] != s2[i]) {
            return false;
        }
    }
    return true;
}

void test()
{
    trigger_high();
    cmp();
    trigger_low();

    trigger_high();
    trigger_low();
}

But if I move the triggers into cmp() and insert a pair around each of two blocks, then trigger.get_trigger_times() returns [992, 1128].

bool cmp(const char* s1, const char* s2)
{ 
    trigger_high();
    if ( (s1 == NULL) || (s2 == NULL) ) {
        trigger_low();
        return false;
    }
    trigger_low();
    
    int i = 0;
    trigger_high();
    for (i = 0; s1[i] != '\0' && s2[i] != '\0'; i++) {
        if (s1[i] != s2[i]) {
            trigger_low();
            return false;
        }
    }
    trigger_low();
    return true;
}

void test()
{
    cmp();

    trigger_high();
    trigger_low();
}

Since each element in the returned list is the number of ADC cycles between successive triggers, the total cycles of cmp() now seems to be 992 + 1128 = 2120 cycles, in the second case. It almost doubles that of the first case, which is 1148 cycles.

Is this understanding correct? Is it caused by the triggers or something else?

Thanks very much.

Hi,

If you’re using the SAM4S target, there is a fair amount of overhead from extra calls to gpio_configure_pin, so that’s probably correct. This isn’t really needed, as it’s already done during setup, so you should be able to remove those calls from hal/sam4s/sam4s_hal.c.

Alex

Yes, I am using the SAM4S target. Thanks.

Another question,

If I place two consecutive calls to cmp as follows. I would expect get_trigger_times to return [992, 1128, 992, 1128]. But I got [992, 1128, 992], missing the last one. What might cause it? Thanks.

void test()
{
    cmp();
    cmp();

    trigger_high();
    trigger_low();
}