syscon_5410x.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * @brief LPC5410X System & Control driver
  3. *
  4. * @note
  5. * Copyright(C) NXP Semiconductors, 2014
  6. * All rights reserved.
  7. *
  8. * @par
  9. * Software that is described herein is for illustrative purposes only
  10. * which provides customers with programming information regarding the
  11. * LPC products. This software is supplied "AS IS" without any warranties of
  12. * any kind, and NXP Semiconductors and its licenser disclaim any and
  13. * all warranties, express or implied, including all implied warranties of
  14. * merchantability, fitness for a particular purpose and non-infringement of
  15. * intellectual property rights. NXP Semiconductors assumes no responsibility
  16. * or liability for the use of the software, conveys no license or rights under any
  17. * patent, copyright, mask work right, or any other intellectual property rights in
  18. * or to any products. NXP Semiconductors reserves the right to make changes
  19. * in the software without notification. NXP Semiconductors also makes no
  20. * representation or warranty that such application will be suitable for the
  21. * specified use without further testing or modification.
  22. *
  23. * @par
  24. * Permission to use, copy, modify, and distribute this software and its
  25. * documentation is hereby granted, under NXP Semiconductors' and its
  26. * licensor's relevant copyrights in the software, without fee, provided that it
  27. * is used in conjunction with NXP Semiconductors microcontrollers. This
  28. * copyright, permission, and disclaimer notice must appear in all copies of
  29. * this code.
  30. */
  31. #include "chip.h"
  32. /*****************************************************************************
  33. * Private types/enumerations/variables
  34. ****************************************************************************/
  35. /*****************************************************************************
  36. * Public types/enumerations/variables
  37. ****************************************************************************/
  38. /*****************************************************************************
  39. * Private functions
  40. ****************************************************************************/
  41. /*****************************************************************************
  42. * Public functions
  43. ****************************************************************************/
  44. /* Set source for non-maskable interrupt (NMI) */
  45. void Chip_SYSCON_SetNMISource(uint32_t intsrc)
  46. {
  47. uint32_t reg;
  48. reg = LPC_SYSCON->NMISRC;
  49. #if defined(CORE_M4)
  50. reg &= ~SYSCON_NMISRC_M4_ENABLE;
  51. #else
  52. reg &= ~SYSCON_NMISRC_M0_ENABLE;
  53. intsrc = (intsrc << 8);
  54. #endif
  55. /* First write without NMI bit, and then write source */
  56. LPC_SYSCON->NMISRC = reg;
  57. LPC_SYSCON->NMISRC = reg | intsrc;
  58. }
  59. /* Enable interrupt used for NMI source */
  60. void Chip_SYSCON_EnableNMISource(void)
  61. {
  62. #if defined(CORE_M4)
  63. LPC_SYSCON->NMISRC |= SYSCON_NMISRC_M4_ENABLE;
  64. #else
  65. LPC_SYSCON->NMISRC |= SYSCON_NMISRC_M0_ENABLE;
  66. #endif
  67. }
  68. /* Disable interrupt used for NMI source */
  69. void Chip_SYSCON_DisableNMISource(void)
  70. {
  71. #if defined(CORE_M4)
  72. LPC_SYSCON->NMISRC &= ~SYSCON_NMISRC_M4_ENABLE;
  73. #else
  74. LPC_SYSCON->NMISRC &= ~SYSCON_NMISRC_M0_ENABLE;
  75. #endif
  76. }
  77. /* Enable or disable asynchronous APB bridge and subsystem */
  78. void Chip_SYSCON_Enable_ASYNC_Syscon(bool enable)
  79. {
  80. if (enable) {
  81. LPC_SYSCON->ASYNCAPBCTRL = 0x01;
  82. }
  83. else {
  84. LPC_SYSCON->ASYNCAPBCTRL = 0x00;
  85. }
  86. }
  87. /* Resets a peripheral */
  88. void Chip_SYSCON_PeriphReset(CHIP_SYSCON_PERIPH_RESET_T periph)
  89. {
  90. uint32_t pid = (uint32_t) periph;
  91. if (pid >= 128) {
  92. /* Async resets mapped to 128 and above, offset for peripheral bit index */
  93. pid = 1 << (((uint32_t) periph) - 128);
  94. LPC_ASYNC_SYSCON->ASYNCPRESETCTRLSET = pid;
  95. LPC_ASYNC_SYSCON->ASYNCPRESETCTRLCLR = pid;
  96. }
  97. else if (periph >= 32) {
  98. pid = 1 << (((uint32_t) periph) - 32);
  99. LPC_SYSCON->PRESETCTRLSET[1] = pid;
  100. LPC_SYSCON->PRESETCTRLCLR[1] = pid;
  101. }
  102. else {
  103. pid = 1 << ((uint32_t) periph);
  104. LPC_SYSCON->PRESETCTRLSET[0] = pid;
  105. LPC_SYSCON->PRESETCTRLCLR[0] = pid;
  106. }
  107. }
  108. /* Returns the computed value for a frequency measurement cycle */
  109. uint32_t Chip_SYSCON_GetCompFreqMeas(uint32_t refClockRate)
  110. {
  111. uint32_t capval;
  112. uint64_t clkrate = 0;
  113. /* Get raw capture value */
  114. capval = Chip_SYSCON_GetRawFreqMeasCapval();
  115. /* Limit CAPVAL check */
  116. if (capval > 2) {
  117. clkrate = (((uint64_t) capval - 2) * (uint64_t) refClockRate) / 0x4000;
  118. }
  119. return (uint32_t) clkrate;
  120. }
  121. void Chip_SYSCON_PowerUp(uint32_t powerupmask)
  122. {
  123. /* If turning the PLL back on, perform the following sequence to accelerate PLL lock */
  124. if (powerupmask & SYSCON_PDRUNCFG_PD_SYS_PLL) {
  125. volatile uint32_t delayX;
  126. uint32_t maxCCO = (1 << 18) | 0x3fff;
  127. uint32_t curSSCTRL = LPC_SYSCON->SYSPLLSSCTRL[0];
  128. /* If NOT using spread spectrum mode */
  129. if (curSSCTRL & (1 << 18)) {
  130. /* Turn on PLL */
  131. LPC_SYSCON->PDRUNCFGCLR = SYSCON_PDRUNCFG_PD_SYS_PLL;
  132. /* this sequence acclerates the PLL lock time */
  133. LPC_SYSCON->SYSPLLSSCTRL[0] = maxCCO | (1 << 17); /* Set mreq to activate */
  134. LPC_SYSCON->SYSPLLSSCTRL[0] = maxCCO; /* clear mreq to prepare for restoring mreq */
  135. /* Delay for 20 uSec @ 12Mhz*/
  136. for (delayX = 0; delayX < 48; ++delayX) {}
  137. /* set original value back with mreq */
  138. LPC_SYSCON->SYSPLLSSCTRL[0] = curSSCTRL | (1 << 17);
  139. }
  140. }
  141. /* Enable peripheral states by setting low */
  142. LPC_SYSCON->PDRUNCFGCLR = powerupmask;
  143. }