Paparazzi UAS  v6.2.0_stable
Paparazzi is a free software Unmanned Aircraft System.
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
mcu.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2010-2012 The Paparazzi team
3  *
4  * This file is part of Paparazzi.
5  *
6  * Paparazzi is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * Paparazzi is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Paparazzi; see the file COPYING. If not, write to
18  * the Free Software Foundation, 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  *
21  */
22 
28 #include "mcu.h"
29 #include "std.h"
30 
31 #ifdef PERIPHERALS_AUTO_INIT
32 #include "mcu_periph/gpio.h"
33 #include "mcu_periph/sys_time.h"
34 #ifdef USE_LED
35 #include "led.h"
36 #endif
37 #if defined RADIO_CONTROL
38 #if defined RADIO_CONTROL_BIND_IMPL_FUNC && defined SPEKTRUM_BIND_PIN_PORT
40 #endif
41 #endif
42 #if USE_UART0 || USE_UART1 || USE_UART2 || USE_UART3 || USE_UART4 || USE_UART5 || USE_UART6 || USE_UART7 || USE_UART8
43 #define USING_UART 1
44 #include "mcu_periph/uart.h"
45 #endif
46 #if USE_I2C0 || USE_I2C1 || USE_I2C2 || USE_I2C3 || USE_I2C4 || USE_SOFTI2C0 || USE_SOFTI2C1
47 #define USING_I2C 1
48 #include "mcu_periph/i2c.h"
49 #endif
50 #if USE_SOFTI2C0 || USE_SOFTI2C1
51 #define USING_SOFTI2C 1
52 #include "mcu_periph/softi2c.h"
53 #endif
54 #if USE_ADC
55 #include "mcu_periph/adc.h"
56 #endif
57 #if USE_USB_SERIAL
58 #include "mcu_periph/usb_serial.h"
59 #endif
60 #ifdef USE_UDP
61 #include "mcu_periph/udp.h"
62 #endif
63 #if USE_SPI
64 #include "mcu_periph/spi.h"
65 #endif
66 #ifdef USE_DAC
67 #include "mcu_periph/dac.h"
68 #endif
69 #ifdef USE_RNG
70 #include "mcu_periph/rng.h"
71 #endif
72 #ifdef USE_PIPE
73 #include "mcu_periph/pipe.h"
74 #endif
75 #endif /* PERIPHERALS_AUTO_INIT */
76 
77 void WEAK board_init(void)
78 {
79  /* default board init function does nothing... */
80 }
81 
82 void WEAK board_init2(void)
83 {
84  /* default board init function does nothing... */
85 }
86 
87 void WEAK mcu_reboot(enum reboot_state_t reboot_state __attribute__((unused)))
88 {
89  /* default reboot function does nothing... */
90 }
91 
92 void WEAK mcu_energy_save(void)
93 {
94  /* default power saving function does nothing... */
95 }
96 
97 void mcu_init(void)
98 {
99  /* If we have a board specific init function, call it.
100  * Otherwise it will simply call the empty weak function.
101  *
102  * For example the ARDrone2 has this implemented to prevent stray data of IMU
103  * from OEM program still running and also accessing AC sensors
104  */
105  board_init();
106 
107  mcu_arch_init();
108 
109  /* First enable the power of the MCU if needed */
110 #if defined MCU_PWR
113 
114 #if defined BTN_ON
116  if (gpio_get(BTN_ON, BTN_ON_PIN)) {
118  } else {
119  // Turn off and stop: wait until all power is off
120  while (true) {
122  }
123  }
124 #endif //BTN_ON
125 
126 #endif //MCU_PWR
127 
128 #ifdef PERIPHERALS_AUTO_INIT
129  sys_time_init();
130 #ifdef USE_LED
131  led_init();
132 #endif
133  /* First enable power of RC */
134 #if defined RADIO_CONTROL_POWER_PORT
137 #endif
138 #ifdef PERIPHERAL3V3_ENABLE_PORT
141 #endif
142  /* for now this means using spektrum */
143 #if defined RADIO_CONTROL && defined RADIO_CONTROL_BIND_IMPL_FUNC && defined SPEKTRUM_BIND_PIN_PORT
144  RADIO_CONTROL_BIND_IMPL_FUNC();
145 #endif
146 #if USE_UART0
147  uart0_init();
148 #endif
149 #if USE_UART1
150  uart1_init();
151 #endif
152 #if USE_UART2
153  uart2_init();
154 #endif
155 #if USE_UART3
156  uart3_init();
157 #endif
158 #if USE_UART4
159  uart4_init();
160 #endif
161 #if USE_UART5
162  uart5_init();
163 #endif
164 #if USE_UART6
165  uart6_init();
166 #endif
167 #if USE_UART7
168  uart7_init();
169 #endif
170 #if USE_UART8
171  uart8_init();
172 #endif
173 #if USING_UART
174  uart_arch_init();
175 #endif
176 #ifdef USE_I2C0
177  i2c0_init();
178 #endif
179 #ifdef USE_I2C1
180  i2c1_init();
181 #endif
182 #ifdef USE_I2C2
183  i2c2_init();
184 #endif
185 #ifdef USE_I2C3
186  i2c3_init();
187 #endif
188 #ifdef USE_I2C4
189  i2c4_init();
190 #endif
191 #ifdef USE_SOFTI2C0
192  softi2c0_init();
193 #endif
194 #ifdef USE_SOFTI2C1
195  softi2c1_init();
196 #endif
197 #if USE_ADC
198  adc_init();
199 #endif
200 #if USE_USB_SERIAL
201  VCOM_init();
202 #endif
203 
204 #if USE_SPI
205 #if SPI_MASTER
206 
207 #if USE_SPI0
208  spi0_init();
209 #endif
210 #if USE_SPI1
211  spi1_init();
212 #endif
213 #if USE_SPI2
214  spi2_init();
215 #endif
216 #if USE_SPI3
217  spi3_init();
218 #endif
219 #if USE_SPI4
220  spi4_init();
221 #endif
222  spi_init_slaves();
223 #endif // SPI_MASTER
224 
225 #if SPI_SLAVE
226 #if USE_SPI0_SLAVE
227  spi0_slave_init();
228 #endif
229 #if USE_SPI1_SLAVE
230  spi1_slave_init();
231 #endif
232 #if USE_SPI2_SLAVE
233  spi2_slave_init();
234 #endif
235 #if USE_SPI3_SLAVE
236  spi3_slave_init();
237 #endif
238 #endif // SPI_SLAVE
239 
240 #if SPI_SLAVE_HS
241  spi_slave_hs_init();
242 #endif
243 #endif // USE_SPI
244 
245 #ifdef USE_DAC
246  dac_init();
247 #endif
248 
249 #if USE_UDP0 || USE_UDP1 || USE_UDP2
250  udp_arch_init();
251 #endif
252 
253 #ifdef USE_RNG
254  rng_init();
255 #endif
256 
257 #else
258  INFO("PERIPHERALS_AUTO_INIT not enabled! Peripherals (including sys_time) need explicit initialization.")
259 #endif /* PERIPHERALS_AUTO_INIT */
260 
261  board_init2();
262 }
263 
264 
265 
266 void mcu_event(void)
267 {
268 #if USING_I2C
269  i2c_event();
270 #endif
271 #if USING_SOFTI2C
272  softi2c_event();
273 #endif
274 
275 #if USE_USB_SERIAL
276  VCOM_event();
277 #endif
278 }
arch independent ADC (Analog to Digital Converter) API
void mcu_arch_init(void)
Initialize the specific archittecture functions.
Definition: mcu_arch.c:124
void adc_init(void)
Adc init.
Definition: adc_arch.c:332
void gpio_setup_output(ioportid_t port, uint16_t gpios)
Setup one or more pins of the given GPIO port as outputs.
Definition: gpio_arch.c:33
void gpio_setup_input(ioportid_t port, uint16_t gpios)
Setup one or more pins of the given GPIO port as inputs.
Definition: gpio_arch.c:40
static uint8_t gpio_get(ioportid_t port, uint16_t pin)
Get level of a gpio.
Definition: gpio_arch.h:88
void rng_init(void)
Definition: rng_arch.c:77
void dac_init(void)
Some architecture independent helper functions for GPIOs.
void i2c_event(void)
i2c_event() function
Definition: i2c_arch.c:383
void WEAK mcu_reboot(enum reboot_state_t reboot_state)
Reboot the MCU Should not be called during flight or ciritcal operations.
Definition: mcu.c:87
reboot_state_t
The requested reboot states.
Definition: mcu.h:42
void WEAK board_init(void)
Optional board init function called at the start of mcu_init().
Definition: mcu.c:77
void mcu_init(void)
Microcontroller peripherals initialization.
Definition: mcu.c:97
void WEAK mcu_energy_save(void)
Puts the MCU in energy save mode This disables features of the MCU to save energy.
Definition: mcu.c:92
void mcu_event(void)
MCU event functions.
Definition: mcu.c:266
void WEAK board_init2(void)
Optional board init function called at the end of mcu_init().
Definition: mcu.c:82
void spi2_init(void)
Definition: spi.c:59
void spi0_init(void)
Definition: spi.c:37
void spi1_init(void)
Definition: spi.c:48
void spi_init_slaves(void)
spi_init_slaves() function
Definition: spi_arch.c:684
Architecture independent I2C (Inter-Integrated Circuit Bus) API.
arch independent LED (Light Emitting Diodes) API
static void led_init(void)
Automatic initialization of actived LED Set to OFF at startup.
Definition: led.h:39
void uart_arch_init(void)
Definition: uart_arch.c:54
void udp_arch_init(void)
Definition: udp_arch.c:43
Arch independent mcu ( Micro Controller Unit ) utilities.
#define MCU_PWR
Definition: opa_ftd_1.0.h:37
#define MCU_PWR_OFF
Definition: opa_ftd_1.0.h:40
#define MCU_PWR_ON
Definition: opa_ftd_1.0.h:39
#define BTN_ON_PIN
Definition: opa_ftd_1.0.h:74
#define RADIO_CONTROL_POWER_ON
Definition: opa_ftd_1.0.h:151
#define RADIO_CONTROL_POWER_PIN
Definition: opa_ftd_1.0.h:150
#define RADIO_CONTROL_POWER_PORT
Definition: opa_ftd_1.0.h:149
#define MCU_PWR_PIN
Definition: opa_ftd_1.0.h:38
#define BTN_ON
Definition: opa_ftd_1.0.h:73
arch independent PIPE API
#define PERIPHERAL3V3_ENABLE_ON
Definition: px4fmu.h:276
#define PERIPHERAL3V3_ENABLE_PORT
Definition: px4fmu.h:274
#define PERIPHERAL3V3_ENABLE_PIN
Definition: px4fmu.h:275
arch independent Random Number Generator API
void softi2c_event(void)
Definition: softi2c.c:624
Platform-independent software I2C implementation.
Radio control spektrum interface.
Architecture independent SPI (Serial Peripheral Interface) API.
void VCOM_event(void)
Poll usb (required by libopencm3).
Definition: usb_ser_hw.c:468
void VCOM_init(void)
Definition: usb_ser_hw.c:565
void sys_time_init(void)
Definition: sys_time.c:92
Architecture independent timing functions.
arch independent UART (Universal Asynchronous Receiver/Transmitter) API
arch independent UDP API
arch independent USB API