init
This commit is contained in:
13
I2C_emulator/.devcontainer/Dockerfile
Normal file
13
I2C_emulator/.devcontainer/Dockerfile
Normal 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"]
|
||||
19
I2C_emulator/.devcontainer/devcontainer.json
Normal file
19
I2C_emulator/.devcontainer/devcontainer.json
Normal 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
78
I2C_emulator/.gitignore
vendored
Normal 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_emulator/.vscode/c_cpp_properties.json
vendored
Normal file
19
I2C_emulator/.vscode/c_cpp_properties.json
vendored
Normal 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
10
I2C_emulator/.vscode/launch.json
vendored
Normal 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
11
I2C_emulator/.vscode/settings.json
vendored
Normal 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_emulator/CMakeLists.txt
Normal file
6
I2C_emulator/CMakeLists.txt
Normal 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
69
I2C_emulator/README.md
Normal 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.
|
||||
20
I2C_emulator/dependencies.lock
Normal file
20
I2C_emulator/dependencies.lock
Normal 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_emulator/main/CMakeLists.txt
Normal file
2
I2C_emulator/main/CMakeLists.txt
Normal file
@@ -0,0 +1,2 @@
|
||||
idf_component_register(SRCS
|
||||
INCLUDE_DIRS ".")
|
||||
49
I2C_emulator/main/Kconfig.projbuild
Normal file
49
I2C_emulator/main/Kconfig.projbuild
Normal 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
|
||||
3
I2C_emulator/main/idf_component.yml
Normal file
3
I2C_emulator/main/idf_component.yml
Normal file
@@ -0,0 +1,3 @@
|
||||
dependencies:
|
||||
espressif/led_strip: "^2.4.1"
|
||||
idf: "*"
|
||||
118
I2C_emulator/main/main.c
Normal file
118
I2C_emulator/main/main.c
Normal 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);
|
||||
}
|
||||
16
I2C_emulator/pytest_blink.py
Normal file
16
I2C_emulator/pytest_blink.py
Normal 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_emulator/sdkconfig.defaults
Normal file
2
I2C_emulator/sdkconfig.defaults
Normal file
@@ -0,0 +1,2 @@
|
||||
CONFIG_BLINK_LED_GPIO=y
|
||||
CONFIG_BLINK_GPIO=8
|
||||
1
I2C_emulator/sdkconfig.defaults.esp32
Normal file
1
I2C_emulator/sdkconfig.defaults.esp32
Normal file
@@ -0,0 +1 @@
|
||||
CONFIG_BLINK_GPIO=5
|
||||
1
I2C_emulator/sdkconfig.defaults.esp32c3
Normal file
1
I2C_emulator/sdkconfig.defaults.esp32c3
Normal file
@@ -0,0 +1 @@
|
||||
CONFIG_BLINK_LED_STRIP=y
|
||||
2
I2C_emulator/sdkconfig.defaults.esp32c5
Normal file
2
I2C_emulator/sdkconfig.defaults.esp32c5
Normal file
@@ -0,0 +1,2 @@
|
||||
CONFIG_BLINK_GPIO=27
|
||||
CONFIG_BLINK_LED_STRIP=y
|
||||
1
I2C_emulator/sdkconfig.defaults.esp32c6
Normal file
1
I2C_emulator/sdkconfig.defaults.esp32c6
Normal file
@@ -0,0 +1 @@
|
||||
CONFIG_BLINK_LED_STRIP=y
|
||||
1
I2C_emulator/sdkconfig.defaults.esp32c61
Normal file
1
I2C_emulator/sdkconfig.defaults.esp32c61
Normal file
@@ -0,0 +1 @@
|
||||
CONFIG_BLINK_LED_STRIP=y
|
||||
1
I2C_emulator/sdkconfig.defaults.esp32h2
Normal file
1
I2C_emulator/sdkconfig.defaults.esp32h2
Normal file
@@ -0,0 +1 @@
|
||||
CONFIG_BLINK_LED_STRIP=y
|
||||
2
I2C_emulator/sdkconfig.defaults.esp32p4
Normal file
2
I2C_emulator/sdkconfig.defaults.esp32p4
Normal file
@@ -0,0 +1,2 @@
|
||||
CONFIG_BLINK_GPIO=44
|
||||
CONFIG_BLINK_LED_STRIP=y
|
||||
2
I2C_emulator/sdkconfig.defaults.esp32s2
Normal file
2
I2C_emulator/sdkconfig.defaults.esp32s2
Normal file
@@ -0,0 +1,2 @@
|
||||
CONFIG_BLINK_LED_STRIP=y
|
||||
CONFIG_BLINK_GPIO=18
|
||||
2
I2C_emulator/sdkconfig.defaults.esp32s3
Normal file
2
I2C_emulator/sdkconfig.defaults.esp32s3
Normal file
@@ -0,0 +1,2 @@
|
||||
CONFIG_BLINK_LED_STRIP=y
|
||||
CONFIG_BLINK_GPIO=48
|
||||
Reference in New Issue
Block a user