mirror of
https://github.com/espressif/esp-idf.git
synced 2024-10-05 20:47:46 -04:00
Merge branch 'example/i2c_wifi' into 'master'
i2c_app_test: Add i2c work together with wifi test See merge request espressif/esp-idf!14736
This commit is contained in:
commit
9aa65db79d
6
tools/test_apps/peripherals/i2c_wifi/CMakeLists.txt
Normal file
6
tools/test_apps/peripherals/i2c_wifi/CMakeLists.txt
Normal file
@ -0,0 +1,6 @@
|
||||
# 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.5)
|
||||
|
||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||
project(i2c_wifi_main)
|
158
tools/test_apps/peripherals/i2c_wifi/README.md
Normal file
158
tools/test_apps/peripherals/i2c_wifi/README.md
Normal file
@ -0,0 +1,158 @@
|
||||
# I2C-WIFI Test
|
||||
|
||||
## Overview
|
||||
|
||||
This test demonstrates basic usage of I2C driver with wifi softAP.
|
||||
|
||||
### I2C MASTER
|
||||
|
||||
1. Switch on wifi softAP, and connect to your personal device, like your mobile phone.
|
||||
2. Send data to slave board.
|
||||
|
||||
### I2C SLAVE
|
||||
|
||||
1. Receive data from master board.
|
||||
|
||||
## How to use test
|
||||
|
||||
### Hardware Required
|
||||
|
||||
To run this test, you should have at least one ESP development board for master. But to make this test convenient, we also use a ESP development board for slave. (You can change your slave device, but don't forget to change code as well if necessary.)
|
||||
|
||||
#### Pin Assignment
|
||||
|
||||
**Note:** The following pin assignments are used by default, you can change these in the `menuconfig` .
|
||||
|
||||
| | SDA | SCL |
|
||||
| ------------------ | ------ | ------ |
|
||||
| ESP32/ESP32-S2 I2C | GPIO18 | GPIO19 |
|
||||
| ESP32-S3 I2C | GPIO1 | GPIO2 |
|
||||
| ESP32-C3 I2C | GPIO5 | GPIO9 |
|
||||
|
||||
**Note:** It is recommended to add external pull-up resistors for SDA/SCL pins to make the communication more stable, though the driver will enable internal pull-up resistors.
|
||||
|
||||
### Configure the project
|
||||
|
||||
Open the project configuration menu (`idf.py menuconfig`). Then go into `Test Configuration` menu.
|
||||
|
||||
#### I2C MASTER configuration
|
||||
|
||||
- In the `I2C working mode select` menu, you can set the working mode of i2c, choose `i2c master mode`.
|
||||
- In the `I2C Configuration` menu, you can set the pin number of SDA/SCL. Also you can modify the I2C port number and frequency of the master. And you can set the slave address here as well.
|
||||
- Enable `WIFI AP` if you need it.
|
||||
- In the `WIFI softAP Configuration` menu, you can set the wifi information here.
|
||||
|
||||
#### I2C SLAVE configuration
|
||||
|
||||
- In the `I2C working mode select` menu, you can set the working mode of i2c, choose `i2c slave mode`.
|
||||
- In the `I2C Configuration` menu, you can set the pin number of SDA/SCL. Also you can modify the I2C port number and address of the slave.
|
||||
|
||||
### Build and Flash
|
||||
|
||||
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.
|
||||
|
||||
## Test Output
|
||||
|
||||
### MASTER
|
||||
|
||||
```bash
|
||||
I (339) cpu_start: Starting scheduler.
|
||||
I (359) I2C-wifi test: ESP_WIFI_MODE_AP
|
||||
I (359) pp: pp rom version: 9387209
|
||||
I (359) net80211: net80211 rom version: 9387209
|
||||
I (369) wifi:wifi driver task: 3fc9def0, prio:23, stack:6656, core=0
|
||||
I (369) system_api: Base MAC address is not set
|
||||
I (379) system_api: read default base MAC address from EFUSE
|
||||
I (379) wifi:wifi firmware version: d062fdb
|
||||
I (389) wifi:wifi certification version: v7.0
|
||||
I (389) wifi:config NVS flash: enabled
|
||||
I (389) wifi:config nano formating: disabled
|
||||
I (399) wifi:Init data frame dynamic rx buffer num: 32
|
||||
I (399) wifi:Init management frame dynamic rx buffer num: 32
|
||||
I (409) wifi:Init management short buffer num: 32
|
||||
I (409) wifi:Init dynamic tx buffer num: 32
|
||||
I (419) wifi:Init static tx FG buffer num: 2
|
||||
I (419) wifi:Init static rx buffer size: 1600
|
||||
I (419) wifi:Init static rx buffer num: 10
|
||||
I (429) wifi:Init dynamic rx buffer num: 32
|
||||
I (429) wifi_init: rx ba win: 6
|
||||
I (439) wifi_init: tcpip mbox: 32
|
||||
I (439) wifi_init: udp mbox: 6
|
||||
I (439) wifi_init: tcp mbox: 6
|
||||
I (449) wifi_init: tcp tx win: 5744
|
||||
I (449) wifi_init: tcp rx win: 5744
|
||||
I (459) wifi_init: tcp mss: 1440
|
||||
I (459) wifi_init: WiFi IRAM OP enabled
|
||||
I (459) wifi_init: WiFi RX IRAM OP enabled
|
||||
I (469) phy_init: phy_version 500,985899c,Apr 19 2021,16:05:08
|
||||
I (589) wifi:set rx active PTI: 0, rx ack PTI: 0, and default PTI: 0
|
||||
I (599) wifi:mode : softAP (7c:df:a1:86:d8:a9)
|
||||
I (599) wifi:Total power save buffer number: 16
|
||||
I (599) wifi:Init max length of beacon: 752/752
|
||||
I (599) wifi:Init max length of beacon: 752/752
|
||||
I (609) I2C-wifi test: wifi_init_softap finished. SSID:myssid password:mypassword channel:1
|
||||
```
|
||||
|
||||
### SLAVE
|
||||
|
||||
```bash
|
||||
I (277) cpu_start: Starting scheduler.
|
||||
the clk config is 0, speed is 0
|
||||
slave mode.read thread start...
|
||||
I (23609) I2C-wifi test: 6a 1b 05 1f 1f 08 01 20 19 03 27
|
||||
I (23619) I2C-wifi test: 6a 1b 05 1f 1f 08 01 20 19 03 27
|
||||
I (23629) I2C-wifi test: 6a 1b 05 1f 1f 08 01 20 19 03 27
|
||||
I (23639) I2C-wifi test: 6a 1b 05 1f 1f 08 01 20 19 03 27
|
||||
I (23649) I2C-wifi test: 6a 1b 05 1f 1f 08 01 20 19 03 27
|
||||
I (23659) I2C-wifi test: 6a 1b 05 1f 1f 08 01 20 19 03 27
|
||||
I (23669) I2C-wifi test: 6a 1b 05 1f 1f 08 01 20 19 03 27
|
||||
I (23679) I2C-wifi test: 6a 1b 05 1f 1f 08 01 20 19 03 27
|
||||
I (23689) I2C-wifi test: 6a 1b 05 1f 1f 08 01 20 19 03 27
|
||||
I (23699) I2C-wifi test: 6a 1b 05 1f 1f 08 01 20 19 03 27
|
||||
I (23709) I2C-wifi test: 6a 1b 05 1f 1f 08 01 20 19 03 27
|
||||
I (23719) I2C-wifi test: 6a 1b 05 1f 1f 08 01 20 19 03 27
|
||||
I (23729) I2C-wifi test: 6a 1b 05 1f 1f 08 01 20 19 03 27
|
||||
I (23739) I2C-wifi test: 6a 1b 05 1f 1f 08 01 20 19 03 27
|
||||
I (23749) I2C-wifi test: 6a 1b 05 1f 1f 08 01 20 19 03 27
|
||||
I (23759) I2C-wifi test: 6a 1b 05 1f 1f 08 01 20 19 03 27
|
||||
I (23769) I2C-wifi test: 6a 1b 05 1f 1f 08 01 20 19 03 27
|
||||
I (23779) I2C-wifi test: 6a 1b 05 1f 1f 08 01 20 19 03 27
|
||||
I (23789) I2C-wifi test: 6a 1b 05 1f 1f 08 01 20 19 03 27
|
||||
I (23799) I2C-wifi test: 6a 1b 05 1f 1f 08 01 20 19 03 27
|
||||
I (23809) I2C-wifi test: 6a 1b 05 1f 1f 08 01 20 19 03 27
|
||||
I (23819) I2C-wifi test: 6a 1b 05 1f 1f 08 01 20 19 03 27
|
||||
I (23829) I2C-wifi test: 6a 1b 05 1f 1f 08 01 20 19 03 27
|
||||
I (23839) I2C-wifi test: 6a 1b 05 1f 1f 08 01 20 19 03 27
|
||||
I (23849) I2C-wifi test: 6a 1b 05 1f 1f 08 01 20 19 03 27
|
||||
I (23859) I2C-wifi test: 6a 1b 05 1f 1f 08 01 20 19 03 27
|
||||
I (23869) I2C-wifi test: 6a 1b 05 1f 1f 08 01 20 19 03 27
|
||||
I (23879) I2C-wifi test: 6a 1b 05 1f 1f 08 01 20 19 03 27
|
||||
I (23889) I2C-wifi test: 6a 1b 05 1f 1f 08 01 20 19 03 27
|
||||
I (23899) I2C-wifi test: 6a 1b 05 1f 1f 08 01 20 19 03 27
|
||||
I (23909) I2C-wifi test: 6a 1b 05 1f 1f 08 01 20 19 03 27
|
||||
I (23919) I2C-wifi test: 6a 1b 05 1f 1f 08 01 20 19 03 27
|
||||
I (23929) I2C-wifi test: 6a 1b 05 1f 1f 08 01 20 19 03 27
|
||||
I (23939) I2C-wifi test: 6a 1b 05 1f 1f 08 01 20 19 03 27
|
||||
I (23949) I2C-wifi test: 6a 1b 05 1f 1f 08 01 20 19 03 27
|
||||
I (23959) I2C-wifi test: 6a 1b 05 1f 1f 08 01 20 19 03 27
|
||||
I (23969) I2C-wifi test: 6a 1b 05 1f 1f 08 01 20 19 03 27
|
||||
I (23979) I2C-wifi test: 6a 1b 05 1f 1f 08 01 20 19 03 27
|
||||
I (23989) I2C-wifi test: 6a 1b 05 1f 1f 08 01 20 19 03 27
|
||||
I (23999) I2C-wifi test: 6a 1b 05 1f 1f 08 01 20 19 03 27
|
||||
I (24009) I2C-wifi test: 6a 1b 05 1f 1f 08 01 20 19 03 27
|
||||
I (24019) I2C-wifi test: 6a 1b 05 1f 1f 08 01 20 19 03 27
|
||||
I (24029) I2C-wifi test: 6a 1b 05 1f 1f 08 01 20 19 03 27
|
||||
I (24039) I2C-wifi test: 6a 1b 05 1f 1f 08 01 20 19 03 27
|
||||
I (24049) I2C-wifi test: 6a 1b 05 1f 1f 08 01 20 19 03 27
|
||||
I (24059) I2C-wifi test: 6a 1b 05 1f 1f 08 01 20 19 03 27
|
||||
I (24069) I2C-wifi test: 6a 1b 05 1f 1f 08 01 20 19 03 27
|
||||
I (24079) I2C-wifi test: 6a 1b 05 1f 1f 08 01 20 19 03 27
|
||||
I (24089) I2C-wifi test: 6a 1b 05 1f 1f 08 01 20 19 03 27
|
||||
I (24099) I2C-wifi test: 6a 1b 05 1f 1f 08 01 20 19 03 27
|
||||
I (24109) I2C-wifi test: 6a 1b 05 1f 1f 08 01 20 19 03 27
|
||||
I (24119) I2C-wifi test: 6a 1b 05 1f 1f 08 01 20 19 03 27
|
||||
```
|
18
tools/test_apps/peripherals/i2c_wifi/app_test.py
Normal file
18
tools/test_apps/peripherals/i2c_wifi/app_test.py
Normal file
@ -0,0 +1,18 @@
|
||||
#!/usr/bin/env python
|
||||
import tiny_test_fw
|
||||
import ttfw_idf
|
||||
|
||||
|
||||
@ttfw_idf.idf_custom_test(env_tag='Example_GENERIC')
|
||||
def test_startup(env, _): # type: (tiny_test_fw.Env.Env, None) -> None
|
||||
# Only test for master usage.
|
||||
dut = env.get_dut('i2c_wifi', 'tools/test_apps/peripherals/i2c_wifi')
|
||||
dut.start_app()
|
||||
|
||||
dut.expect('I2C-WIFI test success')
|
||||
|
||||
env.close_dut(dut.name)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
test_startup()
|
2
tools/test_apps/peripherals/i2c_wifi/main/CMakeLists.txt
Normal file
2
tools/test_apps/peripherals/i2c_wifi/main/CMakeLists.txt
Normal file
@ -0,0 +1,2 @@
|
||||
idf_component_register(SRCS "i2c_wifi_main.c"
|
||||
INCLUDE_DIRS "")
|
83
tools/test_apps/peripherals/i2c_wifi/main/Kconfig.projbuild
Normal file
83
tools/test_apps/peripherals/i2c_wifi/main/Kconfig.projbuild
Normal file
@ -0,0 +1,83 @@
|
||||
menu "Test Configuration"
|
||||
|
||||
choice TEST_I2C_WORK_MODE
|
||||
prompt "I2C working mode select"
|
||||
default TEST_I2C_MASTER_MODE
|
||||
|
||||
config TEST_I2C_MASTER_MODE
|
||||
bool "i2c master mode"
|
||||
config TEST_I2C_SLAVE_MODE
|
||||
bool "I2C slave mode"
|
||||
endchoice
|
||||
|
||||
menu "I2C Configuration"
|
||||
config TEST_I2C_SCL_NUM
|
||||
int "SCL GPIO Num"
|
||||
default 9 if IDF_TARGET_ESP32C3
|
||||
default 2 if IDF_TARGET_ESP32S3
|
||||
default 19 if IDF_TARGET_ESP32 || IDF_TARGET_ESP32S2
|
||||
help
|
||||
GPIO number for I2C Master clock line.
|
||||
|
||||
config TEST_I2C_SDA_NUM
|
||||
int "SDA GPIO Num"
|
||||
default 5 if IDF_TARGET_ESP32C3
|
||||
default 1 if IDF_TARGET_ESP32S3
|
||||
default 18 if IDF_TARGET_ESP32 || IDF_TARGET_ESP32S2
|
||||
help
|
||||
GPIO number for I2C Master data line.
|
||||
|
||||
config TEST_I2C_PORT_NUM
|
||||
int "Port Number"
|
||||
default 0
|
||||
help
|
||||
Port number for I2C Master device.
|
||||
|
||||
config TEST_I2C_MASTER_FREQUENCY
|
||||
int "Master Frequency"
|
||||
default 400000
|
||||
depends on TEST_I2C_MASTER_MODE
|
||||
help
|
||||
I2C Speed of Master device.
|
||||
|
||||
config TEST_I2C_SLAVE_ADDR
|
||||
hex "ESP Slave Address"
|
||||
default 0x58
|
||||
help
|
||||
Hardware Address of I2C Slave Port.
|
||||
endmenu
|
||||
|
||||
config TEST_I2C_WIFI_AP_ENABLE
|
||||
bool "Enable WIFI AP"
|
||||
default y
|
||||
depends on TEST_I2C_MASTER_MODE
|
||||
|
||||
menu "WiFi softAP Configuration"
|
||||
depends on TEST_I2C_WIFI_AP_ENABLE
|
||||
|
||||
config TEST_WIFI_SSID
|
||||
string "WiFi SSID"
|
||||
default "myssid"
|
||||
help
|
||||
SSID (network name) for the test to connect to.
|
||||
|
||||
config TEST_WIFI_PASSWORD
|
||||
string "WiFi Password"
|
||||
default "mypassword"
|
||||
help
|
||||
WiFi password (WPA or WPA2) for the test to use.
|
||||
config TEST_WIFI_CHANNEL
|
||||
int "WiFi Channel"
|
||||
range 1 13
|
||||
default 1
|
||||
help
|
||||
WiFi channel (network channel) for the test to use.
|
||||
|
||||
config TEST_MAX_STA_CONN
|
||||
int "Maximal STA connections"
|
||||
default 4
|
||||
help
|
||||
Max number of the STA connects to AP.
|
||||
endmenu
|
||||
|
||||
endmenu
|
247
tools/test_apps/peripherals/i2c_wifi/main/i2c_wifi_main.c
Normal file
247
tools/test_apps/peripherals/i2c_wifi/main/i2c_wifi_main.c
Normal file
@ -0,0 +1,247 @@
|
||||
/* I2C-WIFI - Test*/
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "esp_system.h"
|
||||
#include "esp_wifi.h"
|
||||
#include "esp_event.h"
|
||||
#include "esp_log.h"
|
||||
#include "nvs_flash.h"
|
||||
|
||||
#include "esp_timer.h"
|
||||
#include "driver/i2c.h"
|
||||
|
||||
#define WAIT_TIMER_PERIOD (1 * 1000)
|
||||
|
||||
#define I2C_SDA_GPIO CONFIG_TEST_I2C_SDA_NUM
|
||||
#define I2C_SCL_GPIO CONFIG_TEST_I2C_SCL_NUM
|
||||
#define I2C_SLAVE_ADDR CONFIG_TEST_I2C_SLAVE_ADDR
|
||||
|
||||
#if CONFIG_TEST_I2C_MASTER_MODE
|
||||
#define I2C_TEST_WIFI_AP_ENABLE CONFIG_TEST_I2C_WIFI_AP_ENABLE
|
||||
#define I2C_CLK_FREQUENCY CONFIG_TEST_I2C_MASTER_FREQUENCY
|
||||
#define I2C_MASTER_NUM_PORT CONFIG_TEST_I2C_PORT_NUM
|
||||
#define I2C_MASTER_TX_BUF_DISABLE 0
|
||||
#define I2C_MASTER_RX_BUF_DISABLE 0
|
||||
#define I2C_ACK_CHECK_EN 0x1
|
||||
#define I2C_ACK_CHECK_DIS 0x0
|
||||
#else
|
||||
#define I2C_SLAVE_NUM_PORT CONFIG_TEST_I2C_PORT_NUM
|
||||
#define I2C_SLAVE_TX_BUF_LEN 256
|
||||
#define I2C_SLAVE_RX_BUF_LEN 256
|
||||
#endif
|
||||
|
||||
/* The tests use WiFi configuration that you can set via project configuration menu.
|
||||
|
||||
If you'd rather not, just change the below entries to strings with
|
||||
the config you want - ie #define TEST_WIFI_SSID "mywifissid"
|
||||
*/
|
||||
static const char* TAG = "I2C-wifi test";
|
||||
|
||||
#if I2C_TEST_WIFI_AP_ENABLE
|
||||
#define TEST_ESP_WIFI_SSID CONFIG_TEST_WIFI_SSID
|
||||
#define TEST_ESP_WIFI_PASS CONFIG_TEST_WIFI_PASSWORD
|
||||
#define TEST_ESP_WIFI_CHANNEL CONFIG_TEST_WIFI_CHANNEL
|
||||
#define TEST_MAX_STA_CONN CONFIG_TEST_MAX_STA_CONN
|
||||
|
||||
|
||||
static void wifi_event_handler(void *arg, esp_event_base_t event_base,
|
||||
int32_t event_id, void *event_data)
|
||||
{
|
||||
if (event_id == WIFI_EVENT_AP_STACONNECTED) {
|
||||
wifi_event_ap_staconnected_t *event = (wifi_event_ap_staconnected_t *) event_data;
|
||||
ESP_LOGI(TAG, "station "MACSTR" join, AID=%d", MAC2STR(event->mac), event->aid);
|
||||
} else if (event_id == WIFI_EVENT_AP_STADISCONNECTED) {
|
||||
wifi_event_ap_stadisconnected_t *event = (wifi_event_ap_stadisconnected_t *) event_data;
|
||||
ESP_LOGI(TAG, "station "MACSTR" leave, AID=%d", MAC2STR(event->mac), event->aid);
|
||||
}
|
||||
}
|
||||
|
||||
void wifi_init_softap(void)
|
||||
{
|
||||
ESP_ERROR_CHECK(esp_netif_init());
|
||||
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
||||
esp_netif_create_default_wifi_ap();
|
||||
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
||||
|
||||
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL, NULL));
|
||||
|
||||
wifi_config_t wifi_config = {
|
||||
.ap = {
|
||||
.ssid = TEST_ESP_WIFI_SSID,
|
||||
.ssid_len = strlen(TEST_ESP_WIFI_SSID),
|
||||
.channel = TEST_ESP_WIFI_CHANNEL,
|
||||
.password = TEST_ESP_WIFI_PASS,
|
||||
.max_connection = TEST_MAX_STA_CONN,
|
||||
.authmode = WIFI_AUTH_WPA_WPA2_PSK
|
||||
},
|
||||
};
|
||||
if (strlen(TEST_ESP_WIFI_PASS) == 0) {
|
||||
wifi_config.ap.authmode = WIFI_AUTH_OPEN;
|
||||
}
|
||||
|
||||
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));
|
||||
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &wifi_config));
|
||||
ESP_ERROR_CHECK(esp_wifi_start());
|
||||
|
||||
ESP_LOGI(TAG, "wifi_init_softap finished. SSID:%s password:%s channel:%d", TEST_ESP_WIFI_SSID, TEST_ESP_WIFI_PASS, TEST_ESP_WIFI_CHANNEL);
|
||||
}
|
||||
|
||||
#endif //I2C_TEST_WIFI_AP_ENABLE
|
||||
|
||||
#if CONFIG_TEST_I2C_MASTER_MODE
|
||||
|
||||
static esp_timer_handle_t wait_timer;
|
||||
static uint32_t timer_count = 1000 * 1000;
|
||||
|
||||
static esp_err_t i2c_master_init(void)
|
||||
{
|
||||
i2c_port_t i2c_master_port = I2C_MASTER_NUM_PORT;
|
||||
|
||||
i2c_config_t conf_master = {
|
||||
.mode = I2C_MODE_MASTER,
|
||||
.sda_io_num = I2C_SDA_GPIO,
|
||||
.sda_pullup_en = GPIO_PULLUP_ENABLE,
|
||||
.scl_io_num = I2C_SCL_GPIO,
|
||||
.scl_pullup_en = GPIO_PULLUP_ENABLE,
|
||||
.master.clk_speed = I2C_CLK_FREQUENCY,
|
||||
};
|
||||
|
||||
esp_err_t err = i2c_param_config(i2c_master_port, &conf_master);
|
||||
if (err != ESP_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
return i2c_driver_install(i2c_master_port, conf_master.mode, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0);
|
||||
}
|
||||
|
||||
// i2c master write
|
||||
static esp_err_t i2c_master_write_to_slave(uint8_t *data, uint32_t size)
|
||||
{
|
||||
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
|
||||
i2c_master_start(cmd);
|
||||
i2c_master_write_byte(cmd, (I2C_SLAVE_ADDR << 1) | I2C_MASTER_WRITE, I2C_ACK_CHECK_DIS);
|
||||
i2c_master_write(cmd, data, size, I2C_ACK_CHECK_DIS);
|
||||
|
||||
i2c_master_stop(cmd);
|
||||
esp_err_t ret = i2c_master_cmd_begin(I2C_MASTER_NUM_PORT, cmd, 1000 / portTICK_RATE_MS);
|
||||
i2c_cmd_link_delete(cmd);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static esp_err_t i2c_data_write(void)
|
||||
{
|
||||
uint8_t data[] = {0x6a, 0x1b, 0x05, 0x1f, 0x1f, 0x08, 0x01, 0x20, 0x19, 0x03, 0x27};
|
||||
|
||||
esp_err_t ret = i2c_master_write_to_slave(data, sizeof(data));
|
||||
if(ret != ESP_OK) {
|
||||
ESP_LOGW(TAG,"I2C timeout");
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void wait_timer_callback(void *arg)
|
||||
{
|
||||
// i2c write
|
||||
i2c_data_write();
|
||||
/* For local test, please remove this part and give long time testing. */
|
||||
timer_count--;
|
||||
if (timer_count == 0) {
|
||||
esp_timer_stop(wait_timer);
|
||||
return;
|
||||
}
|
||||
|
||||
// restart timer
|
||||
esp_timer_start_once(wait_timer, WAIT_TIMER_PERIOD);
|
||||
}
|
||||
|
||||
static const esp_timer_create_args_t wait_timer_args = {
|
||||
.callback = &wait_timer_callback,
|
||||
.arg = NULL,
|
||||
.name = "wait_timer",
|
||||
};
|
||||
|
||||
#else
|
||||
|
||||
static esp_err_t i2c_slave_init(void)
|
||||
{
|
||||
i2c_port_t i2c_slave_port = I2C_SLAVE_NUM_PORT;
|
||||
|
||||
i2c_config_t conf_slave = {
|
||||
.mode = I2C_MODE_SLAVE,
|
||||
.sda_io_num = I2C_SDA_GPIO,
|
||||
.sda_pullup_en = GPIO_PULLUP_ENABLE,
|
||||
.scl_io_num = I2C_SCL_GPIO,
|
||||
.scl_pullup_en = GPIO_PULLUP_ENABLE,
|
||||
.slave.addr_10bit_en = 0,
|
||||
.slave.slave_addr = I2C_SLAVE_ADDR,
|
||||
|
||||
};
|
||||
|
||||
i2c_param_config(i2c_slave_port, &conf_slave);
|
||||
return i2c_driver_install(i2c_slave_port, conf_slave.mode, I2C_SLAVE_RX_BUF_LEN, I2C_SLAVE_TX_BUF_LEN, 0);
|
||||
}
|
||||
|
||||
static void i2c_data_read(void *arg)
|
||||
{
|
||||
printf("slave mode.read thread start...\r\n");
|
||||
while (1) {
|
||||
uint8_t data[11] = {0};
|
||||
i2c_slave_read_buffer(I2C_SLAVE_NUM_PORT, data, sizeof(data), 1000 / portTICK_RATE_MS);
|
||||
ESP_LOG_BUFFER_HEX(TAG, data, sizeof(data));
|
||||
vTaskDelay(1);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
// i2c init function
|
||||
static esp_err_t i2c_init(void)
|
||||
{
|
||||
esp_err_t err = ESP_OK;
|
||||
#if CONFIG_TEST_I2C_MASTER_MODE
|
||||
err = i2c_master_init();
|
||||
#else
|
||||
err = i2c_slave_init();
|
||||
#endif
|
||||
return err;
|
||||
}
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
esp_err_t ret = i2c_init();
|
||||
|
||||
#if I2C_TEST_WIFI_AP_ENABLE
|
||||
//Initialize NVS
|
||||
ret = nvs_flash_init();
|
||||
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
||||
ESP_ERROR_CHECK(nvs_flash_erase());
|
||||
ret = nvs_flash_init();
|
||||
}
|
||||
ESP_ERROR_CHECK(ret);
|
||||
|
||||
ESP_LOGI(TAG, "ESP_WIFI_MODE_AP");
|
||||
wifi_init_softap();
|
||||
#endif
|
||||
|
||||
#if CONFIG_TEST_I2C_MASTER_MODE
|
||||
ret = esp_timer_create(&wait_timer_args, &wait_timer);
|
||||
if (ret != ESP_OK) {
|
||||
ESP_LOGE(TAG, "esp_timer_create fail!");
|
||||
}
|
||||
|
||||
ret = esp_timer_start_once(wait_timer, WAIT_TIMER_PERIOD);
|
||||
if (ret != ESP_OK) {
|
||||
ESP_LOGE(TAG, "esp_timer_start_once fail!");
|
||||
}
|
||||
#else
|
||||
xTaskCreatePinnedToCore(i2c_data_read, "i2c_data_read", (3 * 1024), NULL, 1, NULL, 1);
|
||||
#endif
|
||||
ESP_ERROR_CHECK(ret);
|
||||
printf("I2C-WIFI test success\n");
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user