clock_stm32.h

Go to the documentation of this file.
00001 
00038 #ifndef CLOCK_STM32_H
00039 #define CLOCK_STM32_H
00040 
00041 /* RCC registers bit address */
00042 #define RCC_OFFSET                (RCC_BASE - PERIPH_BASE)
00043 
00047 /*\{*/
00048 /* Alias word address of HSION bit */
00049 #define CR_OFFSET                 (RCC_OFFSET + 0x00)
00050 #define HSION_BITNUMBER           0x00
00051 #define CR_HSION_BB               ((reg32_t *)(PERIPH_BB_BASE + (CR_OFFSET * 32) + (HSION_BITNUMBER * 4)))
00052 
00053 /* Alias word address of PLLON bit */
00054 #define PLLON_BITNUMBER           0x18
00055 #define CR_PLLON_BB               ((reg32_t *)(PERIPH_BB_BASE + (CR_OFFSET * 32) + (PLLON_BITNUMBER * 4)))
00056 
00057 /* Alias word address of CSSON bit */
00058 #define CSSON_BITNUMBER           0x13
00059 #define CR_CSSON_BB               ((reg32_t *)(PERIPH_BB_BASE + (CR_OFFSET * 32) + (CSSON_BITNUMBER * 4)))
00060 /*\}*/
00061 
00065 /*\{*/
00066 /* Alias word address of USBPRE bit */
00067 #define CFGR_OFFSET               (RCC_OFFSET + 0x04)
00068 #define USBPRE_BITNUMBER          0x16
00069 #define CFGR_USBPRE_BB            ((reg32_t *)(PERIPH_BB_BASE + (CFGR_OFFSET * 32) + (USBPRE_BITNUMBER * 4)))
00070 /*\}*/
00071 
00075 /*\{*/
00076 /* Alias word address of RTCEN bit */
00077 #define BDCR_OFFSET               (RCC_OFFSET + 0x20)
00078 #define RTCEN_BITNUMBER           0x0F
00079 #define BDCR_RTCEN_BB             ((reg32_t *)(PERIPH_BB_BASE + (BDCR_OFFSET * 32) + (RTCEN_BITNUMBER * 4)))
00080 
00081 /* Alias word address of BDRST bit */
00082 #define BDRST_BITNUMBER           0x10
00083 #define BDCR_BDRST_BB             ((reg32_t *)(PERIPH_BB_BASE + (BDCR_OFFSET * 32) + (BDRST_BITNUMBER * 4)))
00084 /*\}*/
00085 
00089 /*\{*/
00090 /* Alias word address of LSION bit */
00091 #define CSR_OFFSET                (RCC_OFFSET + 0x24)
00092 #define LSION_BITNUMBER           0x00
00093 #define CSR_LSION_BB              ((reg32_t *)(PERIPH_BB_BASE + (CSR_OFFSET * 32) + (LSION_BITNUMBER * 4)))
00094 /*\}*/
00095 
00099 /*\{*/
00100 /* CR register bit mask */
00101 #define CR_HSEBYP_RESET           (0xFFFBFFFF)
00102 #define CR_HSEBYP_SET             (0x00040000)
00103 #define CR_HSEON_RESET            (0xFFFEFFFF)
00104 #define CR_HSEON_SET              (0x00010000)
00105 #define CR_HSITRIM_MASK           (0xFFFFFF07)
00106 
00107 /* CFGR register bit mask */
00108 #define CFGR_PLL_MASK             (0xFFC0FFFF)
00109 #define CFGR_PLLMull_MASK         (0x003C0000)
00110 #define CFGR_PLLSRC_MASK          (0x00010000)
00111 #define CFGR_PLLXTPRE_MASK        (0x00020000)
00112 #define CFGR_SWS_MASK             (0x0000000C)
00113 #define CFGR_SW_MASK              (0xFFFFFFFC)
00114 #define CFGR_HPRE_RESET_MASK      (0xFFFFFF0F)
00115 #define CFGR_HPRE_SET_MASK        (0x000000F0)
00116 #define CFGR_PPRE1_RESET_MASK     (0xFFFFF8FF)
00117 #define CFGR_PPRE1_SET_MASK       (0x00000700)
00118 #define CFGR_PPRE2_RESET_MASK     (0xFFFFC7FF)
00119 #define CFGR_PPRE2_SET_MASK       (0x00003800)
00120 #define CFGR_ADCPRE_RESET_MASK    (0xFFFF3FFF)
00121 #define CFGR_ADCPRE_SET_MASK      (0x0000C000)
00122 
00123 /* CSR register bit mask */
00124 #define CSR_RVMF_SET              (0x01000000)
00125 
00126 /* RCC Flag MASK */
00127 #define FLAG_MASK                 (0x1F)
00128 
00129 /* Typical VALUE of the HSI in Hz */
00130 #define HSI_VALUE                 (8000000)
00131 
00132 /* BDCR register base address */
00133 #define BDCR_BASE                 (PERIPH_BASE + BDCR_OFFSET)
00134 
00135 /* RCC Flag */
00136 #define RCC_FLAG_HSIRDY                  (0x20)
00137 #define RCC_FLAG_HSERDY                  (0x31)
00138 #define RCC_FLAG_PLLRDY                  (0x39)
00139 #define RCC_FLAG_LSERDY                  (0x41)
00140 #define RCC_FLAG_LSIRDY                  (0x61)
00141 #define RCC_FLAG_PINRST                  (0x7A)
00142 #define RCC_FLAG_PORRST                  (0x7B)
00143 #define RCC_FLAG_SFTRST                  (0x7C)
00144 #define RCC_FLAG_IWDGRST                 (0x7D)
00145 #define RCC_FLAG_WWDGRST                 (0x7E)
00146 #define RCC_FLAG_LPWRRST                 (0x7F)
00147 
00148 /* System clock source */
00149 #define RCC_SYSCLK_HSI             (0x00000000)
00150 #define RCC_SYSCLK_HSE             (0x00000001)
00151 #define RCC_SYSCLK_PLLCLK          (0x00000002)
00152 
00153 /* PLL entry clock source */
00154 #define RCC_PLL_HSI_DIV2           (0x00000000)
00155 #define RCC_PLL_HSE_DIV1           (0x00010000)
00156 #define RCC_PLL_HSE_DIV2           (0x00030000)
00157 
00158 /* PLL multiplication factor */
00159 #define RCC_PLLMUL_2               (0x00000000)
00160 #define RCC_PLLMUL_3               (0x00040000)
00161 #define RCC_PLLMUL_4               (0x00080000)
00162 #define RCC_PLLMUL_5               (0x000C0000)
00163 #define RCC_PLLMUL_6               (0x00100000)
00164 #define RCC_PLLMUL_7               (0x00140000)
00165 #define RCC_PLLMUL_8               (0x00180000)
00166 #define RCC_PLLMUL_9               (0x001C0000)
00167 #define RCC_PLLMUL_10              (0x00200000)
00168 #define RCC_PLLMUL_11              (0x00240000)
00169 #define RCC_PLLMUL_12              (0x00280000)
00170 #define RCC_PLLMUL_13              (0x002C0000)
00171 #define RCC_PLLMUL_14              (0x00300000)
00172 #define RCC_PLLMUL_15              (0x00340000)
00173 #define RCC_PLLMUL_16              (0x00380000)
00174 
00175 /* APB1/APB2 clock source */
00176 #define RCC_HCLK_DIV1              (0x00000000)
00177 #define RCC_HCLK_DIV2              (0x00000400)
00178 #define RCC_HCLK_DIV4              (0x00000500)
00179 #define RCC_HCLK_DIV8              (0x00000600)
00180 #define RCC_HCLK_DIV16             (0x00000700)
00181 
00182 /* USB clock source */
00183 #define RCC_USBCLK_PLLCLK_1DIV5    (0x00)
00184 #define RCC_USBCLK_PLLCLK_DIV1     (0x01)
00185 
00186 /* ADC clock source */
00187 #define RCC_PCLK2_DIV2             (0x00000000)
00188 #define RCC_PCLK2_DIV4             (0x00004000)
00189 #define RCC_PCLK2_DIV6             (0x00008000)
00190 #define RCC_PCLK2_DIV8             (0x0000C000)
00191 
00192 /* AHB clock source */
00193 #define RCC_SYSCLK_DIV1            (0x00000000)
00194 #define RCC_SYSCLK_DIV2            (0x00000080)
00195 #define RCC_SYSCLK_DIV4            (0x00000090)
00196 #define RCC_SYSCLK_DIV8            (0x000000A0)
00197 #define RCC_SYSCLK_DIV16           (0x000000B0)
00198 #define RCC_SYSCLK_DIV64           (0x000000C0)
00199 #define RCC_SYSCLK_DIV128          (0x000000D0)
00200 #define RCC_SYSCLK_DIV256          (0x000000E0)
00201 #define RCC_SYSCLK_DIV512          (0x000000F0)
00202 /*\}*/
00203 
00207 /*\{*/
00208 #define RCC_APB1_TIM2              (0x00000001)
00209 #define RCC_APB1_TIM3              (0x00000002)
00210 #define RCC_APB1_TIM4              (0x00000004)
00211 #define RCC_APB1_WWDG              (0x00000800)
00212 #define RCC_APB1_SPI2              (0x00004000)
00213 #define RCC_APB1_USART2            (0x00020000)
00214 #define RCC_APB1_USART3            (0x00040000)
00215 #define RCC_APB1_I2C1              (0x00200000)
00216 #define RCC_APB1_I2C2              (0x00400000)
00217 #define RCC_APB1_USB               (0x00800000)
00218 #define RCC_APB1_CAN               (0x02000000)
00219 #define RCC_APB1_BKP               (0x08000000)
00220 #define RCC_APB1_PWR               (0x10000000)
00221 #define RCC_APB1_ALL               (0x1AE64807)
00222 /*\}*/
00223 
00227 /*\{*/
00228 #define RCC_APB2_AFIO              (0x00000001)
00229 #define RCC_APB2_GPIOA             (0x00000004)
00230 #define RCC_APB2_GPIOB             (0x00000008)
00231 #define RCC_APB2_GPIOC             (0x00000010)
00232 #define RCC_APB2_GPIOD             (0x00000020)
00233 #define RCC_APB2_GPIOE             (0x00000040)
00234 #define RCC_APB2_ADC1              (0x00000200)
00235 #define RCC_APB2_ADC2              (0x00000400)
00236 #define RCC_APB2_TIM1              (0x00000800)
00237 #define RCC_APB2_SPI1              (0x00001000)
00238 #define RCC_APB2_USART1            (0x00004000)
00239 #define RCC_APB2_ALL               (0x00005E7D)
00240 /*\}*/
00241 
00242 /* Crystal frequency of the main oscillator (8MHz) */
00243 #define PLL_VCO                    8000000
00244 
00245 /* Reset and Clock Controller registers */
00246 struct RCC
00247 {
00248     reg32_t CR;
00249     reg32_t CFGR;
00250     reg32_t CIR;
00251     reg32_t APB2RSTR;
00252     reg32_t APB1RSTR;
00253     reg32_t AHBENR;
00254     reg32_t APB2ENR;
00255     reg32_t APB1ENR;
00256     reg32_t BDCR;
00257     reg32_t CSR;
00258 };
00259 
00260 /* RCC registers base */
00261 extern struct RCC *RCC;
00262 
00263 void clock_init(void);
00264 
00265 #endif /* CLOCK_STM32_h */