From d7e8537dfa714c4f90edb575762b2fb0cb0f8faf Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Thu, 11 Mar 2021 13:42:24 +0100 Subject: [PATCH 1/2] examples/simple_sniffer: only enable apptrace if JTAG is selected Fixes compilation of this example on esp32c3, where apptrace is not yet supported. --- examples/wifi/simple_sniffer/main/Kconfig.projbuild | 1 + examples/wifi/simple_sniffer/sdkconfig.defaults | 4 ---- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/examples/wifi/simple_sniffer/main/Kconfig.projbuild b/examples/wifi/simple_sniffer/main/Kconfig.projbuild index 1de70f1875..0881189a4a 100644 --- a/examples/wifi/simple_sniffer/main/Kconfig.projbuild +++ b/examples/wifi/simple_sniffer/main/Kconfig.projbuild @@ -20,6 +20,7 @@ menu "Example Configuration" Store pcap file to SD card. config SNIFFER_PCAP_DESTINATION_JTAG bool "JTAG (App Trace)" + select APPTRACE_DEST_TRAX help Store pcap file to host via JTAG interface. endchoice diff --git a/examples/wifi/simple_sniffer/sdkconfig.defaults b/examples/wifi/simple_sniffer/sdkconfig.defaults index 8f75f72113..ee62d2d43d 100644 --- a/examples/wifi/simple_sniffer/sdkconfig.defaults +++ b/examples/wifi/simple_sniffer/sdkconfig.defaults @@ -19,7 +19,3 @@ CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y # FatFS CONFIG_FATFS_LFN_HEAP=y CONFIG_FATFS_MAX_LFN=31 - -# App trace -CONFIG_APPTRACE_DEST_TRAX=y -CONFIG_APPTRACE_ENABLE=y From e8ad727b80ab2f845fb4d4fc3282e6c18dae8730 Mon Sep 17 00:00:00 2001 From: laokaiyao Date: Wed, 21 Apr 2021 14:31:42 +0800 Subject: [PATCH 2/2] examples/simple_sniffer: add SPI work mode to SD card to support all chips --- examples/wifi/simple_sniffer/README.md | 15 ++--- .../simple_sniffer/main/Kconfig.projbuild | 22 +++++-- .../main/simple_sniffer_example_main.c | 65 ++++++++++++++++--- 3 files changed, 81 insertions(+), 21 deletions(-) diff --git a/examples/wifi/simple_sniffer/README.md b/examples/wifi/simple_sniffer/README.md index 241aec57fe..14fdf64041 100644 --- a/examples/wifi/simple_sniffer/README.md +++ b/examples/wifi/simple_sniffer/README.md @@ -1,6 +1,3 @@ -| Supported Targets | ESP32 | ESP32-S2 | -| ----------------- | ----- | ------- | - # Simple Sniffer Example (See the README.md file in the upper level 'examples' directory for more information about examples.) @@ -26,8 +23,8 @@ Open the project configuration menu (`idf.py menuconfig`). Then go into `Example - Check `Store command history in flash` if you want to save command history into flash (recommend). - Select where to save the pcap file in `Select destination to store pcap file` menu item. - - `SD Card` means saving packets (pcap format) into the SD card you plug in. - - `JTAG (App Trace)` means sending packets (pcap format) to host via JTAG interface. This feature depends on [app trace component](https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/app_trace.html). + - `SD Card` means saving packets (pcap format) into the SD card you plug in. The default SD card work mode is set to SDMMC for target ESP32 and ESP32S3, but SPI is the only choice for other targets. + - `JTAG (App Trace)` means sending packets (pcap format) to host via JTAG interface. This feature depends on [app trace component](https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/app_trace.html), Component config -> Application Lelvel Tracing -> Data Destination -> Trace memory should be enabled to choose `JTAG (App Trace)` as destination. - Set the mount point in your filesystem in `SD card mount point in the filesystem` menu item. This configuration only takes effect when you choose to save packets into SD card. - Set max name length of pcap file in `Max name length of pcap file` menu item. - Set the length of sniffer work queue in `Length of sniffer work queue` menu item. @@ -85,7 +82,7 @@ The `sniffer` command support some important options as follow: | | ======================================================= -esp32> mount sd +sniffer> mount sd I (158912) example: Initializing SD card I (158912) example: Using SDMMC peripheral I (158912) gpio: GPIO[13]| InputEn: 0| OutputEn: 1| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0 @@ -98,13 +95,13 @@ Size: 14832MB ### Start Sniffer ```bash -esp32> sniffer -f sniffer-example -i wlan -c 2 +sniffer> sniffer -f sniffer-example -i wlan -c 2 I (8946) cmd_sniffer: open file successfully W (8966) phy_init: failed to load RF calibration data (0x1102), falling back to full calibration I (9176) phy: phy_version: 4100, 6fa5e27, Jan 25 2019, 17:02:06, 0, 2 I (9186) wifi: ic_enable_sniffer I (9196) cmd_sniffer: start WiFi promiscuous ok -esp32> sniffer --stop +sniffer> sniffer --stop I (31456) wifi: ic_disable_sniffer I (31456) wifi: flush txq I (31456) wifi: stop sw txq @@ -115,7 +112,7 @@ I (31456) cmd_sniffer: stop WiFi promiscuous ok ### Unmount SD Card ```bash -esp32> unmount sd +sniffer> unmount sd I (248800) example: Card unmounted ``` diff --git a/examples/wifi/simple_sniffer/main/Kconfig.projbuild b/examples/wifi/simple_sniffer/main/Kconfig.projbuild index 0881189a4a..c60fcddeb4 100644 --- a/examples/wifi/simple_sniffer/main/Kconfig.projbuild +++ b/examples/wifi/simple_sniffer/main/Kconfig.projbuild @@ -9,23 +9,37 @@ menu "Example Configuration" choice SNIFFER_PCAP_DESTINATION prompt "Select destination to store pcap file" - default SNIFFER_PCAP_DESTINATION_SD if IDF_TARGET_ESP32 - default SNIFFER_PCAP_DESTINATION_JTAG if IDF_TARGET_ESP32S2 + default SNIFFER_PCAP_DESTINATION_SD help Select where to store the pcap file. - Currently support storing files to SD card or to host via JTAG interface. + Currently support storing files to SD card or to host via JTAG interface with 'Trace memory' enabled. config SNIFFER_PCAP_DESTINATION_SD bool "SD Card" help Store pcap file to SD card. config SNIFFER_PCAP_DESTINATION_JTAG bool "JTAG (App Trace)" - select APPTRACE_DEST_TRAX + depends on APPTRACE_DEST_TRAX help Store pcap file to host via JTAG interface. endchoice if SNIFFER_PCAP_DESTINATION_SD + choice SNIFFER_SD_MODE + prompt "Select SD card work mode" + default SNIFFER_SD_SDMMC_MODE + help + Select whitch peripheral SD card should use. + config SNIFFER_SD_SDMMC_MODE + bool "SDMMC" + depends on IDF_TARGET_ESP32 || IDF_TARGET_ESP32S3 + help + Use SDMMC mode (Not support on esp32c3). + config SNIFFER_SD_SPI_MODE + bool "SPI" + help + Use SPI mode. + endchoice config SNIFFER_MOUNT_POINT string "SD card mount point in the filesystem" default "/sdcard" diff --git a/examples/wifi/simple_sniffer/main/simple_sniffer_example_main.c b/examples/wifi/simple_sniffer/main/simple_sniffer_example_main.c index 4117bcfa96..1012894e2c 100644 --- a/examples/wifi/simple_sniffer/main/simple_sniffer_example_main.c +++ b/examples/wifi/simple_sniffer/main/simple_sniffer_example_main.c @@ -22,6 +22,7 @@ #if CONFIG_SNIFFER_PCAP_DESTINATION_SD #include "driver/sdmmc_host.h" #include "driver/sdspi_host.h" +#include "driver/spi_common.h" #endif #include "nvs_flash.h" #include "sdmmc_cmd.h" @@ -33,6 +34,23 @@ #define HISTORY_FILE_PATH HISTORY_MOUNT_POINT "/history.txt" #endif +#if CONFIG_SNIFFER_SD_SPI_MODE + +#if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2 +#define PIN_NUM_MISO 2 +#define PIN_NUM_MOSI 15 +#define PIN_NUM_CLK 14 +#define PIN_NUM_CS 13 + +#elif CONFIG_IDF_TARGET_ESP32C3 +#define PIN_NUM_MISO 18 +#define PIN_NUM_MOSI 9 +#define PIN_NUM_CLK 8 +#define PIN_NUM_CS 19 +#endif //CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2 + +#endif // CONFIG_SNIFFER_SD_SPI_MODE + static const char *TAG = "example"; #if CONFIG_SNIFFER_STORE_HISTORY @@ -82,6 +100,8 @@ static struct { /** 'mount' command */ static int mount(int argc, char **argv) { + esp_err_t ret; + int nerrors = arg_parse(argc, argv, (void **)&mount_args); if (nerrors != 0) { arg_print_errors(stderr, mount_args.end, argv[0]); @@ -90,6 +110,41 @@ static int mount(int argc, char **argv) /* mount sd card */ if (!strncmp(mount_args.device->sval[0], "sd", 2)) { ESP_LOGI(TAG, "Initializing SD card"); + esp_vfs_fat_sdmmc_mount_config_t mount_config = { + .format_if_mount_failed = true, + .max_files = 4, + .allocation_unit_size = 16 * 1024 + }; + + // initialize SD card and mount FAT filesystem. + sdmmc_card_t *card; + +#if CONFIG_SNIFFER_SD_SPI_MODE + ESP_LOGI(TAG, "Using SPI peripheral"); + sdmmc_host_t host = SDSPI_HOST_DEFAULT(); + spi_bus_config_t bus_cfg = { + .mosi_io_num = PIN_NUM_MOSI, + .miso_io_num = PIN_NUM_MISO, + .sclk_io_num = PIN_NUM_CLK, + .quadwp_io_num = -1, + .quadhd_io_num = -1, + .max_transfer_sz = 4000, + }; + ret = spi_bus_initialize(host.slot, &bus_cfg, SPI_DMA_CH_AUTO); + if (ret != ESP_OK) { + ESP_LOGE(TAG, "Failed to initialize bus."); + return 1; + } + + // This initializes the slot without card detect (CD) and write protect (WP) signals. + // Modify slot_config.gpio_cd and slot_config.gpio_wp if your board has these signals. + sdspi_device_config_t slot_config = SDSPI_DEVICE_CONFIG_DEFAULT(); + slot_config.gpio_cs = PIN_NUM_CS; + slot_config.host_id = host.slot; + + ret = esp_vfs_fat_sdspi_mount(CONFIG_SNIFFER_MOUNT_POINT, &host, &slot_config, &mount_config, &card); + +#else ESP_LOGI(TAG, "Using SDMMC peripheral"); sdmmc_host_t host = SDMMC_HOST_DEFAULT(); sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT(); @@ -100,15 +155,9 @@ static int mount(int argc, char **argv) gpio_set_pull_mode(12, GPIO_PULLUP_ONLY); // D2, needed in 4-line mode only gpio_set_pull_mode(13, GPIO_PULLUP_ONLY); // D3, needed in 4- and 1-line modes - esp_vfs_fat_sdmmc_mount_config_t mount_config = { - .format_if_mount_failed = true, - .max_files = 4, - .allocation_unit_size = 16 * 1024 - }; + ret = esp_vfs_fat_sdmmc_mount(CONFIG_SNIFFER_MOUNT_POINT, &host, &slot_config, &mount_config, &card); +#endif - // initialize SD card and mount FAT filesystem. - sdmmc_card_t *card; - esp_err_t ret = esp_vfs_fat_sdmmc_mount(CONFIG_SNIFFER_MOUNT_POINT, &host, &slot_config, &mount_config, &card); if (ret != ESP_OK) { if (ret == ESP_FAIL) { ESP_LOGE(TAG, "Failed to mount filesystem. "