mirror of
https://github.com/espressif/esp-idf
synced 2025-03-10 01:29:21 -04:00
feat(i2c_slave): Add new example for making i2c slave as a network sensor
This commit is contained in:
parent
e27594efc1
commit
170d44b75d
@ -67,6 +67,12 @@ examples/peripherals/i2c/i2c_eeprom:
|
|||||||
depends_components:
|
depends_components:
|
||||||
- esp_driver_i2c
|
- esp_driver_i2c
|
||||||
|
|
||||||
|
examples/peripherals/i2c/i2c_slave_network_sensor:
|
||||||
|
disable:
|
||||||
|
- if: SOC_I2C_SLAVE_CAN_GET_STRETCH_CAUSE != 1 or (SOC_WIFI_SUPPORTED != 1 and SOC_EMAC_SUPPORTED != 1)
|
||||||
|
depends_components:
|
||||||
|
- esp_driver_i2c
|
||||||
|
|
||||||
examples/peripherals/i2c/i2c_tools:
|
examples/peripherals/i2c/i2c_tools:
|
||||||
disable:
|
disable:
|
||||||
- if: SOC_I2C_SUPPORTED != 1
|
- if: SOC_I2C_SUPPORTED != 1
|
||||||
|
@ -0,0 +1,10 @@
|
|||||||
|
# The following 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)
|
||||||
|
|
||||||
|
|
||||||
|
# "Trim" the build. Include the minimal set of components, main, and anything it depends on.
|
||||||
|
idf_build_set_property(MINIMAL_BUILD ON)
|
||||||
|
project(i2c_slave_network_sensor)
|
129
examples/peripherals/i2c/i2c_slave_network_sensor/README.md
Normal file
129
examples/peripherals/i2c/i2c_slave_network_sensor/README.md
Normal file
@ -0,0 +1,129 @@
|
|||||||
|
| Supported Targets | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-C61 | ESP32-P4 | ESP32-S2 | ESP32-S3 |
|
||||||
|
| ----------------- | -------- | -------- | -------- | --------- | -------- | -------- | -------- |
|
||||||
|
|
||||||
|
|
||||||
|
# I2C slave example
|
||||||
|
|
||||||
|
This code demonstrates how to use the I2C slave driver to make a "network sensor". For illustration, the "sensor" will fetch some data from Github. You can attach this "sensor" to any I2C bus, then the master should be able to read the network data. This example uses the stretch mechanism to solve the problem of the i2c slave not knowing when it should send or receive data. In this example, we have a task to periodically update esp-idf's data on github. When the i2c master comes to visit, if the i2c slave fifo has data, it will return the data directly, if there is no data, it will set off the stretch interrupt, then there will be a callback to fill in the updated github data into the i2c fifo, release the stretch, and the master will return the data.
|
||||||
|
|
||||||
|
## Overview
|
||||||
|
|
||||||
|
This example demonstrates the possible usage of I2C slave driver
|
||||||
|
|
||||||
|
## How to use example
|
||||||
|
|
||||||
|
### Hardware Required
|
||||||
|
|
||||||
|
To run this example, you should have one ESP32-S, ESP32-C based development board. Also this example need wifi signal to access the internet.
|
||||||
|
|
||||||
|
#### Pin Assignment:
|
||||||
|
|
||||||
|
**Note:** The following pin assignments are used by default, you can change these in the `menuconfig` .
|
||||||
|
|
||||||
|
| | SDA | SCL |
|
||||||
|
| ------------------------------ | -------------- | -------------- |
|
||||||
|
| ESP I2C Master (this example) | I2C_MASTER_SDA | I2C_MASTER_SCL |
|
||||||
|
| ESP I2C Slave (this example) | I2C_SLAVE_SDA | I2C_SLAVE_SCL |
|
||||||
|
|
||||||
|
For the actual default value of `I2C_SLAVE_SDA` and `I2C_SLAVE_SCL` see `Example Configuration` in `menuconfig`.
|
||||||
|
|
||||||
|
**Note:** There's no need to add an external pull-up resistors for SDA/SCL pin, because the driver will enable the internal pull-up resistors.
|
||||||
|
|
||||||
|
|
||||||
|
### Command Assignment:
|
||||||
|
|
||||||
|
| Command | Content |
|
||||||
|
| --------- | ------------ |
|
||||||
|
| 0x10 | stars |
|
||||||
|
| 0x20 | forks |
|
||||||
|
| 0x30 | open_issues |
|
||||||
|
| 0x40 | description |
|
||||||
|
|
||||||
|
|
||||||
|
### Build and Flash
|
||||||
|
|
||||||
|
Enter `idf.py menuconfig` to config the internet information. In Example Connection Configuration option to config your wifi ssid and password.
|
||||||
|
|
||||||
|
Enter `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
|
||||||
|
|
||||||
|
```
|
||||||
|
I (11200) wifi:dp: 1, bi: 102400, li: 3, scale listen interval from 307200 us to 307200 us
|
||||||
|
I (11210) wifi:set rx beacon pti, rx_bcn_pti: 0, bcn_timeout: 25000, mt_pti: 0, mt_time: 10000
|
||||||
|
I (11250) wifi:<ba-add>idx:0 (ifx:0, f6:0e:57:fd:ee:8d), tid:0, ssn:1, winSize:64
|
||||||
|
I (11610) wifi:dp: 2, bi: 102400, li: 4, scale listen interval from 307200 us to 409600 us
|
||||||
|
I (11620) wifi:AP's beacon interval = 102400 us, DTIM period = 2
|
||||||
|
I (12460) example_connect: Got IPv6 event: Interface "example_netif_sta" address: fe80:0000:0000:0000:86f7:03ff:fe80:0284, type: ESP_IP6_ADDR_IS_LINK_LOCAL
|
||||||
|
I (12720) esp_netif_handlers: example_netif_sta ip: 192.168.70.133, mask: 255.255.255.0, gw: 192.168.70.132
|
||||||
|
I (12720) example_connect: Got IPv4 event: Interface "example_netif_sta" address: 192.168.70.133
|
||||||
|
I (12730) example_common: Connected to example_netif_sta
|
||||||
|
I (12730) example_common: - IPv4 address: 192.168.70.133,
|
||||||
|
I (12740) example_common: - IPv6 address: fe80:0000:0000:0000:86f7:03ff:fe80:0284, type: ESP_IP6_ADDR_IS_LINK_LOCAL
|
||||||
|
I (12750) gpio: GPIO[5]| InputEn: 1| OutputEn: 1| OpenDrain: 1| Pullup: 0| Pulldown: 0| Intr:0
|
||||||
|
I (12760) gpio: GPIO[4]| InputEn: 1| OutputEn: 1| OpenDrain: 1| Pullup: 0| Pulldown: 0| Intr:0
|
||||||
|
I (12770) main_task: Returned from app_main()
|
||||||
|
I (13470) wifi:<ba-add>idx:1 (ifx:0, f6:0e:57:fd:ee:8d), tid:3, ssn:0, winSize:64
|
||||||
|
Star count: 13401
|
||||||
|
Forks count: 7248
|
||||||
|
issue count: 1767
|
||||||
|
the description is: Espressif IoT Development Framework. Official development framework for Espressif SoCs.
|
||||||
|
I (16280) HTTP_CLIENT: HTTP GET Status = 200, content_length = 6165
|
||||||
|
```
|
||||||
|
|
||||||
|
Then you can read the information from an I2C master device.
|
||||||
|
|
||||||
|
We can get information via `i2c_tool` example with another esp chip plays in a i2c master role:
|
||||||
|
|
||||||
|
```
|
||||||
|
I (203) main_task: Calling app_main()
|
||||||
|
|
||||||
|
==============================================================
|
||||||
|
| Steps to Use i2c-tools |
|
||||||
|
| |
|
||||||
|
| 1. Try 'help', check all supported commands |
|
||||||
|
| 2. Try 'i2cconfig' to configure your I2C bus |
|
||||||
|
| 3. Try 'i2cdetect' to scan devices on the bus |
|
||||||
|
| 4. Try 'i2cget' to get the content of specific register |
|
||||||
|
| 5. Try 'i2cset' to set the value of specific register |
|
||||||
|
| 6. Try 'i2cdump' to dump all the register (Experiment) |
|
||||||
|
| |
|
||||||
|
==============================================================
|
||||||
|
|
||||||
|
|
||||||
|
Type 'help' to get the list of commands.
|
||||||
|
Use UP/DOWN arrows to navigate through command history.
|
||||||
|
Press TAB when typing command name to auto-complete.
|
||||||
|
I (303) main_task: Returned from app_main()
|
||||||
|
i2c-tools> i2cconfig --port=0 --freq=100000 --sda=5 --scl=4
|
||||||
|
i2c-tools> i2cdetect
|
||||||
|
0 1 2 3 4 5 6 7 8 9 a b c d e f
|
||||||
|
00: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
|
||||||
|
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
|
||||||
|
20: -- -- -- -- -- -- -- -- 28 -- -- -- -- -- -- --
|
||||||
|
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
|
||||||
|
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
|
||||||
|
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
|
||||||
|
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
|
||||||
|
70: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
|
||||||
|
i2c-tools> i2cget -c 0x28 -r 0x10 -l 4
|
||||||
|
0x4b 0x35 0x00 0x00
|
||||||
|
i2c-tools> i2cget -c 0x28 -r 0x20 -l 4
|
||||||
|
0x75 0x1c 0x00 0x00
|
||||||
|
i2c-tools> i2cget -c 0x28 -r 0x40 -l 100
|
||||||
|
0x45 0x73 0x70 0x72 0x65 0x73 0x73 0x69 0x66 0x20 0x49 0x6f 0x54 0x20 0x44 0x65
|
||||||
|
0x76 0x65 0x6c 0x6f 0x70 0x6d 0x65 0x6e 0x74 0x20 0x46 0x72 0x61 0x6d 0x65 0x77
|
||||||
|
0x6f 0x72 0x6b 0x2e 0x20 0x4f 0x66 0x66 0x69 0x63 0x69 0x61 0x6c 0x20 0x64 0x65
|
||||||
|
0x76 0x65 0x6c 0x6f 0x70 0x6d 0x65 0x6e 0x74 0x20 0x66 0x72 0x61 0x6d 0x65 0x77
|
||||||
|
0x6f 0x72 0x6b 0x20 0x66 0x6f 0x72 0x20 0x45 0x73 0x70 0x72 0x65 0x73 0x73 0x69
|
||||||
|
0x66 0x20 0x53 0x6f 0x43 0x73 0x2e 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
|
||||||
|
0x00 0x00 0x00 0x00
|
||||||
|
```
|
||||||
|
|
||||||
|
## Troubleshooting
|
||||||
|
|
||||||
|
(For any technical queries, please open an [issue](https://github.com/espressif/esp-idf/issues) on GitHub. We will get back to you as soon as possible.)
|
@ -0,0 +1,5 @@
|
|||||||
|
set(srcs "i2c_slave_main.c")
|
||||||
|
|
||||||
|
idf_component_register(SRCS ${srcs}
|
||||||
|
PRIV_REQUIRES esp_http_client esp_wifi nvs_flash json esp_driver_i2c
|
||||||
|
INCLUDE_DIRS ".")
|
@ -0,0 +1,24 @@
|
|||||||
|
menu "Example Configuration"
|
||||||
|
|
||||||
|
menu "I2C Slave"
|
||||||
|
config I2C_SLAVE_SCL
|
||||||
|
int "SCL GPIO Num"
|
||||||
|
default 4
|
||||||
|
help
|
||||||
|
GPIO number for I2C Slave clock line.
|
||||||
|
|
||||||
|
config I2C_SLAVE_SDA
|
||||||
|
int "SDA GPIO Num"
|
||||||
|
default 5
|
||||||
|
help
|
||||||
|
GPIO number for I2C Slave data line.
|
||||||
|
|
||||||
|
config I2C_SLAVE_ADDRESS
|
||||||
|
hex "I2C SLAVE ADDRESS"
|
||||||
|
default 0x28
|
||||||
|
help
|
||||||
|
Address of I2C slave
|
||||||
|
|
||||||
|
endmenu
|
||||||
|
|
||||||
|
endmenu
|
@ -0,0 +1,252 @@
|
|||||||
|
/*
|
||||||
|
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: Unlicense OR CC0-1.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include "esp_log.h"
|
||||||
|
#include "sdkconfig.h"
|
||||||
|
#include "freertos/FreeRTOS.h"
|
||||||
|
#include "freertos/task.h"
|
||||||
|
#include "esp_event.h"
|
||||||
|
#include "esp_log.h"
|
||||||
|
#include "nvs_flash.h"
|
||||||
|
#include "esp_netif.h"
|
||||||
|
#include "protocol_examples_common.h"
|
||||||
|
#include "esp_http_client.h"
|
||||||
|
#include "cJSON.h"
|
||||||
|
#include "driver/i2c_slave.h"
|
||||||
|
|
||||||
|
static const char *TAG = "example";
|
||||||
|
|
||||||
|
#define I2C_SLAVE_SCL_IO CONFIG_I2C_SLAVE_SCL /*!< gpio number for i2c slave clock */
|
||||||
|
#define I2C_SLAVE_SDA_IO CONFIG_I2C_SLAVE_SDA /*!< gpio number for i2c slave data */
|
||||||
|
#define I2C_SLAVE_NUM 0
|
||||||
|
#define ESP_SLAVE_ADDR CONFIG_I2C_SLAVE_ADDRESS /*!< ESP slave address, you can set any 7bit value */
|
||||||
|
|
||||||
|
// Command Lists
|
||||||
|
#define STARS_COMMAND (0x10)
|
||||||
|
#define FORKS_COMMAND (0x20)
|
||||||
|
#define OPENISSUES_COMMAND (0x30)
|
||||||
|
#define DESCRIPTIONS_COMMAND (0x40)
|
||||||
|
|
||||||
|
#define GITHUB_API_URL "https://api.github.com/repos/espressif/esp-idf"
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
char *json_buffer;
|
||||||
|
int json_size;
|
||||||
|
uint8_t tmp_buffer_stars[sizeof(int)];
|
||||||
|
uint8_t tmp_buffer_forks[sizeof(int)];
|
||||||
|
uint8_t tmp_buffer_open_issues[sizeof(int)];
|
||||||
|
uint8_t tmp_buffer_descriptions[100];
|
||||||
|
QueueHandle_t event_queue;
|
||||||
|
uint8_t command_data;
|
||||||
|
i2c_slave_dev_handle_t handle;
|
||||||
|
} i2c_slave_github_context_t;
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
I2C_SLAVE_EVT_RX,
|
||||||
|
I2C_SLAVE_EVT_TX
|
||||||
|
} i2c_slave_event_t;
|
||||||
|
|
||||||
|
static esp_err_t _http_event_handler(esp_http_client_event_t *evt)
|
||||||
|
{
|
||||||
|
i2c_slave_github_context_t *context = (i2c_slave_github_context_t *)evt->user_data;
|
||||||
|
int star_count = 0, forks_count = 0, open_issues_count = 0;
|
||||||
|
|
||||||
|
switch (evt->event_id) {
|
||||||
|
case HTTP_EVENT_ON_DATA:
|
||||||
|
if (evt->data_len > 0) {
|
||||||
|
if (context->json_buffer == NULL) {
|
||||||
|
context->json_buffer = malloc(evt->data_len + 1);
|
||||||
|
} else {
|
||||||
|
context->json_buffer = realloc(context->json_buffer, context->json_size + evt->data_len + 1);
|
||||||
|
}
|
||||||
|
if (context->json_buffer == NULL) {
|
||||||
|
ESP_LOGE("HTTP_CLIENT", "Failed to allocate memory for data json_buffer");
|
||||||
|
return ESP_FAIL;
|
||||||
|
}
|
||||||
|
memcpy(context->json_buffer + context->json_size, evt->data, evt->data_len);
|
||||||
|
context->json_size += evt->data_len;
|
||||||
|
context->json_buffer[context->json_size] = '\0'; // Null-terminate the string
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case HTTP_EVENT_ON_FINISH:
|
||||||
|
if (context->json_buffer != NULL) {
|
||||||
|
// Process received data
|
||||||
|
cJSON *root = cJSON_Parse(context->json_buffer);
|
||||||
|
cJSON *stars = cJSON_GetObjectItem(root, "stargazers_count");
|
||||||
|
|
||||||
|
if (stars != NULL) {
|
||||||
|
star_count = stars->valueint;
|
||||||
|
printf("Star count: %d\n", star_count);
|
||||||
|
memcpy(context->tmp_buffer_stars, &star_count, sizeof(int));
|
||||||
|
}
|
||||||
|
cJSON *forks = cJSON_GetObjectItem(root, "forks_count");
|
||||||
|
if (forks != NULL) {
|
||||||
|
forks_count = forks->valueint;
|
||||||
|
printf("Forks count: %d\n", forks_count);
|
||||||
|
memcpy(context->tmp_buffer_forks, &forks_count, sizeof(int));
|
||||||
|
}
|
||||||
|
cJSON *open_issues = cJSON_GetObjectItem(root, "open_issues_count");
|
||||||
|
if (open_issues != NULL) {
|
||||||
|
open_issues_count = open_issues->valueint;
|
||||||
|
printf("issue count: %d\n", open_issues_count);
|
||||||
|
memcpy(context->tmp_buffer_open_issues, &open_issues_count, sizeof(int));
|
||||||
|
}
|
||||||
|
cJSON *descriptions = cJSON_GetObjectItem(root, "description");
|
||||||
|
if (descriptions != NULL) {
|
||||||
|
printf("the description is: %s\n", descriptions->valuestring);
|
||||||
|
memcpy(context->tmp_buffer_descriptions, descriptions->valuestring, strlen(descriptions->valuestring));
|
||||||
|
}
|
||||||
|
cJSON_Delete(root);
|
||||||
|
free(context->json_buffer);
|
||||||
|
context->json_buffer = NULL;
|
||||||
|
context->json_size = 0;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ESP_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
void http_get_task(void *pvParameters)
|
||||||
|
{
|
||||||
|
i2c_slave_github_context_t *context = (i2c_slave_github_context_t *)pvParameters;
|
||||||
|
|
||||||
|
esp_http_client_config_t config = {
|
||||||
|
.url = GITHUB_API_URL,
|
||||||
|
.event_handler = _http_event_handler,
|
||||||
|
.method = HTTP_METHOD_GET,
|
||||||
|
.buffer_size = 2048,
|
||||||
|
.user_data = context,
|
||||||
|
};
|
||||||
|
|
||||||
|
while (1) {
|
||||||
|
esp_http_client_handle_t client = esp_http_client_init(&config);
|
||||||
|
esp_err_t err = esp_http_client_perform(client);
|
||||||
|
if (err == ESP_OK) {
|
||||||
|
ESP_LOGI("HTTP_CLIENT", "HTTP GET Status = %d, content_length = %lld",
|
||||||
|
esp_http_client_get_status_code(client),
|
||||||
|
esp_http_client_get_content_length(client));
|
||||||
|
} else {
|
||||||
|
ESP_LOGE("HTTP_CLIENT", "HTTP GET request failed: %s", esp_err_to_name(err));
|
||||||
|
}
|
||||||
|
esp_http_client_cleanup(client);
|
||||||
|
vTaskDelay(30 * 60 * 1000 / portTICK_PERIOD_MS);
|
||||||
|
}
|
||||||
|
vTaskDelete(NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool i2c_slave_request_cb(i2c_slave_dev_handle_t i2c_slave, const i2c_slave_request_event_data_t *evt_data, void *arg)
|
||||||
|
{
|
||||||
|
i2c_slave_github_context_t *context = (i2c_slave_github_context_t *)arg;
|
||||||
|
i2c_slave_event_t evt = I2C_SLAVE_EVT_TX;
|
||||||
|
BaseType_t xTaskWoken = 0;
|
||||||
|
xQueueSendFromISR(context->event_queue, &evt, &xTaskWoken);
|
||||||
|
return xTaskWoken;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool i2c_slave_receive_cb(i2c_slave_dev_handle_t i2c_slave, const i2c_slave_rx_done_event_data_t *evt_data, void *arg)
|
||||||
|
{
|
||||||
|
i2c_slave_github_context_t *context = (i2c_slave_github_context_t *)arg;
|
||||||
|
i2c_slave_event_t evt = I2C_SLAVE_EVT_RX;
|
||||||
|
BaseType_t xTaskWoken = 0;
|
||||||
|
// Command only contains one byte, so just save one bytes here.
|
||||||
|
context->command_data = *evt_data->buffer;
|
||||||
|
xQueueSendFromISR(context->event_queue, &evt, &xTaskWoken);
|
||||||
|
return xTaskWoken;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void i2c_slave_task(void *arg)
|
||||||
|
{
|
||||||
|
i2c_slave_github_context_t *context = (i2c_slave_github_context_t *)arg;
|
||||||
|
i2c_slave_dev_handle_t handle = (i2c_slave_dev_handle_t)context->handle;
|
||||||
|
|
||||||
|
uint8_t zero_buffer[32] = {}; // Use this buffer to clear the fifo.
|
||||||
|
uint32_t write_len, total_written;
|
||||||
|
uint32_t buffer_size = 0;
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
i2c_slave_event_t evt;
|
||||||
|
if (xQueueReceive(context->event_queue, &evt, 10) == pdTRUE) {
|
||||||
|
if (evt == I2C_SLAVE_EVT_TX) {
|
||||||
|
uint8_t *data_buffer;
|
||||||
|
switch (context->command_data) {
|
||||||
|
case STARS_COMMAND:
|
||||||
|
data_buffer = context->tmp_buffer_stars;
|
||||||
|
buffer_size = sizeof(context->tmp_buffer_stars);
|
||||||
|
break;
|
||||||
|
case FORKS_COMMAND:
|
||||||
|
data_buffer = context->tmp_buffer_forks;
|
||||||
|
buffer_size = sizeof(context->tmp_buffer_forks);
|
||||||
|
break;
|
||||||
|
case OPENISSUES_COMMAND:
|
||||||
|
data_buffer = context->tmp_buffer_open_issues;
|
||||||
|
buffer_size = sizeof(context->tmp_buffer_open_issues);
|
||||||
|
break;
|
||||||
|
case DESCRIPTIONS_COMMAND:
|
||||||
|
data_buffer = context->tmp_buffer_descriptions;
|
||||||
|
buffer_size = sizeof(context->tmp_buffer_descriptions);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
ESP_LOGE(TAG, "Invalid command");
|
||||||
|
data_buffer = zero_buffer;
|
||||||
|
buffer_size = sizeof(zero_buffer);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
total_written = 0;
|
||||||
|
while (total_written < buffer_size) {
|
||||||
|
ESP_ERROR_CHECK(i2c_slave_write(handle, data_buffer + total_written, buffer_size - total_written, &write_len, 1000));
|
||||||
|
if (write_len == 0) {
|
||||||
|
ESP_LOGE(TAG, "Write error or timeout");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
total_written += write_len;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
vTaskDelete(NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
void app_main(void)
|
||||||
|
{
|
||||||
|
static i2c_slave_github_context_t context = {0};
|
||||||
|
|
||||||
|
ESP_ERROR_CHECK(nvs_flash_init());
|
||||||
|
ESP_ERROR_CHECK(esp_netif_init());
|
||||||
|
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
||||||
|
ESP_ERROR_CHECK(example_connect());
|
||||||
|
|
||||||
|
context.event_queue = xQueueCreate(16, sizeof(i2c_slave_event_t));
|
||||||
|
if (!context.event_queue) {
|
||||||
|
ESP_LOGE(TAG, "Creating queue failed");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
i2c_slave_config_t i2c_slv_config = {
|
||||||
|
.i2c_port = I2C_SLAVE_NUM,
|
||||||
|
.clk_source = I2C_CLK_SRC_DEFAULT,
|
||||||
|
.scl_io_num = I2C_SLAVE_SCL_IO,
|
||||||
|
.sda_io_num = I2C_SLAVE_SDA_IO,
|
||||||
|
.slave_addr = ESP_SLAVE_ADDR,
|
||||||
|
.send_buf_depth = 100,
|
||||||
|
.receive_buf_depth = 100,
|
||||||
|
};
|
||||||
|
|
||||||
|
ESP_ERROR_CHECK(i2c_new_slave_device(&i2c_slv_config, &context.handle));
|
||||||
|
i2c_slave_event_callbacks_t cbs = {
|
||||||
|
.on_receive = i2c_slave_receive_cb,
|
||||||
|
.on_request = i2c_slave_request_cb,
|
||||||
|
};
|
||||||
|
ESP_ERROR_CHECK(i2c_slave_register_event_callbacks(context.handle, &cbs, &context));
|
||||||
|
|
||||||
|
xTaskCreate(http_get_task, "http_get_task", 4096, &context, 20, NULL);
|
||||||
|
xTaskCreate(i2c_slave_task, "i2c_slave_task", 1024 * 4, &context, 10, NULL);
|
||||||
|
}
|
@ -0,0 +1,3 @@
|
|||||||
|
dependencies:
|
||||||
|
protocol_examples_common:
|
||||||
|
path: ${IDF_PATH}/examples/common_components/protocol_examples_common
|
@ -0,0 +1,5 @@
|
|||||||
|
# Name, Type, SubType, Offset, Size, Flags
|
||||||
|
# Note: if you have increased the bootloader size, make sure to update the offsets to avoid overlap
|
||||||
|
nvs, data,nvs,0x9000,24K,
|
||||||
|
phy_init, data,phy,0xf000,4K,
|
||||||
|
factory, app,factory,0x10000,2M,
|
|
@ -0,0 +1,5 @@
|
|||||||
|
CONFIG_PARTITION_TABLE_CUSTOM=y
|
||||||
|
CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y
|
||||||
|
CONFIG_I2C_ENABLE_SLAVE_DRIVER_VERSION_2=y
|
||||||
|
CONFIG_ESP_TLS_INSECURE=y
|
||||||
|
CONFIG_ESP_TLS_SKIP_SERVER_CERT_VERIFY=y
|
Loading…
x
Reference in New Issue
Block a user