avr: implement setvalue for n12.

This commit is contained in:
Martin Preuss
2024-09-05 21:36:24 +02:00
parent 984cccc25b
commit 6b0972d76e
4 changed files with 186 additions and 17 deletions

View File

@@ -62,6 +62,8 @@
.equ VALUE_ID_REED1 = 0x04
.equ VALUE_ID_REED2 = 0x05
.equ VALUE_ID_REED_CONF = 0x81
; ***************************************************************************
@@ -193,10 +195,11 @@ sramTimerEnqueueValues: .byte 2
.cseg
#ifdef MODULES_LED
ledA3Flash: .db DDRA+0x20, PORTA+0x20, PINA+0x20, (1<<PORTA3)
blinkPattern: .db 2, 50, 0xff, 0xff ; 1 short blink, 5s pause, restart
;blinkPattern2: .db 2, 2, 0xff, 0xff ; 1 short blink, short pause, restart
#endif
@@ -325,20 +328,57 @@ onEvery100ms:
; ---------------------------------------------------------------------------
; onPacketReceived:
; @routine onPacketReceived:
;
; Called after a packet was received via COM module. Add your routine calls here.
;
; The packet will be released in any case after return from this call.
; IN:
; - X : pointer to received buffer
; OUT:
; - CFLAG: set if handled, cleared otherwise
; USED: depending on called routines
;
; @return CFLAG set if message handled, cleared otherwise
; @param X pointer to received buffer
; @clobbers all
onPacketReceived:
rcall CPRO_OnPacketReceived
; get msg code
adiw xh:xl, COM2_MSG_OFFS_CMD
ld r16, x
sbiw xh:xl, COM2_MSG_OFFS_CMD
cpi r16, CPRO_CMD_VALUE_SET
brne onPacketReceived_l1
; msg code is CPRO_CMD_VALUE_SET
; TODO
onPacketReceived_l1:
rjmp CPRO_OnPacketReceived
; @end
onSetValueReceived:
adiw xh:xl, CPRO_PACKET_SETVALUE_OFFS_VALUEID
ld r16, X
sbiw xh:xl, CPRO_PACKET_SETVALUE_OFFS_VALUEID
cpi r16, VALUE_ID_REED_CONF
brne onSetValueReceived_l1
rjmp onSetReedConf
onSetValueReceived_l1:
clc
ret
onSetReedConf:
; get new value
adiw xh:xl, CPRO_PACKET_SETVALUE_OFFS_VALUE
ld r16, X
sbiw xh:xl, CPRO_PACKET_SETVALUE_OFFS_VALUE
; set config
rcall REED_SetConfig
; send ACK
ldi r16, CPRO_CMD_VALUE_SET_ACK
rcall CPRO_SendSetValueResponse
sec
ret

View File

@@ -51,6 +51,11 @@
.equ CPRO_CMD_CONFIG_MODULE_RESPONSE = 101
.equ CPRO_PACKET_SETVALUE_OFFS_VALUEID = 6
.equ CPRO_PACKET_SETVALUE_OFFS_VALUE = 8
.equ CPRO_PACKET_SETVALUE_OFFS_DENOM = 10
.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

View File

