Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 8026

General • Optimiser behaviour (C/C++)

$
0
0
Pi Pico RP2040 C/C++ and annoying optimiser behaviour...

I can't find any mention of this with various searches, so I wonder if it's just me? The optimiser has some very strange behaviours.

Take the code below, a slightly modified version of the same hello_gpio_int example... if I leave the optimiser at the default value it will never output anything. It seems to look at the main loop and say "oh, the edge variable is never changed in this function, so we'll just optimise out that loop content.

I know how to control the optimiser, both at the function and the file level, no problem, but I keep getting caught out by it. It works really well to speed up code, especially at optimising the SDK call internals which can be clumsy and slow, but I keep wasting ages with bugs and then smack my forehead... "oh, the bloody optimiser!".

Not the install. I just rebuilt my PC from scratch over the weekend with a full reinstall of everything. Same behaviour.

I'd love any feedback from RPi or others who have actually had similar issues.

Code:

static uint64_t last_time = 0;static uint64_t time = 0;static bool edge = false;void gpio_callback(uint gpio, uint32_t events) {    // Put the GPIO event(s) that just happened into event_str    // so we can print it//    gpio_event_string(event_str, events);//    printf("GPIO %d %s\n", gpio, event_str);    time = time_us_64() - last_time;    last_time = time_us_64();    edge = true;}int main() {    stdio_init_all();    printf("Hello GPIO IRQ\n");    gpio_init(GPIO_WATCH_PIN);    gpio_set_irq_enabled_with_callback(GPIO_WATCH_PIN, GPIO_IRQ_EDGE_RISE | GPIO_IRQ_EDGE_FALL, true, &gpio_callback);    // Wait forever    while (true) {        if (edge == 1) {            printf("Edge %d Pulse %lluus\n", edge, time);            edge = false;        }    }}

Statistics: Posted by Uncle_Phil — Mon Jun 02, 2025 11:45 pm



Viewing all articles
Browse latest Browse all 8026

Trending Articles