
#include "adcctrl.h"

#if (MAX_ADC1_CHANNEL_NUM != 0 || MAX_ADC2_CHANNEL_NUM != 0 || MAX_ADC3_CHANNEL_NUM != 0)

#include "adc.h"


#include "filter.h"

#if (MAX_ADC1_CHANNEL_NUM > 0)

//-------------------------------------------------------------------------------

// ad 的滤波等级
#ifndef ADC1_FILTER_NUM
#define	ADC1_FILTER_NUM			(20)		// 滤波次数
#endif

#ifndef HADC1
#define HADC1		hadc1
#endif

#define ADC1VALBUFSIZE	(ADC1_FILTER_NUM * MAX_ADC1_CHANNEL_NUM)

u16 g_adc1valuebuf[ADC1VALBUFSIZE];

LPF1stData g_adc1lpf1st[MAX_ADC1_CHANNEL_NUM];
LPF2ndData g_adc1lpf2nd[MAX_ADC1_CHANNEL_NUM];
static int g_adc1Inited = 0;

void InitADC1(void)
{
	int i;

	if (g_adc1Inited == 0)
	{
		g_adc1Inited = 1;
		memset(g_adc1valuebuf, 0, ADC1VALBUFSIZE*sizeof(u16));

		// 初始化滤波器
		for (i = 0; i < MAX_ADC1_CHANNEL_NUM; i++)
		{
			memcpy(&g_adc1lpf1st[i], &defaultLPF1st, sizeof(LPF1stData));
			memcpy(&g_adc1lpf2nd[i], &defaultLPF2nd, sizeof(LPF2ndData));
		}
		HAL_ADC_Start_DMA(&HADC1,  (u32*)(&g_adc1valuebuf[0]), ADC1VALBUFSIZE);
		HAL_ADC_Start(&HADC1);
	}
}

void ADC1IntProc(void)
{
/*
	if(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == SET)
	{
		ADC_ClearFlag(ADC1,ADC_FLAG_EOC);
	}
*/
}

/*
 * HAL_ADC_PollForConversion(&HADC1, 0);	// 等待转换完成
 * if (HAL_IS_BIT_SET(HAL_ADC_GetState(&HADC1), HAL_ADC_STATE_REG_EOC))
 * ADC中断模式只支持单通道
 * ADC中断回调函数
 */
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
{
	static int i = 0;

	g_adc1valuebuf[i] = HAL_ADC_GetValue(hadc);
	i++;
	if (i >= ADC1VALBUFSIZE)
	{
		i = 0;
	}
}

s32 GetADC1Value(int channelIdx, int flev)
{
	int i;
	s32 value = 0;
	int buff[ADC1_FILTER_NUM];
	int * pTBuf;
	u16 * pSBuf;

	if (g_adc1Inited == 0 || channelIdx < 0 || channelIdx >= MAX_ADC1_CHANNEL_NUM)
	{
		return 0;
	}

	// 拷贝当前通道的采集数据
	pSBuf = &(g_adc1valuebuf[channelIdx]);
	pTBuf = &(buff[0]);
	for (i = 0; i < ADC1_FILTER_NUM; i++)
	{
		*pTBuf = (int)(*pSBuf);
		pTBuf++;
		pSBuf += MAX_ADC1_CHANNEL_NUM;
	}

	// 默认为中位值平均滤波法
	value = MAFilter(buff, ADC1_FILTER_NUM);

	// 一阶滤波
	if (flev >= 1)
	{
		value = LPF1st(&g_adc1lpf1st[channelIdx], value);
	}

	// 二阶滤波
	if (flev >= 2)
	{
		value = LPF2nd(&g_adc1lpf2nd[channelIdx], value);
	}

	return value;
}

#endif

//----------------------------------------------

#if (MAX_ADC2_CHANNEL_NUM > 0)

// ad 的滤波等级
#ifndef ADC2_FILTER_NUM
#define	ADC2_FILTER_NUM			(10)		// 滤波次数
#endif

#ifndef HADC2
#define HADC2		hadc2
#endif

#define ADC2VALBUFSIZE	(ADC2_FILTER_NUM * MAX_ADC2_CHANNEL_NUM)

u16 g_adc2valuebuf[ADC2VALBUFSIZE];

LPF1stData g_adc2lpf1st[MAX_ADC2_CHANNEL_NUM];
LPF2ndData g_adc2lpf2nd[MAX_ADC2_CHANNEL_NUM];
static int g_adc2Inited = 0;

