Notice
Recent Posts
Recent Comments
Link
승코딩당당당
[TC275] STM 인터럽트 기반 신호등 구현하기 본문
이번 실습에서는 Infineon AURIX TC275의 System Timer Module(STM)을 이용해 주기 인터럽트 기반 LED 신호등 시스템을 구현한다. 단순한 GPIO 토글이 아니라, STM Compare Interrupt를 활용하여 일정 주기(0.5초 Tick)를 생성하고, 이를 기반으로 상태를 전환하는 구조로 설계하였다.
STM 인터럽트 핸들러(STM_Int0Handler)에서는 Compare Flag를 클리어하고 다음 인터럽트 시점을 갱신한 뒤, g_tickCnt와 g_state를 이용해 신호등 상태를 순환하도록 구성했다. 상태는 총 3단계로 구성되며, 아래 형태로 동작한다.
- State 0 : Red LED ON
- State 1 : Blue LED ON
- State 2 : Blue LED Blink
일정 Tick이 누적되면 상태가 전환되며, 마지막 상태 이후에는 다시 초기 상태로 돌아가는 순환(State Machine) 구조를 가진다.
이번 코드를 통해 단순 LED 제어를 넘어, 아래 내용까지 함께 정리할 수 있다.
- STM Compare 기반 주기 인터럽트 생성 방식
- 인터럽트 내부에서 시간 관리하는 방법
- Tick 카운터 기반 상태 머신 설계
- iLLD 라이브러리를 활용한 포트 및 타이머 설정
즉, 폴링이 아닌 타이머 인터럽트 기반 주기 제어 구조를 이해하는 것이 핵심 목표다.
코드 (Cpu0_Main.c)

/**********************************************************************************************************************
* \file Cpu0_Main.c
* \copyright Copyright (C) Infineon Technologies AG 2019
*
* Use of this file is subject to the terms of use agreed between (i) you or the company in which ordinary course of
* business you are acting and (ii) Infineon Technologies AG or its licensees. If and as long as no such terms of use
* are agreed, use of this file is subject to following:
*
* Boost Software License - Version 1.0 - August 17th, 2003
*
* Permission is hereby granted, free of charge, to any person or organization obtaining a copy of the software and
* accompanying documentation covered by this license (the "Software") to use, reproduce, display, distribute, execute,
* and transmit the Software, and to prepare derivative works of the Software, and to permit third-parties to whom the
* Software is furnished to do so, all subject to the following:
*
* The copyright notices in the Software and this entire statement, including the above license grant, this restriction
* and the following disclaimer, must be included in all copies of the Software, in whole or in part, and all
* derivative works of the Software, unless such copies or derivative works are solely in the form of
* machine-executable object code generated by a source language processor.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*********************************************************************************************************************/
#include "Ifx_Types.h"
#include "IfxCpu.h"
#include "IfxScuWdt.h"
#include "IfxStm.h"
#include "IfxCpu_Irq.h"
#include "IfxPort.h"
#include "IfxPort_PinMap.h"
typedef struct
{
Ifx_STM *stmSfr;
IfxStm_CompareConfig stmConfig;
volatile uint8 LedBlink;
volatile uint32 counter;
} App_Stm;
static App_Stm g_Stm;
volatile uint8 g_blinkOn = 1; // LED blink on/off state flag
volatile uint8 g_state = 0; // 0: Red LED, 1: Blue LED, 2: Blue Led BLINK
static uint8 g_tickCnt =0; // 0.5sec tick counter
void IfxStmDemo_init(void);
void initGPIO(void);
IFX_INTERRUPT(STM_Int0Handler, 0, 100);
void STM_Int0Handler(void)
{
IfxStm_clearCompareFlag(g_Stm.stmSfr, g_Stm.stmConfig.comparator);
IfxStm_increaseCompare(g_Stm.stmSfr, g_Stm.stmConfig.comparator, 50000000u);
g_tickCnt++;
if(g_tickCnt >= 10)
{
g_tickCnt = 0;
g_state++;
if(g_state >= 3)
g_state = 0;
}
switch(g_state)
{
case 0:
IfxPort_setPinHigh(IfxPort_P10_1.port, IfxPort_P10_1.pinIndex);
IfxPort_setPinLow(IfxPort_P10_1.port, IfxPort_P10_2.pinIndex);
break;
case 1:
IfxPort_setPinLow(IfxPort_P10_1.port, IfxPort_P10_1.pinIndex);
IfxPort_setPinHigh(IfxPort_P10_1.port, IfxPort_P10_2.pinIndex);
break;
case 2:
{
static uint8 blueOn = 0;
IfxPort_setPinLow(IfxPort_P10_1.port, IfxPort_P10_1.pinIndex);
if (blueOn)
IfxPort_setPinLow(IfxPort_P10_2.port, IfxPort_P10_2.pinIndex);
else
IfxPort_setPinHigh(IfxPort_P10_2.port, IfxPort_P10_2.pinIndex);
blueOn ^= 1;
break;
}
default:
break;
}
IfxCpu_enableInterrupts();
}
IfxCpu_syncEvent cpuSyncEvent = 0;
void core0_main(void)
{
IfxCpu_enableInterrupts();
/* !!WATCHDOG0 AND SAFETY WATCHDOG ARE DISABLED HERE!!
* Enable the watchdogs and service them periodically if it is required
*/
IfxScuWdt_disableCpuWatchdog(IfxScuWdt_getCpuWatchdogPassword());
IfxScuWdt_disableSafetyWatchdog(IfxScuWdt_getSafetyWatchdogPassword());
/* Wait for CPU sync event */
IfxCpu_emitEvent(&cpuSyncEvent);
IfxCpu_waitEvent(&cpuSyncEvent, 1);
initGPIO();
IfxStmDemo_init();
// initERU();
while(1)
{
}
}
void initGPIO(void)
{
IfxPort_setPinModeInput(
IfxPort_P02_0.port,
IfxPort_P02_0.pinIndex,
IfxPort_InputMode_pullUp);
IfxPort_setPinModeInput(
IfxPort_P02_1.port,
IfxPort_P02_1.pinIndex,
IfxPort_InputMode_pullUp);
IfxPort_setPinModeOutput(
IfxPort_P10_1.port,
IfxPort_P10_1.pinIndex,
IfxPort_OutputMode_pushPull,
IfxPort_OutputIdx_general);
IfxPort_setPinModeOutput(
IfxPort_P10_2.port,
IfxPort_P10_2.pinIndex,
IfxPort_OutputMode_pushPull,
IfxPort_OutputIdx_general);
}
void IfxStmDemo_init(void)
{
boolean interruptState = IfxCpu_disableInterrupts();
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 = 50000000;
IfxStm_initCompare(g_Stm.stmSfr, &g_Stm.stmConfig);
IfxCpu_restoreInterrupts(interruptState);
}
결과
5초 간격으로 "Red LED → Blue LED → Blue LED BLINK" 과정을 확인할 수 있다.
'개발 > 임베디드' 카테고리의 다른 글
| [메모리 구조] 변수 선언 방식에 따른 메모리 영역 확인 (0) | 2026.02.26 |
|---|---|
| [TC275] 4자리 FND 카운터 구현하기 (스위치 제어) (0) | 2026.02.23 |
| [TC275] 인터럽트로 LED Blinking과 멈추기 (0) | 2026.02.20 |
| [TC275] Multi-interrupt로 LED 제어 (Blue/Red) (0) | 2026.02.20 |
| [TC275] ERU 인터럽트로 LED 제어 (On/Off, Toggle) (0) | 2026.02.20 |
