Karl Kurbjun | 7b97fe2 | 2007-09-20 04:46:41 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
Karl Kurbjun | bebbac8 | 2007-09-22 15:43:38 +0000 | [diff] [blame] | 8 | * $Id$ |
Karl Kurbjun | 7b97fe2 | 2007-09-20 04:46:41 +0000 | [diff] [blame] | 9 | * |
| 10 | * Copyright (C) 2007 by Karl Kurbjun |
| 11 | * |
Daniel Stenberg | 2acc0ac | 2008-06-28 18:10:04 +0000 | [diff] [blame^] | 12 | * This program is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU General Public License |
| 14 | * as published by the Free Software Foundation; either version 2 |
| 15 | * of the License, or (at your option) any later version. |
Karl Kurbjun | 7b97fe2 | 2007-09-20 04:46:41 +0000 | [diff] [blame] | 16 | * |
| 17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 18 | * KIND, either express or implied. |
| 19 | * |
| 20 | ****************************************************************************/ |
| 21 | |
| 22 | #include "config.h" |
| 23 | #include "cpu.h" |
| 24 | #include "system.h" |
| 25 | #include "timer.h" |
| 26 | #include "logf.h" |
| 27 | |
| 28 | /* GPB0/TOUT0 should already have been configured as output so that pin |
Maurus Cuelenaere | 95167e0 | 2008-04-24 20:08:28 +0000 | [diff] [blame] | 29 | should not be a functional pin and TIMER0 output unseen there */ |
Karl Kurbjun | 7b97fe2 | 2007-09-20 04:46:41 +0000 | [diff] [blame] | 30 | void TIMER0(void) |
| 31 | { |
Maurus Cuelenaere | 910fd78 | 2008-05-07 13:33:29 +0000 | [diff] [blame] | 32 | IO_INTC_IRQ0 = INTR_IRQ0_TMR0; /* clear TIMER0 interrupt */ |
Karl Kurbjun | 7b97fe2 | 2007-09-20 04:46:41 +0000 | [diff] [blame] | 33 | if (pfn_timer != NULL) |
| 34 | pfn_timer(); |
| 35 | } |
| 36 | |
Karl Kurbjun | 5db6b51 | 2007-11-07 05:30:31 +0000 | [diff] [blame] | 37 | bool __timer_set(long cycles, bool start) |
| 38 | { |
| 39 | int oldlevel; |
| 40 | unsigned int divider=cycles, prescaler=0; |
| 41 | |
| 42 | if(cycles<1) |
| 43 | return false; |
| 44 | |
Maurus Cuelenaere | 95167e0 | 2008-04-24 20:08:28 +0000 | [diff] [blame] | 45 | oldlevel = set_irq_level(HIGHEST_IRQ_LEVEL); |
| 46 | |
| 47 | IO_CLK_MOD2 |= CLK_MOD2_TMR0; //enable TIMER0 clock!!!!!!!!! |
| 48 | |
Karl Kurbjun | 5db6b51 | 2007-11-07 05:30:31 +0000 | [diff] [blame] | 49 | IO_TIMER0_TMMD = CONFIG_TIMER0_TMMD_STOP; |
| 50 | |
| 51 | if (start && pfn_unregister != NULL) |
| 52 | { |
| 53 | pfn_unregister(); |
| 54 | pfn_unregister = NULL; |
| 55 | } |
| 56 | |
Michael Sevakis | af395f4 | 2008-03-26 01:50:41 +0000 | [diff] [blame] | 57 | oldlevel = disable_irq_save(); |
Karl Kurbjun | 5db6b51 | 2007-11-07 05:30:31 +0000 | [diff] [blame] | 58 | |
| 59 | /* Increase prescale values starting from 0 to make the cycle count fit */ |
| 60 | while(divider>65535 && prescaler<1024) |
| 61 | { |
| 62 | prescaler++; |
| 63 | divider=cycles/(prescaler+1); |
| 64 | } |
| 65 | |
| 66 | IO_TIMER0_TMPRSCL = prescaler; |
| 67 | IO_TIMER0_TMDIV = divider; |
| 68 | |
Michael Sevakis | af395f4 | 2008-03-26 01:50:41 +0000 | [diff] [blame] | 69 | restore_irq(oldlevel); |
Karl Kurbjun | 5db6b51 | 2007-11-07 05:30:31 +0000 | [diff] [blame] | 70 | |
| 71 | return true; |
| 72 | } |
| 73 | |
Karl Kurbjun | 7b97fe2 | 2007-09-20 04:46:41 +0000 | [diff] [blame] | 74 | static void stop_timer(void) |
| 75 | { |
Maurus Cuelenaere | 95167e0 | 2008-04-24 20:08:28 +0000 | [diff] [blame] | 76 | IO_INTC_EINT0 &= ~INTR_EINT0_TMR0; //disable TIMER0 interrupt |
Karl Kurbjun | 67ef450 | 2007-09-22 23:17:52 +0000 | [diff] [blame] | 77 | |
Maurus Cuelenaere | 95167e0 | 2008-04-24 20:08:28 +0000 | [diff] [blame] | 78 | IO_INTC_IRQ0 = INTR_IRQ0_TMR0; //clear TIMER0 interrupt |
Karl Kurbjun | 67ef450 | 2007-09-22 23:17:52 +0000 | [diff] [blame] | 79 | |
| 80 | IO_TIMER0_TMMD = CONFIG_TIMER0_TMMD_STOP; |
Maurus Cuelenaere | 95167e0 | 2008-04-24 20:08:28 +0000 | [diff] [blame] | 81 | |
| 82 | IO_CLK_MOD2 &= ~CLK_MOD2_TMR0; //disable TIMER0 clock |
Karl Kurbjun | 7b97fe2 | 2007-09-20 04:46:41 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Karl Kurbjun | 7b97fe2 | 2007-09-20 04:46:41 +0000 | [diff] [blame] | 85 | bool __timer_register(void) |
| 86 | { |
Michael Sevakis | 75f2d44 | 2008-03-31 06:00:23 +0000 | [diff] [blame] | 87 | int oldstatus = disable_interrupt_save(IRQ_FIQ_STATUS); |
Maurus Cuelenaere | 95167e0 | 2008-04-24 20:08:28 +0000 | [diff] [blame] | 88 | |
Karl Kurbjun | 7b97fe2 | 2007-09-20 04:46:41 +0000 | [diff] [blame] | 89 | stop_timer(); |
Maurus Cuelenaere | 95167e0 | 2008-04-24 20:08:28 +0000 | [diff] [blame] | 90 | |
| 91 | IO_CLK_MOD2 |= CLK_MOD2_TMR0; //enable TIMER0 clock!!!!!!!!! |
Karl Kurbjun | 7b97fe2 | 2007-09-20 04:46:41 +0000 | [diff] [blame] | 92 | |
| 93 | /* Turn Timer0 to Free Run mode */ |
Karl Kurbjun | 5db6b51 | 2007-11-07 05:30:31 +0000 | [diff] [blame] | 94 | IO_TIMER0_TMMD = CONFIG_TIMER0_TMMD_FREE_RUN; |
Karl Kurbjun | 67ef450 | 2007-09-22 23:17:52 +0000 | [diff] [blame] | 95 | |
Maurus Cuelenaere | 95167e0 | 2008-04-24 20:08:28 +0000 | [diff] [blame] | 96 | IO_INTC_EINT0 |= INTR_EINT0_TMR0; //enable TIMER0 interrupt |
Karl Kurbjun | 7b97fe2 | 2007-09-20 04:46:41 +0000 | [diff] [blame] | 97 | |
Michael Sevakis | 75f2d44 | 2008-03-31 06:00:23 +0000 | [diff] [blame] | 98 | restore_interrupt(oldstatus); |
Karl Kurbjun | 7b97fe2 | 2007-09-20 04:46:41 +0000 | [diff] [blame] | 99 | |
Karl Kurbjun | 5db6b51 | 2007-11-07 05:30:31 +0000 | [diff] [blame] | 100 | return true; |
Karl Kurbjun | 7b97fe2 | 2007-09-20 04:46:41 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | void __timer_unregister(void) |
| 104 | { |
Michael Sevakis | 75f2d44 | 2008-03-31 06:00:23 +0000 | [diff] [blame] | 105 | int oldstatus = disable_interrupt_save(IRQ_FIQ_STATUS); |
Karl Kurbjun | 7b97fe2 | 2007-09-20 04:46:41 +0000 | [diff] [blame] | 106 | stop_timer(); |
Michael Sevakis | 75f2d44 | 2008-03-31 06:00:23 +0000 | [diff] [blame] | 107 | restore_interrupt(oldstatus); |
Karl Kurbjun | 7b97fe2 | 2007-09-20 04:46:41 +0000 | [diff] [blame] | 108 | } |