Wyatt Hepler | f9fb90f | 2020-09-30 18:59:33 -0700 | [diff] [blame] | 1 | .. _module-pw_arduino_build: |
Anthony DiGirolamo | 9147aa0 | 2020-09-22 10:41:05 -0700 | [diff] [blame] | 2 | |
| 3 | ----------------- |
| 4 | pw_arduino_build |
| 5 | ----------------- |
| 6 | |
| 7 | The ``pw_arduino_build`` module contains both the `arduino_builder`_ command |
| 8 | line utility and an `Arduino Main Wrapper`_. |
| 9 | |
| 10 | .. seealso:: |
Wyatt Hepler | f9fb90f | 2020-09-30 18:59:33 -0700 | [diff] [blame] | 11 | See the :ref:`target-arduino` target documentation for a list of supported |
Anthony DiGirolamo | 9147aa0 | 2020-09-22 10:41:05 -0700 | [diff] [blame] | 12 | hardware. |
| 13 | |
| 14 | Arduino Main Wrapper |
| 15 | ==================== |
| 16 | |
| 17 | ``arduino_main_wrapper.cc`` implements the standard ``setup()`` and ``loop()`` |
| 18 | functions [#f1]_ that are expected in Arduino sketches. |
| 19 | |
| 20 | Pigweed executables rely on being able to define the ``main()`` function. This |
| 21 | is a problem for Arduino code as each core defines it's own ``main()``. To get |
| 22 | around this the Pigweed Arduino target renames ``main()`` to ``ArduinoMain()`` |
| 23 | using a preprocessor macro: ``-Dmain(...)=ArduinoMain()``. This macro only |
| 24 | applies when compiling Arduino core source files. That frees up ``main()`` to be |
| 25 | used elsewhere. |
| 26 | |
| 27 | Most Arduino cores will do some internal initialization before calling |
| 28 | ``setup()`` followed by ``loop()``. To make sure Pigweed ``main()`` is started |
| 29 | after 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 Hepler | f9fb90f | 2020-09-30 18:59:33 -0700 | [diff] [blame] | 42 | ``pw_arduino_Init()`` initializes the :ref:`module-pw_sys_io_arduino` |
Anthony DiGirolamo | 9147aa0 | 2020-09-22 10:41:05 -0700 | [diff] [blame] | 43 | 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 | |
| 51 | arduino_builder |
| 52 | =============== |
| 53 | |
| 54 | ``arduino_builder`` is utility that can extract compile and tooling information |
| 55 | from an Arduino core. It's used within Pigweed to shovel compiler flags into |
| 56 | the `GN <https://gn.googlesource.com/gn/>`_ build system. It will also work |
| 57 | without Pigweed and can be used with other build systems. |
| 58 | |
| 59 | Full documentation is pending. For now run ``arduino_builder --help`` for |
| 60 | details. |
| 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 | |