1、STM簡介
1.1、作用
STM是為需要高精度和長周期的CPU系統(tǒng)定時(shí)應(yīng)用而設(shè)計(jì)的。
1.2、特征
自由運(yùn)行的64位計(jì)數(shù)器;
所有64位都可以同步讀??;
64位計(jì)數(shù)器的不同的32位部分可以被同步讀取;
基于與部分STM內(nèi)容的比較匹配的靈活的服務(wù)請(qǐng)求生成;
在應(yīng)用程序重置后,將自動(dòng)開始計(jì)數(shù);
STM寄存器由應(yīng)用程序重置,如果位錯(cuò)誤。STMxDIS已被清除。如果有點(diǎn)了。STMxDIS設(shè)置完畢,STM寄存器不會(huì)通過應(yīng)用程序重置重置,而是通過系統(tǒng)重置重置。
1.3、STM模塊框圖
擁有STM0~STM6 七個(gè)通用定時(shí)器;STM_CMP0、STM_CMP1兩個(gè)比較定時(shí)器和一個(gè)STM_CAP定時(shí)器。

在這里插入圖片描述
功能描述:
STM是一個(gè)向上的計(jì)數(shù)器,運(yùn)行頻率為fstm。如果應(yīng)用程序重置,STM重置。STMxDIS已被清除。重置后,STM被啟用,并立即開始計(jì)算。在正常操作過程中,不可能影響定時(shí)器的內(nèi)容。定時(shí)器寄存器只能被讀取而不能被寫入。
STM可以選擇禁用,也可以為調(diào)試而暫停。在掛起模式下,STM時(shí)鐘已停止,但所有寄存器仍然可讀。
STM也可以從7個(gè)寄存器,TIM0到TIM6,選擇越來越高的32位范圍。這些可以看作是單獨(dú)的32位計(jì)時(shí)器,每個(gè)都有不同的分辨率和定時(shí)范圍。
64位系統(tǒng)計(jì)時(shí)器的內(nèi)容可以與存儲(chǔ)在CMP0和CMP1寄存器中的兩個(gè)比較值的內(nèi)容進(jìn)行比較??梢栽赟TM與CMP0或CMP1寄存器的比較匹配時(shí)生成服務(wù)請(qǐng)求。
2、實(shí)驗(yàn)功能
通過定時(shí)器實(shí)現(xiàn)延時(shí)功能,控制LED翻轉(zhuǎn);
官方驅(qū)動(dòng)提供的函數(shù)接口:
IfxStm_getFrequency 函數(shù):獲取定時(shí)器頻率
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、具體實(shí)現(xiàn)
3.1、新建工程
命名為:TC3X7_STMDelay_Test

在這里插入圖片描述

在這里插入圖片描述
在工程下新建自己的驅(qū)動(dòng)文件夾:My_Driver : HARDWARE、TestAPP

在這里插入圖片描述
添加文件夾路徑Project->Properties

在這里插入圖片描述

在這里插入圖片描述

在這里插入圖片描述

在這里插入圖片描述
在HARDWARE下新建Systm_Time.c和Systm_Time.h

在這里插入圖片描述

在這里插入圖片描述

在這里插入圖片描述

在這里插入圖片描述

在這里插入圖片描述
Systm_Time.c和Systm_Time.h具體實(shí)現(xiàn):
Systm_Time.c
1/*
2 * System_Time.c
3 *
4 * Created on: 2022年7月9日
5 * 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" //定義數(shù)據(jù)類型
13
14typedef enum // 枚舉STM模塊號(hào)
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//------------------------------------以下宏定義用于延時(shí)------------------------------------
29#define systick_delay_ms(stmn, time) systick_delay(stmn, 1000000, time) //設(shè)置延時(shí)時(shí)間 單位ms
30#define systick_delay_us(stmn, time) systick_delay(stmn, time*1000, 1) //設(shè)置延時(shí)時(shí)間 單位us
31#define systick_delay_ns(stmn, time) systick_delay(stmn, time, 1) //設(shè)置延時(shí)時(shí)間 單位ns
32
33//------------------------------------以下宏定義用于獲取當(dāng)前時(shí)間------------------------------------
34#define systick_getval_ms(stmn) systick_getval(stmn)/100000 //獲取當(dāng)前計(jì)時(shí)時(shí)間 單位ms
35#define systick_getval_us(stmn) systick_getval(stmn)/100 //獲取當(dāng)前計(jì)時(shí)時(shí)間 單位us
36#define systick_getval_ns(stmn) systick_getval(stmn)*10 //獲取當(dāng)前計(jì)時(shí)時(shí)間 單位ns
37
38
39#endif /* MY_DRIVER_HARDWARE_SYSTEM_TIME_H_ */
主函數(shù):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); //延時(shí)1S
30
31 }
32}
4、下載驗(yàn)證
可以看到P20.8端口的LED燈實(shí)現(xiàn)1s亮滅;沒有LED等可以用示波器或者萬用表檢測電平。

在這里插入圖片描述
電子發(fā)燒友App

























評(píng)論