

#define _IN_DRIVERPARAS_C
#include "driverparas.h"

#if (DRIVERS_NUM > 0)

#if (DRIVER_PARA == DRIVER_PARA_ST)

DriverPara g_defPara[DRIVERS_NUM];

#endif


#ifndef DEF_DRV1_CTRLMODE
#define	DEF_DRV1_CTRLMODE	CTRLMODE_OPEN
#endif

#ifndef DEF_DRV2_CTRLMODE
#define	DEF_DRV2_CTRLMODE	CTRLMODE_OPEN
#endif

#ifndef DEF_DRV3_CTRLMODE
#define	DEF_DRV3_CTRLMODE	CTRLMODE_OPEN
#endif

#ifndef DEF_DRV4_CTRLMODE
#define	DEF_DRV4_CTRLMODE	CTRLMODE_OPEN
#endif

#ifndef DEF_DRV1_STEPCIRCLE
#define DEF_DRV1_STEPCIRCLE		4000
#endif

#ifndef DEF_DRV2_STEPCIRCLE
#define DEF_DRV2_STEPCIRCLE		4000
#endif

#ifndef DEF_DRV3_STEPCIRCLE
#define DEF_DRV3_STEPCIRCLE		4000
#endif

#ifndef DEF_DRV4_STEPCIRCLE
#define DEF_DRV4_STEPCIRCLE		4000
#endif

