blob: 6db47e32e617159f6905684c6c09737c353eaa91 [file] [log] [blame]
Wyatt Heplerf9fb90f2020-09-30 18:59:33 -07001.. _module-pw_arduino_build:
Anthony DiGirolamo9147aa02020-09-22 10:41:05 -07002
3-----------------
4pw_arduino_build
5-----------------
6
7The ``pw_arduino_build`` module contains both the `arduino_builder`_ command
8line utility and an `Arduino Main Wrapper`_.
9
10.. seealso::
Wyatt Heplerf9fb90f2020-09-30 18:59:33 -070011 See the :ref:`target-arduino` target documentation for a list of supported
Anthony DiGirolamo9147aa02020-09-22 10:41:05 -070012 hardware.
13
14Arduino Main Wrapper
15====================
16
17``arduino_main_wrapper.cc`` implements the standard ``setup()`` and ``loop()``
18functions [#f1]_ that are expected in Arduino sketches.
19
20Pigweed executables rely on being able to define the ``main()`` function. This
21is a problem for Arduino code as each core defines it's own ``main()``. To get
22around this the Pigweed Arduino target renames ``main()`` to ``ArduinoMain()``
23using a preprocessor macro: ``-Dmain(...)=ArduinoMain()``. This macro only
24applies when compiling Arduino core source files. That frees up ``main()`` to be
25used elsewhere.
26
27Most Arduino cores will do some internal initialization before calling
28``setup()`` followed by ``loop()``. To make sure Pigweed ``main()`` is started
29after that early init we run it within ``setup()``:
30
31.. code-block:: cpp
32
33 void setup() {
34 pw_arduino_Init();
35 // Start Pigweed main()
36 main();
37 }
38
39 void loop() {}
40
41.. note::
Wyatt Heplerf9fb90f2020-09-30 18:59:33 -070042 ``pw_arduino_Init()`` initializes the :ref:`module-pw_sys_io_arduino`
Anthony DiGirolamo9147aa02020-09-22 10:41:05 -070043 module.
44
45.. warning::
46 You may notice ``loop()`` is empty in ``arduino_main_wrapper.cc`` and never
47 called. This will cause any code appearing after ``loop()`` in an Arduino
48 core to never be executed. For most cores this should be ok but may cause
49 issues in some scenarios.
50
51arduino_builder
52===============
53
54``arduino_builder`` is utility that can extract compile and tooling information
55from an Arduino core. It's used within Pigweed to shovel compiler flags into
56the `GN <https://gn.googlesource.com/gn/>`_ build system. It will also work
57without Pigweed and can be used with other build systems.
58
59Full documentation is pending. For now run ``arduino_builder --help`` for
60details.
61
62.. rubric::
63 Footnotes
64
65.. [#f1]
66 See the Arduino Reference documentation on `setup()
67 <https://www.arduino.cc/reference/en/language/structure/sketch/setup/>`_, and
68 `loop()
69 <https://www.arduino.cc/reference/en/language/structure/sketch/loop/>`_.
70