Rebooting and flashing a node now works!

This commit is contained in:
Martin Preuss
2023-04-21 23:38:44 +02:00
parent 76e58b6fec
commit 97016b21b9
20 changed files with 468 additions and 14 deletions

View File

@@ -147,7 +147,7 @@ firmwareStart: rjmp main ; will be overwritten when flashing
; main code
.org 0xd00
.org AQHOME_BOOTLOADER_ADDR
main:
@@ -165,6 +165,7 @@ main:
.include "crc8.asm"
.include "flash.asm"
.include "flashproto.asm"
.include "watchdog.asm"

View File

@@ -175,7 +175,7 @@
; Reset and interrupt vectors (will be removed as soon as we can flash data over COM)
; rjmp main ; Reset vector
rjmp 0xd00 ; Reset vector ; use this for flashed system
rjmp AQHOME_BOOTLOADER_ADDR ; Reset vector ; use this for flashed system
reti ; EXT_INT0
rjmp com2IsrPcint0 ; PCI0
reti ; PCI1
@@ -205,6 +205,8 @@ firmwareStart: rjmp main
.include "utils.asm"
.include "crc8.asm"
.include "watchdog.asm"
#ifdef MODULES_TIMER
.include "timer.asm"
#endif
@@ -223,6 +225,7 @@ firmwareStart: rjmp main
.include "comproto_pong.asm"
.include "comproto_values.asm"
.include "comproto_device.asm"
.include "comproto_reboot.asm"
#endif
#endif
#ifdef MODULES_TWI_MASTER

View File

@@ -167,7 +167,7 @@ com2HandleNextPacketInQueue_retNc:
; - X : pointer to buffer to write to
; OUT:
; - CFLAG: set if okay, clear otherwise
; MODIFIED REGS: R16, R17, X, Y (R3, R4, R15, R16, R17, R18, R19, R20, R21)
; REGS: R16, R17, X, Y (R3, R4, R15, R16, R17, R18, R19, R20, R21)
COM2_WriteMsgWithCmdAndSrcAddr:
ldi r17, COM2_PAYLOAD_FLAGS_SECONDS

View File

@@ -76,6 +76,12 @@ CPRO_OnPacketReceived:
brne CPRO_OnPacketReceived_l1
rjmp cproHandlePing
CPRO_OnPacketReceived_l1:
#ifndef BASE_SYSTEM
cpi r16, CPRO_CMD_REBOOT_REQUEST
brne CPRO_OnPacketReceived_l2
rjmp cproHandleReboot
#endif
CPRO_OnPacketReceived_l2:
#ifdef MODULES_COM_WITH_ADDR_PROTO
rjmp CPRO_Address_OnPacketReceived
#else
@@ -102,6 +108,39 @@ cproHandlePing_notHandled:
ret
; ---------------------------------------------------------------------------
; Compare the UID from a message against out own UID.
;
;IN:
; - X: pointer to UID in a message to compare against out own uid
; OUT:
; - CFLAG set if matches, cleared otherwise
; REGS: r16, r18, r19, r20, r21, X
cproCheckUidInMsg:
push xl
push xh
rcall Utils_ReadUid
pop xh
pop xl
ld r16, X+
cp r16, r18
brne cproCheckUidInMsg_notMe
ld r16, X+
cp r16, r19
brne cproCheckUidInMsg_notMe
ld r16, X+
cp r16, r20
brne cproCheckUidInMsg_notMe
ld r16, X+
cp r16, r21
brne cproCheckUidInMsg_notMe
sec
ret
cproCheckUidInMsg_notMe:
clc
ret
CPRO_END:
.equ MODULE_SIZE_CPRO = CPRO_END-CPRO_BEGIN

View File

@@ -39,11 +39,14 @@
.equ CPRO_CMD_MEMSTATS = 81
.equ CPRO_CMD_SYSSTATS = 82
.equ CPRO_CMD_REBOOT_REQUEST = 90
.equ CPRO_CMD_REBOOT_RESPONSE = 91
.equ CPRO_PACKET_HAVEADDR_OFFS_ADDRESS = COM2_MSG_OFFS_PAYLOAD+4
.equ CPRO_PACKET_CLAIMADDR_OFFS_ADDRESS = COM2_MSG_OFFS_PAYLOAD+4
.equ CPRO_PACKET_DENYADDR_OFFS_ADDRESS = COM2_MSG_OFFS_PAYLOAD+4
.equ CPRO_PACKET_REBOOTREQ_OFFS_UID = COM2_MSG_OFFS_PAYLOAD+0
.equ CPRO_WAITTIME_INITIAL = 10
.equ CPRO_WAITTIME_GETADDR = 130

71
avr/comproto_reboot.asm Normal file
View File

