| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- /*
- * Copyright (C) 2001-2004 by egnite Software GmbH. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. Neither the name of the copyright holders nor the names of
- * contributors may be used to endorse or promote products derived
- * from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
- * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
- * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
- * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
- * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- *
- * For additional information see http://www.ethernut.de/
- *
- */
- /*
- * $Log$
- * Revision 1.7 2008/08/22 09:25:34 haraldkipp
- * Clock value caching and new functions NutArchClockGet, NutClockGet and
- * NutClockSet added.
- *
- * Revision 1.6 2008/08/11 06:59:13 haraldkipp
- * BSD types replaced by stdint types (feature request #1282721).
- *
- * Revision 1.5 2008/07/08 08:25:04 haraldkipp
- * NutDelay is no more architecture specific.
- * Number of loops per millisecond is configurable or will be automatically
- * determined.
- * A new function NutMicroDelay provides shorter delays.
- *
- * Revision 1.4 2007/08/17 10:44:37 haraldkipp
- * Timer enable/disable macro replaces previous global interrupt
- * enable/disable or function calling.
- *
- * Revision 1.3 2006/10/08 16:48:07 haraldkipp
- * Documentation fixed
- *
- * Revision 1.2 2005/10/24 08:58:48 haraldkipp
- * Header file moved.
- *
- * Revision 1.1 2005/07/26 18:02:26 haraldkipp
- * Moved from dev.
- *
- * Revision 1.2 2005/07/20 09:17:26 haraldkipp
- * Default NUT_CPU_FREQ and NUT_TICK_FREQ added.
- * NutTimerIntr() removed, because we can use the hardware independent code.
- *
- * Revision 1.1 2005/05/27 17:16:40 drsung
- * Moved the file.
- *
- * Revision 1.5 2005/04/05 17:50:46 haraldkipp
- * Use register names in gba.h.
- *
- * Revision 1.4 2004/11/08 19:16:37 haraldkipp
- * Hacked in Gameboy timer support
- *
- * Revision 1.3 2004/10/03 18:42:21 haraldkipp
- * No GBA support yet, but let the compiler run through
- *
- * Revision 1.2 2004/09/08 10:19:39 haraldkipp
- * Running on AT91 and S3C, thanks to James Tyou
- *
- */
- #include <cfg/arch.h>
- #if defined(MCU_AT91R40008)
- #include <arch/arm/at91.h>
- #elif defined(MCU_S3C4510B)
- #include <arch/arm.h>
- #include <dev/s3c4510b_hw.h>
- #include <dev/s3c4510b_irqs.h>
- #elif defined(MCU_GBA)
- #include <arch/gba.h>
- #include <dev/irqreg.h>
- #endif
- #ifndef NUT_CPU_FREQ
- #define NUT_CPU_FREQ 1000000UL
- #endif
- #ifndef NUT_TICK_FREQ
- #define NUT_TICK_FREQ 1000UL
- #endif
- #if defined(MCU_AT91R40008)
- static int dummy;
- #endif
- static void (*os_handler) (void *);
- /*!
- * \brief Timer 0 interrupt entry.
- */
- #if defined(MCU_AT91R40008)
- void Timer0Entry(void) NUT_NAKED_FUNC;
- void Timer0Entry(void)
- {
- IRQ_ENTRY();
- dummy = inr(TC0_SR);
- os_handler(0);
- IRQ_EXIT();
- }
- #elif defined(MCU_GBA)
- void Timer3Entry(void *arg)
- {
- outw(REG_IF, INT_TMR3);
- os_handler(0);
- }
- #endif
- /*!
- * \brief Initialize system timer.
- *
- * This function is automatically called by Nut/OS
- * during system initialization.
- *
- * Nut/OS uses on-chip timer 0 for its timer services.
- * Applications should not modify any registers of this
- * timer, but make use of the Nut/OS timer API. Timer 1
- * and timer 2 are available to applications.
- */
- void NutRegisterTimer(void (*handler) (void *))
- {
- os_handler = handler;
- #if defined(MCU_AT91R40008)
- /* Disable the Clock Counter */
- outr(TC0_CCR, TC_CLKDIS);
- /* Disable all interrupts */
- outr(TC0_IDR, 0xFFFFFFFF);
- /* Clear the status register. */
- dummy = inr(TC0_SR);
- /* Select divider and compare trigger */
- outr(TC0_CMR, TC_CLKS_MCK32 | TC_CPCTRG);
- /* Enable the Clock counter */
- outr(TC0_CCR, TC_CLKEN);
- /* Validate the RC compare interrupt */
- outr(TC0_IER, TC_CPCS);
- /* Disable timer 0 interrupts. */
- outr(AIC_IDCR, _BV(TC0_ID));
- /* Set the TC0 IRQ handler address */
- outr(AIC_SVR(4), (unsigned int)Timer0Entry);
- /* Set the trigg and priority for timer 0 interrupt */
- /* Level 7 is highest, level 0 lowest. */
- outr(AIC_SMR(4), (AIC_SRCTYPE_INT_LEVEL_SENSITIVE | 0x4));
- /* Clear timer 0 interrupt */
- outr(AIC_ICCR, _BV(TC0_ID));
- /* Enable timer 0 interrupts */
- outr(AIC_IECR, _BV(TC0_ID));
- /* Set compare value for 1 ms. */
- outr(TC0_RC, 0x80F);
- /* Software trigger starts the clock. */
- outr(TC0_CCR, TC_SWTRG);
- #elif defined(MCU_S3C4510B)
- INT_DISABLE(IRQ_TIMER);
- CSR_WRITE(TCNT0, 0);
- CSR_WRITE(TDATA0, CLOCK_TICK_RATE);
- CSR_WRITE(TMOD, TMOD_TIMER0_VAL);
- CLEAR_PEND_INT(IRQ_TIMER);
- NutRegisterIrqHandler(
- &InterruptHandlers[IRQ_TIMER], handler, 0);
- INT_ENABLE(IRQ_TIMER);
- #elif defined(MCU_GBA)
- /* Disable master interrupt. */
- outw(REG_IME, 0);
- /* Set global interrupt vector. */
- NutRegisterIrqHandler(&sig_TMR3, Timer3Entry, 0);
- /* Enable timer and timer interrupts. */
- outdw(REG_TMR3CNT, TMR_IRQ_ENA | TMR_ENA | 48756);
- /* Enable timer 3 interrupts. */
- outw(REG_IE, inw(REG_IE) | INT_TMR3);
- /* Enable master interrupt. */
- outw(REG_IME, 1);
- #else
- #warning "MCU not defined"
- #endif
- }
- /*!
- * \brief Return the CPU clock in Hertz.
- *
- * \return CPU clock frequency in Hertz.
- */
- uint32_t NutArchClockGet(int idx)
- {
- return NUT_CPU_FREQ;
- }
- /*!
- * \brief Return the number of system ticks per second.
- *
- * \return System tick frequency in Hertz.
- */
- uint32_t NutGetTickClock(void)
- {
- return NUT_TICK_FREQ;
- }
- /*!
- * \brief Calculate system ticks for a given number of milliseconds.
- */
- uint32_t NutTimerMillisToTicks(uint32_t ms)
- {
- #if (NUT_TICK_FREQ % 1000)
- if (ms >= 0x3E8000UL)
- return (ms / 1000UL) * NUT_TICK_FREQ;
- return (ms * NUT_TICK_FREQ + 999UL) / 1000UL;
- #else
- return ms * (NUT_TICK_FREQ / 1000UL);
- #endif
- }
|