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 
16 /*
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.
21  *
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.
26  *
27  * You should have received a copy of the GNU General Public License along
28  * with this program; if not, see <http://www.gnu.org/licenses/>
29  */
30 
31 /* Project Includes */
32 #include "pios.h"
33 
34 #if defined(PIOS_INCLUDE_SYS)
35 
36 /* Private Function Prototypes */
37 void NVIC_Configuration(void);
38 void SysTick_Handler(void);
39 
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)))
44 
48 void PIOS_SYS_Init(void)
49 {
50  /* Setup STM32 system (RCC, clock, PLL and Flash configuration) - CMSIS Function */
51  SystemInit();
52 
53  /* Enable GPIOA, GPIOB, GPIOC, GPIOD, GPIOE and AFIO clocks */
54  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE |
55  RCC_APB2Periph_AFIO | RCC_APB2Periph_SPI1, ENABLE);
56 
57  RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2 | RCC_APB1Periph_SPI3, ENABLE);
58 
59 #if (PIOS_USB_ENABLED)
60  /* Ensure that pull-up is active on detect pin */
61  GPIO_InitTypeDef GPIO_InitStructure;
62  GPIO_StructInit(&GPIO_InitStructure);
63  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
64  GPIO_InitStructure.GPIO_Pin = PIOS_USB_DETECT_GPIO_PIN;
65  GPIO_Init(PIOS_USB_DETECT_GPIO_PORT, &GPIO_InitStructure);
66 #endif
67 
68  /* Initialise Basic NVIC */
69  NVIC_Configuration();
70 
71 }
72 
83 int32_t PIOS_SYS_Reset(void)
84 {
85  // disable all interrupts
87 
88  // turn off all board LEDs
89 #if defined(PIOS_LED_HEARTBEAT)
91 #endif /* PIOS_LED_HEARTBEAT */
92 #if defined(PIOS_LED_ALARM)
94 #endif /* PIOS_LED_ALARM */
95 
96  RCC_APB2PeriphResetCmd(0xffffffff, DISABLE);
97  RCC_APB1PeriphResetCmd(0xffffffff, DISABLE);
98  NVIC_SystemReset();
99 
100  while (1) ;
101 
102  /* We will never reach this point */
103  return -1;
104 }
105 
112 int32_t PIOS_SYS_SerialNumberGetBinary(uint8_t *array)
113 {
114  int i;
115 
116  /* Stored in the so called "electronic signature" */
117  for (i = 0; i < PIOS_SYS_SERIAL_NUM_BINARY_LEN; ++i) {
118  uint8_t b = MEM8(0x1ffff7e8 + i);
119 
120  array[i] = b;
121  }
122 
123  /* No error */
124  return 0;
125 }
126 
133 int32_t PIOS_SYS_SerialNumberGet(char *str)
134 {
135  int i;
136 
137  /* Stored in the so called "electronic signature" */
138  for (i = 0; i < PIOS_SYS_SERIAL_NUM_ASCII_LEN; ++i) {
139  uint8_t b = MEM8(0x1ffff7e8 + (i / 2));
140  if (!(i & 1))
141  b >>= 4;
142  b &= 0x0f;
143 
144  str[i] = ((b > 9) ? ('A' - 10) : '0') + b;
145  }
146  str[i] = '\0';
147 
148  /* No error */
149  return 0;
150 }
151 
152 static inline size_t unused_mem(uint32_t *top, uint32_t *bot, uint32_t pattern)
153 {
154  uint32_t *p = bot;
155  for (; p < top && *p == pattern; p++);
156  return (p - bot) * sizeof(uint32_t);
157 }
158 
159 #ifdef PIOS_INCLUDE_CHIBIOS
160 
161 #if !defined(CRT0_STACKS_FILL_PATTERN)
162 #define CRT0_STACKS_FILL_PATTERN 0x55555555 /* ChibiOS ResetHandler */
163 #endif
164 /* c.f. linker */
165 extern uint32_t __main_stack_base__;
166 extern uint32_t __main_stack_end__;
167 extern uint32_t __process_stack_base__;
168 extern uint32_t __process_stack_end__;
169 
170 size_t PIOS_SYS_IrqStackUnused(void)
171 {
172  return unused_mem(&__main_stack_end__, &__main_stack_base__,
173  CRT0_STACKS_FILL_PATTERN);
174 }
175 
176 size_t PIOS_SYS_OsStackUnused(void)
177 {
178  return unused_mem(&__process_stack_end__, &__process_stack_base__,
179  CRT0_STACKS_FILL_PATTERN);
180 }
181 
182 #else
183 
184 /* c.f. linker */
185 extern uint32_t _irq_stack_end;
186 extern uint32_t _irq_stack_top;
187 
188 size_t PIOS_SYS_IrqStackUnused(void)
189 {
190  return unused_mem(&_irq_stack_top, &_irq_stack_end, 0xa5a5);
191 }
192 
193 size_t PIOS_SYS_OsStackUnused(void)
194 {
195  return 0;
196 }
197 
198 #endif /* PIOS_INCLUDE_CHIBIOS */
199 
203 void NVIC_Configuration(void)
204 {
205  /* Set the Vector Table base address as specified in .ld file */
206  extern void *pios_isr_vector_table_base;
207  NVIC_SetVectorTable((uint32_t)&pios_isr_vector_table_base, 0x0);
208 
209  /* 4 bits for Interrupt priorities so no sub priorities */
210  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
211 
212  /* Configure HCLK clock as SysTick clock source. */
213  SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);
214 }
215 
216 #ifdef USE_FULL_ASSERT
217 
224 void assert_failed(uint8_t * file, uint32_t line)
225 {
226  /* When serial debugging is implemented, use something like this. */
227  /* printf("Wrong parameters value: file %s on line %d\r\n", file, line); */
228 
229  /* Setup the LEDs to Alternate */
230 #if defined(PIOS_LED_HEARTBEAT)
232 #endif /* PIOS_LED_HEARTBEAT */
233 #if defined(PIOS_LED_ALARM)
235 #endif /* PIOS_LED_ALARM */
236 
237  /* Infinite loop */
238  while (1) {
239 #if defined(PIOS_LED_HEARTBEAT)
241 #endif /* PIOS_LED_HEARTBEAT */
242 #if defined(PIOS_LED_ALARM)
244 #endif /* PIOS_LED_ALARM */
245  for (int i = 0; i < 1000000; i++) ;
246  }
247 }
248 #endif
249 
250 #endif
251 
Main PiOS header to include all the compiled in PiOS options.
#define PIOS_SYS_SERIAL_NUM_BINARY_LEN
Definition: pios_sys.h:36
void PIOS_SYS_Init(void)
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)
#define PIOS_USB_DETECT_GPIO_PORT
Definition: pios_board.h:213
size_t PIOS_SYS_IrqStackUnused(void)
uint8_t p
Definition: msp_messages.h:96
#define PIOS_USB_DETECT_GPIO_PIN
Definition: pios_board.h:214
#define PIOS_LED_HEARTBEAT
Definition: pios_board.h:85
int32_t PIOS_SYS_SerialNumberGetBinary(uint8_t array[PIOS_SYS_SERIAL_NUM_BINARY_LEN])