task 2 und 3 fertig

This commit is contained in:
2026-04-14 18:38:37 +02:00
parent 8f074b6f72
commit ca83a9d386
107 changed files with 7898 additions and 3316 deletions
@@ -0,0 +1,238 @@
/* ***************************************************************************************
# Project: task3 - C: Timer & Interrupts
# File: task3_it.s
#
# Language: C
#
# Hardware: STefi Light v1.1
# Processor: STM32G431KBT6U
#
# Author: Manuel Lederhofer
# Datum: 31.10.2014
#
# Version: 6.1
# History:
# 31.10.2014 ML create file
# 27.09.2018 ML edit comments, extend vector table
# 18.12.2018 ML port from MKL05Z32VLC4 to STM32L476RG
# 27.02.2019 ML move section of exception handlers to bottom of file,
# change ASM pseudo commands from .space to .org
# 25.09.2019 ML minor changes for a better code and comment understanding
# 04.09.2020 HL port from STM32L476RG to STM32F411xE
# 21.09.2020 ML tidy up, comments and formatting
# 29.09.2021 ML port from STM32F411xE to STM32F042K6T6
# 09.03.2022 ML port from STM32F042K6T6 to STM32G431KBT6U
# 17.02.2025 TK move ASM:Interrupts to task2, replace with C:Interrupts
# 24.06.2025 TK remove Systick
#
# Status: working
#
# Description:
# See the description and requirements of the requested application
# in the lab exercise guide.
#
# Notes:
# - MCU speed at startup is 16 MHz
#
# ToDo:
# - Change the example code to match the description and requirements
# of the requested application in the lab exercise guide.
************************************************************************************** */
/* ------------------------------------ INCLUDES -------------------------------------- */
#include "stm32g431xx.h"
#include "STefi-Light.h"
/* ------------------------------------ DEFINES --------------------------------------- */
/* ------------------------------------ TYPE DEFINITIONS ------------------------------ */
/* ------------------------------------ GLOBAL VARIABLES ------------------------------ */
extern void* _estack; // initial stack pointer from ldscript
extern void* Reset_Handler; // exception handler from startup code
/* ------------------------------------ PRIVATE VARIABLES ----------------------------- */
/* ------------------------------------ PROTOTYPES ------------------------------------ */
/* ------------------------------------ GLOBAL FUNCTIONS (Exceptions Handlers) -------- */
/* ------------------------------------------------------------------------------------ *\
* method: void ISR_error(void)
*
* Default interrupt handler for core interrupts.
* Enables the green and red LED on the STefi Light board.
\* ------------------------------------------------------------------------------------ */
void ISR_error(void)
{
/* init */
RCC->AHB2ENR |= RCC_AHB2ENR_GPIOAEN; // PA: clock on (LEDs)
GPIOA->ODR |= 0x0F;
GPIOA->MODER = (GPIOA->MODER & 0xFFFFFF00) | 0x11;
while(1)
{ /* light up the LEDs permanently */
GPIOA->ODR &= ~((1 << 2) | (1 << 0));
}
}
/* ------------------------------------------------------------------------------------ *\
* method: void ISR_default(void)
*
* Default interrupt handler for non-core interrupts.
* Enables the blue and yellow LED on the STefi Light board.
\* ------------------------------------------------------------------------------------ */
void ISR_default(void)
{
/* init */
RCC->AHB2ENR |= RCC_AHB2ENR_GPIOAEN; // PA: clock on (LEDs)
GPIOA->ODR |= 0x0F;
GPIOA->MODER = (GPIOA->MODER & 0xFFFFFF00) | 0x44;
while(1)
{ /* light up the LEDs permanently */
GPIOA->ODR &= ~((1 << 3) | (1 << 1));
}
}
/* ------------------------------------------------------------------------------------ *\
* method: Timer
*
* Timer interrupt is used for ...
\* ------------------------------------------------------------------------------------ */
/* ------------------------------------ INTERRUPT VECTOR TABLE ------------------------ */
__attribute__ ((section (".isr_vector")))
void (* const paIsrFunc[118])(void) =
{
/* --- core vectors --- */
(void *)&_estack, /* 00 (0x00000000) initial stack pointer */
(void *)&Reset_Handler, /* 01 (0x00000004) (prio: -3) initial program counter (leads to first command to execute) */
ISR_error, /* 02 (0x00000008) (prio: -2) non-maskable interrupt */
ISR_error, /* 03 (0x0000000C) (prio: -1) hard fault exception */
ISR_error, /* 04 (0x00000010) memory management */
ISR_error, /* 05 (0x00000014) bus fault - pre-fetch fault, memory access fault */
ISR_error, /* 06 (0x00000018) usage fault - undefined instruction or illegal state */
ISR_error, /* 07 (0x0000001C) reserved vector 7 */
ISR_error, /* 08 (0x00000020) reserved vector 8 */
ISR_error, /* 09 (0x00000024) reserved vector 9 */
ISR_error, /* 10 (0x00000028) reserved vector 10 */
ISR_error, /* 11 (0x0000002C) system service call via SWI instruction */
ISR_error, /* 12 (0x00000030) monitor */
ISR_error, /* 13 (0x00000034) reserved vector 13 */
ISR_error, /* 14 (0x00000038) pendable service call exception */
ISR_error, /* 15 (0x0000003C) SysTick exception */
/* --- non-core vectors --- */
ISR_default, /* 0 (0x00000040) window watchdog interrupt */
ISR_default, /* 1 (0x00000044) PVD through EXTI line 16 interrupt */
ISR_default, /* 2 (0x00000048) RTC/TAMP/CSS on LSE through EXTI line 19 interrupt */
ISR_default, /* 3 (0x0000004C) RTC Wakeup timer through EXTI line 20 interrupt */
ISR_default, /* 4 (0x00000050) Flash global interrupt */
ISR_default, /* 5 (0x00000054) RCC global interrupt */
EXTI_IRQHandler, /* 6 (0x00000058) EXTI Line 0 interrupt */
ISR_default, /* 7 (0x0000005C) EXTI Line 1 interrupt */
ISR_default, /* 8 (0x00000060) EXTI Line 2 interrupt */
ISR_default, /* 9 (0x00000064) EXTI Line 3 interrupt */
ISR_default, /* 10 (0x00000068) EXTI Line 4 interrupt */
ISR_default, /* 11 (0x0000006C) DMA1 channel 1 interrupt */
ISR_default, /* 12 (0x00000070) DMA1 channel 2 interrupt */
ISR_default, /* 13 (0x00000074) DMA1 channel 3 interrupt */
ISR_default, /* 14 (0x00000078) DMA1 channel 4 interrupt */
ISR_default, /* 15 (0x0000007C) DMA1 channel 5 interrupt */
ISR_default, /* 16 (0x00000080) DMA1 channel 6 interrupt */
ISR_default, /* 17 (0x00000084) DMA1 channel 7 interrupt */
ISR_default, /* 18 (0x00000088) ADC1 + ADC2 global interrupt */
ISR_default, /* 19 (0x0000008C) USB high priority interrupts */
ISR_default, /* 20 (0x00000090) USB low priority interrupts */
ISR_default, /* 21 (0x00000094) FDCAN1 interrupt 0 */
ISR_default, /* 22 (0x00000098) FDCAN1 interrupt 1 */
ISR_default, /* 23 (0x0000009C) EXTI line [9:5] interrupts */
ISR_default, /* 24 (0x000000A0) TIM1 break + TIM15 global interrupts */
ISR_default, /* 25 (0x000000A4) TIM1 update + TIM16 global interrupts */
ISR_default, /* 26 (0x000000A8) TIM1 trigger, commutation, direction change, index + TIM17 interrupts */
ISR_default, /* 27 (0x000000AC) TIM1 capture compare interrupt */
ISR_default, /* 28 (0x000000B0) TIM2 global interrupt */
ISR_default, /* 29 (0x000000B4) TIM3 global interrupt */
ISR_default, /* 30 (0x000000B8) TIM4 global interrupt */
ISR_default, /* 31 (0x000000BC) I2C1 event + EXTI line 23 interrupts */
ISR_default, /* 32 (0x000000C0) I2C1 error interrupt */
ISR_default, /* 33 (0x000000C4) I2C2 event + EXTI line 24 interrupts */
ISR_default, /* 34 (0x000000C8) I2C2 error interrupt */
ISR_default, /* 35 (0x000000CC) SPI1 global interrupt */
ISR_default, /* 36 (0x000000D0) SPI2 global interrupt */
ISR_default, /* 37 (0x000000D4) USART1 global + EXTI line 25 interrupts */
ISR_default, /* 38 (0x000000D8) USART2 global + EXTI line 26 interrupts */
ISR_default, /* 39 (0x000000DC) USART3 global + EXTI line 28 interrupts */
ISR_default, /* 40 (0x000000E0) EXTI line [15:10] interrupts */
ISR_default, /* 41 (0x000000E4) RTC alarms interrupts */
ISR_default, /* 42 (0x000000E8) USB wakeup from suspend trough EXTI line 18 interrupt */
ISR_default, /* 43 (0x000000EC) TIM8 break, transition error, index error interrupts */
ISR_default, /* 44 (0x000001F0) TIM8 update interrupt */
ISR_default, /* 45 (0x000001F4) TIM8 trigger, commutation, direction change, index interrupts */
ISR_default, /* 46 (0x000001F8) TIM8 capture compare interrupt */
ISR_default, /* 47 (0x000001FC) ADC3 global interrupt */
ISR_default, /* 48 (0x00000100) FSMC global interrupt */
ISR_default, /* 49 (0x00000104) LPTIM1 global interrupt */
ISR_default, /* 50 (0x00000108) TIM5 global interrupt */
ISR_default, /* 51 (0x0000010C) SPI3 global interrupt */
ISR_default, /* 52 (0x00000110) UART4 global + EXTI line 34 interrupts */
ISR_default, /* 53 (0x00000114) UART5 global + EXTI line 35 interrupts */
TIM6_DAC_IRQHandler, /* 54 (0x00000118) TIM6 + DAC1/3 underrun global interrupts */
ISR_default, /* 55 (0x0000011C) TIM7 + DAC2/4 underrun global interrupts */
ISR_default, /* 56 (0x00000120) DMA2 channel 1 interrupt */
ISR_default, /* 57 (0x00000124) DMA2 channel 2 interrupt */
ISR_default, /* 58 (0x00000128) DMA2 channel 3 interrupt */
ISR_default, /* 59 (0x0000012C) DMA2 channel 4 interrupt */
ISR_default, /* 60 (0x00000130) DMA2 channel 5 interrupt */
ISR_default, /* 61 (0x00000134) ADC4 global interrupt */
ISR_default, /* 62 (0x00000138) ADC5 global interrupt */
ISR_default, /* 63 (0x0000013C) UCPD1 global + EXTI line 43 interrupts */
ISR_default, /* 64 (0x00000140) COMP1/COMP2/COMP3 through EXTI lines 21/22/29 interrupts */
ISR_default, /* 65 (0x00000144) COMP4/COMP5/COMP6 through EXTI lines 30/31/32 interrupts */
ISR_default, /* 66 (0x00000148) COMP7 through EXTI line 33 interrupt */
ISR_default, /* 67 (0x0000014C) HRTIM master timer interrupt (hrtim_it1) */
ISR_default, /* 68 (0x00000150) HRTIM timer A interrupt (hrtim_it2) */
ISR_default, /* 69 (0x00000154) HRTIM timer B interrupt (hrtim_it3) */
ISR_default, /* 70 (0x00000158) HRTIM timer C interrupt (hrtim_it4) */
ISR_default, /* 71 (0x0000015C) HRTIM timer D interrupt (hrtim_it5) */
ISR_default, /* 72 (0x00000160) HRTIM timer E interrupt (hrtim_it6) */
ISR_default, /* 73 (0x00000164) HRTIM fault interrupt (hrtim_it8) */
ISR_default, /* 74 (0x00000168) HRTIM timer F interrupt (hrtim_it7) */
ISR_default, /* 75 (0x0000016C) CRS interrupt */
ISR_default, /* 76 (0x00000170) SAI */
ISR_default, /* 77 (0x00000174) TIM20 break, transition error, index error interrupts */
ISR_default, /* 78 (0x00000178) TIM20 update interrupt */
ISR_default, /* 79 (0x0000017C) TIM20 trigger, commutation, direction change, index interrupts */
ISR_default, /* 80 (0x00000180) TIM20 capture compare interrupt */
ISR_default, /* 81 (0x00000184) Floating point interrupt */
ISR_default, /* 82 (0x00000188) I2C4 event + EXTI line 4 interrupts */
ISR_default, /* 83 (0x0000018C) I2C4 error interrupt */
ISR_default, /* 84 (0x00000190) SPI4 global interrupt */
ISR_default, /* 85 (0x00000194) AES global interrupt */
ISR_default, /* 86 (0x00000198) FDCAN2 interrupt 0 */
ISR_default, /* 87 (0x0000019C) FDCAN2 interrupt 1 */
ISR_default, /* 88 (0x000001A0) FDCAN3 interrupt 0 */
ISR_default, /* 89 (0x000001A4) FDCAN3 interrupt 1 */
ISR_default, /* 90 (0x000001A8) RNG global interrupt */
ISR_default, /* 91 (0x000001AC) LPUART global interrupt */
ISR_default, /* 92 (0x000001B0) I2C3 event + EXTI line 27 interrupts */
ISR_default, /* 93 (0x000001B4) I2C3 error interrupt */
ISR_default, /* 94 (0x000001B8) DMAMUX overrun interrupt */
ISR_default, /* 95 (0x000001BC) QUADSPI global interrupt */
ISR_default, /* 96 (0x000001C0) DMA1 channel 8 interrupt */
ISR_default, /* 97 (0x000001C4) DMA2 channel 6 interrupt */
ISR_default, /* 98 (0x000001C8) DMA2 channel 7 interrupt */
ISR_default, /* 99 (0x000001CC) DMA2 channel 8 interrupt */
ISR_default, /* 100 (0x000001D0) Cordic interrupt */
ISR_default /* 101 (0x000001D4) FMAC interrupt */
};
/* ************************************ E O F ***************************************** */
@@ -1,347 +0,0 @@
#****************************************************************************************#
# Project: task2 - ASM: Interrupts
# File: task2.s
# Hardware: STefi Light v1.1
# Processor: STM32G431KBT6U
#****************************************************************************************#
.include "G431_addr.s"
// Falls in G431_addr.s vorhanden, diesen Block löschen um Fehler zu vermeiden:
.equ RCC_AHB2ENR, 0x4002104C
.equ RCC_APB2ENR, 0x40021060
.equ GPIOA_MODER, 0x48000000
.equ GPIOA_ODR, 0x48000014
.equ GPIOB_MODER, 0x48000400
.equ GPIOB_PUPDR, 0x4800040C
.equ GPIOB_IDR, 0x48000410
.equ SYSCFG_BASE, 0x40010000
.equ SYSCFG_EXTICR1, (SYSCFG_BASE + 0x08)
.equ SYSCFG_EXTICR2, (SYSCFG_BASE + 0x0C)
.equ EXTI_BASE, 0x40010400
.equ EXTI_IMR1, (EXTI_BASE + 0x00)
.equ EXTI_FTSR1, (EXTI_BASE + 0x0C)
.equ EXTI_PR1, (EXTI_BASE + 0x14)
.equ NVIC_ISER0, 0xE000E100
.equ DBGMCU_CR, 0xE0042004 // Adresse des Debug Configuration Registers
#----------------------------------------------------------------------------------------#
.section .vectortable,"a"
#----------------------------------------------------------------------------------------#
.align 2
.word 0x20004000 // initial Stack Pointer
.word init // initial Program Counter
.word _ISR_NMI // non-masking interrupt
.word _ISR_HARDF // hard fault interrupt
.space 0x48 // Padding 72 Bytes (Offset 0x10 -> 0x58)
.word _ISR_EXTI0 // EXTI0_IRQHandler (PB0 / S0) - IRQ 6
.space 0x0C // Padding 12 Bytes (Offset 0x5C -> 0x68)
.word _ISR_EXTI4 // EXTI4_IRQHandler (PB4 / S1) - IRQ 10
#----------------------------------------------------------------------------------------#
.text
#----------------------------------------------------------------------------------------#
.align 2
.syntax unified
.thumb
.thumb_func
.global init
.type init, %function
init:
CPSID i
MOVS r0, #0
MOVS r1, #0
MOVS r2, #0
MOVS r3, #0
MOVS r4, #0
MOVS r5, #0
MOVS r6, #0
MOVS r7, #0
MOV r8, r0
MOV r9, r0
MOV r10, r0
MOV r11, r0
MOV r12, r0
#--- enable port clocking (GPIOA, GPIOB)
LDR r1, =RCC_AHB2ENR
MOVS r2, #0x03 // Bit 0 (GPIOA) und Bit 1 (GPIOB)
LDR r0, [r1, #0]
ORRS r0, r2
STR r0, [r1, #0]
#--- port init
#- LEDs (PA0 - PA3) als Output
LDR r1, =GPIOA_MODER
LDR r2, =0x000000FF
LDR r0, [r1, #0]
BICS r0, r2
LDR r2, =0x00000055
ORRS r0, r2
STR r0, [r1, #0]
#- switch LED off
LDR r1, =GPIOA_ODR
MOVS r2, #0x0F
LDR r0, [r1, #0]
ORRS r0, r2
STR r0, [r1, #0]
#- buttons (PB0, PB4) als Input
LDR r1, =GPIOB_MODER
LDR r2, =0x00000303 // Maske Bits 0:1 (PB0) und 8:9 (PB4)
LDR r0, [r1, #0]
BICS r0, r2
STR r0, [r1, #0]
#- Pull-Up für PB0, PB4
LDR r1, =GPIOB_PUPDR
LDR r2, =0x00000303
LDR r0, [r1, #0]
BICS r0, r2
LDR r2, =0x00000101 // 01 = Pull-Up für PB0 und PB4
ORRS r0, r2
STR r0, [r1, #0]
#--- button interrupt config
#- enable clock for SYSCFG module
LDR r1, =RCC_APB2ENR
MOVS r2, #0x01
LDR r0, [r1, #0]
ORRS r0, r2
STR r0, [r1, #0]
#- connect GPIO pins to EXTI lines
LDR r1, =SYSCFG_EXTICR1 // EXTI0 (PB0)
LDR r2, =0x000F // Maske EXTI0 (Bits 3:0)
LDR r0, [r1, #0]
BICS r0, r2
MOVS r2, #0x0001 // Port B (0001)
ORRS r0, r2
STR r0, [r1, #0]
LDR r1, =SYSCFG_EXTICR2 // EXTI4 (PB4)
LDR r2, =0x000F // Maske EXTI4 (Bits 3:0)
LDR r0, [r1, #0]
BICS r0, r2
MOVS r2, #0x0001 // Port B (0001)
ORRS r0, r2
STR r0, [r1, #0]
#- configure EXTI lines (falling edge, unmask)
LDR r1, =EXTI_FTSR1
MOVS r2, #0x11 // Bit 0 (EXTI0) und Bit 4 (EXTI4)
LDR r0, [r1, #0]
ORRS r0, r2
STR r0, [r1, #0]
LDR r1, =EXTI_IMR1
MOVS r2, #0x11 // Bit 0 und Bit 4
LDR r0, [r1, #0]
ORRS r0, r2
STR r0, [r1, #0]
#- NVIC: enable interrupts EXTI0 (IRQ 6) & EXTI4 (IRQ 10)
LDR r1, =NVIC_ISER0
LDR r2, =0x00000440 // Bit 6 (EXTI0) und Bit 10 (EXTI4)
LDR r0, [r1, #0]
ORRS r0, r2
STR r0, [r1, #0]
#--- wachhalten während WFI
LDR r1, =DBGMCU_CR
LDR r0, [r1, #0]
MOVS r2, #0x07 // Setzt DBG_SLEEP, DBG_STOP und DBG_STANDBY Bits
ORRS r0, r2
STR r0, [r1, #0]
CPSIE i
#----------------------------------------------------------------------------------------#
.align 2
.syntax unified
.thumb
.thumb_func
.global main
.type main, %function
main:
WFI
B main
#----------------------------------------------------------------------------------------#
.align 2
.syntax unified
.thumb
.thumb_func
.global delay
.type delay, %function
delay:
LDR r0, =106000
.L1:
SUBS r0, r0, #1
BNE .L1
BX lr
#----------------------------------------------------------------------------------------#
.align 2
.global stop
stop:
NOP
B stop
#----------------------------------------------------------------------------------------#
.lp1:
.ltorg
#----------------------------------------------------------------------------------------#
#----------------------------------------------------------------------------------------#
.section .exhand,"ax"
#----------------------------------------------------------------------------------------#
.align 2
.syntax unified
.thumb
.type _ISR_NMI, %function
_ISR_NMI:
LDR r1, =RCC_AHB2ENR
MOV r2, #0x01
LDR r0, [r1, #0]
ORRS r0, r2
STR r0, [r1, #0]
LDR r1, =GPIOA_MODER
MOVS r2, #0xFF
LDR r0, [r1, #0]
BICS r0, r2
MOVS r2, #0x44
ORRS r0, r2
STR r0, [r1, #0]
LDR r1, =GPIOA_ODR
MOVS r2, #0x0A
LDR r0, [r1, #0]
BICS r0, r2
STR r0, [r1, #0]
B _ISR_NMI
#----------------------------------------------------------------------------------------#
.align 2
.syntax unified
.thumb
.type _ISR_HARDF, %function
_ISR_HARDF:
LDR r1, =RCC_AHB2ENR
MOV r2, #0x01
LDR r0, [r1, #0]
ORRS r0, r2
STR r0, [r1, #0]
LDR r1, =GPIOA_MODER
MOVS r2, #0xFF
LDR r0, [r1, #0]
BICS r0, r2
MOVS r2, #0x11
ORRS r0, r2
STR r0, [r1, #0]
LDR r1, =GPIOA_ODR
MOVS r2, #0x05
LDR r0, [r1, #0]
BICS r0, r2
STR r0, [r1, #0]
B _ISR_HARDF
#----------------------------------------------------------------------------------------#
.align 2
.syntax unified
.thumb
.type _ISR_EXTI0, %function
_ISR_EXTI0:
PUSH {lr}
#--- Entprellen
BL delay
#--- Überprüfen, ob Taster noch gedrückt ist (PB0)
LDR r0, =GPIOB_IDR
LDR r1, [r0, #0]
MOVS r2, #0x01
TST r1, r2
BNE _clear_exti0
#--- LED0 & LED3 toggeln (1001 = 0x09)
LDR r0, =GPIOA_ODR
LDR r1, [r0, #0]
MOVS r2, #0x09
EORS r1, r2
STR r1, [r0, #0]
_clear_exti0:
#--- Interrupt Flag löschen
LDR r0, =EXTI_PR1
MOVS r1, #0x01
STR r1, [r0, #0]
POP {pc}
#----------------------------------------------------------------------------------------#
.align 2
.syntax unified
.thumb
.type _ISR_EXTI4, %function
_ISR_EXTI4:
PUSH {lr}
#--- Entprellen
BL delay
#--- Überprüfen, ob Taster noch gedrückt ist (PB4)
LDR r0, =GPIOB_IDR
LDR r1, [r0, #0]
MOVS r2, #0x10
TST r1, r2
BNE _clear_exti4
#--- LED1 & LED2 toggeln (0110 = 0x06)
LDR r0, =GPIOA_ODR
LDR r1, [r0, #0]
MOVS r2, #0x06
EORS r1, r2
STR r1, [r0, #0]
_clear_exti4:
#--- Interrupt Flag löschen
LDR r0, =EXTI_PR1
MOVS r1, #0x10
STR r1, [r0, #0]
POP {pc}
#----------------------------------------------------------------------------------------#
.lp2:
.ltorg
#----------------------------------------------------------------------------------------#
.end
hu
@@ -0,0 +1,233 @@
/* ***************************************************************************************
# Project: task3 - C: Timer & Interrupts
# File: task3.s
#
# Language: C
#
# Hardware: STefi Light v1.1
# Processor: STM32G431KBT6U
#
# Author: Manuel Lederhofer
# Datum: 31.10.2014
#
# Version: 6.1
# History:
# 31.10.2014 ML create file
# 27.09.2018 ML edit comments, extend vector table
# 18.12.2018 ML port from MKL05Z32VLC4 to STM32L476RG
# 27.02.2019 ML move section of exception handlers to bottom of file,
# change ASM pseudo commands from .space to .org
# 25.09.2019 ML minor changes for a better code and comment understanding
# 04.09.2020 HL port from STM32L476RG to STM32F411xE
# 21.09.2020 ML tidy up, comments and formatting
# 29.09.2021 ML port from STM32F411xE to STM32F042K6T6
# 09.03.2022 ML port from STM32F042K6T6 to STM32G431KBT6U
# 17.02.2025 TK move ASM:Interrupts to task2, replace with C:Timer & Interrupts
# 24.06.2025 TK remove "... put your code here ...", remove Systick
#
# Status: working
#
# Description:
# See the description and requirements of the requested application
# in the lab exercise guide.
#
# Notes:
# - MCU speed at startup is 16 MHz
#
# ToDo:
# - Change the example code to match the description and requirements
# of the requested application in the lab exercise guide.
#
#
# Measurements:
# Timers used :
# Current with WFI :
# Current without WFI :
#
************************************************************************************** */
/* ------------------------------------ INCLUDES -------------------------------------- */
#include "stm32g431xx.h"
#include "STefi-Light.h"
#define ROT_AN (GPIOA->BSRR = (1UL<<16))
#define ROT_AUS (GPIOA->BSRR = (1UL<<0))
#define GELB_AN (GPIOA->BSRR = (1UL<<17))
#define GELB_AUS (GPIOA->BSRR = (1UL<<1))
#define GRÜN_AN (GPIOA->BSRR = (1UL<<18))
#define GRÜN_AUS (GPIOA->BSRR = (1UL<<2))
#define BLAU_AN (GPIOA->BSRR = (1UL<<19))
#define BLAU_AUS (GPIOA->BSRR = (1UL<<3))
/* ------------------------------------ DEFINES --------------------------------------- */
/* ------------------------------------ TYPE DEFINITIONS ------------------------------ */
/* ------------------------------------ GLOBAL VARIABLES ------------------------------ */
volatile uint32_t timer_ticks = 0;
volatile uint8_t ampel_aktiv = 0;
volatile uint32_t lbp = 0;
/* ------------------------------------ PRIVATE VARIABLES ----------------------------- */
/* ------------------------------------ PROTOTYPES ------------------------------------ */
static void GPIO_init(void);
void Timer_init(void);
void EXTI_init(void);
/* ------------------------------------ M A I N --------------------------------------- */
int main(void)
{
/* --- initialization --- */
__disable_irq(); // disable interrupts globally
GPIO_init();
Timer_init();
EXTI_init();
__enable_irq(); // enable interrupts globally
lbp = 0;
GRÜN_AN;
ROT_AUS;
GELB_AUS;
BLAU_AUS;
/* --- one time tasks --- */
/* --- infinite processing loop --- */
while (1){
__WFI();
}
return 1;
}
/* ------------------------------------ GLOBAL FUNCTIONS ------------------------------ */
void EXTI_IRQHandler(void){
if(EXTI->PR1 & EXTI_PR1_PIF0){
EXTI->PR1 = EXTI_PR1_PIF0;
if((timer_ticks - lbp)> 200){
lbp = timer_ticks;
if(ampel_aktiv == 0){
timer_ticks = 0;
ampel_aktiv=1;
}
}
}
TIM6_DAC_IRQHandler();
}
void TIM6_DAC_IRQHandler(void){
if(TIM6->SR & (1<<0)){
TIM6->SR &= ~(1U<<0);
timer_ticks++;
if(ampel_aktiv == 1){
switch(timer_ticks){
case 10000:
GRÜN_AUS;
GELB_AN;
break;
case 11000:
GELB_AUS;
ROT_AN;
break;
case 12000:
break;
case 27000:
BLAU_AUS;
GELB_AN;
break;
case 28000:
ROT_AUS;
GELB_AUS;
GRÜN_AN;
ampel_aktiv = 0;
break;
}
if(timer_ticks >=12000 && timer_ticks < 27000){
if(timer_ticks % 1000 == 0){
BLAU_AN;
}
else if(timer_ticks % 1000 == 500){
BLAU_AUS;
}
}
}
}
}
/* ------------------------------------ PRIVATE FUNCTIONS ----------------------------- */
/* ------------------------------------------------------------------------------------ *\
* method: static void GPIO_init(void)
*
* Initializes GPIOs on STefi Light for pins with peripherals attached.
*
* requires: - nothing -
* parameters: - none -
* returns: - nothing -
\* ------------------------------------------------------------------------------------ */
static void GPIO_init(void)
{
RCC->AHB2ENR |= RCC_AHB2ENR_GPIOAEN | RCC_AHB2ENR_GPIOBEN;
// LEDs PA0-PA3 Output (01)
GPIOA->MODER &= ~(0xFF);
GPIOA->MODER |= 0x55;
// Initial alle aus (Low-Active: 1 = AUS)
GPIOA->ODR |= 0x0F;
// S0 (PB0) Input (00) mit Pull-Up (01)
GPIOB->MODER &= ~(3 << 0);
GPIOB->PUPDR |= (1 << 0);
}
void Timer_init(void) {
RCC->APB1ENR1 |= RCC_APB1ENR1_TIM6EN;
// 16MHz Systemtakt. 16MHz / 16 (PSC+1) = 1MHz.
// Bei 1MHz sind 1000 Ticks (ARR+1) exakt 1 Millisekunde.
TIM6->PSC = 15;
TIM6->ARR = 999;
TIM6->DIER |=(1<<0);
NVIC_EnableIRQ(TIM6_DAC_IRQn);
TIM6->CR1 |= (1<<0);
}
void EXTI_init(void) {
RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN;
SYSCFG->EXTICR[0] = (SYSCFG->EXTICR[0] & ~(0xF)) | 0x1;
EXTI->IMR1 |= (1<<0);
EXTI->FTSR1 |= (1<<0);
NVIC_EnableIRQ(EXTI0_IRQn);
}
/* ------------------------------------------------------------------------------------ *\
* method: static void SysTick_init(void)
*
* At system startup SysTick runs with 1 MHz down to 0.
*
* Register names differ in datasheets.
* ST / CMSIS: ARM:
* - SysTick->CALIB - STK_CALIB
* - SysTick->CTRL - STK_CSR
* - SysTick->LOAD - STK_RVR
* - SysTick->VAL - STK_CVR
*
* requires: - nothing -
* parameters: - none -
* returns: - nothing -
\* ------------------------------------------------------------------------------------ */
/* ************************************ E O F ***************************************** */
@@ -1,36 +0,0 @@
/*
* sketch.c
*
* Created on: Mar 18, 2026
* Author: tobii
*/
int reihenfolge[6] = {0, 1, 2, 3, 2, 1};
int main(){
while(1){
while(/*schalter nicht gedrückt*/){}
delay(10);
while(/*schalter losgelassen*/){}
delay(150);
int aktiv = 1;
while(aktiv){
for(int i; i < reihenfolge.length - 1 && aktiv = 1; i ++){
//alle LED einschalten
if(i = 0){/*alles bis auf LED0 auschalten*/}
if(i = 1){/*..*/}
//...
for(int zähler = 0; zähler < 333 && aktiv == 1; zähler++){
if(/*schalter gerückt*/){
aktiv = 0;
break;
}
else{
delay(1);
}
}
while(/*Taster gedrückt*/){}
}
}
}
}