英飞凌TC3XX系列多核MCU学习笔记(2)

控制/MCU

1869人已加入

描述

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定时器。

定时器
在这里插入图片描述

功能描述:

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

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

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

64位系统计时器的内容可以与存储在CMP0和CMP1寄存器中的两个比较值的内容进行比较。可以在STM与CMP0或CMP1寄存器的比较匹配时生成服务请求。

2、实验功能

通过定时器实现延时功能,控制LED翻转;

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

IfxStm_getFrequency 函数:获取定时器频率

1IFX_INLINE float32 IfxStm_getFrequency(Ifx_STM *stm)
2{
3    IFX_UNUSED_PARAMETER(stm);
4    float32 result;
5
6    result = IfxScuCcu_getStmFrequency();
7
8    return result;
9}

3、具体实现

3.1、新建工程

命名为:TC3X7_STMDelay_Test

定时器
在这里插入图片描述

定时器
在这里插入图片描述

在工程下新建自己的驱动文件夹:My_Driver : HARDWARE、TestAPP

定时器
在这里插入图片描述

添加文件夹路径Project->Properties

定时器
在这里插入图片描述

定时器
在这里插入图片描述

定时器
在这里插入图片描述

定时器
在这里插入图片描述

在HARDWARE下新建Systm_Time.c和Systm_Time.h

定时器
在这里插入图片描述

定时器
在这里插入图片描述

定时器
在这里插入图片描述

定时器
在这里插入图片描述

定时器
在这里插入图片描述

Systm_Time.c和Systm_Time.h具体实现:

Systm_Time.c

1/*
 2 * System_Time.c
 3 *
 4 *  Created on: 2022795 *      Author: Kevin
 6 */
 7
 8#include "System_Time.h"
 9#include "IfxStm.h"
10
11void systick_delay(STMN_enum stmn, uint32 time, uint32 num)
12{
13    uint32 stm_clk;
14    uint32 delay_time;
15    stm_clk = IfxStm_getFrequency(IfxStm_getAddress((IfxStm_Index)stmn));
16    delay_time = (uint32)(stm_clk/1000000*time/1000);
17
18    while(num--)
19    {
20        IfxStm_waitTicks(IfxStm_getAddress((IfxStm_Index)stmn), delay_time);
21    }
22
23}

Systm_Time.h

1/*
 2 * System_Time.h
 3 *
 4 *  Created on: 2022年7月9日
 5 *      Author: Kevin
 6 */
 7
 8#ifndef MY_DRIVER_HARDWARE_SYSTEM_TIME_H_
 9#define MY_DRIVER_HARDWARE_SYSTEM_TIME_H_
10
11
12#include "Platform_Types.h"   //定义数据类型
13
14typedef enum  // 枚举STM模块号
15{
16    STM0,
17    STM1,
18    STM2,
19    STM3,
20    STM4,
21}STMN_enum;
22
23
24void systick_delay(STMN_enum stmn, uint32 time, uint32 num);
25void systick_start(STMN_enum stmn);
26uint32 systick_getval(STMN_enum stmn);
27
28//------------------------------------以下宏定义用于延时------------------------------------
29#define systick_delay_ms(stmn, time)    systick_delay(stmn, 1000000, time)  //设置延时时间  单位ms
30#define systick_delay_us(stmn, time)    systick_delay(stmn, time*1000, 1)   //设置延时时间  单位us
31#define systick_delay_ns(stmn, time)    systick_delay(stmn, time, 1)        //设置延时时间  单位ns
32
33//------------------------------------以下宏定义用于获取当前时间------------------------------------
34#define systick_getval_ms(stmn)         systick_getval(stmn)/100000         //获取当前计时时间  单位ms
35#define systick_getval_us(stmn)         systick_getval(stmn)/100            //获取当前计时时间  单位us
36#define systick_getval_ns(stmn)         systick_getval(stmn)*10             //获取当前计时时间  单位ns
37
38
39#endif /* MY_DRIVER_HARDWARE_SYSTEM_TIME_H_ */

主函数:main.c

1#include "Ifx_Types.h"
 2#include "IfxCpu.h"
 3#include "IfxScuWdt.h"
 4#include "System_Time.h"
 5
 6IFX_ALIGN(4) IfxCpu_syncEvent g_cpuSyncEvent = 0;
 7
 8void core0_main(void)
 9{
10    IfxCpu_enableInterrupts();
11
12    /* !!WATCHDOG0 AND SAFETY WATCHDOG ARE DISABLED HERE!!
13     * Enable the watchdogs and service them periodically if it is required
14     */
15    IfxScuWdt_disableCpuWatchdog(IfxScuWdt_getCpuWatchdogPassword());
16    IfxScuWdt_disableSafetyWatchdog(IfxScuWdt_getSafetyWatchdogPassword());
17
18    IfxPort_setPinModeOutput(&MODULE_P20,8, IfxPort_OutputMode_pushPull, IfxPort_OutputIdx_general);
19    IfxPort_setPinHigh(&MODULE_P20,8);   //Switch OFF the LED (low-level active)
20
21    /* Wait for CPU sync event */
22    IfxCpu_emitEvent(&g_cpuSyncEvent);
23    IfxCpu_waitEvent(&g_cpuSyncEvent, 1);
24
25    while(1)
26    {
27        IfxPort_togglePin(&MODULE_P20,8);       /* Toggle the state of the LED*/
28
29        systick_delay_ms(STM0, 1000); //延时1S
30
31    }
32}

4、下载验证

可以看到P20.8端口的LED灯实现1s亮灭;没有LED等可以用示波器或者万用表检测电平。

定时器
在这里插入图片描述

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分