Skip to content

Commit

Permalink
Save 2 bytes in the watchdog handling code
Browse files Browse the repository at this point in the history
`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.
  • Loading branch information
sigprof authored and volium committed Feb 11, 2022
1 parent e2d8869 commit ce02b53
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions nanoBoot.S
Original file line number Diff line number Diff line change
Expand Up @@ -383,12 +383,9 @@ main:

; We MUST disable the Watchdog Timer first, otherwise it will remain enabled and will keep resetting the system, so...
; Disable Watchdog Timer
ldi r16, _BV(WDCE) | _BV(WDE) ; Load r16 with the value needed to "unlock" the Watchdog Timer Configuration
; Write a logic one to the Watchdog Change Enable bit (WDCE) and Watchdog System Reset Enable (WDE)

mov r17, rZERO ; Load r17 with zero to disable the Watchdog Timer completely

rcall set_watchdog_timer ; Call the subroutine that sets the watchdog timer with the values loaded in r16 and r17
rcall set_watchdog_timer ; Call the subroutine that sets the watchdog timer with the value loaded in r17

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

; Enable WDT, ~250 ms timeout (force a timeout to reset the AVR)
ldi r16, _BV(WDCE) | _BV(WDE) ; Load r16 with the value needed to "unlock" the Watchdog Timer Configuration
; Write a logic one to the Watchdog Change Enable bit (WDCE) and Watchdog System Reset Enable (WDE)

ldi r17, _BV(WDE) | _BV(WDP2) ; Load r17 with the value needed to set the desired Watchdog Configuration (WDCE = 0, not set!)
; Write the WDE and Watchdog prescaler bits (WDP); System Reset Mode (WDE = 1) and ~250 ms timeout (WDP2 = 1)

rcall set_watchdog_timer ; Call the subroutine that sets the wathdog timer with the values loaded in r16 and r17
rcall set_watchdog_timer ; Call the subroutine that sets the watchdog timer with the value loaded in r17

; for (;;);
final_loop:
Expand Down Expand Up @@ -1077,8 +1071,11 @@ EP_ISR_END:

set_watchdog_timer:

; IMPORTANT!! This function assumes the correct values for the WDTCSR register
; configuration are already loaded onto r16 and 17.
; IMPORTANT!! This function assumes the correct value for the WDTCSR register
; configuration is already loaded onto r17; it also modifies r16.

ldi r16, _BV(WDCE) | _BV(WDE) ; Load r16 with the value needed to "unlock" the Watchdog Timer Configuration
; Write a logic one to the Watchdog Change Enable bit (WDCE) and Watchdog System Reset Enable (WDE)

wdr ; Reset the Watchdog Timer

Expand Down

0 comments on commit ce02b53

Please sign in to comment.