Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:23:05

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /**
0004  * @file
0005  *
0006  * @ingroup lpc_clock
0007  *
0008  * @brief Clock driver configuration.
0009  */
0010 
0011 /*
0012  * Copyright (C) 2009, 2015 embedded brains GmbH & Co. KG
0013  *
0014  * Redistribution and use in source and binary forms, with or without
0015  * modification, are permitted provided that the following conditions
0016  * are met:
0017  * 1. Redistributions of source code must retain the above copyright
0018  *    notice, this list of conditions and the following disclaimer.
0019  * 2. Redistributions in binary form must reproduce the above copyright
0020  *    notice, this list of conditions and the following disclaimer in the
0021  *    documentation and/or other materials provided with the distribution.
0022  *
0023  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0024  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0025  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0026  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0027  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0028  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0029  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0030  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0031  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0032  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0033  * POSSIBILITY OF SUCH DAMAGE.
0034  */
0035 
0036 #include <rtems.h>
0037 #include <rtems/timecounter.h>
0038 
0039 #include <bsp/lpc-clock-config.h>
0040 #include <bsp/lpc-timer.h>
0041 
0042 #ifdef ARM_MULTILIB_ARCH_V4
0043 
0044 static volatile lpc_timer *const lpc_clock =
0045   (volatile lpc_timer *) LPC_CLOCK_TIMER_BASE;
0046 
0047 static volatile lpc_timer *const lpc_timecounter =
0048   (volatile lpc_timer *) LPC_CLOCK_TIMECOUNTER_BASE;
0049 
0050 static struct timecounter lpc_clock_tc;
0051 
0052 static uint32_t lpc_clock_tc_get_timecount(struct timecounter *tc)
0053 {
0054   return lpc_timecounter->tc;
0055 }
0056 
0057 static void lpc_clock_at_tick(volatile lpc_timer *regs)
0058 {
0059   regs->ir = LPC_TIMER_IR_MR0;
0060 }
0061 
0062 static void lpc_clock_handler_install(rtems_interrupt_handler handler)
0063 {
0064   rtems_status_code sc = RTEMS_SUCCESSFUL;
0065 
0066   sc = rtems_interrupt_handler_install(
0067     LPC_CLOCK_INTERRUPT,
0068     "Clock",
0069     RTEMS_INTERRUPT_UNIQUE,
0070     handler,
0071     RTEMS_DEVOLATILE(lpc_timer *, lpc_clock)
0072   );
0073   if (sc != RTEMS_SUCCESSFUL) {
0074     rtems_fatal_error_occurred(0xdeadbeef);
0075   }
0076 }
0077 
0078 static void lpc_clock_initialize(void)
0079 {
0080   uint64_t interval = ((uint64_t) LPC_CLOCK_REFERENCE
0081     * (uint64_t) rtems_configuration_get_microseconds_per_tick()) / 1000000;
0082 
0083   /* Enable module */
0084   LPC_CLOCK_MODULE_ENABLE();
0085 
0086   /* Reset timer */
0087   lpc_clock->tcr = LPC_TIMER_TCR_RST;
0088 
0089   /* Clear interrupt flags */
0090   lpc_clock->ir = LPC_TIMER_IR_ALL;
0091 
0092   /* Set timer mode */
0093   lpc_clock->ccr = 0;
0094 
0095   /* Timer is incremented every PERIPH_CLK tick */
0096   lpc_clock->pr = 0;
0097 
0098   /* Set match registers */
0099   lpc_clock->mr0 = (uint32_t) interval;
0100 
0101   /* Generate interrupt and reset counter on match with MR0 */
0102   lpc_clock->mcr = LPC_TIMER_MCR_MR0_INTR | LPC_TIMER_MCR_MR0_RST;
0103 
0104   /* No external match */
0105   lpc_clock->emr = 0x0;
0106 
0107   /* Enable timer */
0108   lpc_clock->tcr = LPC_TIMER_TCR_EN;
0109 
0110   /* Install timecounter */
0111   lpc_clock_tc.tc_get_timecount = lpc_clock_tc_get_timecount;
0112   lpc_clock_tc.tc_counter_mask = 0xffffffff;
0113   lpc_clock_tc.tc_frequency = LPC_CLOCK_REFERENCE;
0114   lpc_clock_tc.tc_quality = RTEMS_TIMECOUNTER_QUALITY_CLOCK_DRIVER;
0115   rtems_timecounter_install(&lpc_clock_tc);
0116 }
0117 
0118 #define Clock_driver_support_at_tick(arg) lpc_clock_at_tick(arg)
0119 #define Clock_driver_support_initialize_hardware() lpc_clock_initialize()
0120 #define Clock_driver_support_install_isr(isr) \
0121   lpc_clock_handler_install(isr)
0122 
0123 /* Include shared source clock driver code */
0124 #include "../../../shared/dev/clock/clockimpl.h"
0125 
0126 #endif /* ARM_MULTILIB_ARCH_V4 */