AURIX TC275学习笔记4 官方GTM例程 GTM_TOM_PWM_1

概述

目的:呼吸灯,也即用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)。也可以参考文章:

寄存器的描述在手册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                        */
}
### 关于GTM_TOM_PWM_1_KIT的技术资料和产品信息 在AutoSAR环境中,Mcal GTM模块中的Tom部分用于处理定时器和计数器的操作。对于特定的套件如GTM_TOM_PWM_1_KIT而言,其主要作用在于提供针对PWM(脉宽调制)功能的支持与实现。 #### 定义配置结构体 为了使能并正确初始化PWM通道,在定义配置结构体时需指定相应的参数集。这些参数通常包括但不限于频率设定、占空比控制以及工作模式的选择等[^1]。 ```c typedef struct { uint8_t channel; /* PWM Channel */ float frequency_Hz; /* Frequency of the PWM signal (in Hz) */ float duty_cycle_percent; /* Duty cycle as a percentage */ } GtmTOM_PwmConfigType; ``` #### 设置寄存器和位字段 接下来是对硬件资源的具体映射——即如何通过软件访问底层物理设备上的寄存器来完成对PWM信号特性的编程。这涉及到多个方面: - **Base Address**: 确定目标外设基地址; - **Register Offsets**: 各类控制/状态寄存器相对于基址的位置偏移量; - **Bit Fields within Registers**: 寄存器内部各位域的意义及其可接受值范围。 例如,要启动某个PWM输出,则可能需要向对应的`CR`(Control Register, 控制寄存器)写入特定命令字节以激活该特性;同时调整其他相关联的寄存器比如周期长度(`PRD`)、比较匹配值(`CMP`)等等,从而精确调控所期望产生的波形特征。 #### 技术文档获取途径 对于更深入的理解及详细的API说明,建议查阅官方发布的《Infineon TriCore Architecture Manual》或是对应系列微控制器的数据手册。此外,《AUTOSAR_SWS_MCALGeneralTimerModule.pdf》也提供了关于通用定时器接口层的一般指导原则,有助于更好地掌握GTM_TOM_PWM_1_KIT的实际应用方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值