621363afcb81fc73e2c84798a3fd184ca1b09650
[fx2fw-sdcc] / fx2 / delay.c
1 /* -*- c++ -*- */\r
2 \r
3 /*-----------------------------------------------------------------------------\r
4 \r
5  * Delay routines\r
6 \r
7  *-----------------------------------------------------------------------------\r
8 \r
9  * Code taken from USRP2 firmware (GNU Radio Project), version 3.0.2,\r
10 \r
11  * Copyright 2003 Free Software Foundation, Inc.\r
12 \r
13  *-----------------------------------------------------------------------------\r
14 \r
15  * This code is part of usbjtag. usbjtag is free software; you can redistribute\r
16 \r
17  * it and/or modify it under the terms of the GNU General Public License as\r
18 \r
19  * published by the Free Software Foundation; either version 2 of the License,\r
20 \r
21  * or (at your option) any later version. usbjtag is distributed in the hope\r
22 \r
23  * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied\r
24 \r
25  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
26 \r
27  * GNU General Public License for more details.  You should have received a\r
28 \r
29  * copy of the GNU General Public License along with this program in the file\r
30 \r
31  * COPYING; if not, write to the Free Software Foundation, Inc., 51 Franklin\r
32 \r
33  * St, Fifth Floor, Boston, MA  02110-1301  USA\r
34 \r
35  *-----------------------------------------------------------------------------\r
36 \r
37  */\r
38 \r
39 \r
40 \r
41 /*\r
42 \r
43  * Delay approximately 1 microsecond (including overhead in udelay).\r
44 \r
45  */\r
46 \r
47 static void\r
48 \r
49 udelay1 (void) _naked\r
50 \r
51 {\r
52 \r
53   _asm                          ; lcall that got us here took 4 bus cycles\r
54 \r
55         ret                     ; 4 bus cycles\r
56 \r
57   _endasm;\r
58 \r
59 }\r
60 \r
61 \r
62 \r
63 /*\r
64 \r
65  * delay for approximately usecs microseconds\r
66 \r
67  */\r
68 \r
69 void\r
70 \r
71 udelay (unsigned char usecs)\r
72 \r
73 {\r
74 \r
75   do {\r
76 \r
77     udelay1 ();\r
78 \r
79   } while (--usecs != 0);\r
80 \r
81 }\r
82 \r
83 \r
84 \r
85 \r
86 \r
87 /*\r
88 \r
89  * Delay approximately 1 millisecond.\r
90 \r
91  * We're running at 48 MHz, so we need 48,000 clock cycles.\r
92 \r
93  *\r
94 \r
95  * Note however, that each bus cycle takes 4 clock cycles (not obvious,\r
96 \r
97  * but explains the factor of 4 problem below).\r
98 \r
99  */\r
100 \r
101 static void\r
102 \r
103 mdelay1 (void) _naked\r
104 \r
105 {\r
106 \r
107   _asm\r
108 \r
109         mov     dptr,#(-1200 & 0xffff)\r
110 \r
111 002$:   \r
112 \r
113         inc     dptr            ; 3 bus cycles\r
114 \r
115         mov     a, dpl          ; 2 bus cycles\r
116 \r
117         orl     a, dph          ; 2 bus cycles\r
118 \r
119         jnz     002$            ; 3 bus cycles\r
120 \r
121 \r
122 \r
123         ret\r
124 \r
125   _endasm;\r
126 \r
127 }\r
128 \r
129 \r
130 \r
131 void\r
132 \r
133 mdelay (unsigned int msecs)\r
134 \r
135 {\r
136 \r
137   do {\r
138 \r
139     mdelay1 ();\r
140 \r
141   } while (--msecs != 0);\r
142 \r
143 }\r
144 \r
145 \r
146 \r
147         \r
148 \r