文章目录
- 功能:
- interrupt.c文件:
- 简单分析:
- 中断服务函数
- 初始化函数
- interrupt.h 文件:
- 主函数调用
- 测试现象:
功能:
**定时1ms的中断(使用GTM模块的子单元TOM0模块),借助LED检测,分别300ms 500ms间隔闪烁2个LED **
基于iLLD库,跟外设GTM模块的子模块TOM相关的基本都在:
#include "IfxGtm_Tom_Timer.h"
#include "IfxGtm.h"
#include "IfxGtm_Cmu.h"
interrupt.c文件:
#include "Interrupt.h"
#include "IfxGtm_Tom_Timer.h"
#include "IfxPort.h"
#include "Blinky_LED.h"#define ISR_PRIORITY_TOM 9 /* Interrupt priority number */
#define TOM_FREQ 1000.0f /* TOM frequency */short one_sec_flag = 0;IfxGtm_Tom_Timer g_timerDriver; //类似linux文件句柄/* Macro to define the Interrupt Service Routine. */
IFX_INTERRUPT(interruptGtmTom, 0, ISR_PRIORITY_TOM);
/* Interrupt Service Routine of the TOM */
void interruptGtmTom(void)
{static int i,j,one_sec = 0;IfxGtm_Tom_Timer_acknowledgeTimerIrq(&g_timerDriver); /* Clear the timer event */i++;j++;one_sec++;if(i>=300){i = 0;IfxPort_togglePin(LED2); /* Toggle the LED */}if(j>=500){j = 0;IfxPort_togglePin(LED1);}if(one_sec>=1000){one_sec = 0;one_sec_flag = 1;}
}void initGtmTom(void)
{Ifx_GTM *gtm = &MODULE_GTM;/* Step 0: Initialize TOM timer configuration *///初始化TOM的结构体IfxGtm_Tom_Timer_Config timerConfig;IfxGtm_Tom_Timer_initConfig(&timerConfig, &MODULE_GTM);/* Step 1: Enable GTM *///使能GTM模块IfxGtm_enable(gtm);/* Step 2: Set GCLK frequency (global clock) and Set CMU_CLK4 frequency (global clock)*///设置GTM内部CMU的总时钟频率为100MHZ,以及CMU的内部子时钟CMU_CLK0频率为1MHZ(当然可以设置CMU_CLK0-7)这里设置1MHZ是由讲究的,肯定有个范围的IfxGtm_Cmu_setGclkFrequency(gtm, 100000000);IfxGtm_Cmu_setClkFrequency(gtm, IfxGtm_Cmu_Clk_0, 1000000);/* Step 3: Configure FXCLK (flexible clock) *///再将FXCLK的时钟,绑定到CMU_CLK0的下面(就是FXCLK使用的是CMU_CLK0-7哪个时钟)timerConfig.gtm->CMU.FXCLK.CTRL.B.FXCLK_SEL = 1; //1代表使用的是CMU_CLK0 2代表使用CMU_CLK1,以此类推timerConfig.base.frequency = 1000.0f; // Set timer frequency to 1 kHztimerConfig.base.isrPriority = ISR_PRIORITY_TOM; // Set interrupt prioritytimerConfig.base.isrProvider = IfxSrc_Tos_cpu0; // Set interrupt provider 设置cpu0 处理1khz的中断timerConfig.tom = IfxGtm_Tom_0; // Use TOM_0timerConfig.timerChannel = IfxGtm_Tom_Ch_0; // Use channel 0timerConfig.clock = IfxGtm_Tom_Ch_ClkSrc_cmuFxclk0; // 这里TOM是使用FXCLK时钟的分频时钟作为TOM输入时钟,这里参数代表对FXCLK时钟是否分频/* Step 5: Enable the required clocks *///使能FXCLK和CMU_CLK0的时钟IfxGtm_Cmu_enableClocks(gtm, IFXGTM_CMU_CLKEN_FXCLK |IFXGTM_CMU_CLKEN_CLK0);/* Step 6: Initialize and start the TOM timer *///初始化TOM模块,以及开启定时器计数IfxGtm_Tom_Timer_init(&g_timerDriver, &timerConfig);IfxGtm_Tom_Timer_run(&g_timerDriver);
}
简单分析:
中断服务函数
IFX_INTERRUPT(interruptGtmTom, 0, ISR_PRIORITY_TOM);
void interruptGtmTom(void)
{
}
TC334系列中断服务函数都是这么写的,IFX_INTERRUPT(中断服务函数名字,0,中断优先级)
然后里面就是精准定时LED闪烁的时间一个是500ms 一个 是300ms 闪烁,以及一个1s精准定时
初始化函数
初始化一个GTM模块的TOM模块配置参数的结构体,然后设置GTM内部管理时钟的CMU的时钟为100MHZ,然后配置CMU管理的子时钟CMU_CLK0为1MHZ,再将FXCLK绑定到CMU_CLK0上,然后设置FXCLK不分频给到TOM,最后初始化TOM,启动TOM。
IfxGtm_Cmu_setGclkFrequency(gtm, 100000000);
这个设置gtm内部CMU的时钟,有说法的,查看手册可以知道最大最小范围。同样设置CMU_CLK0也是有说法的。
IfxGtm_Cmu_setClkFrequency(gtm, IfxGtm_Cmu_Clk_0, 1000000);
这里时钟配置可以理解为STM32那个CUBE_MAX生成代码的时候,自动配置的,但是要配置在合理范围。
将FXCLK的时钟,绑定到CMU_CLK0的下面(就是FXCLK使用的是CMU_CLK0-7哪个时钟)
timerConfig.gtm->CMU.FXCLK.CTRL.B.FXCLK_SEL = 1; //1代表使用的是CMU_CLK0 2代表使用CMU_CLK1,
根据手册查看:
这里TOM是使用FXCLK时钟的分频时钟作为TOM输入时钟,这里参数代表对FXCLK时钟是否分频
timerConfig.clock = IfxGtm_Tom_Ch_ClkSrc_cmuFxclk0; // 这里TOM是使用FXCLK时钟的分频时钟作为TOM输入时钟,这里参数代表对FXCLK时钟是否分频
interrupt.h 文件:
#ifndef __INTERRUPT_H__
#define __INTERRUPT_H__void initGtmTom(void);
extern short one_sec_flag ;#endif
主函数调用
#include "Ifx_Types.h"
#include "IfxCpu.h"
#include "IfxScuWdt.h"#include "Blinky_LED.h"
#include "Interrupt.h"IFX_ALIGN(4) IfxCpu_syncEvent g_cpuSyncEvent = 0;void core0_main(void)
{IfxCpu_enableInterrupts();/* !!WATCHDOG0 AND SAFETY WATCHDOG ARE DISABLED HERE!!* Enable the watchdogs and service them periodically if it is required*/IfxScuWdt_disableCpuWatchdog(IfxScuWdt_getCpuWatchdogPassword());IfxScuWdt_disableSafetyWatchdog(IfxScuWdt_getSafetyWatchdogPassword());/* Wait for CPU sync event */IfxCpu_emitEvent(&g_cpuSyncEvent);IfxCpu_waitEvent(&g_cpuSyncEvent, 1);IfxCpu_enableInterrupts(); /* Enable interrupts after initialization */initLED();initGtmTom();while(1){}
}
LED是上一章节的内容,请回看:(三)点亮LED
测试现象:
LED1,2分别以300ms 500ms间隔闪烁。