Paparazzi UAS  v4.2.2_stable-4-gcc32f65
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
mcu_arch.c
Go to the documentation of this file.
1 /*
2  * Paparazzi stm32 arch dependant microcontroller initialisation function
3  *
4  * Copyright (C) 2010 The Paparazzi team
5  *
6  * This file is part of Paparazzi.
7  *
8  * Paparazzi is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2, or (at your option)
11  * any later version.
12  *
13  * Paparazzi is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with Paparazzi; see the file COPYING. If not, write to
20  * the Free Software Foundation, 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  *
23  */
24 
25 #include "mcu.h"
26 
27 #include BOARD_CONFIG
28 
29 #include <inttypes.h>
30 #include <stm32/gpio.h>
31 #include <stm32/rcc.h>
32 #include <stm32/flash.h>
33 #include <stm32/misc.h>
34 #if USE_OPENCM3
35 # if defined(STM32F1) || defined(STM32F2) || defined(STM32F4)
36 # include <libopencm3/stm32/f1/rcc.h>
37 # else
38 # include <libopencm3/stm32/rcc.h>
39 # endif
40 #endif
41 
42 
43 void mcu_arch_init(void) {
44 #if USE_OPENCM3
45  rcc_clock_setup_in_hse_12mhz_out_72mhz();
46  /* Sometimes luftboot is not setting the value, (for example when using gdb for flashing the firmware). */
47 #if LUFTBOOT
48 #pragma message "We are running luftboot, the interrupt vector is being relocated."
49  NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x00002000);
50 #else
51  NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x00000000);
52 #endif
53  return;
54 #else // !USE_OPENCM3
55 #ifdef HSE_TYPE_EXT_CLK
56 #pragma message "Using external clock."
57  /* Setup the microcontroller system.
58  * Initialize the Embedded Flash Interface,
59  * initialize the PLL and update the SystemFrequency variable.
60  */
61  /* RCC system reset(for debug purpose) */
62  RCC_DeInit();
63  /* Enable HSE with external clock ( HSE_Bypass ) */
64  RCC_HSEConfig( STM32_RCC_MODE );
65  /* Wait till HSE is ready */
66  ErrorStatus HSEStartUpStatus = RCC_WaitForHSEStartUp();
67  if (HSEStartUpStatus != SUCCESS) {
68  /* block if something went wrong */
69  while(1) {}
70  }
71  else {
72  /* Enable Prefetch Buffer */
73  FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
74  /* Flash 2 wait state */
75  FLASH_SetLatency(FLASH_Latency_2);
76  /* HCLK = SYSCLK */
77  RCC_HCLKConfig(RCC_SYSCLK_Div1);
78  /* PCLK2 = HCLK */
79  RCC_PCLK2Config(RCC_HCLK_Div1);
80  /* PCLK1 = HCLK/2 */
81  RCC_PCLK1Config(RCC_HCLK_Div2);
82  /* PLLCLK = 8MHz * 9 = 72 MHz */
83  RCC_PLLConfig(RCC_PLLSource_HSE_Div1, STM32_PLL_MULT);
84  /* Enable PLL */
85  RCC_PLLCmd(ENABLE);
86  /* Wait till PLL is ready */
87  while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) {}
88  /* Select PLL as system clock source */
89  RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
90  /* Wait till PLL is used as system clock source */
91  while(RCC_GetSYSCLKSource() != 0x08) {}
92  }
93 #else /* HSE_TYPE_EXT_CLK */
94 #pragma message "Using normal system clock setup."
95  SystemInit();
96 #endif /* HSE_TYPE_EXT_CLK */
97 /* Set the Vector Table base location. */
98 #if LUFTBOOT
99 #pragma message "We are running luftboot, the interrupt vector is being relocated."
100  /* Sometimes luftboot is not setting the value, (for example when using gdb for flashing the firmware). */
101  NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x00002000);
102 #else
103  NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x00000000);
104 #endif
105 
106 #ifdef STM32_FORCE_ALL_CLOCK_ON
107  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |
108  RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD |
109  RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO, ENABLE);
110 #endif
111 
112 #endif // USE_OPENCM3
113 }
114 
#define STM32_PLL_MULT
Definition: lisa_l_1.0.h:11
arch independent mcu ( Micro Controller Unit ) utilities
#define STM32_RCC_MODE
Definition: lisa_l_1.0.h:10
#define RESET
Definition: humid_sht.h:40
void mcu_arch_init(void)
Definition: mcu_arch.c:36