2

I've just started working with RPi Pico (RP2040), and I tried the following simple program:

#include <stdio.h> #include "pico/stdlib.h" volatile int chars_rxed = 0; int main() { stdio_init_all(); while (true) { printf("%d: chrx %d\n", to_us_since_boot(get_absolute_time()), chars_rxed ); sleep_ms(1000); } return 0; } 

I'm compiling on MINGW64 under Windows. Program compiles fine, then I plug the Pico pressing the BOOTSEL button, I get the "drive", and I copy the .uf2 result there.

Then, when the Pico reboots, I connect to its USB Serial port using tio - and this is the printout I get:

$ tio --baudrate 115200 --databits 8 --flow none --stopbits 1 --parity none /dev/ttyS6 [tio 13:04:18] tio v1.32 [tio 13:04:18] Press ctrl-t q to quit [tio 13:04:18] Connected 0: chrx 5005043 0: chrx 6005262 0: chrx 7005426 0: chrx 8005621 0: chrx 9005778 

Considering the chars_rxed is always 0 in the code, it seems the printout reverses the order of arguments: so instead of printing to_us_since_boot(get_absolute_time()), chars_rxed, it prints chars_rxed, to_us_since_boot(get_absolute_time()).

But WHY? I've never seen this kind of a problem before ...

How can I get printf for Pico to print out the arguments as specified?

1 Answer 1

1

I think I got it - turns out, to_us_since_boot returns uint64_t, and thus the %d string format specifier is inappropriate for it.

Therefore, I have to use a different format specifier, and I decided on PRIu64 from inttypes.h:

#include <inttypes.h> //PRIu64 #include <stdio.h> #include "pico/stdlib.h" volatile int chars_rxed = 0; int main() { stdio_init_all(); while (true) { printf("%"PRIu64 ": chrx %d\n", to_us_since_boot(get_absolute_time()), chars_rxed ); sleep_ms(1000); } return 0; } 

This now prints the expected results:

$ tio --baudrate 115200 --databits 8 --flow none --stopbits 1 --parity none /dev/ttyS6 [tio 13:15:56] tio v1.32 [tio 13:15:56] Press ctrl-t q to quit [tio 13:15:56] Connected 4005136: chrx 0 5005345: chrx 0 6005522: chrx 0 7005689: chrx 0 8005847: chrx 0 

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.