]> sigrok.org Git - sigrok-firmware-fx2lafw.git/blame - fx2lib/lib/delay.c
Import fx2lib into fx2lafw directly.
[sigrok-firmware-fx2lafw.git] / fx2lib / lib / delay.c
CommitLineData
3608c106
UH
1/**
2 * Copyright (C) 2009 Ubixum, Inc.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 **/
18
19#include <fx2regs.h>
20#include <fx2macros.h>
21#include <delay.h>
22
23void delay(WORD millis) {
24 /**
25 * It takes 12 crystal pulses to make 1 machine cycle (8051.com)
26 * ez-usb trm 1.13
27 * 83.3 ns at 48mhz per instruction cycle
28 * (assume 166.6ns at 24mhz)
29 * (assume 333.3ns at 12mhz)
30 * ez-usb trm 12.1
31 * Includes the cycles for each instruction
32 **/
33 WORD loop_count;
34 volatile WORD count; // NOTE perhaps use different solutions w/ out volatile
35
36
37 // set count to the number of times we need to
38 // go around a loop for 1 millisecond
39
40 // then do that loop millis times. (1000 us=1ms)
41
42 // 48mhz: 1000 us / (17 cycles * 83.3 ns / cycle / 1000 ns/us) = 706
43 // 24mhz: 353
44 // 12mhz: 177
45 // recalculate if the number of cycles changes
46 // like if you change the loop below
47 loop_count = CPUFREQ == CLK_12M ? 177 :
48 CPUFREQ == CLK_24M ? 353 : 706;
49
50 // sdcc generated assembly
51 /* cycles code
52 ; delay.c:31: do {
53 00101$:
54 ; delay.c:32: } while ( --count );
55 2 dec _delay_count_1_1
56 2 mov a,#0xff
57 4 cjne a,_delay_count_1_1,00121$
58 2 dec (_delay_count_1_1 + 1)
59 00121$:
60 2 mov a,_delay_count_1_1
61 2 orl a,(_delay_count_1_1 + 1)
62 3 jnz 00101$
63
64 Total 17
65 */
66
67 do {
68 count = loop_count;
69 do {
70 } while ( --count );
71 } while ( --millis );
72
73}