Esp32 Rust Board on Macos M-chip in Docker
Tags: macos, rust, esp32, dcoker
Published: 2024-05-09 08:21:23

## 0. Env

-   [esp-rust-board](https://github.com/esp-rs/esp-rust-board)
-   macOS 14.0
-   MacStudio(M1Ultra), Mac mimi(M2)
-   Docker version 26.0.0, build 2ae903e
-   JetBrains Gateway (beta 2024.1.1)

搭建用于烧写 [esp-rust-book](https://narukara.github.io/std-training-zh-cn/01_intro.html) 演示代码的环境。本例采用 container 里编译构建，本机进行烧录的模式。

## 1. 创建 docker container

clone [esp/std-traning](https://github.com/esp-rs/std-training.git) 到本地，将`.devcontainer.json` 和 `Dockerfile` 移到项目根目录

在 GateWay 中选择 Create Dev Container 和刚刚复制出来的 `.devcontainer.json`


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a7tis1nisbrmuorooq08.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o58yyv76eet2xylldapz.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jh88bqhzy9rhlyfxjjzw.png)



（第一次构建的话，内容可能不大一样）

continue 之后就慢慢等待下载了



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4b78bht8ywf7zldhiwhn.png)


大功告成！



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e4168rii1n0qfbxk7717.png)



## 2. Build

以 [intro/hardware-check](https://narukara.github.io/std-training-zh-cn/02_4_hello_board.html) 为例，在容器内进入该目录进行构建

```shell
esp@d3b7b1f51dc6:/workspace$ cd intro/hardware-check/
esp@d3b7b1f51dc6:/workspace/intro/hardware-check$ cargo build
    Updating crates.io index
  Downloaded atomic-waker v1.1.2
  Downloaded bitflags v1.3.2
  Downloaded anyhow v1.0.75
...
   Compiling hardware-check v0.1.0 (/workspace/intro/hardware-check)
    Finished dev [optimized + debuginfo] target(s) in 1m 11s
```

(真是难为我这 8G 的 Mac mini了)



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hk2rxrum5zk2kg62xbuo.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n71jyx6qkbwjfv4ql3xs.png)




## 3. Flash

构建成功后生成最终的可执行文件在 `target/riscv32imc-esp-espidf/debug/hardware-check`

