[roll third_party/pigweed] pw_emu: mock_emu: start listening before making the port available

There is a race condition in mock_emu because we are making the port
available (writing it to a file for clients to read) before marking
the socket as listening.

If the client gets to run after the port is written to the file but
before the TCP thread has a chance to run we would get this error:

======================================================================
ERROR: test_channel_stream (__main__.TestEmulator)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "../../pw_emu/py/tests/frontend_test.py", line 116, in test_channel_stream
    with self._emu.get_channel_stream('gdb') as _:
  File "/b/s/w/ir/x/w/co/pw_emu/py/pw_emu/frontend.py", line 256, in get_channel_stream
    return self._c().get_channel_stream(name, timeout)
  File "/b/s/w/ir/x/w/co/pw_emu/py/pw_emu/core.py", line 533, in get_channel_stream
    sock.connect((host, port))
ConnectionRefusedError: [Errno 111] Connection refused

The fix is to mark the socket for listening before writing the port to
the file.

Original-Bug: 306155313
Test: stress --cpu 512 and run frontend_test.py 100 times without failures
Original-Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/176856

https://pigweed.googlesource.com/pigweed/pigweed
third_party/pigweed Rolled-Commits: 2f1d686f9f82655..aba67f9a2a71cbc
Roller-URL: https://ci.chromium.org/b/8766876193791408049
GitWatcher: ignore
CQ-Do-Not-Cancel-Tryjobs: true
Change-Id: Ib1860385f3797bd0bd03a29d21f5663cab37fff0
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/experimental/+/176872
Bot-Commit: Pigweed Roller <pigweed-roller@pigweed-service-accounts.iam.gserviceaccount.com>
Commit-Queue: Pigweed Roller <pigweed-roller@pigweed-service-accounts.iam.gserviceaccount.com>
1 file changed
tree: 73d8c14a3aab8ac7951fb5bba2b7cdf25540865e
  1. applications/
  2. build_overrides/
  3. infra/
  4. pw_async_bench/
  5. pw_board_led/
  6. pw_board_led_arduino/
  7. pw_board_led_host/
  8. pw_board_led_mimxrt595_evk/
  9. pw_board_led_pico/
  10. pw_board_led_stm32cube/
  11. pw_board_led_stm32f429i_disc1/
  12. pw_board_led_stm32f769i_disc0/
  13. pw_digital_io_arduino/
  14. pw_digital_io_null/
  15. pw_digital_io_stm32cube/
  16. pw_display_driver/
  17. pw_display_driver_ili9341/
  18. pw_display_driver_imgui/
  19. pw_display_driver_mipi/
  20. pw_display_driver_null/
  21. pw_display_driver_st7735/
  22. pw_display_driver_st7789/
  23. pw_ft6236/
  24. pw_graphics/
  25. pw_i2c_rp2040/
  26. pw_mipi_dsi/
  27. pw_mipi_dsi_mcuxpresso/
  28. pw_pixel_pusher/
  29. pw_pixel_pusher_rp2040_pio/
  30. pw_spi_arduino/
  31. pw_spi_rp2040/
  32. pw_spi_stm32cube/
  33. pw_spin_delay/
  34. pw_spin_delay_arduino/
  35. pw_spin_delay_host/
  36. pw_spin_delay_mcuxpresso/
  37. pw_spin_delay_rp2040/
  38. pw_spin_delay_stm32cube/
  39. pw_spin_delay_stm32f429i_disc1/
  40. pw_spin_delay_stm32f769i_disc0/
  41. pw_sys_io_baremetal_stm32f769/
  42. targets/
  43. third_party/
  44. tools/
  45. .bazelignore
  46. .bazelrc
  47. .gitattributes
  48. .gitignore
  49. .gitmodules
  50. .gn
  51. activate.bat
  52. banner.txt
  53. bootstrap.bat
  54. bootstrap.sh
  55. BUILD.bazel
  56. BUILD.gn
  57. BUILDCONFIG.gn
  58. modules.gni
  59. navbar.md
  60. OWNERS
  61. pigweed.json
  62. README.md
  63. WORKSPACE
README.md

Pigweed Experimental

This repository contains experimental pigweed modules.

Repository setup

Clone this repo with --recursive to get all required submodules.

git clone --recursive https://pigweed.googlesource.com/pigweed/experimental

This will pull the Pigweed source repository into third_party/pigweed. If you already cloned but forgot to --recursive run git submodule update --init to pull all submodules.

pw_graphics

The //pw_graphics folder contains some libraries for drawing to an RGB565 framebuffer and displaying it on various platforms.

The demo applications that make use of these libraries are:

Build instructions

First time setup:

git clone --recursive https://pigweed.googlesource.com/pigweed/experimental
cd experimental
. ./bootstrap.sh
pw package install imgui
pw package install glfw
pw package install stm32cube_f4
pw package install pico_sdk

STM32F429-DISC1

Compile:

gn gen out --export-compile-commands --args="
  dir_pw_third_party_stm32cube_f4=\"$PW_PROJECT_ROOT/environment/packages/stm32cube_f4\"
"
ninja -C out

Flash:

openocd -f third_party/pigweed/targets/stm32f429i_disc1/py/stm32f429i_disc1_utils/openocd_stm32f4xx.cfg -c "program out/stm32f429i_disc1_stm32cube_debug/obj/applications/terminal_display/bin/terminal_demo.elf verify reset exit"

STM32F769-DISC0

