dRonin  adbada4
dRonin firmware
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
pios_sys.c
Go to the documentation of this file.
1 
17 /*
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.
22  *
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
26  * for more details.
27  *
28  * You should have received a copy of the GNU General Public License along
29  * with this program; if not, see <http://www.gnu.org/licenses/>
30  */
31 
32 /* Project Includes */
33 #include "pios.h"
34 
35 #if defined(PIOS_INCLUDE_SYS)
36 
37 #define MEM8(addr) (*((volatile uint8_t *)(addr)))
38 
39 /* Private Function Prototypes */
40 static void NVIC_Configuration(void);
41 
45 void PIOS_SYS_Init(void)
46 {
47 #if !defined(PIOS_INCLUDE_CHIBIOS)
48  /* Setup STM32 system (RCC, clock, PLL and Flash configuration) - CMSIS Function */
49  SystemInit();
50  SystemCoreClockUpdate(); /* update SystemCoreClock for use elsewhere */
51 #endif /* !defined(PIOS_INCLUDE_CHIBIOS) */
52 
53  /*
54  * @todo might make sense to fetch the bus clocks and save them somewhere to avoid
55  * having to use the clunky get-all-clocks API everytime we need one.
56  */
57 
58  /* Initialise Basic NVIC */
59  /* do this early to ensure that we take exceptions in the right place */
60  NVIC_Configuration();
61 
62  /* Enable specific rather than generic hard faults */
63  SCB->SHCSR |= SCB_SHCSR_USGFAULTENA_Msk | SCB_SHCSR_BUSFAULTENA_Msk | SCB_SHCSR_MEMFAULTENA_Msk;
64 
65  /* Init the delay system */
67 
68  /*
69  * Turn on all the peripheral clocks.
70  * Micromanaging clocks makes no sense given the power situation in the system, so
71  * light up everything we might reasonably use here and just leave it on.
72  */
73  RCC_AHBPeriphClockCmd(
74  RCC_AHBPeriph_GPIOA |
75  RCC_AHBPeriph_GPIOB |
76  RCC_AHBPeriph_GPIOC |
77  RCC_AHBPeriph_GPIOD |
78  RCC_AHBPeriph_GPIOE |
79  RCC_AHBPeriph_GPIOF |
80  RCC_AHBPeriph_TS |
81  RCC_AHBPeriph_CRC |
82  RCC_AHBPeriph_FLITF |
83  RCC_AHBPeriph_SRAM |
84  RCC_AHBPeriph_DMA2 |
85  RCC_AHBPeriph_DMA1 |
86  RCC_AHBPeriph_ADC34 |
87  RCC_AHBPeriph_ADC12 |
88  0, ENABLE);
89 
90  RCC_APB1PeriphClockCmd(
91  RCC_APB1Periph_TIM2 |
92  RCC_APB1Periph_TIM3 |
93  RCC_APB1Periph_TIM4 |
94  RCC_APB1Periph_TIM6 |
95  RCC_APB1Periph_TIM7 |
96  RCC_APB1Periph_WWDG |
97  RCC_APB1Periph_SPI2 |
98  RCC_APB1Periph_SPI3 |
99  RCC_APB1Periph_USART2 |
100  RCC_APB1Periph_USART3 |
101  RCC_APB1Periph_UART4 |
102  RCC_APB1Periph_UART5 |
103  RCC_APB1Periph_I2C1 |
104  RCC_APB1Periph_I2C2 |
105  RCC_APB1Periph_USB |
106  RCC_APB1Periph_CAN1 |
107  RCC_APB1Periph_PWR |
108  RCC_APB1Periph_DAC |
109  0, ENABLE);
110 
111  RCC_APB2PeriphClockCmd(
112  RCC_APB2Periph_TIM1 |
113  RCC_APB2Periph_TIM8 |
114  RCC_APB2Periph_TIM15 |
115  RCC_APB2Periph_TIM16 |
116  RCC_APB2Periph_TIM17 |
117  RCC_APB2Periph_USART1 |
118  RCC_APB2Periph_SPI1 |
119  RCC_APB2Periph_SYSCFG |
120  0, ENABLE);
121 
122  /*
123  * Configure all pins as input / pullup to avoid issues with
124  * uncommitted pins, excepting special-function pins that we need to
125  * remain as-is.
126  */
127  GPIO_InitTypeDef GPIO_InitStructure;
128  GPIO_StructInit(&GPIO_InitStructure);
129  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; // default is un-pulled input
130 
131  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
132 #if (PIOS_USB_ENABLED)
133  GPIO_InitStructure.GPIO_Pin &= ~(GPIO_Pin_11 | GPIO_Pin_12); // leave USB D+/D- alone
134 #endif
135  GPIO_InitStructure.GPIO_Pin &= ~(GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15); // leave JTAG pins alone
136  GPIO_Init(GPIOA, &GPIO_InitStructure);
137 
138  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
139  GPIO_Init(GPIOB, &GPIO_InitStructure);
140  GPIO_Init(GPIOC, &GPIO_InitStructure);
141  GPIO_Init(GPIOD, &GPIO_InitStructure);
142  GPIO_Init(GPIOE, &GPIO_InitStructure);
143  GPIO_Init(GPIOF, &GPIO_InitStructure);
144 }
145 
156 int32_t PIOS_SYS_Reset(void)
157 {
158  // disable all interrupts
160 
161  // turn off all board LEDs
162 #if defined(PIOS_INCLUDE_ANNUNC) && defined(PIOS_LED_HEARTBEAT)
164 #endif /* PIOS_LED_HEARTBEAT */
165 #if defined(PIOS_INCLUDE_ANNUNC) && defined(PIOS_LED_ALARM)
167 #endif /* PIOS_LED_ALARM */
168 
169  /* XXX F10x port resets most (but not all) peripherals ... do we care? */
170 
171  /* Reset STM32 */
172  NVIC_SystemReset();
173 
174  while (1) ;
175 
176  /* We will never reach this point */
177  return -1;
178 }
179 
186 int32_t PIOS_SYS_SerialNumberGetBinary(uint8_t *array)
187 {
188  int i;
189 
190  /* Stored in the so called "electronic signature" */
191  for (i = 0; i < PIOS_SYS_SERIAL_NUM_BINARY_LEN; ++i) {
192  uint8_t b = MEM8(0x1ffff7ac + i);
193 
194  array[i] = b;
195  }
196 
197  /* No error */
198  return 0;
199 }
200 
207 int32_t PIOS_SYS_SerialNumberGet(char *str)
208 {
209  int i;
210 
211  /* Stored in the so called "electronic signature" */
212  for (i = 0; i < PIOS_SYS_SERIAL_NUM_ASCII_LEN; ++i) {
213  uint8_t b = MEM8(0x1ffff7ac + (i / 2));
214  if (!(i & 1))
215  b >>= 4;
216  b &= 0x0f;
217 
218  str[i] = ((b > 9) ? ('A' - 10) : '0') + b;
219  }
220  str[i] = '\0';
221 
222  /* No error */
223  return 0;
224 }
225 
226 static inline size_t unused_mem(uint32_t *top, uint32_t *bot, uint32_t pattern)
227 {
228  uint32_t *p = bot;
229  for (; p < top && *p == pattern; p++);
230  return (p - bot) * sizeof(uint32_t);
231 }
232 
233 #ifdef PIOS_INCLUDE_CHIBIOS
234 
235 #if !defined(CRT0_STACKS_FILL_PATTERN)
236 #define CRT0_STACKS_FILL_PATTERN 0x55555555 /* ChibiOS ResetHandler */
237 #endif
238 /* c.f. linker */
239 extern uint32_t __main_stack_base__;
240 extern uint32_t __main_stack_end__;
241 extern uint32_t __process_stack_base__;
242 extern uint32_t __process_stack_end__;
243 
244 size_t PIOS_SYS_IrqStackUnused(void)
245 {
246  return unused_mem(&__main_stack_end__, &__main_stack_base__,
247  CRT0_STACKS_FILL_PATTERN);
248 }
249 
250 size_t PIOS_SYS_OsStackUnused(void)
251 {
252  return unused_mem(&__process_stack_end__, &__process_stack_base__,
253  CRT0_STACKS_FILL_PATTERN);
254 }
255 
256 #else
257 
258 /* c.f. linker */
259 extern uint32_t _irq_stack_end;
260 extern uint32_t _irq_stack_top;
261 
262 size_t PIOS_SYS_IrqStackUnused(void)
263 {
264  return unused_mem(&_irq_stack_top, &_irq_stack_end, 0xa5a5a5a5);
265 }
266 
267 size_t PIOS_SYS_OsStackUnused(void)
268 {
269  return 0;
270 }
271 
272 #endif /* PIOS_INCLUDE_CHIBIOS */
273 
277 static void NVIC_Configuration(void)
278 {
279  /* Set the Vector Table base address as specified in .ld file */
280  extern void *pios_isr_vector_table_base;
281  NVIC_SetVectorTable((uint32_t)&pios_isr_vector_table_base, 0x0);
282 
283  /* 4 bits for Interrupt priorities so no sub priorities */
284  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
285 
286  /* Configure HCLK clock as SysTick clock source. */
287  SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);
288 }
289 
290 #ifdef USE_FULL_ASSERT
291 
298 void assert_failed(uint8_t * file, uint32_t line)
299 {
300  /* When serial debugging is implemented, use something like this. */
301  /* printf("Wrong parameters value: file %s on line %d\r\n", file, line); */
302 
303  /* Setup the LEDs to Alternate */
304 #if defined(PIOS_LED_HEARTBEAT)
306 #endif /* PIOS_LED_HEARTBEAT */
307 #if defined(PIOS_LED_ALARM)
309 #endif /* PIOS_LED_ALARM */
310 
311  /* Infinite loop */
312  while (1) {
313 #if defined(PIOS_LED_HEARTBEAT)
315 #endif /* PIOS_LED_HEARTBEAT */
316 #if defined(PIOS_LED_ALARM)
318 #endif /* PIOS_LED_ALARM */
319  for (int i = 0; i < 1000000; i++) ;
320  }
321 }
322 #endif
323 
324 #endif
325 
Main PiOS header to include all the compiled in PiOS options.
void SystemCoreClockUpdate(void)
Update SystemCoreClock variable according to Clock Register Values. The SystemCoreClock variable cont...
Definition: cmsis_system.c:289
#define PIOS_SYS_SERIAL_NUM_BINARY_LEN
Definition: pios_sys.h:36
void PIOS_SYS_Init(void)
int32_t PIOS_DELAY_Init(void)
Definition: pios_delay.c:98
void PIOS_ANNUNC_Off(uint32_t annunc_id)
void SystemInit(void)
Setup the microcontroller system Initialize the Embedded Flash Interface, the PLL and update the Syst...
Definition: cmsis_system.c:213
#define PIOS_LED_ALARM
Definition: pios_board.h:86
#define PIOS_SYS_SERIAL_NUM_ASCII_LEN
Definition: pios_sys.h:37
void PIOS_ANNUNC_On(uint32_t annunc_id)
uint8_t i
Definition: msp_messages.h:97
int32_t PIOS_SYS_Reset(void)
size_t PIOS_SYS_OsStackUnused(void)
int32_t PIOS_IRQ_Disable(void)
Definition: pios_irq.c:40
int32_t PIOS_SYS_SerialNumberGet(char str[PIOS_SYS_SERIAL_NUM_ASCII_LEN+1])
void PIOS_ANNUNC_Toggle(uint32_t annunc_id)
size_t PIOS_SYS_IrqStackUnused(void)
uint8_t p
Definition: msp_messages.h:96
#define PIOS_LED_HEARTBEAT
Definition: pios_board.h:85
int32_t PIOS_SYS_SerialNumberGetBinary(uint8_t array[PIOS_SYS_SERIAL_NUM_BINARY_LEN])