started reorganizing code into subfolders.

This commit is contained in:
Martin Preuss
2023-04-22 00:04:10 +02:00
parent 97016b21b9
commit 5e12b8ad4e
40 changed files with 38 additions and 60 deletions

236
avr/modules/flash/flash.asm Normal file
View File

@@ -0,0 +1,236 @@
; ***************************************************************************
; 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
FLASH_BEGIN:
; ---------------------------------------------------------------------------
; start flashing a page
;
; Interrupts must be disabled!
;
; IN:
; - Z: Address to work on (byte address as for LPM!)
; OUT:
; - nothing
; REGS: R16 (R0, R1, R16, R20, R24, R25)
Flash_StartPage:
push zh
push zl
ldi r16, ~((PAGESIZE*2)-1)
and zl, r16
rcall Flash_ReadPageIntoPageBuffer ; (R0, R1, R16, R20, R24, R25, Z)
pop zl
pop zh
ret
; ---------------------------------------------------------------------------
; finish flashing a page
;
; Interrupts must be disabled!
;
; IN:
; - Z: Address to work on (byte address as for LPM!)
; OUT:
; - nothing
; REGS: R16 (R15, R20)
Flash_FinishPage:
ldi r16, ~((PAGESIZE*2)-1)
and zl, r16
rcall Flash_ErasePage ; (R15, R16, R20)
rcall Flash_WritePage ; (R15, R16, R20)
ret
; ---------------------------------------------------------------------------
; Flash_ReadPageIntoPageBuffer
;
; Interrupts must be disabled!
;
; IN:
; - Z: Address to read from (byte address as for LPM!)
; OUT:
; - nothing
; REGS: R0, R1, R16, R20, R24, R25, Z
Flash_ReadPageIntoPageBuffer:
ldi r24, LOW(PAGESIZE*2)
ldi r25, HIGH(PAGESIZE*2)
Flash_ReadPageIntoPageBuffer_loop:
lpm r0, Z+ ; read source data from FLASH (low)
lpm r1, Z ; read source data from FLASH (high)
sbiw ZH:ZL, 1 ; rewind Z address for following SPM
ldi r20, (1<<SPMEN) ; enable next SPM, write into temp page buffer
rcall flashDoSpm ; (R16)
adiw ZH:ZL, 2
sbiw r25:r24, 2 ; use subi for PAGESIZEB<=256
brne Flash_ReadPageIntoPageBuffer_loop
ret
; ---------------------------------------------------------------------------
; Flash_WriteIntoPage
;
; Interrupts must be disabled!
;
; IN:
; - Z: Address to write to (byte address as for LPM!)
; - R1:R0: Word to write into page
; OUT:
; - Z: address for next write
; REGS: R16
Flash_WriteIntoPage:
ldi r20, (1<<SPMEN) ; enable next SPM, write into temp page buffer
rcall flashDoSpm ; (R16)
adiw zh:zl, 2
ret
; ---------------------------------------------------------------------------
; Flash_ErasePage
;
; Interrupts must be disabled!
;
; IN:
; - Z: Address of the page to erase.
; OUT:
; - nothing
; REGS: R20 (R16)
Flash_ErasePage:
ldi r20, (1<<PGERS) + (1<<SPMEN) ; enable next SPM, erase page (R1/R0 ignored)
rcall flashDoSpm ; (R16)
ret
; ---------------------------------------------------------------------------
; Flash_WritePage
;
; Interrupts must be disabled!
;
; IN:
; - Z: Address of the page to erase.
; OUT:
; - nothing
; REGS: R15, R20 (R16)
Flash_WritePage:
ldi r20, (1<<PGWRT) + (1<<SPMEN) ; enable next SPM, write page (R1/R0 ignored)
rcall flashDoSpm ; (R16)
ret
; ---------------------------------------------------------------------------
; flashDoSpm
;
; wait until possible previous SPM finished and then issue another SPM.
;
; IN:
; - R20: value for register SPMCR
; - R0: low value for SPM
; - R1: high value for SPM
; - Z : address for SPM (byte address!)
; OUT:
; - nothing
; REGS: R16
flashDoSpm:
wait: ; wait for possibly previous SPM to complete
in r16, SPMCSR
sbrc r16, SPMEN
rjmp wait
; SPM timed sequence
out SPMCSR, r20
spm
ret
; ---------------------------------------------------------------------------
; flashReadEepromIncr
;
; Read a byte from EEPROM (see example in ATtiny24/44/84 manual p.19).
;
; IN:
; - X: EEPROM Address to read from
; OUT:
; - R16: byte read
; - X: EEPROM Address incremented
; REGS: R16
flashReadEepromIncr:
sbic EECR, EEPE ; wait for previous write to complete (if any)
rjmp flashReadEepromIncr
out EEARH, xh ; set EEPROM address
out EEARL, xl
sbi EECR, EERE ; start EEPROM read by writing EERE
in r16, EEDR ; read data from data register
adiw xh:xl, 1
ret
; ---------------------------------------------------------------------------
; flashReadUid
;
; Read UID from EEPROM.
;
; IN:
; OUT:
; - R18:R19:R20:R21: UID
; REGS: R16, X
flashReadUid:
push r15
in r15, SREG
cli
ldi xl, LOW(EEPROM_OFFS_UUID)
ldi xh, HIGH(EEPROM_OFFS_UUID)
rcall flashReadEepromIncr ; (R16)
mov r18, r16
rcall flashReadEepromIncr ; (R16)
mov r19, r16
rcall flashReadEepromIncr ; (R16)
mov r20, r16
rcall flashReadEepromIncr ; (R16)
mov r21, r16
out SREG, r15
pop r15
ret
FLASH_END:
.equ MODULE_SIZE_FLASH = FLASH_END-FLASH_BEGIN

