/******************** (C) COPYRIGHT 2024 SONiX *******************************
* COMPANY:		SONiX
* DATE:				2024/01
* AUTHOR:			SA1
* IC:					SN32F800
*____________________________________________________________________________
*	REVISION	Date				User		Description
*	1.0				2021/07/09	SA1			1. First version released
*																2. Compatible to CMSIS DFP Architecture in Keil MDK v5.X (http://www.keil.com/dd2/pack/)
*																3. Run HexConvert to generate bin file and show checksum after building.
*																4. Set SN_ADC->ADM2 to 3 if DAC VIREF is used.*
*	1.1				2024/01/23	SA1			1. Update system_SN32F800.c
*																2. Update DAC.c     
*
*____________________________________________________________________________
* THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS TIME TO MARKET.
* SONiX SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT OR CONSEQUENTIAL 
* DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT OF SUCH SOFTWARE
* AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION CONTAINED HEREIN 
* IN CONNECTION WITH THEIR PRODUCTS.
*****************************************************************************/

/*_____ I N C L U D E S ____________________________________________________*/
#include <SN32F800.h>
#include <SN32F800_Def.h>
#include ".\Interface\SysTick\SysTick.h"
#include ".\Interface\DAC\DAC.h"
#include ".\Interface\CT\CT16.h"
#include ".\Interface\CT\CT16B0.h"


/*_____ D E C L A R A T I O N S ____________________________________________*/
void PFPA_Init(void);
void NotPinOut_GPIO_init(void);
void DAC_DemoCase0(void);
void DAC_DemoCase1(void);
void DAC_DemoCase2(void);

SDAC_INIT_TYPE sDAC_InitType;

/*_____ D E F I N I T I O N S ______________________________________________*/
#ifndef	SN32F805					//Do NOT Remove or Modify!!!
	#error Please install SONiX.SN32F8_DFP.0.0.3.pack or version >= 0.0.3
#endif
#define	PKG						SN32F805				//User SHALL modify the package on demand (SN32F805)

/*_____ M A C R O S ________________________________________________________*/

/*_____ F U N C T I O N S __________________________________________________*/

/*****************************************************************************
* Function		: main
* Description	: Demo code of DAC
* Input			: None
* Output		: None
* Return		: None
* Note			: None
*****************************************************************************/
int	main(void)
{
	//User can configure System Clock with Configuration Wizard in system_SN32F800.c
	SystemInit();
	SystemCoreClockUpdate();				//Must call for SN32F800, Please do NOT remove!!!

	//Note: User can refer to ClockSwitch sample code to switch various HCLK if needed.

	PFPA_Init();										//User shall set PFPA if used, do NOT remove!!!

	//1. User SHALL define PKG on demand.
	//2. User SHALL set the status of the GPIO which are NOT pin-out to input pull-up.
	NotPinOut_GPIO_init();

	//--------------------------------------------------------------------------
	//User Code starts HERE!!!

	DAC_DemoCase0();	//Use DAC0, trigger source is PCLK 
	DAC_DemoCase1();	//Use DAC1, trigger source is CT16B0 TC matches MR9 
	DAC_DemoCase2();	//Use DAC2, trigger source is software trigger. 
	
	while (1);
}

/*****************************************************************************
* Function		: DAC_DemoCase0
* Description	: Use DAC0 to demo PCLK trigger source
* Input			: None
* Output		: None
* Return		: None
* Note			: None
*****************************************************************************/
void DAC_DemoCase0(void)
{
	sDAC_InitType.bDAC_VHS = DAC_VHS_VIREF;
	sDAC_InitType.bDAC_VIREF = DAC_VIREF_INTERNAL_2P5V;
	sDAC_InitType.bDAC_Clock_Div = mskDAC_DA0CKS_DIV16;
	sDAC_InitType.bDAC_Clock_Source = DAC_DAnTGSEL_PCLK;
	sDAC_InitType.bDAC_IE_Enable = ENABLE;
	
	DAC_Init(DAC_Channel0, &sDAC_InitType);
	DAC_SetData(DAC_Channel0, 0x500);
	DAC_ChannelCtrl(DAC_Channel0, mskDAC_DAO0BUFEN_EN, mskDAC_DAC0EN_EN);
	
}

/*****************************************************************************
* Function		: DAC_DemoCase1
* Description	: Use DAC1 to demo CT16B0 TC matches MR9 trigger source
* Input			: None
* Output		: None
* Return		: None
* Note			: None
*****************************************************************************/
void DAC_DemoCase1(void)
{
	CT16B0_Init();

	sDAC_InitType.bDAC_VHS = DAC_VHS_VIREF;
	sDAC_InitType.bDAC_VIREF = DAC_VIREF_INTERNAL_2P5V;
	sDAC_InitType.bDAC_Clock_Div = mskDAC_DA0CKS_DIV16;
	sDAC_InitType.bDAC_Clock_Source = DAC_DAnTGSEL_CT16B0_TC;
	sDAC_InitType.bDAC_IE_Enable = ENABLE;
	
	DAC_Init(DAC_Channel1, &sDAC_InitType);
	DAC_SetData(DAC_Channel1, 0x500);
	DAC_ChannelCtrl(DAC_Channel1, mskDAC_DAO1BUFEN_EN, mskDAC_DAC1EN_EN);
	
}

/*****************************************************************************
* Function		: DAC_DemoCase2
* Description	: Use DAC2 to demo software trigger source
* Input			: None
* Output		: None
* Return		: None
* Note			: None
*****************************************************************************/
void DAC_DemoCase2(void)
{
	//use systick demo software trigger
	SysTick_Init();
	
	sDAC_InitType.bDAC_VHS = DAC_VHS_VIREF;
	sDAC_InitType.bDAC_VIREF = DAC_VIREF_INTERNAL_2P5V;
	sDAC_InitType.bDAC_Clock_Div = mskDAC_DA0CKS_DIV16;
	sDAC_InitType.bDAC_Clock_Source = DAC_DAnTGSEL_SOFTWARE;
	sDAC_InitType.bDAC_IE_Enable = ENABLE;
	
	DAC_Init(DAC_Channel2, &sDAC_InitType);
	DAC_SetData(DAC_Channel2, 0x500);
	DAC_ChannelCtrl(DAC_Channel2, mskDAC_DAO2BUFEN_EN, mskDAC_DAC2EN_EN);
	
}

/*****************************************************************************
* Function		: NotPinOut_GPIO_init
* Description	: Set the status of the GPIO which are NOT pin-out to input pull-up. 
* Input				: None
* Output			: None
* Return			: None
* Note				: 1. User SHALL define PKG on demand.
*****************************************************************************/
void NotPinOut_GPIO_init(void)
{
#if (PKG == SN32F805)
	//set P0.0~P0.1 to input pull-up
	SN_GPIO0->CFG = 0xAAAAAAA0;
#endif
}

/*****************************************************************************
* Function		: HardFault_Handler
* Description	: ISR of Hard fault interrupt
* Input			: None
* Output		: None
* Return		: None
* Note			: None
*****************************************************************************/
__irq void HardFault_Handler(void)
{
	NVIC_SystemReset();
}
