Skip to content

Commit ce02b53

Browse files
sigprofvolium
authored andcommitted
Save 2 bytes in the watchdog handling code
`set_watchdog_timer` was called in two places, but the parameter value passed in r16 (the value used to unlock the watchdog configuration) was the same in both cases. Move the duplicated initialization of r16 into the `set_watchdog_timer` function itself, saving 2 bytes.
1 parent e2d8869 commit ce02b53

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed

nanoBoot.S

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -383,12 +383,9 @@ main:
383383

384384
; We MUST disable the Watchdog Timer first, otherwise it will remain enabled and will keep resetting the system, so...
385385
; Disable Watchdog Timer
386-
ldi r16, _BV(WDCE) | _BV(WDE) ; Load r16 with the value needed to "unlock" the Watchdog Timer Configuration
387-
; Write a logic one to the Watchdog Change Enable bit (WDCE) and Watchdog System Reset Enable (WDE)
388-
389386
mov r17, rZERO ; Load r17 with zero to disable the Watchdog Timer completely
390387

391-
rcall set_watchdog_timer ; Call the subroutine that sets the watchdog timer with the values loaded in r16 and r17
388+
rcall set_watchdog_timer ; Call the subroutine that sets the watchdog timer with the value loaded in r17
392389

393390
; check_reset_flags:
394391
sbrs rMCUSR, EXTRF ; Skip the next instruction if EXTRF is set (if External Reset Flag, skip next instruction, go to run_bootloader)
@@ -613,13 +610,10 @@ exit_bootloader:
613610
; NOTE!! This part of the code assumes MCUSR has already been cleared
614611

615612
; Enable WDT, ~250 ms timeout (force a timeout to reset the AVR)
616-
ldi r16, _BV(WDCE) | _BV(WDE) ; Load r16 with the value needed to "unlock" the Watchdog Timer Configuration
617-
; Write a logic one to the Watchdog Change Enable bit (WDCE) and Watchdog System Reset Enable (WDE)
618-
619613
ldi r17, _BV(WDE) | _BV(WDP2) ; Load r17 with the value needed to set the desired Watchdog Configuration (WDCE = 0, not set!)
620614
; Write the WDE and Watchdog prescaler bits (WDP); System Reset Mode (WDE = 1) and ~250 ms timeout (WDP2 = 1)
621615

622-
rcall set_watchdog_timer ; Call the subroutine that sets the wathdog timer with the values loaded in r16 and r17
616+
rcall set_watchdog_timer ; Call the subroutine that sets the watchdog timer with the value loaded in r17
623617

624618
; for (;;);
625619
final_loop:
@@ -1077,8 +1071,11 @@ EP_ISR_END:
10771071

10781072
set_watchdog_timer:
10791073

1080-
; IMPORTANT!! This function assumes the correct values for the WDTCSR register
1081-
; configuration are already loaded onto r16 and 17.
1074+
; IMPORTANT!! This function assumes the correct value for the WDTCSR register
1075+
; configuration is already loaded onto r17; it also modifies r16.
1076+
1077+
ldi r16, _BV(WDCE) | _BV(WDE) ; Load r16 with the value needed to "unlock" the Watchdog Timer Configuration
1078+
; Write a logic one to the Watchdog Change Enable bit (WDCE) and Watchdog System Reset Enable (WDE)
10821079

10831080
wdr ; Reset the Watchdog Timer
10841081

0 commit comments

Comments
 (0)