version 4.2.0
[fx2fw-sdcc] / fx2 / timer.c
1 /* -*- c++ -*- */\r
2 /*-----------------------------------------------------------------------------\r
3  * Timer handling for FX2\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 #include "timer.h"\r
22 #include "fx2regs.h"\r
23 #include "isr.h"\r
24 \r
25 /*\r
26  * Arrange to have isr_tick_handler called at 100 Hz.\r
27  *\r
28  * The cpu clock is running at 48e6.  The input to the timer\r
29  * is 48e6 / 12 = 4e6.\r
30  *\r
31  * We arrange to have the timer overflow every 40000 clocks == 100 Hz\r
32  */\r
33 \r
34 #define RELOAD_VALUE    ((unsigned short) -40000)\r
35 \r
36 void \r
37 hook_timer_tick (unsigned short isr_tick_handler)\r
38 {\r
39   ET2 = 0;                      // disable timer 2 interrupts\r
40   hook_sv (SV_TIMER_2, isr_tick_handler);\r
41   \r
42   RCAP2H = RELOAD_VALUE >> 8;   // setup the auto reload value\r
43   RCAP2L = RELOAD_VALUE & 0xFF;\r
44 \r
45   T2CON = 0x04;                 // interrupt on overflow; reload; run\r
46   ET2 = 1;                      // enable timer 2 interrupts\r
47 }\r