printf problem



  • In the following code (connect pin 1 to GND and the interrupt increases a counter) I need to include the first printf-statement in the while-loop otherwise the second printf-statement doesn't produce any output. I have checked that even without output the interrupt works just fine, but no printing. If I include the first printf-statement everything works as expected. Does anybody know what I do wrong?

    Edit: the first printf-statement can also be replaced with e.g. usleep(1). But it has to be before the if-statement, not right before the second printf-statement.

    #include <stdio.h>
    #include <unistd.h>
    #include "fpioa.h"
    #include "gpiohs.h"
    #include "sysctl.h"
    
    #define PIN 1
    #define GPIO 2
    
    uint32_t g_count = 0;
    
    int irq_gpiohs_key(void *ctx)
    {
        uint32_t *tmp = (uint32_t *)(ctx);
        (*tmp)++;
        return 0;
    }
    
    int main(void)
    {
        uint32_t saved_count = 0;
    
        plic_init();
        sysctl_enable_irq();
        // connect physical pin (PIN) to function GPIO
        fpioa_set_function(PIN, FUNC_GPIOHS0 + GPIO);
        gpiohs_set_drive_mode(GPIO, GPIO_DM_INPUT_PULL_UP);
        gpiohs_set_pin_edge(GPIO, GPIO_PE_BOTH);
        gpiohs_irq_register(GPIO, 1, irq_gpiohs_key, &g_count);
    
        while (1)
        {
            uint32_t tmp = g_count;
            // with this printf the one in the if-statement works, without it there is no output at all
            printf("g_count=%d saved_count=%d\n", tmp, saved_count);
            if (tmp != saved_count)
            {   // this printf only works if the previous is present
                printf("====================> g_count = %d\n", tmp);
                saved_count = tmp;
            }
        }
    }