Files
aqhomecontrol/avr/devices/t03/debugboot.asm

399 lines
9.9 KiB
NASM

; ***************************************************************************
; Source file for base system node on AtTiny 84
;
; This is for the maintenance system (i.e. the flash loader).
;
; All definitions and changes should go into this file.
; ***************************************************************************
;.equ clock=1000000 ; Define the clock frequency
.equ clock=8000000 ; Define the clock frequency
.nolist
.include "include/tn841def.inc" ; Define device ATtiny841
.list
.include "./defs.asm"
.include "defs_all.asm"
; ***************************************************************************
; defines
; ---------------------------------------------------------------------------
; generic
.include "common/utils_wait.asm"
.include "modules/com2/defs.asm"
.include "modules/comproto/defs.asm"
; ---------------------------------------------------------------------------
; firmware settings
.equ FIRMWARE_VERSION_MAJOR = 0
.equ FIRMWARE_VERSION_MINOR = 0
.equ FIRMWARE_VERSION_PATCHLEVEL = 1
;#define COM_ACCEPT_ALL_DEST 1
; ---------------------------------------------------------------------------
; LED
.equ LED_DDR = DDRB
.equ LED_PORT = PORTB
.equ LED_PIN = PINB
.equ LED_PINNUM = PORTB2
; ***************************************************************************
; code segment
.cseg
.org 0x0000
; ---------------------------------------------------------------------------
; Reset and interrupt vectors
rjmp main ; 1: RESET Reset vector use this for flashed system
reti ; 2: INT0 External Interrupt Request 0
reti ; 3: PCINT0 Pin Change Interrupt 0
reti ; 4: PCINT1 Pin Change Interrupt 1
reti ; 5: WDT Watchdog Time-out
reti ; 6: TIM1_CAPT Timer/Counter1 Capture Event
reti ; 7: TIM1_COMPA (OC1A) Timer/Counter1 Compare Match A
reti ; 8: TIM1_COMPB (OC1B) Timer/Counter1 Compare Match B
reti ; 9: TIM1_OVF (OVF1) Timer/Counter1 Overflow
reti ; 10: TIM0_COMPA (OC0A) Timer/Counter0 Compare Match A
reti ; 11: TIM0_COMPB (OC0B) Timer/Counter0 Compare Match B
reti ; 12: TIM0_OVF (OVF0) Timer/Counter0 Overflow
reti ; 13: ANA_COMP0 Analog Comparator 0
reti ; 14: ADC_READY ADC Conversion Complete
reti ; 15: EE_RDY (ERDY) EEPROM Ready
reti ; 16: ANA_COMP1 Analog Comparator 1
reti ; 17: TIM2_CAPT Timer/Counter2 Capture Event
reti ; 18: TIM2_COMPA (OC2A) Timer/Counter2 Compare Match A
reti ; 19: TIM2_COMPB (OC2B) Timer/Counter2 Compare Match B
reti ; 20: TIM2_OVF (OVF2) Timer/Counter2 Overflow
reti ; 21: SPI SPI Serial Transfer Complete
reti ; 22: USART0_RXS USART0 Rx Start
reti ; 23: USART0_RXC USART0 Rx Complete
reti ; 24: USART0_DRE USART0 Data Register Empty
reti ; 25: USART0_TXC USART0 Tx Complete
reti ; 26: USART1_RXS USART1 Rx Start
reti ; 27: USART1_RXC USART1 Rx Complete
reti ; 28: USART1_DRE USART1 Data Register Empty
reti ; 29: USART1_TXC USART1 Tx Complete
reti ; 30: TWI Two-Wire-Interface
reti ; 31: RESERVED reserved
devInfoBlock: ; 12 bytes
devInfoManufacturer: .db 'A', 'Q', 'U', 'A'
devInfoId: .db DEVICEINFO_ID, 0
devInfoVersion: .db DEVICEINFO_VERSION, DEVICEINFO_REVISION ; version, revision
firmwareVersion: .db FIRMWARE_VARIANT_BOOT, FIRMWARE_VERSION_MAJOR
.db FIRMWARE_VERSION_MINOR, FIRMWARE_VERSION_PATCHLEVEL
firmwareStart: rjmp main ; will be overwritten when flashing
; ***************************************************************************
; main code
.org BOOTLOADER_ADDR
main:
rjmp debugFlash
; rjmp debugEchoUart1
; rjmp bootLoader ; this routine is in modules/bootloader/main.asm
; ***************************************************************************
; includes
;.include "modules/uart_hw/raw_uart1.asm"
.include "modules/com2/crc.asm"
.include "common/utils_wait_fixed.asm"
.include "common/utils_copy_from_flash.asm"
.include "common/utils_copy_sdram.asm"
.include "modules/flash/defs.asm"
;.include "modules/flash/eeprom.asm"
;.include "modules/flash/io.asm"
;.include "modules/flash/io_uart1.asm"
.include "modules/flash/flash4p.asm"
;.include "modules/flash/flashprocess.asm"
.include "modules/flash/wait.asm"
;.include "modules/bootloader/main.asm"
systemSetSpeed:
.if clock == 1000000
ldi r17, 0xd8
ldi r16, (1<<CLKPS1) | (1<<CLKPS0) ; SUT=0, CLKPS=0011b
sts CCP, r17
sts CLKPR, r16
.endif
.if clock == 8000000
ldi r17, 0xd8
clr r16 ; SUT=0, CLKPS=0
sts CCP, r17
sts CLKPR, r16
.endif
ret
.dseg
debugFlashBuffer: .byte 32
flashUid: .byte 4
.cseg
debugFlash:
cli
; setup stack
.ifdef SPH ; if SPH is defined
ldi r16, High(RAMEND)
out SPH, r16 ; init MSB stack pointer
.endif
ldi r16, Low(RAMEND)
out SPL, r16 ; init LSB stack pointer
rcall systemSetSpeed
sbi LED_DDR, LED_PINNUM ; out
cbi LED_PORT, LED_PINNUM ; on
; set baudrate
.if clock == 8000000
ldi r16, 25 ; (19.2Kb/s at 8MHz)
ldi r17, 0
.endif
.if clock == 1000000
ldi r16, 3 ; (19.2Kb/s at 1MHz)
ldi r17, 0
.endif
sts UBRR1H, r17
sts UBRR1L, r16
; set character format (asynchronous USART, 8-bit, one stop bit, no parity)
ldi r16, (3<<UCSZ10)
sts UCSR1C, r16
; enable transceiver
lds r16, UCSR1B
; cbr r16, (1<<UDRIE1) ; disable DRE interrupt
ori r16, (1<<RXEN1) | (1<<TXEN1) ; enable transmit and receive
sts UCSR1B, r16
ldi xl, LOW(debugFlashBuffer)
ldi xh, HIGH(debugFlashBuffer)
ldi r17, 32
clr r16
debugFlash_loop1:
st X+, r16
inc r16
dec r17
brne debugFlash_loop1
rcall Flash_Init
ldi yl, LOW(debugFlashBuffer)
ldi yh, HIGH(debugFlashBuffer)
ldi zl, LOW(0x500)
ldi zh, HIGH(0x500)
ldi r17, 32
rcall Flash_WriteData
rcall Flash_Fini
debugFlash_loop2:
sbi LED_PIN, LED_PINNUM ; toggle
ldi zl, LOW(0x500)
ldi zh, HIGH(0x500)
ldi r18, 64
rcall debugDumpFlash
ldi r16, 50
rcall flashWaitForMulti100ms ; print every 5 secs
rjmp debugFlash_loop2
ret
debugWriteString:
lpm r16, Z+
tst r16
breq debugWriteString_done
rcall debugPrintOneChar ; (r17)
rjmp debugWriteString
debugWriteString_done:
ret
debugString: .db "FLASH", 13, 10, 0
#if 0
debugPrintLoop:
sbi LED_PORT, LED_PINNUM ; off
; enable transceiver
lds r17, UCSR1B
; cbr r17, (1<<UDRIE1) ; disable DRE interrupt
ori r17, (1<<RXEN1) | (1<<TXEN1) ; enable transmit and receive
sts UCSR1B, r17
debugPrintLoop1:
push r16
rcall debugPrintHexByte
ldi r16, 13
rcall debugPrintOneChar ; (r17)
ldi r16, 10
rcall debugPrintOneChar ; (r17)
ldi r16, 10
rcall flashWaitForMulti100ms
pop r16
rjmp debugPrintLoop1
#endif
; @param Z start of FLASH to dump
; @param r18: number of bytes to dump
debugDumpFlash:
push zh
push zl
ldi zl, LOW(debugString*2)
ldi zh, HIGH(debugString*2)
rcall debugWriteString
pop zl
pop zh
debugDumpFlash_loop1:
mov r16, zh
rcall debugPrintHexByte
mov r16, zl
rcall debugPrintHexByte
ldi r16, ':'
rcall debugPrintOneChar ; (r17)
ldi r16, ' '
rcall debugPrintOneChar ; (r17)
ldi r19, 16
debugDumpFlash_loop2:
lpm r16, Z+
rcall debugPrintHexByte
ldi r16, ' '
rcall debugPrintOneChar ; (r17)
dec r18
breq debugDumpFlash_done
dec r19
brne debugDumpFlash_loop2
ldi r16, 13
rcall debugPrintOneChar ; (r17)
ldi r16, 10
rcall debugPrintOneChar ; (r17)
rjmp debugDumpFlash_loop1
debugDumpFlash_done:
ldi r16, 13
rcall debugPrintOneChar ; (r17)
ldi r16, 10
rcall debugPrintOneChar ; (r17)
ret
; ---------------------------------------------------------------------------
; @routine debugPrintHexByte
;
; Convert a give byte into HEX and write it to USART1.
;
; @return CFLAG set if okay, cleared on error
; @param R16 byte to convert to print
; @clobbers r16, r20 (r17)
debugPrintHexByte:
mov r20, r16
swap r16
rcall debugNibbleToAscii ; write high nibble (r17)
rcall debugPrintOneChar ; (r17)
brcc debugPrintHexByte_error
mov r16, r20
rcall debugNibbleToAscii ; write low nibble (r17)
rcall debugPrintOneChar ; (r17)
brcc debugPrintHexByte_error
sec
ret
debugPrintHexByte_error:
clc
ret
; @end
; ---------------------------------------------------------------------------
; @routine debugNibbleToAscii
;
; Convert a nibble to an ASCII char.
; @return R16 ASCII representation of that nibble (e.g. '0' for 0)
; @param R16 byte (in bits 0-3)
; @clobbers r17
debugNibbleToAscii:
andi r16, 0xf
cpi r16, 10
brcs debugNibbleToAscii_l1
ldi r17, 7
add r16, r17
debugNibbleToAscii_l1:
ldi r17, '0'
add r16, r17
ret
; @end
; ---------------------------------------------------------------------------
; @routine debugPrintOneChar
;
; Write one byte to UART1.
; @param R16 byte to send
; @clobbers r17
debugPrintOneChar:
lds r17, UCSR1A
sbrs r17, UDRE1
rjmp debugPrintOneChar
sbr r17, (1<<TXC1)
sts UCSR1A, r17
sts UDR1, r16
sec
ret
; @end