Nut/OS  5.0.5
API Reference
lpc177x_8x_clk.h
Go to the documentation of this file.
00001 #ifndef _LPC177X_8X_CLK_H_
00002 #define _LPC177X_8X_CLK_H_
00003 
00004 /*
00005  * Copyright (C) 2012 by Ole Reinhardt <ole.reinhardt@embedded-it.de>
00006  *
00007  * All rights reserved.
00008  *
00009  * Redistribution and use in source and binary forms, with or without
00010  * modification, are permitted provided that the following conditions
00011  * are met:
00012  *
00013  * 1. Redistributions of source code must retain the above copyright
00014  *    notice, this list of conditions and the following disclaimer.
00015  * 2. Redistributions in binary form must reproduce the above copyright
00016  *    notice, this list of conditions and the following disclaimer in the
00017  *    documentation and/or other materials provided with the distribution.
00018  * 3. Neither the name of the copyright holders nor the names of
00019  *    contributors may be used to endorse or promote products derived
00020  *    from this software without specific prior written permission.
00021  *
00022  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00025  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00026  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00027  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00028  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
00029  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
00030  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00031  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
00032  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00033  * SUCH DAMAGE.
00034  *
00035  * For additional information see http://www.ethernut.de/
00036  */
00037 
00038 /*
00039  * \verbatim
00040  * $Id: lpc177x_8x_clk.h $
00041  * \endverbatim
00042  */
00043 
00044 /*----------------------------------------------------------------------------*
00045   Define clocks
00046  *----------------------------------------------------------------------------*/
00047 
00048 #define XTAL        (12000000UL)   /* Oscillator frequency               */
00049 #define OSC_CLK     (      XTAL)   /* Main oscillator frequency          */
00050 #define RTC_CLK     (   32768UL)   /* RTC oscillator frequency           */
00051 #define IRC_OSC     (12000000UL)   /* Internal RC oscillator frequency   */
00052 #define WDT_OSC     (  500000UL)   /* Internal WDT oscillator frequency  */
00053 
00054 
00055 /*----------------------------------------------------------------------------*
00056   Peripheral power control bit positions
00057  *----------------------------------------------------------------------------*/
00058 
00059 #define  CLKPWR_PCONP_PCLCD     0  /* LCD controller  */
00060 #define  CLKPWR_PCONP_PCTIM0    1  /* Timer/Counter 0 */
00061 #define  CLKPWR_PCONP_PCTIM1    2  /* Timer/Counter 1 */
00062 #define  CLKPWR_PCONP_PCUART0   3  /* UART 0          */
00063 #define  CLKPWR_PCONP_PCUART1   4  /* UART 1          */
00064 #define  CLKPWR_PCONP_PCPWM0    5  /* PWM0            */
00065 #define  CLKPWR_PCONP_PCPWM1    6  /* PWM1            */
00066 #define  CLKPWR_PCONP_PCI2C0    7  /* I2C 0           */
00067 #define  CLKPWR_PCONP_PCUART4   8  /* UART 4          */
00068 #define  CLKPWR_PCONP_PCRTC     9  /* RTC             */
00069 #define  CLKPWR_PCONP_PCSSP1    10 /* SSP 1           */
00070 #define  CLKPWR_PCONP_PCEMC     11 /* EMC             */
00071 #define  CLKPWR_PCONP_PCADC     12 /* ADC 0           */
00072 #define  CLKPWR_PCONP_PCAN1     13 /* CAN 1           */
00073 #define  CLKPWR_PCONP_PCAN2     14 /* CAN 2           */
00074 #define  CLKPWR_PCONP_PCGPIO    15 /* GPIO            */
00075 
00076 #define  CLKPWR_PCONP_PCMCPWM   17 /* Motor PWM       */
00077 #define  CLKPWR_PCONP_PCQEI     18 /* QEI             */
00078 #define  CLKPWR_PCONP_PCI2C1    19 /* I2C 1           */
00079 #define  CLKPWR_PCONP_PCSSP2    20 /* SSP 2           */
00080 #define  CLKPWR_PCONP_PCSSP0    21 /* SSP 0           */
00081 #define  CLKPWR_PCONP_PCTIM2    22 /* Timer 2         */
00082 #define  CLKPWR_PCONP_PCTIM3    23 /* Timer 3         */
00083 #define  CLKPWR_PCONP_PCUART2   24 /* UART 2          */
00084 #define  CLKPWR_PCONP_PCUART3   25 /* UART 3          */
00085 #define  CLKPWR_PCONP_PCI2C2    26 /* I2C 2           */
00086 #define  CLKPWR_PCONP_PCI2S     27 /* I2S             */
00087 #define  CLKPWR_PCONP_PCSDC     28 /* SD Card         */
00088 #define  CLKPWR_PCONP_PCGPDMA   29 /* GP DMA          */
00089 #define  CLKPWR_PCONP_PCENET    30 /* Ethernet        */
00090 #define  CLKPWR_PCONP_PCUSB     31 /* USB             */
00091 
00092 
00093 /*----------------------------------------------------------------------------*
00094   Peripheral reset control definitions
00095  *----------------------------------------------------------------------------*/
00096 
00097 #define  CLKPWR_RSTCON0_LCD     0  /* LCD controller  */
00098 #define  CLKPWR_RSTCON0_TIM0    1  /* Timer/Counter 0 */
00099 #define  CLKPWR_RSTCON0_TIM1    2  /* Timer/Counter 1 */
00100 #define  CLKPWR_RSTCON0_UART0   3  /* UART 0          */
00101 #define  CLKPWR_RSTCON0_UART1   4  /* UART 1          */
00102 #define  CLKPWR_RSTCON0_PWM0    5  /* PWM0            */
00103 #define  CLKPWR_RSTCON0_PWM1    6  /* PWM1            */
00104 #define  CLKPWR_RSTCON0_I2C0    7  /* I2C 0           */
00105 #define  CLKPWR_RSTCON0_UART4   8  /* UART 4          */
00106 #define  CLKPWR_RSTCON0_RTC     9  /* RTC             */
00107 #define  CLKPWR_RSTCON0_SSP1    10  /* SSP 1           */
00108 #define  CLKPWR_RSTCON0_EMC     11  /* EMC             */
00109 #define  CLKPWR_RSTCON0_ADC     12  /* ADC 0           */
00110 #define  CLKPWR_RSTCON0_CAN1    13  /* CAN 1           */
00111 #define  CLKPWR_RSTCON0_CAN2    14  /* CAN 2           */
00112 #define  CLKPWR_RSTCON0_GPIO    15  /* GPIO            */
00113 
00114 #define  CLKPWR_RSTCON0_MCPWM   17  /* Motor PWM       */
00115 #define  CLKPWR_RSTCON0_QEI     18  /* QEI             */
00116 #define  CLKPWR_RSTCON0_I2C1    19  /* I2C 1           */
00117 #define  CLKPWR_RSTCON0_SSP2    20  /* SSP 2           */
00118 #define  CLKPWR_RSTCON0_SSP0    21  /* SSP 0           */
00119 #define  CLKPWR_RSTCON0_TIM2    22  /* Timer 2         */
00120 #define  CLKPWR_RSTCON0_TIM3    23  /* Timer 3         */
00121 #define  CLKPWR_RSTCON0_UART2   24  /* UART 2          */
00122 #define  CLKPWR_RSTCON0_UART3   25  /* UART 3          */
00123 #define  CLKPWR_RSTCON0_I2C2    26  /* I2C 2           */
00124 #define  CLKPWR_RSTCON0_I2S     27  /* I2S             */
00125 #define  CLKPWR_RSTCON0_SDC     28  /* SD Card         */
00126 #define  CLKPWR_RSTCON0_GPDMA   29  /* GP DMA          */
00127 #define  CLKPWR_RSTCON0_ENET    30  /* Ethernet        */
00128 #define  CLKPWR_RSTCON0_USB     31  /* USB             */
00129 
00130 #define  CLKPWR_RSTCON1_IOCON   32  /* IOCON           */
00131 #define  CLKPWR_RSTCON1_DAC     33  /* DAC             */
00132 #define  CLKPWR_RSTCON1_CANACC  34  /* CAN ACC         */
00133 
00134 
00135 #define SysCtlPeripheralClkEnable(bit)    CM3BBREG(LPC_SC_BASE, LPC_SC_TypeDef, PCONP, (bit)) = 1
00136 #define SysCtlPeripheralClkDisable(bit)   CM3BBREG(LPC_SC_BASE, LPC_SC_TypeDef, PCONP, (bit)) = 0
00137 #define SysCtlPeripheralClkGet(bit)       CM3BBREG(LPC_SC_BASE, LPC_SC_TypeDef, PCONP, (bit))
00138 
00139 #define SysCtlPeripheralResetEnable(bit)  if ((bit) < 32) \
00140                                           CM3BBREG(LPC_SC_BASE, LPC_SC_TypeDef, RSTCON0, (bit)) = 1; else \
00141                                           CM3BBREG(LPC_SC_BASE, LPC_SC_TypeDef, RSTCON1, (bit - 32)) = 1;
00142 #define SysCtlPeripheralResetDisable(bit) if ((bit) < 32) \
00143                                           CM3BBREG(LPC_SC_BASE, LPC_SC_TypeDef, RSTCON0, (bit)) = 0; else \
00144                                           CM3BBREG(LPC_SC_BASE, LPC_SC_TypeDef, RSTCON1, (bit - 32)) = 0;
00145 
00146 uint32_t Lpc17xx_ClockGet(int idx);
00147 uint32_t SysCtlClockGet(void);
00148 int SetSysClock(void);
00149 
00150 /* Transform ns into clock cycles (runtime, only 32bit multiplications):
00151  * Valid input range: Clock < 995 MHz, ns = 0..1000000 (1ms)
00152  *
00153  * a) Divide Clock by 16. This gives enough headroom for step b).
00154  * b) Multiply Clock by 69. This will adjust for decimal/binary divisor.
00155  *    This computation will overflow for a clock > 995 MHz!
00156  *    This computation will give results 0,5% larger than the real value.
00157  * c) Divide Clock by 1048576 (2^20). This will give enough headroom for step d).
00158  * d) Multiply Clock by ns. This will not overflow for ns = 0..1000000 (1ms).
00159  * e) Divide Clock by 4096 (2^12). This will give value in clocks.
00160  * f) Add 1 to account for rounding.
00161  * (Use runtime computations because frequencies may change in runtime).
00162  */
00163 #define NS_2_CLKS(clock, ns) (((((((clock) >> 4)*69) >> 20)*(ns))>>12)+1)
00164 
00165 /* Delay loop for short busy waits */
00166 __attribute__( ( always_inline ) ) static inline void wait_clocks( unsigned int clocks)
00167 {
00168    clocks >>= 1;          // 2 clocks per cycle
00169    while(clocks--) {
00170       asm("":::"memory"); // hint for gcc: do not remove this loop!
00171    }
00172 }
00173 
00174 #endif /* _LPC177X_8X_CLK_H_ */