forked from achamaikin/CCSModuleSW30Web
Overwrite repository contents with fork state.
Replace outdated files with the latest working tree from fork/CCSModuleSW30Web to align source and generated artifacts. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+75
-29
@@ -1,37 +1,34 @@
|
||||
#include "cp.h"
|
||||
#include "adc.h"
|
||||
#include "board.h"
|
||||
#include "debug.h"
|
||||
#include "tim.h"
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define MAX_DUTY 450
|
||||
#define FILTER_ORDER 100
|
||||
|
||||
static int32_t cp_voltage_mv = 0;
|
||||
static uint8_t cp_duty = 0;
|
||||
CP_State_t fake_cp_state = EV_STATE_ACQUIRING;
|
||||
CP_State_t cp_state_buffer = EV_STATE_ACQUIRING;
|
||||
|
||||
static uint32_t CP_ReadAdcChannel(uint32_t ch) {
|
||||
uint32_t adc = 0;
|
||||
|
||||
ADC_Select_Channel(ch);
|
||||
HAL_ADC_Start(&hadc1);
|
||||
HAL_ADC_PollForConversion(&hadc1, 10);
|
||||
adc = HAL_ADC_GetValue(&hadc1);
|
||||
HAL_ADC_Stop(&hadc1);
|
||||
|
||||
return adc;
|
||||
}
|
||||
#define VREFINT_CAL_ADDR ((uint16_t*)0x1FFFF7BA) // для STM32F1!
|
||||
|
||||
static int32_t CP_ReadVoltageMv(void)
|
||||
{
|
||||
uint32_t adc = 0;
|
||||
int32_t v_adc_mv = 0;
|
||||
int32_t v_out_mv = 0;
|
||||
uint32_t adc_cp = adc_data.cp_raw;
|
||||
uint32_t adc_vref = adc_data.vrefint_raw; // нужно измерять!
|
||||
|
||||
adc = CP_ReadAdcChannel((uint32_t)4u);
|
||||
v_adc_mv = (int32_t)((adc * 3300u) / 4095u);
|
||||
v_out_mv = ((v_adc_mv - 1723) * 1000) / 130;
|
||||
// VREFINT в мВ (берётся из даташита или калибровки MCU)
|
||||
const int32_t VREFINT_MV = 1210;
|
||||
|
||||
// напряжение на входе АЦП
|
||||
int32_t v_adc_mv = (adc_cp * VREFINT_MV) / adc_vref;
|
||||
|
||||
// дальше твоя формула
|
||||
int32_t v_out_mv = ((v_adc_mv - 1723) * 7704) / 1000;
|
||||
|
||||
return v_out_mv;
|
||||
}
|
||||
@@ -50,7 +47,7 @@ void CP_Init(void) {
|
||||
#endif
|
||||
|
||||
HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_2);
|
||||
HAL_TIM_OC_Start_IT(&htim3, TIM_CHANNEL_1);
|
||||
HAL_TIM_OC_Start(&htim3, TIM_CHANNEL_1);
|
||||
}
|
||||
|
||||
void CP_SetDuty(uint8_t percentage) {
|
||||
@@ -71,11 +68,12 @@ uint8_t CP_GetDuty(void) {
|
||||
}
|
||||
|
||||
int32_t CP_GetVoltage(void) {
|
||||
cp_voltage_mv = CP_ReadVoltageMv();
|
||||
return cp_voltage_mv;
|
||||
}
|
||||
|
||||
CP_State_t CP_GetState(void) {
|
||||
int32_t voltage_real = cp_voltage_mv;
|
||||
int32_t voltage_real = CP_GetVoltage();
|
||||
|
||||
if(fake_cp_state != EV_STATE_ACQUIRING) {
|
||||
return fake_cp_state;
|
||||
@@ -98,18 +96,66 @@ CP_State_t CP_GetState(void) {
|
||||
}
|
||||
}
|
||||
|
||||
void CP_Loop(void) {
|
||||
(void)CP_GetState();
|
||||
CP_State_t CP_GetFilteredState(void) {
|
||||
return cp_state_buffer;
|
||||
}
|
||||
|
||||
void HAL_TIM_OC_DelayElapsedCallback(TIM_HandleTypeDef *htim)
|
||||
{
|
||||
if (htim->Instance == TIM3 && htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1) {
|
||||
if (ADC_TryLock() == 0u) {
|
||||
return;
|
||||
}
|
||||
cp_voltage_mv = CP_ReadVoltageMv();
|
||||
ADC_Unlock();
|
||||
void CP_FilterState(void) {
|
||||
|
||||
static CP_State_t pending_state = EV_STATE_ACQUIRING;
|
||||
static uint8_t stable_count = 0u;
|
||||
CP_State_t current_state = CP_GetState();
|
||||
|
||||
/* Keep last accepted state while CP is still acquiring. */
|
||||
if (current_state == EV_STATE_ACQUIRING) {
|
||||
pending_state = EV_STATE_ACQUIRING;
|
||||
stable_count = 0u;
|
||||
return;
|
||||
}
|
||||
|
||||
if (current_state != pending_state) {
|
||||
pending_state = current_state;
|
||||
stable_count = 1u;
|
||||
return;
|
||||
}
|
||||
|
||||
if (stable_count < FILTER_ORDER) {
|
||||
stable_count++;
|
||||
}
|
||||
|
||||
if (stable_count >= FILTER_ORDER) {
|
||||
cp_state_buffer = pending_state;
|
||||
}
|
||||
}
|
||||
|
||||
void CP_Loop(void) {
|
||||
static uint32_t tick;
|
||||
if ((int32_t)(HAL_GetTick() - tick) < 1) return;
|
||||
tick = HAL_GetTick();
|
||||
static uint8_t initialized = 0;
|
||||
static CP_State_t prev_state = EV_STATE_ACQUIRING;
|
||||
static uint8_t prev_duty = 0;
|
||||
|
||||
CP_FilterState();
|
||||
|
||||
CP_State_t current_state = CP_GetFilteredState();
|
||||
uint8_t current_duty = cp_duty;
|
||||
|
||||
if (!initialized) {
|
||||
prev_state = current_state;
|
||||
prev_duty = current_duty;
|
||||
initialized = 1;
|
||||
return;
|
||||
}
|
||||
|
||||
if (current_state != prev_state) {
|
||||
log_printf(LOG_INFO, "CP state changed: %d -> %d\n", prev_state, current_state);
|
||||
prev_state = current_state;
|
||||
}
|
||||
|
||||
if (current_duty != prev_duty) {
|
||||
log_printf(LOG_INFO, "CP duty changed: %u -> %u\n", prev_duty, current_duty);
|
||||
prev_duty = current_duty;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user