Back to home page

LXR

 
 

    


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

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /**
0004  * @file
0005  *
0006  * @ingroup RTEMSBSPsARMLPC176X_clocks
0007  *
0008  * @brief System clocks.
0009  */
0010 
0011 /*
0012  * Copyright (C) 2008, 2012 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 <bsp.h>
0037 #include <bsp/system-clocks.h>
0038 
0039 /**
0040  * @brief Internal RC oscillator frequency in [Hz].
0041  */
0042 #define LPC176X_OSCILLATOR_INTERNAL 4000000u
0043 
0044 #ifndef LPC176X_OSCILLATOR_MAIN
0045 #error "unknown main oscillator frequency"
0046 #endif
0047 
0048 #ifndef LPC176X_OSCILLATOR_RTC
0049 #error "unknown RTC oscillator frequency"
0050 #endif
0051 
0052 inline unsigned lpc176x_sysclk( void );
0053 
0054 void lpc176x_timer_initialize( void )
0055 {
0056   /* Reset timer */
0057   LPC176X_T1TCR = TCR_RST;
0058   /* Set timer mode */
0059   LPC176X_T1CTCR = 0u;
0060   /* Set prescaler to zero */
0061   LPC176X_T1PR = 0u;
0062   /* Reset all interrupt flags */
0063   LPC176X_T1IR = 0xffU;
0064   /* Do not stop on a match */
0065   LPC176X_T1MCR = 0u;
0066   /* No captures */
0067   LPC176X_T1CCR = 0u;
0068   /* Start timer */
0069   LPC176X_T1TCR = TCR_EN;
0070 }
0071 
0072 void lpc176x_micro_seconds_delay( const unsigned us )
0073 {
0074   const unsigned start = lpc176x_get_timer1();
0075   const unsigned delay = us * ( LPC176X_PCLK / 1000000u );
0076   unsigned       elapsed = 0u;
0077 
0078   do {
0079     elapsed = lpc176x_get_timer1() - start;
0080   } while ( elapsed < delay );
0081 }
0082 
0083 unsigned lpc176x_sysclk( void )
0084 {
0085   return ( LPC176X_SCB.clksrcsel & LPC176X_SCB_CLKSRCSEL_CLKSRC ) != 0u ?
0086          LPC176X_OSCILLATOR_MAIN : LPC176X_OSCILLATOR_INTERNAL;
0087 }
0088 
0089 unsigned lpc176x_pllclk( void )
0090 {
0091   const unsigned sysclk = lpc176x_sysclk();
0092   const unsigned pllstat = ( LPC176X_SCB.pll_0 ).stat;
0093   const unsigned enabled_and_locked = LPC176X_PLL_STAT_PLLE |
0094                                       LPC176X_PLL_STAT_PLOCK;
0095   unsigned pllclk = 0u;
0096 
0097   if ( ( pllstat & enabled_and_locked ) == enabled_and_locked ) {
0098     unsigned m = LPC176X_PLL_SEL_MSEL_GET( pllstat ) + 1u;
0099     pllclk = sysclk * m;
0100   }
0101 
0102   /* else implies that the pllstat is unlocked. Also,
0103      there is nothing to do. */
0104 
0105   return pllclk;
0106 }
0107 
0108 unsigned lpc176x_cclk( void )
0109 {
0110   const unsigned cclksel = LPC176X_SCB.cclksel;
0111   unsigned       cclk_in = 0u;
0112   unsigned       cclk = 0u;
0113 
0114   if ( ( cclksel & LPC176X_SCB_CCLKSEL_CCLKSEL ) != 0u ) {
0115     cclk_in = lpc176x_pllclk();
0116   } else {
0117     cclk_in = lpc176x_sysclk();
0118   }
0119 
0120   cclk = cclk_in / LPC176X_SCB_CCLKSEL_CCLKDIV_GET( cclksel );
0121 
0122   return cclk;
0123 }
0124 
0125 uint32_t _CPU_Counter_frequency(void)
0126 {
0127   return LPC176X_PCLK;
0128 }
0129 
0130 CPU_Counter_ticks _CPU_Counter_read( void )
0131 {
0132   return lpc176x_get_timer1();
0133 }