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 
18 /*
19  * This program is free software; you can redistribute it and/or modify
20  * it under the terms of the GNU General Public License as published by
21  * the Free Software Foundation; either version 3 of the License, or
22  * (at your option) any later version.
23  *
24  * This program is distributed in the hope that it will be useful, but
25  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
26  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
27  * for more details.
28  *
29  * You should have received a copy of the GNU General Public License along
30  * with this program; if not, see <http://www.gnu.org/licenses/>
31  */
32 
33 /* Project Includes */
34 #include "pios.h"
35 
36 #if defined(PIOS_INCLUDE_SYS)
37 
38 #define MEM8(addr) (*((volatile uint8_t *)(addr)))
39 
40 /* Private Function Prototypes */
41 static void NVIC_Configuration(void);
42 
46 void PIOS_SYS_Init(void)
47 {
48  /* Setup STM32 system (RCC, clock, PLL and Flash configuration) - CMSIS Function */
49  SystemInit();
50  SystemCoreClockUpdate(); /* update SystemCoreClock for use elsewhere */
51 
52  /*
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.
55  */
56 
57  /* Initialise Basic NVIC */
58  /* do this early to ensure that we take exceptions in the right place */
59  NVIC_Configuration();
60 
61  /* Init the delay system */
63 
64  /* Enable the clock that the ADC uses. */
65  RCC_HSI14Cmd(ENABLE);
66 
67  /*
68  * Turn on all the peripheral clocks.
69  * Micromanaging clocks makes no sense given the power situation in the system, so
70  * light up everything we might reasonably use here and just leave it on.
71  */
72  RCC_AHBPeriphClockCmd(
73  RCC_AHBPeriph_GPIOA |
74  RCC_AHBPeriph_GPIOB |
75  RCC_AHBPeriph_GPIOC |
76  RCC_AHBPeriph_GPIOD |
77  RCC_AHBPeriph_GPIOE |
78  RCC_AHBPeriph_GPIOF |
79  RCC_AHBPeriph_TS |
80  RCC_AHBPeriph_CRC |
81  RCC_AHBPeriph_FLITF |
82  RCC_AHBPeriph_SRAM |
83  RCC_AHBPeriph_DMA2 |
84  RCC_AHBPeriph_DMA1 |
85  0, ENABLE);
86 
87  RCC_APB1PeriphClockCmd(
88  RCC_APB1Periph_TIM2 |
89  RCC_APB1Periph_TIM3 |
90  RCC_APB1Periph_TIM6 |
91  RCC_APB1Periph_TIM7 |
92  RCC_APB1Periph_TIM14 |
93  RCC_APB1Periph_WWDG |
94  RCC_APB1Periph_SPI2 |
95  RCC_APB1Periph_USART2 |
96  RCC_APB1Periph_I2C1 |
97  RCC_APB1Periph_I2C2 |
98  RCC_APB1Periph_PWR |
99  0, ENABLE);
100 
101  RCC_APB2PeriphClockCmd(
102  RCC_APB2Periph_SYSCFG |
103  RCC_APB2Periph_ADC1 |
104  RCC_APB2Periph_TIM1 |
105  RCC_APB2Periph_SPI1 |
106  RCC_APB2Periph_USART1 |
107  RCC_APB2Periph_TIM15 |
108  RCC_APB2Periph_TIM16 |
109  RCC_APB2Periph_TIM17 |
110  RCC_APB2Periph_DBGMCU |
111  0, ENABLE);
112 }
113 
124 int32_t PIOS_SYS_Reset(void)
125 {
126  // disable all interrupts
128 
129  // turn off all board LEDs
130 #if defined(PIOS_LED_HEARTBEAT)
132 #endif /* PIOS_LED_HEARTBEAT */
133 #if defined(PIOS_LED_ALARM)
135 #endif /* PIOS_LED_ALARM */
136 
137  /* XXX F10x port resets most (but not all) peripherals ... do we care? */
138 
139  /* Reset STM32 */
140  NVIC_SystemReset();
141 
142  while (1) ;
143 
144  /* We will never reach this point */
145  return -1;
146 }
147 
154 int32_t PIOS_SYS_SerialNumberGetBinary(uint8_t *array)
155 {
156  int i;
157 
158  /* Stored in the so called "electronic signature" */
159  for (i = 0; i < PIOS_SYS_SERIAL_NUM_BINARY_LEN; ++i) {
160  uint8_t b = MEM8(0x1ffff7ac + i);
161 
162  array[i] = b;
163  }
164 
165  /* No error */
166  return 0;
167 }
168 
175 int32_t PIOS_SYS_SerialNumberGet(char *str)
176 {
177  int i;
178 
179  /* Stored in the so called "electronic signature" */
180  for (i = 0; i < PIOS_SYS_SERIAL_NUM_ASCII_LEN; ++i) {
181  uint8_t b = MEM8(0x1ffff7ac + (i / 2));
182  if (!(i & 1))
183  b >>= 4;
184  b &= 0x0f;
185 
186  str[i] = ((b > 9) ? ('A' - 10) : '0') + b;
187  }
188  str[i] = '\0';
189 
190  /* No error */
191  return 0;
192 }
193 
194 size_t PIOS_SYS_IrqStackUnused(void)
195 {
196  return 0;
197 }
198 
199 size_t PIOS_SYS_OsStackUnused(void)
200 {
201  return 0;
202 }
203 
207 static void NVIC_Configuration(void)
208 {
209  /* Configure HCLK clock as SysTick clock source. */
210  SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);
211 }
212 
213 #ifdef USE_FULL_ASSERT
214 
221 void assert_failed(uint8_t * file, uint32_t line)
222 {
223  /* When serial debugging is implemented, use something like this. */
224  /* printf("Wrong parameters value: file %s on line %d\r\n", file, line); */
225 
226  /* Setup the LEDs to Alternate */
227 #if defined(PIOS_LED_HEARTBEAT)
229 #endif /* PIOS_LED_HEARTBEAT */
230 #if defined(PIOS_LED_ALARM)
232 #endif /* PIOS_LED_ALARM */
233 
234  /* Infinite loop */
235  while (1) {
236 #if defined(PIOS_LED_HEARTBEAT)
238 #endif /* PIOS_LED_HEARTBEAT */
239 #if defined(PIOS_LED_ALARM)
241 #endif /* PIOS_LED_ALARM */
242  for (int i = 0; i < 1000000; i++) ;
243  }
244 }
245 #endif
246 
247 #endif
248 
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)
#define PIOS_LED_HEARTBEAT
Definition: pios_board.h:85
int32_t PIOS_SYS_SerialNumberGetBinary(uint8_t array[PIOS_SYS_SERIAL_NUM_BINARY_LEN])