xref: /aosp_15_r20/external/pigweed/pw_system/docs.rst (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1*61c4878aSAndroid Build Coastguard Worker.. _module-pw_system:
2*61c4878aSAndroid Build Coastguard Worker
3*61c4878aSAndroid Build Coastguard Worker=========
4*61c4878aSAndroid Build Coastguard Workerpw_system
5*61c4878aSAndroid Build Coastguard Worker=========
6*61c4878aSAndroid Build Coastguard Worker.. warning::
7*61c4878aSAndroid Build Coastguard Worker  This module is an early work-in-progress towards an opinionated framework for
8*61c4878aSAndroid Build Coastguard Worker  new projects built on Pigweed. It is under active development, so stay tuned!
9*61c4878aSAndroid Build Coastguard Worker
10*61c4878aSAndroid Build Coastguard Workerpw_system is quite different from typical Pigweed modules. Rather than providing
11*61c4878aSAndroid Build Coastguard Workera single slice of vertical functionality, pw_system pulls together many modules
12*61c4878aSAndroid Build Coastguard Workeracross Pigweed to construct a working system with RPC, Logging, an OS
13*61c4878aSAndroid Build Coastguard WorkerAbstraction layer, and more. pw_system exists to greatly simplify the process
14*61c4878aSAndroid Build Coastguard Workerof starting a new project using Pigweed by drastically reducing the required
15*61c4878aSAndroid Build Coastguard Workerconfiguration space required to go from first signs of on-device life to a more
16*61c4878aSAndroid Build Coastguard Workersophisticated production-ready system.
17*61c4878aSAndroid Build Coastguard Worker
18*61c4878aSAndroid Build Coastguard Worker--------------------
19*61c4878aSAndroid Build Coastguard WorkerTrying out pw_system
20*61c4878aSAndroid Build Coastguard Worker--------------------
21*61c4878aSAndroid Build Coastguard WorkerIf you'd like to give pw_system a spin and have a STM32F429I Discovery board,
22*61c4878aSAndroid Build Coastguard Workerrefer to the board's
23*61c4878aSAndroid Build Coastguard Worker:ref:`target documentation<target-stm32f429i-disc1-stm32cube>` for instructions
24*61c4878aSAndroid Build Coastguard Workeron how to build the demo and try things out
25*61c4878aSAndroid Build Coastguard Worker
26*61c4878aSAndroid Build Coastguard WorkerIf you don't have a discovery board, there's a simulated device variation that
27*61c4878aSAndroid Build Coastguard Workeryou can run on your local machine with no additional hardware. Check out the
28*61c4878aSAndroid Build Coastguard Workersteps for trying this out :ref:`here<target-host-device-simulator>`.
29*61c4878aSAndroid Build Coastguard Worker
30*61c4878aSAndroid Build Coastguard Worker--------------
31*61c4878aSAndroid Build Coastguard WorkerTarget Bringup
32*61c4878aSAndroid Build Coastguard Worker--------------
33*61c4878aSAndroid Build Coastguard WorkerBringing up a new device is as easy as 1-2-3! (Kidding, this is a work in
34*61c4878aSAndroid Build Coastguard Workerprogress)
35*61c4878aSAndroid Build Coastguard Worker
36*61c4878aSAndroid Build Coastguard Worker#. **Configure the build.** How exactly to do this depends on the build
37*61c4878aSAndroid Build Coastguard Worker   system.
38*61c4878aSAndroid Build Coastguard Worker
39*61c4878aSAndroid Build Coastguard Worker   *  **GN**: Create a ``pw_system_target`` in your GN build. This is what will
40*61c4878aSAndroid Build Coastguard Worker      control the configuration of your target from a build system level. This
41*61c4878aSAndroid Build Coastguard Worker      includes which compiler will be used, what architecture flags will be
42*61c4878aSAndroid Build Coastguard Worker      used, which backends will be used, and more. A large quantity of
43*61c4878aSAndroid Build Coastguard Worker      configuration will be pre-set to work with pw_system after you select the
44*61c4878aSAndroid Build Coastguard Worker      CPU and scheduler your target will use, but your target will likely need
45*61c4878aSAndroid Build Coastguard Worker      to set a few other things to get to a fully working state.
46*61c4878aSAndroid Build Coastguard Worker
47*61c4878aSAndroid Build Coastguard Worker   *  **Bazel**: Add a dependency on ``@pigweed//pw_system`` to your ``cc_binary``,
48*61c4878aSAndroid Build Coastguard Worker      and set one `label flag
49*61c4878aSAndroid Build Coastguard Worker      <https://bazel.build/extending/config#label-typed-build-settings>`__,
50*61c4878aSAndroid Build Coastguard Worker      ``@pigweed//pw_system:extra_platform_libs``. Point it to a ``cc_library``
51*61c4878aSAndroid Build Coastguard Worker      containing any platform-dependent dependencies of your ``pw_system``
52*61c4878aSAndroid Build Coastguard Worker      instantiation. In particular, this should include platform-specific
53*61c4878aSAndroid Build Coastguard Worker      initialization code (see next point) and the custom
54*61c4878aSAndroid Build Coastguard Worker      :ref:`pw_linker_script <module-pw_build-bazel-pw_linker_script>` (if any)
55*61c4878aSAndroid Build Coastguard Worker      to use when linking the ``pw_system`` binary.
56*61c4878aSAndroid Build Coastguard Worker
57*61c4878aSAndroid Build Coastguard Worker      .. warning::
58*61c4878aSAndroid Build Coastguard Worker
59*61c4878aSAndroid Build Coastguard Worker         You should always add the ``alwayslink = 1`` attribute to the target
60*61c4878aSAndroid Build Coastguard Worker         you point ``@pigweed//pw_system:extra_platform_libs`` to. This is
61*61c4878aSAndroid Build Coastguard Worker         because Bazel `links files in topological order
62*61c4878aSAndroid Build Coastguard Worker         <https://stackoverflow.com/a/73006724/24291280>`__, but the
63*61c4878aSAndroid Build Coastguard Worker         dependencies from ``extra_platform_libs`` may appear before the
64*61c4878aSAndroid Build Coastguard Worker         objects they are used in. The ``alwayslink = 1`` will prevent the
65*61c4878aSAndroid Build Coastguard Worker         linker from erroneously garbage-collecting them.
66*61c4878aSAndroid Build Coastguard Worker
67*61c4878aSAndroid Build Coastguard Worker#. **Write target-specific initialization.**
68*61c4878aSAndroid Build Coastguard Worker   Most embedded devices require a linker script, manual initialization of
69*61c4878aSAndroid Build Coastguard Worker   memory, and some clock initialization. pw_system leaves this to users to
70*61c4878aSAndroid Build Coastguard Worker   implement as the exact initialization sequence can be very project-specific.
71*61c4878aSAndroid Build Coastguard Worker   All that's required is that after early memory initialization and clock
72*61c4878aSAndroid Build Coastguard Worker   configuration is complete, your target initialization should call
73*61c4878aSAndroid Build Coastguard Worker   ``pw::system::Init()`` and then start the RTOS scheduler (e.g.
74*61c4878aSAndroid Build Coastguard Worker   ``vTaskStartScheduler()``).
75*61c4878aSAndroid Build Coastguard Worker#. **Implement ``pw::system::UserAppInit()`` in your application.**
76*61c4878aSAndroid Build Coastguard Worker   This is where most of your project's application-specific logic goes. This
77*61c4878aSAndroid Build Coastguard Worker   could be starting threads, registering RPC services, turning on Bluetooth,
78*61c4878aSAndroid Build Coastguard Worker   or more. In ``UserAppInit()``, the RTOS will be running so you're free to use
79*61c4878aSAndroid Build Coastguard Worker   OS primitives and use features that rely on threading (e.g. RPC, logging).
80*61c4878aSAndroid Build Coastguard Worker
81*61c4878aSAndroid Build Coastguard WorkerPigweed's ``stm32f429i_disc1_stm32cube`` target demonstrates what's required by
82*61c4878aSAndroid Build Coastguard Workerthe first two steps. The third step is where you get to decide how to turn your
83*61c4878aSAndroid Build Coastguard Workernew platform into a project that does something cool! It might be as simple as
84*61c4878aSAndroid Build Coastguard Workera blinking LED, or something more complex like a Bluetooth device that brews you
85*61c4878aSAndroid Build Coastguard Workera cup of coffee whenever ``pw watch`` kicks off a new build.
86*61c4878aSAndroid Build Coastguard Worker
87*61c4878aSAndroid Build Coastguard Worker.. note::
88*61c4878aSAndroid Build Coastguard Worker  Because of the nature of the hard-coded conditions in ``pw_system_target``,
89*61c4878aSAndroid Build Coastguard Worker  you may find that some options are missing for various RTOSes and
90*61c4878aSAndroid Build Coastguard Worker  architectures. The design of the GN integration is still a work-in-progress
91*61c4878aSAndroid Build Coastguard Worker  to improve the scalability of this, but in the meantime the Pigweed team
92*61c4878aSAndroid Build Coastguard Worker  welcomes contributions to expand the breadth of RTOSes and architectures
93*61c4878aSAndroid Build Coastguard Worker  supported as ``pw_system_target``\s.
94*61c4878aSAndroid Build Coastguard Worker
95*61c4878aSAndroid Build Coastguard WorkerGN Target Toolchain Template
96*61c4878aSAndroid Build Coastguard Worker============================
97*61c4878aSAndroid Build Coastguard WorkerThis module includes a target toolchain template called ``pw_system_target``
98*61c4878aSAndroid Build Coastguard Workerthat reduces the amount of work required to declare a target toolchain with
99*61c4878aSAndroid Build Coastguard Workerpre-selected backends for pw_log, pw_assert, pw_malloc, pw_thread, and more.
100*61c4878aSAndroid Build Coastguard WorkerThe configurability and extensibility of this template is relatively limited,
101*61c4878aSAndroid Build Coastguard Workeras this template serves as a "one-size-fits-all" starting point rather than
102*61c4878aSAndroid Build Coastguard Workerbeing foundational infrastructure.
103*61c4878aSAndroid Build Coastguard Worker
104*61c4878aSAndroid Build Coastguard Worker.. code-block::
105*61c4878aSAndroid Build Coastguard Worker
106*61c4878aSAndroid Build Coastguard Worker   # Declare a toolchain with suggested, compiler, compiler flags, and default
107*61c4878aSAndroid Build Coastguard Worker   # backends.
108*61c4878aSAndroid Build Coastguard Worker   pw_system_target("stm32f429i_disc1_stm32cube_size_optimized") {
109*61c4878aSAndroid Build Coastguard Worker     # These options drive the logic for automatic configuration by this
110*61c4878aSAndroid Build Coastguard Worker     # template.
111*61c4878aSAndroid Build Coastguard Worker     cpu = PW_SYSTEM_CPU.CORTEX_M4F
112*61c4878aSAndroid Build Coastguard Worker     scheduler = PW_SYSTEM_SCHEDULER.FREERTOS
113*61c4878aSAndroid Build Coastguard Worker
114*61c4878aSAndroid Build Coastguard Worker     # Optionally, override pw_system's defaults to build with clang.
115*61c4878aSAndroid Build Coastguard Worker     system_toolchain = pw_toolchain_arm_clang
116*61c4878aSAndroid Build Coastguard Worker
117*61c4878aSAndroid Build Coastguard Worker     # The pre_init source set provides things like the interrupt vector table,
118*61c4878aSAndroid Build Coastguard Worker     # pre-main init, and provision of FreeRTOS hooks.
119*61c4878aSAndroid Build Coastguard Worker     link_deps = [ "$dir_pigweed/targets/stm32f429i_disc1_stm32cube:pre_init" ]
120*61c4878aSAndroid Build Coastguard Worker
121*61c4878aSAndroid Build Coastguard Worker     # These are hardware-specific options that set up this particular board.
122*61c4878aSAndroid Build Coastguard Worker     # These are declared in ``declare_args()`` blocks throughout Pigweed. Any
123*61c4878aSAndroid Build Coastguard Worker     # build arguments set by the user will be overridden by these settings.
124*61c4878aSAndroid Build Coastguard Worker     build_args = {
125*61c4878aSAndroid Build Coastguard Worker       pw_third_party_freertos_CONFIG = "$dir_pigweed/targets/stm32f429i_disc1_stm32cube:stm32f4xx_freertos_config"
126*61c4878aSAndroid Build Coastguard Worker       pw_third_party_freertos_PORT = "$dir_pw_third_party/freertos:arm_cm4f"
127*61c4878aSAndroid Build Coastguard Worker       pw_sys_io_BACKEND = dir_pw_sys_io_stm32cube
128*61c4878aSAndroid Build Coastguard Worker       dir_pw_third_party_stm32cube = dir_pw_third_party_stm32cube_f4
129*61c4878aSAndroid Build Coastguard Worker       pw_third_party_stm32cube_PRODUCT = "STM32F429xx"
130*61c4878aSAndroid Build Coastguard Worker       pw_third_party_stm32cube_CONFIG =
131*61c4878aSAndroid Build Coastguard Worker           "//targets/stm32f429i_disc1_stm32cube:stm32f4xx_hal_config"
132*61c4878aSAndroid Build Coastguard Worker       pw_third_party_stm32cube_CORE_INIT = ""
133*61c4878aSAndroid Build Coastguard Worker       pw_boot_cortex_m_LINK_CONFIG_DEFINES = [
134*61c4878aSAndroid Build Coastguard Worker         "PW_BOOT_FLASH_BEGIN=0x08000200",
135*61c4878aSAndroid Build Coastguard Worker         "PW_BOOT_FLASH_SIZE=2048K",
136*61c4878aSAndroid Build Coastguard Worker         "PW_BOOT_HEAP_SIZE=7K",
137*61c4878aSAndroid Build Coastguard Worker         "PW_BOOT_MIN_STACK_SIZE=1K",
138*61c4878aSAndroid Build Coastguard Worker         "PW_BOOT_RAM_BEGIN=0x20000000",
139*61c4878aSAndroid Build Coastguard Worker         "PW_BOOT_RAM_SIZE=192K",
140*61c4878aSAndroid Build Coastguard Worker         "PW_BOOT_VECTOR_TABLE_BEGIN=0x08000000",
141*61c4878aSAndroid Build Coastguard Worker         "PW_BOOT_VECTOR_TABLE_SIZE=512",
142*61c4878aSAndroid Build Coastguard Worker       ]
143*61c4878aSAndroid Build Coastguard Worker     }
144*61c4878aSAndroid Build Coastguard Worker   }
145*61c4878aSAndroid Build Coastguard Worker
146*61c4878aSAndroid Build Coastguard Worker   # Example for the Emcraft SmartFusion2 system-on-module
147*61c4878aSAndroid Build Coastguard Worker   pw_system_target("emcraft_sf2_som_size_optimized") {
148*61c4878aSAndroid Build Coastguard Worker     cpu = PW_SYSTEM_CPU.CORTEX_M3
149*61c4878aSAndroid Build Coastguard Worker     scheduler = PW_SYSTEM_SCHEDULER.FREERTOS
150*61c4878aSAndroid Build Coastguard Worker
151*61c4878aSAndroid Build Coastguard Worker     link_deps = [ "$dir_pigweed/targets/emcraft_sf2_som:pre_init" ]
152*61c4878aSAndroid Build Coastguard Worker     build_args = {
153*61c4878aSAndroid Build Coastguard Worker       pw_log_BACKEND = dir_pw_log_basic #dir_pw_log_tokenized
154*61c4878aSAndroid Build Coastguard Worker       pw_log_tokenized_HANDLER_BACKEND = "//pw_system:log"
155*61c4878aSAndroid Build Coastguard Worker       pw_third_party_freertos_CONFIG = "$dir_pigweed/targets/emcraft_sf2_som:sf2_freertos_config"
156*61c4878aSAndroid Build Coastguard Worker       pw_third_party_freertos_PORT = "$dir_pw_third_party/freertos:arm_cm3"
157*61c4878aSAndroid Build Coastguard Worker       pw_sys_io_BACKEND = dir_pw_sys_io_emcraft_sf2
158*61c4878aSAndroid Build Coastguard Worker       dir_pw_third_party_smartfusion_mss = dir_pw_third_party_smartfusion_mss_exported
159*61c4878aSAndroid Build Coastguard Worker       pw_third_party_stm32cube_CONFIG =
160*61c4878aSAndroid Build Coastguard Worker           "//targets/emcraft_sf2_som:sf2_mss_hal_config"
161*61c4878aSAndroid Build Coastguard Worker       pw_third_party_stm32cube_CORE_INIT = ""
162*61c4878aSAndroid Build Coastguard Worker       pw_boot_cortex_m_LINK_CONFIG_DEFINES = [
163*61c4878aSAndroid Build Coastguard Worker         "PW_BOOT_FLASH_BEGIN=0x00000200",
164*61c4878aSAndroid Build Coastguard Worker         "PW_BOOT_FLASH_SIZE=200K",
165*61c4878aSAndroid Build Coastguard Worker
166*61c4878aSAndroid Build Coastguard Worker         # TODO: b/235348465 - Currently "pw_tokenizer/detokenize_test" requires at
167*61c4878aSAndroid Build Coastguard Worker         # least 6K bytes in heap when using pw_malloc:bucket_block_allocator.
168*61c4878aSAndroid Build Coastguard Worker         # The heap size required for tests should be investigated.
169*61c4878aSAndroid Build Coastguard Worker         "PW_BOOT_HEAP_SIZE=7K",
170*61c4878aSAndroid Build Coastguard Worker         "PW_BOOT_MIN_STACK_SIZE=1K",
171*61c4878aSAndroid Build Coastguard Worker         "PW_BOOT_RAM_BEGIN=0x20000000",
172*61c4878aSAndroid Build Coastguard Worker         "PW_BOOT_RAM_SIZE=64K",
173*61c4878aSAndroid Build Coastguard Worker         "PW_BOOT_VECTOR_TABLE_BEGIN=0x00000000",
174*61c4878aSAndroid Build Coastguard Worker         "PW_BOOT_VECTOR_TABLE_SIZE=512",
175*61c4878aSAndroid Build Coastguard Worker       ]
176*61c4878aSAndroid Build Coastguard Worker     }
177*61c4878aSAndroid Build Coastguard Worker   }
178*61c4878aSAndroid Build Coastguard Worker
179*61c4878aSAndroid Build Coastguard Worker-------
180*61c4878aSAndroid Build Coastguard WorkerMetrics
181*61c4878aSAndroid Build Coastguard Worker-------
182*61c4878aSAndroid Build Coastguard WorkerThe log backend is tracking metrics to illustrate how to use pw_metric and
183*61c4878aSAndroid Build Coastguard Workerretrieve them using `Device.get_and_log_metrics()`.
184*61c4878aSAndroid Build Coastguard Worker
185*61c4878aSAndroid Build Coastguard Worker-------
186*61c4878aSAndroid Build Coastguard WorkerConsole
187*61c4878aSAndroid Build Coastguard Worker-------
188*61c4878aSAndroid Build Coastguard WorkerThe ``pw-system-console`` can be used to interact with the targets.
189*61c4878aSAndroid Build Coastguard WorkerSee :ref:`module-pw_system-cli` for detailed CLI usage information.
190*61c4878aSAndroid Build Coastguard Worker
191*61c4878aSAndroid Build Coastguard Worker.. toctree::
192*61c4878aSAndroid Build Coastguard Worker   :hidden:
193*61c4878aSAndroid Build Coastguard Worker   :maxdepth: 1
194*61c4878aSAndroid Build Coastguard Worker
195*61c4878aSAndroid Build Coastguard Worker   cli
196*61c4878aSAndroid Build Coastguard Worker
197*61c4878aSAndroid Build Coastguard Worker-------------------
198*61c4878aSAndroid Build Coastguard WorkerMulti-endpoint mode
199*61c4878aSAndroid Build Coastguard Worker-------------------
200*61c4878aSAndroid Build Coastguard Worker
201*61c4878aSAndroid Build Coastguard WorkerThe default configuration serves all its traffic with the same
202*61c4878aSAndroid Build Coastguard Workerchannel ID and RPC address. There is an alternative mode that assigns a separate
203*61c4878aSAndroid Build Coastguard Workerchannel ID and address for logging. This can be useful if you want to separate logging and primary RPC to
204*61c4878aSAndroid Build Coastguard Worker``pw_system`` among multiple clients.
205*61c4878aSAndroid Build Coastguard Worker
206*61c4878aSAndroid Build Coastguard WorkerTo use this mode, add the following to ``gn args out``:
207*61c4878aSAndroid Build Coastguard Worker
208*61c4878aSAndroid Build Coastguard Worker.. code-block::
209*61c4878aSAndroid Build Coastguard Worker
210*61c4878aSAndroid Build Coastguard Worker   pw_system_USE_MULTI_ENDPOINT_CONFIG = true
211*61c4878aSAndroid Build Coastguard Worker
212*61c4878aSAndroid Build Coastguard WorkerThe settings for the channel ID and address can be found in the target
213*61c4878aSAndroid Build Coastguard Worker``//pw_system:multi_endpoint_rpc_overrides``.
214*61c4878aSAndroid Build Coastguard Worker
215*61c4878aSAndroid Build Coastguard Worker.. _module-pw_system-logchannel:
216*61c4878aSAndroid Build Coastguard Worker
217*61c4878aSAndroid Build Coastguard Worker---------------------
218*61c4878aSAndroid Build Coastguard WorkerExtra logging channel
219*61c4878aSAndroid Build Coastguard Worker---------------------
220*61c4878aSAndroid Build Coastguard WorkerIn multi-processor devices, logs are typically forwarded to a primary
221*61c4878aSAndroid Build Coastguard Workerapplication-class core. By default, ``pw_system`` assumes a simpler device
222*61c4878aSAndroid Build Coastguard Workerarchitecture where a single processor is communicating with an external host
223*61c4878aSAndroid Build Coastguard Workersystem (e.g. a Linux workstation) for developer debugging. This means that
224*61c4878aSAndroid Build Coastguard Workerlogging and RPCs are expected to coexist on the same channel. It is possible
225*61c4878aSAndroid Build Coastguard Workerto redirect the logs to a different RPC channel output by configuring
226*61c4878aSAndroid Build Coastguard Worker``PW_SYSTEM_LOGGING_CHANNEL_ID`` to a different channel ID, but this would
227*61c4878aSAndroid Build Coastguard Workerstill mean that logs would inaccessible from either the application-class
228*61c4878aSAndroid Build Coastguard Workerprocessor, or the host system.
229*61c4878aSAndroid Build Coastguard Worker
230*61c4878aSAndroid Build Coastguard WorkerThe logging multisink can be leveraged to address this completely by forwarding
231*61c4878aSAndroid Build Coastguard Workera copy of the logs to the application-class core without impacting the behavior
232*61c4878aSAndroid Build Coastguard Workerof the debug communication channel. This allows ``pw-system-console`` work as
233*61c4878aSAndroid Build Coastguard Workerusual, while also ensuring logs are available from the larger application-class
234*61c4878aSAndroid Build Coastguard Workerprocessor. Additionally, this allows the debug channel to easily be disabled in
235*61c4878aSAndroid Build Coastguard Workerproduction environments while maintaining the log forwarding path through the
236*61c4878aSAndroid Build Coastguard Workerlarger processor.
237*61c4878aSAndroid Build Coastguard Worker
238*61c4878aSAndroid Build Coastguard WorkerAn example configuration is provided below:
239*61c4878aSAndroid Build Coastguard Worker
240*61c4878aSAndroid Build Coastguard Worker.. code-block::
241*61c4878aSAndroid Build Coastguard Worker
242*61c4878aSAndroid Build Coastguard Worker   config("extra_logging_channel") {
243*61c4878aSAndroid Build Coastguard Worker     defines = [ "PW_SYSTEM_EXTRA_LOGGING_CHANNEL_ID=2" ]
244*61c4878aSAndroid Build Coastguard Worker   }
245*61c4878aSAndroid Build Coastguard Worker
246*61c4878aSAndroid Build Coastguard Worker   pw_system_target("my_system") {
247*61c4878aSAndroid Build Coastguard Worker     global_configs = [ ":extra_logging_channel" ]
248*61c4878aSAndroid Build Coastguard Worker   }
249*61c4878aSAndroid Build Coastguard Worker
250*61c4878aSAndroid Build Coastguard WorkerOnce you have configured pw_system as shown in the example above, you will
251*61c4878aSAndroid Build Coastguard Workerstill need to define an RPC channel for the channel ID that you selected so
252*61c4878aSAndroid Build Coastguard Workerthe logs can be routed to the appropriate destination.
253*61c4878aSAndroid Build Coastguard Worker
254*61c4878aSAndroid Build Coastguard Worker---------------
255*61c4878aSAndroid Build Coastguard Workerpw_system:async
256*61c4878aSAndroid Build Coastguard Worker---------------
257*61c4878aSAndroid Build Coastguard Worker``pw_system:async`` is a new version of ``pw_system`` based on
258*61c4878aSAndroid Build Coastguard Worker:ref:`module-pw_async2` and :ref:`module-pw_channel`. It provides an async
259*61c4878aSAndroid Build Coastguard Workerdispatcher, which may be used to run async tasks, including with C++20
260*61c4878aSAndroid Build Coastguard Workercoroutines.
261*61c4878aSAndroid Build Coastguard Worker
262*61c4878aSAndroid Build Coastguard WorkerTo use ``pw_system:async``, add a dependency on ``@pigweed//pw_system:async`` in
263*61c4878aSAndroid Build Coastguard WorkerBazel. Then, from your main function, invoke :cpp:func:`pw::SystemStart` with a
264*61c4878aSAndroid Build Coastguard Worker:cpp:type:`pw::channel::ByteReaderWriter` to use for IO.
265*61c4878aSAndroid Build Coastguard Worker
266*61c4878aSAndroid Build Coastguard Worker.. literalinclude:: system_async_test.cc
267*61c4878aSAndroid Build Coastguard Worker   :language: cpp
268*61c4878aSAndroid Build Coastguard Worker   :linenos:
269*61c4878aSAndroid Build Coastguard Worker   :start-after: [pw_system-async-example-main]
270*61c4878aSAndroid Build Coastguard Worker   :end-before: [pw_system-async-example-main]
271*61c4878aSAndroid Build Coastguard Worker
272*61c4878aSAndroid Build Coastguard Workerpw_system:async Linux example
273*61c4878aSAndroid Build Coastguard Worker=============================
274*61c4878aSAndroid Build Coastguard Worker``//pw_system/system_async_host_simulator_example`` is an example app for
275*61c4878aSAndroid Build Coastguard Workerrunning ``pw_system:async`` on a Linux host. Running the example requires two
276*61c4878aSAndroid Build Coastguard Workerterminals. In the first terminal, start the ``pw_system:async`` instance:
277*61c4878aSAndroid Build Coastguard Worker
278*61c4878aSAndroid Build Coastguard Worker.. code-block:: sh
279*61c4878aSAndroid Build Coastguard Worker
280*61c4878aSAndroid Build Coastguard Worker   bazelisk run //pw_system/system_async_host_simulator_example
281*61c4878aSAndroid Build Coastguard Worker
282*61c4878aSAndroid Build Coastguard WorkerThat will wait for a TCP connection from the ``pw_system`` console. To connect
283*61c4878aSAndroid Build Coastguard Workerto it from the console, run the following:
284*61c4878aSAndroid Build Coastguard Worker
285*61c4878aSAndroid Build Coastguard Worker.. code-block:: sh
286*61c4878aSAndroid Build Coastguard Worker
287*61c4878aSAndroid Build Coastguard Worker   bazelisk run //pw_system/py:pw_system_console -- -s 127.0.0.1:33000
288*61c4878aSAndroid Build Coastguard Worker
289*61c4878aSAndroid Build Coastguard WorkerDebugging pw_system_console with VSCode
290*61c4878aSAndroid Build Coastguard Worker---------------------------------------
291*61c4878aSAndroid Build Coastguard WorkerWhen running a python script through bazel, python is run inside a bazel sandbox,
292*61c4878aSAndroid Build Coastguard Workerwhich can make re-creating this environment difficult when running the script
293*61c4878aSAndroid Build Coastguard Workeroutside of bazel to attach a debugger.
294*61c4878aSAndroid Build Coastguard Worker
295*61c4878aSAndroid Build Coastguard WorkerTo simplify debugging setup, Pigweed makes available the `debugpy <https://github.com/microsoft/debugpy>`__
296*61c4878aSAndroid Build Coastguard Workerpackage to ease attaching ``pw_system_console``.
297*61c4878aSAndroid Build Coastguard Worker
298*61c4878aSAndroid Build Coastguard WorkerFirst configure VSCode to add the following to the configuration section of ``launch.json``.
299*61c4878aSAndroid Build Coastguard WorkerThis file can be automatically opened by selecting ``Run -> Open Configurations```, or
300*61c4878aSAndroid Build Coastguard Worker``Run -> Add Configuration`` if there is no existing ``launch.json``.
301*61c4878aSAndroid Build Coastguard Worker
302*61c4878aSAndroid Build Coastguard Worker.. TODO: b/372079357 - can this be automated by the VSCode plugin?
303*61c4878aSAndroid Build Coastguard Worker.. code-block:: json
304*61c4878aSAndroid Build Coastguard Worker
305*61c4878aSAndroid Build Coastguard Worker   "configurations": [
306*61c4878aSAndroid Build Coastguard Worker   {
307*61c4878aSAndroid Build Coastguard Worker     "name": "Python Debugger: Remote Attach",
308*61c4878aSAndroid Build Coastguard Worker     "type": "debugpy",
309*61c4878aSAndroid Build Coastguard Worker     "request": "attach",
310*61c4878aSAndroid Build Coastguard Worker     "connect": {
311*61c4878aSAndroid Build Coastguard Worker       "host": "localhost",
312*61c4878aSAndroid Build Coastguard Worker       "port": 5678
313*61c4878aSAndroid Build Coastguard Worker     },
314*61c4878aSAndroid Build Coastguard Worker     "pathMappings": [
315*61c4878aSAndroid Build Coastguard Worker       {
316*61c4878aSAndroid Build Coastguard Worker         "localRoot": "${workspaceFolder}",
317*61c4878aSAndroid Build Coastguard Worker         "remoteRoot": "."
318*61c4878aSAndroid Build Coastguard Worker       }
319*61c4878aSAndroid Build Coastguard Worker     ]
320*61c4878aSAndroid Build Coastguard Worker   }
321*61c4878aSAndroid Build Coastguard Worker
322*61c4878aSAndroid Build Coastguard Worker  ]
323*61c4878aSAndroid Build Coastguard Worker
324*61c4878aSAndroid Build Coastguard WorkerNext, run the console through bazel, adding the argument(s) ``--debugger-listen`` and optionally
325*61c4878aSAndroid Build Coastguard Worker``--debugger-wait-for-client`` to pause the console until the debugger attached.  For example:
326*61c4878aSAndroid Build Coastguard Worker
327*61c4878aSAndroid Build Coastguard Worker.. code-block:: sh
328*61c4878aSAndroid Build Coastguard Worker
329*61c4878aSAndroid Build Coastguard Worker   bazelisk run //pw_system/py:pw_system_console -- --debugger-listen
330*61c4878aSAndroid Build Coastguard Worker
331*61c4878aSAndroid Build Coastguard WorkerOnce the console has been started, simply select ``Run -> Start Debugging`` and the VS code debugger
332*61c4878aSAndroid Build Coastguard Workerwill automatically attach to the running python console.
333*61c4878aSAndroid Build Coastguard Worker
334*61c4878aSAndroid Build Coastguard Worker
335*61c4878aSAndroid Build Coastguard WorkerAPI reference
336*61c4878aSAndroid Build Coastguard Worker=============
337*61c4878aSAndroid Build Coastguard Worker.. doxygenfunction:: pw::SystemStart(channel::ByteReaderWriter&)
338*61c4878aSAndroid Build Coastguard Worker.. doxygenfunction:: pw::System
339*61c4878aSAndroid Build Coastguard Worker.. doxygenclass:: pw::system::AsyncCore
340*61c4878aSAndroid Build Coastguard Worker   :members:
341