@@ -0,0 +1,71 @@
; ***************************************************************************
; copyright : (C) 2023 by Martin Preuss
; email : martin@libchipcard.de
;
; ***************************************************************************
; * This file is part of the project "AqHome". *
; * Please see toplevel file COPYING of that project for license details. *
; ***************************************************************************
; ***************************************************************************
; code
.cseg
; ---------------------------------------------------------------------------
; Handle reboot request
;
; IN:
; - X : buffer containing the received packet
; OUT:
; - nothing
; REGS:
cproHandleReboot:
adiw xh:xl, CPRO_PACKET_REBOOTREQ_OFFS_UID
rcall cproCheckUidInMsg
brcc cproHandleReboot_notHandled
sbiw xh:xl, CPRO_PACKET_REBOOTREQ_OFFS_UID+4
adiw xh:xl, COM2_MSG_OFFS_SRCADDR
ld r16, x
cpi r16, 0xff
breq cproHandleReboot_notHandled ; dont handle src address 255
cproHandleReboot_loop1:
push r16
ldi xl, LOW(com2SendBuffer)
ldi xh, HIGH(com2SendBuffer)
rcall CPRO_WriteRebootResponse
rcall COM2_SendPacket
pop r16
brcc cproHandleReboot_loop1
; directly call bootloader
cli
rjmp AQHOME_BOOTLOADER_ADDR
cproHandleReboot_notHandled:
clc
ret
; ---------------------------------------------------------------------------
; Write a Reboot Response packet.
;
; IN:
; - R16: destination address
; - X : buffer to write to
; OUT:
; - nothing
; REGS: R3, R4, R16, R17, R18, X (R19, R20, R21)
CPRO_WriteRebootResponse:
ldi r18, CPRO_CMD_REBOOT_RESPONSE
rjmp COM2_WriteMsgWithCmdAndSrcAddr ; R3, R4, R15, R16, R17, R18, R19, R20, R21, X

View File

@@ -29,6 +29,7 @@
;.equ AQHOME_FW_START_ADDRESS_MAIN = 0x0500
.equ AQHOME_BOOTLOADER_ADDR = 0xd00
; ---------------------------------------------------------------------------
; EEPROM positions

View File

@@ -183,7 +183,7 @@ wait: ; wait for possibly previous SPM to complete
; OUT:
; - R16: byte read
; - X: EEPROM Address incremented
; MODIFIED REGISTERS: R16
; REGS: R16
flashReadEepromIncr:
sbic EECR, EEPE ; wait for previous write to complete (if any)
@@ -208,8 +208,8 @@ flashReadEepromIncr:
; REGS: R16, X
flashReadUid:
in r15, SREG
push r15
in r15, SREG
cli
ldi xl, LOW(EEPROM_OFFS_UUID)
ldi xh, HIGH(EEPROM_OFFS_UUID)
@@ -221,8 +221,8 @@ flashReadUid:
mov r20, r16
rcall flashReadEepromIncr ; (R16)
mov r21, r16
out SREG, r15
pop r15
out SREG, r15
ret

View File

@@ -69,6 +69,8 @@ bootLoader:
ldi r16, Low(RAMEND)
out SPL, r16 ; init LSB stack pointer
; rcall watchdogOff ; turn off watchdog timer (sometimes it stays on after reboot)
; setup pins and interrupts
cbi COM_PORT_DATA, COM_PINNUM_DATA ; disable internal pullup for DATA
cbi COM_DDR_DATA, COM_PINNUM_DATA ; set DATA port as input

View File

@@ -40,6 +40,8 @@ main:
.endif
ldi r16, Low(RAMEND)
out SPL, r16 ; init LSB stack pointer
rcall watchdogOff ; turn off watchdog timer (sometimes it stays on after reboot)
rcall initModules

View File

@@ -367,13 +367,14 @@ Utils_UpdateSeedInEeprom:
; Read UID from EEPROM.
;
; IN:
; - nothing
; OUT:
; - R18:R19:R20:R21: UID
; REGS: R16, X
Utils_ReadUid:
in r15, SREG
push r15
in r15, SREG
cli
ldi xl, LOW(EEPROM_OFFS_UUID)
ldi xh, HIGH(EEPROM_OFFS_UUID)
@@ -385,8 +386,8 @@ Utils_ReadUid:
mov r20, r16
rcall Utils_ReadEepromIncr ; (R16)
mov r21, r16
out SREG, r15
pop r15
out SREG, r15
ret

42
avr/watchdog.asm Normal file
View File

@@ -0,0 +1,42 @@
; ***************************************************************************
; copyright : (C) 2023 by Martin Preuss
; email : martin@libchipcard.de
;
; ***************************************************************************
; * This file is part of the project "AqHome". *
; * Please see toplevel file COPYING of that project for license details. *
; ***************************************************************************
; ***************************************************************************
; code
.cseg
watchdogOn:
in r16, WDTCSR
ori r16, (1<<WDE) | (1<<WDP0) ; about 32ms period
out WDTCSR, r16
ret
watchdogOff:
wdr ; reset WDT
in r16, WDTCSR
ori r16, (1<<WDCE)|(1<<WDE)
push r15
in r15, SREG
cli
out WDTCSR, r16
ldi r16, (0<<WDE) ; Turn off WDT
out WDTCSR, r16
out SREG, r15
pop r15
ret