Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 130 additions & 15 deletions src/firmware-code/csi-receiver/main/app_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@
*/
// build test

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "nvs_flash.h"

#include "driver/uart.h"
#include "driver/gpio.h"

#include "esp_csi_gain_ctrl.h"
#include "esp_log.h"
#include "esp_mac.h"
Expand Down Expand Up @@ -60,6 +64,83 @@
static const uint8_t CONFIG_CSI_SEND_MAC[] = {0x1a, 0x00, 0x00, 0x00, 0x00, 0x00};
static const char *TAG = "csi_recv";
static int g_csi_package_count = 0;

#define EX_UART_NUM UART_NUM_1
#define UART_TXD_PIN 11
#define UART_RXD_PIN 12
#define UART_BAUD_RATE 921600
#define CSI_TO_UART_ENABLED 1







static void uart_init(void)
{
const uart_config_t uart_config = {
.baud_rate = UART_BAUD_RATE,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
.source_clk = UART_SCLK_DEFAULT,
};

ESP_ERROR_CHECK(uart_driver_install(EX_UART_NUM, 1024, 0, 0, NULL, 0));
ESP_ERROR_CHECK(uart_param_config(EX_UART_NUM, &uart_config));
ESP_ERROR_CHECK(uart_set_pin(EX_UART_NUM, UART_TXD_PIN, UART_RXD_PIN, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));
}

static void uart_send_csi_binary(uint32_t seq, int8_t *buf, uint16_t len, float compensate_gain)
{
if (len > 1024) {
len = 1024;
}

uint8_t header[10];
header[0] = 'C';
header[1] = 'S';
header[2] = 'I';
header[3] = '1';

// seq (4字节,小端)
header[4] = (uint8_t)(seq & 0xFF);
header[5] = (uint8_t)((seq >> 8) & 0xFF);
header[6] = (uint8_t)((seq >> 16) & 0xFF);
header[7] = (uint8_t)((seq >> 24) & 0xFF);

// len (2字节,小端)
header[8] = (uint8_t)(len & 0xFF);
header[9] = (uint8_t)((len >> 8) & 0xFF);

int8_t payload[1024];
for (uint16_t i = 0; i < len; ++i) {
int val = (int)(compensate_gain * buf[i]);
if (val > 127) val = 127;
if (val < -128) val = -128;
payload[i] = (int8_t)val;
}

// 校验和 (头10字节之和 + 载荷之和,低16位)
uint32_t sum = 0;
for (int i = 0; i < 10; ++i) {
sum += header[i];
}
for (uint16_t i = 0; i < len; ++i) {
sum += (uint8_t)payload[i];
}

uint8_t checksum_bytes[2];
checksum_bytes[0] = (uint8_t)(sum & 0xFF);
checksum_bytes[1] = (uint8_t)((sum >> 8) & 0xFF);

uart_write_bytes(EX_UART_NUM, (const char *)header, 10);
uart_write_bytes(EX_UART_NUM, (const char *)payload, len);
uart_write_bytes(EX_UART_NUM, (const char *)checksum_bytes, 2);
}

static void wifi_init()
{
ESP_ERROR_CHECK(esp_event_loop_create_default());
Expand Down Expand Up @@ -151,7 +232,7 @@ static void wifi_esp_now_init(esp_now_peer_info_t peer)
ESP_ERROR_CHECK(esp_now_set_peer_rate_config(peer.peer_addr, &rate_config));
}

static void wifi_csi_rx_cb(void *ctx, wifi_csi_info_t *info)
__attribute__((unused)) static void wifi_csi_rx_cb(void *ctx, wifi_csi_info_t *info)
{
if (!info || !info->buf)
{
Expand Down Expand Up @@ -186,8 +267,7 @@ static void wifi_csi_rx_cb(void *ctx, wifi_csi_info_t *info)
#endif
}
esp_csi_gain_ctrl_get_gain_compensation(&compensate_gain, agc_gain, fft_gain);
ESP_LOGI(TAG, "compensate_gain %f, agc_gain %d, fft_gain %d", compensate_gain, agc_gain,
fft_gain);
//ESP_LOGI(TAG, "compensate_gain %f, agc_gain %d, fft_gain %d", compensate_gain, agc_gain,fft_gain);
#endif

uint32_t rx_id = *(uint32_t *)(info->payload + 15);
Expand Down Expand Up @@ -220,6 +300,9 @@ static void wifi_csi_rx_cb(void *ctx, wifi_csi_info_t *info)
rx_ctrl->rx_state);

#endif

#define SEND_TO_CONSOLE 0
#if SEND_TO_CONSOLE == 1
#if (CONFIG_IDF_TARGET_ESP32C5 || CONFIG_IDF_TARGET_ESP32C61) && CSI_FORCE_LLTF
int16_t csi = ((int16_t)(((((uint16_t)info->buf[1]) << 8) | info->buf[0]) << 4) >> 4);
ets_printf(",%d,%d,\"[%d", (info->len - 2) / 2, info->first_word_invalid,
Expand All @@ -230,20 +313,39 @@ static void wifi_csi_rx_cb(void *ctx, wifi_csi_info_t *info)
ets_printf(",%d", (int16_t)(compensate_gain * csi));
}
#else
ets_printf(",%d,%d,\"[%d", info->len, info->first_word_invalid,
printf(",%d,%d,\"[%d", info->len, info->first_word_invalid,
(int16_t)(compensate_gain * info->buf[0]));

for (int i = 1; i < info->len; i++)
{
ets_printf(",%d", (int16_t)(compensate_gain * info->buf[i]));
{

printf(",%d", (int16_t)(compensate_gain * info->buf[i]));
}
#endif
ets_printf("]\"\n");
printf("]\"\n");
#endif


#if CSI_TO_UART_ENABLED
uart_send_csi_binary(rx_id, info->buf, info->len, compensate_gain);
#endif
s_count++;
}








void temp_callback(void *ctx, wifi_csi_info_t *info)
{

if (memcmp(info->mac, CONFIG_CSI_SEND_MAC, 6))
{
return;
}
const wifi_pkt_rx_ctrl_t *rx_ctrl = &info->rx_ctrl;
float compensate_gain = 1.0f;
static uint8_t agc_gain = 0;
Expand All @@ -270,20 +372,28 @@ void temp_callback(void *ctx, wifi_csi_info_t *info)
g_csi_package_count++;

#if IS_PRINT_CSI_INFO
ets_printf("index:%d len:%d data:[", g_csi_package_count, info->len);

//ESP_LOGI(TAG, "compensate_gain %f, agc_gain %d, fft_gain %d", compensate_gain,agc_gain, fft_gain);
printf("index:%d len:%d compensate_gain:%f data:[", g_csi_package_count, info->len, compensate_gain);


if (info->len > 0)
{

ets_printf("%d", (int16_t)(compensate_gain * info->buf[0]));
printf("%d", (int16_t)(compensate_gain * info->buf[0]));
// 剩下的元素前面加逗号
for (int i = 1; i < info->len; i++)
{
ets_printf(",%d", (int16_t)(compensate_gain * info->buf[i]));
{

printf(",%d", (int16_t)(compensate_gain * info->buf[i]));
}
}



ets_printf("]\n");
printf("]\n");
#endif
#if CSI_TO_UART_ENABLED
uart_send_csi_binary(g_csi_package_count, info->buf, info->len, compensate_gain);
#endif
}

Expand Down Expand Up @@ -341,6 +451,7 @@ void app_main()
/**
* @brief Initialize NVS
*/
ESP_LOGI(TAG,"d");
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND)
{
Expand All @@ -352,6 +463,10 @@ void app_main()
/**
* @brief Initialize Wi-Fi
*/
#if CSI_TO_UART_ENABLED
uart_init();
#endif

wifi_init();

/**
Expand All @@ -368,6 +483,6 @@ void app_main()
};

wifi_esp_now_init(peer);

wifi_csi_init();
wifi_csi_init();
}
6 changes: 6 additions & 0 deletions src/firmware-code/p4_remote_wifi/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# 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.16)

include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(p4_remote_wifi)
151 changes: 151 additions & 0 deletions src/firmware-code/p4_remote_wifi/dependencies.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
dependencies:
espressif/dl_fft:
component_hash: ced3cf28cc70452b7859c06f4e5059215167254a2047e34c893d6f501ccd6ea2
dependencies:
- name: idf
require: private
version: '>=5.0'
source:
registry_url: https://components.espressif.com
type: service
version: 0.4.0
espressif/eppp_link:
component_hash: 04f7f31868b8ae6014c96132740a192d09611a6e46aaa7038c865ffc7e26b38f
dependencies:
- name: espressif/esp_serial_slave_link
registry_url: https://components.espressif.com
require: private
version: ^1.1.0
- name: idf
require: private
version: '>=5.2'
source:
registry_url: https://components.espressif.com
type: service
version: 1.1.5
espressif/esp-dl:
component_hash: 3720071be45539d7a5d729024d471a2959b473149161b062ba11af384e46f078
dependencies:
- name: espressif/dl_fft
registry_url: https://components.espressif.com
require: private
version: '>=0.3.1'
- name: espressif/esp-dsp
registry_url: https://components.espressif.com
require: private
version: ==1.8.0
- name: espressif/esp_new_jpeg
registry_url: https://components.espressif.com
require: private
version: ^1
- name: idf
require: private
version: '*'
source:
registry_url: https://components.espressif.com/
type: service
targets:
- esp32
- esp32c2
- esp32c3
- esp32c5
- esp32c6
- esp32p4
- esp32s2
- esp32s3
- esp32s31
version: 3.3.5
espressif/esp-dsp:
component_hash: 12e44db246517a627bc0fe2b255b8335410c0215c98bfba2df5a8e3edea839ef
dependencies:
- name: idf
require: private
version: '>=4.2'
source:
registry_url: https://components.espressif.com
type: service
version: 1.8.0
espressif/esp_hosted:
component_hash: 037d55a70dc5e2699754c0f3e21427d0df4ff29dc89cc80746978ad5b9dc4589
dependencies:
- name: idf
require: private
version: '>=5.3'
source:
registry_url: https://components.espressif.com
type: service
version: 2.12.8
espressif/esp_new_jpeg:
component_hash: e1e7e6991193c41ed6c9fc386702aa7821433319e7aa9c3147cd3236310cd69c
dependencies: []
source:
registry_url: https://components.espressif.com
type: service
targets:
- esp32
- esp32s2
- esp32s3
- esp32s31
- esp32p4
- esp32c2
- esp32c3
- esp32c5
- esp32c6
- esp32c61
- esp32h4
version: 1.0.2
espressif/esp_serial_slave_link:
component_hash: ac1776806de0a6e371c84e87898bb983e19ce62aa7f1e2e5c4a3b0234a575d2c
dependencies:
- name: idf
require: private
version: '>=5.0'
source:
registry_url: https://components.espressif.com
type: service
version: 1.1.2
espressif/esp_wifi_remote:
component_hash: 0b57d4ca039bbda1d4eb794f994f8d59547cdbcfd7962227890cd3a0979e6fa8
dependencies:
- name: espressif/esp_hosted
registry_url: https://components.espressif.com
require: private
rules:
- if: target in [esp32h2, esp32p4]
version: '>=2.11'
- name: espressif/wifi_remote_over_eppp
registry_url: https://components.espressif.com
require: private
version: '>=0.1'
- name: idf
require: private
version: '>=5.3'
source:
registry_url: https://components.espressif.com/
type: service
version: 1.6.0
espressif/wifi_remote_over_eppp:
component_hash: 06d8bc15348bbd21f1390daace36ed5adcf6026c93c5b69decc55fa04df2711a
dependencies:
- name: idf
require: private
version: '>=5.3'
- name: espressif/eppp_link
registry_url: https://components.espressif.com
require: private
version: '>=0.1'
source:
registry_url: https://components.espressif.com
type: service
version: 0.3.2
idf:
source:
type: idf
version: 5.5.3
direct_dependencies:
- espressif/esp-dl
- espressif/esp_wifi_remote
- idf
manifest_hash: e918910e096e1eb8450f6ab9e5a645197442368607aeb640dc34b0271f59eee5
target: esp32p4
version: 2.0.0
2 changes: 2 additions & 0 deletions src/firmware-code/p4_remote_wifi/main/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
idf_component_register(SRCS "main.c" "wifi_manager.c"
INCLUDE_DIRS ".")
Loading
Loading