在本机上连上板子，通过 [espflash](https://github.com/esp-rs/espflash/blob/main/espflash/README.md) 将目标文件烧录进板子

看到 `I (8438) wifi:connected with ${cfg.toml中的路由器名称}` 和 `I (11458) hardware_check: Hello, world!` 就说明烧录成功并且硬件检测通过了

```shell
$ espflash flash target/riscv32imc-esp-espidf/debug/hardware-check --monitor
[2024-05-07T15:05:05Z INFO ] Detected 2 serial ports
[2024-05-07T15:05:05Z INFO ] Ports which match a known common dev board are highlighted
[2024-05-07T15:05:05Z INFO ] Please select a port
✔ Remember this serial port for future use? · no
[2024-05-07T15:05:08Z INFO ] Serial port: '/dev/tty.usbmodem111401'
[2024-05-07T15:05:08Z INFO ] Connecting...
​
[2024-05-07T15:05:08Z INFO ] Using flash stub
Chip type:         esp32c3 (revision v0.4)
Crystal frequency: 40 MHz
Flash size:        4MB
Features:          WiFi, BLE
MAC address:       40:4c:ca:8c:9e:28
App/part. size:    798,384/4,128,768 bytes, 19.34%
[00:00:00] [========================================]      13/13      0x0               
[00:00:00] [========================================]       1/1       0x8000           
[00:00:06] [========================================]     481/481     0x10000                                                                               [2024-05-07T15:05:17Z INFO ] Flashing has completed!
Commands:
    CTRL+R    Reset chip
    CTRL+C    Exit
​
ESP-ROM:esp32c3-api1-20210207
Build:Feb  7 2021
rst:0x15 (USB_UART_CHIP_RESET),boot:0xc (SPI_FAST_FLASH_BOOT)
Saved PC:0x40380638
0x40380638 - rmt_driver_isr_default
    at /home/esp/.espressif/frameworks/esp-idf/components/driver/deprecated/rmt_legacy.c:832
SPIWP:0xee
mode:DIO, clock div:2
load:0x3fcd5820,len:0x1714
load:0x403cc710,len:0x968
load:0x403ce710,len:0x2f9c
entry 0x403cc710
I (24) boot: ESP-IDF v5.1.2-342-gbcf1645e44 2nd stage bootloader
I (24) boot: compile time Dec 12 2023 10:50:58
I (25) boot: chip revision: v0.4
I (29) boot.esp32c3: SPI Speed      : 40MHz
I (34) boot.esp32c3: SPI Mode       : DIO
I (38) boot.esp32c3: SPI Flash Size : 4MB
I (43) boot: Enabling RNG early entropy source...
I (48) boot: Partition Table:
I (52) boot: ## Label            Usage          Type ST Offset   Length
I (59) boot:  0 nvs              WiFi data        01 02 00009000 00006000
I (67) boot:  1 phy_init         RF data          01 01 0000f000 00001000
I (74) boot:  2 factory          factory app      00 00 00010000 003f0000
I (82) boot: End of partition table
I (86) esp_image: segment 0: paddr=00010020 vaddr=3c090020 size=21448h (136264) map
I (124) esp_image: segment 1: paddr=00031470 vaddr=3fc90000 size=02974h ( 10612) load
I (127) esp_image: segment 2: paddr=00033dec vaddr=40380000 size=0c22ch ( 49708) load
I (143) esp_image: segment 3: paddr=00040020 vaddr=42000020 size=8f24ch (586316) map
I (273) esp_image: segment 4: paddr=000cf274 vaddr=4038c22c size=03c18h ( 15384) load
I (282) boot: Loaded app from partition at offset 0x10000
I (282) boot: Disabling RNG early entropy source...
I (294) cpu_start: Unicore app
I (294) cpu_start: Pro cpu up.
I (303) cpu_start: Pro cpu start user code
I (303) cpu_start: cpu freq: 160000000 Hz
I (303) cpu_start: Application information:
I (306) cpu_start: Project name:     libespidf
I (311) cpu_start: App version:      1
I (315) cpu_start: Compile time:     May  7 2024 14:32:28
I (322) cpu_start: ELF file SHA256:  0000000000000000...
I (327) cpu_start: ESP-IDF:          v5.1.2-dirty
I (333) cpu_start: Min chip rev:     v0.3
I (338) cpu_start: Max chip rev:     v0.99 
I (342) cpu_start: Chip rev:         v0.4
I (347) heap_init: Initializing. RAM available for dynamic allocation:
I (354) heap_init: At 3FC969B0 len 00029650 (165 KiB): DRAM
I (361) heap_init: At 3FCC0000 len 0001C710 (113 KiB): DRAM/RETENTION
I (368) heap_init: At 3FCDC710 len 00002950 (10 KiB): DRAM/RETENTION/STACK
I (375) heap_init: At 50000010 len 00001FD8 (7 KiB): RTCRAM
I (383) spi_flash: detected chip: generic
I (386) spi_flash: flash io: dio
W (391) rmt(legacy): legacy driver is deprecated, please migrate to `driver/rmt_tx.h` and/or `driver/rmt_rx.h`
W (401) timer_group: legacy driver is deprecated, please migrate to `driver/gptimer.h`
I (410) sleep: Configure to isolate all GPIO pins in sleep state
I (416) sleep: Enable automatic switching of GPIO sleep configuration
I (424) app_start: Starting scheduler on CPU0
I (428) main_task: Started on CPU0
I (428) main_task: Calling app_main()
I (428) hardware_check: Hello, world!
I (438) pp: pp rom version: 9387209
I (438) net80211: net80211 rom version: 9387209
I (458) wifi:wifi driver task: 3fca2380, prio:23, stack:6656, core=0
I (458) wifi:wifi firmware version: 91b9630
I (458) wifi:wifi certification version: v7.0
I (458) wifi:config NVS flash: disabled
I (458) wifi:config nano formating: disabled
I (468) wifi:Init data frame dynamic rx buffer num: 32
I (468) wifi:Init static rx mgmt buffer num: 10
I (478) wifi:Init management short buffer num: 32
I (478) wifi:Init dynamic tx buffer num: 32
I (488) wifi:Init static tx FG buffer num: 2
I (488) wifi:Init static rx buffer size: 1600
I (488) wifi:Init static rx buffer num: 10
I (498) wifi:Init dynamic rx buffer num: 32
I (498) wifi_init: rx ba win: 6
I (508) wifi_init: tcpip mbox: 32
I (508) wifi_init: udp mbox: 6
I (508) wifi_init: tcp mbox: 6
I (518) wifi_init: tcp tx win: 5744
I (518) wifi_init: tcp rx win: 5744
I (528) wifi_init: tcp mss: 1440
I (528) wifi_init: WiFi IRAM OP enabled
I (528) wifi_init: WiFi RX IRAM OP enabled
I (538) wifi: Starting wifi...
I (538) phy_init: phy_version 1130,b4e4b80,Sep  5 2023,11:09:30
E (548) phy_init: esp_phy_load_cal_data_from_nvs: NVS has not been initialized. Call nvs_flash_init before starting WiFi/BT.
W (558) phy_init: failed to load RF calibration data (0x1101), falling back to full calibration
I (608) wifi:mode : sta (40:4c:ca:8c:9e:28)
I (608) wifi:enable tsf
I (618) wifi: Scanning...
I (5718) wifi: Found configured access point TP-LINK_D1AB on channel 1
I (5718) wifi: Connecting wifi...
I (8128) wifi:new:<1,1>, old:<1,0>, ap:<255,255>, sta:<1,1>, prof:1
I (8398) wifi:state: init -> auth (b0)
I (8408) wifi:state: auth -> assoc (0)
I (8408) wifi:state: assoc -> run (10)
I (8438) wifi:connected with TP-LINK_D1AB, aid = 1, channel 1, 40U, bssid = 98:97:cc:50:d1:ab
I (8438) wifi:security: WPA2-PSK, phy: bgn, rssi: -40
I (8438) wifi:pm start, type: 1
​
I (8438) wifi:set rx beacon pti, rx_bcn_pti: 0, bcn_timeout: 25000, mt_pti: 0, mt_time: 10000
I (8448) wifi: Waiting for DHCP lease...
I (8478) wifi:AP's beacon interval = 102400 us, DTIM period = 1
I (9508) wifi:<ba-add>idx:0 (ifx:0, 98:97:cc:50:d1:ab), tid:0, ssn:0, winSize:64
I (10448) esp_netif_handlers: sta ip: 192.168.0.105, mask: 255.255.255.0, gw: 192.168.0.1
I (10448) wifi: Wifi DHCP info: IpInfo { ip: 192.168.0.105, subnet: Subnet { gateway: 192.168.0.1, mask: Mask(24) }, dns: Some(192.168.1.1), secondary_dns: Some(192.168.0.1) }
I (11458) hardware_check: Hello, world!
```