avr: try calculating idle times.

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

View File

@@ -59,6 +59,7 @@
msg_sendstats.h
msg_recvstats.h
msg_memstats.h
msg_sysstats.h
msg_value.h
msg_value2.h
msg_device.h
@@ -91,6 +92,7 @@
msg_sendstats.c
msg_recvstats.c
msg_memstats.c
msg_sysstats.c
msg_value.c
msg_value2.c
msg_device.c

View File

@@ -19,6 +19,7 @@
#include "aqhome/msg/msg_sendstats.h"
#include "aqhome/msg/msg_recvstats.h"
#include "aqhome/msg/msg_memstats.h"
#include "aqhome/msg/msg_sysstats.h"
#include "aqhome/msg/msg_ping.h"
#include "aqhome/msg/msg_pong.h"
#include "aqhome/msg/msg_needaddr.h"
@@ -136,6 +137,7 @@ void _logMessage(GWEN_MSG_ENDPOINT *ep, const GWEN_MSG *msg)
case AQH_MSG_TYPE_DENY_ADDRESS: AQH_DenyAddrMsg_DumpToBuffer(msg, dbuf, "received"); break;
case AQH_MSG_TYPE_DEVICE: AQH_DeviceMsg_DumpToBuffer(msg, dbuf, "received"); break;
case AQH_MSG_TYPE_MEMSTATS: AQH_MemStatsMsg_DumpToBuffer(msg, dbuf, "received"); break;
case AQH_MSG_TYPE_SYSSTATS: AQH_SysStatsMsg_DumpToBuffer(msg, dbuf, "received"); break;
default: AQH_NodeMsg_DumpToBuffer(msg, dbuf, "received"); break;
}
}

View File

@@ -26,8 +26,9 @@
#define AQH_MSG_OFFS_MEMSTATS_MAXBUFFERSUSED 11 /* 1 byte */
#define AQH_MSG_OFFS_MEMSTATS_SENDNOBUFFER 12 /* 2 bytes */
#define AQH_MSG_OFFS_MEMSTATS_RECVNOBUFFER 14 /* 2 bytes */
#define AQH_MSG_OFFS_MEMSTATS_IDLEPERCENT 16 /* 1 byte */
#define AQH_MSG_MEMSTATS_MINSIZE (AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_MEMSTATS_RECVNOBUFFER+2)
#define AQH_MSG_MEMSTATS_MINSIZE (AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_MEMSTATS_IDLEPERCENT+1)
@@ -53,14 +54,14 @@ uint16_t AQH_MemStatsMsg_GetStackPtr(const GWEN_MSG *msg)
uint8_t AQH_MemStatsMsg_GetBuffersUsed(const GWEN_MSG *msg)
{
return AQH_NodeMsg_GetUint16At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_MEMSTATS_BUFFERSUSED, 0);
return AQH_NodeMsg_GetUint8At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_MEMSTATS_BUFFERSUSED, 0);
}
uint8_t AQH_MemStatsMsg_GetMaxBuffersUsed(const GWEN_MSG *msg)
{
return AQH_NodeMsg_GetUint16At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_MEMSTATS_MAXBUFFERSUSED, 0);
return AQH_NodeMsg_GetUint8At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_MEMSTATS_MAXBUFFERSUSED, 0);
}
@@ -79,15 +80,23 @@ uint16_t AQH_MemStatsMsg_GetRecvNoBufferErrors(const GWEN_MSG *msg)
uint8_t AQH_MemStatsMsg_GetIdlePercentage(const GWEN_MSG *msg)
{
return AQH_NodeMsg_GetUint8At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_MEMSTATS_IDLEPERCENT, 0);
}
void AQH_MemStatsMsg_DumpToBuffer(const GWEN_MSG *msg, GWEN_BUFFER *dbuf, const char *sText)
{
GWEN_Buffer_AppendArgs(dbuf,
"0x%02x->0x%02x: MEMSTATS %s (uid=0x%08x, uptime=%d, stackptr=%d[%04x], buffers used=%d(max=%d), no sendbuf errs=%d, no recvbuf=%d)\n",
"0x%02x->0x%02x: MEMSTATS %s (uid=0x%08x, uptime=%d, idle=%d, stackptr=%d[%04x], buffers used=%d(max=%d), no sendbuf errs=%d, no recvbuf=%d)\n",
AQH_NodeMsg_GetSourceAddress(msg),
AQH_NodeMsg_GetDestAddress(msg),
sText,
(unsigned int) AQH_MemStatsMsg_GetUid(msg),
AQH_MemStatsMsg_GetSeconds(msg),
AQH_MemStatsMsg_GetIdlePercentage(msg),
AQH_MemStatsMsg_GetStackPtr(msg),
AQH_MemStatsMsg_GetStackPtr(msg),
AQH_MemStatsMsg_GetBuffersUsed(msg),

View File

@@ -25,6 +25,7 @@ AQHOME_API uint8_t AQH_MemStatsMsg_GetBuffersUsed(const GWEN_MSG *msg);
AQHOME_API uint8_t AQH_MemStatsMsg_GetMaxBuffersUsed(const GWEN_MSG *msg);
AQHOME_API uint16_t AQH_MemStatsMsg_GetSendNoBufferErrors(const GWEN_MSG *msg);
AQHOME_API uint16_t AQH_MemStatsMsg_GetRecvNoBufferErrors(const GWEN_MSG *msg);
AQHOME_API uint8_t AQH_MemStatsMsg_GetIdlePercentage(const GWEN_MSG *msg);
AQHOME_API void AQH_MemStatsMsg_DumpToBuffer(const GWEN_MSG *msg, GWEN_BUFFER *dbuf, const char *sText);

View File

@@ -205,6 +205,7 @@ uint32_t AQH_NodeMsg_GetMsgGroup(uint8_t msgType)
case AQH_MSG_TYPE_DEBUG:
case AQH_MSG_TYPE_DEVICE:
case AQH_MSG_TYPE_MEMSTATS:
case AQH_MSG_TYPE_SYSSTATS:
return AQH_MSG_TYPEGROUP_INFO;
case AQH_MSG_TYPE_VALUE:
case AQH_MSG_TYPE_VALUE2:

View File

@@ -43,6 +43,7 @@
#define AQH_MSG_TYPE_DEVICE 80
#define AQH_MSG_TYPE_MEMSTATS 81
#define AQH_MSG_TYPE_SYSSTATS 82
/* internal msg types via NET interface */

85
aqhome/msg/msg_sysstats.c Normal file
View File

@@ -0,0 +1,85 @@
/****************************************************************************
* This file is part of the project AqHome.
* AqHome (c) by 2023 Martin Preuss, all rights reserved.
*
* The license for this file can be found in the file COPYING which you
* should have received along with this file.
****************************************************************************/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include "aqhome/msg/msg_sysstats.h"
#include <gwenhywfar/misc.h>
#include <gwenhywfar/list.h>
#include <gwenhywfar/error.h>
#include <gwenhywfar/debug.h>
#define AQH_MSG_OFFS_SYSSTATS_SECONDS 0 /* 4 bytes */
#define AQH_MSG_OFFS_SYSSTATS_UID 4 /* 4 bytes */
#define AQH_MSG_OFFS_SYSSTATS_COMIRQS 8 /* 2 bytes */
#define AQH_MSG_OFFS_SYSSTATS_TIMERIRQS 10 /* 2 bytes */
#define AQH_MSG_OFFS_SYSSTATS_IDLEPERCENT 12 /* 1 byte */
#define AQH_MSG_MEMSTATS_MINSIZE (AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_SYSSTATS_IDLEPERCENT+1)
uint32_t AQH_SysStatsMsg_GetUid(const GWEN_MSG *msg)
{
return AQH_NodeMsg_GetUint32At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_SYSSTATS_UID, 0);
}
uint32_t AQH_SysStatsMsg_GetSeconds(const GWEN_MSG *msg)
{
return AQH_NodeMsg_GetUint32At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_SYSSTATS_SECONDS, 0);
}
uint16_t AQH_SysStatsMsg_GetComInterrupts(const GWEN_MSG *msg)
{
return AQH_NodeMsg_GetUint16At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_SYSSTATS_COMIRQS, 0);
}
uint16_t AQH_SysStatsMsg_GetTimerInterrupts(const GWEN_MSG *msg)
{
return AQH_NodeMsg_GetUint16At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_SYSSTATS_TIMERIRQS, 0);
}
uint8_t AQH_SysStatsMsg_GetIdlePercentage(const GWEN_MSG *msg)
{
return AQH_NodeMsg_GetUint8At(msg, AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_SYSSTATS_IDLEPERCENT, 0);
}
void AQH_SysStatsMsg_DumpToBuffer(const GWEN_MSG *msg, GWEN_BUFFER *dbuf, const char *sText)
{
GWEN_Buffer_AppendArgs(dbuf,
"0x%02x->0x%02x: SYSSTATS %s (uid=0x%08x, uptime=%d, idle=%d, com irqs=%d, timer irqs=%d)\n",
AQH_NodeMsg_GetSourceAddress(msg),
AQH_NodeMsg_GetDestAddress(msg),
sText,
(unsigned int) AQH_SysStatsMsg_GetUid(msg),
AQH_SysStatsMsg_GetSeconds(msg),
AQH_SysStatsMsg_GetIdlePercentage(msg),
AQH_SysStatsMsg_GetComInterrupts(msg),
AQH_SysStatsMsg_GetTimerInterrupts(msg));
}

