When I took a look at how the arduino libraries were coded I expected to see register names like I/O registers(PINx,DDRx,PORTx) and other such registers with values assigned to them but instead I saw normal C code with functions and a class.
Why isn't there any embedded C in the structure of the library? Am I misunderstanding something very basic? Please help me understand.
2 Answers 2
If you take a look at the Arduino core library (e.g., the AVR version of wiring.c), you will see lots of low-level register access. That's why the core code is specific to a processor architecture.
Other libraries are built on top of the core API in order to be portable.
-
1Then why do people even code using low-level registers when they can just use the core API?roaibrain– roaibrain2018年08月23日 10:00:08 +00:00Commented Aug 23, 2018 at 10:00
-
1@Roshan: 1. Performance:
digitalWrite()
, for example, is more than 100 times slower than direct port access. 2. Hardware features: the core only provides access to the most generic stuff, you can fully exploit the hardware capabilities only if you access the hardware directly.Edgar Bonet– Edgar Bonet2018年08月23日 10:05:23 +00:00Commented Aug 23, 2018 at 10:05
Because the Arduino API abstracts those things.
If you put that kind of low-level code into a library the library becomes non-portable. By sticking to the Arduino API (digitalWrite, pinMode, etc) the library can be used on all boards.