-
Notifications
You must be signed in to change notification settings - Fork 42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
micros() is non-monotonic #65
Comments
I tracked down the CH32 source code for micros(). It just calls getCurrentMicros, which is found in clock.c. The code appears to have been written assuming that SysTick counts down. However, in hw_config.c, the SysTick CTRL register is set to 0xF, which configures SysTick to count up. I modified getCurrentMicros, replacing two lines of code as follows:
That appears to work correctly, at least on the CH32V307 chip. The SysTick timer on the CH32V10x processors is different, and it is configured differently. Unfortunately, I do not have a CH32V103 chip to test with, and the RISC-V SysTick registers are not documented in the CH32V10x Reference Manual, so exercise caution if you decide to apply this change. |
I purchased a CH32V103C8T6 board, and I am experiencing the same problem with non-monotonic micros() output. The CH32V103 Systick device has 32-bit high and low count registers, so it is not code compatible with the other CH32V chips. There is separate code for the CH32V103 chips further down in clock.c. I made a similar change to that code and it seems to correct the problem.
In my previous post, I noted that the Systick device was not documented in the WCH reference manuals. The Systick device is part of the RISC-V core, so it is documented in the (V2, V3, etc.) QingKe RISC-V microprocessor manuals instead. |
I was benchmarking some floating point operations on a CH32V307 chip and I was getting crazy results. The following sketch illustrates the problem:
The output starts out with:
And about half way down, this happens:
The counts should be strictly increasing. I haven't looked at the CH32 core, but in the Arduino core, micros() is essentially calculated from millis(). There seems to be an arithmetic error in the CH32 version. I tested this sketch on versions 1.0.3 and 1.0.4 and I obtained the same results.
The text was updated successfully, but these errors were encountered: