lpc176x_clk.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #ifndef _LPC17XX_CLK_H_
  2. #define _LPC17XX_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: $
  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 ( 32000UL) /* RTC oscillator frequency */
  47. #define IRC_OSC ( 4000000UL) /* Internal RC oscillator frequency */
  48. /*----------------------------------------------------------------------------*
  49. Peripheral clock divider bit positions
  50. *----------------------------------------------------------------------------*/
  51. #define CLKPWR_PCLKSEL_WDT 0 /* WDT */
  52. #define CLKPWR_PCLKSEL_TIMER0 2 /* TIMER 0 */
  53. #define CLKPWR_PCLKSEL_TIMER1 4 /* TIMER 1 */
  54. #define CLKPWR_PCLKSEL_UART0 6 /* UART 0 */
  55. #define CLKPWR_PCLKSEL_UART1 8 /* UART 1 */
  56. #define CLKPWR_PCLKSEL_PWM1 12 /* PWM 1 */
  57. #define CLKPWR_PCLKSEL_I2C0 14 /* I2C 0 */
  58. #define CLKPWR_PCLKSEL_SPI 16 /* SPI */
  59. #define CLKPWR_PCLKSEL_SSP1 20 /* SSP 1 */
  60. #define CLKPWR_PCLKSEL_DAC 22 /* DAC */
  61. #define CLKPWR_PCLKSEL_ADC 24 /* ADC */
  62. #define CLKPWR_PCLKSEL_CAN1 26 /* CAN 1 */
  63. #define CLKPWR_PCLKSEL_CAN2 28 /* CAN 2 */
  64. #define CLKPWR_PCLKSEL_ACF 30 /* ACF */
  65. #define CLKPWR_PCLKSEL_QEI 32 /* QEI */
  66. #define CLKPWR_PCLKSEL_PCB 36 /* PCB */
  67. #define CLKPWR_PCLKSEL_I2C1 38 /* I2C 1 */
  68. #define CLKPWR_PCLKSEL_SSP0 42 /* SSP 0 */
  69. #define CLKPWR_PCLKSEL_TIMER2 44 /* TIMER 2 */
  70. #define CLKPWR_PCLKSEL_TIMER3 46 /* TIMER 3 */
  71. #define CLKPWR_PCLKSEL_UART2 48 /* UART 2 */
  72. #define CLKPWR_PCLKSEL_UART3 50 /* UART 3 */
  73. #define CLKPWR_PCLKSEL_I2C2 52 /* I2C 2 */
  74. #define CLKPWR_PCLKSEL_I2S 54 /* I2S */
  75. #define CLKPWR_PCLKSEL_RIT 58 /* RIT */
  76. #define CLKPWR_PCLKSEL_SYSCON 60 /* SYSCON */
  77. #define CLKPWR_PCLKSEL_MC 62 /* MC */
  78. /* Peripheral clock devider selection values
  79. * Note: When CCLK_DIV_8, Peripheral's clock is selected to
  80. * PCLK_xyz = CCLK/8 except for CAN1, CAN2, and CAN filtering
  81. * when 0b11 selects PCLK_xyz = CCLK/6
  82. */
  83. #define CLKPWR_PCLKSEL_CCLK_DIV_4 0 /* CCLK / 4 */
  84. #define CLKPWR_PCLKSEL_CCLK_DIV_1 1 /* CCLK */
  85. #define CLKPWR_PCLKSEL_CCLK_DIV_2 2 /* CCLK / 2 */
  86. #define CLKPWR_PCLKSEL_CCLK_DIV_8 3 /* CCLK / 8 */
  87. /*----------------------------------------------------------------------------*
  88. Peripheral power control bit positions
  89. *----------------------------------------------------------------------------*/
  90. #define CLKPWR_PCONP_PCTIM0 1 /* Timer/Counter 0 */
  91. #define CLKPWR_PCONP_PCTIM1 2 /* Timer/Counter 1 */
  92. #define CLKPWR_PCONP_PCUART0 3 /* UART 0 */
  93. #define CLKPWR_PCONP_PCUART1 4 /* UART 1 */
  94. #define CLKPWR_PCONP_PCPWM1 6 /* PWM1 */
  95. #define CLKPWR_PCONP_PCI2C0 7 /* I2C 0 */
  96. #define CLKPWR_PCONP_PCSPI 8 /* SPI */
  97. #define CLKPWR_PCONP_PCRTC 9 /* RTC */
  98. #define CLKPWR_PCONP_PCSSP1 10 /* SSP 1 */
  99. #define CLKPWR_PCONP_PCAD 12 /* ADC 0 */
  100. #define CLKPWR_PCONP_PCCAN1 13 /* CAN 1 */
  101. #define CLKPWR_PCONP_PCCAN2 14 /* CAN 2 */
  102. #define CLKPWR_PCONP_PCGPIO 15 /* GPIO */
  103. #define CLKPWR_PCONP_PCRIT 16 /* RIT */
  104. #define CLKPWR_PCONP_PCMC 17 /* Motor PWM */
  105. #define CLKPWR_PCONP_PCQEI 18 /* QEI */
  106. #define CLKPWR_PCONP_PCI2C1 19 /* I2C 1 */
  107. #define CLKPWR_PCONP_PCSSP0 21 /* SSP 0 */
  108. #define CLKPWR_PCONP_PCTIM2 22 /* Timer 2 */
  109. #define CLKPWR_PCONP_PCTIM3 23 /* Timer 3 */
  110. #define CLKPWR_PCONP_PCUART2 24 /* UART 2 */
  111. #define CLKPWR_PCONP_PCUART3 25 /* UART 3 */
  112. #define CLKPWR_PCONP_PCI2C2 26 /* I2C 2 */
  113. #define CLKPWR_PCONP_PCI2S 27 /* I2S */
  114. #define CLKPWR_PCONP_PCGPDMA 29 /* GP DMA */
  115. #define CLKPWR_PCONP_PCENET 30 /* Ethernet */
  116. #define CLKPWR_PCONP_PCUSB 31 /* USB */
  117. #define SysCtlPeripheralClkEnable(bit) CM3BBSET(LPC_SC_BASE, LPC_SC_TypeDef, PCONP, (bit))
  118. #define SysCtlPeripheralClkDisable(bit) CM3BBCLR(LPC_SC_BASE, LPC_SC_TypeDef, PCONP, (bit))
  119. #define SysCtlPeripheralClkGet(bit) CM3BBGET(LPC_SC_BASE, LPC_SC_TypeDef, PCONP, (bit))
  120. extern uint32_t SysCtlClockGet(void);
  121. extern int SetSysClock(void);
  122. extern uint32_t Lpc17xx_ClockGet(int idx);
  123. extern int Lpc176x_PclkDivGet(int id);
  124. extern void Lpc176x_PclkDivSet(int id, int div) ;
  125. /* Transform ns into clock cycles (runtime, only 32bit multiplications):
  126. * Valid input range: Clock < 995 MHz, ns = 0..1000000 (1ms)
  127. *
  128. * a) Divide Clock by 16. This gives enough headroom for step b).
  129. * b) Multiply Clock by 69. This will adjust for decimal/binary divisor.
  130. * This computation will overflow for a clock > 995 MHz!
  131. * This computation will give results 0,5% larger than the real value.
  132. * c) Divide Clock by 1048576 (2^20). This will give enough headroom for step d).
  133. * d) Multiply Clock by ns. This will not overflow for ns = 0..1000000 (1ms).
  134. * e) Divide Clock by 4096 (2^12). This will give value in clocks.
  135. * f) Add 1 to account for rounding.
  136. * (Use runtime computations because frequencies may change in runtime).
  137. */
  138. #define NS_2_CLKS(clock, ns) (((((((clock) >> 4)*69) >> 20)*(ns))>>12)+1)
  139. /* Delay loop for short busy waits */
  140. __attribute__( ( always_inline ) ) static inline void wait_clocks( unsigned int clocks)
  141. {
  142. clocks >>= 1; // 2 clocks per cycle
  143. while(clocks--) {
  144. asm("":::"memory"); // hint for gcc: do not remove this loop!
  145. }
  146. }
  147. #endif /* _LPC17XX_CLK_H_ */