xref: /aosp_15_r20/external/pigweed/targets/arduino/target_docs.rst (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1*61c4878aSAndroid Build Coastguard Worker.. _target-arduino:
2*61c4878aSAndroid Build Coastguard Worker
3*61c4878aSAndroid Build Coastguard Worker-------
4*61c4878aSAndroid Build Coastguard WorkerArduino
5*61c4878aSAndroid Build Coastguard Worker-------
6*61c4878aSAndroid Build Coastguard Worker
7*61c4878aSAndroid Build Coastguard WorkerThis target supports building Pigweed on a few Arduino cores.
8*61c4878aSAndroid Build Coastguard Worker
9*61c4878aSAndroid Build Coastguard Worker.. seealso::
10*61c4878aSAndroid Build Coastguard Worker   There are a few caveats when running Pigweed on top of the Arduino API. See
11*61c4878aSAndroid Build Coastguard Worker   :ref:`module-pw_arduino_build` for details.
12*61c4878aSAndroid Build Coastguard Worker
13*61c4878aSAndroid Build Coastguard WorkerSupported Boards
14*61c4878aSAndroid Build Coastguard Worker================
15*61c4878aSAndroid Build Coastguard Worker
16*61c4878aSAndroid Build Coastguard WorkerCurrently only Teensy 4.x and 3.x boards are supported.
17*61c4878aSAndroid Build Coastguard Worker
18*61c4878aSAndroid Build Coastguard Worker+------------------------------------------------------------------+-------------------------------------------------------------------+-----------+----------+-------------+
19*61c4878aSAndroid Build Coastguard Worker| Core                                                             | Board Name                                                        | Compiling | Flashing | Test Runner |
20*61c4878aSAndroid Build Coastguard Worker+==================================================================+===================================================================+===========+==========+=============+
21*61c4878aSAndroid Build Coastguard Worker| `teensy <https://www.pjrc.com/teensy/td_download.html>`_         | `Teensy 4.1 <https://www.pjrc.com/store/teensy41.html>`_          | ✓         | ✓        | ✓           |
22*61c4878aSAndroid Build Coastguard Worker+------------------------------------------------------------------+-------------------------------------------------------------------+-----------+----------+-------------+
23*61c4878aSAndroid Build Coastguard Worker| `teensy <https://www.pjrc.com/teensy/td_download.html>`_         | `Teensy 4.0 <https://www.pjrc.com/store/teensy40.html>`_          | ✓         | ✓        | ✓           |
24*61c4878aSAndroid Build Coastguard Worker+------------------------------------------------------------------+-------------------------------------------------------------------+-----------+----------+-------------+
25*61c4878aSAndroid Build Coastguard Worker| `teensy <https://www.pjrc.com/teensy/td_download.html>`_         | `Teensy 3.6 <https://www.pjrc.com/store/teensy36.html>`_          | ✓         | ✓        | ✓           |
26*61c4878aSAndroid Build Coastguard Worker+------------------------------------------------------------------+-------------------------------------------------------------------+-----------+----------+-------------+
27*61c4878aSAndroid Build Coastguard Worker| `teensy <https://www.pjrc.com/teensy/td_download.html>`_         | `Teensy 3.5 <https://www.pjrc.com/store/teensy35.html>`_          | ✓         | ✓        | ✓           |
28*61c4878aSAndroid Build Coastguard Worker+------------------------------------------------------------------+-------------------------------------------------------------------+-----------+----------+-------------+
29*61c4878aSAndroid Build Coastguard Worker| `teensy <https://www.pjrc.com/teensy/td_download.html>`_         | `Teensy 3.2 <https://www.pjrc.com/store/teensy32.html>`_          | ✓         | ✓        | ✓           |
30*61c4878aSAndroid Build Coastguard Worker+------------------------------------------------------------------+-------------------------------------------------------------------+-----------+----------+-------------+
31*61c4878aSAndroid Build Coastguard Worker| `arduino-samd <https://github.com/arduino/ArduinoCore-samd>`_    | `Arduino Zero <https://store.arduino.cc/usa/arduino-zero>`_       |           |          |             |
32*61c4878aSAndroid Build Coastguard Worker+------------------------------------------------------------------+-------------------------------------------------------------------+-----------+----------+-------------+
33*61c4878aSAndroid Build Coastguard Worker| `arduino-sam <https://github.com/arduino/ArduinoCore-sam>`_      | `Arduino Due <https://store.arduino.cc/usa/due>`_                 |           |          |             |
34*61c4878aSAndroid Build Coastguard Worker+------------------------------------------------------------------+-------------------------------------------------------------------+-----------+----------+-------------+
35*61c4878aSAndroid Build Coastguard Worker| `adafruit-samd <https://github.com/adafruit/ArduinoCore-samd>`_  | `Adafruit Feather M0 <https://www.adafruit.com/?q=feather+m0>`_   |           |          |             |
36*61c4878aSAndroid Build Coastguard Worker+------------------------------------------------------------------+-------------------------------------------------------------------+-----------+----------+-------------+
37*61c4878aSAndroid Build Coastguard Worker| `adafruit-samd <https://github.com/adafruit/ArduinoCore-samd>`_  | `Adafruit SAMD51 Boards <https://www.adafruit.com/category/952>`_ |           |          |             |
38*61c4878aSAndroid Build Coastguard Worker+------------------------------------------------------------------+-------------------------------------------------------------------+-----------+----------+-------------+
39*61c4878aSAndroid Build Coastguard Worker| `stm32duino <https://github.com/stm32duino/Arduino_Core_STM32>`_ |                                                                   |           |          |             |
40*61c4878aSAndroid Build Coastguard Worker+------------------------------------------------------------------+-------------------------------------------------------------------+-----------+----------+-------------+
41*61c4878aSAndroid Build Coastguard Worker
42*61c4878aSAndroid Build Coastguard WorkerSetup
43*61c4878aSAndroid Build Coastguard Worker=====
44*61c4878aSAndroid Build Coastguard Worker
45*61c4878aSAndroid Build Coastguard WorkerYou must first install an Arduino core or let Pigweed know where you have cores
46*61c4878aSAndroid Build Coastguard Workerinstalled using the ``pw_arduino_build_CORE_PATH`` build arg.
47*61c4878aSAndroid Build Coastguard Worker
48*61c4878aSAndroid Build Coastguard WorkerInstalling Arduino Cores
49*61c4878aSAndroid Build Coastguard Worker------------------------
50*61c4878aSAndroid Build Coastguard Worker
51*61c4878aSAndroid Build Coastguard WorkerThe ``arduino_builder`` utility can install Arduino cores automatically. It's
52*61c4878aSAndroid Build Coastguard Workerrecommended to install them to into ``third_party/arduino/cores/``.
53*61c4878aSAndroid Build Coastguard Worker
54*61c4878aSAndroid Build Coastguard Worker.. code-block:: sh
55*61c4878aSAndroid Build Coastguard Worker
56*61c4878aSAndroid Build Coastguard Worker   # Setup pigweed environment.
57*61c4878aSAndroid Build Coastguard Worker   . ./activate.sh
58*61c4878aSAndroid Build Coastguard Worker   # Install an arduino core, only teensy is supported
59*61c4878aSAndroid Build Coastguard Worker   pw package install teensy
60*61c4878aSAndroid Build Coastguard Worker
61*61c4878aSAndroid Build Coastguard WorkerBuilding
62*61c4878aSAndroid Build Coastguard Worker========
63*61c4878aSAndroid Build Coastguard WorkerTo build for this Pigweed target, simply build the top-level "arduino" Ninja
64*61c4878aSAndroid Build Coastguard Workertarget. You can set Arduino build options using ``gn args out`` or by running:
65*61c4878aSAndroid Build Coastguard Worker
66*61c4878aSAndroid Build Coastguard Worker.. code-block:: sh
67*61c4878aSAndroid Build Coastguard Worker
68*61c4878aSAndroid Build Coastguard Worker   gn gen out --args='
69*61c4878aSAndroid Build Coastguard Worker     pw_arduino_build_CORE_PATH = "//environment/packages"
70*61c4878aSAndroid Build Coastguard Worker     pw_arduino_build_CORE_NAME = "teensy"
71*61c4878aSAndroid Build Coastguard Worker     pw_arduino_build_PACKAGE_NAME = "avr/1.58.1"
72*61c4878aSAndroid Build Coastguard Worker     pw_arduino_build_BOARD = "teensy40"
73*61c4878aSAndroid Build Coastguard Worker     pw_arduino_build_MENU_OPTIONS=["menu.usb.serial", "menu.keys.en-us"]'
74*61c4878aSAndroid Build Coastguard Worker
75*61c4878aSAndroid Build Coastguard WorkerOn a Windows machine it's easier to run:
76*61c4878aSAndroid Build Coastguard Worker
77*61c4878aSAndroid Build Coastguard Worker.. code-block:: sh
78*61c4878aSAndroid Build Coastguard Worker
79*61c4878aSAndroid Build Coastguard Worker   gn args out
80*61c4878aSAndroid Build Coastguard Worker
81*61c4878aSAndroid Build Coastguard WorkerThat will open a text file where you can paste the args in:
82*61c4878aSAndroid Build Coastguard Worker
83*61c4878aSAndroid Build Coastguard Worker.. code-block:: text
84*61c4878aSAndroid Build Coastguard Worker
85*61c4878aSAndroid Build Coastguard Worker   pw_arduino_build_CORE_PATH = "//environment/packages"
86*61c4878aSAndroid Build Coastguard Worker   pw_arduino_build_CORE_NAME = "teensy"
87*61c4878aSAndroid Build Coastguard Worker   pw_arduino_build_PACKAGE_NAME = "avr/1.58.1"
88*61c4878aSAndroid Build Coastguard Worker   pw_arduino_build_BOARD = "teensy40"
89*61c4878aSAndroid Build Coastguard Worker   pw_arduino_build_MENU_OPTIONS = ["menu.usb.serial", "menu.keys.en-us"]
90*61c4878aSAndroid Build Coastguard Worker
91*61c4878aSAndroid Build Coastguard WorkerSave the file and close the text editor.
92*61c4878aSAndroid Build Coastguard Worker
93*61c4878aSAndroid Build Coastguard WorkerThen build with:
94*61c4878aSAndroid Build Coastguard Worker
95*61c4878aSAndroid Build Coastguard Worker.. code-block:: sh
96*61c4878aSAndroid Build Coastguard Worker
97*61c4878aSAndroid Build Coastguard Worker   ninja -C out arduino
98*61c4878aSAndroid Build Coastguard Worker
99*61c4878aSAndroid Build Coastguard WorkerTo see supported boards and Arduino menu options for a given core:
100*61c4878aSAndroid Build Coastguard Worker
101*61c4878aSAndroid Build Coastguard Worker.. code-block:: sh
102*61c4878aSAndroid Build Coastguard Worker
103*61c4878aSAndroid Build Coastguard Worker   arduino_builder --arduino-package-path ./environment/packages/teensy \
104*61c4878aSAndroid Build Coastguard Worker                   --arduino-package-name avr/1.58.1 \
105*61c4878aSAndroid Build Coastguard Worker                   list-boards
106*61c4878aSAndroid Build Coastguard Worker
107*61c4878aSAndroid Build Coastguard Worker.. code-block:: text
108*61c4878aSAndroid Build Coastguard Worker
109*61c4878aSAndroid Build Coastguard Worker   Board Name  Description
110*61c4878aSAndroid Build Coastguard Worker   teensy41    Teensy 4.1
111*61c4878aSAndroid Build Coastguard Worker   teensy40    Teensy 4.0
112*61c4878aSAndroid Build Coastguard Worker   teensy36    Teensy 3.6
113*61c4878aSAndroid Build Coastguard Worker   teensy35    Teensy 3.5
114*61c4878aSAndroid Build Coastguard Worker   teensy31    Teensy 3.2 / 3.1
115*61c4878aSAndroid Build Coastguard Worker
116*61c4878aSAndroid Build Coastguard WorkerYou may wish to set different arduino build options in
117*61c4878aSAndroid Build Coastguard Worker``pw_arduino_build_MENU_OPTIONS``. Run this to see what's available for your core:
118*61c4878aSAndroid Build Coastguard Worker
119*61c4878aSAndroid Build Coastguard Worker.. code-block:: sh
120*61c4878aSAndroid Build Coastguard Worker
121*61c4878aSAndroid Build Coastguard Worker   arduino_builder --arduino-package-path ./environment/packages/teensy \
122*61c4878aSAndroid Build Coastguard Worker                   --arduino-package-name avr/1.58.1 \
123*61c4878aSAndroid Build Coastguard Worker                   list-menu-options \
124*61c4878aSAndroid Build Coastguard Worker                   --board teensy40
125*61c4878aSAndroid Build Coastguard Worker
126*61c4878aSAndroid Build Coastguard WorkerThat will show all menu options that can be added to ``gn args out``.
127*61c4878aSAndroid Build Coastguard Worker
128*61c4878aSAndroid Build Coastguard Worker.. code-block:: text
129*61c4878aSAndroid Build Coastguard Worker
130*61c4878aSAndroid Build Coastguard Worker   All Options
131*61c4878aSAndroid Build Coastguard Worker   ----------------------------------------------------------------
132*61c4878aSAndroid Build Coastguard Worker   menu.usb.serial             Serial
133*61c4878aSAndroid Build Coastguard Worker   menu.usb.serial2            Dual Serial
134*61c4878aSAndroid Build Coastguard Worker   menu.usb.serial3            Triple Serial
135*61c4878aSAndroid Build Coastguard Worker   menu.usb.keyboard           Keyboard
136*61c4878aSAndroid Build Coastguard Worker   menu.usb.touch              Keyboard + Touch Screen
137*61c4878aSAndroid Build Coastguard Worker   menu.usb.hidtouch           Keyboard + Mouse + Touch Screen
138*61c4878aSAndroid Build Coastguard Worker   menu.usb.hid                Keyboard + Mouse + Joystick
139*61c4878aSAndroid Build Coastguard Worker   menu.usb.serialhid          Serial + Keyboard + Mouse + Joystick
140*61c4878aSAndroid Build Coastguard Worker   menu.usb.midi               MIDI
141*61c4878aSAndroid Build Coastguard Worker   ...
142*61c4878aSAndroid Build Coastguard Worker
143*61c4878aSAndroid Build Coastguard Worker   Default Options
144*61c4878aSAndroid Build Coastguard Worker   --------------------------------------
145*61c4878aSAndroid Build Coastguard Worker   menu.usb.serial             Serial
146*61c4878aSAndroid Build Coastguard Worker   menu.speed.600              600 MHz
147*61c4878aSAndroid Build Coastguard Worker   menu.opt.o2std              Faster
148*61c4878aSAndroid Build Coastguard Worker   menu.keys.en-us             US English
149*61c4878aSAndroid Build Coastguard Worker
150*61c4878aSAndroid Build Coastguard WorkerTesting
151*61c4878aSAndroid Build Coastguard Worker=======
152*61c4878aSAndroid Build Coastguard WorkerWhen working in upstream Pigweed, building this target will build all Pigweed
153*61c4878aSAndroid Build Coastguard Workermodules' unit tests.  These tests can be run on-device in a few different ways.
154*61c4878aSAndroid Build Coastguard Worker
155*61c4878aSAndroid Build Coastguard WorkerRun a unit test
156*61c4878aSAndroid Build Coastguard Worker---------------
157*61c4878aSAndroid Build Coastguard WorkerIf using ``out`` as a build directory, tests will be located in
158*61c4878aSAndroid Build Coastguard Worker``out/arduino_debug/obj/[module name]/[test_name].elf``.
159*61c4878aSAndroid Build Coastguard Worker
160*61c4878aSAndroid Build Coastguard WorkerTests can be flashed and run using the `arduino_unit_test_runner` tool. Here is
161*61c4878aSAndroid Build Coastguard Workera sample bash script to run all tests on a Linux machine.
162*61c4878aSAndroid Build Coastguard Worker
163*61c4878aSAndroid Build Coastguard Worker.. code-block:: sh
164*61c4878aSAndroid Build Coastguard Worker
165*61c4878aSAndroid Build Coastguard Worker   #!/bin/bash
166*61c4878aSAndroid Build Coastguard Worker   gn gen out --export-compile-commands \
167*61c4878aSAndroid Build Coastguard Worker       --args='pw_arduino_build_CORE_PATH="environment/packages"
168*61c4878aSAndroid Build Coastguard Worker               pw_arduino_build_CORE_NAME="teensy"
169*61c4878aSAndroid Build Coastguard Worker               pw_arduino_build_PACKAGE_NAME="avr/1.58.1"
170*61c4878aSAndroid Build Coastguard Worker               pw_arduino_build_BOARD="teensy40"
171*61c4878aSAndroid Build Coastguard Worker               pw_arduino_build_MENU_OPTIONS=["menu.usb.serial", "menu.keys.en-us"]' && \
172*61c4878aSAndroid Build Coastguard Worker     ninja -C out arduino
173*61c4878aSAndroid Build Coastguard Worker
174*61c4878aSAndroid Build Coastguard Worker   for f in $(find out/arduino_debug/obj/ -iname "*.elf"); do
175*61c4878aSAndroid Build Coastguard Worker       arduino_unit_test_runner --verbose \
176*61c4878aSAndroid Build Coastguard Worker           --config-file ./out/arduino_debug/gen/arduino_builder_config.json \
177*61c4878aSAndroid Build Coastguard Worker           --upload-tool teensyloader \
178*61c4878aSAndroid Build Coastguard Worker           $f
179*61c4878aSAndroid Build Coastguard Worker   done
180*61c4878aSAndroid Build Coastguard Worker
181*61c4878aSAndroid Build Coastguard WorkerUsing the test server
182*61c4878aSAndroid Build Coastguard Worker---------------------
183*61c4878aSAndroid Build Coastguard Worker
184*61c4878aSAndroid Build Coastguard WorkerTests may also be run using the `pw_arduino_use_test_server = true` GN arg.
185*61c4878aSAndroid Build Coastguard WorkerThe server must be run with an `arduino_builder` config file so it can locate
186*61c4878aSAndroid Build Coastguard Workerthe correct Arduino core, compiler path, and Arduino board used.
187*61c4878aSAndroid Build Coastguard Worker
188*61c4878aSAndroid Build Coastguard Worker.. code-block:: sh
189*61c4878aSAndroid Build Coastguard Worker
190*61c4878aSAndroid Build Coastguard Worker   arduino_test_server --verbose \
191*61c4878aSAndroid Build Coastguard Worker       --config-file ./out/arduino_debug/gen/arduino_builder_config.json
192*61c4878aSAndroid Build Coastguard Worker
193*61c4878aSAndroid Build Coastguard Worker.. TODO(tonymd): Flesh out this section similar to the stm32f429i target docs.
194*61c4878aSAndroid Build Coastguard Worker
195*61c4878aSAndroid Build Coastguard WorkerFlashing Known Issues
196*61c4878aSAndroid Build Coastguard Worker---------------------
197*61c4878aSAndroid Build Coastguard Worker
198*61c4878aSAndroid Build Coastguard WorkerTeensy Boards
199*61c4878aSAndroid Build Coastguard Worker^^^^^^^^^^^^^
200*61c4878aSAndroid Build Coastguard Worker
201*61c4878aSAndroid Build Coastguard WorkerBy default Teensyduino uses the `Teensy Loader Application
202*61c4878aSAndroid Build Coastguard Worker<https://www.pjrc.com/teensy/loader.html>`_ which has a couple limitations:
203*61c4878aSAndroid Build Coastguard Worker
204*61c4878aSAndroid Build Coastguard Worker- Requires a GUI (or X11 on Linux).
205*61c4878aSAndroid Build Coastguard Worker- Can only flash one board at a time.
206*61c4878aSAndroid Build Coastguard Worker
207*61c4878aSAndroid Build Coastguard WorkerGN Target Example
208*61c4878aSAndroid Build Coastguard Worker=================
209*61c4878aSAndroid Build Coastguard Worker
210*61c4878aSAndroid Build Coastguard WorkerHere is an example `pw_executable` gn rule that includes some Teensyduino
211*61c4878aSAndroid Build Coastguard Workerlibraries.
212*61c4878aSAndroid Build Coastguard Worker
213*61c4878aSAndroid Build Coastguard Worker.. code-block:: text
214*61c4878aSAndroid Build Coastguard Worker
215*61c4878aSAndroid Build Coastguard Worker   import("//build_overrides/pigweed.gni")
216*61c4878aSAndroid Build Coastguard Worker   import("$dir_pw_arduino_build/arduino.gni")
217*61c4878aSAndroid Build Coastguard Worker   import("$dir_pw_build/target_types.gni")
218*61c4878aSAndroid Build Coastguard Worker
219*61c4878aSAndroid Build Coastguard Worker   _library_args = [
220*61c4878aSAndroid Build Coastguard Worker     "--library-path",
221*61c4878aSAndroid Build Coastguard Worker     rebase_path(arduino_core_library_path, root_build_dir),
222*61c4878aSAndroid Build Coastguard Worker     "--library-names",
223*61c4878aSAndroid Build Coastguard Worker     "Time",
224*61c4878aSAndroid Build Coastguard Worker     "Wire",
225*61c4878aSAndroid Build Coastguard Worker   ]
226*61c4878aSAndroid Build Coastguard Worker
227*61c4878aSAndroid Build Coastguard Worker   pw_executable("my_app") {
228*61c4878aSAndroid Build Coastguard Worker     # All Library Sources
229*61c4878aSAndroid Build Coastguard Worker     _library_c_files = exec_script(
230*61c4878aSAndroid Build Coastguard Worker             arduino_builder_script,
231*61c4878aSAndroid Build Coastguard Worker             arduino_show_command_args + _library_args + [
232*61c4878aSAndroid Build Coastguard Worker               "--library-c-files"
233*61c4878aSAndroid Build Coastguard Worker             ],
234*61c4878aSAndroid Build Coastguard Worker             "list lines")
235*61c4878aSAndroid Build Coastguard Worker     _library_cpp_files = exec_script(
236*61c4878aSAndroid Build Coastguard Worker             arduino_builder_script,
237*61c4878aSAndroid Build Coastguard Worker             arduino_show_command_args + _library_args + [
238*61c4878aSAndroid Build Coastguard Worker               "--library-cpp-files"
239*61c4878aSAndroid Build Coastguard Worker             ],
240*61c4878aSAndroid Build Coastguard Worker             "list lines")
241*61c4878aSAndroid Build Coastguard Worker
242*61c4878aSAndroid Build Coastguard Worker     sources = [ "main.cc" ] + _library_c_files + _library_cpp_files
243*61c4878aSAndroid Build Coastguard Worker
244*61c4878aSAndroid Build Coastguard Worker     deps = [
245*61c4878aSAndroid Build Coastguard Worker       "$dir_pw_hex_dump",
246*61c4878aSAndroid Build Coastguard Worker       "$dir_pw_log",
247*61c4878aSAndroid Build Coastguard Worker       "$dir_pw_string",
248*61c4878aSAndroid Build Coastguard Worker     ]
249*61c4878aSAndroid Build Coastguard Worker
250*61c4878aSAndroid Build Coastguard Worker     include_dirs = exec_script(arduino_builder_script,
251*61c4878aSAndroid Build Coastguard Worker                                arduino_show_command_args + _library_args +
252*61c4878aSAndroid Build Coastguard Worker                                    [ "--library-include-dirs" ],
253*61c4878aSAndroid Build Coastguard Worker                                "list lines")
254*61c4878aSAndroid Build Coastguard Worker
255*61c4878aSAndroid Build Coastguard Worker     # Required for using Arduino.h and any Arduino API functions
256*61c4878aSAndroid Build Coastguard Worker     remove_configs = [ "$dir_pw_build:strict_warnings" ]
257*61c4878aSAndroid Build Coastguard Worker     deps += [ "$dir_pw_third_party/arduino:arduino_core_sources" ]
258*61c4878aSAndroid Build Coastguard Worker   }
259