avr: try calculating idle times.

This commit is contained in:
Martin Preuss
2023-04-10 23:33:24 +02:00
parent 6c8f8e19b2
commit eca6fc6efc
15 changed files with 315 additions and 7 deletions

View File

@@ -228,6 +228,7 @@
.include "comproto_stats.asm"
.include "comproto_device.asm"
.include "comproto_memstats.asm"
.include "comproto_sysstats.asm"
.include "comproto_values.asm"
;.include "comproto_debug.asm"
;.include "comproto_twi.asm"
@@ -246,6 +247,7 @@
sramTimerProtocolEverySec: .byte 2
sramTimerEnqueueComStats: .byte 2
sramTimerEnqueueMemStats: .byte 2
sramTimerEnqueueSysStats: .byte 2
sramTimerEnqueueValues: .byte 2
sramTimerSI7021Measure: .byte 2
@@ -272,6 +274,7 @@ timerList:
.dw sramTimerProtocolEverySec, CPRO_OnEverySecond, 0, 1 ; every 1s
.dw sramTimerEnqueueComStats, enqueueComStats, TIMER_FLAGS_IF_ADDR, 300 ; every 5m
.dw sramTimerEnqueueMemStats, enqueueMemStats, TIMER_FLAGS_IF_ADDR, 300 ; every 5m
.dw sramTimerEnqueueSysStats, enqueueSysStats, TIMER_FLAGS_IF_ADDR, 60 ; every 60s
.dw sramTimerSI7021Measure, SI7021_PeriodicMeasurement, 0, 30 ; every 30s
.dw sramTimerEnqueueValues, Main_SendValueMsg, TIMER_FLAGS_IF_ADDR, 60 ; every 1m
.dw 0 ; end of list
@@ -311,6 +314,11 @@ enqueueMemStats:
rjmp CPRO_EnqueueDevice
enqueueSysStats:
ldi r16, 0xff ; send to everybody
rjmp CPRO_EnqueueSysStats

View File

