hdo  beta
/home/mrazik/Documents/web/old/hdo/at91lib/peripherals/pmc/pmc.c
00001 /* ----------------------------------------------------------------------------
00002  *         ATMEL Microcontroller Software Support 
00003  * ----------------------------------------------------------------------------
00004  * Copyright (c) 2008, Atmel Corporation
00005  *
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions are met:
00010  *
00011  * - Redistributions of source code must retain the above copyright notice,
00012  * this list of conditions and the disclaimer below.
00013  *
00014  * Atmel's name may not be used to endorse or promote products derived from
00015  * this software without specific prior written permission.
00016  *
00017  * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00019  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
00020  * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00022  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
00023  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00024  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00025  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
00026  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027  * ----------------------------------------------------------------------------
00028  */
00029 
00030 //------------------------------------------------------------------------------
00031 //         Headers
00032 //------------------------------------------------------------------------------
00033 
00034 #include "pmc.h"
00035 #include <board.h>
00036 #include <utility/assert.h>
00037 #include <utility/trace.h>
00038 
00039 #ifdef CP15_PRESENT
00040 #include <cp15/cp15.h>
00041 #endif
00042 
00043 #define MASK_STATUS 0x3FFFFFFC
00044 
00045 //------------------------------------------------------------------------------
00046 //         Global functions
00047 //------------------------------------------------------------------------------
00048 
00049 #if defined(at91sam7l64) || defined(at91sam7l128)
00050 //------------------------------------------------------------------------------
00053 //------------------------------------------------------------------------------
00054 void PMC_SetFastWakeUpInputs(unsigned int inputs)
00055 {
00056     SANITY_CHECK((inputs & ~0xFF) == 0);
00057     AT91C_BASE_PMC->PMC_FSMR = inputs;
00058 }
00059 
00060 #if !defined(__ICCARM__)
00061 __attribute__ ((section (".ramfunc"))) // GCC
00062 #endif
00063 //------------------------------------------------------------------------------
00065 //------------------------------------------------------------------------------
00066 void PMC_DisableMainOscillatorForWaitMode(void)
00067 {
00068     AT91C_BASE_PMC->PMC_MOR = 0x37 << 16;
00069     while ((AT91C_BASE_PMC->PMC_MOR & AT91C_PMC_MAINSELS) != AT91C_PMC_MAINSELS);
00070 }
00071 
00072 #endif
00073 
00074 #if defined(at91sam7l)
00075 //------------------------------------------------------------------------------
00077 //------------------------------------------------------------------------------
00078 void PMC_DisableMainOscillator(void)
00079 {
00080     AT91C_BASE_PMC->PMC_MOR = 0x37 << 16;
00081     while ((AT91C_BASE_PMC->PMC_SR & AT91C_PMC_MAINSELS) == AT91C_PMC_MAINSELS);
00082 }
00083 #endif
00084 
00085 //------------------------------------------------------------------------------
00087 //------------------------------------------------------------------------------
00088 void PMC_DisableProcessorClock(void)
00089 {    
00090     AT91C_BASE_PMC->PMC_SCDR = AT91C_PMC_PCK;   
00091     while ((AT91C_BASE_PMC->PMC_SCSR & AT91C_PMC_PCK) != AT91C_PMC_PCK); 
00092 }
00093 
00094 //------------------------------------------------------------------------------
00099 //------------------------------------------------------------------------------
00100 void PMC_EnablePeripheral(unsigned int id)
00101 {
00102     SANITY_CHECK(id < 32);
00103 
00104     if ((AT91C_BASE_PMC->PMC_PCSR & (1 << id)) == (1 << id)) {
00105 
00106         TRACE_INFO("PMC_EnablePeripheral: clock of peripheral"
00107                    " %u is already enabled\n\r",
00108                    id);
00109     }
00110     else {
00111 
00112         AT91C_BASE_PMC->PMC_PCER = 1 << id;
00113     }
00114 }
00115 
00116 //------------------------------------------------------------------------------
00121 //------------------------------------------------------------------------------
00122 void PMC_DisablePeripheral(unsigned int id)
00123 {
00124     SANITY_CHECK(id < 32);
00125 
00126     if ((AT91C_BASE_PMC->PMC_PCSR & (1 << id)) != (1 << id)) {
00127 
00128         TRACE_INFO("PMC_DisablePeripheral: clock of peripheral"
00129                    " %u is not enabled\n\r",
00130                    id);
00131     }
00132     else {
00133 
00134         AT91C_BASE_PMC->PMC_PCDR = 1 << id;
00135     }
00136 }
00137 
00138 //------------------------------------------------------------------------------
00141 //------------------------------------------------------------------------------
00142 void PMC_EnableAllPeripherals(void)
00143 {
00144     AT91C_BASE_PMC->PMC_PCER = MASK_STATUS;
00145     while( (AT91C_BASE_PMC->PMC_PCSR & MASK_STATUS) != MASK_STATUS);
00146     TRACE_INFO("Enable all periph clocks\n\r"); 
00147 }
00148 
00149 //------------------------------------------------------------------------------
00152 //------------------------------------------------------------------------------
00153 void PMC_DisableAllPeripherals(void)
00154 {
00155     AT91C_BASE_PMC->PMC_PCDR = MASK_STATUS;
00156     while((AT91C_BASE_PMC->PMC_PCSR & MASK_STATUS) != 0);
00157     TRACE_INFO("Disable all periph clocks\n\r");
00158 }
00159 
00160 //-----------------------------------------------------------------------------
00162 //-----------------------------------------------------------------------------
00163 unsigned int PMC_IsAllPeriphEnabled(void)
00164 {
00165     return (AT91C_BASE_PMC->PMC_PCSR == MASK_STATUS);
00166 }
00167 
00168 //-----------------------------------------------------------------------------
00170 //-----------------------------------------------------------------------------
00171 unsigned int PMC_IsPeriphEnabled(unsigned int id)
00172 {
00173     return (AT91C_BASE_PMC->PMC_PCSR & (1 << id));  
00174 }
00175 //------------------------------------------------------------------------------
00177 //------------------------------------------------------------------------------
00178 void PMC_CPUInIdleMode(void)
00179 {
00180     PMC_DisableProcessorClock();
00181 #ifdef CP15_PRESENT
00182     _waitForInterrupt();
00183 #endif
00184 }
00185 
00186