lpc177x_8x_clk.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #ifndef _LPC177X_8X_CLK_H_
  2. #define _LPC177X_8X_CLK_H_
  3. /*
  4. * Copyright (C) 2012 by Ole Reinhardt <ole.reinhardt@embedded-it.de>
  5. *
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. Neither the name of the copyright holders nor the names of
  18. * contributors may be used to endorse or promote products derived
  19. * from this software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  30. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  31. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  32. * SUCH DAMAGE.
  33. *
  34. * For additional information see http://www.ethernut.de/
  35. */
  36. /*
  37. * \verbatim
  38. * $Id: lpc177x_8x_clk.h $
  39. * \endverbatim
  40. */
  41. /*----------------------------------------------------------------------------*
  42. Define clocks
  43. *----------------------------------------------------------------------------*/
  44. #define XTAL (12000000UL) /* Oscillator frequency */
  45. #define OSC_CLK ( XTAL) /* Main oscillator frequency */
  46. #define RTC_CLK ( 32768UL) /* RTC oscillator frequency */
  47. #define IRC_OSC (12000000UL) /* Internal RC oscillator frequency */
  48. #define WDT_OSC ( 500000UL) /* Internal WDT oscillator frequency */
  49. /*----------------------------------------------------------------------------*
  50. Peripheral power control bit positions
  51. *----------------------------------------------------------------------------*/
  52. #define CLKPWR_PCONP_PCLCD 0 /* LCD controller */
  53. #define CLKPWR_PCONP_PCTIM0 1 /* Timer/Counter 0 */
  54. #define CLKPWR_PCONP_PCTIM1 2 /* Timer/Counter 1 */
  55. #define CLKPWR_PCONP_PCUART0 3 /* UART 0 */
  56. #define CLKPWR_PCONP_PCUART1 4 /* UART 1 */
  57. #define CLKPWR_PCONP_PCPWM0 5 /* PWM0 */
  58. #define CLKPWR_PCONP_PCPWM1 6 /* PWM1 */
  59. #define CLKPWR_PCONP_PCI2C0 7 /* I2C 0 */
  60. #define CLKPWR_PCONP_PCUART4 8 /* UART 4 */
  61. #define CLKPWR_PCONP_PCRTC 9 /* RTC */
  62. #define CLKPWR_PCONP_PCSSP1 10 /* SSP 1 */
  63. #define CLKPWR_PCONP_PCEMC 11 /* EMC */
  64. #define CLKPWR_PCONP_PCADC 12 /* ADC 0 */
  65. #define CLKPWR_PCONP_PCCAN1 13 /* CAN 1 */
  66. #define CLKPWR_PCONP_PCCAN2 14 /* CAN 2 */
  67. #define CLKPWR_PCONP_PCGPIO 15 /* GPIO */
  68. #define CLKPWR_PCONP_PCMCPWM 17 /* Motor PWM */
  69. #define CLKPWR_PCONP_PCQEI 18 /* QEI */
  70. #define CLKPWR_PCONP_PCI2C1 19 /* I2C 1 */
  71. #define CLKPWR_PCONP_PCSSP2 20 /* SSP 2 */
  72. #define CLKPWR_PCONP_PCSSP0 21 /* SSP 0 */
  73. #define CLKPWR_PCONP_PCTIM2 22 /* Timer 2 */
  74. #define CLKPWR_PCONP_PCTIM3 23 /* Timer 3 */
  75. #define CLKPWR_PCONP_PCUART2 24 /* UART 2 */
  76. #define CLKPWR_PCONP_PCUART3 25 /* UART 3 */
  77. #define CLKPWR_PCONP_PCI2C2 26 /* I2C 2 */
  78. #define CLKPWR_PCONP_PCI2S 27 /* I2S */
  79. #define CLKPWR_PCONP_PCSDC 28 /* SD Card */
  80. #define CLKPWR_PCONP_PCGPDMA 29 /* GP DMA */
  81. #define CLKPWR_PCONP_PCENET 30 /* Ethernet */
  82. #define CLKPWR_PCONP_PCUSB 31 /* USB */
  83. /*----------------------------------------------------------------------------*
  84. Peripheral reset control definitions
  85. *----------------------------------------------------------------------------*/
  86. #define CLKPWR_RSTCON0_LCD 0 /* LCD controller */
  87. #define CLKPWR_RSTCON0_TIM0 1 /* Timer/Counter 0 */
  88. #define CLKPWR_RSTCON0_TIM1 2 /* Timer/Counter 1 */
  89. #define CLKPWR_RSTCON0_UART0 3 /* UART 0 */
  90. #define CLKPWR_RSTCON0_UART1 4 /* UART 1 */
  91. #define CLKPWR_RSTCON0_PWM0 5 /* PWM0 */
  92. #define CLKPWR_RSTCON0_PWM1 6 /* PWM1 */
  93. #define CLKPWR_RSTCON0_I2C0 7 /* I2C 0 */
  94. #define CLKPWR_RSTCON0_UART4 8 /* UART 4 */
  95. #define CLKPWR_RSTCON0_RTC 9 /* RTC */
  96. #define CLKPWR_RSTCON0_SSP1 10 /* SSP 1 */
  97. #define CLKPWR_RSTCON0_EMC 11 /* EMC */
  98. #define CLKPWR_RSTCON0_ADC 12 /* ADC 0 */
  99. #define CLKPWR_RSTCON0_CAN1 13 /* CAN 1 */
  100. #define CLKPWR_RSTCON0_CAN2 14 /* CAN 2 */
  101. #define CLKPWR_RSTCON0_GPIO 15 /* GPIO */
  102. #define CLKPWR_RSTCON0_MCPWM 17 /* Motor PWM */
  103. #define CLKPWR_RSTCON0_QEI 18 /* QEI */
  104. #define CLKPWR_RSTCON0_I2C1 19 /* I2C 1 */
  105. #define CLKPWR_RSTCON0_SSP2 20 /* SSP 2 */
  106. #define CLKPWR_RSTCON0_SSP0 21 /* SSP 0 */
  107. #define CLKPWR_RSTCON0_TIM2 22 /* Timer 2 */
  108. #define CLKPWR_RSTCON0_TIM3 23 /* Timer 3 */
  109. #define CLKPWR_RSTCON0_UART2 24 /* UART 2 */
  110. #define CLKPWR_RSTCON0_UART3 25 /* UART 3 */
  111. #define CLKPWR_RSTCON0_I2C2 26 /* I2C 2 */
  112. #define CLKPWR_RSTCON0_I2S 27 /* I2S */
  113. #define CLKPWR_RSTCON0_SDC 28 /* SD Card */
  114. #define CLKPWR_RSTCON0_GPDMA 29 /* GP DMA */
  115. #define CLKPWR_RSTCON0_ENET 30 /* Ethernet */
  116. #define CLKPWR_RSTCON0_USB 31 /* USB */
  117. #define CLKPWR_RSTCON1_IOCON 32 /* IOCON */
  118. #define CLKPWR_RSTCON1_DAC 33 /* DAC */
  119. #define CLKPWR_RSTCON1_CANACC 34 /* CAN ACC */
  120. #define SysCtlPeripheralClkEnable(bit) CM3BBSET(LPC_SC_BASE, LPC_SC_TypeDef, PCONP, (bit))
  121. #define SysCtlPeripheralClkDisable(bit) CM3BBCLR(LPC_SC_BASE, LPC_SC_TypeDef, PCONP, (bit))
  122. #define SysCtlPeripheralClkGet(bit) CM3BBGET(LPC_SC_BASE, LPC_SC_TypeDef, PCONP, (bit))
  123. #define SysCtlPeripheralResetEnable(bit) if ((bit) < 32) \
  124. CM3BBSET(LPC_SC_BASE, LPC_SC_TypeDef, RSTCON0, (bit)); else \
  125. CM3BBSET(LPC_SC_BASE, LPC_SC_TypeDef, RSTCON1, (bit - 32));
  126. #define SysCtlPeripheralResetDisable(bit) if ((bit) < 32) \
  127. CM3BBCLR(LPC_SC_BASE, LPC_SC_TypeDef, RSTCON0, (bit)); else \
  128. CM3BBCLR(LPC_SC_BASE, LPC_SC_TypeDef, RSTCON1, (bit - 32));
  129. extern uint32_t Lpc17xx_ClockGet(int idx);
  130. extern uint32_t SysCtlClockGet(void);
  131. extern int SetSysClock(void);
  132. /* Transform ns into clock cycles (runtime, only 32bit multiplications):
  133. * Valid input range: Clock < 995 MHz, ns = 0..1000000 (1ms)
  134. *
  135. * a) Divide Clock by 16. This gives enough headroom for step b).
  136. * b) Multiply Clock by 69. This will adjust for decimal/binary divisor.
  137. * This computation will overflow for a clock > 995 MHz!
  138. * This computation will give results 0,5% larger than the real value.
  139. * c) Divide Clock by 1048576 (2^20). This will give enough headroom for step d).
  140. * d) Multiply Clock by ns. This will not overflow for ns = 0..1000000 (1ms).
  141. * e) Divide Clock by 4096 (2^12). This will give value in clocks.
  142. * f) Add 1 to account for rounding.
  143. * (Use runtime computations because frequencies may change in runtime).
  144. */
  145. #define NS_2_CLKS(clock, ns) (((((((clock) >> 4)*69) >> 20)*(ns))>>12)+1)
  146. /* Delay loop for short busy waits */
  147. __attribute__( ( always_inline ) ) static inline void wait_clocks( unsigned int clocks)
  148. {
  149. clocks >>= 1; // 2 clocks per cycle
  150. while(clocks--) {
  151. asm("":::"memory"); // hint for gcc: do not remove this loop!
  152. }
  153. }
  154. #endif /* _LPC177X_8X_CLK_H_ */