Revert "REVONANO - Workaround for the SBUS issue (use oversample 8 instead of 16...
[librepilot.git] / flight / pios / stm32f10x / pios_sys.c
blob63791fa22e5b1c679e549937c5c2215e14784e9f
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 GPIOA, GPIOB, GPIOC, GPIOD, GPIOE and AFIO clocks */
57 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE |
58 RCC_APB2Periph_AFIO, ENABLE);
60 #if (PIOS_USB_ENABLED)
61 /* Ensure that pull-up is active on detect pin */
62 GPIO_InitTypeDef GPIO_InitStructure;
63 GPIO_StructInit(&GPIO_InitStructure);
64 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
65 GPIO_InitStructure.GPIO_Pin = PIOS_USB_DETECT_GPIO_PIN;
66 GPIO_Init(PIOS_USB_DETECT_GPIO_PORT, &GPIO_InitStructure);
67 #endif
69 /* Initialise Basic NVIC */
70 NVIC_Configuration();
73 /**
74 * Shutdown PIOS and reset the microcontroller:<BR>
75 * <UL>
76 * <LI>Disable all RTOS tasks
77 * <LI>Disable all interrupts
78 * <LI>Turn off all board LEDs
79 * <LI>Reset STM32
80 * </UL>
81 * \return < 0 if reset failed
83 int32_t PIOS_SYS_Reset(void)
85 /* Disable all RTOS tasks */
86 #if defined(PIOS_INCLUDE_FREERTOS)
87 /* port specific FreeRTOS function to disable tasks (nested) */
88 portENTER_CRITICAL();
89 #endif
91 // disable all interrupts
92 PIOS_IRQ_Disable();
94 // turn off all board LEDs
95 #if defined(PIOS_LED_HEARTBEAT)
96 PIOS_LED_Off(PIOS_LED_HEARTBEAT);
97 #endif /* PIOS_LED_HEARTBEAT */
98 #if defined(PIOS_LED_ALARM)
99 PIOS_LED_Off(PIOS_LED_ALARM);
100 #endif /* PIOS_LED_ALARM */
102 RCC_APB2PeriphResetCmd(0xffffffff, DISABLE);
103 RCC_APB1PeriphResetCmd(0xffffffff, DISABLE);
104 NVIC_SystemReset();
106 while (1) {
110 /* We will never reach this point */
111 return -1;
115 * Returns the CPU's flash size (in bytes)
117 uint32_t PIOS_SYS_getCPUFlashSize(void)
119 return (uint32_t)MEM16(0x1FFFF7E0) * 1000;
123 * Returns the serial number as a string
124 * param[out] uint8_t pointer to a string which can store at least 12 bytes
125 * (12 bytes returned for STM32)
126 * return < 0 if feature not supported
128 int32_t PIOS_SYS_SerialNumberGetBinary(uint8_t *array)
130 int i;
132 /* Stored in the so called "electronic signature" */
133 for (i = 0; i < PIOS_SYS_SERIAL_NUM_BINARY_LEN; ++i) {
134 uint8_t b = MEM8(0x1ffff7e8 + i);
136 array[i] = b;
139 /* No error */
140 return 0;
144 * Returns the serial number as a string
145 * param[out] str pointer to a string which can store at least 32 digits + zero terminator!
146 * (24 digits returned for STM32)
147 * return < 0 if feature not supported
149 int32_t PIOS_SYS_SerialNumberGet(char *str)
151 int i;
153 /* Stored in the so called "electronic signature" */
154 for (i = 0; i < PIOS_SYS_SERIAL_NUM_ASCII_LEN; ++i) {
155 uint8_t b = MEM8(0x1ffff7e8 + (i / 2));
156 if (!(i & 1)) {
157 b >>= 4;
159 b &= 0x0f;
161 str[i] = ((b > 9) ? ('A' - 10) : '0') + b;
163 str[i] = '\0';
165 /* No error */
166 return 0;
170 * Configures Vector Table base location and SysTick
172 void NVIC_Configuration(void)
174 /* Set the Vector Table base address as specified in .ld file */
175 extern void *pios_isr_vector_table_base;
177 NVIC_SetVectorTable((uint32_t)&pios_isr_vector_table_base, 0x0);
179 /* 4 bits for Interrupt priorities so no sub priorities */
180 NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
182 /* Configure HCLK clock as SysTick clock source. */
183 SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);
186 #ifdef USE_FULL_ASSERT
188 * Reports the name of the source file and the source line number
189 * where the assert_param error has occurred.
190 * \param[in] file pointer to the source file name
191 * \param[in] line assert_param error line source number
192 * \retval None
194 void assert_failed(uint8_t *file, uint32_t line)
196 /* When serial debugging is implemented, use something like this. */
197 /* printf("Wrong parameters value: file %s on line %d\r\n", file, line); */
199 /* Setup the LEDs to Alternate */
200 #if defined(PIOS_LED_HEARTBEAT)
201 PIOS_LED_On(PIOS_LED_HEARTBEAT);
202 #endif /* PIOS_LED_HEARTBEAT */
203 #if defined(PIOS_LED_ALARM)
204 PIOS_LED_Off(PIOS_LED_ALARM);
205 #endif /* PIOS_LED_ALARM */
207 /* Infinite loop */
208 while (1) {
209 #if defined(PIOS_LED_HEARTBEAT)
210 PIOS_LED_Toggle(PIOS_LED_HEARTBEAT);
211 #endif /* PIOS_LED_HEARTBEAT */
212 #if defined(PIOS_LED_ALARM)
213 PIOS_LED_Toggle(PIOS_LED_ALARM);
214 #endif /* PIOS_LED_ALARM */
215 for (int i = 0; i < 1000000; i++) {
220 #endif /* ifdef USE_FULL_ASSERT */
222 #endif /* PIOS_INCLUDE_SYS */
225 * @}
226 * @}