Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2008 Travis Geiselbrecht |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining |
| 5 | * a copy of this software and associated documentation files |
| 6 | * (the "Software"), to deal in the Software without restriction, |
| 7 | * including without limitation the rights to use, copy, modify, merge, |
| 8 | * publish, distribute, sublicense, and/or sell copies of the Software, |
| 9 | * and to permit persons to whom the Software is furnished to do so, |
| 10 | * subject to the following conditions: |
| 11 | * |
| 12 | * The above copyright notice and this permission notice shall be |
| 13 | * included in all copies or substantial portions of the Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 18 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 19 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 20 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 21 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 22 | */ |
| 23 | #include <sys/types.h> |
| 24 | #include <err.h> |
| 25 | #include <kernel/thread.h> |
| 26 | #include <debug.h> |
| 27 | #include <platform.h> |
| 28 | #include <platform/interrupts.h> |
| 29 | #include <platform/timer.h> |
| 30 | #include <platform/integrator.h> |
| 31 | #include "platform_p.h" |
| 32 | |
| 33 | static time_t system_time = 0; |
| 34 | |
| 35 | static time_t tick_interval; |
| 36 | static uint32_t ticks_per_interval; |
| 37 | static platform_timer_callback t_callback; |
| 38 | static void *callback_arg; |
| 39 | |
| 40 | status_t platform_set_periodic_timer(platform_timer_callback callback, void *arg, time_t interval) |
| 41 | { |
| 42 | #if 0 |
| 43 | enter_critical_section(); |
| 44 | |
| 45 | t_callback = callback; |
| 46 | callback_arg = arg; |
| 47 | tick_interval = interval; |
| 48 | ticks_per_interval = interval * 32768 / 1000; // interval is in ms |
| 49 | |
| 50 | OS_TIMER_CTRL_REG = 0; // stop it |
| 51 | OS_TIMER_TICK_VALUE_REG = ticks_per_interval; |
| 52 | OS_TIMER_CTRL_REG = (1<<3) | (1<<2) | (1<<1) | (1<<0); |
| 53 | |
| 54 | exit_critical_section(); |
| 55 | #endif |
| 56 | |
| 57 | return NO_ERROR; |
| 58 | } |
| 59 | |
| 60 | time_t current_time(void) |
| 61 | { |
| 62 | #if 0 |
| 63 | time_t t; |
| 64 | uint32_t delta_ticks; |
| 65 | uint32_t delta_ticks2; |
| 66 | |
| 67 | retry: |
| 68 | delta_ticks = OS_TIMER_TICK_COUNTER_REG; |
| 69 | t = system_time; |
| 70 | delta_ticks2 = OS_TIMER_TICK_COUNTER_REG; |
| 71 | if (delta_ticks2 > delta_ticks) |
| 72 | goto retry; |
| 73 | |
| 74 | t += ((ticks_per_interval - delta_ticks2) * tick_interval) / ticks_per_interval; |
| 75 | |
| 76 | return t; |
| 77 | #else |
| 78 | static time_t time = 0; |
| 79 | return time++; |
| 80 | #endif |
| 81 | |
| 82 | } |
| 83 | |
| 84 | static enum handler_return os_timer_tick(void *arg) |
| 85 | { |
| 86 | system_time += tick_interval; |
| 87 | // dprintf("os_timer_tick %d\n", system_time); |
| 88 | |
| 89 | return t_callback(callback_arg, system_time); |
| 90 | } |
| 91 | |
| 92 | void platform_init_timer(void) |
| 93 | { |
| 94 | #if 0 |
| 95 | OS_TIMER_CTRL_REG = 0; // stop the timer if it's already running |
| 96 | |
| 97 | register_int_handler(IRQ_OS_TIMER, &os_timer_tick, NULL); |
| 98 | unmask_interrupt(IRQ_OS_TIMER, NULL); |
| 99 | #endif |
| 100 | } |
| 101 | |