Perusing the code within the core functions

Hello all,
I find myself often curious about the code which makes up an Arduino function. I can sometimes google for a given function and occasionally someone will display the code of that given function so I can read it and understand it.

I've dug around inside my installation (which is linux) looking for .cpp and/or .h files.

But I may not be able to find the code for a given function.
Take the function bitSet() for example.
Where would I find the code which makes up this function?

Perhaps there is a Github site somewhere which has all of the core functions - and one can see the code within them?

Sincere thanks!

I will take a SWAG and say that is part of the compiler and would not appear in a lilbrary.

@got_arduino1 I'll give you the link because it's easier than guessing where the core is stored in your installation. :wink:

There you have to entertain yourself.

bitSet and the like are macros defined in Arduino.h:

#define bitRead(value, bit) (((value) >> (bit)) & 0x01)
#define bitSet(value, bit) ((value) |= (1UL << (bit)))
#define bitClear(value, bit) ((value) &= ~(1UL << (bit)))
#define bitToggle(value, bit) ((value) ^= (1UL << (bit)))
#define bitWrite(value, bit, bitvalue) ((bitvalue) ? bitSet(value, bit) : bitClear(value, bit))
1 Like

Wonderful!!!!
Thank you very much!! :-]
With my installation - I have an "Arduino" folder in the /home/user file path.
This is where I was searching.

The "Arduino.h" file is located on my system - in a different file path
That's why I couldn't find it

Very sincere thanks!!!

Thank you!!

Note that there will be more than one file named Arduino.h on your system if you have boards other than the standard ones installed.

This is to allow for functions with the same name to work differently depending on the processor being used

Yes I can understand why that would be the case!
My sincere thanks!

One of the "Arduino" folders (the one I was originally searching) is a hidden folder.

But the "Arduino.h" file is located in a different path which is not hidden.
I used Catfish to find it. Love linux!!! :-]

Thanks my friend!

1 Like

since you're Linux, you can do things like

_Others find /tools/Arduino/hardware/arduino -name "*.h" -exec grep -H bitSet {} \;
/tools/Arduino/hardware/arduino/avr/cores/arduino/Arduino.h:#define bitSet(value, bit) ((value) |= (1UL << (bit)))
/tools/Arduino/hardware/arduino/avr/cores/arduino/Arduino.h:#define bitWrite(value, bit, bitvalue) ((bitvalue) ? bitSet(value, bit) : bitClear(value, bit))

See also the VERY USEFUL Gnu ID Utils

westfw@ww-gaming:/mnt/c/Users/westf/AppData/Local/Arduino15/packages$ mkid

westfw@ww-gaming:/mnt/c/Users/westf/AppData/Local/Arduino15/packages$ gid bitWrite
MegaCore/hardware/avr/3.0.0/cores/MCUdude_corefiles/Arduino.h:156:#define bitWrite(value, bit, bitvalue) ((bitvalue) ? bitSet(value, bit) : bitClear(value, bit))
arduino/hardware/avr/1.8.6/cores/arduino/Arduino.h:115:#define bitWrite(value, bit, bitvalue) ((bitvalue) ? bitSet(value, bit) : bitClear(value, bit))
arduino/hardware/sam/1.6.12/cores/arduino/wiring_constants.h:89:#define bitWrite(value, bit, bitvalue) (bitvalue ? bitSet(value, bit) : bitClear(value, bit))
esp32/hardware/esp32/2.0.14/cores/esp32/Arduino.h:99:#define bitWrite(value, bit, bitvalue) ((bitvalue) ? bitSet(value, bit) : bitClear(value, bit))
esp8266/hardware/esp8266/3.1.2/cores/esp8266/Arduino.h:148:#define bitWrite(value, bit, bitvalue) (bitvalue ? bitSet(value, bit) : bitClear(value, bit))
teensy/hardware/avr/1.58.1/cores/teensy/wiring.h:88:#define bitWrite(value, bit, bitvalue) ((bitvalue) ? bitSet((value), (bit)) : bitClear((value), (bit)))
teensy/hardware/avr/1.58.1/cores/teensy3/wiring.h:190:#define bitWrite(value, bit, bitvalue) ((bitvalue) ? bitSet((value), (bit)) : bitClear((value), (bit)))
teensy/hardware/avr/1.58.1/cores/teensy4/wiring.h:211:#define bitWrite(value, bit, bitvalue) ((bitvalue) ? bitSet((value), (bit)) : bitClear((value), (bit)))
teensy/hardware/avr/1.58.1/libraries/ShiftPWM/ShiftPWM.h:92:      bitWrite(*dataPort, dataBit, *(ledPtr)<=counter );
teensy/hardware/avr/1.58.1/libraries/ShiftPWM/ShiftPWM.h:95:      bitWrite(*dataPort, dataBit, *(ledPtr)>counter );
vega/hardware/riscv/1.0.7/cores/arduino/Common.h:63:#define bitWrite(value, bit, bitvalue) (bitvalue ? bitSet(value, bit) : bitClear(value, bit))
vega/hardware/riscv/1.0.7/cores/arduino/wiring_constants.h:93:#define bitWrite(value, bit, bitvalue) (bitvalue ? bitSet(value, bit) : bitClear(value, bit))
arduino/hardware/mbed_giga/4.0.8/cores/arduino/api/Common.h:70:#define bitWrite(value, bit, bitvalue) ((bitvalue) ? bitSet((value), (bit)) : bitClear((value), (bit)))
arduino/hardware/mbed_rp2040/4.0.8/cores/arduino/api/Common.h:70:#define bitWrite(value, bit, bitvalue) ((bitvalue) ? bitSet((value), (bit)) : bitClear((value), (bit)))
arduino/hardware/megaavr/1.8.8/cores/arduino/api/Common.h:70:#define bitWrite(value, bit, bitvalue) ((bitvalue) ? bitSet((value), (bit)) : bitClear((value), (bit)))
arduino/hardware/renesas_uno/1.0.4/cores/arduino/api/Common.h:70:#define bitWrite(value, bit, bitvalue) ((bitvalue) ? bitSet((value), (bit)) : bitClear((value), (bit)))
arduino/hardware/samd/1.8.13/cores/arduino/api/Common.h:68:#define bitWrite(value, bit, bitvalue) (bitvalue ? bitSet(value, bit) : bitClear(value, bit))

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.