void InitDriverParas(void)
{
#if (DRIVER_PARA == DRIVER_PARA_NOR)
	InitNorParas();

#if (DRIVERS_NUM >= 1)
	SetNorParaList(DRIVER1_PARA_BLOCK, (void*)&g_defDriverParaList);				// 设置参数列表
	SetNorParaNameString(DRIVER1_PARA_BLOCK, (void*)&g_driverParaStrList);			// 设置参数名称列表

	if (GetNorParaEn(DRIVER1_PARA_BLOCK) != PARA_VALID)		// 参数无效,使用默认参数
	{
		printf("driver 1 load default para\r\n");
		LoadNorDefPara(DRIVER1_PARA_BLOCK);
		if (DEF_DRV1_CTRLMODE == CTRLMODE_OPEN || DEF_DRV1_CTRLMODE == CTRLMODE_CLOSE)
		{
			GetDriverParas(0)->closeCtrlMode = DEF_DRV1_CTRLMODE;
		}
		GetDriverParas(0)->stepPerCircle = DEF_DRV1_STEPCIRCLE;
#ifdef DEF_DRV1_CLOSECIRCLE
		GetDriverParas(0)->closeCircle = DEF_DRV1_CLOSECIRCLE;
#endif
#ifdef DEF_DRV1_KP
		GetDriverParas(0)->kp = DEF_DRV1_KP;
#endif
#ifdef DEF_DRV1_KI
		GetDriverParas(0)->ki = DEF_DRV1_KI;
#endif
#ifdef DEF_DRV1_BASECIRCLE
		GetDriverParas(0)->basecircle = DEF_DRV1_BASECIRCLE;
#endif
		SaveNorParas(DRIVER1_PARA_BLOCK);
	}

	if (GetDriverParas(0)->stepPerCircle != DEF_DRV1_STEPCIRCLE ||
			GetDriverParas(0)->closeCtrlMode != DEF_DRV1_CTRLMODE ||
			0)
	{
		GetDriverParas(0)->stepPerCircle = DEF_DRV1_STEPCIRCLE;
		GetDriverParas(0)->closeCtrlMode = DEF_DRV1_CTRLMODE;
		SaveNorParas(DRIVER1_PARA_BLOCK);
	}

	CheckParaRange(DRIVER1_PARA_BLOCK);			// 进行参数范围检测
#endif

#if (DRIVERS_NUM >= 2)
	SetNorParaList(DRIVER2_PARA_BLOCK, (void*)&g_defDriverParaList);				// 设置参数列表
	SetNorParaNameString(DRIVER2_PARA_BLOCK, (void*)&g_driverParaStrList);			// 设置参数名称列表

	if (GetNorParaEn(DRIVER2_PARA_BLOCK) != PARA_VALID)		// 参数无效,使用默认参数
	{
		printf("driver 2 load default para\r\n");
		LoadNorDefPara(DRIVER2_PARA_BLOCK);
		if (DEF_DRV2_CTRLMODE == CTRLMODE_OPEN || DEF_DRV2_CTRLMODE == CTRLMODE_CLOSE)
		{
			GetDriverParas(1)->closeCtrlMode = DEF_DRV2_CTRLMODE;
		}
		GetDriverParas(1)->stepPerCircle = DEF_DRV2_STEPCIRCLE;
#ifdef DEF_DRV2_CLOSECIRCLE
		GetDriverParas(1)->closeCircle = DEF_DRV2_CLOSECIRCLE;
#endif
#ifdef DEF_DRV2_KP
		GetDriverParas(1)->kp = DEF_DRV2_KP;
#endif
#ifdef DEF_DRV2_KI
		GetDriverParas(1)->ki = DEF_DRV2_KI;
#endif
#ifdef DEF_DRV2_BASECIRCLE
		GetDriverParas(1)->basecircle = DEF_DRV2_BASECIRCLE;
#endif
		SaveNorParas(DRIVER2_PARA_BLOCK);
	}

	if (GetDriverParas(1)->stepPerCircle != DEF_DRV2_STEPCIRCLE ||
			GetDriverParas(1)->closeCtrlMode != DEF_DRV2_CTRLMODE ||
			0)
	{
		GetDriverParas(1)->stepPerCircle = DEF_DRV2_STEPCIRCLE;
		GetDriverParas(1)->closeCtrlMode = DEF_DRV2_CTRLMODE;
		SaveNorParas(DRIVER2_PARA_BLOCK);
	}

	CheckParaRange(DRIVER2_PARA_BLOCK);			// 进行参数范围检测
#endif

#if (DRIVERS_NUM >= 3)
	SetNorParaList(DRIVER3_PARA_BLOCK, (void*)&g_defDriverParaList);				// 设置参数列表
	SetNorParaNameString(DRIVER3_PARA_BLOCK, (void*)&g_driverParaStrList);			// 设置参数名称列表

	if (GetNorParaEn(DRIVER3_PARA_BLOCK) != PARA_VALID)		// 参数无效,使用默认参数
	{
		printf("driver 3 load default para\r\n");
		LoadNorDefPara(DRIVER3_PARA_BLOCK);
		if (DEF_DRV3_CTRLMODE == CTRLMODE_OPEN || DEF_DRV3_CTRLMODE == CTRLMODE_CLOSE)
		{
			GetDriverParas(2)->closeCtrlMode = DEF_DRV3_CTRLMODE;
		}
		GetDriverParas(2)->stepPerCircle = DEF_DRV3_STEPCIRCLE;
#ifdef DEF_DRV3_CLOSECIRCLE
		GetDriverParas(2)->closeCircle = DEF_DRV3_CLOSECIRCLE;
#endif
#ifdef DEF_DRV3_KP
		GetDriverParas(2)->kp = DEF_DRV3_KP;
#endif
#ifdef DEF_DRV3_KI
		GetDriverParas(2)->ki = DEF_DRV3_KI;
#endif
		SaveNorParas(DRIVER3_PARA_BLOCK);
	}

	if (GetDriverParas(2)->stepPerCircle != DEF_DRV3_STEPCIRCLE ||
			GetDriverParas(2)->closeCtrlMode != DEF_DRV3_CTRLMODE ||
			0)
	{
		GetDriverParas(2)->stepPerCircle = DEF_DRV3_STEPCIRCLE;
		GetDriverParas(2)->closeCtrlMode = DEF_DRV3_CTRLMODE;
		SaveNorParas(DRIVER3_PARA_BLOCK);
	}

	CheckParaRange(DRIVER3_PARA_BLOCK);			// 进行参数范围检测
#endif

#if (DRIVERS_NUM >= 4)
	SetNorParaList(DRIVER4_PARA_BLOCK, (void*)&g_defDriverParaList);				// 设置参数列表
	SetNorParaNameString(DRIVER4_PARA_BLOCK, (void*)&g_driverParaStrList);			// 设置参数名称列表

	if (GetNorParaEn(DRIVER4_PARA_BLOCK) != PARA_VALID)		// 参数无效,使用默认参数
	{
		printf("driver 4 load default para\r\n");
		LoadNorDefPara(DRIVER4_PARA_BLOCK);
		if (DEF_DRV4_CTRLMODE == CTRLMODE_OPEN || DEF_DRV4_CTRLMODE == CTRLMODE_CLOSE)
		{
			GetDriverParas(3)->closeCtrlMode = DEF_DRV4_CTRLMODE;
		}
		GetDriverParas(3)->stepPerCircle = DEF_DRV4_STEPCIRCLE;
#ifdef DEF_DRV4_CLOSECIRCLE
		GetDriverParas(3)->closeCircle = DEF_DRV4_CLOSECIRCLE;
#endif
#ifdef DEF_DRV4_KP
		GetDriverParas(3)->kp = DEF_DRV4_KP;
#endif
#ifdef DEF_DRV4_KI
		GetDriverParas(3)->ki = DEF_DRV4_KI;
#endif
		SaveNorParas(DRIVER4_PARA_BLOCK);
	}

	if (GetDriverParas(3)->stepPerCircle != DEF_DRV4_STEPCIRCLE ||
			GetDriverParas(3)->closeCtrlMode != DEF_DRV4_CTRLMODE ||
			0)
	{
		GetDriverParas(3)->stepPerCircle = DEF_DRV4_STEPCIRCLE;
		GetDriverParas(3)->closeCtrlMode = DEF_DRV4_CTRLMODE;
		SaveNorParas(DRIVER4_PARA_BLOCK);
	}

	CheckParaRange(DRIVER4_PARA_BLOCK);			// 进行参数范围检测
#endif

#else

#if (DRIVERS_NUM >= 1)
	{
		u32 * pDat = (u32 *)&g_defPara[0];
		for (int i = 0; i < DRV_PARA_NUMS; i++)
		{
			*pDat = g_defDriverParaList[i][2];
			pDat++;
		}
		g_defPara[0].closeCtrlMode = DEF_DRV1_CTRLMODE;
		g_defPara[0].stepPerCircle = DEF_DRV1_STEPCIRCLE;
#ifdef DEF_DRV1_CLOSECIRCLE
		g_defPara[0].closeCircle = DEF_DRV1_CLOSECIRCLE;
#endif
#ifdef DEF_DRV1_KP
		g_defPara[0].kp = DEF_DRV1_KP;
#endif
#ifdef DEF_DRV1_KI
		g_defPara[0].ki = DEF_DRV1_KI;
#endif
	}
#endif

#if (DRIVERS_NUM >= 2)
	{
		u32 * pDat = (u32 *)&g_defPara[1];
		for (int i = 0; i < DRV_PARA_NUMS; i++)
		{
			*pDat = g_defDriverParaList[i][2];
			pDat++;
		}
		g_defPara[1].closeCtrlMode = DEF_DRV2_CTRLMODE;
		g_defPara[1].stepPerCircle = DEF_DRV2_STEPCIRCLE;
#ifdef DEF_DRV2_CLOSECIRCLE
		g_defPara[1].closeCircle = DEF_DRV2_CLOSECIRCLE;
#endif
#ifdef DEF_DRV2_KP
		g_defPara[1].kp = DEF_DRV2_KP;
#endif
#ifdef DEF_DRV2_KI
		g_defPara[1].ki = DEF_DRV2_KI;
#endif
	}
#endif

#if (DRIVERS_NUM >= 3)
	{
		u32 * pDat = (u32 *)&g_defPara[2];
		for (int i = 0; i < DRV_PARA_NUMS; i++)
		{
			*pDat = g_defDriverParaList[i][2];
			pDat++;
		}
		g_defPara[2].closeCtrlMode = DEF_DRV3_CTRLMODE;
		g_defPara[2].stepPerCircle = DEF_DRV3_STEPCIRCLE;
#ifdef DEF_DRV3_CLOSECIRCLE
		g_defPara[2].closeCircle = DEF_DRV3_CLOSECIRCLE;
#endif
#ifdef DEF_DRV3_KP
		g_defPara[2].kp = DEF_DRV3_KP;
#endif
#ifdef DEF_DRV3_KI
		g_defPara[2].ki = DEF_DRV3_KI;
#endif
	}
#endif

#if (DRIVERS_NUM >= 4)
	{
		u32 * pDat = (u32 *)&g_defPara[3];
		for (int i = 0; i < DRV_PARA_NUMS; i++)
		{
			*pDat = g_defDriverParaList[i][2];
			pDat++;
		}
		g_defPara[3].closeCtrlMode = DEF_DRV4_CTRLMODE;
		g_defPara[3].stepPerCircle = DEF_DRV4_STEPCIRCLE;
#ifdef DEF_DRV4_CLOSECIRCLE
		g_defPara[3].closeCircle = DEF_DRV4_CLOSECIRCLE;
#endif
#ifdef DEF_DRV4_KP
		g_defPara[3].kp = DEF_DRV4_KP;
#endif
#ifdef DEF_DRV4_KI
		g_defPara[3].ki = DEF_DRV4_KI;
#endif
	}
#endif

#endif

}

