This commit is contained in:
2026-02-09 13:25:36 +01:00
commit af9e0c11e0
56 changed files with 1082 additions and 0 deletions

3
.vscode/settings.json vendored Normal file
View File

@@ -0,0 +1,3 @@
{
"idf.currentSetup": "c:\\ESP_IDF\\v5.4\\esp-idf"
}

22
HIL_doc.md Normal file
View File

@@ -0,0 +1,22 @@
# HIL Test Environment: ESP32 Device Mapping
This document defines the persistent hardware mapping for the ASF-SH Debugger environment.
Devices are mapped via **udev rules** to ensure `/dev/` paths remain constant regardless of plug order.
## Setup 1: Debugging & Programming
| Device Name | Friendly Symlink | Environment Variable | Hardware ID / Serial |
| :--- | :--- | :--- | :--- |
| **EPROG Debugger** | `/dev/eprog_debugger` | `$EPROG` | FTDI Dual RS232 (Port 0) |
| **ESP32-S3 Target** | `/dev/esp32_s3_debug` | `$ESP_DEBUG` | Serial: `123456` |
## Setup 2: I2C & Sensor Emulation
| Device Name | Friendly Symlink | Environment Variable | Hardware ID / Serial |
| :--- | :--- | :--- | :--- |
| **ESP Sensor Test** | `/dev/esp_sensor_test` | `$ESP_SENSOR` | ID: `1a86:7523` |
| **I2C Emulator** | `/dev/i2c_emulator` | `$I2C_EMULATOR` | CP2102 Serial: `0001` |
## Usage in Scripts
Always use the environment variables in your automation scripts to ensure compatibility:
- **Flash Sensor:** `idf.py flash -p $ESP_SENSOR`
- **Monitor Emulator:** `idf.py monitor -p $I2C_EMULATOR`

View File

@@ -0,0 +1,13 @@
ARG DOCKER_TAG=latest
FROM espressif/idf:${DOCKER_TAG}
ENV LC_ALL=C.UTF-8
ENV LANG=C.UTF-8
RUN apt-get update -y && apt-get install udev -y
RUN echo "source /opt/esp/idf/export.sh > /dev/null 2>&1" >> ~/.bashrc
ENTRYPOINT [ "/opt/esp/entrypoint.sh" ]
CMD ["/bin/bash", "-c"]

View File

@@ -0,0 +1,19 @@
{
"name": "ESP-IDF QEMU",
"build": {
"dockerfile": "Dockerfile"
},
"customizations": {
"vscode": {
"settings": {
"terminal.integrated.defaultProfile.linux": "bash",
"idf.gitPath": "/usr/bin/git"
},
"extensions": [
"espressif.esp-idf-extension",
"espressif.esp-idf-web"
]
}
},
"runArgs": ["--privileged"]
}

78
I2C/.gitignore vendored Normal file
View File

@@ -0,0 +1,78 @@
# macOS
.DS_Store
.AppleDouble
.LSOverride
# Directory metadata
.directory
# Temporary files
*~
*.swp
*.swo
*.bak
*.tmp
# Log files
*.log
# Build artifacts and directories
**/build/
build/
*.o
*.a
*.out
*.exe # For any host-side utilities compiled on Windows
# ESP-IDF specific build outputs
*.bin
*.elf
*.map
flasher_args.json # Generated in build directory
sdkconfig.old
sdkconfig
# ESP-IDF dependencies
# For older versions or manual component management
/components/.idf/
**/components/.idf/
# For modern ESP-IDF component manager
managed_components/
# If ESP-IDF tools are installed/referenced locally to the project
.espressif/
# CMake generated files
CMakeCache.txt
CMakeFiles/
cmake_install.cmake
install_manifest.txt
CTestTestfile.cmake
# Python environment files
*.pyc
*.pyo
*.pyd
__pycache__/
*.egg-info/
dist/
# Virtual environment folders
venv/
.venv/
env/
# Language Servers
.clangd/
.ccls-cache/
compile_commands.json
# Windows specific
Thumbs.db
ehthumbs.db
Desktop.ini
# User-specific configuration files
*.user
*.workspace # General workspace files, can be from various tools
*.suo # Visual Studio Solution User Options
*.sln.docstates # Visual Studio

19
I2C/.vscode/c_cpp_properties.json vendored Normal file
View File

@@ -0,0 +1,19 @@
{
"configurations": [
{
"name": "ESP-IDF",
"compilerPath": "c:\\Espressif\\tools\\tools\\xtensa-esp-elf\\esp-14.2.0_20241119\\xtensa-esp-elf\\bin\\xtensa-esp32-elf-gcc.exe",
"compileCommands": "${config:idf.buildPath}/compile_commands.json",
"includePath": [
"${workspaceFolder}/**"
],
"browse": {
"path": [
"${workspaceFolder}"
],
"limitSymbolsToIncludedHeaders": true
}
}
],
"version": 4
}

