Kévin Redon | 93717e4 | 2018-07-08 13:26:15 +0200 | [diff] [blame] | 1 | /* ---------------------------------------------------------------------------- |
| 2 | * ATMEL Microcontroller Software Support |
| 3 | * ---------------------------------------------------------------------------- |
| 4 | * Copyright (c) 2009, 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 | /*---------------------------------------------------------------------------- |
| 31 | * Headers |
| 32 | *----------------------------------------------------------------------------*/ |
| 33 | |
| 34 | #include "chip.h" |
| 35 | #include "trace.h" |
| 36 | |
| 37 | #include <assert.h> |
| 38 | |
| 39 | /*---------------------------------------------------------------------------- |
| 40 | * Local definitions |
| 41 | *----------------------------------------------------------------------------*/ |
| 42 | |
| 43 | #define MASK_STATUS0 0xFFFFFFFC |
| 44 | #define MASK_STATUS1 0xFFFFFFFF |
| 45 | |
| 46 | /*---------------------------------------------------------------------------- |
| 47 | * Exported functions |
| 48 | *----------------------------------------------------------------------------*/ |
| 49 | |
| 50 | /** |
| 51 | * \brief Enables the clock of a peripheral. The peripheral ID is used |
| 52 | * to identify which peripheral is targetted. |
| 53 | * |
| 54 | * \note The ID must NOT be shifted (i.e. 1 << ID_xxx). |
| 55 | * |
| 56 | * \param id Peripheral ID (ID_xxx). |
| 57 | */ |
| 58 | extern void PMC_EnablePeripheral( uint32_t dwId ) |
| 59 | { |
| 60 | assert( dwId < 35 ) ; |
| 61 | |
| 62 | if ( dwId < 32 ) |
| 63 | { |
| 64 | if ( (PMC->PMC_PCSR0 & ((uint32_t)1 << dwId)) == ((uint32_t)1 << dwId) ) |
| 65 | { |
| 66 | TRACE_DEBUG( "PMC_EnablePeripheral: clock of peripheral" " %" PRIu32 " is already enabled\n\r", dwId ) ; |
| 67 | } |
| 68 | else |
| 69 | { |
| 70 | PMC->PMC_PCER0 = 1 << dwId ; |
| 71 | } |
| 72 | } |
| 73 | else |
| 74 | { |
| 75 | dwId -= 32; |
| 76 | if ((PMC->PMC_PCSR1 & ((uint32_t)1 << dwId)) == ((uint32_t)1 << dwId)) |
| 77 | { |
| 78 | TRACE_DEBUG( "PMC_EnablePeripheral: clock of peripheral" " %" PRIu32 " is already enabled\n\r", dwId + 32 ) ; |
| 79 | } |
| 80 | else |
| 81 | { |
| 82 | PMC->PMC_PCER1 = 1 << dwId ; |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * \brief Disables the clock of a peripheral. The peripheral ID is used |
| 89 | * to identify which peripheral is targetted. |
| 90 | * |
| 91 | * \note The ID must NOT be shifted (i.e. 1 << ID_xxx). |
| 92 | * |
| 93 | * \param id Peripheral ID (ID_xxx). |
| 94 | */ |
| 95 | extern void PMC_DisablePeripheral( uint32_t dwId ) |
| 96 | { |
| 97 | assert( dwId < 35 ) ; |
| 98 | |
| 99 | if ( dwId < 32 ) |
| 100 | { |
| 101 | if ( (PMC->PMC_PCSR0 & ((uint32_t)1 << dwId)) != ((uint32_t)1 << dwId) ) |
| 102 | { |
| 103 | TRACE_DEBUG("PMC_DisablePeripheral: clock of peripheral" " %" PRIu32 " is not enabled\n\r", dwId ) ; |
| 104 | } |
| 105 | else |
| 106 | { |
| 107 | PMC->PMC_PCDR0 = 1 << dwId ; |
| 108 | } |
| 109 | } |
| 110 | else |
| 111 | { |
| 112 | dwId -= 32 ; |
| 113 | if ( (PMC->PMC_PCSR1 & ((uint32_t)1 << dwId)) != ((uint32_t)1 << dwId) ) |
| 114 | { |
| 115 | TRACE_DEBUG( "PMC_DisablePeripheral: clock of peripheral" " %" PRIu32 " is not enabled\n\r", dwId + 32 ) ; |
| 116 | } |
| 117 | else |
| 118 | { |
| 119 | PMC->PMC_PCDR1 = 1 << dwId ; |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * \brief Enable all the periph clock via PMC. |
| 126 | */ |
| 127 | extern void PMC_EnableAllPeripherals( void ) |
| 128 | { |
| 129 | PMC->PMC_PCER0 = MASK_STATUS0 ; |
| 130 | while ( (PMC->PMC_PCSR0 & MASK_STATUS0) != MASK_STATUS0 ) ; |
| 131 | |
| 132 | PMC->PMC_PCER1 = MASK_STATUS1 ; |
| 133 | while ( (PMC->PMC_PCSR1 & MASK_STATUS1) != MASK_STATUS1 ) ; |
| 134 | |
| 135 | TRACE_DEBUG( "Enable all periph clocks\n\r" ) ; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * \brief Disable all the periph clock via PMC. |
| 140 | */ |
| 141 | extern void PMC_DisableAllPeripherals( void ) |
| 142 | { |
| 143 | PMC->PMC_PCDR0 = MASK_STATUS0 ; |
| 144 | while ( (PMC->PMC_PCSR0 & MASK_STATUS0) != 0 ) ; |
| 145 | |
| 146 | PMC->PMC_PCDR1 = MASK_STATUS1 ; |
| 147 | while ( (PMC->PMC_PCSR1 & MASK_STATUS1) != 0 ) ; |
| 148 | |
| 149 | TRACE_DEBUG( "Disable all periph clocks\n\r" ) ; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * \brief Get Periph Status for the given peripheral ID. |
| 154 | * |
| 155 | * \param id Peripheral ID (ID_xxx). |
| 156 | */ |
| 157 | extern uint32_t PMC_IsPeriphEnabled( uint32_t dwId ) |
| 158 | { |
| 159 | assert( dwId < 35 ) ; |
| 160 | |
| 161 | if ( dwId < 32 ) |
| 162 | { |
| 163 | return ( PMC->PMC_PCSR0 & (1 << dwId) ) ; |
| 164 | } |
| 165 | else { |
| 166 | return ( PMC->PMC_PCSR1 & (1 << (dwId - 32)) ) ; |
| 167 | } |
| 168 | } |