[其他ST产品] AURIX-TC3X7:系统定时器-STM实验

[复制链接]
1237|18
 楼主| 过期的塔头 发表于 2023-11-24 11:01 | 显示全部楼层 |阅读模式
1、STM简介
1.1、作用
STM是为需要高精度和长周期的CPU系统定时应用而设计的。

1.2、特征
自由运行的64位计数器;

所有64位都可以同步读取;

64位计数器的不同的32位部分可以被同步读取;

基于与部分STM内容的比较匹配的灵活的服务请求生成;

在应用程序重置后,将自动开始计数;

STM寄存器由应用程序重置,如果位错误。STMxDIS已被清除。如果有点了。STMxDIS设置完毕,STM寄存器不会通过应用程序重置重置,而是通过系统重置重置。
1.3、STM模块框图

拥有STM0~STM6 七个通用定时器;STM_CMP0、STM_CMP1两个比较定时器和一个STM_CAP定时器。

96976656011f9475ee.png


 楼主| 过期的塔头 发表于 2023-11-24 11:01 | 显示全部楼层
功能描述:

STM是一个向上的计数器,运行频率为fstm。如果应用程序重置,STM重置。STMxDIS已被清除。重置后,STM被启用,并立即开始计算。在正常操作过程中,不可能影响定时器的内容。定时器寄存器只能被读取而不能被写入。

STM可以选择禁用,也可以为调试而暂停。在挂起模式下,STM时钟已停止,但所有寄存器仍然可读。

STM也可以从7个寄存器,TIM0到TIM6,选择越来越高的32位范围。这些可以看作是单独的32位计时器,每个都有不同的分辨率和定时范围。

64位系统计时器的内容可以与存储在CMP0和CMP1寄存器中的两个比较值的内容进行比较。可以在STM与CMP0或CMP1寄存器的比较匹配时生成服务请求。
 楼主| 过期的塔头 发表于 2023-11-24 11:01 | 显示全部楼层
2、实验功能
通过定时器实现延时功能,控制LED翻转;

官方驱动提供的函数接口:

IfxStm_getFrequency 函数:获取定时器频率

IFX_INLINE float32 IfxStm_getFrequency(Ifx_STM *stm)
{
    IFX_UNUSED_PARAMETER(stm);
    float32 result;

    result = IfxScuCcu_getStmFrequency();

    return result;
}
 楼主| 过期的塔头 发表于 2023-11-24 11:02 | 显示全部楼层
3、具体实现
3.1、新建工程
命名为:TC3X7_STMDelay_Test
5681865601228c68ae.png
 楼主| 过期的塔头 发表于 2023-11-24 11:02 | 显示全部楼层
 楼主| 过期的塔头 发表于 2023-11-24 11:02 | 显示全部楼层
在工程下新建自己的驱动文件夹:My_Driver : HARDWARE、TestAPP

255976560123fc6a4c.png
 楼主| 过期的塔头 发表于 2023-11-24 11:02 | 显示全部楼层
添加文件夹路径Project->Properties
756226560124cc149a.png
 楼主| 过期的塔头 发表于 2023-11-24 11:02 | 显示全部楼层
 楼主| 过期的塔头 发表于 2023-11-24 11:02 | 显示全部楼层
 楼主| 过期的塔头 发表于 2023-11-24 11:03 | 显示全部楼层
 楼主| 过期的塔头 发表于 2023-11-24 11:03 | 显示全部楼层
在HARDWARE下新建Systm_Time.c和Systm_Time.h

2599565601272cd5fc.png
 楼主| 过期的塔头 发表于 2023-11-24 11:03 | 显示全部楼层
 楼主| 过期的塔头 发表于 2023-11-24 11:03 | 显示全部楼层
 楼主| 过期的塔头 发表于 2023-11-24 11:03 | 显示全部楼层
 楼主| 过期的塔头 发表于 2023-11-24 11:03 | 显示全部楼层
 楼主| 过期的塔头 发表于 2023-11-24 11:03 | 显示全部楼层
Systm_Time.c和Systm_Time.h具体实现:

