My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

lpc17xx_clkpwr.c 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /**********************************************************************
  2. * $Id$ lpc17xx_clkpwr.c 2010-06-18
  3. *//**
  4. * @file lpc17xx_clkpwr.c
  5. * @brief Contains all functions support for Clock and Power Control
  6. * firmware library on LPC17xx
  7. * @version 3.0
  8. * @date 18. June. 2010
  9. * @author NXP MCU SW Application Team
  10. *
  11. * Copyright(C) 2010, NXP Semiconductor
  12. * All rights reserved.
  13. *
  14. ***********************************************************************
  15. * Software that is described herein is for illustrative purposes only
  16. * which provides customers with programming information regarding the
  17. * products. This software is supplied "AS IS" without any warranties.
  18. * NXP Semiconductors assumes no responsibility or liability for the
  19. * use of the software, conveys no license or title under any patent,
  20. * copyright, or mask work right to the product. NXP Semiconductors
  21. * reserves the right to make changes in the software without
  22. * notification. NXP Semiconductors also make no representation or
  23. * warranty that such application will be suitable for the specified
  24. * use without further testing or modification.
  25. * Permission to use, copy, modify, and distribute this software and its
  26. * documentation is hereby granted, under NXP Semiconductors'
  27. * relevant copyright in the software, without fee, provided that it
  28. * is used in conjunction with NXP Semiconductors microcontrollers. This
  29. * copyright, permission, and disclaimer notice must appear in all copies of
  30. * this code.
  31. **********************************************************************/
  32. /* Peripheral group ----------------------------------------------------------- */
  33. /** @addtogroup CLKPWR
  34. * @{
  35. */
  36. /* Includes ------------------------------------------------------------------- */
  37. #include "lpc17xx_clkpwr.h"
  38. /* Public Functions ----------------------------------------------------------- */
  39. /** @addtogroup CLKPWR_Public_Functions
  40. * @{
  41. */
  42. /*********************************************************************//**
  43. * @brief Set value of each Peripheral Clock Selection
  44. * @param[in] ClkType Peripheral Clock Selection of each type,
  45. * should be one of the following:
  46. * - CLKPWR_PCLKSEL_WDT : WDT
  47. - CLKPWR_PCLKSEL_TIMER0 : Timer 0
  48. - CLKPWR_PCLKSEL_TIMER1 : Timer 1
  49. - CLKPWR_PCLKSEL_UART0 : UART 0
  50. - CLKPWR_PCLKSEL_UART1 : UART 1
  51. - CLKPWR_PCLKSEL_PWM1 : PWM 1
  52. - CLKPWR_PCLKSEL_I2C0 : I2C 0
  53. - CLKPWR_PCLKSEL_SPI : SPI
  54. - CLKPWR_PCLKSEL_SSP1 : SSP 1
  55. - CLKPWR_PCLKSEL_DAC : DAC
  56. - CLKPWR_PCLKSEL_ADC : ADC
  57. - CLKPWR_PCLKSEL_CAN1 : CAN 1
  58. - CLKPWR_PCLKSEL_CAN2 : CAN 2
  59. - CLKPWR_PCLKSEL_ACF : ACF
  60. - CLKPWR_PCLKSEL_QEI : QEI
  61. - CLKPWR_PCLKSEL_PCB : PCB
  62. - CLKPWR_PCLKSEL_I2C1 : I2C 1
  63. - CLKPWR_PCLKSEL_SSP0 : SSP 0
  64. - CLKPWR_PCLKSEL_TIMER2 : Timer 2
  65. - CLKPWR_PCLKSEL_TIMER3 : Timer 3
  66. - CLKPWR_PCLKSEL_UART2 : UART 2
  67. - CLKPWR_PCLKSEL_UART3 : UART 3
  68. - CLKPWR_PCLKSEL_I2C2 : I2C 2
  69. - CLKPWR_PCLKSEL_I2S : I2S
  70. - CLKPWR_PCLKSEL_RIT : RIT
  71. - CLKPWR_PCLKSEL_SYSCON : SYSCON
  72. - CLKPWR_PCLKSEL_MC : MC
  73. * @param[in] DivVal Value of divider, should be:
  74. * - CLKPWR_PCLKSEL_CCLK_DIV_4 : PCLK_peripheral = CCLK/4
  75. * - CLKPWR_PCLKSEL_CCLK_DIV_1 : PCLK_peripheral = CCLK/1
  76. * - CLKPWR_PCLKSEL_CCLK_DIV_2 : PCLK_peripheral = CCLK/2
  77. *
  78. * @return none
  79. **********************************************************************/
  80. void CLKPWR_SetPCLKDiv (uint32_t ClkType, uint32_t DivVal)
  81. {
  82. uint32_t bitpos;
  83. bitpos = (ClkType < 32) ? (ClkType) : (ClkType - 32);
  84. /* PCLKSEL0 selected */
  85. if (ClkType < 32)
  86. {
  87. /* Clear two bit at bit position */
  88. LPC_SC->PCLKSEL0 &= (~(CLKPWR_PCLKSEL_BITMASK(bitpos)));
  89. /* Set two selected bit */
  90. LPC_SC->PCLKSEL0 |= (CLKPWR_PCLKSEL_SET(bitpos, DivVal));
  91. }
  92. /* PCLKSEL1 selected */
  93. else
  94. {
  95. /* Clear two bit at bit position */
  96. LPC_SC->PCLKSEL1 &= ~(CLKPWR_PCLKSEL_BITMASK(bitpos));
  97. /* Set two selected bit */
  98. LPC_SC->PCLKSEL1 |= (CLKPWR_PCLKSEL_SET(bitpos, DivVal));
  99. }
  100. }
  101. /*********************************************************************//**
  102. * @brief Get current value of each Peripheral Clock Selection
  103. * @param[in] ClkType Peripheral Clock Selection of each type,
  104. * should be one of the following:
  105. * - CLKPWR_PCLKSEL_WDT : WDT
  106. - CLKPWR_PCLKSEL_TIMER0 : Timer 0
  107. - CLKPWR_PCLKSEL_TIMER1 : Timer 1
  108. - CLKPWR_PCLKSEL_UART0 : UART 0
  109. - CLKPWR_PCLKSEL_UART1 : UART 1
  110. - CLKPWR_PCLKSEL_PWM1 : PWM 1
  111. - CLKPWR_PCLKSEL_I2C0 : I2C 0
  112. - CLKPWR_PCLKSEL_SPI : SPI
  113. - CLKPWR_PCLKSEL_SSP1 : SSP 1
  114. - CLKPWR_PCLKSEL_DAC : DAC
  115. - CLKPWR_PCLKSEL_ADC : ADC
  116. - CLKPWR_PCLKSEL_CAN1 : CAN 1
  117. - CLKPWR_PCLKSEL_CAN2 : CAN 2
  118. - CLKPWR_PCLKSEL_ACF : ACF
  119. - CLKPWR_PCLKSEL_QEI : QEI
  120. - CLKPWR_PCLKSEL_PCB : PCB
  121. - CLKPWR_PCLKSEL_I2C1 : I2C 1
  122. - CLKPWR_PCLKSEL_SSP0 : SSP 0
  123. - CLKPWR_PCLKSEL_TIMER2 : Timer 2
  124. - CLKPWR_PCLKSEL_TIMER3 : Timer 3
  125. - CLKPWR_PCLKSEL_UART2 : UART 2
  126. - CLKPWR_PCLKSEL_UART3 : UART 3
  127. - CLKPWR_PCLKSEL_I2C2 : I2C 2
  128. - CLKPWR_PCLKSEL_I2S : I2S
  129. - CLKPWR_PCLKSEL_RIT : RIT
  130. - CLKPWR_PCLKSEL_SYSCON : SYSCON
  131. - CLKPWR_PCLKSEL_MC : MC
  132. * @return Value of Selected Peripheral Clock Selection
  133. **********************************************************************/
  134. uint32_t CLKPWR_GetPCLKSEL (uint32_t ClkType)
  135. {
  136. uint32_t bitpos, retval;
  137. if (ClkType < 32)
  138. {
  139. bitpos = ClkType;
  140. retval = LPC_SC->PCLKSEL0;
  141. }
  142. else
  143. {
  144. bitpos = ClkType - 32;
  145. retval = LPC_SC->PCLKSEL1;
  146. }
  147. retval = CLKPWR_PCLKSEL_GET(bitpos, retval);
  148. return retval;
  149. }
  150. /*********************************************************************//**
  151. * @brief Get current value of each Peripheral Clock
  152. * @param[in] ClkType Peripheral Clock Selection of each type,
  153. * should be one of the following:
  154. * - CLKPWR_PCLKSEL_WDT : WDT
  155. - CLKPWR_PCLKSEL_TIMER0 : Timer 0
  156. - CLKPWR_PCLKSEL_TIMER1 : Timer 1
  157. - CLKPWR_PCLKSEL_UART0 : UART 0
  158. - CLKPWR_PCLKSEL_UART1 : UART 1
  159. - CLKPWR_PCLKSEL_PWM1 : PWM 1
  160. - CLKPWR_PCLKSEL_I2C0 : I2C 0
  161. - CLKPWR_PCLKSEL_SPI : SPI
  162. - CLKPWR_PCLKSEL_SSP1 : SSP 1
  163. - CLKPWR_PCLKSEL_DAC : DAC
  164. - CLKPWR_PCLKSEL_ADC : ADC
  165. - CLKPWR_PCLKSEL_CAN1 : CAN 1
  166. - CLKPWR_PCLKSEL_CAN2 : CAN 2
  167. - CLKPWR_PCLKSEL_ACF : ACF
  168. - CLKPWR_PCLKSEL_QEI : QEI
  169. - CLKPWR_PCLKSEL_PCB : PCB
  170. - CLKPWR_PCLKSEL_I2C1 : I2C 1
  171. - CLKPWR_PCLKSEL_SSP0 : SSP 0
  172. - CLKPWR_PCLKSEL_TIMER2 : Timer 2
  173. - CLKPWR_PCLKSEL_TIMER3 : Timer 3
  174. - CLKPWR_PCLKSEL_UART2 : UART 2
  175. - CLKPWR_PCLKSEL_UART3 : UART 3
  176. - CLKPWR_PCLKSEL_I2C2 : I2C 2
  177. - CLKPWR_PCLKSEL_I2S : I2S
  178. - CLKPWR_PCLKSEL_RIT : RIT
  179. - CLKPWR_PCLKSEL_SYSCON : SYSCON
  180. - CLKPWR_PCLKSEL_MC : MC
  181. * @return Value of Selected Peripheral Clock
  182. **********************************************************************/
  183. uint32_t CLKPWR_GetPCLK (uint32_t ClkType)
  184. {
  185. uint32_t retval, div;
  186. retval = SystemCoreClock;
  187. div = CLKPWR_GetPCLKSEL(ClkType);
  188. switch (div)
  189. {
  190. case 0:
  191. div = 4;
  192. break;
  193. case 1:
  194. div = 1;
  195. break;
  196. case 2:
  197. div = 2;
  198. break;
  199. case 3:
  200. div = 8;
  201. break;
  202. }
  203. retval /= div;
  204. return retval;
  205. }
  206. /*********************************************************************//**
  207. * @brief Configure power supply for each peripheral according to NewState
  208. * @param[in] PPType Type of peripheral used to enable power,
  209. * should be one of the following:
  210. * - CLKPWR_PCONP_PCTIM0 : Timer 0
  211. - CLKPWR_PCONP_PCTIM1 : Timer 1
  212. - CLKPWR_PCONP_PCUART0 : UART 0
  213. - CLKPWR_PCONP_PCUART1 : UART 1
  214. - CLKPWR_PCONP_PCPWM1 : PWM 1
  215. - CLKPWR_PCONP_PCI2C0 : I2C 0
  216. - CLKPWR_PCONP_PCSPI : SPI
  217. - CLKPWR_PCONP_PCRTC : RTC
  218. - CLKPWR_PCONP_PCSSP1 : SSP 1
  219. - CLKPWR_PCONP_PCAD : ADC
  220. - CLKPWR_PCONP_PCAN1 : CAN 1
  221. - CLKPWR_PCONP_PCAN2 : CAN 2
  222. - CLKPWR_PCONP_PCGPIO : GPIO
  223. - CLKPWR_PCONP_PCRIT : RIT
  224. - CLKPWR_PCONP_PCMC : MC
  225. - CLKPWR_PCONP_PCQEI : QEI
  226. - CLKPWR_PCONP_PCI2C1 : I2C 1
  227. - CLKPWR_PCONP_PCSSP0 : SSP 0
  228. - CLKPWR_PCONP_PCTIM2 : Timer 2
  229. - CLKPWR_PCONP_PCTIM3 : Timer 3
  230. - CLKPWR_PCONP_PCUART2 : UART 2
  231. - CLKPWR_PCONP_PCUART3 : UART 3
  232. - CLKPWR_PCONP_PCI2C2 : I2C 2
  233. - CLKPWR_PCONP_PCI2S : I2S
  234. - CLKPWR_PCONP_PCGPDMA : GPDMA
  235. - CLKPWR_PCONP_PCENET : Ethernet
  236. - CLKPWR_PCONP_PCUSB : USB
  237. *
  238. * @param[in] NewState New state of Peripheral Power, should be:
  239. * - ENABLE : Enable power for this peripheral
  240. * - DISABLE : Disable power for this peripheral
  241. *
  242. * @return none
  243. **********************************************************************/
  244. void CLKPWR_ConfigPPWR (uint32_t PPType, FunctionalState NewState)
  245. {
  246. if (NewState == ENABLE)
  247. {
  248. LPC_SC->PCONP |= PPType & CLKPWR_PCONP_BITMASK;
  249. }
  250. else if (NewState == DISABLE)
  251. {
  252. LPC_SC->PCONP &= (~PPType) & CLKPWR_PCONP_BITMASK;
  253. }
  254. }
  255. /*********************************************************************//**
  256. * @brief Enter Sleep mode with co-operated instruction by the Cortex-M3.
  257. * @param[in] None
  258. * @return None
  259. **********************************************************************/
  260. void CLKPWR_Sleep(void)
  261. {
  262. LPC_SC->PCON = 0x00;
  263. /* Sleep Mode*/
  264. __WFI();
  265. }
  266. /*********************************************************************//**
  267. * @brief Enter Deep Sleep mode with co-operated instruction by the Cortex-M3.
  268. * @param[in] None
  269. * @return None
  270. **********************************************************************/
  271. void CLKPWR_DeepSleep(void)
  272. {
  273. /* Deep-Sleep Mode, set SLEEPDEEP bit */
  274. SCB->SCR = 0x4;
  275. LPC_SC->PCON = 0x00;
  276. /* Deep Sleep Mode*/
  277. __WFI();
  278. }
  279. /*********************************************************************//**
  280. * @brief Enter Power Down mode with co-operated instruction by the Cortex-M3.
  281. * @param[in] None
  282. * @return None
  283. **********************************************************************/
  284. void CLKPWR_PowerDown(void)
  285. {
  286. /* Deep-Sleep Mode, set SLEEPDEEP bit */
  287. SCB->SCR = 0x4;
  288. LPC_SC->PCON = 0x01;
  289. /* Power Down Mode*/
  290. __WFI();
  291. }
  292. /*********************************************************************//**
  293. * @brief Enter Deep Power Down mode with co-operated instruction by the Cortex-M3.
  294. * @param[in] None
  295. * @return None
  296. **********************************************************************/
  297. void CLKPWR_DeepPowerDown(void)
  298. {
  299. /* Deep-Sleep Mode, set SLEEPDEEP bit */
  300. SCB->SCR = 0x4;
  301. LPC_SC->PCON = 0x03;
  302. /* Deep Power Down Mode*/
  303. __WFI();
  304. }
  305. /**
  306. * @}
  307. */
  308. /**
  309. * @}
  310. */
  311. /* --------------------------------- End Of File ------------------------------ */