First time setup:

pw package install stm32cube_f7

Compile:

gn gen out --export-compile-commands --args="
  dir_pw_third_party_stm32cube_f7=\"//environment/packages/stm32cube_f7\"
"
ninja -C out

Flash:

openocd -f targets/stm32f769i_disc0/py/stm32f769i_disc0_utils/openocd_stm32f7xx.cfg \
  -c "program out/stm32f769i_disc0_debug/obj/applications/blinky/bin/blinky.elf verify reset exit"

Linux, Windows or Mac

Compile:

gn gen out --export-compile-commands --args="
  dir_pw_third_party_imgui=\"$PW_PROJECT_ROOT/environment/packages/imgui\"
  dir_pw_third_party_glfw=\"$PW_PROJECT_ROOT/environment/packages/glfw\"
"
ninja -C out

Run:

out/host_debug/obj/applications/terminal_display/bin/terminal_demo

Raspberry Pi Pico Connected to an external SPI display

Working displays:

First time setup:

pw package install pico_sdk

Compile:

gn gen out --export-compile-commands --args='
  PICO_SRC_DIR="//environment/packages/pico_sdk"
'
ninja -C out

Flash:

  • Using a uf2 file:

    1. Reboot pico into BOOTSEL mode by holding the bootsel button on startup.
    2. Copy ./out/rp2040/obj/applications/terminal_display/terminal_demo.uf2 to your Pi Pico.
  • Using picotool:

    1. Reboot pico into BOOTSEL mode by holding the bootsel button on startup. Or try forcing a reboot with picotool:
    picotool reboot -f -u
    
    1. Flash the elf or uf2 file and reboot.
    picotool load ./out/rp2040/obj/applications/terminal_display/bin/terminal_demo.elf
    picotool reboot
    
  • Using a Pico Probe and openocd:

    This requires installing the Raspberry Pi foundation's OpenOCD fork for the Pico probe. More details including how to connect the two Pico boards is available at Raspberry Pi Pico and RP2040 - C/C++ Part 2: Debugging with VS Code

    Install RaspberryPi's OpenOCD Fork:

    git clone https://github.com/raspberrypi/openocd.git \
      --branch picoprobe \
      --depth=1 \
      --no-single-branch \
      openocd-picoprobe
    
    cd openocd-picoprobe
    
    ./bootstrap
    ./configure --enable-picoprobe --prefix=$HOME/apps/openocd --disable-werror
    make -j2
    make install
    

    Setup udev rules (Linux only):

    cat <<EOF > 49-picoprobe.rules
    SUBSYSTEMS=="usb", ATTRS{idVendor}=="2e8a", ATTRS{idProduct}=="0004", MODE:="0666"
    KERNEL=="ttyACM*", ATTRS{idVendor}=="2e8a", ATTRS{idProduct}=="0004", MODE:="0666"
    EOF
    sudo cp 49-picoprobe.rules /usr/lib/udev/rules.d/49-picoprobe.rules
    sudo udevadm control --reload-rules
    

    Flash the Pico:

    ~/apps/openocd/bin/openocd -f ~/apps/openocd/share/openocd/scripts/interface/picoprobe.cfg -f ~/apps/openocd/share/openocd/scripts/target/rp2040.cfg -c 'program out/rp2040/obj/applications/terminal_display/bin/terminal_demo.elf verify reset exit'
    

    Launching gdb*

    ~/apps/openocd/bin/openocd -f ~/apps/openocd/share/openocd/scripts/interface/picoprobe.cfg -f ~/apps/openocd/share/openocd/scripts/target/rp2040.cfg
    
    gdb-multiarch -ex "target remote :3333" -ex "set print pretty on" out/rp2040/obj/applications/terminal_display/bin/terminal_demo.elf
    

    arm-none-eabi-gdb can be used in place of gdb-multiarch above.

MIMXRT595-EVK Connected to an external MIPI display

Setup NXP SDK:

  1. Build a NXP SDK
  2. Download SDK
  3. Extract SDK's zip file to //environment/SDK_2_12_1_EVK-MIMXRT595

Compile:

gn gen out --export-compile-commands --args="
  pw_MIMXRT595_EVK_SDK=\"//environment/SDK_2_12_1_EVK-MIMXRT595\"
  pw_target_mimxrt595_evk_MANIFEST=\"//environment/SDK_2_12_1_EVK-MIMXRT595/EVK-MIMXRT595_manifest_v3_10.xml\"
  pw_third_party_mcuxpresso_SDK=\"//targets/mimxrt595_evk:mimxrt595_sdk\"
"

ninja -C out

Flash the MIMXRT595-EVK:

Follow the instructions to flash the MIMXRT595-EVK with the SEGGER J-Link firmware and using arm-none-eabi-gdb at https://pigweed.dev/targets/mimxrt595_evk/target_docs.html#running-and-debugging.

Teensy 4.1

https://www.pjrc.com/teensy/loader_cli.html

brew install teensy_loader_cli
OBJCOPY=/Applications/Arduino.app/Contents/Java/hardware/tools/avr/bin/avr-objcopy
INFILE=out/arduino_debug/obj/applications/terminal_display/bin/terminal_demo.elf
OUTFILE=foo.hex
$OBJCOPY -O ihex -R .eeprom -R .fuse -R .lock -R .signature $INFILE $OUTFILE
teensy_loader_cli --mcu=TEENSY41 -w -v $OUTFILE