avr: added functions to change speed.

Main code might work at 8 MHz, but boot code is compiled for 1 MHz,
so we need to set speed accordingly when rebooting into boot loader.
This commit is contained in:
Martin Preuss
2024-09-13 21:41:38 +02:00
parent 2d09e22ec6
commit 7a5900be25
6 changed files with 111 additions and 18 deletions

View File

@@ -100,7 +100,7 @@
reti ; OC1A reti ; OC1A
reti ; OC1B reti ; OC1B
reti ; OVF1 reti ; OVF1
rjmp timerIrqOC0A ; OC0A rjmp baseTimerIrqOC0A ; OC0A
reti ; OC0B reti ; OC0B
reti ; OVF0 reti ; OVF0
reti ; ACI reti ; ACI
@@ -129,6 +129,8 @@ firmwareStart: rjmp main
.include "common/utils_copy_sdram.asm" .include "common/utils_copy_sdram.asm"
.include "common/crc8.asm" .include "common/crc8.asm"
.include "modules/basetimer/main.asm"
#ifdef MODULES_TIMER #ifdef MODULES_TIMER
.include "modules/timer/main.asm" .include "modules/timer/main.asm"
#endif #endif
@@ -244,6 +246,26 @@ timerList:
.include "main.asm" .include "main.asm"
systemSetSpeed:
.if clock == 8000000
ldi r16, (1<<CLKPCE)
ldi r17, 0
out CLKPR, r16
out CLKPR, r17
.endif
ret
systemSetBootSpeed:
.if clock == 8000000
ldi r16, (1<<CLKPCE)
ldi r17, (1<<CLKPS1) | (1<<CLKPS0)
out CLKPR, r16
out CLKPR, r17
.endif
ret
; --------------------------------------------------------------------------- ; ---------------------------------------------------------------------------
; Called on first time run, i.e. on system start. No arguments, no results. ; Called on first time run, i.e. on system start. No arguments, no results.

View File

@@ -100,7 +100,7 @@
reti ; OC1A reti ; OC1A
reti ; OC1B reti ; OC1B
reti ; OVF1 reti ; OVF1
rjmp timerIrqOC0A ; OC0A rjmp baseTimerIrqOC0A ; OC0A
reti ; OC0B reti ; OC0B
reti ; OVF0 reti ; OVF0
reti ; ACI reti ; ACI
@@ -129,6 +129,8 @@ firmwareStart: rjmp main
.include "common/utils_copy_sdram.asm" .include "common/utils_copy_sdram.asm"
.include "common/crc8.asm" .include "common/crc8.asm"
.include "modules/basetimer/main.asm"
#ifdef MODULES_TIMER #ifdef MODULES_TIMER
.include "modules/timer/main.asm" .include "modules/timer/main.asm"
#endif #endif
@@ -244,6 +246,26 @@ timerList:
.include "main.asm" .include "main.asm"
systemSetSpeed:
.if clock == 8000000
ldi r16, (1<<CLKPCE)
ldi r17, 0
out CLKPR, r16
out CLKPR, r17
.endif
ret
systemSetBootSpeed:
.if clock == 8000000
ldi r16, (1<<CLKPCE)
ldi r17, (1<<CLKPS1) | (1<<CLKPS0)
out CLKPR, r16
out CLKPR, r17
.endif
ret
; --------------------------------------------------------------------------- ; ---------------------------------------------------------------------------
; Called on first time run, i.e. on system start. No arguments, no results. ; Called on first time run, i.e. on system start. No arguments, no results.

View File

@@ -22,6 +22,7 @@
.equ clock=1000000 ; Define the clock frequency .equ clock=1000000 ; Define the clock frequency
;.equ clock=8000000 ; Define the clock frequency
@@ -104,7 +105,7 @@
reti ; OC1A reti ; OC1A
reti ; OC1B reti ; OC1B
reti ; OVF1 reti ; OVF1
rjmp timerIrqOC0A ; OC0A rjmp baseTimerIrqOC0A ; OC0A
reti ; OC0B reti ; OC0B
reti ; OVF0 reti ; OVF0
reti ; ACI reti ; ACI
@@ -133,6 +134,8 @@ firmwareStart: rjmp main
.include "common/utils_copy_sdram.asm" .include "common/utils_copy_sdram.asm"
.include "common/crc8.asm" .include "common/crc8.asm"
.include "modules/basetimer/main.asm"
#ifdef MODULES_TIMER #ifdef MODULES_TIMER
.include "modules/timer/main.asm" .include "modules/timer/main.asm"
#endif #endif
@@ -263,6 +266,28 @@ timerList:
.include "main.asm" .include "main.asm"
systemSetSpeed:
.if clock == 8000000
ldi r16, (1<<CLKPCE)
ldi r17, 0
out CLKPR, r16
out CLKPR, r17
.endif
ret
systemSetBootSpeed:
.if clock == 8000000
ldi r16, (1<<CLKPCE)
ldi r17, (1<<CLKPS1) | (1<<CLKPS0)
out CLKPR, r16
out CLKPR, r17
.endif
ret
; --------------------------------------------------------------------------- ; ---------------------------------------------------------------------------
; Called on first time run, i.e. on system start. No arguments, no results. ; Called on first time run, i.e. on system start. No arguments, no results.