35
aqhome/msg/msg_sysstats.h Normal file
View File

@@ -0,0 +1,35 @@
/****************************************************************************
* This file is part of the project AqHome.
* AqHome (c) by 2023 Martin Preuss, all rights reserved.
*
* The license for this file can be found in the file COPYING which you
* should have received along with this file.
****************************************************************************/
#ifndef AQH_SYS_MEMSTATS_H
#define AQH_SYS_MEMSTATS_H
#include <aqhome/api.h>
#include <aqhome/msg/msg_node.h>
#include <gwenhywfar/msg.h>
#include <gwenhywfar/buffer.h>
AQHOME_API uint32_t AQH_SysStatsMsg_GetUid(const GWEN_MSG *msg);
AQHOME_API uint32_t AQH_SysStatsMsg_GetSeconds(const GWEN_MSG *msg);
AQHOME_API uint16_t AQH_SysStatsMsg_GetComInterrupts(const GWEN_MSG *msg);
AQHOME_API uint16_t AQH_SysStatsMsg_GetTimerInterrupts(const GWEN_MSG *msg);
AQHOME_API uint8_t AQH_SysStatsMsg_GetIdlePercentage(const GWEN_MSG *msg);
AQHOME_API void AQH_SysStatsMsg_DumpToBuffer(const GWEN_MSG *msg, GWEN_BUFFER *dbuf, const char *sText);
#endif

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

@@ -71,6 +71,8 @@ main_loop:
; cbi PORTA, PORTA2 ; debug (on)
; sbi PORTA, PORTA2 ; debug (off)
rcall Timer_BeforeSleep
; only modify SE, SM1 and SM0
in r16, MCUCR
ldi r17, (1<<SE) | (1<<SM1) | (1<<SM0)
@@ -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