DriverPara * GetDriverParas(int didx)
{
#if (DRIVER_PARA == DRIVER_PARA_NOR)

#if (DRIVERS_NUM >= 1)
	if (didx == 0)
	{
		return (DriverPara *)GetNorParaBlock(DRIVER1_PARA_BLOCK);
	}
	else
#endif

#if (DRIVERS_NUM >= 2)
	if (didx == 1)
	{
		return (DriverPara *)GetNorParaBlock(DRIVER2_PARA_BLOCK);
	}
	else
#endif
#if (DRIVERS_NUM >= 3)
	if (didx == 2)
	{
		return (DriverPara *)GetNorParaBlock(DRIVER3_PARA_BLOCK);
	}
	else
#endif
#if (DRIVERS_NUM >= 4)
	if (didx == 3)
	{
		return (DriverPara *)GetNorParaBlock(DRIVER4_PARA_BLOCK);
	}
	else
#endif

#else
	if (didx >= 0 && didx < DRIVERS_NUM)
	{
		return (DriverPara *)(&g_defPara[didx]);
	}
	else
#endif
	{
		return NULL;
	}
}

void SaveDriverParas(int didx)
{
#if (DRIVER_PARA == DRIVER_PARA_NOR)

#if (DRIVERS_NUM >= 1)
	if (didx == 0)
	{
		SaveNorParas(DRIVER1_PARA_BLOCK);
	}
	else
#endif

#if (DRIVERS_NUM >= 2)
	if (didx == 1)
	{
		SaveNorParas(DRIVER2_PARA_BLOCK);
	}
	else
#endif
#if (DRIVERS_NUM >= 3)
	if (didx == 2)
	{
		SaveNorParas(DRIVER3_PARA_BLOCK);
	}
	else
#endif
#if (DRIVERS_NUM >= 4)
	if (didx == 3)
	{
		SaveNorParas(DRIVER4_PARA_BLOCK);
	}
	else
#endif

#else
#endif
	{
	}
}

const char * GetDriverParaName(int pidx)
{
	if (pidx >= 0 && pidx < DRV_PARA_NUMS)
	{
		return (const char*)&(g_driverParaStrList[pidx]);
	}
	else
	{
		return NULL;
	}
}

#endif
