2 ******************************************************************************
3 * @addtogroup PIOS PIOS Core hardware abstraction layer
5 * @addtogroup PIOS_SYS System Functions
6 * @brief PIOS System Initialization code
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
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
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);
42 #define MEM8(addr) (*((volatile uint8_t *)(addr)))
43 #define MEM16(addr) (*((volatile uint16_t *)(addr)))
44 #define MEM32(addr) (*((volatile uint32_t *)(addr)))
47 * Initialises all system peripherals
49 void PIOS_SYS_Init(void)
51 /* Setup STM32 system (RCC, clock, PLL and Flash configuration) - CMSIS Function */
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 */
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(
79 RCC_APB1PeriphClockCmd(
80 RCC_APB1Periph_USART2
|
81 RCC_APB1Periph_USART3
|
91 RCC_APB2PeriphClockCmd(
92 RCC_APB2Periph_SYSCFG
|
93 RCC_APB2Periph_USART1
|
98 /* Init the delay system */
102 * Configure all pins as input / pullup to avoid issues with
103 * uncommitted pins, excepting special-function pins that we need to
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>
125 * <LI>Disable all RTOS tasks
126 * <LI>Disable all interrupts
127 * <LI>Turn off all board LEDs
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();
140 // disable all interrupts
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 */
163 /* We will never reach this point */
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
)
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
);
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
)
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));
214 str
[i
] = ((b
> 9) ? ('A' - 10) : '0') + b
;
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
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
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 */
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)
290 void HardFault_Handler(void)
295 void MemManage_Handler(void)
300 void BusFault_Handler(void)
305 void UsageFault_Handler(void)
314 #endif /* PIOS_INCLUDE_SYS */