version 4.2.0
[fx2fw-sdcc] / fx2 / delay.c
1 /* -*- c++ -*- */\r
2 /*-----------------------------------------------------------------------------\r
3  * Delay routines\r
4  *-----------------------------------------------------------------------------\r
5  * Code taken from USRP2 firmware (GNU Radio Project), version 3.0.2,\r
6  * Copyright 2003 Free Software Foundation, Inc.\r
7  *-----------------------------------------------------------------------------\r
8  * This code is part of usbjtag. usbjtag is free software; you can redistribute\r
9  * it and/or modify it under the terms of the GNU General Public License as\r
10  * published by the Free Software Foundation; either version 2 of the License,\r
11  * or (at your option) any later version. usbjtag is distributed in the hope\r
12  * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied\r
13  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14  * GNU General Public License for more details.  You should have received a\r
15  * copy of the GNU General Public License along with this program in the file\r
16  * COPYING; if not, write to the Free Software Foundation, Inc., 51 Franklin\r
17  * St, Fifth Floor, Boston, MA  02110-1301  USA\r
18  *-----------------------------------------------------------------------------\r
19  */\r
20 \r
21 /*\r
22  * Delay approximately 1 microsecond (including overhead in udelay).\r
23  */\r
24 static void\r
25 udelay1 (void) _naked\r
26 {\r
27   _asm                          ; lcall that got us here took 4 bus cycles\r
28         ret                     ; 4 bus cycles\r
29   _endasm;\r
30 }\r
31 \r
32 /*\r
33  * delay for approximately usecs microseconds\r
34  */\r
35 void\r
36 udelay (unsigned char usecs)\r
37 {\r
38   do {\r
39     udelay1 ();\r
40   } while (--usecs != 0);\r
41 }\r
42 \r
43 \r
44 /*\r
45  * Delay approximately 1 millisecond.\r
46  * We're running at 48 MHz, so we need 48,000 clock cycles.\r
47  *\r
48  * Note however, that each bus cycle takes 4 clock cycles (not obvious,\r
49  * but explains the factor of 4 problem below).\r
50  */\r
51 static void\r
52 mdelay1 (void) _naked\r
53 {\r
54   _asm\r
55         mov     dptr,#(-1200 & 0xffff)\r
56 002$:   \r
57         inc     dptr            ; 3 bus cycles\r
58         mov     a, dpl          ; 2 bus cycles\r
59         orl     a, dph          ; 2 bus cycles\r
60         jnz     002$            ; 3 bus cycles\r
61 \r
62         ret\r
63   _endasm;\r
64 }\r
65 \r
66 void\r
67 mdelay (unsigned int msecs)\r
68 {\r
69   do {\r
70     mdelay1 ();\r
71   } while (--msecs != 0);\r
72 }\r
73 \r
74         \r