initial commit
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
# 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.5)
|
||||
|
||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||
|
||||
set(EXTRA_COMPONENT_DIRS ../../../SensorLib)
|
||||
|
||||
project(ESP_IDF_TouchDrvExample)
|
||||
@@ -0,0 +1,10 @@
|
||||
#
|
||||
# This is a project Makefile. It is assumed the directory this Makefile resides in is a
|
||||
# project subdirectory.
|
||||
#
|
||||
|
||||
PROJECT_NAME := ESP_IDF_TouchDrv_Example
|
||||
|
||||
EXTRA_COMPONENT_DIRS = ../../../SensorLib
|
||||
|
||||
include $(IDF_PATH)/make/project.mk
|
||||
@@ -0,0 +1,154 @@
|
||||
# ESP-IDF SensorLib TouchDrv examples
|
||||
|
||||
```bash
|
||||
The current example only writes the CSTxxx series touch application.
|
||||
```
|
||||
|
||||
## Configure the Project
|
||||
|
||||
Open the project configuration menu (`idf.py menuconfig`).
|
||||
|
||||
|
||||
In the `SensorLib Example Configuration` menu:
|
||||
|
||||
* Select the communication method, will interface, callback interface, LL new version interface
|
||||
1. Implemented using built-in read and write methods (Read and write methods are provided internally by SensorLib, supporting high version esp-idf >= 5.0, and low version methods (< 5.0))
|
||||
2. Implemented using read and write callback methods (Implemented using externally provided methods, suitable for multiple platforms)
|
||||
* In `Sensor SCL GPIO Num` select the clock pin to connect to the PMU,the default is 7
|
||||
* In `Sensor SDA GPIO Num` select the data pin connected to the PMU,the default is 6
|
||||
* Select the interrupt pin connected to the PMU in `Sensor Interrupt Pin`, the default is 8
|
||||
* Select `Sensor reset Pin` , the default is 17
|
||||
* `Master Frequency` The maximum communication frequency defaults to 100000HZ, and you can decide whether to change it according to the situation.
|
||||
|
||||
## How to Use Example
|
||||
|
||||
Before project configuration and build, be sure to set the correct chip target using `idf.py set-target <chip_name>`. default use **esp32s3**
|
||||
|
||||
## 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
|
||||
|
||||
The output information is to configure the output voltage and enable status of the PMU
|
||||
|
||||
```bash
|
||||
SPIWP:0xee
|
||||
mode:DIO, clock div:1
|
||||
load:0x3fce2810,len:0x178c
|
||||
load:0x403c8700,len:0x4
|
||||
load:0x403c8704,len:0xc10
|
||||
load:0x403cb700,len:0x2dac
|
||||
entry 0x403c8904
|
||||
I (26) boot: ESP-IDF v5.3-beta1-105-g3f632df143-dirt 2nd stage bootloader
|
||||
I (27) boot: compile time Jan 23 2025 16:49:34
|
||||
I (27) boot: Multicore bootloader
|
||||
I (27) boot: chip revision: v0.2
|
||||
I (27) boot.esp32s3: Boot SPI Speed : 80MHz
|
||||
I (28) boot.esp32s3: SPI Mode : DIO
|
||||
I (28) boot.esp32s3: SPI Flash Size : 2MB
|
||||
I (28) boot: Enabling RNG early entropy source...
|
||||
I (28) boot: Partition Table:
|
||||
I (28) boot: ## Label Usage Type ST Offset Length
|
||||
I (29) boot: 0 nvs WiFi data 01 02 00009000 00006000
|
||||
I (29) boot: 1 phy_init RF data 01 01 0000f000 00001000
|
||||
I (30) boot: 2 factory factory app 00 00 00010000 00100000
|
||||
I (30) boot: End of partition table
|
||||
I (30) esp_image: segment 0: paddr=00010020 vaddr=3c030020 size=0ca94h ( 51860) map
|
||||
I (40) esp_image: segment 1: paddr=0001cabc vaddr=3fc92e00 size=02a38h ( 10808) load
|
||||
I (43) esp_image: segment 2: paddr=0001f4fc vaddr=40374000 size=00b1ch ( 2844) load
|
||||
I (44) esp_image: segment 3: paddr=00020020 vaddr=42000020 size=21020h (135200) map
|
||||
I (69) esp_image: segment 4: paddr=00041048 vaddr=40374b1c size=0e254h ( 57940) load
|
||||
I (88) boot: Loaded app from partition at offset 0x10000
|
||||
I (88) boot: Disabling RNG early entropy source...
|
||||
I (88) cpu_start: Multicore app
|
||||
I (98) cpu_start: Pro cpu start user code
|
||||
I (98) cpu_start: cpu freq: 240000000 Hz
|
||||
I (98) app_init: Application information:
|
||||
I (98) app_init: Project name: ESP_IDF_TouchDrvExample
|
||||
I (98) app_init: App version: v0.2.5-8-gde9a1d2-dirty
|
||||
I (99) app_init: Compile time: Jan 23 2025 16:51:47
|
||||
I (99) app_init: ELF file SHA256: 179a53fa8...
|
||||
I (99) app_init: ESP-IDF: v5.3-beta1-105-g3f632df143-dirt
|
||||
I (99) efuse_init: Min chip rev: v0.0
|
||||
I (99) efuse_init: Max chip rev: v0.99
|
||||
I (100) efuse_init: Chip rev: v0.2
|
||||
I (100) heap_init: Initializing. RAM available for dynamic allocation:
|
||||
I (100) heap_init: At 3FC96158 len 000535B8 (333 KiB): RAM
|
||||
I (100) heap_init: At 3FCE9710 len 00005724 (21 KiB): RAM
|
||||
I (101) heap_init: At 3FCF0000 len 00008000 (32 KiB): DRAM
|
||||
I (101) heap_init: At 600FE100 len 00001EE8 (7 KiB): RTCRAM
|
||||
I (102) spi_flash: detected chip: winbond
|
||||
I (102) spi_flash: flash io: dio
|
||||
W (102) spi_flash: Detected size(16384k) larger than the size in the binary image header(2048k). Using the size in the binary image header.
|
||||
I (103) sleep: Configure to isolate all GPIO pins in sleep state
|
||||
I (103) sleep: Enable automatic switching of GPIO sleep configuration
|
||||
I (104) main_task: Started on CPU0
|
||||
I (114) main_task: Calling app_main()
|
||||
I (114) I2C: Implemented using read and write callback methods (Use higher version >= 5.0 API)
|
||||
I (114) gpio: GPIO[6]| InputEn: 1| OutputEn: 1| OpenDrain: 1| Pullup: 1| Pulldown: 0| Intr:0
|
||||
I (114) gpio: GPIO[7]| InputEn: 1| OutputEn: 1| OpenDrain: 1| Pullup: 1| Pulldown: 0| Intr:0
|
||||
I (114) main: I2C initialized successfully
|
||||
I (114) TOUCH: ----SensorLib TouchDrv Examples----
|
||||
Scand I2C Devices:
|
||||
0 1 2 3 4 5 6 7 8 9 a b c d e f
|
||||
00: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
|
||||
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
|
||||
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
|
||||
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
|
||||
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
|
||||
50: -- -- -- -- -- -- -- -- -- -- 5a -- -- -- -- --
|
||||
60: -- -- -- -- -- -- -- -- -- -- 6a -- -- -- -- --
|
||||
70: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
|
||||
|
||||
|
||||
|
||||
I (224) TOUCH: Find touch address 0x5A
|
||||
I (224) TOUCH: Implemented using built-in read and write methods (Use higher version >= 5.0 API)
|
||||
I (224) SensorLib: Using ESP-IDF Driver interface.
|
||||
I (224) SensorLib: Added Device Address : 0x5A New Dev Address: 0x3fc9a3a0 Speed :400000
|
||||
I (224) TOUCH: Initializing the capacitive touch screen successfully
|
||||
I (224) TOUCH: Using CST226SE model
|
||||
I (224) gpio: GPIO[8]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
|
||||
I (224) main: Run...
|
||||
I (224) main_task: Returned from app_main()
|
||||
X[0]:143 Y[0]:167
|
||||
X[0]:143 Y[0]:167
|
||||
X[0]:143 Y[0]:167
|
||||
X[0]:143 Y[0]:167
|
||||
X[0]:143 Y[0]:167
|
||||
X[0]:143 Y[0]:167
|
||||
X[0]:143 Y[0]:167
|
||||
X[0]:143 Y[0]:167
|
||||
X[0]:143 Y[0]:167
|
||||
X[0]:143 Y[0]:167
|
||||
```
|
||||
|
||||
## Build process example
|
||||
|
||||
Assuming you don't have esp-idf yet
|
||||
|
||||
```bash
|
||||
mkdir -p ~/esp
|
||||
cd ~/esp
|
||||
git clone --recursive https://github.com/espressif/esp-idf.git
|
||||
git clone https://github.com/lewisxhe/SensorLib.git
|
||||
cd esp-idf
|
||||
./install.sh
|
||||
. ./export.sh
|
||||
cd ..
|
||||
cd SensorLib/examples/ESP_IDF_TouchDrvExample
|
||||
|
||||
Configure SDA,SCL,INT,RST,Pin
|
||||
...
|
||||
|
||||
idf.py menuconfig
|
||||
idf.py build
|
||||
idf.py -b 921600 flash
|
||||
idf.py monitor
|
||||
|
||||
```
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
idf_component_register(SRCS
|
||||
"touch_drv.cpp"
|
||||
"i2c_driver.cpp"
|
||||
"main.cpp"
|
||||
INCLUDE_DIRS ".")
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
menu "SensorLib Example Configuration"
|
||||
|
||||
choice I2C_COMMUNICATION_METHOD
|
||||
prompt "SensorLib read and write methods"
|
||||
default I2C_COMMUNICATION_METHOD_BUILTIN_RW
|
||||
help
|
||||
Define SensorLib read and write methods
|
||||
|
||||
config I2C_COMMUNICATION_METHOD_BUILTIN_RW
|
||||
bool "Implemented using built-in read and write methods"
|
||||
config I2C_COMMUNICATION_METHOD_CALLBACK_RW
|
||||
bool "Implemented using read and write callback methods"
|
||||
endchoice
|
||||
|
||||
config I2C_MASTER_PORT_NUM
|
||||
int "Sensor I2C Port Number"
|
||||
default 1
|
||||
help
|
||||
Port number for I2C Master device.
|
||||
|
||||
config I2C_MASTER_FREQUENCY
|
||||
int "Master Frequency"
|
||||
default 100000
|
||||
help
|
||||
I2C Speed of Master device.
|
||||
|
||||
config SENSOR_SCL
|
||||
int "Sensor SCL GPIO Num"
|
||||
default 7
|
||||
help
|
||||
GPIO number for I2C clock line.
|
||||
|
||||
config SENSOR_SDA
|
||||
int "Sensor SDA GPIO Num"
|
||||
default 6
|
||||
help
|
||||
GPIO number for I2C data line.
|
||||
|
||||
config SENSOR_IRQ
|
||||
int "Sensor Interrupt Pin"
|
||||
default 8
|
||||
help
|
||||
Sensor interrupt pin.
|
||||
|
||||
config SENSOR_RST
|
||||
int "Sensor reset Pin"
|
||||
default 17
|
||||
help
|
||||
Sensor reset pin.
|
||||
|
||||
|
||||
endmenu
|
||||
@@ -0,0 +1,4 @@
|
||||
#
|
||||
# "main" pseudo-component makefile.
|
||||
#
|
||||
# (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.)
|
||||
+275
@@ -0,0 +1,275 @@
|
||||
/**
|
||||
*
|
||||
* @license MIT License
|
||||
*
|
||||
* Copyright (c) 2025 lewis he
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*
|
||||
* @file port_i2c.cpp
|
||||
* @author Lewis He (lewishe@outlook.com)
|
||||
* @date 2025-01-22
|
||||
*
|
||||
*/
|
||||
#include <cstring>
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "driver/gpio.h"
|
||||
#include "esp_log.h"
|
||||
#include "sdkconfig.h"
|
||||
#include "i2c_driver.h"
|
||||
|
||||
static const char *TAG = "I2C";
|
||||
|
||||
#define I2C_MASTER_NUM (i2c_port_t)CONFIG_I2C_MASTER_PORT_NUM
|
||||
#define I2C_MASTER_SDA_IO (gpio_num_t)CONFIG_SENSOR_SDA
|
||||
#define I2C_MASTER_SCL_IO (gpio_num_t)CONFIG_SENSOR_SCL
|
||||
#define I2C_MASTER_FREQ_HZ CONFIG_I2C_MASTER_FREQUENCY /*!< I2C master clock frequency */
|
||||
#define I2C_MASTER_TX_BUF_DISABLE 0 /*!< I2C master doesn't need buffer */
|
||||
#define I2C_MASTER_RX_BUF_DISABLE 0 /*!< I2C master doesn't need buffer */
|
||||
|
||||
#if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5,0,0)) && defined(CONFIG_SENSORLIB_ESP_IDF_NEW_API)
|
||||
#include "soc/clk_tree_defs.h"
|
||||
|
||||
i2c_master_dev_handle_t i2c_device;
|
||||
|
||||
// * Using the new API of esp-idf 5.x, you need to pass the I2C BUS handle,
|
||||
// * which is useful when the bus shares multiple devices.
|
||||
i2c_master_bus_handle_t bus_handle;
|
||||
#endif
|
||||
|
||||
|
||||
#if CONFIG_I2C_COMMUNICATION_METHOD_CALLBACK_RW
|
||||
|
||||
/**
|
||||
* @brief Read a sequence of bytes from a pmu registers
|
||||
*/
|
||||
int i2c_read_callback(uint8_t devAddr, uint8_t regAddr, uint8_t *data, uint8_t len)
|
||||
{
|
||||
esp_err_t ret;
|
||||
if (len == 0) {
|
||||
return ESP_OK;
|
||||
}
|
||||
if (data == NULL) {
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
#if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5,0,0)) && defined(CONFIG_SENSORLIB_ESP_IDF_NEW_API)
|
||||
|
||||
ret = i2c_master_transmit_receive(
|
||||
i2c_device,
|
||||
(const uint8_t *)®Addr,
|
||||
1,
|
||||
data,
|
||||
len,
|
||||
-1);
|
||||
#else
|
||||
|
||||
i2c_cmd_handle_t cmd;
|
||||
|
||||
cmd = i2c_cmd_link_create();
|
||||
i2c_master_start(cmd);
|
||||
i2c_master_write_byte(cmd, (devAddr << 1) | I2C_MASTER_WRITE, true);
|
||||
i2c_master_write_byte(cmd, regAddr, true);
|
||||
i2c_master_stop(cmd);
|
||||
ret = i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, pdTICKS_TO_MS(1000));
|
||||
i2c_cmd_link_delete(cmd);
|
||||
if (ret != ESP_OK) {
|
||||
ESP_LOGE(TAG, "PMU i2c_master_cmd_begin FAILED! > ");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
cmd = i2c_cmd_link_create();
|
||||
i2c_master_start(cmd);
|
||||
i2c_master_write_byte(cmd, (devAddr << 1) | I2C_MASTER_READ, true);
|
||||
if (len > 1) {
|
||||
i2c_master_read(cmd, data, len - 1, I2C_MASTER_ACK);
|
||||
}
|
||||
i2c_master_read_byte(cmd, &data[len - 1], I2C_MASTER_NACK);
|
||||
i2c_master_stop(cmd);
|
||||
ret = i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, pdTICKS_TO_MS(1000));
|
||||
i2c_cmd_link_delete(cmd);
|
||||
if (ret != ESP_OK) {
|
||||
ESP_LOGE(TAG, "i2c_read_error.");
|
||||
}
|
||||
#endif
|
||||
return ret == ESP_OK ? 0 : -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Write a byte to a pmu register
|
||||
*/
|
||||
int i2c_write_callback(uint8_t devAddr, uint8_t regAddr, uint8_t *data, uint8_t len)
|
||||
{
|
||||
esp_err_t ret;
|
||||
if (data == NULL) {
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
#if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5,0,0)) && defined(CONFIG_SENSORLIB_ESP_IDF_NEW_API)
|
||||
uint8_t *write_buffer = (uint8_t *)malloc(sizeof(uint8_t) * (len + 1));
|
||||
if (!write_buffer) {
|
||||
return -1;
|
||||
}
|
||||
write_buffer[0] = regAddr;
|
||||
memcpy(write_buffer + 1, data, len);
|
||||
|
||||
ret = i2c_master_transmit(
|
||||
i2c_device,
|
||||
write_buffer,
|
||||
len + 1,
|
||||
-1);
|
||||
free(write_buffer);
|
||||
#else
|
||||
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
|
||||
i2c_master_start(cmd);
|
||||
i2c_master_write_byte(cmd, (devAddr << 1) | I2C_MASTER_WRITE, true);
|
||||
i2c_master_write_byte(cmd, regAddr, true);
|
||||
i2c_master_write(cmd, data, len, true);
|
||||
i2c_master_stop(cmd);
|
||||
ret = i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, pdTICKS_TO_MS(1000));
|
||||
i2c_cmd_link_delete(cmd);
|
||||
if (ret != ESP_OK) {
|
||||
ESP_LOGE(TAG, "i2c_write_error.");
|
||||
}
|
||||
#endif
|
||||
return ret == ESP_OK ? 0 : -1;
|
||||
}
|
||||
|
||||
esp_err_t i2c_drv_device_init(uint8_t address)
|
||||
{
|
||||
#if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5,0,0)) && defined(CONFIG_SENSORLIB_ESP_IDF_NEW_API)
|
||||
i2c_device_config_t i2c_dev_conf = {
|
||||
.dev_addr_length = I2C_ADDR_BIT_LEN_7,
|
||||
.device_address = address,
|
||||
.scl_speed_hz = I2C_MASTER_FREQ_HZ,
|
||||
};
|
||||
return i2c_master_bus_add_device(bus_handle, &i2c_dev_conf, &i2c_device);
|
||||
#else
|
||||
return ESP_OK;
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif //CONFIG_I2C_COMMUNICATION_METHOD_CALLBACK_RW
|
||||
|
||||
|
||||
/**
|
||||
* @brief i2c master initialization
|
||||
*/
|
||||
esp_err_t i2c_drv_init(void)
|
||||
{
|
||||
#if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5,0,0)) && defined(CONFIG_SENSORLIB_ESP_IDF_NEW_API)
|
||||
|
||||
ESP_LOGI(TAG, "Implemented using read and write callback methods (Use higher version >= 5.0 API)");
|
||||
|
||||
i2c_master_bus_config_t i2c_bus_config;
|
||||
memset(&i2c_bus_config, 0, sizeof(i2c_bus_config));
|
||||
i2c_bus_config.clk_source = I2C_CLK_SRC_DEFAULT;
|
||||
i2c_bus_config.i2c_port = I2C_MASTER_NUM;
|
||||
i2c_bus_config.scl_io_num = (gpio_num_t)I2C_MASTER_SCL_IO;
|
||||
i2c_bus_config.sda_io_num = (gpio_num_t)I2C_MASTER_SDA_IO;
|
||||
i2c_bus_config.glitch_ignore_cnt = 7;
|
||||
i2c_bus_config.flags.enable_internal_pullup = true;
|
||||
ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_config, &bus_handle));
|
||||
|
||||
return ESP_OK;
|
||||
#else
|
||||
|
||||
ESP_LOGI(TAG, "Implemented using read and write callback methods (Use lower version < 5.0 API)");
|
||||
|
||||
i2c_config_t i2c_conf ;
|
||||
memset(&i2c_conf, 0, sizeof(i2c_conf));
|
||||
i2c_conf.mode = I2C_MODE_MASTER;
|
||||
i2c_conf.sda_io_num = I2C_MASTER_SDA_IO;
|
||||
i2c_conf.scl_io_num = I2C_MASTER_SCL_IO;
|
||||
i2c_conf.sda_pullup_en = GPIO_PULLUP_ENABLE;
|
||||
i2c_conf.scl_pullup_en = GPIO_PULLUP_ENABLE;
|
||||
i2c_conf.master.clk_speed = I2C_MASTER_FREQ_HZ;
|
||||
i2c_param_config(I2C_MASTER_NUM, &i2c_conf);
|
||||
return i2c_driver_install(I2C_MASTER_NUM, i2c_conf.mode, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0);
|
||||
#endif
|
||||
}
|
||||
|
||||
void i2c_drv_scan()
|
||||
{
|
||||
#if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5,0,0)) && defined(CONFIG_SENSORLIB_ESP_IDF_NEW_API)
|
||||
|
||||
esp_err_t err = ESP_OK;
|
||||
uint8_t address = 0x00;
|
||||
printf("Scand I2C Devices:\n");
|
||||
printf(" 0 1 2 3 4 5 6 7 8 9 a b c d e f\r\n");
|
||||
for (int i = 0; i < 128; i += 16) {
|
||||
printf("%02x: ", i);
|
||||
for (int j = 0; j < 16; j++) {
|
||||
fflush(stdout);
|
||||
address = i + j;
|
||||
err = i2c_master_probe(bus_handle, address, 1000);
|
||||
if (err == ESP_OK) {
|
||||
printf("%02x ", address);
|
||||
} else if (err == ESP_ERR_TIMEOUT) {
|
||||
printf("UU ");
|
||||
} else {
|
||||
printf("-- ");
|
||||
}
|
||||
}
|
||||
printf("\r\n");
|
||||
}
|
||||
printf("\n\n\n");
|
||||
#else
|
||||
uint8_t address;
|
||||
printf("Scand I2C Devices:\n");
|
||||
printf(" 0 1 2 3 4 5 6 7 8 9 a b c d e f\r\n");
|
||||
for (int i = 0; i < 128; i += 16) {
|
||||
printf("%02x: ", i);
|
||||
for (int j = 0; j < 16; j++) {
|
||||
fflush(stdout);
|
||||
address = i + j;
|
||||
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
|
||||
i2c_master_start(cmd);
|
||||
i2c_master_write_byte(cmd, (address << 1) | I2C_MASTER_WRITE, true);
|
||||
i2c_master_stop(cmd);
|
||||
esp_err_t ret = i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, 50 / portTICK_PERIOD_MS);
|
||||
i2c_cmd_link_delete(cmd);
|
||||
if (ret == ESP_OK) {
|
||||
printf("%02x ", address);
|
||||
} else if (ret == ESP_ERR_TIMEOUT) {
|
||||
printf("UU ");
|
||||
} else {
|
||||
printf("-- ");
|
||||
}
|
||||
}
|
||||
printf("\r\n");
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
bool i2c_drv_probe(uint8_t devAddr)
|
||||
{
|
||||
#if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5,0,0)) && defined(CONFIG_SENSORLIB_ESP_IDF_NEW_API)
|
||||
return ESP_OK == i2c_master_probe(bus_handle, devAddr, 1000);
|
||||
#else
|
||||
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
|
||||
i2c_master_start(cmd);
|
||||
i2c_master_write_byte(cmd, (devAddr << 1) | I2C_MASTER_WRITE, true);
|
||||
i2c_master_stop(cmd);
|
||||
esp_err_t ret = i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, 50 / portTICK_PERIOD_MS);
|
||||
i2c_cmd_link_delete(cmd);
|
||||
return (ret == ESP_OK);
|
||||
#endif //ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5,0,0)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
/**
|
||||
*
|
||||
* @license MIT License
|
||||
*
|
||||
* Copyright (c) 2025 lewis he
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*
|
||||
* @file i2c_driver.h
|
||||
* @author Lewis He (lewishe@outlook.com)
|
||||
* @date 2025-01-22
|
||||
*
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include "esp_err.h"
|
||||
#include "esp_log.h"
|
||||
#include "driver/gpio.h"
|
||||
#include "esp_idf_version.h"
|
||||
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5,0,0) && defined(CONFIG_SENSORLIB_ESP_IDF_NEW_API)
|
||||
#include "driver/i2c_master.h"
|
||||
#else
|
||||
#include "driver/i2c.h"
|
||||
#endif //ESP_IDF_VERSION
|
||||
|
||||
|
||||
esp_err_t i2c_drv_init(void);
|
||||
void i2c_drv_scan();
|
||||
bool i2c_drv_probe(uint8_t devAddr);
|
||||
int i2c_read_callback(uint8_t devAddr, uint8_t regAddr, uint8_t *data, uint8_t len);
|
||||
int i2c_write_callback(uint8_t devAddr, uint8_t regAddr, uint8_t *data, uint8_t len);
|
||||
|
||||
#if CONFIG_I2C_COMMUNICATION_METHOD_CALLBACK_RW
|
||||
esp_err_t i2c_drv_device_init(uint8_t address);
|
||||
#endif
|
||||
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
/**
|
||||
*
|
||||
* @license MIT License
|
||||
*
|
||||
* Copyright (c) 2025 lewis he
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*
|
||||
* @file main.cpp
|
||||
* @author Lewis He (lewishe@outlook.com)
|
||||
* @date 2025-01-22
|
||||
*
|
||||
*/
|
||||
#include "sdkconfig.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "driver/gpio.h"
|
||||
#include "freertos/queue.h"
|
||||
#include "i2c_driver.h"
|
||||
|
||||
static const char *TAG = "main";
|
||||
|
||||
extern esp_err_t sensor_drv_init();
|
||||
extern esp_err_t touch_drv_init();
|
||||
extern void touch_loop();
|
||||
static void touch_task(void *);
|
||||
|
||||
extern "C" void app_main(void)
|
||||
{
|
||||
ESP_ERROR_CHECK(i2c_drv_init());
|
||||
ESP_LOGI(TAG, "I2C initialized successfully");
|
||||
|
||||
ESP_ERROR_CHECK(touch_drv_init());
|
||||
|
||||
xTaskCreate(touch_task, "touch", 4 * 1024, NULL, 10, NULL);
|
||||
|
||||
ESP_LOGI(TAG, "Run...");
|
||||
}
|
||||
|
||||
|
||||
static void touch_task(void *args)
|
||||
{
|
||||
while (1) {
|
||||
touch_loop();
|
||||
// vTaskDelay(pdMS_TO_TICKS(10));
|
||||
vTaskDelay(1 / portTICK_PERIOD_MS);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+227
@@ -0,0 +1,227 @@
|
||||
/**
|
||||
*
|
||||
* @license MIT License
|
||||
*
|
||||
* Copyright (c) 2025 lewis he
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*
|
||||
* @file touch_drv.cpp
|
||||
* @author Lewis He (lewishe@outlook.com)
|
||||
* @date 2025-01-22
|
||||
*
|
||||
*/
|
||||
#include "sdkconfig.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_err.h"
|
||||
#include "i2c_driver.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "TouchDrvCSTXXX.hpp"
|
||||
|
||||
static const char *TAG = "TOUCH";
|
||||
|
||||
#define SENSOR_INPUT_PIN (gpio_num_t)CONFIG_SENSOR_IRQ
|
||||
#define SENSOR_INPUT_PIN_SEL (1ULL<<SENSOR_INPUT_PIN)
|
||||
|
||||
TouchDrvCSTXXX touch;
|
||||
static QueueHandle_t xQueue;
|
||||
|
||||
static void touch_home_button_callback(void *user_data)
|
||||
{
|
||||
ESP_LOGI(TAG, "Touch Home button pressed!");
|
||||
}
|
||||
|
||||
static void IRAM_ATTR sensor_irq_handler(void *arg)
|
||||
{
|
||||
uint32_t gpio_num = (uint32_t) arg;
|
||||
xQueueSendFromISR(xQueue, &gpio_num, NULL);
|
||||
}
|
||||
|
||||
static void irq_init()
|
||||
{
|
||||
gpio_config_t io_conf;
|
||||
io_conf.intr_type = GPIO_INTR_DISABLE;
|
||||
io_conf.mode = GPIO_MODE_INPUT;
|
||||
io_conf.pin_bit_mask = SENSOR_INPUT_PIN_SEL;
|
||||
io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
|
||||
io_conf.pull_up_en = GPIO_PULLUP_ENABLE;
|
||||
gpio_config(&io_conf);
|
||||
// gpio_set_intr_type(SENSOR_INPUT_PIN, GPIO_INTR_NEGEDGE);
|
||||
// //install gpio isr service
|
||||
// gpio_install_isr_service(0);
|
||||
// //hook isr handler for specific gpio pin
|
||||
// gpio_isr_handler_add(SENSOR_INPUT_PIN, sensor_irq_handler, (void *) SENSOR_INPUT_PIN);
|
||||
|
||||
}
|
||||
|
||||
esp_err_t touch_drv_init()
|
||||
{
|
||||
uint8_t address = 0xFF;
|
||||
|
||||
ESP_LOGI(TAG, "----SensorLib TouchDrv Examples----");
|
||||
|
||||
/*
|
||||
* CST816, CST328 Some chips have automatic sleep function.
|
||||
* If you want to scan the device address,
|
||||
* must reset the chip first. If the reset pin is not connected,
|
||||
* you may not be able to obtain the device address by scanning the bus.
|
||||
*/
|
||||
gpio_num_t gpio_num = (gpio_num_t)CONFIG_SENSOR_RST;
|
||||
ESP_ERROR_CHECK(gpio_set_direction(gpio_num, GPIO_MODE_OUTPUT));
|
||||
|
||||
gpio_set_level(gpio_num, 0);
|
||||
vTaskDelay(pdMS_TO_TICKS(15));
|
||||
gpio_set_level(gpio_num, 1);
|
||||
vTaskDelay(pdMS_TO_TICKS(80));
|
||||
|
||||
// Run bus scan
|
||||
i2c_drv_scan();
|
||||
|
||||
// Get known address
|
||||
if (i2c_drv_probe(CST816_SLAVE_ADDRESS)) {
|
||||
address = CST816_SLAVE_ADDRESS;
|
||||
ESP_LOGI(TAG, "Find touch address 0x%X", address);
|
||||
} else if (i2c_drv_probe(CST226SE_SLAVE_ADDRESS)) {
|
||||
address = CST226SE_SLAVE_ADDRESS;
|
||||
ESP_LOGI(TAG, "Find touch address 0x%X", address);
|
||||
} else if (i2c_drv_probe(CST328_SLAVE_ADDRESS)) {
|
||||
address = CST328_SLAVE_ADDRESS;
|
||||
ESP_LOGI(TAG, "Find touch address 0x%X", address);
|
||||
}
|
||||
|
||||
if (address == 0xFF) {
|
||||
ESP_LOGE(TAG, "Could't find touch chip!");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
// Set reset and interrupt pin
|
||||
touch.setPins(CONFIG_SENSOR_RST, CONFIG_SENSOR_IRQ);
|
||||
|
||||
//* Implemented using read and write callback methods, applicable to other platforms
|
||||
#if CONFIG_I2C_COMMUNICATION_METHOD_CALLBACK_RW
|
||||
|
||||
// * Provide the device address to the callback function
|
||||
i2c_drv_device_init(address);
|
||||
|
||||
ESP_LOGI(TAG, "Implemented using read and write callback methods");
|
||||
if (touch.begin(address, i2c_read_callback, i2c_write_callback)) {
|
||||
ESP_LOGI(TAG, "Initializing the capacitive touch screen successfully");
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Failed to initialize the capacitive touch screen");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
#endif
|
||||
|
||||
//* Use the built-in esp-idf communication method
|
||||
#if CONFIG_I2C_COMMUNICATION_METHOD_BUILTIN_RW
|
||||
#if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5,0,0)) && defined(CONFIG_SENSORLIB_ESP_IDF_NEW_API)
|
||||
|
||||
ESP_LOGI(TAG, "Implemented using built-in read and write methods (Use higher version >= 5.0 API)");
|
||||
|
||||
// * Using the new API of esp-idf 5.x, you need to pass the I2C BUS handle,
|
||||
// * which is useful when the bus shares multiple devices.
|
||||
extern i2c_master_bus_handle_t bus_handle;
|
||||
|
||||
if (touch.begin(bus_handle, address)) {
|
||||
ESP_LOGI(TAG, "Initializing the capacitive touch screen successfully");
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Failed to initialize the capacitive touch screen");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
#else
|
||||
|
||||
ESP_LOGI(TAG, "Implemented using built-in read and write methods (Use lower version < 5.0 API)");
|
||||
if (touch.begin((i2c_port_t)CONFIG_I2C_MASTER_PORT_NUM, address, CONFIG_SENSOR_SDA, CONFIG_SENSOR_SCL)) {
|
||||
ESP_LOGI(TAG, "Initializing the capacitive touch screen successfully");
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Failed to initialize the capacitive touch screen");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
#endif //ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5,0,0)
|
||||
#endif //CONFIG_I2C_COMMUNICATION_METHOD_BUILTIN_RW
|
||||
|
||||
const char *model = touch.getModelName();
|
||||
ESP_LOGI(TAG, "Using %s model", model);
|
||||
if (strncmp(model, "CST8", 4) == 0) {
|
||||
ESP_LOGI(TAG, "Some CST816 will automatically enter sleep,can use the touch.disableAutoSleep() to turn off");
|
||||
// Some CST816 will automatically enter sleep,
|
||||
// can use the touch.disableAutoSleep() to turn off
|
||||
touch.disableAutoSleep();
|
||||
|
||||
/**
|
||||
* Some touch screens have touch buttons,
|
||||
* and the touch coordinates need to be given
|
||||
* before the touch button callback function can be used.
|
||||
*/
|
||||
// Example touch coordinates using T-Display-S3 touch
|
||||
touch.setCenterButtonCoordinate(85, 360);
|
||||
|
||||
touch.setHomeButtonCallback(touch_home_button_callback);
|
||||
}
|
||||
|
||||
// Set the maximum valid coordinates
|
||||
// touch.setMaxCoordinates(320, 170);
|
||||
|
||||
// Swap XY coordinates
|
||||
// touch.setSwapXY(true);
|
||||
|
||||
// Mirror XY coordinates
|
||||
// touch.setMirrorXY(false, true);
|
||||
|
||||
|
||||
// Create a queue to handle gpio event from isr
|
||||
xQueue = xQueueCreate(5, sizeof(uint32_t));
|
||||
|
||||
// Register Sensor interrupt pins
|
||||
irq_init();
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
|
||||
void touch_drv_isr_handler()
|
||||
{
|
||||
int16_t x[5], y[5];
|
||||
uint8_t touched = touch.getPoint(x, y, touch.getSupportTouchPoint());
|
||||
if (touched) {
|
||||
for (int i = 0; i < touched; ++i) {
|
||||
printf("X[");
|
||||
printf("%d", i);
|
||||
printf("]:");
|
||||
printf("%d", x[i]);
|
||||
printf(" ");
|
||||
printf(" Y[");
|
||||
printf("%d", i);
|
||||
printf("]:");
|
||||
printf("%d", y[i]);
|
||||
printf(" ");
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
void touch_loop()
|
||||
{
|
||||
uint32_t io_num;
|
||||
// if (xQueueReceive(xQueue, &io_num, pdMS_TO_TICKS(10))) {
|
||||
// if (gpio_get_level((gpio_num_t)CONFIG_SENSOR_IRQ) == 0) {
|
||||
touch_drv_isr_handler();
|
||||
// }
|
||||
// }
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
CONFIG_IDF_TARGET="esp32s3"
|
||||
CONFIG_IDF_TARGET_ESP32S3=y
|
||||
CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG=y
|
||||
CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ_240=y
|
||||
CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ=240
|
||||
|
||||
Reference in New Issue
Block a user