View File

@@ -90,7 +90,7 @@
reti ; OC1A reti ; OC1A
reti ; OC1B reti ; OC1B
reti ; OVF1 reti ; OVF1
rjmp timerIrqOC0A ; OC0A rjmp baseTimerIrqOC0A ; OC0A
reti ; OC0B reti ; OC0B
reti ; OVF0 reti ; OVF0
reti ; ACI reti ; ACI
@@ -119,6 +119,8 @@ firmwareStart: rjmp main
.include "common/utils_copy_sdram.asm" .include "common/utils_copy_sdram.asm"
.include "common/crc8.asm" .include "common/crc8.asm"
.include "modules/basetimer/main.asm"
#ifdef MODULES_TIMER #ifdef MODULES_TIMER
.include "modules/timer/main.asm" .include "modules/timer/main.asm"
#endif #endif
@@ -249,6 +251,26 @@ timerList:
.include "main.asm" .include "main.asm"
systemSetSpeed:
.if clock == 8000000
ldi r16, (1<<CLKPCE)
ldi r17, 0
out CLKPR, r16
out CLKPR, r17
.endif
ret
systemSetBootSpeed:
.if clock == 8000000
ldi r16, (1<<CLKPCE)
ldi r17, (1<<CLKPS1) | (1<<CLKPS0)
out CLKPR, r16
out CLKPR, r17
.endif
ret
; --------------------------------------------------------------------------- ; ---------------------------------------------------------------------------
; Called on first time run, i.e. on system start. No arguments, no results. ; Called on first time run, i.e. on system start. No arguments, no results.

View File

@@ -1,5 +1,5 @@
; *************************************************************************** ; ***************************************************************************
; copyright : (C) 2023 by Martin Preuss ; copyright : (C) 2024 by Martin Preuss
; email : martin@libchipcard.de ; email : martin@libchipcard.de
; ;
; *************************************************************************** ; ***************************************************************************
@@ -21,6 +21,7 @@
; ;
main: main:
cli
; setup stack ; setup stack
.ifdef SPH ; if SPH is defined .ifdef SPH ; if SPH is defined
ldi r16, High(RAMEND) ldi r16, High(RAMEND)
@@ -31,6 +32,8 @@ main:
; rcall watchdogOff ; turn off watchdog timer (sometimes it stays on after reboot) ; rcall watchdogOff ; turn off watchdog timer (sometimes it stays on after reboot)
rcall systemSetSpeed
rcall initModules rcall initModules
rcall Utils_SetupUid rcall Utils_SetupUid
@@ -60,7 +63,7 @@ main:
; sts twiMasterScanEnabled, r16 ; sts twiMasterScanEnabled, r16
main_loop: main_loop:
rcall runModulesUntilIdle rcall runModules
sei ; make sure interrupts really are enabled sei ; make sure interrupts really are enabled
; only modify SE, SM1 and SM0 ; only modify SE, SM1 and SM0
@@ -89,6 +92,7 @@ main_loop:
initModules: initModules:
rcall Utils_Init rcall Utils_Init
rcall BaseTimer_Init ; unconditionally call this
#ifdef MODULES_TIMER #ifdef MODULES_TIMER
rcall Timer_Init rcall Timer_Init
@@ -150,7 +154,7 @@ initModules:
; --------------------------------------------------------------------------- ; ---------------------------------------------------------------------------
; runModulesUntilIdle ; runModules
; ;
; Call run functions of the used modules. Add your routine calls here. ; Call run functions of the used modules. Add your routine calls here.
; ;
@@ -160,24 +164,20 @@ initModules:
; - nothing ; - nothing
; USED: depending on called routines ; USED: depending on called routines
runModulesUntilIdle: runModules:
rcall BaseTimer_Run
#ifdef MODULES_TIMER
; TIMER module
rcall Timer_Run
#endif
#ifdef MODULES_COM #ifdef MODULES_COM
; COM module (call until carry flag cleared but at most 10 times to not starve other modules) ; COM module (call until carry flag cleared but at most 10 times to not starve other modules)
ldi r16, 10 ldi r16, 10
runModulesUntilIdle_Com: runModules_Com:
push r16 push r16
rcall Com2_Run rcall Com2_Run
pop r16 pop r16
brcc runModulesUntilIdle_ComEnd brcc runModules_ComEnd
dec r16 dec r16
brne runModulesUntilIdle_Com brne runModules_Com
runModulesUntilIdle_ComEnd: runModules_ComEnd:
#endif #endif
@@ -238,6 +238,7 @@ initialWait_l2: ; wait for 10ms
#ifdef MODULES_LCD #ifdef MODULES_LCD
printTimerMark: printTimerMark:

View File

@@ -46,6 +46,7 @@ cproHandleReboot_loop1:
; directly call bootloader ; directly call bootloader
cli cli
rcall systemSetBootSpeed ; reset speed if necessary (TODO: let bootloader set its speed)
rjmp BOOTLOADER_ADDR rjmp BOOTLOADER_ADDR
cproHandleReboot_notHandled: cproHandleReboot_notHandled:
clc clc