When I finally put the two boards described in Flashing the CH552 dev board from the command line to use I noticed that button actions were faster. Counter advance worked twice as fast. The no-activity timeout for exiting set mode was 32 seconds instead of 64. At boot-up the MCU runs at 24 MHz, but this can be set to 12 MHz for compatibility with the classic 8051. The tick timer which governs the speed of button actions depends on the clock speed.
I thought I had taken care of this in the initialisation code, but to cut to the chase, here's the diff showing what I did wrong:
$ git diff 227da07..67b402a
diff --git a/227da07 b/67b402a
index 227da07..67b402a 100644
--- a/227da07
+++ b/67b402a
@@ -200,8 +200,7 @@ void main(void)
// change the clock divisor to generate 12 MHz
SAFE_MOD = 0x55;
SAFE_MOD = 0xAA;
- CLOCK_CFG &= ~CLOCK_DIVISOR;
- CLOCK_CFG |= CLOCK_DIVISOR;
+ CLOCK_CFG = (bOSC_EN_INT | CLOCK_DIVISOR);
SAFE_MOD = 0x00; // any value will do
#ifdef DEBUG
#else
The clock configuration register is protected against accidental modification by a protocol. You have to write the values 0x55 and 0xAA to the SAFE_MOD register, make your change, then exit by writing anything to SAFE_MOD. The intention of the original code was to read the bits in the CLOCK_CFG register, except for the CLOCK_DIVISOR (bottom 3) bits, then write it back with those bits modified. You can see that the first line ought to be:
CLOCK_CFG &= (0xF8 | ~CLOCK_DIVISOR);
to preserve the top 5 bits.
But I also fell foul of another restriction and that is modification mode only lasts for a limited number of clock cycles. More instructions and the changes don't take. That was probably what was happening, it timed out before CLOCK_CFG was changed. So I just went for a simple one line replacement and that fixed the double speed problem.
BTW, I prefer to run the MCU at a lower clock rate rather than recalibrate the timer for a faster clock. It's more reliable at lower clock rates, and the MCU is idle most of the time anyway.
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.