Merged in f5soh/librepilot/update_credits (pull request #529)
[librepilot.git] / flight / pios / stm32f0x / pios_sys.c
blobc4a774e06c91deabbd3d1a876844da7c6e854abc
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 * The OpenPilot Team, http://www.openpilot.org Copyright (C) 2012.
12 * @brief Sets up basic STM32 system hardware, functions are called from Main.
13 * @see The GNU Public License (GPL) Version 3
15 *****************************************************************************/
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 3 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful, but
23 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
24 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
25 * for more details.
27 * You should have received a copy of the GNU General Public License along
28 * with this program; if not, write to the Free Software Foundation, Inc.,
29 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 #include "pios.h"
34 #ifdef PIOS_INCLUDE_SYS
36 __IO uint32_t VectorTable[48] __attribute__((section(".ram_vector_table")));
38 /* Private Function Prototypes */
39 void NVIC_Configuration(void);
40 void stopHandler();
41 /* Local Macros */
42 #define MEM8(addr) (*((volatile uint8_t *)(addr)))
43 #define MEM16(addr) (*((volatile uint16_t *)(addr)))
44 #define MEM32(addr) (*((volatile uint32_t *)(addr)))
46 /**
47 * Initialises all system peripherals
49 void PIOS_SYS_Init(void)
51 /* Setup STM32 system (RCC, clock, PLL and Flash configuration) - CMSIS Function */
52 SystemInit();
53 SystemCoreClockUpdate(); /* update SystemCoreClock for use elsewhere */
56 * @todo might make sense to fetch the bus clocks and save them somewhere to avoid
57 * having to use the clunky get-all-clocks API everytime we need one.
60 /* Initialise Basic NVIC */
61 /* do this early to ensure that we take exceptions in the right place */
62 NVIC_Configuration();
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_FLITF |
74 RCC_AHBPeriph_SRAM |
75 RCC_AHBPeriph_DMA1 |
76 RCC_AHBPeriph_CRC
77 , ENABLE);
79 RCC_APB1PeriphClockCmd(
80 RCC_APB1Periph_USART2 |
81 RCC_APB1Periph_USART3 |
82 RCC_APB1Periph_WWDG |
83 RCC_APB1Periph_PWR |
84 RCC_APB1Periph_TIM2 |
85 RCC_APB1Periph_TIM3 |
86 RCC_APB1Periph_I2C1 |
87 RCC_APB1Periph_I2C2 |
88 RCC_APB1Periph_SPI2
89 , ENABLE);
91 RCC_APB2PeriphClockCmd(
92 RCC_APB2Periph_SYSCFG |
93 RCC_APB2Periph_USART1 |
94 RCC_APB2Periph_SPI1 |
95 RCC_APB2Periph_TIM1 |
96 0, ENABLE);
98 /* Init the delay system */
99 PIOS_DELAY_Init();
102 * Configure all pins as input / pullup to avoid issues with
103 * uncommitted pins, excepting special-function pins that we need to
104 * remain as-is.
106 GPIO_InitTypeDef GPIO_InitStructure;
107 GPIO_StructInit(&GPIO_InitStructure);
108 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; // default is un-pulled input
110 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
112 GPIO_InitStructure.GPIO_Pin &= ~(GPIO_Pin_13 | GPIO_Pin_14); // leave SWD pins alone
113 GPIO_Init(GPIOA, &GPIO_InitStructure);
115 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
116 GPIO_Init(GPIOB, &GPIO_InitStructure);
118 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
119 GPIO_Init(GPIOC, &GPIO_InitStructure);
123 * Shutdown PIOS and reset the microcontroller:<BR>
124 * <UL>
125 * <LI>Disable all RTOS tasks
126 * <LI>Disable all interrupts
127 * <LI>Turn off all board LEDs
128 * <LI>Reset STM32
129 * </UL>
130 * \return < 0 if reset failed
132 int32_t PIOS_SYS_Reset(void)
134 /* Disable all RTOS tasks */
135 #if defined(PIOS_INCLUDE_FREERTOS)
136 /* port specific FreeRTOS function to disable tasks (nested) */
137 portENTER_CRITICAL();
138 #endif
140 // disable all interrupts
141 PIOS_IRQ_Disable();
143 // turn off all board LEDs
144 #ifdef PIOS_INCLUDE_LED
145 # ifdef PIOS_LED_HEARTBEAT
146 PIOS_LED_Off(PIOS_LED_HEARTBEAT);
147 # endif /* PIOS_LED_HEARTBEAT */
148 # ifdef PIOS_LED_ALARM
149 PIOS_LED_Off(PIOS_LED_ALARM);
150 # endif /* PIOS_LED_ALARM */
151 # ifdef PIOS_BUZZER_ALARM
152 PIOS_LED_Off(PIOS_BUZZER_ALARM);
153 # endif /* PIOS_BUZZER_ALARM */
154 #endif /* PIOS_INCLUDE_LED */
156 /* Reset STM32 */
157 NVIC_SystemReset();
159 while (1) {
163 /* We will never reach this point */
164 return -1;
168 * Returns the CPU's flash size (in bytes)
170 uint32_t PIOS_SYS_getCPUFlashSize(void)
172 return (uint32_t)MEM16(0x1fff7a22) * 1024; // it might be possible to locate in the OTP area, but haven't looked and not documented
176 * Returns the serial number as a string
177 * param[out] str pointer to a string which can store at least 32 digits + zero terminator!
178 * (24 digits returned for STM32)
179 * return < 0 if feature not supported
181 int32_t PIOS_SYS_SerialNumberGetBinary(uint8_t *array)
183 int i;
185 /* Stored in the so called "electronic signature" */
186 for (i = 0; i < PIOS_SYS_SERIAL_NUM_BINARY_LEN; ++i) {
187 uint8_t b = MEM8(0x1FFFF7AC + i);
189 array[i] = b;
192 /* No error */
193 return 0;
197 * Returns the serial number as a string
198 * param[out] str pointer to a string which can store at least 32 digits + zero terminator!
199 * (24 digits returned for STM32)
200 * return < 0 if feature not supported
202 int32_t PIOS_SYS_SerialNumberGet(char *str)
204 int i;
206 /* Stored in the so called "electronic signature" */
207 for (i = 0; i < PIOS_SYS_SERIAL_NUM_ASCII_LEN; ++i) {
208 uint8_t b = MEM8(0x1FFFF7AC + (i / 2));
209 if (!(i & 1)) {
210 b >>= 4;
212 b &= 0x0f;
214 str[i] = ((b > 9) ? ('A' - 10) : '0') + b;
216 str[i] = '\0';
218 /* No error */
219 return 0;
223 * Configures Vector Table base location and SysTick
225 void NVIC_Configuration(void)
227 /* Relocate by software the vector table to the internal SRAM at 0x20000000 ***/
228 extern uint32_t pios_isr_vector_table_base;
229 uint32_t *romTable = &pios_isr_vector_table_base;
231 /* Copy the vector table from the Flash (mapped at the base of the application
232 load address 0x0800X000) to the base address of the SRAM at 0x20000000. */
234 for (uint32_t i = 0; i < 48; i++) {
235 VectorTable[i] = romTable[i];
237 // Ensure all memory operation completes prior the remap
238 __DSB();
240 /* Enable the SYSCFG peripheral clock*/
241 RCC_APB2PeriphResetCmd(RCC_APB2Periph_SYSCFG, ENABLE);
242 /* Remap SRAM at 0x00000000 */
243 SYSCFG_MemoryRemapConfig(SYSCFG_MemoryRemap_SRAM);
245 /* Configure HCLK clock as SysTick clock source. */
246 SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);
249 #ifdef USE_FULL_ASSERT
251 * Reports the name of the source file and the source line number
252 * where the assert_param error has occurred.
253 * \param[in] file pointer to the source file name
254 * \param[in] line assert_param error line source number
255 * \retval None
257 void assert_failed(uint8_t *file, uint32_t line)
259 /* When serial debugging is implemented, use something like this. */
260 /* printf("Wrong parameters value: file %s on line %d\r\n", file, line); */
262 /* Setup the LEDs to Alternate */
263 #if defined(PIOS_LED_HEARTBEAT)
264 PIOS_LED_On(PIOS_LED_HEARTBEAT);
265 #endif /* PIOS_LED_HEARTBEAT */
266 #if defined(PIOS_LED_ALARM)
267 PIOS_LED_Off(PIOS_LED_ALARM);
268 #endif /* PIOS_LED_ALARM */
270 /* Infinite loop */
271 while (1) {
272 #if defined(PIOS_LED_HEARTBEAT)
273 PIOS_LED_Toggle(PIOS_LED_HEARTBEAT);
274 #endif /* PIOS_LED_HEARTBEAT */
275 #if defined(PIOS_LED_ALARM)
276 PIOS_LED_Toggle(PIOS_LED_ALARM);
277 #endif /* PIOS_LED_ALARM */
278 for (int i = 0; i < 1000000; i++) {
283 #endif /* ifdef USE_FULL_ASSERT */
285 void NMI_Handler(void)
287 stopHandler();
290 void HardFault_Handler(void)
292 stopHandler();
295 void MemManage_Handler(void)
297 stopHandler();
300 void BusFault_Handler(void)
302 stopHandler();
305 void UsageFault_Handler(void)
307 stopHandler();
310 void stopHandler()
312 while (1) {}
314 #endif /* PIOS_INCLUDE_SYS */
317 * @}
318 * @}