View File

@@ -0,0 +1,600 @@
; ***************************************************************************
; 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. *
; ***************************************************************************
.equ FLASH_ERROR_NONE = 0
.equ FLASH_ERROR_MSGERROR = 1
.equ FLASH_RECVBUFFER_MAXLEN = 128
.equ FLASH_CMD_FLASH_RSP = 74
.equ FLASH_MSG_OFFS_DESTADDR = 0
.equ FLASH_MSG_OFFS_MSGLEN = 1
.equ FLASH_MSG_OFFS_MSGDATA = 2
.equ FLASH_MSG_OFFS_CMD = 2 ; first at COM2_MSG_OFFS_MSGDATA
.equ FLASH_MSG_OFFS_SRCADDR = 3
.equ FLASH_MSG_OFFS_PAYLOAD = 4 ; payload for the cmd follows here
.equ FLASH_PACKET_DATA_OFFS_ADDR = FLASH_MSG_OFFS_PAYLOAD+0 ; 4 bytes
.equ FLASH_PACKET_DATA_OFFS_DATA = FLASH_MSG_OFFS_PAYLOAD+4 ; n bytes
.equ FLASH_PACKET_START_OFFS_UID = FLASH_MSG_OFFS_PAYLOAD+0 ; 4 bytes
; ***************************************************************************
; data
.dseg
flashDataBegin:
flashUid: .byte 4
flashSendBuffer: .byte 32
flashRecvBuffer: .byte FLASH_RECVBUFFER_MAXLEN
flashDataEnd:
; ***************************************************************************
; code
.cseg
FLASH_PROTO_BEGIN:
bootLoader:
cli ; disable interrupts throughout the whole process
; 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 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
cbi COM_PORT_ATTN, COM_PINNUM_ATTN ; disable internal pullup for ATTN
cbi COM_DDR_ATTN, COM_PINNUM_ATTN ; set ATTN port as input
sbi DDRA, PORTA3 ; out
cbi PORTA, PORTA3 ; on
sbi DDRA, PORTA2 ; out (debug)
sbi PORTA, PORTA2 ; off (debug)
rcall flashReadUid
sts flashUid, r18
sts flashUid+1, r19
sts flashUid+2, r20
sts flashUid+3, r21
; wait for 3 secs before sending message
ldi r16, 30
rcall flashWaitForMulti100ms
; send flash ready message
ldi xl, LOW(flashSendBuffer)
ldi xh, HIGH(flashSendBuffer)
rcall flashWriteFlashReady
rcall flashSendPacketUntilSuccess
sbi PORTA, PORTA3 ; LED off
bootLoader_loop1:
; wait up to 10s for incoming FLASH_START message
ldi r16, CPRO_CMD_FLASH_START
ldi r17, 100 ; 100*100ms=10s
rcall flashWaitForSpecificMessageWithLed
brcc bootLoader_startFirmware
; either FLASH_START or FLASH_END received
cpi r16, CPRO_CMD_FLASH_START ; not FLASH_START, no flashing requested
brne bootLoader_startFirmware
; receive and flash new firmware
rcall flashHandleFlashStart
brcc bootLoader_loop1
; received a FLASH START msg, handle flashing
rcall bootLoaderFlash
brcc bootLoader_waitAndRestartBootLoader
; try to start firmware
bootLoader_startFirmware:
sbi PORTA, PORTA3 ; LED off
ldi r16, 20
rcall flashWaitForMulti100ms
rjmp firmwareStart
bootLoader_waitAndRestartBootLoader:
sbi PORTA, PORTA3 ; LED off
ldi r16, 20
rcall flashWaitForMulti100ms
rjmp bootLoader
bootLoaderFlash:
bootLoaderFlash_loop1:
; wait up to 10s for incoming FLASH_DATA message
ldi r16, CPRO_CMD_FLASH_DATA
ldi r17, 100 ; 100*100ms=10s
rcall flashWaitForSpecificMessageWithLed
brcc bootLoaderFlash_error
; either FLASH_DATA or FLASH_END received
cpi r16, CPRO_CMD_FLASH_DATA ; not FLASH_DATA, flashing ended/aborted
brne bootLoaderFlash_end
; flash data
rcall flashHandleFlashData
rjmp bootLoaderFlash_loop1
bootLoaderFlash_end:
sec
ret
bootLoaderFlash_error:
clc
ret
; ---------------------------------------------------------------------------
; wait for a specific message to arrive within given time, flashing LED
;
; IN:
; - R16: message type to receive
; - R17: wait time in 100ms (1=100ms, 2=200ms etc.)
; OUT:
; - CFLAG: set if msg received (either expected msg or FLASH_END), cleared otherwise
; - R16: message type received (if CFLAG set)
; REGS: R2, R16, R17 (R1, R24, R25, X)
flashWaitForSpecificMessageWithLed:
mov r2, r16
sbi PORTA, PORTA3 ; LED off
flashWaitForSpecificMessageWithLed_loop:
sbi PINA, PORTA3 ; toggle LED
mov r16, r2
push r17
ldi r17, 100 ; wait up to 100ms
rcall flashWaitForSpecificMessage ; (R1, R16, R17, R24, R25, X)
pop r17
brcs flashWaitForSpecificMessageWithLed_received
dec r17
brne flashWaitForSpecificMessageWithLed_loop
sbi PORTA, PORTA3 ; off
clc
ret
flashWaitForSpecificMessageWithLed_received:
sbi PORTA, PORTA3 ; off
sec
ret
; ---------------------------------------------------------------------------
; wait for a specific message to arrive within given time.
;
; IN:
; - R16: msg command to wait for
; - R17: time to wait for packet (in milliseconds)
; OUT:
; - CFLAG: set if msg received, cleared on timeout
; - R16 : message type received
; REGS: R1, R16, R17, X (R24, R25)
flashWaitForSpecificMessage:
mov r1, r16 ; expected message type
flashWaitForSpecificMessage_loop0:
; wait for ATTN to go low
flashWaitForSpecificMessage_loop1:
ldi r16, 0 ; wait for low
rcall flashWaitForAttnState1ms ; (R22, R24)
brcs flashWaitForSpecificMessage_isLow
dec r17
brne flashWaitForSpecificMessage_loop1
rjmp flashWaitForSpecificMessage_timeout
; receive message
flashWaitForSpecificMessage_isLow: ; is low, receive message, check for msg type
push r17
ldi r16, COM2_MAINTENANCE_ADDR
ldi r17, FLASH_RECVBUFFER_MAXLEN-3
ldi xl, LOW(flashRecvBuffer)
ldi xh, HIGH(flashRecvBuffer)
rcall com2ReceivePacketRaw
pop r17
brcc flashWaitForSpecificMessage_waitAttnHigh
ldi xl, LOW(flashRecvBuffer)
ldi xh, HIGH(flashRecvBuffer)
adiw xh:xl, COM2_MSG_OFFS_CMD
ld r16, X
sbiw xh:xl, COM2_MSG_OFFS_CMD
cp r16, r1
breq flashWaitForSpecificMessage_received
cpi r16, CPRO_CMD_FLASH_END
breq flashWaitForSpecificMessage_received
flashWaitForSpecificMessage_waitAttnHigh:
dec r17
breq flashWaitForSpecificMessage_timeout
; wait for ATTN to go high
flashWaitForSpecificMessage_loop2:
ldi r16, 0xff ; wait for high
rcall flashWaitForAttnState1ms ; (R22, R24)
brcs flashWaitForSpecificMessage_isHigh
dec r17
brne flashWaitForSpecificMessage_loop2
rjmp flashWaitForSpecificMessage_timeout
flashWaitForSpecificMessage_isHigh:
rjmp flashWaitForSpecificMessage_loop0
flashWaitForSpecificMessage_received: ; R16 contains message type
sec
ret
flashWaitForSpecificMessage_timeout:
clc
ret
; ---------------------------------------------------------------------------
; Wait for up to 1ms for ATTN line to reach the given state
;
; IN:
; - R16: expected state (0xff for high, 0 for low)
; OUT:
; - CFLAG: set if state reached, cleared otherwise
; REGS: R24 (R22)
flashWaitForAttnState1ms:
ldi r24, 100
flashWaitForAttnState1ms_loop:
push r17
in r17, COM_PIN_ATTN
eor r17, r16
andi r17, (1<<COM_PINNUM_ATTN)
pop r17
breq flashWaitForAttnState1ms_stateReached
Utils_WaitNanoSecs 10000, 0, r22 ; wait for 10us
dec r24
brne flashWaitForAttnState1ms_loop
clc
ret
flashWaitForAttnState1ms_stateReached:
sec
ret
; ---------------------------------------------------------------------------
; wait indefinately until ATTN line is high, send packet over wire, retry until successfull
;
; IN:
; - x : ptr to buffer to send
; OUT:
; - nothing
; REGS: R15, R16, R22 (R17, R21, X)
flashSendPacketUntilSuccess:
push xl
push xh
rcall COM2_SendPacketWithAttn
pop xh
pop xl
brcc flashSendPacket_error
ret
flashSendPacket_error:
rcall flashWaitForAttnHigh
rjmp flashSendPacketUntilSuccess
; ---------------------------------------------------------------------------
; wait indefinately for free ATTN line
;
; IN:
; - nothing
; OUT:
; - nothing
; REGS: R15, R16, R22 (R17, R21, X)
flashWaitForAttnHigh:
rcall com2WaitForAttnHigh ; waits for up to 100us
brcc flashWaitForAttnHigh_stillLow
ret
flashWaitForAttnHigh_stillLow:
rcall flashWaitDependingOnUid ; (R16, R18, R22, R24, R25)
rjmp flashWaitForAttnHigh
; ---------------------------------------------------------------------------
; wait depending on lowest byte of uid.
;
; Wait interval is between 100ms and 25s (i.e. 255*100ms). This is used to avoid
; all nodes on the network trying to send messages at the exact same time (e.g. after
; power outage which would affect all nodes at the same time).
;
; IN:
; - nothing
; OUT:
; - nothing
; REGS: R16 (R18, R22, R24, R25)
flashWaitDependingOnUid:
lds r16, flashUid
tst r16
brne flashWaitDependingOnUid_l1
ldi r16, 17
flashWaitDependingOnUid_l1:
rjmp flashWaitForMulti100ms
; ---------------------------------------------------------------------------
; wait for multiples of 100ms.
;
; IN:
; - R16: number of 100ms loops
; OUT:
; - nothing
; REGS: R16 (R18, R22, R24, R25)
flashWaitForMulti100ms:
flashWaitForMulti100ms_loop:
rcall flashWaitFor100ms ; (R18, R22, R24, R25)
dec r16
brne flashWaitForMulti100ms_loop
ret
; ---------------------------------------------------------------------------
; wait for 100 milliseconds.
;
; IN:
; - nothing
; OUT:
; - nothing
; REGS: R18 (R22, R24, R25)
flashWaitFor100ms:
ldi r18, 100
flashWaitFor100ms_loop:
rcall flashWaitFor1ms ; (R22, R24, R25)
dec r18
brne flashWaitFor100ms_loop
ret
; ---------------------------------------------------------------------------
; wait for 1 millisecond.
;
; IN:
; - nothing
; OUT:
; - nothing
; REGS: R24 (R22)
flashWaitFor1ms:
ldi r24, 10
flashWaitFor1ms_loop:
Utils_WaitNanoSecs 100000, 0, r22 ; wait for 100us
dec r24
brne flashWaitFor1ms_loop
ret
; ---------------------------------------------------------------------------
; Write a FLASH_RESPONSE packet.
;
; IN:
; - R16: response code (0 if ok, error code otherwise)
; - X : buffer to write to
; OUT:
; - nothing
; MODIFIED REGS: R16, R17 (R18, R19, R20)
flashWriteFlashRsp:
clr r18
st X+, r18 ; dest address (unused)
ldi r18, 3 ; msg code+src address+one payload byte
st X+, r18 ; msg len
ldi r17, CPRO_CMD_FLASH_RSP
st X+, r17 ; msg code
clr r17
st X+, r17 ; src address (not used)
st X, r16 ; payload byte
sbiw xh:xl, 4
rcall com2CalcAndAddChecksumByte ; (R16, R17, R18, R19, R20, X)
sbiw xh:xl, 5
ret
; ---------------------------------------------------------------------------
; Write a FLASH_READY packet.
;
; IN:
; - X : buffer to write to
; OUT:
; - nothing
; MODIFIED REGS: R16, Z (R17, R18, R19, R20)
flashWriteFlashReady:
ldi r16, 0xff
st X+, r16 ; dest address (unused)
ldi r16, 12 ; msg code+src address+ten payload bytes
st X+, r16 ; msg len
ldi r16, CPRO_CMD_FLASH_READY
st X+, r16 ; msg code
ldi r16, COM2_MAINTENANCE_ADDR
st X+, r16 ; src address (not used)
; payload
lds r16, flashUid
st X+, r16
lds r16, flashUid+1
st X+, r16
lds r16, flashUid+2
st X+, r16
lds r16, flashUid+3
st X+, r16
ldi zl, low(firmwareType*2)
ldi zh, HIGH(firmwareType*2)
lpm r16, Z+
st X+, r16 ; firmwareType (low)
lpm r16, Z+
st X+, r16 ; firmwareType (high)
lpm r16, Z+
st X+, r16 ; firmwareVersion (low)
lpm r16, Z+
st X+, r16 ; firmwareVersion (high)
ldi r16, LOW(PAGESIZE*2)
st X+, r16
ldi r16, HIGH(PAGESIZE*2)
st X, r16
sbiw xh:xl, 13
rcall com2CalcAndAddChecksumByte ; (R16, R17, R18, R19, R20, X)
sbiw xh:xl, 14
ret
; ---------------------------------------------------------------------------
; Handle FLASH START packet.
;
; IN:
; - X : buffer containing the message
; OUT:
; - CFLAG: set if message is for us, cleared otherwise
; REGS: r16, r18. r19. r20, r21, X (R15, R17, R22)
flashHandleFlashStart:
lds r18, flashUid
lds r19, flashUid+1
lds r20, flashUid+2
lds r21, flashUid+3
adiw xh:xl, FLASH_PACKET_START_OFFS_UID
ld r16, X+
cp r16, r18
brne flashHandleFlashStart_notMe
ld r16, X+
cp r16, r19
brne flashHandleFlashStart_notMe
ld r16, X+
cp r16, r20
brne flashHandleFlashStart_notMe
ld r16, X
cp r16, r21
brne flashHandleFlashStart_notMe
; okay, flash start message is for us
rcall flashWaitFor100ms
rcall flashSendFlashResponse ; (R15, R16, R17, R18, R19, R20, R21, R22, X)
sec
ret
flashHandleFlashStart_notMe:
clc
ret
; ---------------------------------------------------------------------------
; Handle FLASH DATA packet.
;
; IN:
; - X : buffer containing the message
; OUT:
; - CFLAG: set if message is for us, cleared otherwise
; REGS: R0, R1, R15, R16, R17, R18, R19, R20, R21, R24, R25, X, Z
flashHandleFlashData:
adiw xh:xl, FLASH_MSG_OFFS_MSGLEN
ld r18, X ; length (subtract 6
cpi r18, 6 ; cmd(1), src(1), addr(4)
brcs flashHandleFlashData_badData
subi r18, 6 ; remaining length
adiw xh:xl, FLASH_PACKET_DATA_OFFS_ADDR-FLASH_MSG_OFFS_MSGLEN
ld zl, X+ ; address (low)
ld zh, X+ ; address (high)
adiw xh:xl, 2 ; ignore high bytes, points to first data byte now
; rcall Flash_StartPage ; (R0, R1, R16, R20, R24, R25)
push zl
push zh
flashHandleFlashData_loop:
ld r0, X+
ld r1, X+
rcall Flash_WriteIntoPage ; (R15, R16, Z+)
subi r18, 2
brne flashHandleFlashData_loop
pop zh
pop zl
rcall Flash_FinishPage ; (R15, R16, R20)
clr r16
rjmp flashHandleFlashData_sendResponse
flashHandleFlashData_badData:
ldi r16, FLASH_ERROR_MSGERROR
flashHandleFlashData_sendResponse:
rcall flashSendFlashResponse ; (R15, R16, R17, R18, R19, R20, R21, R22, X)
sec
ret
; ---------------------------------------------------------------------------
; Create and send a FLASH RESPONSE packet
;
; IN:
; - R16: response code to send
; OUT:
; - nothing
; REGS: r16, X (R15, R17, R18, R19, R20, R21, R22)
flashSendFlashResponse:
; send flash ready message
ldi xl, LOW(flashSendBuffer)
ldi xh, HIGH(flashSendBuffer)
clr r16
rcall flashWriteFlashRsp ; (R16, R17, R18, R19, R20)
rjmp flashSendPacketUntilSuccess ; (R15, R16, R17, R21, R22, X)
FLASH_PROTO_END:
.equ MODULE_SIZE_FLASH_PROTO = FLASH_PROTO_END-FLASH_PROTO_BEGIN