pile_com_stm32/main/peripheral/config/config.c

162 lines
7.3 KiB
C
Raw Normal View History

2024-03-30 18:47:02 +08:00
#include <string.h>
#include "esp_log.h"
2023-07-20 10:17:11 +08:00
#include "config.h"
#include "fram.h"
2024-03-30 18:47:02 +08:00
#include "flow.h"
#include "depth.h"
2023-07-20 10:17:11 +08:00
static const char *TAG = "config";
2024-03-30 18:47:02 +08:00
extern cal_4_20ma_t *cal_4_20ma; //电流数据结构体
extern flow_config_t *flow_config; //流量数据结构体
extern depth_config_t *depth_config; //深度数据结构体
2023-07-20 10:17:11 +08:00
2024-03-30 18:47:02 +08:00
extern float ac_current_coef[3]; //可能表示交流电电流的系数或校准参数 ?
2023-07-20 10:17:11 +08:00
// 三标一号机
2024-03-30 18:47:02 +08:00
const cal_4_20ma_t default_cal_4_20ma = {0, {{12740, 63845}, {12760, 63953}}};
2023-07-20 10:17:11 +08:00
const flow_config_t default_flow_cfg = {0, 1, {0, 0}, {{0, 10000}, {0, 10000}}, {6944, 6944}}; // 4~20MA 输入6m/H 100.00L/min
2023-11-15 22:06:47 +08:00
/*
const flow_config_t default_flow_cfg = {
.magic = 0,
.input_type = 1,
.min_flow = {0, 0},
.ad_cal = {
{ .flow_min = 0, .flow_max = 10000 },
{ .flow_min = 0, .flow_max = 10000 },
},
.pulse_coef = {6944, 6944},
.rsv = {0, 0, 0, 0, 0, 0}
};
*/
2023-07-20 10:17:11 +08:00
// const flow_config_t default_flow_cfg = {0,1,{60,60},{{0,20000},{0,20000}},{6944,6944}}; //脉冲输入 12m/H
// const depth_config_t default_depth_cfg = {0, 2,0, 1000, 32, -100, 12000, 100, -100, 1000,500, 500, 100, 150, 150}; // 方向脉冲编码器 10线2倍频
const depth_config_t default_depth_cfg = {
.magic = 0,
.input_type = 0,
.port = 1,
.N = 1000,
.M = 640,
.min_depth = -100,
.max_depth = 12000,
.sample_depth = 100,
.depth_offset = -100,
.min_valid_depth = 1000,
.inc_pile_depth = 5000,
.current_on_threshold = 500,
.current_off_threshold = 100,
.move_on_duratino = 150,
.move_off_duration = 150,
.move_current_channel = 2,
2024-03-30 18:47:02 +08:00
}; // 方向脉冲编码器 10线2倍频
2023-07-20 10:17:11 +08:00
// const depth_config_t default_depth_cfg = {0,3,0,3800,-200,16000,100,-200}; //200线开漏型正交
// const depth_config_t default_depth_cfg = {0,1,0,76000,-200,16000,100,-200};
2023-11-15 22:06:47 +08:00
2024-03-30 18:47:02 +08:00
uint16_t last_pile_id = 0;
2023-11-15 22:06:47 +08:00
/*fram_read */
/*
I2C 线 ESP32 FRAM
param:
param:
param:
*/
2023-07-20 10:17:11 +08:00
void config_load(void)
{
uint16_t temp[2];
2023-11-15 22:06:47 +08:00
fram_read(0, &temp, sizeof(temp));//通过调用 fram_read 函数将 FRAM 中的两个 uint16_t 数据读取到 temp 数组中,起始地址为 0长度为两个 uint16_t 的大小
2023-07-20 10:17:11 +08:00
{
if (temp[0] == MAGIC)
{
last_pile_id = temp[1];
}
}
fram_read(EE_CAL_4_20MA_ADDR, cal_4_20ma, sizeof(default_cal_4_20ma));
if (cal_4_20ma->magic != MAGIC)
{
ESP_LOGI(TAG, "fram_read cal_4_20ma failed, use default value");
memcpy(cal_4_20ma, &default_cal_4_20ma, sizeof(default_cal_4_20ma));
}
2024-02-23 17:21:13 +08:00
ESP_LOGW(TAG, "load cal_4_20ma:0x%x 0x%x 0x%x 0x%x 0x%x", (unsigned int)cal_4_20ma->magic, (unsigned int)cal_4_20ma->ch[0].ad_4ma, (unsigned int)cal_4_20ma->ch[0].ad_20ma, (unsigned int)cal_4_20ma->ch[1].ad_4ma, (unsigned int)cal_4_20ma->ch[1].ad_20ma);
2023-07-20 10:17:11 +08:00
fram_read(EE_FLOW_CFG_ADDR, flow_config, sizeof(default_flow_cfg));
if (flow_config->magic != MAGIC)
{
ESP_LOGW(TAG, "fram_read flow_config failed, use default value");
memcpy(flow_config, &default_flow_cfg, sizeof(default_flow_cfg));
}
2024-02-23 17:21:13 +08:00
ESP_LOGW(TAG, "load flow_cfg:0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x", (unsigned int)flow_config->magic, (unsigned int)flow_config->input_type, (unsigned int)flow_config->min_flow[0], (unsigned int)flow_config->min_flow[1], (unsigned int)flow_config->ad_cal[0].flow_min, (unsigned int)flow_config->ad_cal[0].flow_max, (unsigned int)flow_config->ad_cal[1].flow_min, (unsigned int)flow_config->ad_cal[1].flow_max, (unsigned int)flow_config->pulse_coef[0], (unsigned int)flow_config->pulse_coef[1]);
2023-07-20 10:17:11 +08:00
2024-03-30 18:47:02 +08:00
#if 1
2023-07-20 10:17:11 +08:00
fram_read(EE_DEPTH_CFG_ADDR, depth_config, sizeof(default_depth_cfg));
if (depth_config->magic != MAGIC)
{
ESP_LOGW(TAG, "fram_read depth_config failed, use default value");
memcpy(depth_config, &default_depth_cfg, sizeof(default_depth_cfg));
}
2024-02-23 17:21:13 +08:00
ESP_LOGW(TAG, "load depth_cfg: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x", \
(unsigned int)depth_config->magic, (unsigned int)depth_config->input_type, (unsigned int)depth_config->port, \
(unsigned int)depth_config->N, (unsigned int)depth_config->M, (unsigned int)depth_config->min_depth, (unsigned int)depth_config->max_depth, \
(unsigned int)depth_config->sample_depth, (unsigned int)depth_config->depth_offset, (unsigned int)depth_config->min_valid_depth, \
(unsigned int)depth_config->inc_pile_depth, (unsigned int)depth_config->current_on_threshold, (unsigned int)depth_config->current_off_threshold, \
(unsigned int)depth_config->move_on_duratino, (unsigned int)depth_config->move_off_duration, (unsigned int)depth_config->move_current_channel);
2024-03-30 18:47:02 +08:00
#endif
2023-07-20 10:17:11 +08:00
}
2024-01-20 17:44:50 +08:00
//恢复默认配置
2023-07-20 10:17:11 +08:00
void restore_default(void)
{
memcpy(cal_4_20ma, &default_cal_4_20ma, sizeof(default_cal_4_20ma));
memcpy(flow_config, &default_flow_cfg, sizeof(default_flow_cfg));
2024-03-30 18:47:02 +08:00
// memcpy(depth_config, &default_depth_cfg, sizeof(default_depth_cfg));
2023-07-20 10:17:11 +08:00
fram_write(EE_CAL_4_20MA_ADDR, cal_4_20ma, sizeof(default_cal_4_20ma));
fram_write(EE_FLOW_CFG_ADDR, flow_config, sizeof(default_flow_cfg));
2024-03-30 18:47:02 +08:00
// fram_write(EE_DEPTH_CFG_ADDR, depth_config, sizeof(default_depth_cfg));
2023-07-20 10:17:11 +08:00
}
void save_cal_4_20ma(void)
{
2024-02-23 17:21:13 +08:00
uint16_t a = 1;
ESP_LOGW(TAG, "save_cal_4_20ma:0x%x 0x%x 0x%x 0x%x 0x%x", (unsigned int)cal_4_20ma->magic, (unsigned int)cal_4_20ma->ch[0].ad_4ma, (unsigned int)cal_4_20ma->ch[0].ad_20ma, (unsigned int)cal_4_20ma->ch[1].ad_4ma, (unsigned int)cal_4_20ma->ch[1].ad_20ma);
2023-07-20 10:17:11 +08:00
cal_4_20ma->magic = MAGIC;
cal_4_20ma->magic = fram_write(EE_CAL_4_20MA_ADDR, cal_4_20ma, sizeof(default_cal_4_20ma));
}
void save_flow_cfg(void)
{
2024-02-23 17:21:13 +08:00
ESP_LOGW(TAG, "save_flow_cfg:0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x", (unsigned int)flow_config->magic, (unsigned int)flow_config->input_type, (unsigned int)flow_config->min_flow[0], (unsigned int)flow_config->min_flow[1], (unsigned int)flow_config->ad_cal[0].flow_min, (unsigned int)flow_config->ad_cal[0].flow_max, (unsigned int)flow_config->ad_cal[1].flow_min, (unsigned int)flow_config->ad_cal[1].flow_max, (unsigned int)flow_config->pulse_coef[0], (unsigned int)flow_config->pulse_coef[1]);
2023-07-20 10:17:11 +08:00
flow_config->magic = MAGIC;
flow_config->magic = fram_write(EE_FLOW_CFG_ADDR, flow_config, sizeof(default_flow_cfg));
}
2024-03-30 18:47:02 +08:00
#if 1
2023-07-20 10:17:11 +08:00
void save_depth_cfg(void)
{
2024-02-23 17:21:13 +08:00
ESP_LOGW(TAG, "save_depth_cfg: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x", \
(unsigned int)depth_config->magic, (unsigned int)depth_config->input_type, (unsigned int)depth_config->port, \
(unsigned int)depth_config->N, (unsigned int)depth_config->M, (unsigned int)depth_config->min_depth, (unsigned int)depth_config->max_depth, \
(unsigned int)depth_config->sample_depth, (unsigned int)depth_config->depth_offset, (unsigned int)depth_config->min_valid_depth, \
(unsigned int)depth_config->inc_pile_depth, (unsigned int)depth_config->current_on_threshold, (unsigned int)depth_config->current_off_threshold, \
(unsigned int)depth_config->move_on_duratino, (unsigned int)depth_config->move_off_duration, (unsigned int)depth_config->move_current_channel);
2023-07-20 10:17:11 +08:00
depth_config->magic = MAGIC;
depth_config->magic = fram_write(EE_DEPTH_CFG_ADDR, depth_config, sizeof(default_depth_cfg));
}
2024-03-30 18:47:02 +08:00
#endif
2023-07-20 10:17:11 +08:00
void save_pile_id(void)
{
uint16_t temp[2] = {MAGIC, last_pile_id};
fram_write(0, &temp, sizeof(temp));
}
2024-03-30 18:47:02 +08:00
void config_init(void)
{
ESP_ERROR_CHECK(i2c_master_init());
restore_default();
save_cal_4_20ma();
save_flow_cfg();
config_load();//读取保存在FRAM里的数据
}