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 RTEMSDriverClockArmv7MSysTick
0007  *
0008  * @brief This source file contains the Armv7-M SysTick Clock Driver.
0009  */
0010 
0011 /*
0012  * Copyright (C) 2020 embedded brains GmbH & Co. KG
0013  * Copyright (C) 2011, 2018 Sebastian Huber
0014  *
0015  * Redistribution and use in source and binary forms, with or without
0016  * modification, are permitted provided that the following conditions
0017  * are met:
0018  * 1. Redistributions of source code must retain the above copyright
0019  *    notice, this list of conditions and the following disclaimer.
0020  * 2. Redistributions in binary form must reproduce the above copyright
0021  *    notice, this list of conditions and the following disclaimer in the
0022  *    documentation and/or other materials provided with the distribution.
0023  *
0024  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0025  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0026  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0027  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0028  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0029  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0030  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0031  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0032  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0033  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0034  * POSSIBILITY OF SUCH DAMAGE.
0035  */
0036 
0037 #include <bsp/clock-armv7m.h>
0038 
0039 #include <rtems.h>
0040 #include <rtems/sysinit.h>
0041 
0042 #ifdef ARM_MULTILIB_ARCH_V7M
0043 
0044 /* This is defined in dev/clock/clockimpl.h */
0045 static void Clock_isr(void *arg);
0046 
0047 ARMV7M_Timecounter _ARMV7M_TC;
0048 
0049 static uint32_t _ARMV7M_TC_get_timecount(struct timecounter *base)
0050 {
0051   return _ARMV7M_Clock_counter((ARMV7M_Timecounter *) base);
0052 }
0053 
0054 void _ARMV7M_Clock_handler(void)
0055 {
0056   _ARMV7M_Interrupt_service_enter();
0057   Clock_isr(NULL);
0058   _ARMV7M_Interrupt_service_leave();
0059 }
0060 
0061 static void _ARMV7M_Clock_handler_install(void)
0062 {
0063   _ARMV7M_Set_exception_priority(
0064     ARMV7M_VECTOR_SYSTICK,
0065     BSP_ARMV7M_SYSTICK_PRIORITY
0066   );
0067 }
0068 
0069 static void _ARMV7M_Clock_initialize(void)
0070 {
0071   volatile ARMV7M_Systick *systick;
0072   ARMV7M_Timecounter *tc;
0073 
0074   systick = _ARMV7M_Systick;
0075   tc = &_ARMV7M_TC;
0076 
0077   systick->csr = ARMV7M_SYSTICK_CSR_ENABLE
0078     | ARMV7M_SYSTICK_CSR_TICKINT
0079     | ARMV7M_SYSTICK_CSR_CLKSOURCE;
0080 
0081   tc->base.tc_get_timecount = _ARMV7M_TC_get_timecount;
0082   tc->base.tc_counter_mask = 0xffffffff;
0083   tc->base.tc_frequency = _ARMV7M_Clock_frequency();
0084   tc->base.tc_quality = RTEMS_TIMECOUNTER_QUALITY_CLOCK_DRIVER;
0085   rtems_timecounter_install(&tc->base);
0086 }
0087 
0088 static void _ARMV7M_Clock_initialize_early(void)
0089 {
0090   volatile ARMV7M_Systick *systick;
0091   uint32_t us_per_tick;
0092   uint64_t freq;
0093   uint32_t interval;
0094 
0095   systick = _ARMV7M_Systick;
0096   us_per_tick = rtems_configuration_get_microseconds_per_tick();
0097   freq = _ARMV7M_Clock_frequency();
0098 
0099   interval = (uint32_t) ((freq * us_per_tick) / 1000000);
0100 
0101   systick->rvr = interval - 1;
0102   systick->cvr = 0;
0103   systick->csr = ARMV7M_SYSTICK_CSR_ENABLE | ARMV7M_SYSTICK_CSR_CLKSOURCE;
0104 }
0105 
0106 RTEMS_SYSINIT_ITEM(
0107   _ARMV7M_Clock_initialize_early,
0108   RTEMS_SYSINIT_CPU_COUNTER,
0109   RTEMS_SYSINIT_ORDER_FIRST
0110 );
0111 
0112 #define Clock_driver_support_initialize_hardware() \
0113   _ARMV7M_Clock_initialize()
0114 
0115 #define Clock_driver_support_install_isr(isr) \
0116   _ARMV7M_Clock_handler_install()
0117 
0118 /* Include shared source clock driver code */
0119 #include "../../../shared/dev/clock/clockimpl.h"
0120 
0121 #endif /* ARM_MULTILIB_ARCH_V7M */