forked from apple/swift-embedded-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added basic blink example for Esp32-C6 (apple#21)
- Loading branch information
Showing
12 changed files
with
168 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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). | ||
|
||
 | ||
 | ||
|
||
## 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. |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.