From 301023e6afdd00a2a95f0d8cc94484111eeab3db Mon Sep 17 00:00:00 2001 From: MagicPrince666 <846863428@qq.com> Date: Wed, 7 Jun 2023 10:38:15 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=BC=96=E8=AF=91?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 ++ .../network_adapter/main/slave_control.c | 23 +++++++++---------- 2 files changed, 13 insertions(+), 12 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000..4c1de46386 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +**/build +**/sdkconfig \ No newline at end of file diff --git a/esp_hosted_fg/esp/esp_driver/network_adapter/main/slave_control.c b/esp_hosted_fg/esp/esp_driver/network_adapter/main/slave_control.c index cc03ecaf3b..84af05b1ed 100644 --- a/esp_hosted_fg/esp/esp_driver/network_adapter/main/slave_control.c +++ b/esp_hosted_fg/esp/esp_driver/network_adapter/main/slave_control.c @@ -650,10 +650,10 @@ static esp_err_t req_get_ap_config_handler (CtrlMsg *req, } snprintf((char *)credentials.bssid,BSSID_LENGTH,MACSTR,MAC2STR(ap_info->bssid)); - if (ap_info->ssid) { - strncpy((char *)credentials.ssid, (char *)ap_info->ssid, - min(sizeof(credentials.ssid), strlen((char *)ap_info->ssid)+1)); - } + + strncpy((char *)credentials.ssid, (char *)ap_info->ssid, + min(sizeof(credentials.ssid), strlen((char *)ap_info->ssid)+1)); + credentials.rssi = ap_info->rssi; credentials.chnl = ap_info->primary; credentials.ecn = ap_info->authmode; @@ -794,14 +794,13 @@ static esp_err_t req_get_softap_config_handler (CtrlMsg *req, goto err; } - if (get_conf.ap.ssid) { - strncpy((char *)credentials.ssid,(char *)&get_conf.ap.ssid, - min(sizeof(credentials.ssid), strlen((char *)&get_conf.ap.ssid)+1)); - } - if (get_conf.ap.password) { - strncpy((char *)credentials.pwd,(char *)&get_conf.ap.password, - min(sizeof(credentials.pwd), strlen((char *)&get_conf.ap.password)+1)); - } + + strncpy((char *)credentials.ssid,(char *)&get_conf.ap.ssid, + min(sizeof(credentials.ssid), strlen((char *)&get_conf.ap.ssid)+1)); + + strncpy((char *)credentials.pwd,(char *)&get_conf.ap.password, + min(sizeof(credentials.pwd), strlen((char *)&get_conf.ap.password)+1)); + credentials.chnl = get_conf.ap.channel; credentials.max_conn = get_conf.ap.max_connection; credentials.ecn = get_conf.ap.authmode; From d480eb4473c3c31f760484a7a251ad1d265e7ac0 Mon Sep 17 00:00:00 2001 From: leo Date: Tue, 20 Jun 2023 18:41:08 +0800 Subject: [PATCH 2/4] fix bug --- .../esp/esp_driver/network_adapter/main/app_main.c | 3 ++- .../esp/esp_driver/network_adapter/main/cmd.c | 14 +++++++------- .../network_adapter/main/sdio_slave_api.c | 1 + 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/esp_hosted_ng/esp/esp_driver/network_adapter/main/app_main.c b/esp_hosted_ng/esp/esp_driver/network_adapter/main/app_main.c index 3c172a7e6b..d21c8c9652 100644 --- a/esp_hosted_ng/esp/esp_driver/network_adapter/main/app_main.c +++ b/esp_hosted_ng/esp/esp_driver/network_adapter/main/app_main.c @@ -32,6 +32,7 @@ #include "app_main.h" #include "esp_wifi.h" #include "cmd.h" +#include #include "freertos/task.h" #include "freertos/queue.h" @@ -664,7 +665,7 @@ void app_main() /*send capabilities to host*/ send_bootup_event_to_host(capa); - tcpip_adapter_init(); + esp_netif_init(); ESP_LOGI(TAG,"Initial set up done"); } diff --git a/esp_hosted_ng/esp/esp_driver/network_adapter/main/cmd.c b/esp_hosted_ng/esp/esp_driver/network_adapter/main/cmd.c index 35c921e43a..86733c1984 100644 --- a/esp_hosted_ng/esp/esp_driver/network_adapter/main/cmd.c +++ b/esp_hosted_ng/esp/esp_driver/network_adapter/main/cmd.c @@ -136,7 +136,7 @@ int station_rx_eapol(uint8_t *src_addr, uint8_t *buf, uint32_t len) u8 own_mac[MAC_ADDR_LEN] = {0}; if (!src_addr || !buf || !len) { - ESP_LOGI(TAG, "eapol err - src_addr: %p buf: %p len: %u\n", + ESP_LOGI(TAG, "eapol err - src_addr: %p buf: %p len: %lu\n", src_addr, buf, len); //TODO : free buf using esp_wifi_internal_free_rx_buffer? return ESP_FAIL; @@ -415,7 +415,7 @@ static void wifi_event_handler(void* arg, esp_event_base_t event_base, esp_err_t result; if (event_base != WIFI_EVENT) { - ESP_LOGI(TAG, "Received unregistered event %s[%d]\n", event_base, event_id); + ESP_LOGI(TAG, "Received unregistered event %s[%ld]\n", event_base, event_id); return; } @@ -458,7 +458,7 @@ static void wifi_event_handler(void* arg, esp_event_base_t event_base, break; default: - ESP_LOGI(TAG, "Unregistered event: %d\n", event_id); + ESP_LOGI(TAG, "Unregistered event: %ld\n", event_id); } } @@ -1016,7 +1016,7 @@ int process_auth_request(uint8_t if_type, uint8_t *payload, uint16_t payload_len } else { wifi_scan_config_t params = {0}; - if (cmd_auth->bssid) { + { params.bssid = malloc(sizeof(cmd_auth->bssid)); assert(params.bssid); @@ -1211,7 +1211,7 @@ int process_sta_connect(uint8_t if_type, uint8_t *payload, uint16_t payload_len) wifi_config.sta.channel = cmd_connect->channel; ESP_LOGI(TAG, "%s, channel: %u", cmd_connect->ssid, cmd_connect->channel); - if (cmd_connect->assoc_ie && cmd_connect->assoc_ie_len) { + if (cmd_connect->assoc_ie_len) { esp_wifi_unset_appie_internal(WIFI_APPIE_ASSOC_REQ); esp_wifi_set_appie_internal(WIFI_APPIE_ASSOC_REQ, cmd_connect->assoc_ie, cmd_connect->assoc_ie_len, 0); @@ -1710,7 +1710,7 @@ int process_add_key(uint8_t if_type, uint8_t *payload, uint16_t payload_len) if (key->algo == WIFI_WPA_ALG_IGTK) { wifi_wpa_igtk_t igtk = {0}; - ESP_LOGI(TAG, "Setting iGTK [%d]\n", key->index); + ESP_LOGI(TAG, "Setting iGTK [%ld]\n", key->index); memcpy(igtk.igtk, key->data, key->len); memcpy(igtk.pn, key->seq, key->seq_len); @@ -1718,7 +1718,7 @@ int process_add_key(uint8_t if_type, uint8_t *payload, uint16_t payload_len) ret = esp_wifi_set_igtk_internal(0, &igtk); } else { /* GTK */ - ESP_LOGI(TAG, "Setting GTK [%d]\n", key->index); + ESP_LOGI(TAG, "Setting GTK [%ld]\n", key->index); ret = esp_wifi_set_sta_key_internal(key->algo, key->mac_addr, key->index, 0, key->seq, key->seq_len, key->data, key->len, KEY_FLAG_GROUP | KEY_FLAG_RX); diff --git a/esp_hosted_ng/esp/esp_driver/network_adapter/main/sdio_slave_api.c b/esp_hosted_ng/esp/esp_driver/network_adapter/main/sdio_slave_api.c index ff56392d49..ba6e8c7c40 100644 --- a/esp_hosted_ng/esp/esp_driver/network_adapter/main/sdio_slave_api.c +++ b/esp_hosted_ng/esp/esp_driver/network_adapter/main/sdio_slave_api.c @@ -26,6 +26,7 @@ #include "sdio_slave_api.h" #include "driver/sdio_slave.h" #include "soc/sdio_slave_periph.h" +#include "soc/gpio_reg.h" #include "endian.h" #include "freertos/semphr.h" #include "stats.h" From d085ccb06e9a042eb4800c387681483b0bd5b557 Mon Sep 17 00:00:00 2001 From: leo Date: Tue, 20 Jun 2023 19:48:12 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E5=B1=8F=E8=94=BD=E6=97=A0=E7=94=A8?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- esp_hosted_ng/esp/esp_driver/network_adapter/main/cmd.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/esp_hosted_ng/esp/esp_driver/network_adapter/main/cmd.c b/esp_hosted_ng/esp/esp_driver/network_adapter/main/cmd.c index 86733c1984..069f1c6bb1 100644 --- a/esp_hosted_ng/esp/esp_driver/network_adapter/main/cmd.c +++ b/esp_hosted_ng/esp/esp_driver/network_adapter/main/cmd.c @@ -123,7 +123,7 @@ int sta_connection(uint8_t *bssid) return ESP_FAIL; } memcpy(ap_bssid, bssid, MAC_ADDR_LEN); - esp_wifi_connect_internal(ap_bssid); + // esp_wifi_connect_internal(ap_bssid); return ESP_OK; } @@ -1011,7 +1011,7 @@ int process_auth_request(uint8_t if_type, uint8_t *payload, uint16_t payload_len /* WPA3 specific */ ESP_LOGI(TAG, "AUTH Confirm\n"); - esp_wifi_issue_auth_internal(0); + // esp_wifi_issue_auth_internal(0); } else { wifi_scan_config_t params = {0}; @@ -1147,7 +1147,7 @@ int process_assoc_request(uint8_t if_type, uint8_t *payload, uint16_t payload_le cmd_assoc->assoc_ie_len, 0); } - esp_wifi_issue_assoc_internal(0); + // esp_wifi_issue_assoc_internal(0); buf_handle.if_type = if_type; buf_handle.if_num = 0; From d17af671b86b890e4313ae038c0169e969f80e1a Mon Sep 17 00:00:00 2001 From: leo Date: Wed, 21 Jun 2023 10:59:06 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E6=B7=BB=E5=8A=A0adc=E5=92=8Ctft?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 5 +- .../network_adapter/main/CMakeLists.txt | 2 +- .../network_adapter/main/Kconfig.projbuild | 32 ++ .../network_adapter/main/adc_read.c | 151 +++++++++ .../network_adapter/main/app_main.c | 4 + .../network_adapter/main/idf_component.yml | 6 + .../network_adapter/main/include/adc_read.h | 6 + .../network_adapter/main/include/display.h | 6 + .../network_adapter/main/lvgl_demo_ui.c | 86 +++++ .../network_adapter/main/spi_lcd_touch.c | 309 ++++++++++++++++++ 10 files changed, 605 insertions(+), 2 deletions(-) create mode 100644 esp_hosted_ng/esp/esp_driver/network_adapter/main/adc_read.c create mode 100644 esp_hosted_ng/esp/esp_driver/network_adapter/main/idf_component.yml create mode 100644 esp_hosted_ng/esp/esp_driver/network_adapter/main/include/adc_read.h create mode 100644 esp_hosted_ng/esp/esp_driver/network_adapter/main/include/display.h create mode 100644 esp_hosted_ng/esp/esp_driver/network_adapter/main/lvgl_demo_ui.c create mode 100644 esp_hosted_ng/esp/esp_driver/network_adapter/main/spi_lcd_touch.c diff --git a/.gitignore b/.gitignore index 4c1de46386..908037c975 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,5 @@ **/build -**/sdkconfig \ No newline at end of file +**/managed_components +**/sdkconfig +**/dependencies.lock +**/sdkconfig.old diff --git a/esp_hosted_ng/esp/esp_driver/network_adapter/main/CMakeLists.txt b/esp_hosted_ng/esp/esp_driver/network_adapter/main/CMakeLists.txt index 45267293c4..6303302ce4 100644 --- a/esp_hosted_ng/esp/esp_driver/network_adapter/main/CMakeLists.txt +++ b/esp_hosted_ng/esp/esp_driver/network_adapter/main/CMakeLists.txt @@ -1,4 +1,4 @@ -set(COMPONENT_SRCS "app_main.c" "slave_bt.c" "cmd.c" "stats.c") +set(COMPONENT_SRCS "app_main.c" "slave_bt.c" "cmd.c" "stats.c" "spi_lcd_touch.c" "lvgl_demo_ui.c" "adc_read.c") set(COMPONENT_ADD_INCLUDEDIRS "./include") if(CONFIG_ESP_SDIO_HOST_INTERFACE) diff --git a/esp_hosted_ng/esp/esp_driver/network_adapter/main/Kconfig.projbuild b/esp_hosted_ng/esp/esp_driver/network_adapter/main/Kconfig.projbuild index d5a589797d..609a4c7aa7 100644 --- a/esp_hosted_ng/esp/esp_driver/network_adapter/main/Kconfig.projbuild +++ b/esp_hosted_ng/esp/esp_driver/network_adapter/main/Kconfig.projbuild @@ -100,4 +100,36 @@ menu "Example Configuration" help UART Baudrate for HCI over ESP32C3. Please use standard baudrate. + choice EXAMPLE_LCD_CONTROLLER + prompt "LCD controller model" + default EXAMPLE_LCD_CONTROLLER_ILI9341 + help + Select LCD controller model + + config EXAMPLE_LCD_CONTROLLER_ILI9341 + bool "ILI9341" + + config EXAMPLE_LCD_CONTROLLER_GC9A01 + bool "GC9A01" + endchoice + + config EXAMPLE_LCD_TOUCH_ENABLED + bool "Enable LCD touch" + default n + help + Enable this option if you wish to use display touch. You can select from touch controllers. + + choice EXAMPLE_LCD_TOUCH_CONTROLLER + prompt "LCD touch controller model" + depends on EXAMPLE_LCD_TOUCH_ENABLED + default EXAMPLE_LCD_TOUCH_CONTROLLER_STMPE610 + help + Select LCD touch controller model + + config EXAMPLE_LCD_TOUCH_CONTROLLER_STMPE610 + bool "STMPE610" + help + Touch controller STMPE610 connected via SPI. + endchoice + endmenu diff --git a/esp_hosted_ng/esp/esp_driver/network_adapter/main/adc_read.c b/esp_hosted_ng/esp/esp_driver/network_adapter/main/adc_read.c new file mode 100644 index 0000000000..07ad1ccf9c --- /dev/null +++ b/esp_hosted_ng/esp/esp_driver/network_adapter/main/adc_read.c @@ -0,0 +1,151 @@ +/* + * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include "sdkconfig.h" +#include "esp_log.h" +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "freertos/semphr.h" +#include "esp_adc/adc_continuous.h" +#include "adc_read.h" + +#define EXAMPLE_ADC_UNIT ADC_UNIT_1 +#define _EXAMPLE_ADC_UNIT_STR(unit) #unit +#define EXAMPLE_ADC_UNIT_STR(unit) _EXAMPLE_ADC_UNIT_STR(unit) +#define EXAMPLE_ADC_CONV_MODE ADC_CONV_SINGLE_UNIT_1 +#define EXAMPLE_ADC_ATTEN ADC_ATTEN_DB_0 +#define EXAMPLE_ADC_BIT_WIDTH SOC_ADC_DIGI_MAX_BITWIDTH + +#if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2 +#define EXAMPLE_ADC_OUTPUT_TYPE ADC_DIGI_OUTPUT_FORMAT_TYPE1 +#define EXAMPLE_ADC_GET_CHANNEL(p_data) ((p_data)->type1.channel) +#define EXAMPLE_ADC_GET_DATA(p_data) ((p_data)->type1.data) +#else +#define EXAMPLE_ADC_OUTPUT_TYPE ADC_DIGI_OUTPUT_FORMAT_TYPE2 +#define EXAMPLE_ADC_GET_CHANNEL(p_data) ((p_data)->type2.channel) +#define EXAMPLE_ADC_GET_DATA(p_data) ((p_data)->type2.data) +#endif + +#define EXAMPLE_READ_LEN 256 + +#if CONFIG_IDF_TARGET_ESP32 +static adc_channel_t channel[2] = {ADC_CHANNEL_6, ADC_CHANNEL_7}; +#else +static adc_channel_t channel[5] = {ADC_CHANNEL_0, ADC_CHANNEL_3, ADC_CHANNEL_4, ADC_CHANNEL_6, ADC_CHANNEL_7}; +#endif + +static TaskHandle_t s_task_handle; +static const char *TAG = "EXAMPLE"; + + +static bool IRAM_ATTR s_conv_done_cb(adc_continuous_handle_t handle, const adc_continuous_evt_data_t *edata, void *user_data) +{ + BaseType_t mustYield = pdFALSE; + //Notify that ADC continuous driver has done enough number of conversions + vTaskNotifyGiveFromISR(s_task_handle, &mustYield); + + return (mustYield == pdTRUE); +} + +static void continuous_adc_init(adc_channel_t *channel, uint8_t channel_num, adc_continuous_handle_t *out_handle) +{ + adc_continuous_handle_t handle = NULL; + + adc_continuous_handle_cfg_t adc_config = { + .max_store_buf_size = 1024, + .conv_frame_size = EXAMPLE_READ_LEN, + }; + ESP_ERROR_CHECK(adc_continuous_new_handle(&adc_config, &handle)); + + adc_continuous_config_t dig_cfg = { + .sample_freq_hz = 20 * 1000, + .conv_mode = EXAMPLE_ADC_CONV_MODE, + .format = EXAMPLE_ADC_OUTPUT_TYPE, + }; + + adc_digi_pattern_config_t adc_pattern[SOC_ADC_PATT_LEN_MAX] = {0}; + dig_cfg.pattern_num = channel_num; + for (int i = 0; i < channel_num; i++) { + adc_pattern[i].atten = EXAMPLE_ADC_ATTEN; + adc_pattern[i].channel = channel[i] & 0x7; + adc_pattern[i].unit = EXAMPLE_ADC_UNIT; + adc_pattern[i].bit_width = EXAMPLE_ADC_BIT_WIDTH; + + ESP_LOGI(TAG, "adc_pattern[%d].atten is :%"PRIx8, i, adc_pattern[i].atten); + ESP_LOGI(TAG, "adc_pattern[%d].channel is :%"PRIx8, i, adc_pattern[i].channel); + ESP_LOGI(TAG, "adc_pattern[%d].unit is :%"PRIx8, i, adc_pattern[i].unit); + } + dig_cfg.adc_pattern = adc_pattern; + ESP_ERROR_CHECK(adc_continuous_config(handle, &dig_cfg)); + + *out_handle = handle; +} + +void adc_init(void) +{ + esp_err_t ret; + uint32_t ret_num = 0; + uint8_t result[EXAMPLE_READ_LEN] = {0}; + memset(result, 0xcc, EXAMPLE_READ_LEN); + + s_task_handle = xTaskGetCurrentTaskHandle(); + + adc_continuous_handle_t handle = NULL; + continuous_adc_init(channel, sizeof(channel) / sizeof(adc_channel_t), &handle); + + adc_continuous_evt_cbs_t cbs = { + .on_conv_done = s_conv_done_cb, + }; + ESP_ERROR_CHECK(adc_continuous_register_event_callbacks(handle, &cbs, NULL)); + ESP_ERROR_CHECK(adc_continuous_start(handle)); + + while(1) { + + /** + * This is to show you the way to use the ADC continuous mode driver event callback. + * This `ulTaskNotifyTake` will block when the data processing in the task is fast. + * However in this example, the data processing (print) is slow, so you barely block here. + * + * Without using this event callback (to notify this task), you can still just call + * `adc_continuous_read()` here in a loop, with/without a certain block timeout. + */ + ulTaskNotifyTake(pdTRUE, portMAX_DELAY); + + char unit[] = EXAMPLE_ADC_UNIT_STR(EXAMPLE_ADC_UNIT); + + while (1) { + ret = adc_continuous_read(handle, result, EXAMPLE_READ_LEN, &ret_num, 0); + if (ret == ESP_OK) { + ESP_LOGI("TASK", "ret is %x, ret_num is %"PRIu32" bytes", ret, ret_num); + for (int i = 0; i < ret_num; i += SOC_ADC_DIGI_RESULT_BYTES) { + adc_digi_output_data_t *p = (adc_digi_output_data_t*)&result[i]; + uint32_t chan_num = EXAMPLE_ADC_GET_CHANNEL(p); + uint32_t data = EXAMPLE_ADC_GET_DATA(p); + /* Check the channel number validation, the data is invalid if the channel num exceed the maximum channel */ + if (chan_num < SOC_ADC_CHANNEL_NUM(EXAMPLE_ADC_UNIT)) { + ESP_LOGI(TAG, "Unit: %s, Channel: %"PRIu32", Value: %"PRIx32, unit, chan_num, data); + } else { + ESP_LOGW(TAG, "Invalid data [%s_%"PRIu32"_%"PRIx32"]", unit, chan_num, data); + } + } + /** + * Because printing is slow, so every time you call `ulTaskNotifyTake`, it will immediately return. + * To avoid a task watchdog timeout, add a delay here. When you replace the way you process the data, + * usually you don't need this delay (as this task will block for a while). + */ + vTaskDelay(1); + } else if (ret == ESP_ERR_TIMEOUT) { + //We try to read `EXAMPLE_READ_LEN` until API returns timeout, which means there's no available data + break; + } + } + } + + ESP_ERROR_CHECK(adc_continuous_stop(handle)); + ESP_ERROR_CHECK(adc_continuous_deinit(handle)); +} diff --git a/esp_hosted_ng/esp/esp_driver/network_adapter/main/app_main.c b/esp_hosted_ng/esp/esp_driver/network_adapter/main/app_main.c index d21c8c9652..4a67980b5d 100644 --- a/esp_hosted_ng/esp/esp_driver/network_adapter/main/app_main.c +++ b/esp_hosted_ng/esp/esp_driver/network_adapter/main/app_main.c @@ -47,6 +47,8 @@ #include "driver/periph_ctrl.h" #include "slave_bt.c" #include "stats.h" +#include "display.h" +#include "adc_read.h" static const char TAG[] = "FW_MAIN"; @@ -609,6 +611,8 @@ void app_main() } ESP_ERROR_CHECK( ret ); + // spi_touch_tftf_init(); + ret = initialise_wifi(); ESP_ERROR_CHECK( ret ); diff --git a/esp_hosted_ng/esp/esp_driver/network_adapter/main/idf_component.yml b/esp_hosted_ng/esp/esp_driver/network_adapter/main/idf_component.yml new file mode 100644 index 0000000000..a1a4039923 --- /dev/null +++ b/esp_hosted_ng/esp/esp_driver/network_adapter/main/idf_component.yml @@ -0,0 +1,6 @@ +dependencies: + idf: ">=4.4" + lvgl/lvgl: "~8.3.0" + esp_lcd_ili9341: "^1.0" + esp_lcd_gc9a01: "^1.0" + esp_lcd_touch_stmpe610: "^1.0" diff --git a/esp_hosted_ng/esp/esp_driver/network_adapter/main/include/adc_read.h b/esp_hosted_ng/esp/esp_driver/network_adapter/main/include/adc_read.h new file mode 100644 index 0000000000..5792f919f2 --- /dev/null +++ b/esp_hosted_ng/esp/esp_driver/network_adapter/main/include/adc_read.h @@ -0,0 +1,6 @@ +#ifndef __ESP_ADC_REMOTE_H +#define __ESP_ADC_REMOTE_H + +void adc_init(void); + +#endif \ No newline at end of file diff --git a/esp_hosted_ng/esp/esp_driver/network_adapter/main/include/display.h b/esp_hosted_ng/esp/esp_driver/network_adapter/main/include/display.h new file mode 100644 index 0000000000..3633b48bff --- /dev/null +++ b/esp_hosted_ng/esp/esp_driver/network_adapter/main/include/display.h @@ -0,0 +1,6 @@ +#ifndef __ESP_SPI_TOUCH_TFT_H +#define __ESP_SPI_TOUCH_TFT_H + +void spi_touch_tftf_init(void); + +#endif \ No newline at end of file diff --git a/esp_hosted_ng/esp/esp_driver/network_adapter/main/lvgl_demo_ui.c b/esp_hosted_ng/esp/esp_driver/network_adapter/main/lvgl_demo_ui.c new file mode 100644 index 0000000000..591dd95eed --- /dev/null +++ b/esp_hosted_ng/esp/esp_driver/network_adapter/main/lvgl_demo_ui.c @@ -0,0 +1,86 @@ +/* + * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: CC0-1.0 + */ + +// This demo UI is adapted from LVGL official example: https://docs.lvgl.io/master/widgets/extra/meter.html#simple-meter + +#include "lvgl.h" + +static lv_obj_t *meter; +static lv_obj_t * btn; +static lv_disp_rot_t rotation = LV_DISP_ROT_NONE; + +static void set_value(void *indic, int32_t v) +{ + lv_meter_set_indicator_end_value(meter, indic, v); +} + +static void btn_cb(lv_event_t * e) +{ + lv_disp_t *disp = lv_event_get_user_data(e); + rotation++; + if (rotation > LV_DISP_ROT_270) { + rotation = LV_DISP_ROT_NONE; + } + lv_disp_set_rotation(disp, rotation); +} + +void example_lvgl_demo_ui(lv_disp_t *disp) +{ + lv_obj_t *scr = lv_disp_get_scr_act(disp); + meter = lv_meter_create(scr); + lv_obj_center(meter); + lv_obj_set_size(meter, 200, 200); + + /*Add a scale first*/ + lv_meter_scale_t *scale = lv_meter_add_scale(meter); + lv_meter_set_scale_ticks(meter, scale, 41, 2, 10, lv_palette_main(LV_PALETTE_GREY)); + lv_meter_set_scale_major_ticks(meter, scale, 8, 4, 15, lv_color_black(), 10); + + lv_meter_indicator_t *indic; + + /*Add a blue arc to the start*/ + indic = lv_meter_add_arc(meter, scale, 3, lv_palette_main(LV_PALETTE_BLUE), 0); + lv_meter_set_indicator_start_value(meter, indic, 0); + lv_meter_set_indicator_end_value(meter, indic, 20); + + /*Make the tick lines blue at the start of the scale*/ + indic = lv_meter_add_scale_lines(meter, scale, lv_palette_main(LV_PALETTE_BLUE), lv_palette_main(LV_PALETTE_BLUE), false, 0); + lv_meter_set_indicator_start_value(meter, indic, 0); + lv_meter_set_indicator_end_value(meter, indic, 20); + + /*Add a red arc to the end*/ + indic = lv_meter_add_arc(meter, scale, 3, lv_palette_main(LV_PALETTE_RED), 0); + lv_meter_set_indicator_start_value(meter, indic, 80); + lv_meter_set_indicator_end_value(meter, indic, 100); + + /*Make the tick lines red at the end of the scale*/ + indic = lv_meter_add_scale_lines(meter, scale, lv_palette_main(LV_PALETTE_RED), lv_palette_main(LV_PALETTE_RED), false, 0); + lv_meter_set_indicator_start_value(meter, indic, 80); + lv_meter_set_indicator_end_value(meter, indic, 100); + + /*Add a needle line indicator*/ + indic = lv_meter_add_needle_line(meter, scale, 4, lv_palette_main(LV_PALETTE_GREY), -10); + + btn = lv_btn_create(scr); + lv_obj_t * lbl = lv_label_create(btn); + lv_label_set_text_static(lbl, LV_SYMBOL_REFRESH" ROTATE"); + lv_obj_align(btn, LV_ALIGN_BOTTOM_LEFT, 30, -30); + /*Button event*/ + lv_obj_add_event_cb(btn, btn_cb, LV_EVENT_CLICKED, disp); + + /*Create an animation to set the value*/ + lv_anim_t a; + lv_anim_init(&a); + lv_anim_set_exec_cb(&a, set_value); + lv_anim_set_var(&a, indic); + lv_anim_set_values(&a, 0, 100); + lv_anim_set_time(&a, 2000); + lv_anim_set_repeat_delay(&a, 100); + lv_anim_set_playback_time(&a, 500); + lv_anim_set_playback_delay(&a, 100); + lv_anim_set_repeat_count(&a, LV_ANIM_REPEAT_INFINITE); + lv_anim_start(&a); +} diff --git a/esp_hosted_ng/esp/esp_driver/network_adapter/main/spi_lcd_touch.c b/esp_hosted_ng/esp/esp_driver/network_adapter/main/spi_lcd_touch.c new file mode 100644 index 0000000000..0885c4ba51 --- /dev/null +++ b/esp_hosted_ng/esp/esp_driver/network_adapter/main/spi_lcd_touch.c @@ -0,0 +1,309 @@ +/* + * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: CC0-1.0 + */ + +#include +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "esp_timer.h" +#include "esp_lcd_panel_io.h" +#include "esp_lcd_panel_vendor.h" +#include "esp_lcd_panel_ops.h" +#include "driver/gpio.h" +#include "driver/spi_master.h" +#include "esp_err.h" +#include "esp_log.h" +#include "lvgl.h" +#include "display.h" + +#if CONFIG_EXAMPLE_LCD_CONTROLLER_ILI9341 +#include "esp_lcd_ili9341.h" +#elif CONFIG_EXAMPLE_LCD_CONTROLLER_GC9A01 +#include "esp_lcd_gc9a01.h" +#endif + +#if CONFIG_EXAMPLE_LCD_TOUCH_CONTROLLER_STMPE610 +#include "esp_lcd_touch_stmpe610.h" +#endif + +static const char *TAG = "example"; + +// Using SPI2 in the example +#define LCD_HOST SPI2_HOST + +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +//////////////////// Please update the following configuration according to your LCD spec ////////////////////////////// +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +#define EXAMPLE_LCD_PIXEL_CLOCK_HZ (20 * 1000 * 1000) +#define EXAMPLE_LCD_BK_LIGHT_ON_LEVEL 1 +#define EXAMPLE_LCD_BK_LIGHT_OFF_LEVEL !EXAMPLE_LCD_BK_LIGHT_ON_LEVEL +#define EXAMPLE_PIN_NUM_SCLK 16 +#define EXAMPLE_PIN_NUM_MOSI 17 +#define EXAMPLE_PIN_NUM_MISO 21 +#define EXAMPLE_PIN_NUM_LCD_DC 26 +#define EXAMPLE_PIN_NUM_LCD_RST 0 +#define EXAMPLE_PIN_NUM_LCD_CS 25 +#define EXAMPLE_PIN_NUM_BK_LIGHT 27 +#define EXAMPLE_PIN_NUM_TOUCH_CS 22 + +// The pixel number in horizontal and vertical +#if CONFIG_EXAMPLE_LCD_CONTROLLER_ILI9341 +#define EXAMPLE_LCD_H_RES 240 +#define EXAMPLE_LCD_V_RES 320 +#elif CONFIG_EXAMPLE_LCD_CONTROLLER_GC9A01 +#define EXAMPLE_LCD_H_RES 240 +#define EXAMPLE_LCD_V_RES 240 +#endif +// Bit number used to represent command and parameter +#define EXAMPLE_LCD_CMD_BITS 8 +#define EXAMPLE_LCD_PARAM_BITS 8 + +#define EXAMPLE_LVGL_TICK_PERIOD_MS 2 + + +#if CONFIG_EXAMPLE_LCD_TOUCH_ENABLED +esp_lcd_touch_handle_t tp = NULL; +#endif + +extern void example_lvgl_demo_ui(lv_disp_t *disp); + +static bool example_notify_lvgl_flush_ready(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_io_event_data_t *edata, void *user_ctx) +{ + lv_disp_drv_t *disp_driver = (lv_disp_drv_t *)user_ctx; + lv_disp_flush_ready(disp_driver); + return false; +} + +static void example_lvgl_flush_cb(lv_disp_drv_t *drv, const lv_area_t *area, lv_color_t *color_map) +{ + esp_lcd_panel_handle_t panel_handle = (esp_lcd_panel_handle_t) drv->user_data; + int offsetx1 = area->x1; + int offsetx2 = area->x2; + int offsety1 = area->y1; + int offsety2 = area->y2; + // copy a buffer's content to a specific area of the display + esp_lcd_panel_draw_bitmap(panel_handle, offsetx1, offsety1, offsetx2 + 1, offsety2 + 1, color_map); +} + +/* Rotate display and touch, when rotated screen in LVGL. Called when driver parameters are updated. */ +static void example_lvgl_port_update_callback(lv_disp_drv_t *drv) +{ + esp_lcd_panel_handle_t panel_handle = (esp_lcd_panel_handle_t) drv->user_data; + + switch (drv->rotated) { + case LV_DISP_ROT_NONE: + // Rotate LCD display + esp_lcd_panel_swap_xy(panel_handle, false); + esp_lcd_panel_mirror(panel_handle, true, false); +#if CONFIG_EXAMPLE_LCD_TOUCH_ENABLED + // Rotate LCD touch + esp_lcd_touch_set_mirror_y(tp, false); + esp_lcd_touch_set_mirror_x(tp, false); +#endif + break; + case LV_DISP_ROT_90: + // Rotate LCD display + esp_lcd_panel_swap_xy(panel_handle, true); + esp_lcd_panel_mirror(panel_handle, true, true); +#if CONFIG_EXAMPLE_LCD_TOUCH_ENABLED + // Rotate LCD touch + esp_lcd_touch_set_mirror_y(tp, false); + esp_lcd_touch_set_mirror_x(tp, false); +#endif + break; + case LV_DISP_ROT_180: + // Rotate LCD display + esp_lcd_panel_swap_xy(panel_handle, false); + esp_lcd_panel_mirror(panel_handle, false, true); +#if CONFIG_EXAMPLE_LCD_TOUCH_ENABLED + // Rotate LCD touch + esp_lcd_touch_set_mirror_y(tp, false); + esp_lcd_touch_set_mirror_x(tp, false); +#endif + break; + case LV_DISP_ROT_270: + // Rotate LCD display + esp_lcd_panel_swap_xy(panel_handle, true); + esp_lcd_panel_mirror(panel_handle, false, false); +#if CONFIG_EXAMPLE_LCD_TOUCH_ENABLED + // Rotate LCD touch + esp_lcd_touch_set_mirror_y(tp, false); + esp_lcd_touch_set_mirror_x(tp, false); +#endif + break; + } +} + +#if CONFIG_EXAMPLE_LCD_TOUCH_ENABLED +static void example_lvgl_touch_cb(lv_indev_drv_t * drv, lv_indev_data_t * data) +{ + uint16_t touchpad_x[1] = {0}; + uint16_t touchpad_y[1] = {0}; + uint8_t touchpad_cnt = 0; + + /* Read touch controller data */ + esp_lcd_touch_read_data(drv->user_data); + + /* Get coordinates */ + bool touchpad_pressed = esp_lcd_touch_get_coordinates(drv->user_data, touchpad_x, touchpad_y, NULL, &touchpad_cnt, 1); + + if (touchpad_pressed && touchpad_cnt > 0) { + data->point.x = touchpad_x[0]; + data->point.y = touchpad_y[0]; + data->state = LV_INDEV_STATE_PRESSED; + } else { + data->state = LV_INDEV_STATE_RELEASED; + } +} +#endif + +static void example_increase_lvgl_tick(void *arg) +{ + /* Tell LVGL how many milliseconds has elapsed */ + lv_tick_inc(EXAMPLE_LVGL_TICK_PERIOD_MS); +} + +void spi_touch_tftf_init(void) +{ + static lv_disp_draw_buf_t disp_buf; // contains internal graphic buffer(s) called draw buffer(s) + static lv_disp_drv_t disp_drv; // contains callback functions + + ESP_LOGI(TAG, "Turn off LCD backlight"); + gpio_config_t bk_gpio_config = { + .mode = GPIO_MODE_OUTPUT, + .pin_bit_mask = 1ULL << EXAMPLE_PIN_NUM_BK_LIGHT + }; + ESP_ERROR_CHECK(gpio_config(&bk_gpio_config)); + + ESP_LOGI(TAG, "Initialize SPI bus"); + spi_bus_config_t buscfg = { + .sclk_io_num = EXAMPLE_PIN_NUM_SCLK, + .mosi_io_num = EXAMPLE_PIN_NUM_MOSI, + .miso_io_num = EXAMPLE_PIN_NUM_MISO, + .quadwp_io_num = -1, + .quadhd_io_num = -1, + .max_transfer_sz = EXAMPLE_LCD_H_RES * 80 * sizeof(uint16_t), + }; + ESP_ERROR_CHECK(spi_bus_initialize(LCD_HOST, &buscfg, SPI_DMA_CH_AUTO)); + + ESP_LOGI(TAG, "Install panel IO"); + esp_lcd_panel_io_handle_t io_handle = NULL; + esp_lcd_panel_io_spi_config_t io_config = { + .dc_gpio_num = EXAMPLE_PIN_NUM_LCD_DC, + .cs_gpio_num = EXAMPLE_PIN_NUM_LCD_CS, + .pclk_hz = EXAMPLE_LCD_PIXEL_CLOCK_HZ, + .lcd_cmd_bits = EXAMPLE_LCD_CMD_BITS, + .lcd_param_bits = EXAMPLE_LCD_PARAM_BITS, + .spi_mode = 0, + .trans_queue_depth = 10, + .on_color_trans_done = example_notify_lvgl_flush_ready, + .user_ctx = &disp_drv, + }; + // Attach the LCD to the SPI bus + ESP_ERROR_CHECK(esp_lcd_new_panel_io_spi((esp_lcd_spi_bus_handle_t)LCD_HOST, &io_config, &io_handle)); + + esp_lcd_panel_handle_t panel_handle = NULL; + esp_lcd_panel_dev_config_t panel_config = { + .reset_gpio_num = EXAMPLE_PIN_NUM_LCD_RST, + .rgb_endian = LCD_RGB_ENDIAN_BGR, + .bits_per_pixel = 16, + }; +#if CONFIG_EXAMPLE_LCD_CONTROLLER_ILI9341 + ESP_LOGI(TAG, "Install ILI9341 panel driver"); + ESP_ERROR_CHECK(esp_lcd_new_panel_ili9341(io_handle, &panel_config, &panel_handle)); +#elif CONFIG_EXAMPLE_LCD_CONTROLLER_GC9A01 + ESP_LOGI(TAG, "Install GC9A01 panel driver"); + ESP_ERROR_CHECK(esp_lcd_new_panel_gc9a01(io_handle, &panel_config, &panel_handle)); +#endif + + ESP_ERROR_CHECK(esp_lcd_panel_reset(panel_handle)); + ESP_ERROR_CHECK(esp_lcd_panel_init(panel_handle)); +#if CONFIG_EXAMPLE_LCD_CONTROLLER_GC9A01 + ESP_ERROR_CHECK(esp_lcd_panel_invert_color(panel_handle, true)); +#endif + ESP_ERROR_CHECK(esp_lcd_panel_mirror(panel_handle, true, false)); + + // user can flush pre-defined pattern to the screen before we turn on the screen or backlight + ESP_ERROR_CHECK(esp_lcd_panel_disp_on_off(panel_handle, true)); + +#if CONFIG_EXAMPLE_LCD_TOUCH_ENABLED + esp_lcd_panel_io_handle_t tp_io_handle = NULL; + esp_lcd_panel_io_spi_config_t tp_io_config = ESP_LCD_TOUCH_IO_SPI_STMPE610_CONFIG(EXAMPLE_PIN_NUM_TOUCH_CS); + // Attach the TOUCH to the SPI bus + ESP_ERROR_CHECK(esp_lcd_new_panel_io_spi((esp_lcd_spi_bus_handle_t)LCD_HOST, &tp_io_config, &tp_io_handle)); + + esp_lcd_touch_config_t tp_cfg = { + .x_max = EXAMPLE_LCD_H_RES, + .y_max = EXAMPLE_LCD_V_RES, + .rst_gpio_num = -1, + .int_gpio_num = -1, + .flags = { + .swap_xy = 0, + .mirror_x = 0, + .mirror_y = 0, + }, + }; + +#if CONFIG_EXAMPLE_LCD_TOUCH_CONTROLLER_STMPE610 + ESP_LOGI(TAG, "Initialize touch controller STMPE610"); + ESP_ERROR_CHECK(esp_lcd_touch_new_spi_stmpe610(tp_io_handle, &tp_cfg, &tp)); +#endif // CONFIG_EXAMPLE_LCD_TOUCH_CONTROLLER_STMPE610 +#endif // CONFIG_EXAMPLE_LCD_TOUCH_ENABLED + + ESP_LOGI(TAG, "Turn on LCD backlight"); + gpio_set_level(EXAMPLE_PIN_NUM_BK_LIGHT, EXAMPLE_LCD_BK_LIGHT_ON_LEVEL); + + ESP_LOGI(TAG, "Initialize LVGL library"); + lv_init(); + // alloc draw buffers used by LVGL + // it's recommended to choose the size of the draw buffer(s) to be at least 1/10 screen sized + lv_color_t *buf1 = heap_caps_malloc(EXAMPLE_LCD_H_RES * 20 * sizeof(lv_color_t), MALLOC_CAP_DMA); + assert(buf1); + lv_color_t *buf2 = heap_caps_malloc(EXAMPLE_LCD_H_RES * 20 * sizeof(lv_color_t), MALLOC_CAP_DMA); + assert(buf2); + // initialize LVGL draw buffers + lv_disp_draw_buf_init(&disp_buf, buf1, buf2, EXAMPLE_LCD_H_RES * 20); + + ESP_LOGI(TAG, "Register display driver to LVGL"); + lv_disp_drv_init(&disp_drv); + disp_drv.hor_res = EXAMPLE_LCD_H_RES; + disp_drv.ver_res = EXAMPLE_LCD_V_RES; + disp_drv.flush_cb = example_lvgl_flush_cb; + disp_drv.drv_update_cb = example_lvgl_port_update_callback; + disp_drv.draw_buf = &disp_buf; + disp_drv.user_data = panel_handle; + lv_disp_t *disp = lv_disp_drv_register(&disp_drv); + + ESP_LOGI(TAG, "Install LVGL tick timer"); + // Tick interface for LVGL (using esp_timer to generate 2ms periodic event) + const esp_timer_create_args_t lvgl_tick_timer_args = { + .callback = &example_increase_lvgl_tick, + .name = "lvgl_tick" + }; + esp_timer_handle_t lvgl_tick_timer = NULL; + ESP_ERROR_CHECK(esp_timer_create(&lvgl_tick_timer_args, &lvgl_tick_timer)); + ESP_ERROR_CHECK(esp_timer_start_periodic(lvgl_tick_timer, EXAMPLE_LVGL_TICK_PERIOD_MS * 1000)); + +#if CONFIG_EXAMPLE_LCD_TOUCH_ENABLED + static lv_indev_drv_t indev_drv; // Input device driver (Touch) + lv_indev_drv_init(&indev_drv); + indev_drv.type = LV_INDEV_TYPE_POINTER; + indev_drv.disp = disp; + indev_drv.read_cb = example_lvgl_touch_cb; + indev_drv.user_data = tp; + + lv_indev_drv_register(&indev_drv); +#endif + + ESP_LOGI(TAG, "Display LVGL Meter Widget"); + example_lvgl_demo_ui(disp); + + // while (1) { + // // raise the task priority of LVGL and/or reduce the handler period can improve the performance + // vTaskDelay(pdMS_TO_TICKS(10)); + // // The task running lv_timer_handler should have lower priority than that running `lv_tick_inc` + // lv_timer_handler(); + // } +}