#ifndef _MATLAB_ADC_H_
#define _MATLAB_ADC_H_

#include "stm32_matlab_conf.h"


#ifdef STM32F1
#define ADC_NOISE_LSB 30  // Большой шум STMF103 в LSB (квантах АЦП)
#endif

#ifdef STM32F4
#define ADC_NOISE_LSB 3  // Шум в LSB (квантах АЦП)
#endif

/////////////////////////////---STRUCTURES---///////////////////////////
struct ADC_Sim
{
    double conversion_time_elapsed;
    uint32_t current_channel;
    uint32_t current_rank;
    uint16_t last_conversion_value;

    double channel_values[19];
    uint8_t channel_connected[19];

    // Timing
    double simulation_step;
    double adc_clock_freq;

    // Добавьте для поддержки триггеров
    uint8_t external_trigger_enabled;
    uint32_t trigger_source;
    uint8_t last_trigger_state;
};
///////////////////////////////////////////////////////////////////////

///////////////////////////---FUNCTIONS---///////////////////////////

void Simulate_ADCs(void);
void ADC_Simulation(ADC_TypeDef* ADCx, struct ADC_Sim* ADCS);
void ADC_Start_Conversion(ADC_TypeDef* ADCx, struct ADC_Sim* ADCS);
void ADC_Complete_Conversion(ADC_TypeDef* ADCx, struct ADC_Sim* ADCS);

void ADC_Check_External_Trigger(ADC_TypeDef* ADCx, struct ADC_Sim* ADCS);
double ADC_Get_Total_Conversion_Time(ADC_TypeDef* ADCx, struct ADC_Sim* ADCS);
double ADC_Get_Total_Sequence_Time(ADC_TypeDef* ADCx, struct ADC_Sim* ADCS);
uint32_t ADC_Get_Sampling_Cycles(ADC_TypeDef* ADCx, uint32_t channel);
uint32_t ADC_Get_Sequence_Length(ADC_TypeDef* ADCx);
uint32_t ADC_Get_Sequence_Channel(ADC_TypeDef* ADCx, uint32_t rank);
void ADC_DMA_Sim_Transfer(ADC_TypeDef* ADCx, struct ADC_Sim* ADCS);
void ADC_Set_Channel_Value(ADC_TypeDef* ADCx, uint32_t channel, double voltage);

void ADC_SIM_DEINIT(void);

///////////////////////////////////////////////////////////////////////

#endif // _MATLAB_ADC_H_