-
Notifications
You must be signed in to change notification settings - Fork 64
Added basic blink example for Esp32-C6 #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
89086c8
Addind standard blink example on GPIO8
allexoK 248097f
Update README.md
allexoK a7c9f23
Tested when changed CMAKE to 3.29, cleaned grammar and formatting, ch…
allexoK 1b3195e
Rotate the images
allexoK 1faeeb4
change pictures location to 'assets/images/...'
allexoK File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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. |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
last nit: could you change this path to
assets/images/...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, changed it :)