Systm_Time.c
  1. /*
  2. * System_Time.c
  3. *
  4. *  Created on: 2022年7月9日
  5. *      Author: Kevin
  6. */

  7. #include "System_Time.h"
  8. #include "IfxStm.h"

  9. void systick_delay(STMN_enum stmn, uint32 time, uint32 num)
  10. {
  11.     uint32 stm_clk;
  12.     uint32 delay_time;
  13.     stm_clk = IfxStm_getFrequency(IfxStm_getAddress((IfxStm_Index)stmn));
  14.     delay_time = (uint32)(stm_clk/1000000*time/1000);

  15.     while(num--)
  16.     {
  17.         IfxStm_waitTicks(IfxStm_getAddress((IfxStm_Index)stmn), delay_time);
  18.     }

  19. }
 楼主| 过期的塔头 发表于 2023-11-24 11:04 | 显示全部楼层
Systm_Time.h

  1. /*
  2. * System_Time.h
  3. *
  4. *  Created on: 2022年7月9日
  5. *      Author: Kevin
  6. */

  7. #ifndef MY_DRIVER_HARDWARE_SYSTEM_TIME_H_
  8. #define MY_DRIVER_HARDWARE_SYSTEM_TIME_H_


  9. #include "Platform_Types.h"   //定义数据类型

  10. typedef enum  // 枚举STM模块号
  11. {
  12.     STM0,
  13.     STM1,
  14.     STM2,
  15.     STM3,
  16.     STM4,
  17. }STMN_enum;


  18. void systick_delay(STMN_enum stmn, uint32 time, uint32 num);
  19. void systick_start(STMN_enum stmn);
  20. uint32 systick_getval(STMN_enum stmn);

  21. //------------------------------------以下宏定义用于延时------------------------------------
  22. #define systick_delay_ms(stmn, time)    systick_delay(stmn, 1000000, time)  //设置延时时间  单位ms
  23. #define systick_delay_us(stmn, time)    systick_delay(stmn, time*1000, 1)   //设置延时时间  单位us
  24. #define systick_delay_ns(stmn, time)    systick_delay(stmn, time, 1)        //设置延时时间  单位ns

  25. //------------------------------------以下宏定义用于获取当前时间------------------------------------
  26. #define systick_getval_ms(stmn)         systick_getval(stmn)/100000         //获取当前计时时间  单位ms
  27. #define systick_getval_us(stmn)         systick_getval(stmn)/100            //获取当前计时时间  单位us
  28. #define systick_getval_ns(stmn)         systick_getval(stmn)*10             //获取当前计时时间  单位ns


  29. #endif /* MY_DRIVER_HARDWARE_SYSTEM_TIME_H_ */
 楼主| 过期的塔头 发表于 2023-11-24 11:04 | 显示全部楼层
主函数:main.c

  1. #include "Ifx_Types.h"
  2. #include "IfxCpu.h"
  3. #include "IfxScuWdt.h"
  4. #include "System_Time.h"

  5. IFX_ALIGN(4) IfxCpu_syncEvent g_cpuSyncEvent = 0;

  6. void core0_main(void)
  7. {
  8.     IfxCpu_enableInterrupts();
  9.    
  10.     /* !!WATCHDOG0 AND SAFETY WATCHDOG ARE DISABLED HERE!!
  11.      * Enable the watchdogs and service them periodically if it is required
  12.      */
  13.     IfxScuWdt_disableCpuWatchdog(IfxScuWdt_getCpuWatchdogPassword());
  14.     IfxScuWdt_disableSafetyWatchdog(IfxScuWdt_getSafetyWatchdogPassword());
  15.    
  16.     IfxPort_setPinModeOutput(&MODULE_P20,8, IfxPort_OutputMode_pushPull, IfxPort_OutputIdx_general);
  17.     IfxPort_setPinHigh(&MODULE_P20,8);   //Switch OFF the LED (low-level active)

  18.     /* Wait for CPU sync event */
  19.     IfxCpu_emitEvent(&g_cpuSyncEvent);
  20.     IfxCpu_waitEvent(&g_cpuSyncEvent, 1);
  21.    
  22.     while(1)
  23.     {
  24.         IfxPort_togglePin(&MODULE_P20,8);       /* Toggle the state of the LED*/

  25.         systick_delay_ms(STM0, 1000); //延时1S

  26.     }
  27. }
 楼主| 过期的塔头 发表于 2023-11-24 11:04 | 显示全部楼层
4、下载验证
可以看到P20.8端口的LED灯实现1s亮灭;没有LED等可以用示波器或者万用表检测电平。

51574656012c76aeb6.png
您需要登录后才可以回帖 登录 | 注册

本版积分规则

85

主题

999

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部