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 * @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
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 */
36 #if defined(PIOS_INCLUDE_SYS)
38 #define MEM8(addr) (*((volatile uint8_t *)(addr)))
40 /* Private Function Prototypes */
41 static void NVIC_Configuration(void);
44 * Initialises all system peripherals
46 void PIOS_SYS_Init(void)
48 /* Setup STM32 system (RCC, clock, PLL and Flash configuration) - CMSIS Function */
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 */
61 /* Init the delay system */
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(
86 RCC_APB1PeriphClockCmd(
95 RCC_APB1Periph_USART2
|
96 RCC_APB1Periph_USART3
|
97 RCC_APB1Periph_UART4
|
98 RCC_APB1Periph_UART5
|
100 RCC_APB1Periph_I2C2
|
102 RCC_APB1Periph_CAN1
|
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
|
119 * Configure all pins as input / pullup to avoid issues with
120 * uncommitted pins, excepting special-function pins that we need to
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
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>
145 * <LI>Disable all RTOS tasks
146 * <LI>Disable all interrupts
147 * <LI>Turn off all board LEDs
150 * \return < 0 if reset failed
152 int32_t PIOS_SYS_Reset(void)
154 // disable all interrupts
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? */
178 /* We will never reach this point */
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
)
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
);
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
)
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));
221 str
[i
] = ((b
> 9) ? ('A' - 10) : '0') + b
;
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
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 */
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) */