This commit is contained in:
lmx
2025-11-26 11:25:20 +08:00
parent 8089cf5b3a
commit 6b8d9ea6a0
27 changed files with 151096 additions and 150478 deletions

View File

@ -70,6 +70,10 @@
#include "bt_background.h"
#include "default_event_handler.h"
#if (JL_EARPHONE_APP_EN)
#include "rcsp_adv_user_update.h"
#endif
#define xlog(format, ...) printf("[%s] " format, __func__, ##__VA_ARGS__)
@ -2471,6 +2475,19 @@ static int event_handler(struct application *app, struct sys_event *event)
} else if ((u32)event->arg == DEVICE_EVENT_FROM_POWER) {
return app_power_event_handler(&event->u.dev);
}
#if (JL_EARPHONE_APP_EN)
else if ((u32)event->arg == DEVICE_EVENT_FROM_RCSP) {
log_info("DEVICE_EVENT_FROM_RCSP: %d", event->u.rcsp.event);
switch (event->u.rcsp.event) {
case MSG_JL_UPDATE_START:
log_info(">>> EARPHONE APP: MSG_JL_UPDATE_START\n");
// You can add UI notifications here, like LED blinking or a tone.
break;
default:
break;
}
}
#endif
#if TCFG_CHARGESTORE_ENABLE
else if ((u32)event->arg == DEVICE_EVENT_CHARGE_STORE) {
app_chargestore_event_handler(&event->u.chargestore);
@ -2790,3 +2807,4 @@ static int state_machine(struct application *app, enum app_state state, struct i
// .ops = &app_earphone_ops,
// .state = APP_STA_DESTROY,
// };

View File

@ -286,9 +286,11 @@ void rfid_task_fuc(void)
xlog("FM176XX HardReset SUCCESS\r\n");
}
rfid_delay_ms(10); // 复位后延时
}
gpio_direction_output(IO_PORTA_05,0);
// 3. 读取芯片版本号,确认通信是否正常
GetReg(REG_VERSION, &reg_data);
@ -298,5 +300,4 @@ void rfid_task_fuc(void)
// TYPE_B_EVENT();
TYPE_V_EVENT();
// TYPE_F_EVENT();
gpio_direction_output(IO_PORTA_05,1);
}

View File

