[Aurix TC275] System Timer Module (STM)

사이킷·2025년 1월 16일

AURIX MCU

목록 보기
2/13

Ststem Timer Module (STM)을 사용하여
1초 마다 interrupt를 발생시켜 LED를 켰다 껐다 해보겠다.

STM

STM은 MCU내 Clock Control Unit (CCU)에 의해서 100MHz의 Clock Speed로 동작한다.
즉 1초에 100 000 000 번의 Tick이 발생하다.
100,000,000의 Tick이 발생할때 마다 인터럽트를 걸라고 요청하면 1초마다 인터럽트가 발생하게 된다.

STM 초기화 코드

typedef struct
{
    Ifx_STM             *stmSfr;            /**< \brief Pointer to Stm register base */
    IfxStm_CompareConfig stmConfig;         /**< \brief Stm Configuration structure */
    volatile uint8       LedBlink;          /**< \brief LED state variable */
    volatile uint32      counter;           /**< \brief interrupt counter */
} App_Stm;

App_Stm g_Stm; /**< \brief Stm global data */

void IfxStmDemo_init(void)
{
    /* disable interrupts */
    boolean interruptState = IfxCpu_disableInterrupts();

    IfxStm_enableOcdsSuspend(&MODULE_STM0);

    g_Stm.stmSfr = &MODULE_STM0;
    IfxStm_initCompareConfig(&g_Stm.stmConfig);

    g_Stm.stmConfig.triggerPriority = 100u;
    g_Stm.stmConfig.typeOfService   = IfxSrc_Tos_cpu0;
    g_Stm.stmConfig.ticks           = 100000000;

    IfxStm_initCompare(g_Stm.stmSfr, &g_Stm.stmConfig);

    /* enable interrupts again */
    IfxCpu_restoreInterrupts(interruptState);
}

IfxStm_enableOcdsSuspend(&MODULE_STM0)

Debuger로 멈추면 STM이 Count를 멈추게하는 코드라고 한다.

g_Stm.stmConfig.triggerPriority = 100u;

STM 인터럽트 우선순위이다. 255까지 설정가능하며 클수록 우선순위가 높다.

g_Stm.stmConfig.typeOfService = IfxSrc_Tos_cpu0;

인터럽트가 발생하면 어떤 Core에게 인터럽트를 걸지 의미한다.
즉 Core 0 에게 인터럽트를 거는 것이다.

g_Stm.stmConfig.ticks = 100000000;

몇번의 Tick이 발생할때 인터럽트를 걸지 설정하는 변수이다.
STM의 100Mhz 로 동작하므로 100000000번의 Tick마다 인터럽트를 걸면 1초마다 걸리게 된다.

IfxStm_initCompare(g_Stm.stmSfr, &g_Stm.stmConfig);

STM 모듈을 초기화 시키는 함수이다.

인터럽트 소스 코드

IFX_INTERRUPT(STM_Int0Handler,0,100);

void STM_Int0Handler(void)
{
    static int flag = 0;
    static int cnt = 0;

    IfxStm_clearCompareFlag(g_Stm.stmSfr, g_Stm.stmConfig.comparator);
    IfxStm_increaseCompare(g_Stm.stmSfr, g_Stm.stmConfig.comparator, 100000000u);

    cnt++;

    if(flag == 0)
    {
        IfxPort_setPinLow(IfxPort_P00_5.port, IfxPort_P00_5.pinIndex);
        flag = 1;
    }
    else
    {
        IfxPort_setPinHigh(IfxPort_P00_5.port, IfxPort_P00_5.pinIndex);
        flag = 0;
    }

    IfxCpu_enableInterrupts();

}

IFX_INTERRUPT(STM_Int0Handler,0,100);

Interrupt Service Routine (ISR) 을 선언하는 코드이다.

STM_Int0Handler: 인터럽트가 발생하면 호출될 함수 이름이다.
0: 인터럽트 요청 라인의 CPU 번호입니다. 여기서는 CPU 0을 사용한다.
100: 인터럽트 우선순위를 나타낸다.

IfxStm_clearCompareFlag(g_Stm.stmSfr, g_Stm.stmConfig.comparator);

STM의 CompareFlag를 초기화 한다.

IfxStm_increaseCompare(g_Stm.stmSfr, g_Stm.stmConfig.comparator, 100000000u);

현재 Compare 값을 증가시켜 다음 인터럽트가 발생할 시간을 설정한다.

그 밑으로는 flag를 0과 1을 왔다 갔다 하면서 LED를 켰다 껏다 한다.

동작확인

참조

https://cafe.naver.com/binaryembedded
https://embeddedchallenge.tistory.com/228

profile
공부한거 정리, 잘못된 정보 태클은 언제나 환영입니다.

0개의 댓글