pile_com_stm32/main/stm32/config.c
2024-03-04 13:10:21 +08:00

193 lines
8.7 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "config.h"
#include "fram.h"
#include "utils.h"
#include "modbuss.h"
#include "stdint.h"
#include "esp_log.h"
// #include "flow.h"
// #include "depth.h"
// config_data_t * config_para = (config_data_t*)&gWordVar[256];
// extern real_t * real;
static const char *TAG = "config";
extern cal_4_20ma_t *cal_4_20ma;//电流数据结构体
extern flow_config_t *flow_config;//流量数据结构体
extern depth_config_t *depth_config;//深度数据结构体
extern float ac_current_coef[3];//可能表示交流电电流的系数或校准参数 ?
// 三标一号机
const cal_4_20ma_t default_cal_4_20ma = {0, {{12740, 63845}, {12760, 63953}}};
const flow_config_t default_flow_cfg = {0, 1, {0, 0}, {{0, 10000}, {0, 10000}}, {6944, 6944}}; // 4~20MA 输入6m/H 100.00L/min
/*
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}
};
*/
// 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,
}; // 方向脉冲编码器 10线2倍频
// 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};
#define MAGIC 30627
#define EE_CAL_4_20MA_ADDR 16
#define EE_FLOW_CFG_ADDR (EE_CAL_4_20MA_ADDR + sizeof(default_cal_4_20ma))
#define EE_DEPTH_CFG_ADDR (EE_FLOW_CFG_ADDR + sizeof(default_flow_cfg))
extern uint16_t last_pile_id = 0;
extern void init_comm(void);
// void sys_init(void)
// {
// // HAL_Delay(100);
// // GetCompileDateTime(&gWordVar[0]);
// // flash_load();
// // modbus_addr = config_para->modbus_addr;
// // sliding_window_filter_init(&sliding_window_a,real->data_long,200);
// // HAL_TIM_Base_Start(&htim6);//ms<6D><73>ʱ<EFBFBD><CAB1>
// // HAL_TIM_Base_Start_IT(&htim17);//us<75><73>ʱ<EFBFBD><CAB1>
// //// HAL_TIM_Encoder_Start_IT(&htim1,TIM_CHANNEL_ALL);//enc
// // HAL_TIM_Encoder_Start(&htim3,TIM_CHANNEL_ALL);//enc
// // HAL_TIM_IC_Start_IT(&htim1,TIM_CHANNEL_1);//ll
// // HAL_TIM_IC_Start_IT(&htim1,TIM_CHANNEL_2);//ll
// // HAL_TIM_IC_Start_IT(&htim15,TIM_CHANNEL_1);//ll
// // HAL_TIM_IC_Start_IT(&htim15,TIM_CHANNEL_2);//ll
// // HAL_UART_Receive_IT(&huart1,&temp_rx_data,1);
// // init_comm();
// // if(config_para->traffic_mode == MA_4_20)
// // {
// // ADS1220_Init();
// // }
// // HAL_GPIO_WritePin(GPS_PWR_EN_GPIO_Port, GPS_PWR_EN_Pin, GPIO_PIN_SET);
// // HAL_GPIO_WritePin(LED2_GPIO_Port, LED2_Pin,0);
// // HAL_Delay(500);
// }
/*fram_read */
/*
通过 I2C 总线读取 ESP32 上连接的 FRAM非易失性存储器芯片中指定地址的数据
param:地址
param:存储数据的数组
param:需要读取的长度
*/
void config_load(void)
{
uint16_t temp[2];
fram_read(0, &temp, sizeof(temp));//通过调用 fram_read 函数将 FRAM 中的两个 uint16_t 数据读取到 temp 数组中,起始地址为 0长度为两个 uint16_t 的大小
{
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));
}
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);
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));
}
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]);
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));
}
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);
// 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));
// }
}
//恢复默认配置
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));
memcpy(depth_config, &default_depth_cfg, sizeof(default_depth_cfg));
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));
fram_write(EE_DEPTH_CFG_ADDR, depth_config, sizeof(default_depth_cfg));
}
void save_cal_4_20ma(void)
{
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);
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)
{
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]);
flow_config->magic = MAGIC;
flow_config->magic = fram_write(EE_FLOW_CFG_ADDR, flow_config, sizeof(default_flow_cfg));
}
void save_depth_cfg(void)
{
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);
depth_config->magic = MAGIC;
depth_config->magic = fram_write(EE_DEPTH_CFG_ADDR, depth_config, sizeof(default_depth_cfg));
}
void save_pile_id(void)
{
uint16_t temp[2] = {MAGIC, last_pile_id};
fram_write(0, &temp, sizeof(temp));
}