1. 测试了wifi的驱动,连带着蓝牙一同测试,带着蓝牙则测试不通过,需要重写蓝牙驱动代码

2. 修改wifi与蓝牙的驱动代码,并且进行了测试,测试通过
3. 新增分区表,为板载16M的flash作分区规划,预留ota分区
4. 测试了SD卡的驱动,正常读出数据,测试通过
5. 新增了CMake配置,just_monitor(monitor_only.sh),只监视,无编译,无烧录
This commit is contained in:
Misaki
2025-08-24 15:25:38 +08:00
parent fec1a52093
commit 439a379945
16 changed files with 4106 additions and 20 deletions
+1 -1
View File
@@ -1,3 +1,3 @@
### 本目录为各种外设驱动库,部分驱动库之间存在依赖关系,会在下面有所说明
#### 1.
#### 1.QMI8658依赖I2C_Driver
+184
View File
@@ -0,0 +1,184 @@
#include "SD_MMC.h"
#define EXAMPLE_MAX_CHAR_SIZE 64
static const char *SD_TAG = "SD";
uint32_t Flash_Size = 0;
uint32_t SDCard_Size = 0;
esp_err_t s_example_write_file(const char *path, char *data)
{
ESP_LOGI(SD_TAG, "Opening file %s", path);
FILE *f = fopen(path, "w");
if (f == NULL) {
ESP_LOGE(SD_TAG, "Failed to open file for writing");
return ESP_FAIL;
}
fprintf(f, data);
fclose(f);
ESP_LOGI(SD_TAG, "File written");
return ESP_OK;
}
esp_err_t s_example_read_file(const char *path)
{
ESP_LOGI(SD_TAG, "Reading file %s", path);
FILE *f = fopen(path, "r");
if (f == NULL) {
ESP_LOGE(SD_TAG, "Failed to open file for reading");
return ESP_FAIL;
}
char line[EXAMPLE_MAX_CHAR_SIZE];
fgets(line, sizeof(line), f);
fclose(f);
// strip newline
char *pos = strchr(line, '\n');
if (pos) {
*pos = '\0';
}
ESP_LOGI(SD_TAG, "Read from file: '%s'", line);
return ESP_OK;
}
void SD_Init(void)
{
esp_err_t ret;
// Options for mounting the filesystem.
// If format_if_mount_failed is set to true, SD card will be partitioned and formatted in case when mounting fails. false true
esp_vfs_fat_sdmmc_mount_config_t mount_config = {
.format_if_mount_failed = true,
.max_files = 5,
.allocation_unit_size = 16 * 1024
};
sdmmc_card_t *card;
const char mount_point[] = MOUNT_POINT;
ESP_LOGI(SD_TAG, "Initializing SD card");
// Use settings defined above to initialize SD card and mount FAT filesystem.
// Note: esp_vfs_fat_sdmmc/sdspi_mount is all-in-one convenience functions.
// Please check its source code and implement error recovery when developing production applications.
ESP_LOGI(SD_TAG, "Using SPI peripheral");
// By default, SD card frequency is initialized to SDMMC_FREQ_DEFAULT (20MHz)
// For setting a specific frequency, use host.max_freq_khz (range 400kHz - 20MHz for SDSPI)
// Example: for fixed frequency of 10MHz, use host.max_freq_khz = 10000;
sdmmc_host_t host = SDMMC_HOST_DEFAULT();
// 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.
sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
slot_config.width = 1; // 1-wire / 4-wire slot_config.width = 4;
slot_config.clk = CONFIG_EXAMPLE_PIN_CLK;
slot_config.cmd = CONFIG_EXAMPLE_PIN_CMD;
slot_config.d0 = CONFIG_EXAMPLE_PIN_D0;
slot_config.d1 = CONFIG_EXAMPLE_PIN_D1;
slot_config.d2 = CONFIG_EXAMPLE_PIN_D2;
slot_config.d3 = CONFIG_EXAMPLE_PIN_D3;
// Enable internal pullups on enabled pins. The internal pullups are insufficient however, please make sure 10k external pullups are connected on the bus. This is for debug / example purpose only.
slot_config.flags |= SDMMC_SLOT_FLAG_INTERNAL_PULLUP;
ESP_LOGI(SD_TAG, "Mounting filesystem");
ret = esp_vfs_fat_sdmmc_mount(mount_point, &host, &slot_config, &mount_config, &card);
if (ret != ESP_OK) {
if (ret == ESP_FAIL) {
ESP_LOGE(SD_TAG, "Failed to mount filesystem. "
"If you want the card to be formatted, set the CONFIG_EXAMPLE_FORMAT_IF_MOUNT_FAILED menuconfig option.");
} else {
ESP_LOGE(SD_TAG, "Failed to initialize the card (%s). "
"Make sure SD card lines have pull-up resistors in place.", esp_err_to_name(ret));
}
return;
}
ESP_LOGI(SD_TAG, "Filesystem mounted");
// Card has been initialized, print its properties
sdmmc_card_print_info(stdout, card);
SDCard_Size = ((uint64_t) card->csd.capacity) * card->csd.sector_size / (1024 * 1024);
}
void Flash_Searching(void)
{
if(esp_flash_get_physical_size(NULL, &Flash_Size) == ESP_OK)
{
Flash_Size = Flash_Size / (uint32_t)(1024 * 1024);
printf("Flash size: %ld MB\n", Flash_Size);
}
else{
printf("Get flash size failed\n");
}
}
FILE* Open_File(const char *file_path) {
ESP_LOGI(SD_TAG, "Attempting to open file: %s", file_path);
FILE *fp = fopen(file_path, "rb"); // Open the MP3 file in binary mode
if (fp == NULL) {
ESP_LOGE(SD_TAG, "Failed to open file %s. Error: %s", file_path, strerror(errno));
}
else
printf("File %s was successfully opened. \r\n", file_path);
return fp;
}
#define MAX_FILE_NAME_SIZE 100 // Define maximum file name size
#define MAX_PATH_SIZE 512 // Define a larger size for the full path
uint16_t Folder_retrieval(const char* directory, const char* fileExtension, char File_Name[][MAX_FILE_NAME_SIZE], uint16_t maxFiles)
{
DIR *dir = opendir(directory);
if (dir == NULL) {
ESP_LOGE(SD_TAG, "Path: <%s> does not exist", directory);
return 0;
}
uint16_t fileCount = 0;
struct dirent *entry;
while ((entry = readdir(dir)) != NULL && fileCount < maxFiles) {
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
continue;
}
const char *dot = strrchr(entry->d_name, '.');
if (dot != NULL && dot != entry->d_name) {
if (strcasecmp(dot, fileExtension) == 0) {
strncpy(File_Name[fileCount], entry->d_name, MAX_FILE_NAME_SIZE - 1);
File_Name[fileCount][MAX_FILE_NAME_SIZE - 1] = '\0';
char filePath[MAX_PATH_SIZE];
snprintf(filePath, MAX_PATH_SIZE, "%s/%s", directory, entry->d_name);
printf("File found: %s\r\n", filePath);
fileCount++;
}
}
else{
// printf("No extension found for file: %s\r\n", entry->d_name);
}
}
closedir(dir);
if (fileCount > 0) {
ESP_LOGI(SD_TAG, "Retrieved %d files with extension '%s'", fileCount, fileExtension);
} else {
ESP_LOGW(SD_TAG, "No files with extension '%s' found in directory: %s", fileExtension, directory);
}
return fileCount;
}
+38
View File
@@ -0,0 +1,38 @@
#pragma once
#include <string.h>
#include <sys/unistd.h>
#include <sys/stat.h>
#include "esp_vfs_fat.h"
#include "dirent.h"
#include "sdmmc_cmd.h"
#include "driver/sdmmc_host.h"
#include "esp_log.h"
#include <errno.h>
#include "esp_flash.h"
#define CONFIG_EXAMPLE_PIN_CLK 14
#define CONFIG_EXAMPLE_PIN_CMD 17
#define CONFIG_EXAMPLE_PIN_D0 16
#define CONFIG_EXAMPLE_PIN_D1 -1
#define CONFIG_EXAMPLE_PIN_D2 -1
#define CONFIG_EXAMPLE_PIN_D3 -1
#define CONFIG_SD_Card_D3 21
#define MOUNT_POINT "/sdcard"
esp_err_t SD_Card_CS_EN(void);
esp_err_t SD_Card_CS_Dis(void);
esp_err_t s_example_write_file(const char *path, char *data);
esp_err_t s_example_read_file(const char *path);
extern uint32_t SDCard_Size;
extern uint32_t Flash_Size;
void SD_Init(void);
void Flash_Searching(void);
FILE* Open_File(const char *file_path);
uint16_t Folder_retrieval(const char* directory, const char* fileExtension, char File_Name[][100],uint16_t maxFiles);
+331
View File
@@ -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);
}
}
+45
View File
@@ -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);