update credits
[librepilot.git] / flight / pios / stm32f10x / pios_sys.c
blob288fc0775b31d80d59a7449c5a09854140f3d980
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 The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
11 * Parts by Thorsten Klose (tk@midibox.org) (tk@midibox.org)
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 /* Private Function Prototypes */
37 void NVIC_Configuration(void);
38 void SysTick_Handler(void);
40 /* Local Macros */
41 #define MEM8(addr) (*((volatile uint8_t *)(addr)))
42 #define MEM16(addr) (*((volatile uint16_t *)(addr)))
43 #define MEM32(addr) (*((volatile uint32_t *)(addr)))
45 /**
46 * Initialises all system peripherals
48 void PIOS_SYS_Init(void)
50 /* Setup STM32 system (RCC, clock, PLL and Flash configuration) - CMSIS Function */
51 SystemInit();
53 /* Init the delay system */
54 PIOS_DELAY_Init();
56 /* Enable DMA */
57 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1 |
58 RCC_AHBPeriph_DMA2,
59 ENABLE);
61 /* Enable GPIOA, GPIOB, GPIOC, GPIOD, GPIOE and AFIO clocks */
62 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE |
63 RCC_APB2Periph_AFIO, ENABLE);
65 #if (PIOS_USB_ENABLED)
66 /* Ensure that pull-up is active on detect pin */
67 GPIO_InitTypeDef GPIO_InitStructure;
68 GPIO_StructInit(&GPIO_InitStructure);
69 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
70 GPIO_InitStructure.GPIO_Pin = PIOS_USB_DETECT_GPIO_PIN;
71 GPIO_Init(PIOS_USB_DETECT_GPIO_PORT, &GPIO_InitStructure);
72 #endif
74 /* Initialise Basic NVIC */
75 NVIC_Configuration();
78 /**
79 * Shutdown PIOS and reset the microcontroller:<BR>
80 * <UL>
81 * <LI>Disable all RTOS tasks
82 * <LI>Disable all interrupts
83 * <LI>Turn off all board LEDs
84 * <LI>Reset STM32
85 * </UL>
86 * \return < 0 if reset failed
88 int32_t PIOS_SYS_Reset(void)
90 /* Disable all RTOS tasks */
91 #if defined(PIOS_INCLUDE_FREERTOS)
92 /* port specific FreeRTOS function to disable tasks (nested) */
93 portENTER_CRITICAL();
94 #endif
96 // disable all interrupts
97 PIOS_IRQ_Disable();
99 // turn off all board LEDs
100 #ifdef PIOS_INCLUDE_LED
101 # ifdef PIOS_LED_HEARTBEAT
102 PIOS_LED_Off(PIOS_LED_HEARTBEAT);
103 # endif /* PIOS_LED_HEARTBEAT */
104 # ifdef PIOS_LED_ALARM
105 PIOS_LED_Off(PIOS_LED_ALARM);
106 # endif /* PIOS_LED_ALARM */
107 # ifdef PIOS_BUZZER_ALARM
108 PIOS_LED_Off(PIOS_BUZZER_ALARM);
109 # endif /* PIOS_BUZZER_ALARM */
110 #endif /* PIOS_INCLUDE_LED */
112 RCC_APB2PeriphResetCmd(0xffffffff, DISABLE);
113 RCC_APB1PeriphResetCmd(0xffffffff, DISABLE);
114 NVIC_SystemReset();
116 while (1) {
120 /* We will never reach this point */
121 return -1;
125 * Returns the CPU's flash size (in bytes)
127 uint32_t PIOS_SYS_getCPUFlashSize(void)
129 return (uint32_t)MEM16(0x1FFFF7E0) * 1000;
133 * Returns the serial number as a string
134 * param[out] uint8_t pointer to a string which can store at least 12 bytes
135 * (12 bytes returned for STM32)
136 * return < 0 if feature not supported
138 int32_t PIOS_SYS_SerialNumberGetBinary(uint8_t *array)
140 int i;
142 /* Stored in the so called "electronic signature" */
143 for (i = 0; i < PIOS_SYS_SERIAL_NUM_BINARY_LEN; ++i) {
144 uint8_t b = MEM8(0x1ffff7e8 + i);
146 array[i] = b;
149 /* No error */
150 return 0;
154 * Returns the serial number as a string
155 * param[out] str pointer to a string which can store at least 32 digits + zero terminator!
156 * (24 digits returned for STM32)
157 * return < 0 if feature not supported
159 int32_t PIOS_SYS_SerialNumberGet(char *str)
161 int i;
163 /* Stored in the so called "electronic signature" */
164 for (i = 0; i < PIOS_SYS_SERIAL_NUM_ASCII_LEN; ++i) {
165 uint8_t b = MEM8(0x1ffff7e8 + (i / 2));
166 if (!(i & 1)) {
167 b >>= 4;
169 b &= 0x0f;
171 str[i] = ((b > 9) ? ('A' - 10) : '0') + b;
173 str[i] = '\0';
175 /* No error */
176 return 0;
180 * Configures Vector Table base location and SysTick
182 void NVIC_Configuration(void)
184 /* Set the Vector Table base address as specified in .ld file */
185 extern void *pios_isr_vector_table_base;
187 NVIC_SetVectorTable((uint32_t)&pios_isr_vector_table_base, 0x0);
189 /* 4 bits for Interrupt priorities so no sub priorities */
190 NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
192 /* Configure HCLK clock as SysTick clock source. */
193 SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);
196 #ifdef USE_FULL_ASSERT
198 * Reports the name of the source file and the source line number
199 * where the assert_param error has occurred.
200 * \param[in] file pointer to the source file name
201 * \param[in] line assert_param error line source number
202 * \retval None
204 void assert_failed(uint8_t *file, uint32_t line)
206 /* When serial debugging is implemented, use something like this. */
207 /* printf("Wrong parameters value: file %s on line %d\r\n", file, line); */
209 /* Setup the LEDs to Alternate */
210 #if defined(PIOS_LED_HEARTBEAT)
211 PIOS_LED_On(PIOS_LED_HEARTBEAT);
212 #endif /* PIOS_LED_HEARTBEAT */
213 #if defined(PIOS_LED_ALARM)
214 PIOS_LED_Off(PIOS_LED_ALARM);
215 #endif /* PIOS_LED_ALARM */
217 /* Infinite loop */
218 while (1) {
219 #if defined(PIOS_LED_HEARTBEAT)
220 PIOS_LED_Toggle(PIOS_LED_HEARTBEAT);
221 #endif /* PIOS_LED_HEARTBEAT */
222 #if defined(PIOS_LED_ALARM)
223 PIOS_LED_Toggle(PIOS_LED_ALARM);
224 #endif /* PIOS_LED_ALARM */
225 for (int i = 0; i < 1000000; i++) {
230 #endif /* ifdef USE_FULL_ASSERT */
232 #endif /* PIOS_INCLUDE_SYS */
235 * @}
236 * @}