文章目录
- 概述
- 其他例程
- 代码分析
- IfxGtm_enable()
- IfxGtm_Cmu_enableClocks()
- IfxGtm_Tom_Pwm_initConfig()
- IfxGtm_Tom_Pwm_init()
- IfxGtm_Tom_Pwm_start()
- fadeLED()
概述
目的:呼吸灯,也即用GTM TOM模块输出PWM波控制LED的亮度明暗变化。
官方文档:GTM TOM PWM generation
网友博客:TOM模块的配置以及控制PWM的输出,以及TOM作为ADC模块的触发源,ADC采集到信号后通过DMA发送到寄存器
其他例程
当前适用于TC275 Lite的开发板有以下例程,后期慢慢研究。
代码分析
initGtmTomPwm();
函数用来初始化GTM外设:
/* This function initializes the TOM */
void initGtmTomPwm(void)
{IfxGtm_enable(&MODULE_GTM); /* Enable GTM */IfxGtm_Cmu_enableClocks(&MODULE_GTM, IFXGTM_CMU_CLKEN_FXCLK); /* Enable the FXU clock *//* Initialize the configuration structure with default parameters */IfxGtm_Tom_Pwm_initConfig(&g_tomConfig, &MODULE_GTM);g_tomConfig.tom = LED.tom; /* Select the TOM depending on the LED */g_tomConfig.tomChannel = LED.channel; /* Select the channel depending on the LED */g_tomConfig.period = PWM_PERIOD; /* Set the timer period */g_tomConfig.pin.outputPin = &LED; /* Set the LED port pin as output */g_tomConfig.synchronousUpdateEnabled = TRUE; /* Enable synchronous update */IfxGtm_Tom_Pwm_init(&g_tomDriver, &g_tomConfig); /* Initialize the GTM TOM */IfxGtm_Tom_Pwm_start(&g_tomDriver, TRUE); /* Start the PWM */
}
IfxGtm_enable()
void IfxGtm_enable(Ifx_GTM *gtm)
{uint16 psw = IfxScuWdt_getCpuWatchdogPassword();IfxScuWdt_clearCpuEndinit(psw);gtm->CLC.B.DISR = 0;IfxScuWdt_setCpuEndinit(psw);
}
这个函数比较好理解,核心就是这句gtm->CLC.B.DISR = 0;
,对应了手册中DISR寄存器,赋值为0就是使能。
IfxGtm_Cmu_enableClocks()
这个函数只有一行
void IfxGtm_Cmu_enableClocks(Ifx_GTM *gtm, uint32 clkMask)
{gtm->CMU.CLK_EN.U = clkMask;
}
其中clkMask
的传入参数是#define IFXGTM_CMU_CLKEN_FXCLK (0x00800000)
,也就是第23位是1,即使能固定时钟FXCLK。
IfxGtm_Tom_Pwm_initConfig()
这个就是在配置句柄之前先把句柄初始化。
void IfxGtm_Tom_Pwm_initConfig(IfxGtm_Tom_Pwm_Config *config, Ifx_GTM *gtm)
{config->gtm = gtm;config->tom = IfxGtm_Tom_0;config->tomChannel = IfxGtm_Tom_Ch_0;config->clock = IfxGtm_Tom_Ch_ClkSrc_cmuFxclk0;config->period = 20;config->dutyCycle = 10;config->signalLevel = Ifx_ActiveState_high;config->oneShotModeEnabled = FALSE;config->synchronousUpdateEnabled = FALSE;config->immediateStartEnabled = TRUE;config->interrupt.ccu0Enabled = FALSE;config->interrupt.ccu1Enabled = FALSE;config->interrupt.mode = IfxGtm_IrqMode_pulseNotify;config->interrupt.isrProvider = IfxSrc_Tos_cpu0;config->interrupt.isrPriority = 0;config->pin.outputPin = NULL_PTR;config->pin.outputMode = IfxPort_OutputMode_pushPull;config->pin.padDriver = IfxPort_PadDriver_cmosAutomotiveSpeed1;
}
IfxGtm_Tom_Pwm_init()
这个函数是将用户的配置写入寄存器。
IfxGtm_Tom_Pwm_start()
开启PWM输出。这个函数中又分成了3个子函数。
void IfxGtm_Tom_Pwm_start(IfxGtm_Tom_Pwm_Driver *driver, boolean immediate)
{/* Enable channel if not enabled already */IfxGtm_Tom_Tgc_enableChannel(driver->tgc[0], driver->tomChannel, TRUE, immediate);IfxGtm_Tom_Tgc_enableChannelOutput(driver->tgc[0], driver->tomChannel, TRUE, immediate);/* Trigger the start now */IfxGtm_Tom_Tgc_trigger(driver->tgc[0]);
}
其中IfxGtm_Tom_Tgc_trigger()
函数是操作GLB_CTRL
寄存器,这个寄存器的功能介绍在手册25.11.2 TOM Global Channel Control (TGC0, TGC1)
。也可以参考文章:
- 【学一点英飞凌】AutoSar-MCAL-Gtm-TOM模块
寄存器的描述在手册25.11.8.1 Register TOMi_TGC0_GLB_CTRL
fadeLED()
TOM已经配置完成,接下来周期性的调整占空比即可。
void fadeLED(void)
{if((g_fadeValue + FADE_STEP) >= PWM_PERIOD){g_fadeDir = -1; /* Set the direction of the fade */}else if((g_fadeValue - FADE_STEP) <= 0){g_fadeDir = 1; /* Set the direction of the fade */}g_fadeValue += g_fadeDir * FADE_STEP; /* Calculation of the new duty cycle */setDutyCycle(g_fadeValue); /* Set the duty cycle of the PWM */
}
在这个setDutyCycle()函数中,代码实现居然是重新初始化TOM……
单独配置占空比的寄存器会不会更省资源一些……
void setDutyCycle(uint32 dutyCycle)
{g_tomConfig.dutyCycle = dutyCycle; /* Change the value of the duty cycle */IfxGtm_Tom_Pwm_init(&g_tomDriver, &g_tomConfig); /* Re-initialize the PWM */
}