10
I2C/.vscode/launch.json vendored Normal file
View File

@@ -0,0 +1,10 @@
{
"version": "0.2.0",
"configurations": [
{
"type": "gdbtarget",
"request": "attach",
"name": "Eclipse CDT GDB Adapter"
}
]
}

11
I2C/.vscode/settings.json vendored Normal file
View File

@@ -0,0 +1,11 @@
{
"C_Cpp.intelliSenseEngine": "default",
"idf.openOcdConfigs": [
"board/esp32-wrover-kit-3.3v.cfg"
],
"idf.portWin": "detect",
"idf.currentSetup": "C:\\Users\\mahmo\\esp\\v5.4\\esp-idf",
"idf.customExtraVars": {
"IDF_TARGET": "esp32"
}
}

6
I2C/CMakeLists.txt Normal file
View File

@@ -0,0 +1,6 @@
# The following five lines of boilerplate have to be in your project's
# CMakeLists in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.16)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(I2C)

69
I2C/README.md Normal file
View File

@@ -0,0 +1,69 @@
| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-C61 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 |
| ----------------- | ----- | -------- | -------- | -------- | -------- | --------- | -------- | -------- | -------- | -------- |
# Blink Example
(See the README.md file in the upper level 'examples' directory for more information about examples.)
This example demonstrates how to blink a LED by using the GPIO driver or using the [led_strip](https://components.espressif.com/component/espressif/led_strip) library if the LED is addressable e.g. [WS2812](https://cdn-shop.adafruit.com/datasheets/WS2812B.pdf). The `led_strip` library is installed via [component manager](main/idf_component.yml).
## How to Use Example
Before project configuration and build, be sure to set the correct chip target using `idf.py set-target <chip_name>`.
### Hardware Required
* A development board with normal LED or addressable LED on-board (e.g., ESP32-S3-DevKitC, ESP32-C6-DevKitC etc.)
* A USB cable for Power supply and programming
See [Development Boards](https://www.espressif.com/en/products/devkits) for more information about it.
### Configure the Project
Open the project configuration menu (`idf.py menuconfig`).
In the `Example Configuration` menu:
* Select the LED type in the `Blink LED type` option.
* Use `GPIO` for regular LED
* Use `LED strip` for addressable LED
* If the LED type is `LED strip`, select the backend peripheral
* `RMT` is only available for ESP targets with RMT peripheral supported
* `SPI` is available for all ESP targets
* Set the GPIO number used for the signal in the `Blink GPIO number` option.
* Set the blinking period in the `Blink period in ms` option.
### Build and Flash
Run `idf.py -p PORT flash monitor` to build, flash and monitor the project.
(To exit the serial monitor, type ``Ctrl-]``.)
See the [Getting Started Guide](https://docs.espressif.com/projects/esp-idf/en/latest/get-started/index.html) for full steps to configure and use ESP-IDF to build projects.
## Example Output
As you run the example, you will see the LED blinking, according to the previously defined period. For the addressable LED, you can also change the LED color by setting the `led_strip_set_pixel(led_strip, 0, 16, 16, 16);` (LED Strip, Pixel Number, Red, Green, Blue) with values from 0 to 255 in the [source file](main/blink_example_main.c).
```text
I (315) example: Example configured to blink addressable LED!
I (325) example: Turning the LED OFF!
I (1325) example: Turning the LED ON!
I (2325) example: Turning the LED OFF!
I (3325) example: Turning the LED ON!
I (4325) example: Turning the LED OFF!
I (5325) example: Turning the LED ON!
I (6325) example: Turning the LED OFF!
I (7325) example: Turning the LED ON!
I (8325) example: Turning the LED OFF!
```
Note: The color order could be different according to the LED model.
The pixel number indicates the pixel position in the LED strip. For a single LED, use 0.
## Troubleshooting
* If the LED isn't blinking, check the GPIO or the LED type selection in the `Example Configuration` menu.
For any technical queries, please open an [issue](https://github.com/espressif/esp-idf/issues) on GitHub. We will get back to you soon.

View File

@@ -0,0 +1,5 @@
idf_component_register(
SRCS "scd30.c"
INCLUDE_DIRS "."
REQUIRES driver
)

View File

@@ -0,0 +1,82 @@
#include "scd30.h"
#include "driver/i2c_master.h"
#include <string.h>
#include <stdio.h>
#define SCD30_I2C_ADDRESS 0x61
#define SCD30_CMD_START_MEASUREMENT 0x0010
#define SCD30_CMD_READ_DATA 0x0300
#define SCD30_CMD_READY_STATUS 0x0202
// Private function to calculate CRC-8
static uint8_t calculate_crc8(const uint8_t *data, size_t len) {
const uint8_t polynomial = 0x31;
uint8_t crc = 0xFF;
for (size_t i = 0; i < len; i++) {
crc ^= data[i];
for (uint8_t bit = 0; bit < 8; bit++) {
if (crc & 0x80) {
crc = (crc << 1) ^ polynomial;
} else {
crc <<= 1;
}
}
}
return crc;
}
void scd30_init(void) {
i2c_master_config_t i2c_config = {
.scl_wait_us = 150000 // Clock stretching
};
i2c_master_init(I2C_NUM_0, &i2c_config);
}
void scd30_start_measurement(void) {
uint8_t command[5] = {
(SCD30_CMD_START_MEASUREMENT >> 8) & 0xFF,
SCD30_CMD_START_MEASUREMENT & 0xFF,
0x00, 0x10, // Argument for continuous measurement
0x00 // Placeholder for CRC
};
command[4] = calculate_crc8(&command[2], 2);
i2c_master_write_to_device(I2C_NUM_0, SCD30_I2C_ADDRESS, command, sizeof(command), 1000 / portTICK_PERIOD_MS);
}
int scd30_read_data(scd30_data_t *data) {
uint8_t ready_command[2] = {
(SCD30_CMD_READY_STATUS >> 8) & 0xFF,
SCD30_CMD_READY_STATUS & 0xFF
};
uint8_t ready_status[3];
// Poll until data is ready
do {
i2c_master_write_read_device(I2C_NUM_0, SCD30_I2C_ADDRESS, ready_command, sizeof(ready_command), ready_status, sizeof(ready_status), 1000 / portTICK_PERIOD_MS);
} while (ready_status[1] != 0x01);
uint8_t read_command[2] = {
(SCD30_CMD_READ_DATA >> 8) & 0xFF,
SCD30_CMD_READ_DATA & 0xFF
};
uint8_t buffer[18];
// Read 18 bytes of data
i2c_master_write_read_device(I2C_NUM_0, SCD30_I2C_ADDRESS, read_command, sizeof(read_command), buffer, sizeof(buffer), 1000 / portTICK_PERIOD_MS);
// Validate CRC for each pair of data
for (int i = 0; i < 18; i += 3) {
if (calculate_crc8(buffer + i, 2) != buffer[i + 2]) {
return -1; // CRC error
}
}
// Parse data
data->co2 = (float)((buffer[0] << 8) | buffer[1]);
data->temp = (float)((buffer[3] << 8) | buffer[4]);
data->humidity = (float)((buffer[6] << 8) | buffer[7]);
return 0; // Success
}

View File

@@ -0,0 +1,18 @@
#ifndef SCD30_H
#define SCD30_H
#include <stdint.h>
// Data structure to hold SCD30 sensor data
typedef struct {
float co2; // CO2 concentration in ppm
float temp; // Temperature in degrees Celsius
float humidity; // Relative humidity in percentage
} scd30_data_t;
// Function prototypes
void scd30_init(void);
void scd30_start_measurement(void);
int scd30_read_data(scd30_data_t *data);
#endif // SCD30_H

20
I2C/dependencies.lock Normal file
View File

@@ -0,0 +1,20 @@
dependencies:
espressif/led_strip:
component_hash: 28c6509a727ef74925b372ed404772aeedf11cce10b78c3f69b3c66799095e2d
dependencies:
- name: idf
require: private
version: '>=4.4'
source:
registry_url: https://components.espressif.com/
type: service
version: 2.5.5
idf:
source:
type: idf
version: 5.4.0
direct_dependencies:
- espressif/led_strip
manifest_hash: a9af7824fb34850fbe175d5384052634b3c00880abb2d3a7937e666d07603998
target: esp32
version: 2.0.0

2
I2C/main/CMakeLists.txt Normal file
View File

@@ -0,0 +1,2 @@
idf_component_register(SRCS "blink_example_main.c"
INCLUDE_DIRS ".")

View File

@@ -0,0 +1,49 @@
menu "Example Configuration"
orsource "$IDF_PATH/examples/common_components/env_caps/$IDF_TARGET/Kconfig.env_caps"
choice BLINK_LED
prompt "Blink LED type"
default BLINK_LED_GPIO
help
Select the LED type. A normal level controlled LED or an addressable LED strip.
The default selection is based on the Espressif DevKit boards.
You can change the default selection according to your board.
config BLINK_LED_GPIO
bool "GPIO"
config BLINK_LED_STRIP
bool "LED strip"
endchoice
choice BLINK_LED_STRIP_BACKEND
depends on BLINK_LED_STRIP
prompt "LED strip backend peripheral"
default BLINK_LED_STRIP_BACKEND_RMT if SOC_RMT_SUPPORTED
default BLINK_LED_STRIP_BACKEND_SPI
help
Select the backend peripheral to drive the LED strip.
config BLINK_LED_STRIP_BACKEND_RMT
depends on SOC_RMT_SUPPORTED
bool "RMT"
config BLINK_LED_STRIP_BACKEND_SPI
bool "SPI"
endchoice
config BLINK_GPIO
int "Blink GPIO number"
range ENV_GPIO_RANGE_MIN ENV_GPIO_OUT_RANGE_MAX
default 8
help
GPIO number (IOxx) to blink on and off the LED.
Some GPIOs are used for other purposes (flash connections, etc.) and cannot be used to blink.
config BLINK_PERIOD
int "Blink period in ms"
range 10 3600000
default 1000
help
Define the blinking period in milliseconds.
endmenu

View File

@@ -0,0 +1,104 @@
/* Blink Example
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "esp_log.h"
#include "led_strip.h"
#include "sdkconfig.h"
static const char *TAG = "example";
/* Use project configuration menu (idf.py menuconfig) to choose the GPIO to blink,
or you can edit the following line and set a number here.
*/
#define BLINK_GPIO CONFIG_BLINK_GPIO
static uint8_t s_led_state = 0;
#ifdef CONFIG_BLINK_LED_STRIP
static led_strip_handle_t led_strip;
static void blink_led(void)
{
/* If the addressable LED is enabled */
if (s_led_state) {
/* Set the LED pixel using RGB from 0 (0%) to 255 (100%) for each color */
led_strip_set_pixel(led_strip, 0, 16, 16, 16);
/* Refresh the strip to send data */
led_strip_refresh(led_strip);
} else {
/* Set all LED off to clear all pixels */
led_strip_clear(led_strip);
}
}
static void configure_led(void)
{
ESP_LOGI(TAG, "Example configured to blink addressable LED!");
/* LED strip initialization with the GPIO and pixels number*/
led_strip_config_t strip_config = {
.strip_gpio_num = BLINK_GPIO,
.max_leds = 1, // at least one LED on board
};
#if CONFIG_BLINK_LED_STRIP_BACKEND_RMT
led_strip_rmt_config_t rmt_config = {
.resolution_hz = 10 * 1000 * 1000, // 10MHz
.flags.with_dma = false,
};
ESP_ERROR_CHECK(led_strip_new_rmt_device(&strip_config, &rmt_config, &led_strip));
#elif CONFIG_BLINK_LED_STRIP_BACKEND_SPI
led_strip_spi_config_t spi_config = {
.spi_bus = SPI2_HOST,
.flags.with_dma = true,
};
ESP_ERROR_CHECK(led_strip_new_spi_device(&strip_config, &spi_config, &led_strip));
#else
#error "unsupported LED strip backend"
#endif
/* Set all LED off to clear all pixels */
led_strip_clear(led_strip);
}
#elif CONFIG_BLINK_LED_GPIO
static void blink_led(void)
{
/* Set the GPIO level according to the state (LOW or HIGH)*/
gpio_set_level(BLINK_GPIO, s_led_state);
}
static void configure_led(void)
{
ESP_LOGI(TAG, "Example configured to blink GPIO LED!");
gpio_reset_pin(BLINK_GPIO);
/* Set the GPIO as a push/pull output */
gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT);
}
#else
#error "unsupported LED type"
#endif
void app_main(void)
{
/* Configure the peripheral according to the LED type */
configure_led();
while (1) {
ESP_LOGI(TAG, "Turning the LED %s!", s_led_state == true ? "ON" : "OFF");
blink_led();
/* Toggle the LED state */
s_led_state = !s_led_state;
vTaskDelay(CONFIG_BLINK_PERIOD / portTICK_PERIOD_MS);
}
}

View File

@@ -0,0 +1,2 @@
dependencies:
espressif/led_strip: "^2.4.1"

25
I2C/main/main.c Normal file
View File

@@ -0,0 +1,25 @@
#include <stdio.h>
#include "scd30.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
void app_main(void) {
scd30_data_t sensor_data;
// Initialize the SCD30 sensor
scd30_init();
scd30_start_measurement();
while (1) {
// Read data from the sensor
if (scd30_read_data(&sensor_data) == 0) {
printf("CO2: %.2f ppm, Temp: %.2f °C, Humidity: %.2f %%\n",
sensor_data.co2, sensor_data.temp, sensor_data.humidity);
} else {
printf("Failed to read data from SCD30 sensor\n");
}
// Wait for 2 seconds
vTaskDelay(pdMS_TO_TICKS(2000));
}
}

16
I2C/pytest_blink.py Normal file
View File

@@ -0,0 +1,16 @@
# SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
# SPDX-License-Identifier: CC0-1.0
import logging
import os
import pytest
from pytest_embedded_idf.dut import IdfDut
@pytest.mark.supported_targets
@pytest.mark.generic
def test_blink(dut: IdfDut) -> None:
# check and log bin size
binary_file = os.path.join(dut.app.binary_path, 'blink.bin')
bin_size = os.path.getsize(binary_file)
logging.info('blink_bin_size : {}KB'.format(bin_size // 1024))

2
I2C/sdkconfig.defaults Normal file
View File

@@ -0,0 +1,2 @@
CONFIG_BLINK_LED_GPIO=y
CONFIG_BLINK_GPIO=8

View File

@@ -0,0 +1 @@
CONFIG_BLINK_GPIO=5

View File

@@ -0,0 +1 @@
CONFIG_BLINK_LED_STRIP=y

View File

@@ -0,0 +1,2 @@
CONFIG_BLINK_GPIO=27
CONFIG_BLINK_LED_STRIP=y

View File

@@ -0,0 +1 @@
CONFIG_BLINK_LED_STRIP=y

View File

@@ -0,0 +1 @@
CONFIG_BLINK_LED_STRIP=y

View File

@@ -0,0 +1 @@
CONFIG_BLINK_LED_STRIP=y

View File

@@ -0,0 +1,2 @@
CONFIG_BLINK_GPIO=44
CONFIG_BLINK_LED_STRIP=y

View File

@@ -0,0 +1,2 @@
CONFIG_BLINK_LED_STRIP=y
CONFIG_BLINK_GPIO=18

View File

@@ -0,0 +1,2 @@
CONFIG_BLINK_LED_STRIP=y
CONFIG_BLINK_GPIO=48

View File

@@ -0,0 +1,13 @@
ARG DOCKER_TAG=latest
FROM espressif/idf:${DOCKER_TAG}
ENV LC_ALL=C.UTF-8
ENV LANG=C.UTF-8
RUN apt-get update -y && apt-get install udev -y
RUN echo "source /opt/esp/idf/export.sh > /dev/null 2>&1" >> ~/.bashrc
ENTRYPOINT [ "/opt/esp/entrypoint.sh" ]
CMD ["/bin/bash", "-c"]

View File

@@ -0,0 +1,19 @@
{
"name": "ESP-IDF QEMU",
"build": {
"dockerfile": "Dockerfile"
},
"customizations": {
"vscode": {
"settings": {
"terminal.integrated.defaultProfile.linux": "bash",
"idf.gitPath": "/usr/bin/git"
},
"extensions": [
"espressif.esp-idf-extension",
"espressif.esp-idf-web"
]
}
},
"runArgs": ["--privileged"]
}

78
I2C_emulator/.gitignore vendored Normal file
View File

@@ -0,0 +1,78 @@
# macOS
.DS_Store
.AppleDouble
.LSOverride
# Directory metadata
.directory
# Temporary files
*~
*.swp
*.swo
*.bak
*.tmp
# Log files
*.log
# Build artifacts and directories
**/build/
build/
*.o
*.a
*.out
*.exe # For any host-side utilities compiled on Windows
# ESP-IDF specific build outputs
*.bin
*.elf
*.map
flasher_args.json # Generated in build directory
sdkconfig.old
sdkconfig
# ESP-IDF dependencies
# For older versions or manual component management
/components/.idf/
**/components/.idf/
# For modern ESP-IDF component manager
managed_components/
# If ESP-IDF tools are installed/referenced locally to the project
.espressif/
# CMake generated files
CMakeCache.txt
CMakeFiles/
cmake_install.cmake
install_manifest.txt
CTestTestfile.cmake
# Python environment files
*.pyc
*.pyo
*.pyd
__pycache__/
*.egg-info/
dist/
# Virtual environment folders
venv/
.venv/
env/
# Language Servers
.clangd/
.ccls-cache/
compile_commands.json
# Windows specific
Thumbs.db
ehthumbs.db
Desktop.ini
# User-specific configuration files
*.user
*.workspace # General workspace files, can be from various tools
*.suo # Visual Studio Solution User Options
*.sln.docstates # Visual Studio

View File

@@ -0,0 +1,19 @@
{
"configurations": [
{
"name": "ESP-IDF",
"compilerPath": "c:\\Espressif\\tools\\tools\\xtensa-esp-elf\\esp-14.2.0_20241119\\xtensa-esp-elf\\bin\\xtensa-esp32-elf-gcc.exe",
"compileCommands": "${config:idf.buildPath}/compile_commands.json",
"includePath": [
"${workspaceFolder}/**"
],
"browse": {
"path": [
"${workspaceFolder}"
],
"limitSymbolsToIncludedHeaders": true
}
}
],
"version": 4
}

10
I2C_emulator/.vscode/launch.json vendored Normal file
View File

@@ -0,0 +1,10 @@
{
"version": "0.2.0",
"configurations": [
{
"type": "gdbtarget",
"request": "attach",
"name": "Eclipse CDT GDB Adapter"
}
]
}

11
I2C_emulator/.vscode/settings.json vendored Normal file
View File

@@ -0,0 +1,11 @@
{
"C_Cpp.intelliSenseEngine": "default",
"idf.openOcdConfigs": [
"board/esp32-wrover-kit-3.3v.cfg"
],
"idf.portWin": "detect",
"idf.currentSetup": "C:\\Users\\mahmo\\esp\\v5.4\\esp-idf",
"idf.customExtraVars": {
"IDF_TARGET": "esp32"
}
}

View File

@@ -0,0 +1,6 @@
# The following five lines of boilerplate have to be in your project's
# CMakeLists in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.16)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(I2C)

69
I2C_emulator/README.md Normal file
View File

@@ -0,0 +1,69 @@
| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-C61 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 |
| ----------------- | ----- | -------- | -------- | -------- | -------- | --------- | -------- | -------- | -------- | -------- |
# Blink Example
(See the README.md file in the upper level 'examples' directory for more information about examples.)
This example demonstrates how to blink a LED by using the GPIO driver or using the [led_strip](https://components.espressif.com/component/espressif/led_strip) library if the LED is addressable e.g. [WS2812](https://cdn-shop.adafruit.com/datasheets/WS2812B.pdf). The `led_strip` library is installed via [component manager](main/idf_component.yml).
## How to Use Example
Before project configuration and build, be sure to set the correct chip target using `idf.py set-target <chip_name>`.
### Hardware Required
* A development board with normal LED or addressable LED on-board (e.g., ESP32-S3-DevKitC, ESP32-C6-DevKitC etc.)
* A USB cable for Power supply and programming
See [Development Boards](https://www.espressif.com/en/products/devkits) for more information about it.
### Configure the Project
Open the project configuration menu (`idf.py menuconfig`).
In the `Example Configuration` menu:
* Select the LED type in the `Blink LED type` option.
* Use `GPIO` for regular LED
* Use `LED strip` for addressable LED
* If the LED type is `LED strip`, select the backend peripheral
* `RMT` is only available for ESP targets with RMT peripheral supported
* `SPI` is available for all ESP targets
* Set the GPIO number used for the signal in the `Blink GPIO number` option.
* Set the blinking period in the `Blink period in ms` option.
### Build and Flash
Run `idf.py -p PORT flash monitor` to build, flash and monitor the project.
(To exit the serial monitor, type ``Ctrl-]``.)
See the [Getting Started Guide](https://docs.espressif.com/projects/esp-idf/en/latest/get-started/index.html) for full steps to configure and use ESP-IDF to build projects.
## Example Output
As you run the example, you will see the LED blinking, according to the previously defined period. For the addressable LED, you can also change the LED color by setting the `led_strip_set_pixel(led_strip, 0, 16, 16, 16);` (LED Strip, Pixel Number, Red, Green, Blue) with values from 0 to 255 in the [source file](main/blink_example_main.c).
```text
I (315) example: Example configured to blink addressable LED!
I (325) example: Turning the LED OFF!
I (1325) example: Turning the LED ON!
I (2325) example: Turning the LED OFF!
I (3325) example: Turning the LED ON!
I (4325) example: Turning the LED OFF!
I (5325) example: Turning the LED ON!
I (6325) example: Turning the LED OFF!
I (7325) example: Turning the LED ON!
I (8325) example: Turning the LED OFF!
```
Note: The color order could be different according to the LED model.
The pixel number indicates the pixel position in the LED strip. For a single LED, use 0.
## Troubleshooting
* If the LED isn't blinking, check the GPIO or the LED type selection in the `Example Configuration` menu.
For any technical queries, please open an [issue](https://github.com/espressif/esp-idf/issues) on GitHub. We will get back to you soon.

View File

@@ -0,0 +1,20 @@
dependencies:
espressif/led_strip:
component_hash: 28c6509a727ef74925b372ed404772aeedf11cce10b78c3f69b3c66799095e2d
dependencies:
- name: idf
require: private
version: '>=4.4'
source:
registry_url: https://components.espressif.com/
type: service
version: 2.5.5
idf:
source:
type: idf
version: 5.4.0
direct_dependencies:
- espressif/led_strip
manifest_hash: a9af7824fb34850fbe175d5384052634b3c00880abb2d3a7937e666d07603998
target: esp32
version: 2.0.0

View File

@@ -0,0 +1,2 @@
idf_component_register(SRCS
INCLUDE_DIRS ".")

View File

@@ -0,0 +1,49 @@
menu "Example Configuration"
orsource "$IDF_PATH/examples/common_components/env_caps/$IDF_TARGET/Kconfig.env_caps"
choice BLINK_LED
prompt "Blink LED type"
default BLINK_LED_GPIO
help
Select the LED type. A normal level controlled LED or an addressable LED strip.
The default selection is based on the Espressif DevKit boards.
You can change the default selection according to your board.
config BLINK_LED_GPIO
bool "GPIO"
config BLINK_LED_STRIP
bool "LED strip"
endchoice
choice BLINK_LED_STRIP_BACKEND
depends on BLINK_LED_STRIP
prompt "LED strip backend peripheral"
default BLINK_LED_STRIP_BACKEND_RMT if SOC_RMT_SUPPORTED
default BLINK_LED_STRIP_BACKEND_SPI
help
Select the backend peripheral to drive the LED strip.
config BLINK_LED_STRIP_BACKEND_RMT
depends on SOC_RMT_SUPPORTED
bool "RMT"
config BLINK_LED_STRIP_BACKEND_SPI
bool "SPI"
endchoice
config BLINK_GPIO
int "Blink GPIO number"
range ENV_GPIO_RANGE_MIN ENV_GPIO_OUT_RANGE_MAX
default 8
help
GPIO number (IOxx) to blink on and off the LED.
Some GPIOs are used for other purposes (flash connections, etc.) and cannot be used to blink.
config BLINK_PERIOD
int "Blink period in ms"
range 10 3600000
default 1000
help
Define the blinking period in milliseconds.
endmenu

View File

@@ -0,0 +1,3 @@
dependencies:
espressif/led_strip: "^2.4.1"
idf: "*"

118
I2C_emulator/main/main.c Normal file
View File

@@ -0,0 +1,118 @@
#include <stdio.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/i2c_slave.h"
#include "driver/uart.h"
#include "esp_log.h"
#define I2C_SLAVE_SDA_IO 21
#define I2C_SLAVE_SCL_IO 22
#define I2C_SLAVE_NUM I2C_NUM_0
#define I2C_SLAVE_ADDR 0x61
#define I2C_SLAVE_RX_BUF_LEN 128
#define I2C_SLAVE_TX_BUF_LEN 128
#define UART_NUM UART_NUM_0
#define UART_BAUD_RATE 921600
#define UART_BUF_SIZE 256
#define TAG "I2C2SERIAL"
static void i2c_slave_init(void) {
i2c_config_t conf_slave = {
.mode = I2C_MODE_SLAVE,
.sda_io_num = I2C_SLAVE_SDA_IO,
.sda_pullup_en = GPIO_PULLUP_ENABLE,
.scl_io_num = I2C_SLAVE_SCL_IO,
.scl_pullup_en = GPIO_PULLUP_ENABLE,
.slave = {
.slave_addr = I2C_SLAVE_ADDR,
.maximum_speed = 400000,
},
};
ESP_ERROR_CHECK(i2c_param_config(I2C_SLAVE_NUM, &conf_slave));
ESP_ERROR_CHECK(i2c_driver_install(I2C_SLAVE_NUM, conf_slave.mode, I2C_SLAVE_RX_BUF_LEN, I2C_SLAVE_TX_BUF_LEN, 0));
}
static void uart_init(void) {
uart_config_t uart_config = {
.baud_rate = UART_BAUD_RATE,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
.source_clk = UART_SCLK_APB,
};
ESP_ERROR_CHECK(uart_driver_install(UART_NUM, UART_BUF_SIZE, UART_BUF_SIZE, 0, NULL, 0));
ESP_ERROR_CHECK(uart_param_config(UART_NUM, &uart_config));
}
static void i2c2serial_task(void *arg) {
uint8_t i2c_rx[I2C_SLAVE_RX_BUF_LEN];
uint8_t uart_rx[UART_BUF_SIZE];
uint8_t i2c_tx[I2C_SLAVE_TX_BUF_LEN];
while (1) {
// Wait for I2C master write
int rx_len = i2c_slave_read_buffer(I2C_SLAVE_NUM, i2c_rx, sizeof(i2c_rx), pdMS_TO_TICKS(100));
if (rx_len > 0) {
// Print CMD to serial in hex
printf("CMD:");
for (int i = 0; i < rx_len; ++i) {
printf("%02X", i2c_rx[i]);
}
printf("\n");
// Wait for DATA:[hex]\n from serial, with 200ms timeout
int uart_len = 0;
int total_len = 0;
TickType_t start_tick = xTaskGetTickCount();
bool got_data = false;
while ((xTaskGetTickCount() - start_tick) < pdMS_TO_TICKS(200)) {
uart_len = uart_read_bytes(UART_NUM, uart_rx + total_len, UART_BUF_SIZE - total_len, pdMS_TO_TICKS(10));
if (uart_len > 0) {
total_len += uart_len;
// Look for a full line
char *newline = memchr(uart_rx, '\n', total_len);
if (newline) {
int line_len = newline - (char *)uart_rx + 1;
uart_rx[line_len-1] = 0; // Null-terminate
if (strncmp((char *)uart_rx, "DATA:", 5) == 0) {
// Parse hex after DATA:
char *hexstr = (char *)uart_rx + 5;
int tx_len = 0;
while (*hexstr && tx_len < I2C_SLAVE_TX_BUF_LEN) {
unsigned int val;
if (sscanf(hexstr, "%2x", &val) == 1) {
i2c_tx[tx_len++] = val;
hexstr += 2;
} else {
break;
}
}
// Write to I2C slave TX buffer
i2c_slave_write_buffer(I2C_SLAVE_NUM, i2c_tx, tx_len, 0);
got_data = true;
}
// Remove processed line
memmove(uart_rx, newline + 1, total_len - line_len);
total_len -= line_len;
break;
}
}
}
if (!got_data) {
// Timeout: clear I2C TX buffer
i2c_slave_write_buffer(I2C_SLAVE_NUM, NULL, 0, 0);
}
}
vTaskDelay(pdMS_TO_TICKS(1));
}
}
void app_main(void) {
i2c_slave_init();
uart_init();
xTaskCreate(i2c2serial_task, "i2c2serial_task", 4096, NULL, 10, NULL);
}

View File

@@ -0,0 +1,16 @@
# SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
# SPDX-License-Identifier: CC0-1.0
import logging
import os
import pytest
from pytest_embedded_idf.dut import IdfDut
@pytest.mark.supported_targets
@pytest.mark.generic
def test_blink(dut: IdfDut) -> None:
# check and log bin size
binary_file = os.path.join(dut.app.binary_path, 'blink.bin')
bin_size = os.path.getsize(binary_file)
logging.info('blink_bin_size : {}KB'.format(bin_size // 1024))

View File

@@ -0,0 +1,2 @@
CONFIG_BLINK_LED_GPIO=y
CONFIG_BLINK_GPIO=8

View File

@@ -0,0 +1 @@
CONFIG_BLINK_GPIO=5

View File

@@ -0,0 +1 @@
CONFIG_BLINK_LED_STRIP=y

View File

@@ -0,0 +1,2 @@
CONFIG_BLINK_GPIO=27
CONFIG_BLINK_LED_STRIP=y

View File

@@ -0,0 +1 @@
CONFIG_BLINK_LED_STRIP=y

View File

@@ -0,0 +1 @@
CONFIG_BLINK_LED_STRIP=y

View File

@@ -0,0 +1 @@
CONFIG_BLINK_LED_STRIP=y

View File

@@ -0,0 +1,2 @@
CONFIG_BLINK_GPIO=44
CONFIG_BLINK_LED_STRIP=y

View File

@@ -0,0 +1,2 @@
CONFIG_BLINK_LED_STRIP=y
CONFIG_BLINK_GPIO=18

View File

@@ -0,0 +1,2 @@
CONFIG_BLINK_LED_STRIP=y
CONFIG_BLINK_GPIO=48

46
check_connected_hw.sh Normal file
View File

@@ -0,0 +1,46 @@
#!/bin/bash
# Define the variables we are looking for
VARS=("EPROG" "ESP_DEBUG" "ESP_SENSOR" "I2C_EMULATOR")
echo "------------------------------------------------"
echo "🔍 Starting ESP Hardware Verification..."
echo "------------------------------------------------"
FAILED=0
for VAR_NAME in "${VARS[@]}"; do
# 1. Check if Environment Variable exists
PORT_PATH="${!VAR_NAME}"
if [ -z "$PORT_PATH" ]; then
echo "❌ [ERROR]: \$$VAR_NAME is not set in environment."
FAILED=1
continue
fi
# 2. Check if the device node exists
if [ -e "$PORT_PATH" ]; then
# 3. Check for Read/Write permissions
if [ -r "$PORT_PATH" ] && [ -w "$PORT_PATH" ]; then
TARGET=$(readlink -f "$PORT_PATH")
echo "✅ [OK]: $VAR_NAME is active at $PORT_PATH -> ($TARGET)"
else
echo "⚠️ [PERM]: $VAR_NAME found at $PORT_PATH but NO R/W PERMISSION!"
echo " Run: sudo chmod 666 $PORT_PATH"
FAILED=1
fi
else
echo "❌ [MISSING]: $VAR_NAME ($PORT_PATH) is NOT connected."
FAILED=1
fi
done
echo "------------------------------------------------"
if [ $FAILED -eq 0 ]; then
echo "🚀 ALL SYSTEMS GO: Ready for automation."
exit 0
else
echo "🛑 STOP: Please fix the errors above before running tests."
exit 1
fi