Skip to content

micros() and millis() are uint32_t (unsigned int), instead of unsigned long #384

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
erenfro opened this issue May 18, 2017 · 5 comments
Closed

Comments

@erenfro
Copy link

erenfro commented May 18, 2017

Arduino-esp32/cores/esp32/esp32-hal.h

Shows that millis() and micros() are uint32_t types, which is unsigned int's. This is incompatible with the ESP8266 code which was unsigned long types. This causes portability to go downhill due to type differences.

It causes problems in portability in the SDK itself. How I found it specifically in my case is I have a function that is passed the reference of either millis or micros to use to determine timing mathmatics for frequency calculations. It was expecting the pointer to be an unsigned long because that's what micros and millis are in everything Arduino, except for ESP32's codebase.

It's also timing as well, because those numbers are long for the reasons of longevity. millis has about the time span of about 50 days, while micros has about 70 minutes. They can do that because of the size of an unsigned long, but can't with an unsigned int because the number isn't as large. This may not be the case of the ESP8266 or ESP32 specifically, however the Arduino SDK documents and uses unsigned long as part of the official documentation and implementation.

@me-no-dev
Copy link
Member

unsigned long on AVR is 32 bit ;) Just FYI.
I tried making the functions 64 bit at some point, but ran into issues, I'll give it another go... maybe use different method for millis();

@erenfro
Copy link
Author

erenfro commented May 19, 2017

I'm not sure if it's a bit issue, but it's definitely a typecast issue in respects to keeping with the Arduino SDK standard at the least.

Like I said, I primarily noticed this when porting ESP8266 code to ESP32 and one library I use to measure pulse width frequencies failed to work because millis() and micros() were not the same typecast as was expected, which was unsigned long. When using millis and micros, I've always seen unsigned long's used in reference to it.

@kriswiner
Copy link

unsigned long in ambiguous. Why not use uint64_t, for example, this is specific and cannot be confused with anything else.

@igrr
Copy link
Member

igrr commented May 20, 2017

As @erenfro correctly noted, this isn't a bit width issue, as both int and long are 32 bit on Xtensa. However in C++ int and long are different types, even if they are of the same size. So the only way to allow libraries to be portable between ESP32 Arduino core and other cores is to use unsigned long for the return value. Yes, this is ambiguous (there could be a platform with 64-bit long), but language-wise this is the only way to remain compatible with other cores.

@me-no-dev
Copy link
Member

Merged :)

blue-2357 pushed a commit to blue-2357/arduino-esp32 that referenced this issue Jul 17, 2024
dash0820 added a commit to dash0820/arduino-esp32-stripped that referenced this issue Mar 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants