1. 测试了wifi的驱动,连带着蓝牙一同测试,带着蓝牙则测试不通过,需要重写蓝牙驱动代码
2. 修改wifi与蓝牙的驱动代码,并且进行了测试,测试通过 3. 新增分区表,为板载16M的flash作分区规划,预留ota分区 4. 测试了SD卡的驱动,正常读出数据,测试通过 5. 新增了CMake配置,just_monitor(monitor_only.sh),只监视,无编译,无烧录
This commit is contained in:
@@ -0,0 +1,331 @@
|
||||
#include "Wireless.h"
|
||||
|
||||
uint16_t BLE_NUM = 0;
|
||||
uint16_t WIFI_NUM = 0;
|
||||
bool Scan_finish = 0;
|
||||
|
||||
bool WiFi_Scan_Finish = 0;
|
||||
bool BLE_Scan_Finish = 0;
|
||||
|
||||
wifi_ap_info_t *wifi_ap_list = NULL;
|
||||
uint16_t wifi_ap_count = 0;
|
||||
ble_device_info_t *ble_device_list = NULL;
|
||||
|
||||
void Wireless_Init(void)
|
||||
{
|
||||
// Initialize NVS.
|
||||
esp_err_t 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 );
|
||||
// WiFi
|
||||
xTaskCreatePinnedToCore(
|
||||
WIFI_Init,
|
||||
"WIFI task",
|
||||
4096,
|
||||
NULL,
|
||||
1,
|
||||
NULL,
|
||||
0);
|
||||
// BLE
|
||||
xTaskCreatePinnedToCore(
|
||||
BLE_Init,
|
||||
"BLE task",
|
||||
4096,
|
||||
NULL,
|
||||
2,
|
||||
NULL,
|
||||
0);
|
||||
}
|
||||
|
||||
void WIFI_Init(void *arg)
|
||||
{
|
||||
esp_netif_init();
|
||||
esp_event_loop_create_default();
|
||||
esp_netif_create_default_wifi_sta();
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
esp_wifi_init(&cfg);
|
||||
esp_wifi_set_mode(WIFI_MODE_STA);
|
||||
esp_wifi_start();
|
||||
|
||||
WIFI_NUM = WIFI_Scan();
|
||||
printf("WIFI:%d\r\n",WIFI_NUM);
|
||||
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
|
||||
uint16_t WIFI_Scan(void)
|
||||
{
|
||||
uint16_t ap_count = 0;
|
||||
esp_wifi_scan_start(NULL, true); // 开始扫描
|
||||
ESP_ERROR_CHECK(esp_wifi_scan_get_ap_num(&ap_count));
|
||||
|
||||
// 分配内存保存AP信息
|
||||
if (wifi_ap_list) { // 如果已有内存,则释放
|
||||
free(wifi_ap_list);
|
||||
}
|
||||
wifi_ap_list = malloc(ap_count * sizeof(wifi_ap_info_t));
|
||||
if (!wifi_ap_list) {
|
||||
printf("Failed to allocate memory for WiFi AP list\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 获取AP列表
|
||||
wifi_ap_record_t *ap_records = malloc(ap_count * sizeof(wifi_ap_record_t));
|
||||
if (!ap_records) {
|
||||
printf("Failed to allocate memory for AP records\n");
|
||||
free(wifi_ap_list); // 释放AP列表内存
|
||||
return 0;
|
||||
}
|
||||
|
||||
ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&ap_count, ap_records));
|
||||
|
||||
// 保存AP信息
|
||||
wifi_ap_count = ap_count;
|
||||
for (int i = 0; i < ap_count; i++) {
|
||||
memset(wifi_ap_list[i].ssid, 0, sizeof(wifi_ap_list[i].ssid));
|
||||
strncpy(wifi_ap_list[i].ssid, (char*)ap_records[i].ssid, sizeof(wifi_ap_list[i].ssid) - 1);
|
||||
memcpy(wifi_ap_list[i].bssid, ap_records[i].bssid, 6);
|
||||
wifi_ap_list[i].rssi = ap_records[i].rssi;
|
||||
wifi_ap_list[i].authmode = ap_records[i].authmode;
|
||||
}
|
||||
|
||||
free(ap_records);
|
||||
esp_wifi_scan_stop();
|
||||
|
||||
WiFi_Scan_Finish = 1;
|
||||
if (BLE_Scan_Finish == 1 || WiFi_Scan_Finish == 1) {
|
||||
Scan_finish = 1;
|
||||
}
|
||||
|
||||
return ap_count;
|
||||
}
|
||||
|
||||
|
||||
#define GATTC_TAG "GATTC_TAG"
|
||||
#define SCAN_DURATION 5
|
||||
#define MAX_DISCOVERED_DEVICES 100
|
||||
|
||||
typedef struct {
|
||||
uint8_t address[6];
|
||||
bool is_valid;
|
||||
} discovered_device_t;
|
||||
|
||||
static discovered_device_t discovered_devices[MAX_DISCOVERED_DEVICES];
|
||||
static size_t num_discovered_devices = 0;
|
||||
static size_t num_devices_with_name = 0;
|
||||
|
||||
static bool is_device_discovered(const uint8_t *addr) {
|
||||
for (size_t i = 0; i < num_discovered_devices; i++) {
|
||||
if (memcmp(discovered_devices[i].address, addr, 6) == 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static void add_device_to_list(const uint8_t *addr) {
|
||||
if (num_discovered_devices < MAX_DISCOVERED_DEVICES) {
|
||||
memcpy(discovered_devices[num_discovered_devices].address, addr, 6);
|
||||
discovered_devices[num_discovered_devices].is_valid = true;
|
||||
num_discovered_devices++;
|
||||
}
|
||||
}
|
||||
|
||||
static bool extract_device_name(const uint8_t *adv_data, uint8_t adv_data_len, char *device_name, size_t max_name_len) {
|
||||
size_t offset = 0;
|
||||
while (offset < adv_data_len) {
|
||||
if (adv_data[offset] == 0) break;
|
||||
|
||||
uint8_t length = adv_data[offset];
|
||||
if (length == 0 || offset + length > adv_data_len) break;
|
||||
|
||||
uint8_t type = adv_data[offset + 1];
|
||||
if (type == ESP_BLE_AD_TYPE_NAME_CMPL || type == ESP_BLE_AD_TYPE_NAME_SHORT) {
|
||||
if (length > 1 && length - 1 < max_name_len) {
|
||||
memcpy(device_name, &adv_data[offset + 2], length - 1);
|
||||
device_name[length - 1] = '\0';
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
offset += length + 1;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static void esp_gap_cb(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) {
|
||||
static char device_name[100];
|
||||
|
||||
switch (event) {
|
||||
case ESP_GAP_BLE_SCAN_RESULT_EVT:
|
||||
if (param->scan_rst.search_evt == ESP_GAP_SEARCH_INQ_RES_EVT) {
|
||||
if (!is_device_discovered(param->scan_rst.bda)) {
|
||||
add_device_to_list(param->scan_rst.bda);
|
||||
BLE_NUM++;
|
||||
|
||||
// 分配内存保存BLE设备信息
|
||||
if (!ble_device_list) {
|
||||
ble_device_list = malloc(MAX_DISCOVERED_DEVICES * sizeof(ble_device_info_t));
|
||||
if (!ble_device_list) {
|
||||
printf("Failed to allocate memory for BLE device list\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// 保存设备信息
|
||||
int idx = BLE_NUM - 1;
|
||||
if (idx < MAX_DISCOVERED_DEVICES) {
|
||||
memset(ble_device_list[idx].name, 0, sizeof(ble_device_list[idx].name));
|
||||
|
||||
if (extract_device_name(param->scan_rst.ble_adv,
|
||||
param->scan_rst.adv_data_len,
|
||||
device_name,
|
||||
sizeof(device_name))) {
|
||||
strncpy(ble_device_list[idx].name, device_name, sizeof(ble_device_list[idx].name) - 1);
|
||||
num_devices_with_name++;
|
||||
} else {
|
||||
strncpy(ble_device_list[idx].name, "Unknown", sizeof(ble_device_list[idx].name) - 1);
|
||||
}
|
||||
|
||||
memcpy(ble_device_list[idx].address, param->scan_rst.bda, 6);
|
||||
ble_device_list[idx].rssi = param->scan_rst.rssi;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case ESP_GAP_BLE_SCAN_STOP_COMPLETE_EVT:
|
||||
ESP_LOGI(GATTC_TAG, "Scan complete. Total devices found: %d (with names: %d)", BLE_NUM, num_devices_with_name);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void BLE_Init(void *arg)
|
||||
{
|
||||
ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT));
|
||||
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
|
||||
esp_err_t ret = esp_bt_controller_init(&bt_cfg);
|
||||
if (ret) {
|
||||
printf("%s initialize controller failed: %s\n", __func__, esp_err_to_name(ret));
|
||||
return;}
|
||||
ret = esp_bt_controller_enable(ESP_BT_MODE_BLE);
|
||||
if (ret) {
|
||||
printf("%s enable controller failed: %s\n", __func__, esp_err_to_name(ret));
|
||||
return;}
|
||||
ret = esp_bluedroid_init();
|
||||
if (ret) {
|
||||
printf("%s init bluetooth failed: %s\n", __func__, esp_err_to_name(ret));
|
||||
return;}
|
||||
ret = esp_bluedroid_enable();
|
||||
if (ret) {
|
||||
printf("%s enable bluetooth failed: %s\n", __func__, esp_err_to_name(ret));
|
||||
return;}
|
||||
|
||||
//register the callback function to the gap module
|
||||
ret = esp_ble_gap_register_callback(esp_gap_cb);
|
||||
if (ret){
|
||||
printf("%s gap register error, error code = %x\n", __func__, ret);
|
||||
return;
|
||||
}
|
||||
BLE_Scan();
|
||||
// while(1)
|
||||
// {
|
||||
// vTaskDelay(pdMS_TO_TICKS(150));
|
||||
// }
|
||||
|
||||
vTaskDelete(NULL);
|
||||
|
||||
}
|
||||
uint16_t BLE_Scan(void)
|
||||
{
|
||||
esp_ble_scan_params_t scan_params = {
|
||||
.scan_type = BLE_SCAN_TYPE_ACTIVE,
|
||||
.own_addr_type = BLE_ADDR_TYPE_RPA_PUBLIC,
|
||||
.scan_filter_policy = BLE_SCAN_FILTER_ALLOW_ALL,
|
||||
.scan_interval = 0x50,
|
||||
.scan_window = 0x30,
|
||||
.scan_duplicate = BLE_SCAN_DUPLICATE_DISABLE
|
||||
};
|
||||
ESP_ERROR_CHECK(esp_ble_gap_set_scan_params(&scan_params));
|
||||
|
||||
printf("Starting BLE scan...\n");
|
||||
ESP_ERROR_CHECK(esp_ble_gap_start_scanning(SCAN_DURATION));
|
||||
|
||||
// Set scanning duration
|
||||
vTaskDelay(SCAN_DURATION * 1000 / portTICK_PERIOD_MS);
|
||||
|
||||
printf("Stopping BLE scan...\n");
|
||||
// ESP_ERROR_CHECK(esp_ble_gap_stop_scanning());
|
||||
ESP_ERROR_CHECK(esp_ble_dtm_stop());
|
||||
BLE_Scan_Finish = 1;
|
||||
if(WiFi_Scan_Finish == 1)
|
||||
Scan_finish = 1;
|
||||
return BLE_NUM;
|
||||
}
|
||||
|
||||
// 获取WiFi AP列表
|
||||
uint16_t wireless_get_wifi_ap_list(wifi_ap_info_t *ap_list, uint16_t max_aps) {
|
||||
if (!wifi_ap_list || wifi_ap_count == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint16_t copy_count = (wifi_ap_count < max_aps) ? wifi_ap_count : max_aps;
|
||||
memcpy(ap_list, wifi_ap_list, copy_count * sizeof(wifi_ap_info_t));
|
||||
|
||||
return copy_count;
|
||||
}
|
||||
|
||||
// 获取BLE设备列表
|
||||
uint16_t wireless_get_ble_device_list(ble_device_info_t *device_list, uint16_t max_devices) {
|
||||
if (!ble_device_list || BLE_NUM == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint16_t copy_count = (BLE_NUM < max_devices) ? BLE_NUM : max_devices;
|
||||
memcpy(device_list, ble_device_list, copy_count * sizeof(ble_device_info_t));
|
||||
|
||||
return copy_count;
|
||||
}
|
||||
|
||||
// 打印所有WiFi AP信息
|
||||
void wireless_print_wifi_aps(void) {
|
||||
if (!wifi_ap_list || wifi_ap_count == 0) {
|
||||
printf("No WiFi APs found or scan not completed\n");
|
||||
return;
|
||||
}
|
||||
|
||||
printf("Found %d WiFi APs:\n", wifi_ap_count);
|
||||
for (int i = 0; i < wifi_ap_count; i++) {
|
||||
printf(" %d: SSID: %s, BSSID: %02X:%02X:%02X:%02X:%02X:%02X, RSSI: %d, Auth: %d\n",
|
||||
i + 1,
|
||||
wifi_ap_list[i].ssid,
|
||||
wifi_ap_list[i].bssid[0], wifi_ap_list[i].bssid[1], wifi_ap_list[i].bssid[2],
|
||||
wifi_ap_list[i].bssid[3], wifi_ap_list[i].bssid[4], wifi_ap_list[i].bssid[5],
|
||||
wifi_ap_list[i].rssi,
|
||||
wifi_ap_list[i].authmode);
|
||||
}
|
||||
}
|
||||
|
||||
// 打印所有BLE设备信息
|
||||
void wireless_print_ble_devices(void) {
|
||||
if (!ble_device_list || BLE_NUM == 0) {
|
||||
printf("No BLE devices found or scan not completed\n");
|
||||
return;
|
||||
}
|
||||
|
||||
printf("Found %d BLE devices:\n", BLE_NUM);
|
||||
for (int i = 0; i < BLE_NUM; i++) {
|
||||
printf(" %d: Name: %s, Address: %02X:%02X:%02X:%02X:%02X:%02X, RSSI: %d\n",
|
||||
i + 1,
|
||||
ble_device_list[i].name,
|
||||
ble_device_list[i].address[0], ble_device_list[i].address[1], ble_device_list[i].address[2],
|
||||
ble_device_list[i].address[3], ble_device_list[i].address[4], ble_device_list[i].address[5],
|
||||
ble_device_list[i].rssi);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
#pragma once
|
||||
|
||||
#include "esp_log.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "esp_wifi.h"
|
||||
#include "nvs_flash.h"
|
||||
#include "esp_log.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h> // For memcpy
|
||||
#include "esp_system.h"
|
||||
#include "esp_bt.h"
|
||||
#include "esp_gap_ble_api.h"
|
||||
#include "esp_bt_main.h"
|
||||
|
||||
// WiFi AP信息结构体
|
||||
typedef struct {
|
||||
char ssid[33]; // SSID名称 (最大32字符 + 1个空字符)
|
||||
uint8_t bssid[6]; // MAC地址
|
||||
int8_t rssi; // 信号强度
|
||||
wifi_auth_mode_t authmode; // 认证模式
|
||||
} wifi_ap_info_t;
|
||||
|
||||
// BLE设备信息结构体
|
||||
typedef struct {
|
||||
char name[100]; // 设备名称
|
||||
uint8_t address[6]; // MAC地址
|
||||
int8_t rssi; // 信号强度
|
||||
} ble_device_info_t;
|
||||
|
||||
extern uint16_t BLE_NUM;
|
||||
extern uint16_t WIFI_NUM;
|
||||
extern bool Scan_finish;
|
||||
|
||||
void Wireless_Init(void);
|
||||
void WIFI_Init(void *arg);
|
||||
uint16_t WIFI_Scan(void);
|
||||
void BLE_Init(void *arg);
|
||||
uint16_t BLE_Scan(void);
|
||||
|
||||
// 新增接口函数
|
||||
uint16_t wireless_get_wifi_ap_list(wifi_ap_info_t *ap_list, uint16_t max_aps);
|
||||
uint16_t wireless_get_ble_device_list(ble_device_info_t *device_list, uint16_t max_devices);
|
||||
void wireless_print_wifi_aps(void);
|
||||
void wireless_print_ble_devices(void);
|
||||
Reference in New Issue
Block a user