e91753d62a06498a7c9c2a7c47899ce7a72c60bc
[bertos.git] / bertos / cpu / cortex-m3 / drv / clock_stm32.h
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2010 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief Low-level clocking driver for Cortex-M3 STM32.
34  *
35  * \author Andrea Righi <arighi@develer.com>
36  */
37
38 #ifndef CLOCK_STM32_H
39 #define CLOCK_STM32_H
40
41
42 #include <cfg/compiler.h>
43
44 /* RCC registers bit address */
45 #define RCC_OFFSET                (RCC_BASE - PERIPH_BASE)
46
47 /**
48  * CR Register
49  */
50 /*\{*/
51 /* Alias word address of HSION bit */
52 #define CR_OFFSET                 (RCC_OFFSET + 0x00)
53 #define HSION_BITNUMBER           0x00
54 #define CR_HSION_BB               ((reg32_t *)(PERIPH_BB_BASE + (CR_OFFSET * 32) + (HSION_BITNUMBER * 4)))
55
56 /* Alias word address of PLLON bit */
57 #define PLLON_BITNUMBER           0x18
58 #define CR_PLLON_BB               ((reg32_t *)(PERIPH_BB_BASE + (CR_OFFSET * 32) + (PLLON_BITNUMBER * 4)))
59
60 /* Alias word address of CSSON bit */
61 #define CSSON_BITNUMBER           0x13
62 #define CR_CSSON_BB               ((reg32_t *)(PERIPH_BB_BASE + (CR_OFFSET * 32) + (CSSON_BITNUMBER * 4)))
63 /*\}*/
64
65 /**
66  * CFGR Register
67  */
68 /*\{*/
69 /* Alias word address of USBPRE bit */
70 #define CFGR_OFFSET               (RCC_OFFSET + 0x04)
71 #define USBPRE_BITNUMBER          0x16
72 #define CFGR_USBPRE_BB            ((reg32_t *)(PERIPH_BB_BASE + (CFGR_OFFSET * 32) + (USBPRE_BITNUMBER * 4)))
73 /*\}*/
74
75 /**
76  * BDCR Register
77  */
78 /*\{*/
79 /* Alias word address of RTCEN bit */
80 #define BDCR_OFFSET               (RCC_OFFSET + 0x20)
81 #define RTCEN_BITNUMBER           0x0F
82 #define BDCR_RTCEN_BB             ((reg32_t *)(PERIPH_BB_BASE + (BDCR_OFFSET * 32) + (RTCEN_BITNUMBER * 4)))
83
84 /* Alias word address of BDRST bit */
85 #define BDRST_BITNUMBER           0x10
86 #define BDCR_BDRST_BB             ((reg32_t *)(PERIPH_BB_BASE + (BDCR_OFFSET * 32) + (BDRST_BITNUMBER * 4)))
87 /*\}*/
88
89 /**
90  * CSR Register
91  */
92 /*\{*/
93 /* Alias word address of LSION bit */
94 #define CSR_OFFSET                (RCC_OFFSET + 0x24)
95 #define LSION_BITNUMBER           0x00
96 #define CSR_LSION_BB              ((reg32_t *)(PERIPH_BB_BASE + (CSR_OFFSET * 32) + (LSION_BITNUMBER * 4)))
97 /*\}*/
98
99 /**
100  * RCC registers bit mask
101  */
102 /*\{*/
103 /* CR register bit mask */
104 #define CR_HSEBYP_RESET           (0xFFFBFFFF)
105 #define CR_HSEBYP_SET             (0x00040000)
106 #define CR_HSEON_RESET            (0xFFFEFFFF)
107 #define CR_HSEON_SET              (0x00010000)
108 #define CR_HSITRIM_MASK           (0xFFFFFF07)
109
110 /* CFGR register bit mask */
111 #define CFGR_PLL_MASK             (0xFFC0FFFF)
112 #define CFGR_PLLMull_MASK         (0x003C0000)
113 #define CFGR_PLLSRC_MASK          (0x00010000)
114 #define CFGR_PLLXTPRE_MASK        (0x00020000)
115 #define CFGR_SWS_MASK             (0x0000000C)
116 #define CFGR_SW_MASK              (0xFFFFFFFC)
117 #define CFGR_HPRE_RESET_MASK      (0xFFFFFF0F)
118 #define CFGR_HPRE_SET_MASK        (0x000000F0)
119 #define CFGR_PPRE1_RESET_MASK     (0xFFFFF8FF)
120 #define CFGR_PPRE1_SET_MASK       (0x00000700)
121 #define CFGR_PPRE2_RESET_MASK     (0xFFFFC7FF)
122 #define CFGR_PPRE2_SET_MASK       (0x00003800)
123 #define CFGR_ADCPRE_RESET_MASK    (0xFFFF3FFF)
124 #define CFGR_ADCPRE_SET_MASK      (0x0000C000)
125
126 /* CSR register bit mask */
127 #define CSR_RVMF_SET              (0x01000000)
128
129 /* RCC Flag MASK */
130 #define FLAG_MASK                 (0x1F)
131
132 /* Typical VALUE of the HSI in Hz */
133 #define HSI_VALUE                 (8000000)
134
135 /* BDCR register base address */
136 #define BDCR_BASE                 (PERIPH_BASE + BDCR_OFFSET)
137
138 /* RCC Flag */
139 #define RCC_FLAG_HSIRDY                  (0x20)
140 #define RCC_FLAG_HSERDY                  (0x31)
141 #define RCC_FLAG_PLLRDY                  (0x39)
142 #define RCC_FLAG_LSERDY                  (0x41)
143 #define RCC_FLAG_LSIRDY                  (0x61)
144 #define RCC_FLAG_PINRST                  (0x7A)
145 #define RCC_FLAG_PORRST                  (0x7B)
146 #define RCC_FLAG_SFTRST                  (0x7C)
147 #define RCC_FLAG_IWDGRST                 (0x7D)
148 #define RCC_FLAG_WWDGRST                 (0x7E)
149 #define RCC_FLAG_LPWRRST                 (0x7F)
150
151 /* System clock source */
152 #define RCC_SYSCLK_HSI             (0x00000000)
153 #define RCC_SYSCLK_HSE             (0x00000001)
154 #define RCC_SYSCLK_PLLCLK          (0x00000002)
155
156 /* PLL entry clock source */
157 #define RCC_PLL_HSI_DIV2           (0x00000000)
158 #define RCC_PLL_HSE_DIV1           (0x00010000)
159 #define RCC_PLL_HSE_DIV2           (0x00030000)
160
161 /* PLL multiplication factor */
162 #define RCC_PLLMUL_2               (0x00000000)
163 #define RCC_PLLMUL_3               (0x00040000)
164 #define RCC_PLLMUL_4               (0x00080000)
165 #define RCC_PLLMUL_5               (0x000C0000)
166 #define RCC_PLLMUL_6               (0x00100000)
167 #define RCC_PLLMUL_7               (0x00140000)
168 #define RCC_PLLMUL_8               (0x00180000)
169 #define RCC_PLLMUL_9               (0x001C0000)
170 #define RCC_PLLMUL_10              (0x00200000)
171 #define RCC_PLLMUL_11              (0x00240000)
172 #define RCC_PLLMUL_12              (0x00280000)
173 #define RCC_PLLMUL_13              (0x002C0000)
174 #define RCC_PLLMUL_14              (0x00300000)
175 #define RCC_PLLMUL_15              (0x00340000)
176 #define RCC_PLLMUL_16              (0x00380000)
177
178 /* APB1/APB2 clock source */
179 #define RCC_HCLK_DIV1              (0x00000000)
180 #define RCC_HCLK_DIV2              (0x00000400)
181 #define RCC_HCLK_DIV4              (0x00000500)
182 #define RCC_HCLK_DIV8              (0x00000600)
183 #define RCC_HCLK_DIV16             (0x00000700)
184
185 /* USB clock source */
186 #define RCC_USBCLK_PLLCLK_1DIV5    (0x00)
187 #define RCC_USBCLK_PLLCLK_DIV1     (0x01)
188
189 /* ADC clock source */
190 #define RCC_PCLK2_DIV2             (0x00000000)
191 #define RCC_PCLK2_DIV4             (0x00004000)
192 #define RCC_PCLK2_DIV6             (0x00008000)
193 #define RCC_PCLK2_DIV8             (0x0000C000)
194
195 /* AHB clock source */
196 #define RCC_SYSCLK_DIV1            (0x00000000)
197 #define RCC_SYSCLK_DIV2            (0x00000080)
198 #define RCC_SYSCLK_DIV4            (0x00000090)
199 #define RCC_SYSCLK_DIV8            (0x000000A0)
200 #define RCC_SYSCLK_DIV16           (0x000000B0)
201 #define RCC_SYSCLK_DIV64           (0x000000C0)
202 #define RCC_SYSCLK_DIV128          (0x000000D0)
203 #define RCC_SYSCLK_DIV256          (0x000000E0)
204 #define RCC_SYSCLK_DIV512          (0x000000F0)
205 /*\}*/
206
207 /**
208  * RCC register: APB1 peripheral
209  */
210 /*\{*/
211 #define RCC_APB1_TIM2              (0x00000001)
212 #define RCC_APB1_TIM3              (0x00000002)
213 #define RCC_APB1_TIM4              (0x00000004)
214 #define RCC_APB1_WWDG              (0x00000800)
215 #define RCC_APB1_SPI2              (0x00004000)
216 #define RCC_APB1_USART2            (0x00020000)
217 #define RCC_APB1_USART3            (0x00040000)
218 #define RCC_APB1_I2C1              (0x00200000)
219 #define RCC_APB1_I2C2              (0x00400000)
220 #define RCC_APB1_USB               (0x00800000)
221 #define RCC_APB1_CAN               (0x02000000)
222 #define RCC_APB1_BKP               (0x08000000)
223 #define RCC_APB1_PWR               (0x10000000)
224 #define RCC_APB1_ALL               (0x1AE64807)
225 /*\}*/
226
227 /**
228  * RCC register: APB2 peripheral
229  */
230 /*\{*/
231 #define RCC_APB2_AFIO              (0x00000001)
232 #define RCC_APB2_GPIOA             (0x00000004)
233 #define RCC_APB2_GPIOB             (0x00000008)
234 #define RCC_APB2_GPIOC             (0x00000010)
235 #define RCC_APB2_GPIOD             (0x00000020)
236 #define RCC_APB2_GPIOE             (0x00000040)
237 #define RCC_APB2_ADC1              (0x00000200)
238 #define RCC_APB2_ADC2              (0x00000400)
239 #define RCC_APB2_TIM1              (0x00000800)
240 #define RCC_APB2_SPI1              (0x00001000)
241 #define RCC_APB2_USART1            (0x00004000)
242 #define RCC_APB2_ALL               (0x00005E7D)
243
244 /**
245  * RCC register:  BCDR
246  */
247 #define RCC_BDCR_LSEON             (0x00000001)
248 #define RCC_BDCR_LSERDY            (0x00000002)
249 #define RCC_BDCR_RTCSEL            (0x00000300)
250 #define RCC_BDCR_RTCEN             (0x00008000)
251 /*\}*/
252
253 /* Crystal frequency of the main oscillator (8MHz) */
254 #define PLL_VCO                    8000000
255
256 /* Reset and Clock Controller registers */
257 struct RCC
258 {
259         reg32_t CR;
260         reg32_t CFGR;
261         reg32_t CIR;
262         reg32_t APB2RSTR;
263         reg32_t APB1RSTR;
264         reg32_t AHBENR;
265         reg32_t APB2ENR;
266         reg32_t APB1ENR;
267         reg32_t BDCR;
268         reg32_t CSR;
269 };
270
271 /* RCC registers base */
272 extern struct RCC *RCC;
273
274 void clock_init(void);
275
276 #endif /* CLOCK_STM32_h */