]> sigrok.org Git - sigrok-firmware-fx2lafw.git/blame - fx2lib/lib/delay.c
license: remove FSF postal address from boiler plate license text
[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
040a6eae 15 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
3608c106
UH
16 **/
17
18#include <fx2regs.h>
19#include <fx2macros.h>
20#include <delay.h>
21
22void delay(WORD millis) {
23 /**
24 * It takes 12 crystal pulses to make 1 machine cycle (8051.com)
25 * ez-usb trm 1.13
26 * 83.3 ns at 48mhz per instruction cycle
27 * (assume 166.6ns at 24mhz)
28 * (assume 333.3ns at 12mhz)
29 * ez-usb trm 12.1
30 * Includes the cycles for each instruction
31 **/
32 WORD loop_count;
33 volatile WORD count; // NOTE perhaps use different solutions w/ out volatile
34
35
36 // set count to the number of times we need to
37 // go around a loop for 1 millisecond
38
39 // then do that loop millis times. (1000 us=1ms)
40
41 // 48mhz: 1000 us / (17 cycles * 83.3 ns / cycle / 1000 ns/us) = 706
42 // 24mhz: 353
43 // 12mhz: 177
44 // recalculate if the number of cycles changes
45 // like if you change the loop below
46 loop_count = CPUFREQ == CLK_12M ? 177 :
47 CPUFREQ == CLK_24M ? 353 : 706;
48
49 // sdcc generated assembly
50 /* cycles code
51 ; delay.c:31: do {
52 00101$:
53 ; delay.c:32: } while ( --count );
54 2 dec _delay_count_1_1
55 2 mov a,#0xff
56 4 cjne a,_delay_count_1_1,00121$
57 2 dec (_delay_count_1_1 + 1)
58 00121$:
59 2 mov a,_delay_count_1_1
60 2 orl a,(_delay_count_1_1 + 1)
61 3 jnz 00101$
62
63 Total 17
64 */
65
66 do {
67 count = loop_count;
68 do {
69 } while ( --count );
70 } while ( --millis );
71
72}