/**
 * @file analog_loop_driver.h
 * @brief Driver for current loop analog signal processing with dual ADC support.
 *
 * This driver interfaces with a 4-20mA current loop signal, using:
 * - HCNR201 optocoupler with LED current sensing through 100Ω resistor
 * - MMBT3906 transistor controlled by NCV20071 opamp
 * - 24.9Ω shunt resistor for current-to-voltage conversion
 * - Isolation using HCNR201 (input side) and NCV20071 (output side)
 * - ADC input to STM32F103 via 100Ω resistor with dual ADC capability
 */

#ifndef INC_ANALOG_LOOP_DRIVER_H_
#define INC_ANALOG_LOOP_DRIVER_H_

#ifdef __cplusplus
extern "C" {
#endif

#include "stm32f1xx_hal.h"
#include <stdint.h>

// --- Configuration Parameters ---
#define ANALOG_LOOP_ADC_CHANNEL     ADC_CHANNEL_0   		// Assuming PA0 is used for ADC input
#define ANALOG_LOOP_ADC_RANK        ADC_RANK_CHANNELNUMBER 	// ADC Bus
#define ANALOG_LOOP_VREF_MV         3300U           		// Reference voltage in millivolts (3.3V)
#define ANALOG_LOOP_CURRENT_MIN_UA  4000U             	// Minimum current in uA (4mA)
#define ANALOG_LOOP_CURRENT_MAX_UA  20000U            	// Maximum current in uA (20mA)
#define ANALOG_LOOP_R1_OHMS         100U            		// Resistance of R1 in ohms
#define ANALOG_LOOP_R2_OHMS         249U            		// Resistance of R2 in ohms
#define ANALOG_LOOP_R3_OHMS         100U            		// Resistance of R3 in ohms (ADC input resistor)

// --- Function Prototypes ---
HAL_StatusTypeDef AnalogLoop_Init(ADC_HandleTypeDef *hadc);
uint32_t AnalogLoop_GetRawValue(void);
int16_t AnalogLoop_GetCurrent_uA(void);
int16_t AnalogLoop_GetVoltage_mV(void);

#ifdef __cplusplus
}
#endif

#endif /* INC_ANALOG_LOOP_DRIVER_H_ */
