Merged in f5soh/librepilot/update_credits (pull request #529)
[librepilot.git] / flight / pios / stm32f30x / pios_sys.c
blob6d849bc147327cc2b928e57e625d7c06e2bb765e
1 /**
2 ******************************************************************************
3 * @addtogroup PIOS PIOS Core hardware abstraction layer
4 * @{
5 * @addtogroup PIOS_SYS System Functions
6 * @brief PIOS System Initialization code
7 * @{
9 * @file pios_sys.c
10 * @author Michael Smith Copyright (C) 2011
11 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2012.
12 * @author Tau Labs, http://taulabs.org, Copyright (C) 2012-2014
13 * @brief Sets up basic STM32 system hardware, functions are called from Main.
14 * @see The GNU Public License (GPL) Version 3
16 *****************************************************************************/
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 3 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful, but
24 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
25 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
26 * for more details.
28 * You should have received a copy of the GNU General Public License along
29 * with this program; if not, write to the Free Software Foundation, Inc.,
30 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33 /* Project Includes */
34 #include "pios.h"
36 #if defined(PIOS_INCLUDE_SYS)
38 #define MEM8(addr) (*((volatile uint8_t *)(addr)))
40 /* Private Function Prototypes */
41 static void NVIC_Configuration(void);
43 /**
44 * Initialises all system peripherals
46 void PIOS_SYS_Init(void)
48 /* Setup STM32 system (RCC, clock, PLL and Flash configuration) - CMSIS Function */
49 SystemInit();
50 SystemCoreClockUpdate(); /* update SystemCoreClock for use elsewhere */
53 * @todo might make sense to fetch the bus clocks and save them somewhere to avoid
54 * having to use the clunky get-all-clocks API everytime we need one.
57 /* Initialise Basic NVIC */
58 /* do this early to ensure that we take exceptions in the right place */
59 NVIC_Configuration();
61 /* Init the delay system */
62 PIOS_DELAY_Init();
65 * Turn on all the peripheral clocks.
66 * Micromanaging clocks makes no sense given the power situation in the system, so
67 * light up everything we might reasonably use here and just leave it on.
69 RCC_AHBPeriphClockCmd(
70 RCC_AHBPeriph_GPIOA |
71 RCC_AHBPeriph_GPIOB |
72 RCC_AHBPeriph_GPIOC |
73 RCC_AHBPeriph_GPIOD |
74 RCC_AHBPeriph_GPIOE |
75 RCC_AHBPeriph_GPIOF |
76 RCC_AHBPeriph_TS |
77 RCC_AHBPeriph_CRC |
78 RCC_AHBPeriph_FLITF |
79 RCC_AHBPeriph_SRAM |
80 RCC_AHBPeriph_DMA2 |
81 RCC_AHBPeriph_DMA1 |
82 RCC_AHBPeriph_ADC34 |
83 RCC_AHBPeriph_ADC12 |
84 0, ENABLE);
86 RCC_APB1PeriphClockCmd(
87 RCC_APB1Periph_TIM2 |
88 RCC_APB1Periph_TIM3 |
89 RCC_APB1Periph_TIM4 |
90 RCC_APB1Periph_TIM6 |
91 RCC_APB1Periph_TIM7 |
92 RCC_APB1Periph_WWDG |
93 RCC_APB1Periph_SPI2 |
94 RCC_APB1Periph_SPI3 |
95 RCC_APB1Periph_USART2 |
96 RCC_APB1Periph_USART3 |
97 RCC_APB1Periph_UART4 |
98 RCC_APB1Periph_UART5 |
99 RCC_APB1Periph_I2C1 |
100 RCC_APB1Periph_I2C2 |
101 RCC_APB1Periph_USB |
102 RCC_APB1Periph_CAN1 |
103 RCC_APB1Periph_PWR |
104 RCC_APB1Periph_DAC |
105 0, ENABLE);
107 RCC_APB2PeriphClockCmd(
108 RCC_APB2Periph_TIM1 |
109 RCC_APB2Periph_TIM8 |
110 RCC_APB2Periph_TIM15 |
111 RCC_APB2Periph_TIM16 |
112 RCC_APB2Periph_TIM17 |
113 RCC_APB2Periph_USART1 |
114 RCC_APB2Periph_SPI1 |
115 RCC_APB2Periph_SYSCFG |
116 0, ENABLE);
119 * Configure all pins as input / pullup to avoid issues with
120 * uncommitted pins, excepting special-function pins that we need to
121 * remain as-is.
123 GPIO_InitTypeDef GPIO_InitStructure;
124 GPIO_StructInit(&GPIO_InitStructure);
125 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; // default is un-pulled input
127 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
128 #if (PIOS_USB_ENABLED)
129 GPIO_InitStructure.GPIO_Pin &= ~(GPIO_Pin_11 | GPIO_Pin_12); // leave USB D+/D- alone
130 #endif
131 GPIO_InitStructure.GPIO_Pin &= ~(GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15); // leave JTAG pins alone
132 GPIO_Init(GPIOA, &GPIO_InitStructure);
134 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
135 GPIO_Init(GPIOB, &GPIO_InitStructure);
136 GPIO_Init(GPIOC, &GPIO_InitStructure);
137 GPIO_Init(GPIOD, &GPIO_InitStructure);
138 GPIO_Init(GPIOE, &GPIO_InitStructure);
139 GPIO_Init(GPIOF, &GPIO_InitStructure);
143 * Shutdown PIOS and reset the microcontroller:<BR>
144 * <UL>
145 * <LI>Disable all RTOS tasks
146 * <LI>Disable all interrupts
147 * <LI>Turn off all board LEDs
148 * <LI>Reset STM32
149 * </UL>
150 * \return < 0 if reset failed
152 int32_t PIOS_SYS_Reset(void)
154 // disable all interrupts
155 PIOS_IRQ_Disable();
157 // turn off all board LEDs
158 #ifdef PIOS_INCLUDE_LED
159 # ifdef PIOS_LED_HEARTBEAT
160 PIOS_LED_Off(PIOS_LED_HEARTBEAT);
161 # endif /* PIOS_LED_HEARTBEAT */
162 # ifdef PIOS_LED_ALARM
163 PIOS_LED_Off(PIOS_LED_ALARM);
164 # endif /* PIOS_LED_ALARM */
165 # ifdef PIOS_BUZZER_ALARM
166 PIOS_LED_Off(PIOS_BUZZER_ALARM);
167 # endif /* PIOS_BUZZER_ALARM */
168 #endif /* PIOS_INCLUDE_LED */
169 /* XXX F10x port resets most (but not all) peripherals ... do we care? */
171 /* Reset STM32 */
172 NVIC_SystemReset();
174 while (1) {
178 /* We will never reach this point */
179 return -1;
183 * Returns the serial number as byte array
184 * param[out] pointer to a byte array which can store at least 12 bytes
185 * (12 bytes returned for STM32)
186 * return < 0 if feature not supported
188 int32_t PIOS_SYS_SerialNumberGetBinary(uint8_t *array)
190 int i;
192 /* Stored in the so called "electronic signature" */
193 for (i = 0; i < PIOS_SYS_SERIAL_NUM_BINARY_LEN; ++i) {
194 uint8_t b = MEM8(0x1ffff7ac + i);
196 array[i] = b;
199 /* No error */
200 return 0;
204 * Returns the serial number as a string
205 * param[out] str pointer to a string which can store at least 32 digits + zero terminator!
206 * (24 digits returned for STM32)
207 * return < 0 if feature not supported
209 int32_t PIOS_SYS_SerialNumberGet(char *str)
211 int i;
213 /* Stored in the so called "electronic signature" */
214 for (i = 0; i < PIOS_SYS_SERIAL_NUM_ASCII_LEN; ++i) {
215 uint8_t b = MEM8(0x1ffff7ac + (i / 2));
216 if (!(i & 1)) {
217 b >>= 4;
219 b &= 0x0f;
221 str[i] = ((b > 9) ? ('A' - 10) : '0') + b;
223 str[i] = '\0';
225 /* No error */
226 return 0;
230 * Configures Vector Table base location and SysTick
232 static void NVIC_Configuration(void)
234 /* Set the Vector Table base address as specified in .ld file */
235 extern void *pios_isr_vector_table_base;
237 NVIC_SetVectorTable((uint32_t)&pios_isr_vector_table_base, 0x0);
239 /* 4 bits for Interrupt priorities so no sub priorities */
240 NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
242 /* Configure HCLK clock as SysTick clock source. */
243 SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);
246 #ifdef USE_FULL_ASSERT
248 * Reports the name of the source file and the source line number
249 * where the assert_param error has occurred.
250 * \param[in] file pointer to the source file name
251 * \param[in] line assert_param error line source number
252 * \retval None
254 void assert_failed(uint8_t *file, uint32_t line)
256 /* When serial debugging is implemented, use something like this. */
257 /* printf("Wrong parameters value: file %s on line %d\r\n", file, line); */
259 /* Setup the LEDs to Alternate */
260 #if defined(PIOS_LED_HEARTBEAT)
261 PIOS_LED_On(PIOS_LED_HEARTBEAT);
262 #endif /* PIOS_LED_HEARTBEAT */
263 #if defined(PIOS_LED_ALARM)
264 PIOS_LED_Off(PIOS_LED_ALARM);
265 #endif /* PIOS_LED_ALARM */
267 /* Infinite loop */
268 while (1) {
269 #if defined(PIOS_LED_HEARTBEAT)
270 PIOS_LED_Toggle(PIOS_LED_HEARTBEAT);
271 #endif /* PIOS_LED_HEARTBEAT */
272 #if defined(PIOS_LED_ALARM)
273 PIOS_LED_Toggle(PIOS_LED_ALARM);
274 #endif /* PIOS_LED_ALARM */
275 for (int i = 0; i < 1000000; i++) {
280 #endif /* ifdef USE_FULL_ASSERT */
282 #endif /* if defined(PIOS_INCLUDE_SYS) */
285 * @}
286 * @}