Skip to content

Commit

Permalink
Added basic blink example for Esp32-C6 (apple#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
allexoK authored Jun 28, 2024
1 parent b85692e commit 52eb879
Show file tree
Hide file tree
Showing 12 changed files with 168 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ This repository is a set of demonstration projects of **Embedded Swift**. Embedd
| [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"> |
| [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"> |
| [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"> |
| [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"> |


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.

Expand Down
3 changes: 3 additions & 0 deletions esp32-led-blink-sdk/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
cmake_minimum_required(VERSION 3.29)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(main)
39 changes: 39 additions & 0 deletions esp32-led-blink-sdk/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# esp32-led-blink-sdk

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).

![Led on](assets/images/ledon.jpg)
![Led off](assets/images/ledoff.jpg)

## Requirements

- 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).
- Make sure you specifically set up development for the RISC-V ESP32-C6, and not the Xtensa based products.

- 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:
- Try building and running the "get-started/blink" example from ESP-IDF written in C.

## Building

- Make sure you have a recent nightly Swift toolchain that has Embedded Swift support.
- If needed, run export.sh to get access to the idf.py script from ESP-IDF.
- Specify the nightly toolchain to be used via the `TOOLCHAINS` environment variable and the target board type by using `idf.py set-target`.
``` console
$ cd esp32-led-blink-sdk
$ export TOOLCHAINS=...
$ . <path-to-esp-idf>/export.sh
$ idf.py set-target esp32c6
$ idf.py build
```

## Running

- 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.
- 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.
- Use `idf.py` to upload the firmware and to run it:

```console
$ idf.py flash
```

- The LED should be blinking now.
Binary file added esp32-led-blink-sdk/assets/images/ledoff.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added esp32-led-blink-sdk/assets/images/ledon.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions esp32-led-blink-sdk/dependencies.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
dependencies:
idf:
component_hash: null
source:
type: idf
version: 5.2.1
manifest_hash: 4bd9cabb9e4ea4c9742d3fdac782a5f66e6b6b748ce0bf390a9183818d1a2291
target: esp32c6
version: 1.0.0
17 changes: 17 additions & 0 deletions esp32-led-blink-sdk/main/BridgingHeader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2024 Apple Inc. and the Swift project authors.
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//

#include <stdio.h>

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "sdkconfig.h"
30 changes: 30 additions & 0 deletions esp32-led-blink-sdk/main/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
idf_component_register(
SRCS "Dummy.c"
INCLUDE_DIRS "."
)

execute_process(COMMAND xcrun -f swiftc OUTPUT_VARIABLE SWIFTC OUTPUT_STRIP_TRAILING_WHITESPACE)

add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/_swiftcode.o
COMMAND
${SWIFTC}
-target riscv32-none-none-eabi
-Xfrontend -function-sections -enable-experimental-feature Embedded -wmo -parse-as-library -Osize
$$\( echo '$<TARGET_PROPERTY:__idf_main,INCLUDE_DIRECTORIES>' | tr '\;' '\\n' | sed -e 's/\\\(.*\\\)/-Xcc -I\\1/g' \)
$$\( echo '${CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES}' | tr ' ' '\\n' | sed -e 's/\\\(.*\\\)/-Xcc -I\\1/g' \)
-import-bridging-header ${CMAKE_CURRENT_LIST_DIR}/BridgingHeader.h
${CMAKE_CURRENT_LIST_DIR}/Main.swift
${CMAKE_CURRENT_LIST_DIR}/Led.swift
-c -o ${CMAKE_CURRENT_BINARY_DIR}/_swiftcode.o
DEPENDS
${CMAKE_CURRENT_LIST_DIR}/BridgingHeader.h
${CMAKE_CURRENT_LIST_DIR}/Main.swift
${CMAKE_CURRENT_LIST_DIR}/Led.swift
)
add_custom_target(main-swiftcode DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/_swiftcode.o)

target_link_libraries(__idf_main
${CMAKE_CURRENT_BINARY_DIR}/_swiftcode.o
)
add_dependencies(__idf_main main-swiftcode)
12 changes: 12 additions & 0 deletions esp32-led-blink-sdk/main/Dummy.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2024 Apple Inc. and the Swift project authors.
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//

// We need to have at least one .c file for the ESP-IDF CMake build system to work.
30 changes: 30 additions & 0 deletions esp32-led-blink-sdk/main/Led.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2024 Apple Inc. and the Swift project authors.
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//

// A simple "overlay" to provide nicer APIs in Swift
struct Led {
var ledPin: gpio_num_t
init(gpioPin: Int) {
ledPin = gpio_num_t(Int32(gpioPin))

guard gpio_reset_pin(ledPin) == ESP_OK else {
fatalError("cannot reset led")
}

guard gpio_set_direction(ledPin, GPIO_MODE_OUTPUT) == ESP_OK else {
fatalError("cannot reset led")
}
}
func setLed(value:Bool) {
let level: UInt32 = value ? 1 : 0
gpio_set_level(ledPin, level)
}
}
26 changes: 26 additions & 0 deletions esp32-led-blink-sdk/main/Main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2024 Apple Inc. and the Swift project authors.
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//

//The code will blink an LED on GPIO8. To change the pin, modify Led(gpioPin: 8)
@_cdecl("app_main")
func app_main() {
print("Hello from Swift on ESP32-C6!")

var ledValue: Bool = false
let blinkDelayMs: UInt32 = 500
let led = Led(gpioPin: 8)

while true {
led.setLed(value: ledValue)
ledValue.toggle() // Toggle the boolean value
vTaskDelay(blinkDelayMs / (1000 / UInt32(configTICK_RATE_HZ)))
}
}
Empty file.

0 comments on commit 52eb879

Please sign in to comment.