00001
00038 #ifndef CHIP_SYSCLK_H_INCLUDED
00039 #define CHIP_SYSCLK_H_INCLUDED
00040
00041 #include <compiler.h>
00042 #include <clk/pll.h>
00043 #include <regs/xmega_pr.h>
00044 #include <regs/xmega_clk.h>
00045 #include <stdint.h>
00046 #include <stdbool.h>
00047
00049
00050 #define SYSCLK_SRC_RC2MHZ 0x00
00051 #define SYSCLK_SRC_RC32MHZ 0x01
00052 #define SYSCLK_SRC_RC32KHZ 0x02
00053 #define SYSCLK_SRC_XOSC 0x03
00054 #define SYSCLK_SRC_PLL 0x04
00055
00056
00058
00059 #define SYSCLK_PORT_GEN XMEGA_PR_PRGEN //!< No particular port
00060 #define SYSCLK_PORT_A XMEGA_PR_PRPA //!< Devices on PORTA
00061 #define SYSCLK_PORT_B XMEGA_PR_PRPB //!< Devices on PORTB
00062 #define SYSCLK_PORT_C XMEGA_PR_PRPC //!< Devices on PORTC
00063 #define SYSCLK_PORT_D XMEGA_PR_PRPD //!< Devices on PORTD
00064 #define SYSCLK_PORT_E XMEGA_PR_PRPE //!< Devices on PORTE
00065 #define SYSCLK_PORT_F XMEGA_PR_PRPF //!< Devices on PORTF
00066
00067
00069
00070 #define SYSCLK_DMA (1U << PR_DMA_BIT) //!< DMA Controller
00071 #define SYSCLK_EVSYS (1U << PR_EVSYS_BIT) //!< Event System
00072 #define SYSCLK_RTC (1U << PR_RTC_BIT) //!< Real-Time Counter
00073 #define SYSCLK_EBI (1U << PR_EBI_BIT) //!< Ext Bus Interface
00074 #define SYSCLK_AES (1U << PR_AES_BIT) //!< AES Module
00075 #ifdef CONFIG_XMEGA_USB
00076 # define SYSCLK_USB (1U << PR_USB_BIT) //!< USB Module
00077 #endif
00078
00079
00081
00082 #define SYSCLK_AC (1U << PR_AC_BIT) //!< Analog Comparator
00083 #define SYSCLK_ADC (1U << PR_ADC_BIT) //!< A/D Converter
00084 #define SYSCLK_DAC (1U << PR_DAC_BIT) //!< D/A Converter
00085
00086
00088
00089 #define SYSCLK_TC0 (1U << PR_TC0_BIT) //!< Timer/Counter 0
00090 #define SYSCLK_TC1 (1U << PR_TC1_BIT) //!< Timer/Counter 1
00091 #define SYSCLK_HIRES (1U << PR_HIRES_BIT) //!< Hi-Res Extension
00092 #define SYSCLK_SPI (1U << PR_SPI_BIT) //!< SPI controller
00093 #define SYSCLK_USART0 (1U << PR_USART0_BIT) //!< USART 0
00094 #define SYSCLK_USART1 (1U << PR_USART1_BIT) //!< USART 1
00095 #define SYSCLK_TWI (1U << PR_TWI_BIT) //!< TWI controller
00096
00097
00103 #ifndef __ASSEMBLY__
00104
00105 #include <assert.h>
00106 #include <chip/memory-map.h>
00107 #include <io.h>
00108
00120
00131 static inline uint32_t sysclk_get_main_hz(void)
00132 {
00133 switch (CONFIG_SYSCLK_SOURCE) {
00134 case SYSCLK_SRC_RC2MHZ:
00135 return 2000000UL;
00136 case SYSCLK_SRC_RC32MHZ:
00137 return 32000000UL;
00138 case SYSCLK_SRC_RC32KHZ:
00139 return 32768UL;
00140 #ifdef BOARD_XOSC_HZ
00141 case SYSCLK_SRC_XOSC:
00142 return BOARD_XOSC_HZ;
00143 #endif
00144 #ifdef CONFIG_PLL0_SOURCE
00145 case SYSCLK_SRC_PLL:
00146 return pll_get_default_rate(0);
00147 #endif
00148 default:
00149 unhandled_case(CONFIG_SYSCLK_SOURCE);
00150 return 0;
00151 }
00152 }
00153
00159 static inline uint32_t sysclk_get_per4_hz(void)
00160 {
00161 uint8_t shift = 0;
00162
00163 if (CONFIG_SYSCLK_PSADIV & 0x01)
00164 shift = (CONFIG_SYSCLK_PSADIV >> 1) + 1;
00165
00166 return sysclk_get_main_hz() >> shift;
00167 }
00168
00174 static inline uint32_t sysclk_get_per2_hz(void)
00175 {
00176 switch (CONFIG_SYSCLK_PSBCDIV) {
00177 case XMEGA_CLK_PSBCDIV_1_1:
00178 case XMEGA_CLK_PSBCDIV_1_2:
00179 return sysclk_get_per4_hz();
00180 case XMEGA_CLK_PSBCDIV_4_1:
00181 return sysclk_get_per4_hz() / 4;
00182 case XMEGA_CLK_PSBCDIV_2_2:
00183 return sysclk_get_per4_hz() / 2;
00184 default:
00185 unhandled_case(CONFIG_SYSCLK_PSBCDIV);
00186 return 0;
00187 }
00188 }
00189
00195 static inline uint32_t sysclk_get_per_hz(void)
00196 {
00197 if (CONFIG_SYSCLK_PSBCDIV & 0x01)
00198 return sysclk_get_per2_hz() / 2;
00199 else
00200 return sysclk_get_per2_hz();
00201 }
00202
00206 static inline uint32_t sysclk_get_cpu_hz(void)
00207 {
00208 return sysclk_get_per_hz();
00209 }
00210
00212
00214
00215
00216 extern void sysclk_enable_module(uint8_t port, uint8_t id);
00217 extern void sysclk_disable_module(uint8_t port, uint8_t id);
00218
00220
00232 __always_inline static bool sysclk_module_is_enabled(uint8_t port, uint8_t id)
00233 {
00234 uint8_t mask = mmio_read8((void *)(PR_BASE + port));
00235 return (mask & id) == 0;
00236 }
00237
00239
00240
00241 extern void sysclk_init(void);
00242
00244
00245 #endif
00246
00248
00249 #endif