Skip to content

Commit d6f88d3

Browse files
authored
Add Xcc flags that fixes crash on ESP32-C3. (#20)
Makes the `smart-light` example run on ESP32-C3. Fixes #15
1 parent b4d5d41 commit d6f88d3

File tree

3 files changed

+25
-10
lines changed

3 files changed

+25
-10
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Build a Matter accessory using Embedded Swift
66

77
<!-- Image -->
88

9-
This repository contains an example simple Embedded Swift application that implements a Matter smart light accessory and can be used from HomeKit. The examples use an ESP32-C6 (RISC-V) microcontroller and build on top of the ESP-IDF and ESP-Matter SDKs.
9+
This repository contains an example simple Embedded Swift application that implements a Matter smart light accessory and can be used from HomeKit. The examples use an ESP32-C6 or ESP32-C3 (RISC-V) microcontroller and build on top of the ESP-IDF and ESP-Matter SDKs.
1010

1111
> [!NOTE]
1212
> This repository is associated with WWDC24 session 10197: [Go small with Embedded Swift](https://developer.apple.com/wwdc24/10197).
@@ -37,7 +37,7 @@ Before running the examples, ensure you have the following tools available:
3737

3838
- Hardware:
3939
- [ESP32-C6-DevKitC-1-N8](https://docs.espressif.com/projects/espressif-esp-dev-kits/en/latest/esp32c6/esp32-c6-devkitc-1/user_guide.html)
40-
- Software
40+
- Software
4141
- [Swift Nightly Toolchain](https://www.swift.org/download)
4242
- [CMake 3.29+](https://cmake.org/download)
4343
- [ESP-IDF 5.2.1](https://docs.espressif.com/projects/esp-idf/en/v5.2.1/esp32c6/get-started/index.html)
@@ -59,17 +59,17 @@ Ensure your shell has access to the tools listed above, see [Get started on macO
5959
$ cd swift-matter-examples/smart-light
6060
```
6161

62-
2. Configure the build system for your microcontroller.
62+
2. Configure the build system for your microcontroller, the `smart-light` example should also be runnable on `esp32c3`.
6363
```shell
6464
$ idf.py set-target esp32c6
6565
```
6666

67-
3. Build and deploy the application to your device.
67+
3. Build and deploy the application to your device.
6868
```shell
6969
$ idf.py build flash monitor
7070
```
7171

72-
Explore the implementation, see [Explore the LED Blink example](https://apple.github.io/swift-matter-examples/tutorials/swiftmatterexamples/run-example-led-blink#Understand-the-code) and
72+
Explore the implementation, see [Explore the LED Blink example](https://apple.github.io/swift-matter-examples/tutorials/swiftmatterexamples/run-example-led-blink#Understand-the-code) and
7373
[Explore the Smart Light example](https://apple.github.io/swift-matter-examples/tutorials/swiftmatterexamples/run-example-smart-light#Understand-the-code) for guided walkthroughs.
7474

7575
## Contributing to Swift Matter Examples

smart-light/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Embedded Swift Matter Example: Smart Light example
22

3-
This directory contains an example implementation of a Matter smart LED light accessory in Embedded Swift, and it can be built using the ESP IDF and ESP Matter SDKs, and uploaded to an ESP32C6 development board.
3+
This directory contains an example implementation of a Matter smart LED light accessory in Embedded Swift, and it can be built using the ESP IDF and ESP Matter SDKs, and uploaded to an ESP32C6 or ESP32C3 development board.
44

55
Breakdown of the files included:
66

@@ -27,7 +27,7 @@ Breakdown of the files included:
2727
For full steps how to build the example code, follow the [Setup Your Environment](https://apple.github.io/swift-embedded/swift-matter-examples/tutorials/tutorial-table-of-contents#setup-your-environment) tutorials and the [Explore the Smart Light example](https://apple.github.io/swift-matter-examples/tutorials/swiftmatterexamples/run-example-smart-light) tutorial. In summary:
2828

2929
- Ensure your system has all the required software installed and your shell has access to the tools listed in the top-level README file.
30-
- Plug in the ESP32C6 development board via a USB cable.
30+
- Plug in the ESP32C6/C3 development board via a USB cable.
3131
- Have a set up HomeKit or other Matter-enabled smart home ecosystem.
3232
- For HomeKit, this includes a configured home, a Wi-Fi network which additional devices can join, a [home hub](https://support.apple.com/en-us/102557), and an iOS device for managing the home.
3333

@@ -37,17 +37,17 @@ For full steps how to build the example code, follow the [Setup Your Environment
3737
$ cd swift-matter-examples/smart-light
3838
```
3939

40-
2. Configure the build system for your microcontroller.
40+
2. Configure the build system for your microcontroller, this example should also be runnable on `esp32c3`.
4141
```shell
4242
$ idf.py set-target esp32c6
4343
```
4444

45-
3. Build and deploy the application to your device.
45+
3. Build and deploy the application to your device.
4646
```shell
4747
$ idf.py build flash monitor
4848
```
4949

50-
4. Register the device in your home network. See [Connect-using-Matter](https://apple.github.io/swift-matter-examples/tutorials/swiftmatterexamples/run-example-smart-light#Connect-using-Matter) for detailed pairing instructions with HomeKit.
50+
4. Register the device in your home network. See [Connect-using-Matter](https://apple.github.io/swift-matter-examples/tutorials/swiftmatterexamples/run-example-smart-light#Connect-using-Matter) for detailed pairing instructions with HomeKit.
5151

5252
5. You can now control the smart light. In case of a HomeKit network, the Home app, and Siri can both be used to turn the light on, off, change colors, etc.
5353

smart-light/main/CMakeLists.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,19 @@ foreach(dir ${CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES})
2222
string(CONCAT SWIFT_INCLUDES ${SWIFT_INCLUDES} "-I${dir} ")
2323
endforeach()
2424

25+
# Set device-specific march/mabi flags
26+
idf_build_get_property(target IDF_TARGET)
27+
if(${target} STREQUAL "esp32c2" OR ${target} STREQUAL "esp32c3")
28+
set(march_flag "rv32imc_zicsr_zifencei")
29+
set(mabi_flag "ilp32")
30+
elseif(${target} STREQUAL "esp32p4")
31+
set(march_flag "rv32imafc_zicsr_zifencei")
32+
set(mabi_flag "ilp32f")
33+
else()
34+
set(march_flag "rv32imac_zicsr_zifencei")
35+
set(mabi_flag "ilp32")
36+
endif()
37+
2538
# Swift compiler flags to build in Embedded Swift mode, optimize for size, choose the right ISA, ABI, C++ language standard, etc.
2639
target_compile_options(${COMPONENT_LIB} PUBLIC "$<$<COMPILE_LANGUAGE:Swift>:SHELL:
2740
-target riscv32-none-none-eabi
@@ -46,6 +59,8 @@ target_compile_options(${COMPONENT_LIB} PUBLIC "$<$<COMPILE_LANGUAGE:Swift>:SHEL
4659
-Xcc -D_GLIBCXX_HAVE_POSIX_SEMAPHORE
4760
-Xcc -D_GLIBCXX_USE_POSIX_SEMAPHORE
4861
-Xcc -D_POSIX_READER_WRITER_LOCKS
62+
-Xcc -march=${march_flag}
63+
-Xcc -mabi=${mabi_flag}
4964
5065
-pch-output-dir /tmp
5166
-Xfrontend -enable-single-module-llvm-emission

0 commit comments

Comments
 (0)