@@ -17,7 +17,7 @@
; ---------------------------------------------------------------------------
; @routine CPRO_WriteReportValue
; @routine CPRO_WriteReportValue @global
; Write a REPORT_VALUE packet.
;
; @return nothing
@@ -56,16 +56,72 @@ CPRO_WriteReportValue:
; ---------------------------------------------------------------------------
; @routine CPRO_WriteSetValueResponse
; @routine CPRO_WriteSetValueResponse @global
; Write response to a SET_VALUE request.
;
; @return nothing
; @param R16 message code (CPRO_CMD_VALUE_SET_ACK or CPRO_CMD_VALUE_SET_NACK)
; @param X buffer to write to
; @param Y buffer with received setValueRequest message
; @clobbers R16, R17, R18, R19, (Y)
; @param R16 message code (CPRO_CMD_VALUE_SET_ACK or CPRO_CMD_VALUE_SET_NACK)
; @param X buffer to write to
; @param Y buffer with received setValueRequest message
; @clobbers R16, R17, R18, R19, (Y)
CPRO_WriteSetValueResponse:
rcall cproInvertValueMsg ; (R16, R17, R18, R19, Y)
; add checksum byte
rcall com2CalcAndAddChecksumByte ; (R16, R17, R18, R19, X)
sbiw xh:xl, 13 ; go back to beginning of message
ret
; @end
; ---------------------------------------------------------------------------
; @routine CPRO_WriteGetValueResponse @global
; Write response to a SET_VALUE request.
;
; @return nothing
; @param R16 message code (CPRO_CMD_VALUE_SET_ACK or CPRO_CMD_VALUE_SET_NACK)
; @param R19:R18 value
; @param R21:R20 denom (e.g. 100, meaning value must be divided by 100, 0=don't change)
; @param X buffer to write to
; @param Y buffer with received setValueRequest message
; @clobbers R16, R17, R18, R19, (Y)
CPRO_WriteGetValueResponse:
push r18
push r19
rcall cproInvertValueMsg ; (R16, R17, R18, R19, Y)
pop r19
pop r18
; replace values
adiw xh:xl, CPRO_PACKET_SETVALUE_OFFS_VALUE
st X+, r18 ; value (low)
st X+, r19 ; value (high)
st X+, r20 ; denom (low)
st X+, r21 ; denom (high)
sbiw xh:xl, CPRO_PACKET_SETVALUE_OFFS_VALUE+4
; add checksum byte
rcall com2CalcAndAddChecksumByte ; (R16, R17, R18, R19, X)
sbiw xh:xl, 13 ; go back to beginning of message
ret
; @end
; ---------------------------------------------------------------------------
; @routine cproInvertValueMsg
; Write response to a SET_VALUE request.
;
; @return nothing
; @param R16 message code (CPRO_CMD_VALUE_SET_ACK or CPRO_CMD_VALUE_SET_NACK)
; @param X buffer to write to
; @param Y buffer with received setValueRequest message
; @clobbers R16, R17, R18, R19, (Y)
cproInvertValueMsg:
; copy received message into new message
mov r19, r16
ldi r18, 12 ; message size without checksum byte
@@ -84,13 +140,61 @@ CPRO_WriteSetValueResponse:
adiw xh:xl, 2 ; msg code
st X, r19
sbiw xh:xl, 2
ret
; add checksum byte
rcall com2CalcAndAddChecksumByte ; (R16, R17, R18, R19, X)
sbiw xh:xl, 13 ; go back to beginning of message
; ---------------------------------------------------------------------------
; @routine CPRO_SendSetValueResponse @global
; Write response to a SET_VALUE request.
;
; @return CFLAG set if okay, cleared on error
; @param R16 message code (CPRO_CMD_VALUE_SET_ACK or CPRO_CMD_VALUE_SET_NACK)
; @param X buffer to write to
; @param Y buffer with received setValueRequest message
; @clobbers Y, (R16, R17, R18, R19, R22)
CPRO_SendSetValueResponse:
push xh
push xl
mov yh, xh
mov yl, xl
ldi xl, LOW(com2SendBuffer)
ldi xh, HIGH(com2SendBuffer)
rcall CPRO_WriteSetValueResponse ; (R16, R17, R18, R19, Y)
rcall COM2_SendPacket ; (r18, r19, r22, X)
pop xl
pop xh
ret
; @end
; ---------------------------------------------------------------------------
; @routine CPRO_SendGetValueResponse @global
; Write response to a GET_VALUE request.
;
; @return CFLAG set if okay, cleared on error
; @param R16 message code (CPRO_CMD_VALUE_SET_ACK or CPRO_CMD_VALUE_SET_NACK)
; @param R19:R18 value
; @param R21:R20 denom (e.g. 100, meaning value must be divided by 100, 0=don't change)
; @param X buffer to write to
; @param Y buffer with received getValueRequest message
; @clobbers Y, (R16, R17, R18, R19, R22)
CPRO_SendGetValueResponse:
push xh
push xl
mov yh, xh
mov yl, xl
ldi xl, LOW(com2SendBuffer)
ldi xh, HIGH(com2SendBuffer)
rcall CPRO_WriteGetValueResponse ; (R16, R17, R18, R19, Y)
rcall COM2_SendPacket ; (r18, r19, r22, X)
pop xl
pop xh
ret
; @end

View File

@@ -170,6 +170,25 @@ REED_Every100ms:
; ---------------------------------------------------------------------------
; @routine REED_SetConfig
;
; Set config and write to EEPROM
;
; @return nothing
; @param r16 new configuration
; @clobbers r16, X, (R17)
REED_SetConfig:
sts reedConfigFromEeprom, r16
ldi xh, HIGH(EEPROM_OFFS_REED_CONF) ; write config to EEPROM
ldi xl, LOW(EEPROM_OFFS_REED_CONF) ;
rcall Utils_WriteEepromIncr ; (R17)
ret
; @end
; ---------------------------------------------------------------------------
; @routine reedSetupConfig
;
@@ -194,6 +213,7 @@ reedSetupConfig_haveConfig:
sts reedConfigFromEeprom, r16
reedSetupConfig_end:
ret
; @end