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

embedded/2025/2/7 8:17:46/

文章目录

  • 概述
    • 其他例程
  • 代码分析
    • 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                        */
}

http://www.ppmy.cn/embedded/160245.html

相关文章

【wiki知识库】07.用户管理后端SpringBoot部分

目录 一、今日目标 二、&#x1f388;SpringBoot部分类的添加 2.1 使用逆向工程新增User模块 2.2 UserQueryParam添加 2.3 UserSaveParam添加 2.4 UserResetPasswordParam添加 2.5 UserQueryVo添加 2.6 SnowFlake工具类 三、&#x1f686;后端新增接口 3.1 /user/li…

Spring Boot中使用MockMvc测试PATCH请求控制器

在Spring Boot项目中&#xff0c;对控制器进行单元测试是确保代码质量和功能正确性的重要环节。本文将通过一个具体的例子&#xff0c;展示如何使用Spring的MockMvc框架来测试处理PATCH请求的控制器。 示例项目结构 假设我们有一个简单的Spring Boot项目&#xff0c;其中包含一…

每日Attention学习22——Inverted Residual RWKV

模块出处 [arXiv 25] [link] [code] RWKV-UNet: Improving UNet with Long-Range Cooperation for Effective Medical Image Segmentation 模块名称 Inverted Residual RWKV (IR-RWKV) 模块作用 用于vision的RWKV结构 模块结构 模块代码 注&#xff1a;cpp扩展请参考作者原…

npm中央仓库

1、官网地址 npm | Home 2、搜索依赖包

五十天精通硬件设计第六天-放大器知识及选型

系列文章传送门-总体规划: 50天精通硬件设计第一天-总体规划-CSDN博客 目录 一、运算放大器的基本知识 1. **基本结构**: 2. **主要参数**: 3. **理想运放特性**: 二、运算放大器的选型要点 1. **电源电压范围**: 2. **输入失调电压和电流**: 3. **带宽和压摆率…

word数学模式公式显示不全

1.调整段落间距&#xff0c;更换行距 2.调整单个公式的内部位置 右键公式---字体-----

C++类嵌套

文章目录 1. 基本语法2. 类嵌套的访问控制(1) 嵌套类是公共的(2) 嵌套类是私有的(3) 嵌套类是保护的 3. 嵌套类的作用(1) 增强封装性(2) 限制对类的访问(3) 增强层次化结构 4. 嵌套类与静态成员5. 嵌套类的特殊用法&#xff1a;友元类总结 在 C 中&#xff0c;类嵌套指的是一个…

深入了解 MySQL:从基础到高级特性

引言 在当今数字化时代&#xff0c;数据的存储和管理至关重要。MySQL 作为一款广泛使用的开源关系型数据库管理系统&#xff08;RDBMS&#xff09;&#xff0c;凭借其高性能、可靠性和易用性&#xff0c;成为众多开发者和企业的首选。本文将详细介绍 MySQL 的基础概念、安装启…