xref: /aosp_15_r20/external/pigweed/pw_arduino_build/docs.rst (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1*61c4878aSAndroid Build Coastguard Worker.. _module-pw_arduino_build:
2*61c4878aSAndroid Build Coastguard Worker
3*61c4878aSAndroid Build Coastguard Worker-----------------
4*61c4878aSAndroid Build Coastguard Workerpw_arduino_build
5*61c4878aSAndroid Build Coastguard Worker-----------------
6*61c4878aSAndroid Build Coastguard Worker.. pigweed-module::
7*61c4878aSAndroid Build Coastguard Worker   :name: pw_arduino_build
8*61c4878aSAndroid Build Coastguard Worker
9*61c4878aSAndroid Build Coastguard WorkerThe ``pw_arduino_build`` module contains both the `arduino_builder`_ command
10*61c4878aSAndroid Build Coastguard Workerline utility and an `Arduino Main Wrapper`_.
11*61c4878aSAndroid Build Coastguard Worker
12*61c4878aSAndroid Build Coastguard Worker.. seealso::
13*61c4878aSAndroid Build Coastguard Worker   See the :ref:`target-arduino` target documentation for a list of supported
14*61c4878aSAndroid Build Coastguard Worker   hardware.
15*61c4878aSAndroid Build Coastguard Worker
16*61c4878aSAndroid Build Coastguard WorkerArduino Main Wrapper
17*61c4878aSAndroid Build Coastguard Worker====================
18*61c4878aSAndroid Build Coastguard Worker
19*61c4878aSAndroid Build Coastguard Worker``arduino_main_wrapper.cc`` implements the standard ``setup()`` and ``loop()``
20*61c4878aSAndroid Build Coastguard Workerfunctions [#f1]_ that are expected in Arduino sketches.
21*61c4878aSAndroid Build Coastguard Worker
22*61c4878aSAndroid Build Coastguard WorkerPigweed executables rely on being able to define the ``main()`` function. This
23*61c4878aSAndroid Build Coastguard Workeris a problem for Arduino code as each core defines it's own ``main()``. To get
24*61c4878aSAndroid Build Coastguard Workeraround this the Pigweed Arduino target renames ``main()`` to ``ArduinoMain()``
25*61c4878aSAndroid Build Coastguard Workerusing a preprocessor macro: ``-Dmain(...)=ArduinoMain()``. This macro only
26*61c4878aSAndroid Build Coastguard Workerapplies when compiling Arduino core source files. That frees up ``main()`` to be
27*61c4878aSAndroid Build Coastguard Workerused elsewhere.
28*61c4878aSAndroid Build Coastguard Worker
29*61c4878aSAndroid Build Coastguard WorkerMost Arduino cores will do some internal initialization before calling
30*61c4878aSAndroid Build Coastguard Worker``setup()`` followed by ``loop()``. To make sure Pigweed ``main()`` is started
31*61c4878aSAndroid Build Coastguard Workerafter that early init we run it within ``setup()``:
32*61c4878aSAndroid Build Coastguard Worker
33*61c4878aSAndroid Build Coastguard Worker.. code-block:: cpp
34*61c4878aSAndroid Build Coastguard Worker
35*61c4878aSAndroid Build Coastguard Worker   void setup() {
36*61c4878aSAndroid Build Coastguard Worker     pw_arduino_Init();
37*61c4878aSAndroid Build Coastguard Worker     // Start Pigweed main()
38*61c4878aSAndroid Build Coastguard Worker     main();
39*61c4878aSAndroid Build Coastguard Worker   }
40*61c4878aSAndroid Build Coastguard Worker
41*61c4878aSAndroid Build Coastguard Worker   void loop() {}
42*61c4878aSAndroid Build Coastguard Worker
43*61c4878aSAndroid Build Coastguard Worker.. note::
44*61c4878aSAndroid Build Coastguard Worker   ``pw_arduino_Init()`` initializes the :ref:`module-pw_sys_io_arduino`
45*61c4878aSAndroid Build Coastguard Worker   module.
46*61c4878aSAndroid Build Coastguard Worker
47*61c4878aSAndroid Build Coastguard Worker.. warning::
48*61c4878aSAndroid Build Coastguard Worker   You may notice ``loop()`` is empty in ``arduino_main_wrapper.cc`` and never
49*61c4878aSAndroid Build Coastguard Worker   called. This will cause any code appearing after ``loop()`` in an Arduino
50*61c4878aSAndroid Build Coastguard Worker   core to never be executed. For most cores this should be ok but may cause
51*61c4878aSAndroid Build Coastguard Worker   issues in some scenarios.
52*61c4878aSAndroid Build Coastguard Worker
53*61c4878aSAndroid Build Coastguard Workerarduino_builder
54*61c4878aSAndroid Build Coastguard Worker===============
55*61c4878aSAndroid Build Coastguard Worker
56*61c4878aSAndroid Build Coastguard Worker``arduino_builder`` is utility that can extract compile and tooling information
57*61c4878aSAndroid Build Coastguard Workerfrom an Arduino core. It's used within Pigweed to shovel compiler flags into
58*61c4878aSAndroid Build Coastguard Workerthe `GN <https://gn.googlesource.com/gn/>`_ build system. It will also work
59*61c4878aSAndroid Build Coastguard Workerwithout Pigweed and can be used with other build systems.
60*61c4878aSAndroid Build Coastguard Worker
61*61c4878aSAndroid Build Coastguard WorkerFull documentation is pending. For now run ``arduino_builder --help`` for
62*61c4878aSAndroid Build Coastguard Workerdetails.
63*61c4878aSAndroid Build Coastguard Worker
64*61c4878aSAndroid Build Coastguard Worker.. rubric::
65*61c4878aSAndroid Build Coastguard Worker   Footnotes
66*61c4878aSAndroid Build Coastguard Worker
67*61c4878aSAndroid Build Coastguard Worker.. [#f1]
68*61c4878aSAndroid Build Coastguard Worker   See the Arduino Reference documentation on `setup()
69*61c4878aSAndroid Build Coastguard Worker   <https://www.arduino.cc/reference/en/language/structure/sketch/setup/>`_, and
70*61c4878aSAndroid Build Coastguard Worker   `loop()
71*61c4878aSAndroid Build Coastguard Worker   <https://www.arduino.cc/reference/en/language/structure/sketch/loop/>`_.
72