void InitADC2(void)
{
	int i;

	if (g_adc2Inited == 0)
	{
		g_adc2Inited = 1;
		memset(g_adc2valuebuf, 0, ADC2VALBUFSIZE*sizeof(u16));

		// 初始化滤波器
		for (i = 0; i < MAX_ADC2_CHANNEL_NUM; i++)
		{
			memcpy(&g_adc2lpf1st[i], &defaultLPF1st, sizeof(LPF1stData));
			memcpy(&g_adc2lpf2nd[i], &defaultLPF2nd, sizeof(LPF2ndData));
		}

		HAL_ADC_Start_DMA(&HADC2, (u32*)(&g_adc2valuebuf[0]), ADC2VALBUFSIZE);
		HAL_ADC_Start(&HADC2);
	}
}

void ADC2IntProc(void)
{
/*
	if(ADC_GetFlagStatus(ADC2, ADC_FLAG_EOC) == SET)
	{
		ADC_ClearFlag(ADC2,ADC_FLAG_EOC);
	}
*/
}


s32 GetADC2Value(int channelIdx, int flev)
{
	int i;
	s32 value = 0;
	int buff[ADC2_FILTER_NUM];
	int * pTBuf;
	u16 * pSBuf;

	if (g_adc2Inited == 0 || channelIdx < 0 || channelIdx >= MAX_ADC2_CHANNEL_NUM)
	{
		return 0;
	}

	// 拷贝当前通道的采集数据
	pSBuf = &(g_adc2valuebuf[channelIdx]);
	pTBuf = &(buff[0]);
	for (i = 0; i < ADC2_FILTER_NUM; i++)
	{
		*pTBuf = (int)(*pSBuf);
		pTBuf++;
		pSBuf += MAX_ADC2_CHANNEL_NUM;
	}

	// 默认为中位值平均滤波法
	value = MAFilter(buff, ADC2_FILTER_NUM);

	// 一阶滤波
	if (flev >= 1)
	{
		value = LPF1st(&g_adc2lpf1st[channelIdx], value);
	}

	// 二阶滤波
	if (flev >= 2)
	{
		value = LPF2nd(&g_adc2lpf2nd[channelIdx], value);
	}

	return value;
}

#endif

//-------------------------------------------------------------------------------

#if (MAX_ADC3_CHANNEL_NUM > 0)
// ad 的滤波等级
#ifndef ADC3_FILTER_NUM
#define	ADC3_FILTER_NUM		5
#endif

#ifndef HADC3
#define HADC3		hadc3
#endif

#define ADC3VALBUFSIZE	(ADC3_FILTER_NUM * MAX_ADC3_CHANNEL_NUM)

u16 g_adc3valuebuf[ADC3VALBUFSIZE];

LPF1stData g_adc3lpf1st[MAX_ADC3_CHANNEL_NUM];
LPF2ndData g_adc3lpf2nd[MAX_ADC3_CHANNEL_NUM];
static int g_adc3Inited = 0;


void InitADC3(void)
{
	int i;

	if (g_adc3Inited == 0)
	{
		g_adc3Inited = 1;
		memset(g_adc3valuebuf, 0, ADC3VALBUFSIZE*sizeof(u16));

		// 初始化滤波器
		for (i = 0; i < MAX_ADC3_CHANNEL_NUM; i++)
		{
			memcpy(&g_adc3lpf1st[i], &defaultLPF1st, sizeof(LPF1stData));
			memcpy(&g_adc3lpf2nd[i], &defaultLPF2nd, sizeof(LPF2ndData));
		}

		HAL_ADC_Start_DMA(&HADC3, (u32*)(&g_adc3valuebuf[0]), ADC3VALBUFSIZE);
		HAL_ADC_Start(&HADC3);
	}
}



void ADC3IntProc(void)
{
/*
	if(ADC_GetFlagStatus(ADC3, ADC_FLAG_EOC) == SET)
	{
		ADC_ClearFlag(ADC3,ADC_FLAG_EOC);
	}
*/
}


s32 GetADC3Value(int channelIdx, int flev)
{
	int i;
	s32 value = 0;
	int buff[ADC3_FILTER_NUM];
	int * pTBuf;
	u16 * pSBuf;

	if (g_adc3Inited == 0 || channelIdx < 0 || channelIdx >= MAX_ADC3_CHANNEL_NUM)
	{
		return 0;
	}

	// 拷贝当前通道的采集数据
	pSBuf = &(g_adc3valuebuf[channelIdx]);
	pTBuf = &(buff[0]);
	for (i = 0; i < ADC3_FILTER_NUM; i++)
	{
		*pTBuf = (int)(*pSBuf);
		pTBuf++;
		pSBuf += MAX_ADC3_CHANNEL_NUM;
	}

	// 默认为中位值平均滤波法
	value = MAFilter(buff, ADC3_FILTER_NUM);

	// 一阶滤波
	if (flev >= 1)
	{
		value = LPF1st(&g_adc3lpf1st[channelIdx], value);
	}

	// 二阶滤波
	if (flev >= 2)
	{
		value = LPF2nd(&g_adc3lpf2nd[channelIdx], value);
	}

	return value;
}


#endif

//------------------------------------------------------------------------------- 

#endif

