pit是定時器中斷,每經過一段規定的時間發生中斷,其中中斷服務函數寫在isr.c檔案中,中斷優先級規定寫在isr_config.h中
zf_ccu6_pit.c
//-------------------------------------------------------------------------------------------------------------------
// @brief pit初始化
// @param ccu6n 選擇CCU6子產品(CCU6_0、CCU6_1)
// @param pit_ch 選擇通道(PIT_CH0、PIT_CH1)
// @param time 周期時間
// @return void
// @note 請使用.h檔案中 帶時間機關的宏定義函數
// Sample usage: pit_init(CCU6_0, PIT_CH0, 5000); //設定周期中斷5000us
//-------------------------------------------------------------------------------------------------------------------
void pit_init(CCU6N_enum ccu6n, CCU6_CHN_enum pit_ch, uint32 time)
{
uint8 i;
volatile Ifx_CCU6 *module;
uint64 timer_input_clk;
IfxCcu6_Timer g_Ccu6Timer;
IfxCcu6_Timer_Config timerConfig;
uint32 timer_period;
boolean interrupt_state = disableInterrupts();
module = IfxCcu6_getAddress((IfxCcu6_Index)ccu6n);
IfxCcu6_Timer_initModuleConfig(&timerConfig, module);
timer_input_clk = IfxScuCcu_getSpbFrequency();
i = 0;
while(i<16)
{
timer_period = (uint32)(timer_input_clk * time / 1000000);
if(timer_period < 0xffff) break;
timer_input_clk >>= 1;
i++;
}
if(16 <= i) IFX_ASSERT(IFX_VERBOSE_LEVEL_ERROR, FALSE);
switch(ccu6n)
{
case CCU6_0:
{
if(PIT_CH0 == pit_ch)
{
timerConfig.interrupt1.typeOfService = CCU6_0_CH0_INT_SERVICE;
timerConfig.interrupt1.priority = CCU6_0_CH0_ISR_PRIORITY;
}
else
{
timerConfig.interrupt2.typeOfService = CCU6_0_CH1_INT_SERVICE;
timerConfig.interrupt2.priority = CCU6_0_CH1_ISR_PRIORITY;
}
}break;
case CCU6_1:
{
if(PIT_CH0 == pit_ch)
{
timerConfig.interrupt1.typeOfService = CCU6_1_CH0_INT_SERVICE;
timerConfig.interrupt1.priority = CCU6_1_CH0_ISR_PRIORITY;
}
else
{
timerConfig.interrupt2.typeOfService = CCU6_1_CH1_INT_SERVICE;
timerConfig.interrupt2.priority = CCU6_1_CH1_ISR_PRIORITY;
}
}break;
}
if(PIT_CH0 == pit_ch)
{
timerConfig.timer = IfxCcu6_TimerId_t12;
timerConfig.interrupt1.source = IfxCcu6_InterruptSource_t12PeriodMatch;
timerConfig.interrupt1.serviceRequest = IfxCcu6_ServiceRequest_1;
timerConfig.base.t12Period = timer_period;
timerConfig.base.t12Frequency = (float)timer_input_clk;
timerConfig.clock.t12countingInputMode = IfxCcu6_CountingInputMode_internal;
}
else
{
timerConfig.timer = IfxCcu6_TimerId_t13;
timerConfig.interrupt2.source = IfxCcu6_InterruptSource_t13PeriodMatch;
timerConfig.interrupt2.serviceRequest = IfxCcu6_ServiceRequest_2;
timerConfig.base.t13Period = timer_period;
timerConfig.base.t13Frequency = (float)timer_input_clk;
timerConfig.clock.t13countingInputMode = IfxCcu6_CountingInputMode_internal;
}
timerConfig.timer12.counterValue = 0;
timerConfig.timer13.counterValue = 0;
timerConfig.trigger.t13InSyncWithT12 = FALSE;
IfxCcu6_Timer_initModule(&g_Ccu6Timer, &timerConfig);
restoreInterrupts(interrupt_state);
IfxCcu6_setSuspendMode(module, IfxCcu6_SuspendMode_hard);
IfxCcu6_Timer_start(&g_Ccu6Timer);
}
//-------------------------------------------------------------------------------------------------------------------
// @brief pit關閉
// @param ccu6n 選擇CCU6子產品(CCU6_0、CCU6_1)
// @param pit_ch 選擇通道(PIT_CH0、PIT_CH1)
// @return void
// @note
// Sample usage: pit_close(CCU6_0, PIT_CH0); //關閉CCU60 通道0的計時器
//-------------------------------------------------------------------------------------------------------------------
void pit_close(CCU6N_enum ccu6n, CCU6_CHN_enum pit_ch)
{
volatile Ifx_CCU6 *module;
IfxCcu6_Timer g_Ccu6Timer;
module = IfxCcu6_getAddress((IfxCcu6_Index)ccu6n);
g_Ccu6Timer.ccu6 = module;
g_Ccu6Timer.timer = (IfxCcu6_TimerId)(pit_ch);
IfxCcu6_Timer_stop(&g_Ccu6Timer);
}
//-------------------------------------------------------------------------------------------------------------------
// @brief pit開始
// @param ccu6n 選擇CCU6子產品(CCU6_0、CCU6_1)
// @param pit_ch 選擇通道(PIT_CH0、PIT_CH1)
// @return void
// @note
// Sample usage: pit_start(CCU6_0, PIT_CH0); //打開CCU60 通道0的計時器
//-------------------------------------------------------------------------------------------------------------------
void pit_start(CCU6N_enum ccu6n, CCU6_CHN_enum pit_ch)
{
volatile Ifx_CCU6 *module;
IfxCcu6_Timer g_Ccu6Timer;
module = IfxCcu6_getAddress((IfxCcu6_Index)ccu6n);
g_Ccu6Timer.ccu6 = module;
g_Ccu6Timer.timer = (IfxCcu6_TimerId)(pit_ch);
IfxCcu6_Timer_start(&g_Ccu6Timer);
}
//-------------------------------------------------------------------------------------------------------------------
// @brief 禁止pit中斷
// @param ccu6n 選擇CCU6子產品(CCU6_0、CCU6_1)
// @param pit_ch 選擇通道(PIT_CH0、PIT_CH1)
// @return void
// @note
// Sample usage: pit_disable_interrupt(CCU6_0, PIT_CH0); //禁止CCU60 通道0的中斷
//-------------------------------------------------------------------------------------------------------------------
void pit_disable_interrupt(CCU6N_enum ccu6n, CCU6_CHN_enum pit_ch)
{
volatile Ifx_CCU6 *module;
module = IfxCcu6_getAddress((IfxCcu6_Index)ccu6n);
IfxCcu6_disableInterrupt(module, pit_ch * 2 + 7);
}
//-------------------------------------------------------------------------------------------------------------------
// @brief 使能pit中斷
// @param ccu6n 選擇CCU6子產品(CCU6_0、CCU6_1)
// @param pit_ch 選擇通道(PIT_CH0、PIT_CH1)
// @return void
// @note
// Sample usage: pit_enable_interrupt(CCU6_0, PIT_CH0); //開啟CCU60 通道0的中斷
//-------------------------------------------------------------------------------------------------------------------
void pit_enable_interrupt(CCU6N_enum ccu6n, CCU6_CHN_enum pit_ch)
{
volatile Ifx_CCU6 *module;
module = IfxCcu6_getAddress((IfxCcu6_Index)ccu6n);
IfxCcu6_enableInterrupt(module, pit_ch * 2 + 7);
}
主函數:
int core0_main(void)
{
disableInterrupts();
get_clk();//擷取時鐘頻率 務必保留
//使用者在此處調用各種初始化函數等
//使用CCU6_0子產品的通道0 産生一個 100ms的周期中斷
pit_interrupt_ms(CCU6_0, PIT_CH0, 100);
//中斷函數在isr.c中 函數名稱為cc60_pit_ch0_isr
//中斷相關的配置參數在isr_config.h内
//可配置參數有 CCU6_0_CH0_INT_SERVICE 和 CCU6_0_CH0_ISR_PRIORITY
//CCU6_0_CH0_INT_SERVICE 中斷服務者
//CCU6_0_CH0_ISR_PRIORITY 中斷優先級 優先級範圍1-255 越大優先級越高 與平時使用的單片機不一樣
//需要特備注意的是 不可以有優先級相同的中斷函數 每個中斷的優先級都必須是不一樣的
enableInterrupts();
while (TRUE)
{
//程式運作之後 PIT中斷每執行一次就會列印一次
//将結果通過序列槽列印,可以先學習printf例程,了解如何使用printf
}
}
中斷服務函數:
uint16 time;
IFX_INTERRUPT(cc60_pit_ch0_isr, 0, CCU6_0_CH0_ISR_PRIORITY)
{
enableInterrupts();//開啟中斷嵌套
PIT_CLEAR_FLAG(CCU6_0, PIT_CH0);
time++;
printf("pit count: %d\n", time);
}
結果是每隔100ms将times(即發生中斷的次數)輸出