Skip to content

Commit 52eb879

Browse files
authored
Added basic blink example for Esp32-C6 (#21)
1 parent b85692e commit 52eb879

File tree

12 files changed

+168
-0
lines changed

12 files changed

+168
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ This repository is a set of demonstration projects of **Embedded Swift**. Embedd
3030
| [pico-w-blink-sdk](./pico-w-blink-sdk) | Raspberry Pi Pico W | Baremetal program that blinks an LED to signal 'SOS' in Morse code. Demonstrates how to use code and libraries from the Pico SDK and add Swift code on top of it. | <img width="300" src="https://github.com/apple/swift-embedded-examples/assets/26223064/a4949a2e-1887-4325-8f5f-a681963c93d7"> |
3131
| [nrfx-blink-sdk](./nrfx-blink-sdk) | nRF52840-DK | Baremetal program that blinks an LED repeatedly. Demonstrates how to use code and libraries from the Zephyr SDK and add Swift code on top of it. | <img width="300" src="https://raw.githubusercontent.com/kubamracek/swift-evolution/branch/assets/nrfx-blink-sdk.jpeg"> |
3232
| [esp32-led-strip-sdk](./esp32-led-strip-sdk) | ESP32-C6-DevKitC-1 | Demonstrates how to integrate with ESP-IDF SDK and use Swift to control the LED strip library from the SDK. | <img width="300" src="https://raw.githubusercontent.com/kubamracek/swift-evolution/branch/assets/esp32-led-strip-sdk.jpg"> |
33+
| [esp32-led-blink-sdk](./esp32-led-blink-sdk) | ESP32-C6-Bug | Demonstrates how to integrate with ESP-IDF SDK and use Swift to control the standard LED from the SDK. | <img width="300" src="esp32-led-blink-sdk/assets/images/ledon.jpg"> |
34+
3335

3436
Note that the SDK integration examples (Pico SDK, Zephyr SDK, etc.) are not recommendations or endorsement, the same is true for build system choice (Make, CMake, SwiftPM, shell scripts). Embedded Swift aims to be versatile and to allow for integration into more existing SDKs and build systems, and the example projects are merely showing the possibilities.
3537

esp32-led-blink-sdk/CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
cmake_minimum_required(VERSION 3.29)
2+
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
3+
project(main)

esp32-led-blink-sdk/README.md

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# esp32-led-blink-sdk
2+
3+
This example demonstrates how to integrate with the ESP-IDF SDK via CMake and how to use the standard GPIO library to control LED from Swift. This example is specifically made for the RISC-V MCUs from ESP32 (the Xtensa MCUs are not currently supported by Swift).
4+
5+
![Led on](assets/images/ledon.jpg)
6+
![Led off](assets/images/ledoff.jpg)
7+
8+
## Requirements
9+
10+
- Set up the [ESP-IDF](https://docs.espressif.com/projects/esp-idf/en/stable/esp32/) development environment. Follow the steps in the [ESP32-C6 "Get Started" guide](https://docs.espressif.com/projects/esp-idf/en/v5.2/esp32c6/get-started/index.html).
11+
- Make sure you specifically set up development for the RISC-V ESP32-C6, and not the Xtensa based products.
12+
13+
- Before trying to use Swift with the ESP-IDF SDK, make sure your environment works and can build the provided C/C++ sample projects, in particular:
14+
- Try building and running the "get-started/blink" example from ESP-IDF written in C.
15+
16+
## Building
17+
18+
- Make sure you have a recent nightly Swift toolchain that has Embedded Swift support.
19+
- If needed, run export.sh to get access to the idf.py script from ESP-IDF.
20+
- Specify the nightly toolchain to be used via the `TOOLCHAINS` environment variable and the target board type by using `idf.py set-target`.
21+
``` console
22+
$ cd esp32-led-blink-sdk
23+
$ export TOOLCHAINS=...
24+
$ . <path-to-esp-idf>/export.sh
25+
$ idf.py set-target esp32c6
26+
$ idf.py build
27+
```
28+
29+
## Running
30+
31+
- Connect the Esp32-C6-Bug board (or any other board with integrated LED on GPIO pin 8) over a USB cable to your Mac. Alternatively you can just connect external LED to GPIO pin 8 on any other board.
32+
- Connect RX pin of USB-UART converter to TX0 pin of your board if you need serial ouput. You may also need to connect GND converter pin to the GND pin of the board.
33+
- Use `idf.py` to upload the firmware and to run it:
34+
35+
```console
36+
$ idf.py flash
37+
```
38+
39+
- The LED should be blinking now.
208 KB
Loading
205 KB
Loading

esp32-led-blink-sdk/dependencies.lock

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
dependencies:
2+
idf:
3+
component_hash: null
4+
source:
5+
type: idf
6+
version: 5.2.1
7+
manifest_hash: 4bd9cabb9e4ea4c9742d3fdac782a5f66e6b6b748ce0bf390a9183818d1a2291
8+
target: esp32c6
9+
version: 1.0.0
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift open source project
4+
//
5+
// Copyright (c) 2024 Apple Inc. and the Swift project authors.
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
//
10+
//===----------------------------------------------------------------------===//
11+
12+
#include <stdio.h>
13+
14+
#include "freertos/FreeRTOS.h"
15+
#include "freertos/task.h"
16+
#include "driver/gpio.h"
17+
#include "sdkconfig.h"
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
idf_component_register(
2+
SRCS "Dummy.c"
3+
INCLUDE_DIRS "."
4+
)
5+
6+
execute_process(COMMAND xcrun -f swiftc OUTPUT_VARIABLE SWIFTC OUTPUT_STRIP_TRAILING_WHITESPACE)
7+
8+
add_custom_command(
9+
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/_swiftcode.o
10+
COMMAND
11+
${SWIFTC}
12+
-target riscv32-none-none-eabi
13+
-Xfrontend -function-sections -enable-experimental-feature Embedded -wmo -parse-as-library -Osize
14+
$$\( echo '$<TARGET_PROPERTY:__idf_main,INCLUDE_DIRECTORIES>' | tr '\;' '\\n' | sed -e 's/\\\(.*\\\)/-Xcc -I\\1/g' \)
15+
$$\( echo '${CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES}' | tr ' ' '\\n' | sed -e 's/\\\(.*\\\)/-Xcc -I\\1/g' \)
16+
-import-bridging-header ${CMAKE_CURRENT_LIST_DIR}/BridgingHeader.h
17+
${CMAKE_CURRENT_LIST_DIR}/Main.swift
18+
${CMAKE_CURRENT_LIST_DIR}/Led.swift
19+
-c -o ${CMAKE_CURRENT_BINARY_DIR}/_swiftcode.o
20+
DEPENDS
21+
${CMAKE_CURRENT_LIST_DIR}/BridgingHeader.h
22+
${CMAKE_CURRENT_LIST_DIR}/Main.swift
23+
${CMAKE_CURRENT_LIST_DIR}/Led.swift
24+
)
25+
add_custom_target(main-swiftcode DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/_swiftcode.o)
26+
27+
target_link_libraries(__idf_main
28+
${CMAKE_CURRENT_BINARY_DIR}/_swiftcode.o
29+
)
30+
add_dependencies(__idf_main main-swiftcode)

esp32-led-blink-sdk/main/Dummy.c

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift open source project
4+
//
5+
// Copyright (c) 2024 Apple Inc. and the Swift project authors.
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
//
10+
//===----------------------------------------------------------------------===//
11+
12+
// We need to have at least one .c file for the ESP-IDF CMake build system to work.

esp32-led-blink-sdk/main/Led.swift

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift open source project
4+
//
5+
// Copyright (c) 2024 Apple Inc. and the Swift project authors.
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
//
10+
//===----------------------------------------------------------------------===//
11+
12+
// A simple "overlay" to provide nicer APIs in Swift
13+
struct Led {
14+
var ledPin: gpio_num_t
15+
init(gpioPin: Int) {
16+
ledPin = gpio_num_t(Int32(gpioPin))
17+
18+
guard gpio_reset_pin(ledPin) == ESP_OK else {
19+
fatalError("cannot reset led")
20+
}
21+
22+
guard gpio_set_direction(ledPin, GPIO_MODE_OUTPUT) == ESP_OK else {
23+
fatalError("cannot reset led")
24+
}
25+
}
26+
func setLed(value:Bool) {
27+
let level: UInt32 = value ? 1 : 0
28+
gpio_set_level(ledPin, level)
29+
}
30+
}

esp32-led-blink-sdk/main/Main.swift

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift open source project
4+
//
5+
// Copyright (c) 2024 Apple Inc. and the Swift project authors.
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
//
10+
//===----------------------------------------------------------------------===//
11+
12+
//The code will blink an LED on GPIO8. To change the pin, modify Led(gpioPin: 8)
13+
@_cdecl("app_main")
14+
func app_main() {
15+
print("Hello from Swift on ESP32-C6!")
16+
17+
var ledValue: Bool = false
18+
let blinkDelayMs: UInt32 = 500
19+
let led = Led(gpioPin: 8)
20+
21+
while true {
22+
led.setLed(value: ledValue)
23+
ledValue.toggle() // Toggle the boolean value
24+
vTaskDelay(blinkDelayMs / (1000 / UInt32(configTICK_RATE_HZ)))
25+
}
26+
}

esp32-led-blink-sdk/main/idf_component.yml

Whitespace-only changes.

0 commit comments

Comments
 (0)