AVR-Programmer icon indicating copy to clipboard operation
AVR-Programmer copied to clipboard

CH552, sdcc and delay.c

Open dfxhub opened this issue 2 years ago • 3 comments

Hi! Delays are not correct.

sdcc 4.2.0 compiles your implemetation to this:

;	src/delay.c:25: SAFE_MOD++;                     // 2 Fsys cycles, for higher Fsys, add operation here
	mov	a,_SAFE_MOD
	inc	a
	mov	_SAFE_MOD,a

and it is not 2 Fsys cycles, so f.e. DLY_ms(1000) at 24MHz becomes ~2+ secs. While this is more correct (about 1.200 secs at 24MHz) (as of Blinkinlabs or official SDK):

;	src/delay.c:25: ++SAFE_MOD;                     // 2 Fsys cycles, for higher Fsys, add operation here
	inc	_SAFE_MOD

Thanks.

dfxhub avatar Nov 18 '23 16:11 dfxhub

Yes, I know, this delay routine is very inaccurate. It also varies because with the 8051 the number of clock cycles for jumps can only be predicted exactly under certain conditions. Depending on the version, the compiler then does the rest with its questionable results. For precise delays you have to use a timer or the touchkey interrupt flag. In this case, however, the delay routine should not and does not have to be precise.

wagiminator avatar Nov 18 '23 18:11 wagiminator

I agree in that for applications with strong timings requirements delays based on timers or any other hardware are preferred. But why not to use right syntax for particular compiler that leads to more reasonable results? Of cause, IMO, more right way for software delays with predictable results is using assembler and not to trust to compiler, or at least to check for it.

Thanks again for your helpful projects.

dfxhub avatar Nov 18 '23 20:11 dfxhub

I've made a few changes so that the delays are hopefully more precise now.

wagiminator avatar Nov 19 '23 08:11 wagiminator