Why doesn't compile functions like pinMod to required instructions !

why doesn't compile functions like pinMod to just required instructions like setting registers instead of putting whole function to avr
for taking less space and more speed

Because just setting a register needs more understanding than calling it a function name that is more like English.

You are not required to use pinMode(), digitalWrite(), etc. People who need higher speed use direct register access instructions.

It's a matter of convenience. Arduino supports more platforms than only the avr processors. pinMode and digitalWrite works on all supported platforms. In the end all these variants set registers directly and they may do some checking.
When setting avr registers directly you stick to that processor. But of course you can do that if you like.

I don't mean changing syntax or functions of Arduino functions by "doesn't compile functions like pinMode to required instructions." I mean compile the functions with the same syntax to the truly necessary code by the IC. For example, a readable pinMode function being compiled to a simple register instruction instead of bloating the IC with the whole function. also by "compiler" I don't mean changing compiler I mean doing that by editing libraries
thank you for your answer

Hi,
If we edit the base libraries to support multiple device-specific compilations for the same functions (like pinMode), it would be possible to support all platforms in the most optimized and user-friendly way, without being tied to a specific processor. :smiling_face_with_tear:
thanks for your answer

Why do this, you use pin mode only once in your code, or at least you should do.
What you call bloat is simply working what pin is being asked to change, and working out what register bit to change that maps to that pin. How are you going to cut that down? Have a look at the code for the pin mode function and see what is involved, then try and do better. I think you are worrying too much about this, and it is not a problem.

However, for a function that you use a lot, like digitalWrite, there might be a bit of fat to trim.
Look at this:-

For the sort if thing you need for that.

1 Like

Please provide a compilable replacement for e.g. pinMode() as to how you think it can work; it needs to support two processors (e.g. 328P for Uno and 2560 for Mega).

There really is not that much bloat in pinMode(), at least not for AVR based processors.

  1. Determine the bit in the register that needs to be changed.
  2. Determine the register to be changed.
  3. Write the register based on the required mode.

There is a little additional stuff to harden the code.

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