From 0abe83e3e54ec68a59f6a16730e41ad385f88ace Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20van=20Dorst?= Date: Fri, 26 Dec 2025 09:12:25 +0100 Subject: [PATCH 1/2] sfr: add 16 bit SFR for the timer0. So we don't have to manual split 16-bit value into for bytes. Let the compiler do the work. --- rtl837x_sfr.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/rtl837x_sfr.h b/rtl837x_sfr.h index 9a99ac7..d8a7024 100644 --- a/rtl837x_sfr.h +++ b/rtl837x_sfr.h @@ -97,3 +97,7 @@ __sfr __at(0xb4) SFR_NIC_DATA_H; __sfr16 __at(0xb6b5) SFR_NIC_RING_U16LE; __sfr __at(0xb5) SFR_NIC_RING_L; __sfr __at(0xb6) SFR_NIC_RING_H; + +/* Standard 8051 sfr */ +// Timer 0 value +__sfr16 __at(0x8c8a) T0_U16; From 9ff114d2343ec3dcac4fab5bad7f5e4b0e0e91ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20van=20Dorst?= Date: Fri, 26 Dec 2025 09:15:09 +0100 Subject: [PATCH 2/2] SYSTICK: Fix writing value to TMR0 registers. A `%` modulo operator was used instead of a `&` and-operator, to split a 16 bit value into two 8 bit values. This causes that the SYSTICKs were 0.1% too fast. Replace the manual split with a sfr16 type, so the compiler does the split for us. --- rtlplayground.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/rtlplayground.c b/rtlplayground.c index 8872f0b..aaa2f43 100644 --- a/rtlplayground.c +++ b/rtlplayground.c @@ -119,8 +119,7 @@ __xdata uint8_t sfp_options[2]; void isr_timer0(void) __interrupt(1) { TR0 = 0; // Stop timer 0 - TH0 = SYSTICK_TIMER0_VALUE >> 8; - TL0 = SYSTICK_TIMER0_VALUE % 0xff; + T0_U16 = SYSTICK_TIMER0_VALUE; TR0 = 1; // Re-start timer 0 ticks++; @@ -295,8 +294,7 @@ void setup_timer0(void) /* The TH0 registers contain the high/low byte that we load into Timer0 when T0 * overflows to 0x10000 */ - TH0 = SYSTICK_TIMER0_VALUE >> 8; - TL0 = SYSTICK_TIMER0_VALUE % 0xff; + T0_U16 = SYSTICK_TIMER0_VALUE; CKCON &= 0xc7; TCON = 0x10; // Start timer 0