cun
This commit is contained in:
@ -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, ®_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);
|
||||
}
|
||||
|
||||
@ -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;
|
||||
|
||||
113
apps/earphone/remote_control/nvs.c
Normal file
113
apps/earphone/remote_control/nvs.c
Normal 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");
|
||||
}
|
||||
44
apps/earphone/remote_control/nvs.h
Normal file
44
apps/earphone/remote_control/nvs.h
Normal 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__
|
||||
Reference in New Issue
Block a user