pmc.c
上传用户:jnhtjd
上传日期:2022-07-16
资源大小:403k
文件大小:7k
源码类别:

微处理器开发

开发平台:

C/C++

  1. /* ----------------------------------------------------------------------------
  2.  *         ATMEL Microcontroller Software Support 
  3.  * ----------------------------------------------------------------------------
  4.  * Copyright (c) 2008, Atmel Corporation
  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 are met:
  10.  *
  11.  * - Redistributions of source code must retain the above copyright notice,
  12.  * this list of conditions and the disclaimer below.
  13.  *
  14.  * Atmel's name may not be used to endorse or promote products derived from
  15.  * this software without specific prior written permission.
  16.  *
  17.  * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
  18.  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  19.  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  20.  * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
  21.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  22.  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  23.  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  24.  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  25.  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  26.  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27.  * ----------------------------------------------------------------------------
  28.  */
  29. //------------------------------------------------------------------------------
  30. //         Headers
  31. //------------------------------------------------------------------------------
  32. #include "pmc.h"
  33. #include <board.h>
  34. #include <utility/assert.h>
  35. #include <utility/trace.h>
  36. #ifdef CP15_PRESENT
  37. #include <cp15/cp15.h>
  38. #endif
  39. #define MASK_STATUS 0x3FFFFFFC
  40. //------------------------------------------------------------------------------
  41. //         Global functions
  42. //------------------------------------------------------------------------------
  43. #if defined(at91sam7l64) || defined(at91sam7l128)
  44. //------------------------------------------------------------------------------
  45. /// Sets the fast wake-up inputs that can get the device out of Wait mode.
  46. /// param inputs  Fast wake-up inputs to enable.
  47. //------------------------------------------------------------------------------
  48. void PMC_SetFastWakeUpInputs(unsigned int inputs)
  49. {
  50.     SANITY_CHECK((inputs & ~0xFF) == 0);
  51.     AT91C_BASE_PMC->PMC_FSMR = inputs;
  52. }
  53. #if !defined(__ICCARM__)
  54. __attribute__ ((section (".ramfunc"))) // GCC
  55. #endif
  56. //------------------------------------------------------------------------------
  57. /// Disables the main oscillator, making the device enter Wait mode.
  58. //------------------------------------------------------------------------------
  59. void PMC_DisableMainOscillatorForWaitMode(void)
  60. {
  61.     AT91C_BASE_PMC->PMC_MOR = 0x37 << 16;
  62.     while ((AT91C_BASE_PMC->PMC_MOR & AT91C_PMC_MAINSELS) != AT91C_PMC_MAINSELS);
  63. }
  64. #endif
  65. #if defined(at91sam7l)
  66. //------------------------------------------------------------------------------
  67. /// Disables the main oscillator when NOT running on it.
  68. //------------------------------------------------------------------------------
  69. void PMC_DisableMainOscillator(void)
  70. {
  71.     AT91C_BASE_PMC->PMC_MOR = 0x37 << 16;
  72.     while ((AT91C_BASE_PMC->PMC_SR & AT91C_PMC_MAINSELS) == AT91C_PMC_MAINSELS);
  73. }
  74. #endif
  75. //------------------------------------------------------------------------------
  76. /// Disables the processor clock
  77. //------------------------------------------------------------------------------
  78. void PMC_DisableProcessorClock(void)
  79. {    
  80.     AT91C_BASE_PMC->PMC_SCDR = AT91C_PMC_PCK;   
  81.     while ((AT91C_BASE_PMC->PMC_SCSR & AT91C_PMC_PCK) != AT91C_PMC_PCK); 
  82. }
  83. //------------------------------------------------------------------------------
  84. /// Enables the clock of a peripheral. The peripheral ID (AT91C_ID_xxx) is used
  85. /// to identify which peripheral is targetted.
  86. /// Note that the ID must NOT be shifted (i.e. 1 << AT91C_ID_xxx).
  87. /// param id  Peripheral ID (AT91C_ID_xxx).
  88. //------------------------------------------------------------------------------
  89. void PMC_EnablePeripheral(unsigned int id)
  90. {
  91.     SANITY_CHECK(id < 32);
  92.     if ((AT91C_BASE_PMC->PMC_PCSR & (1 << id)) == (1 << id)) {
  93.         TRACE_INFO("PMC_EnablePeripheral: clock of peripheral"
  94.                    " %u is already enablednr",
  95.                    id);
  96.     }
  97.     else {
  98.         AT91C_BASE_PMC->PMC_PCER = 1 << id;
  99.     }
  100. }
  101. //------------------------------------------------------------------------------
  102. /// Disables the clock of a peripheral. The peripheral ID (AT91C_ID_xxx) is used
  103. /// to identify which peripheral is targetted.
  104. /// Note that the ID must NOT be shifted (i.e. 1 << AT91C_ID_xxx).
  105. /// param id  Peripheral ID (AT91C_ID_xxx).
  106. //------------------------------------------------------------------------------
  107. void PMC_DisablePeripheral(unsigned int id)
  108. {
  109.     SANITY_CHECK(id < 32);
  110.     if ((AT91C_BASE_PMC->PMC_PCSR & (1 << id)) != (1 << id)) {
  111.         TRACE_INFO("PMC_DisablePeripheral: clock of peripheral"
  112.                    " %u is not enablednr",
  113.                    id);
  114.     }
  115.     else {
  116.         AT91C_BASE_PMC->PMC_PCDR = 1 << id;
  117.     }
  118. }
  119. //------------------------------------------------------------------------------
  120. /// Enable all the periph clock via PMC
  121. /// (Becareful of the last 2 bits, it is not periph clock)
  122. //------------------------------------------------------------------------------
  123. void PMC_EnableAllPeripherals(void)
  124. {
  125.     AT91C_BASE_PMC->PMC_PCER = MASK_STATUS;
  126.     while( (AT91C_BASE_PMC->PMC_PCSR & MASK_STATUS) != MASK_STATUS);
  127.     TRACE_INFO("Enable all periph clocksnr"); 
  128. }
  129. //------------------------------------------------------------------------------
  130. /// Disable all the periph clock via PMC
  131. /// (Becareful of the last 2 bits, it is not periph clock)
  132. //------------------------------------------------------------------------------
  133. void PMC_DisableAllPeripherals(void)
  134. {
  135.     AT91C_BASE_PMC->PMC_PCDR = MASK_STATUS;
  136.     while((AT91C_BASE_PMC->PMC_PCSR & MASK_STATUS) != 0);
  137.     TRACE_INFO("Disable all periph clocksnr");
  138. }
  139. //-----------------------------------------------------------------------------
  140. /// Get Periph Status
  141. //-----------------------------------------------------------------------------
  142. unsigned int PMC_IsAllPeriphEnabled(void)
  143. {
  144.     return (AT91C_BASE_PMC->PMC_PCSR == MASK_STATUS);
  145. }
  146. //-----------------------------------------------------------------------------
  147. /// Get Periph Status
  148. //-----------------------------------------------------------------------------
  149. unsigned int PMC_IsPeriphEnabled(unsigned int id)
  150. {
  151.     return (AT91C_BASE_PMC->PMC_PCSR & (1 << id));  
  152. }
  153. //------------------------------------------------------------------------------
  154. /// Put the CPU in Idle Mode for lower consumption
  155. //------------------------------------------------------------------------------
  156. void PMC_CPUInIdleMode(void)
  157. {
  158.     PMC_DisableProcessorClock();
  159. #ifdef CP15_PRESENT
  160.     _waitForInterrupt();
  161. #endif
  162. }