@ -107,16 +107,19 @@ unsigned char GetReg(unsigned char address, unsigned char *reg_data){
addr_byte = (address << 1) | 0x01;
// ---- 开始SPI事务 ----
// gpio_direction_output(IO_PORTA_05,0);
gpio_set_output_value(IO_PORTA_05,0);
// 1. 发送地址字节,忽略接收到的数据
spi_send_byte(SPI1, addr_byte);
spi_send_byte(SPI1, addr_byte);
asm("nop");
// 2. 接收数据字节 (通过发送一个Dummy Byte 0xFF 来产生时钟)
*reg_data = spi_recv_byte(SPI1, &err);
asm("nop");
// ---- 结束SPI事务 ----
// gpio_direction_output(IO_PORTA_05,1);
gpio_set_output_value(IO_PORTA_05,1);
os_time_dly(1);
if (err != 0) {
xlog("GetReg error\n");
@ -139,12 +142,16 @@ unsigned char SetReg(unsigned char address, unsigned char reg_data){
unsigned char addr_byte = (address << 1) & 0xFE; // Bit0=0 for write
int err1, err2;
// gpio_direction_output(IO_PORTA_05,0); // <<-- CS拉低开始事务
gpio_set_output_value(IO_PORTA_05,0); // <<-- CS拉低开始事务
err1 = spi_send_byte(SPI1, addr_byte); // 发送地址
asm("nop");
err2 = spi_send_byte(SPI1, reg_data); // 发送数据
asm("nop");
// gpio_direction_output(IO_PORTA_05,1); // <<-- CS拉高结束事务
gpio_set_output_value(IO_PORTA_05,1); // <<-- CS拉高结束事务
os_time_dly(1);
if (err1 != 0 || err2 != 0) {
return FAIL;

View File

@ -0,0 +1,113 @@
#include "system/includes.h"
#include "system/syscfg_id.h"
#include "nvs.h"
// 2. 定义一个唯一的配置项ID (必须在1-49之间)
#define CFG_FACTORY_INFO_ID 10
/**
* @brief 将出厂信息写入Flash
*
* @param info 指向要写入的出厂信息结构体的指针
* @return 实际写入的长度, <0: 失败
*/
int nvs_write_factory_info(const factory_info_t *info)
{
if (!info) {
return -1;
}
printf("--> Writing factory info to flash...\n");
int ret = syscfg_write(CFG_FACTORY_INFO_ID, (void*)info, sizeof(factory_info_t));
if (ret != sizeof(factory_info_t)) {
printf("!!! syscfg_write factory info failed, ret = %d\n", ret);
} else {
printf("--> syscfg_write factory info success.\n");
}
return ret;
}
/**
* @brief 从Flash读取出厂信息
*
* @param info 指向用于存储读取数据的出厂信息结构体的指针
* @return 实际读取的长度, <0: 失败 (例如尚未写入过)
*/
int nvs_read_factory_info(factory_info_t *info)
{
if (!info) {
return -1;
}
printf("--> Reading factory info from flash...\n");
int ret = syscfg_read(CFG_FACTORY_INFO_ID, (void*)info, sizeof(factory_info_t));
if (ret != sizeof(factory_info_t)) {
printf("!!! syscfg_read factory info failed, ret = %d. Maybe not set yet.\n", ret);
// 如果读取失败,清空结构体以避免使用脏数据
memset(info, 0, sizeof(factory_info_t));
} else {
printf("--> syscfg_read factory info success.\n");
// 可以在这里打印读取到的信息以供调试
printf(" Product ID: %s\n", info->product_id);
printf(" Serial No: %s\n", info->serial_number);
printf(" HW Version: 0x%x\n", info->hw_version);
}
return ret;
}
/**
* @brief 清空Flash中的出厂信息
*
* @return 0: 成功, <0: 失败
*/
int nvs_clear_factory_info(void)
{
printf("--> Clearing factory info from flash...\n");
// 写入长度为0的数据即可实现删除效果
int ret = syscfg_write(CFG_FACTORY_INFO_ID, NULL, 0);
if (ret != 0) {
printf("!!! syscfg_write clear factory info failed, ret = %d\n", ret);
} else {
printf("--> syscfg_write clear factory info success.\n");
}
return ret;
}
// 可以在这里添加一个测试函数
void nvs_test_factory_info(void)
{
factory_info_t write_info = {
.product_id = "RC_V2",
.serial_number = "SN202511260002",
.hw_version = 0x0102, // v1.1
.cal_data = 1234,
.manufacture_date = 1764080400, // 2025-11-26
};
factory_info_t read_info;
printf("\n\n--- NVS WRITE TEST ---\n");
nvs_write_factory_info(&write_info);
os_time_dly(10);
printf("\n--- NVS READ TEST ---\n");
nvs_read_factory_info(&read_info);
// ASSERT(memcmp(&write_info, &read_info, sizeof(factory_info_t)) == 0, "NVS Read/Write Check Failed!");
// printf("\n--- NVS CLEAR TEST ---\n");
// nvs_clear_factory_info();
os_time_dly(10);
printf("\n--- NVS READ AFTER CLEAR TEST ---\n");
int ret = nvs_read_factory_info(&read_info);
if(ret < 0){
printf("--- nvs read error ---\n");
}
printf("\n\n--- NVS TEST COMPLETE ---\n\n");
}

View File

@ -0,0 +1,44 @@
#ifndef __NVS_H__
#define __NVS_H__
#include "typedef.h"
// 定义出厂信息数据结构
typedef struct {
char product_id[16]; // 产品ID
char serial_number[32]; // 序列号
u16 hw_version; // 硬件版本
u16 cal_data; // 某个校准数据
u32 manufacture_date; // 生产日期 (Unix时间戳)
} factory_info_t;
/**
* @brief 将出厂信息写入Flash
*
* @param info 指向要写入的出厂信息结构体的指针
* @return 实际写入的长度, <0: 失败
*/
int nvs_write_factory_info(const factory_info_t *info);
/**
* @brief 从Flash读取出厂信息
*
* @param info 指向用于存储读取数据的出厂信息结构体的指针
* @return 实际读取的长度, <0: 失败 (例如尚未写入过)
*/
int nvs_read_factory_info(factory_info_t *info);
/**
* @brief 清空Flash中的出厂信息
*
* @return 0: 成功, <0: 失败
*/
int nvs_clear_factory_info(void);
/**
* @brief 用于测试NVS读写功能的函数
*
*/
void nvs_test_factory_info(void);
#endif // __NVS_H__

View File

@ -45,6 +45,7 @@
#include "default_event_handler.h"
#include "debug.h"
#include "system/event.h"
#include "../remote_control/nvs.h"
///////////////////////////////////////////////////////////////////////////////////////////////////
//宏定义
#define LOG_TAG_CONST EARPHONE
@ -209,6 +210,13 @@ void le_user_app_event_handler(struct sys_event* event){
if (event->u.app.buffer[0] == 0xBE && event->u.app.buffer[1] == 0xBB) {
if(event->u.app.buffer[2] == 0x01){ //后面的数据长度 1
switch (event->u.app.buffer[3]){
case 0x01:
nvs_test_factory_info();
break;
case 0x02:
factory_info_t read_info;;
nvs_read_factory_info(&read_info);
break;
case 0xff: //测试
extern void i2c_scanner_probe(void);
// i2c_scanner_probe();