/*********************************************************************************************************************
* RT1064DVL6A Opensourec Library RT1064DVL6A Դ⣩һڹٷ SDK ӿڵĵԴ
* Copyright (c) 2022 SEEKFREE ɿƼ
* 
* ļ RT1064DVL6A Դһ
* 
* RT1064DVL6A Դ 
* Ըᷢ GPLGNU General Public License GNUͨù֤
*  GPL ĵ3棨 GPL3.0ѡģκκİ汾·/޸
* 
* Դķϣܷãδκεı֤
* ûԻʺض;ı֤
* ϸμ GPL
* 
* ӦյԴͬʱյһ GPL ĸ
* ûУ<https://www.gnu.org/licenses/>
* 
* ע
* Դʹ GPL3.0 Դ֤Э Ϊİ汾
* Ӣİ libraries/doc ļµ GPL3_permission_statement.txt ļ
* ֤ libraries ļ ļµ LICENSE ļ
* ӭλʹò ޸ʱ뱣ɿƼİȨ
* 
* ļ          zf_driver_pit
* ˾          ɶɿƼ޹˾
* 汾Ϣ          鿴 libraries/doc ļ version ļ 汾˵
*           IAR 8.32.4 or MDK 5.33
* ƽ̨          RT1064DVL6A
*           https://seekfree.taobao.com/
* 
* ޸ļ¼
*                               ע
* 2022-09-21        SeekFree            first version
********************************************************************************************************************/

#include "zf_common_clock.h"
#include "zf_common_debug.h"
#include "zf_common_interrupt.h"


#include "zf_driver_pit.h"

//-------------------------------------------------------------------------------------------------------------------
//      PIT жʹ
// ˵     pit_chn             PIT ģ
// ز     void
// ʹʾ     pit_enable(PIT_CH0);
// עϢ     
//-------------------------------------------------------------------------------------------------------------------
void pit_enable (pit_index_enum pit_chn)
{
    PIT_StartTimer(PIT, (pit_chnl_t)pit_chn);
}

//-------------------------------------------------------------------------------------------------------------------
//      PIT жϽֹ
// ˵     pit_chn             PIT ģ
// ز     void
// ʹʾ     pit_disable(PIT_CH0);
// עϢ     
//-------------------------------------------------------------------------------------------------------------------
void pit_disable (pit_index_enum pit_chn)
{
    PIT_StopTimer(PIT, (pit_chnl_t)pit_chn);
}

//-------------------------------------------------------------------------------------------------------------------
//      PIT ʼ һ pit_ms_init  pit_us_init
// ˵     pit_chn             PIT ģ
// ˵     period              PIT  һоƬģʱƵʼ
// ز     void
// ʹʾ     pit_init(PIT_CH0, 120);
// עϢ     
//-------------------------------------------------------------------------------------------------------------------
void pit_init (pit_index_enum pit_chn, uint32 period)
{
    static uint8 init_flag;
    
    if(0 == init_flag)
    {
        init_flag = 1;
        pit_config_t pitConfig;
    
        PIT_GetDefaultConfig(&pitConfig);   //ĬΪfalse
                  
        PIT_Init(PIT, &pitConfig);          //һγʼڴʱ
        PIT_Deinit(PIT);                    //λ
        PIT_Init(PIT, &pitConfig);          //³ʼȷĲ
    }
    
    
    PIT_SetTimerPeriod(PIT, (pit_chnl_t)pit_chn, period);
    PIT_EnableInterrupts(PIT, (pit_chnl_t)pit_chn, kPIT_TimerInterruptEnable);//PITͨ0ж
	PIT_SetTimerChainMode(PIT, (pit_chnl_t)pit_chn, false);
    PIT_StartTimer(PIT, (pit_chnl_t)pit_chn);
    EnableIRQ(PIT_IRQn);
}