@@ -66,6 +66,8 @@ comDataBegin:
comRepeatCount: .byte 1
comReserved1: .byte 1
comInterrupts: .byte 2
comStatsPacketsIn: .byte 2
comStatsPacketsOut: .byte 2
@@ -116,6 +118,11 @@ comIsrPcint0:
push xl
push yh
push yl
lds xl, comInterrupts
lds xh, comInterrupts+1
adiw xh:xl, 1
sts comInterrupts, xl
sts comInterrupts+1, xh
rcall comReceivePacketHandleBuffer ; (R1, R16, R17, R18, R19, R20, R21, R22, X, Y
pop yl
pop yh

View File

@@ -37,6 +37,7 @@
.equ CPRO_CMD_DEVICE = 80
.equ CPRO_CMD_MEMSTATS = 81
.equ CPRO_CMD_SYSSTATS = 82
; flags for variable payload enqueue function

View File

@@ -34,7 +34,7 @@ CPRO_EnqueueMemStats:
push xh
push xl
mov r16, r6
ldi r17, CPRO_PAYLOAD_FLAGS_UID | CPRO_PAYLOAD_FLAGS_SECONDS | (8<<CPRO_PAYLOAD_FLAGS_SHIFT_NUM)
ldi r17, CPRO_PAYLOAD_FLAGS_UID | CPRO_PAYLOAD_FLAGS_SECONDS | (9<<CPRO_PAYLOAD_FLAGS_SHIFT_NUM)
ldi r18, CPRO_CMD_MEMSTATS
rcall cproBeginMsgWithVariablePayload ; (R3, R4, R16, R17, R18, R19, R20, R21, X)
; payload
@@ -54,6 +54,12 @@ CPRO_EnqueueMemStats:
st X+, r17 ; recvNoBuffer
lds r17, comStatsRecvNoBuffer+1
st X+, r17
push xl
push xh
rcall Timer_CalcIdle ; (r16, r17, r18, r24, r25, X)
pop xh
pop xl
st X+, r16
pop xl
pop xh
rcall comCalcAndAddChecksumByte ; (R16, R17, R18, R19, X)

72
avr/comproto_sysstats.asm Normal file
View File

@@ -0,0 +1,72 @@
; ***************************************************************************
; 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
; ---------------------------------------------------------------------------
; Enqueue a MEMSTATS packet.
;
; IN:
; - R16: destination address
; OUT:
; - CFLAG: set if okay, clear otherwise
; MODIFIED REGS: R6, R16, R17, R18, R20 (R3, R4, R15, R19, R21, X)
CPRO_EnqueueSysStats:
mov r6, r16
rcall COM_AllocBufferAndGetXY ; (r16, r17, r21)
brcc CPRO_EnqueueSysStats_error
push xh
push xl
mov r16, r6
ldi r17, CPRO_PAYLOAD_FLAGS_UID | CPRO_PAYLOAD_FLAGS_SECONDS | (9<<CPRO_PAYLOAD_FLAGS_SHIFT_NUM)
ldi r18, CPRO_CMD_SYSSTATS
rcall cproBeginMsgWithVariablePayload ; (R3, R4, R16, R17, R18, R19, R20, R21, X)
; payload
lds r17, comInterrupts
st X+, r17 ; com interrupts
lds r17, comInterrupts+1
st X+, r17
lds r17, timerInterrupts
st X+, r17 ; timer interrupts
lds r17, timerInterrupts+1
st X+, r17
push xl
push xh
rcall Timer_CalcIdle ; (r16, r17, r18, r24, r25, X)
pop xh
pop xl
st X+, r16 ; idle percentage
pop xl
pop xh
rcall comCalcAndAddChecksumByte ; (R16, R17, R18, R19, X)
; mark buffer as enqueued with PRIO "normal" (slightly limited number of retries)
ldi r20, COM_BUFFER_PRIO_NORMAL
rcall COM_EnqueuePacket ; (R15, R16)
brcc CPRO_EnqueueSysStats_error
sec
ret
CPRO_EnqueueSysStats_error:
clc
ret

View File

@@ -70,6 +70,8 @@ main_loop:
; sbi PINA, PORTA2 ; debug (toggle)
; cbi PORTA, PORTA2 ; debug (on)
; sbi PORTA, PORTA2 ; debug (off)
rcall Timer_BeforeSleep
; only modify SE, SM1 and SM0
in r16, MCUCR
@@ -79,6 +81,7 @@ main_loop:
ori r16, (1<<SE) ; sleep mode "idle", enable
out MCUCR, r16
sleep ; sleep, wait for interrupt
rcall Timer_AfterSleep
rjmp main_loop

View File

@@ -16,6 +16,8 @@
.equ TIMER_FLAGS_IF_ADDR = 1
.equ TIMER_STATES_MASK = 7
; ***************************************************************************
@@ -27,7 +29,10 @@ timerModuleData:
timerModuleTickCounter: .byte 1
timerTicksSinceLastRun: .byte 2
timerModuleCounterSecs: .byte 4
timerInterrupts: .byte 2
timerStateBeforeSleep: .byte 1
timerStatePos: .byte 1
timerStates: .byte TIMER_STATES_MASK+1
timerModuleData_end:
@@ -163,6 +168,7 @@ timerInitTimers_loop:
st X+, r20
st X, r21
inc r16
inc r16
rjmp timerInitTimers_loop
timerInitTimers_end:
@@ -242,6 +248,68 @@ timerCallR19R18:
Timer_AfterSleep:
in r16, TCNT0
sts timerStateBeforeSleep, r16
ret
Timer_BeforeSleep:
in r16, TCNT0 ; read counter value
lds r17, timerStateBeforeSleep
sub r16, r17
brcc Timer_BeforeSleep_l1
ldi r17, 98-1
add r16, r17
Timer_BeforeSleep_l1:
ldi xl, LOW(timerStates)
ldi xh, HIGH(timerStates)
lds r18, timerStatePos
clr r17
add xl, r18
adc xh, r17
st X, r16
inc r18
andi r18, TIMER_STATES_MASK
sts timerStatePos, r18
ret
; ---------------------------------------------------------------------------
; Calculate aproximate idle percentage
;
; IN:
; OUT:
; - R16: approximate idle percentage (between 0 and 97)
; REGS: r16, r17, r18, r24, r25, X
Timer_CalcIdle:
ldi xl, LOW(timerStates)
ldi xh, HIGH(timerStates)
ldi r18, TIMER_STATES_MASK+1
clr r24
clr r25
clr r17
Timer_CalcIdle_loop:
ld r16, X+
add r24, r16
adc r25, r17
dec r18
brne Timer_CalcIdle_loop
lsr r25
ror r24
lsr r25
ror r24
lsr r25
ror r24
mov r16, r24
ret
; ---------------------------------------------------------------------------
; OC0A interrupt handler
;
@@ -259,6 +327,13 @@ timerIrqOC0A:
adiw r25:r24, 1
sts timerTicksSinceLastRun, r24
sts timerTicksSinceLastRun+1, r25
lds r24, timerInterrupts
lds r25, timerInterrupts+1
adiw r25:r24, 1
sts timerInterrupts, r24
sts timerInterrupts+1, r25
pop r25
pop r24
out SREG, r15