Merged in f5soh/librepilot/update_credits (pull request #529)
[librepilot.git] / flight / targets / boards / revonano / firmware / pios_board.c
bloba6e89c839af37b6be40a3fb3b166df618f74db6a
1 /**
2 ******************************************************************************
3 * @file pios_board.c
4 * @author The LibrePilot Project, http://www.librepilot.org Copyright (C) 2015.
5 * The OpenPilot Team, http://www.openpilot.org Copyright (C) 2011.
6 * PhoenixPilot, http://github.com/PhoenixPilot, Copyright (C) 2012
7 * @addtogroup OpenPilotSystem OpenPilot System
8 * @{
9 * @addtogroup OpenPilotCore OpenPilot Core
10 * @{
11 * @brief Defines board specific static initializers for hardware for the revomini board.
12 *****************************************************************************/
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 3 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * for more details.
24 * You should have received a copy of the GNU General Public License along
25 * with this program; if not, write to the Free Software Foundation, Inc.,
26 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include "inc/openpilot.h"
30 #include <pios_board_info.h>
31 #include <uavobjectsinit.h>
32 #include <hwsettings.h>
33 #include <taskinfo.h>
34 #include <pios_ws2811.h>
36 #include "sanitycheck.h"
37 #include "actuatorsettings.h"
39 #ifdef PIOS_INCLUDE_INSTRUMENTATION
40 #include <pios_instrumentation.h>
41 #endif
43 #include <pios_board_io.h>
44 #include <pios_board_sensors.h>
47 * Pull in the board-specific static HW definitions.
48 * Including .c files is a bit ugly but this allows all of
49 * the HW definitions to be const and static to limit their
50 * scope.
52 * NOTE: THIS IS THE ONLY PLACE THAT SHOULD EVER INCLUDE THIS FILE
54 #include "../board_hw_defs.c"
56 uintptr_t pios_uavo_settings_fs_id;
57 uintptr_t pios_user_fs_id;
59 #ifdef PIOS_INCLUDE_WS2811
60 uint32_t pios_ws2811_id;
61 #endif
63 static SystemAlarmsExtendedAlarmStatusOptions RevoNanoConfigHook();
64 static void ActuatorSettingsUpdatedCb(UAVObjEvent *ev);
66 /**
67 * PIOS_Board_Init()
68 * initializes all the core subsystems on this specific hardware
69 * called from System/openpilot.c
72 #include <pios_board_info.h>
74 static const PIOS_BOARD_IO_UART_Function flexi_function_map[] = {
75 [HWSETTINGS_RM_FLEXIPORT_TELEMETRY] = PIOS_BOARD_IO_UART_TELEMETRY,
76 [HWSETTINGS_RM_FLEXIPORT_GPS] = PIOS_BOARD_IO_UART_GPS,
77 [HWSETTINGS_RM_FLEXIPORT_DSM] = PIOS_BOARD_IO_UART_DSM_FLEXI,
78 [HWSETTINGS_RM_FLEXIPORT_EXBUS] = PIOS_BOARD_IO_UART_EXBUS,
79 [HWSETTINGS_RM_FLEXIPORT_HOTTSUMD] = PIOS_BOARD_IO_UART_HOTT_SUMD,
80 [HWSETTINGS_RM_FLEXIPORT_HOTTSUMH] = PIOS_BOARD_IO_UART_HOTT_SUMH,
81 [HWSETTINGS_RM_FLEXIPORT_SRXL] = PIOS_BOARD_IO_UART_SRXL,
82 [HWSETTINGS_RM_FLEXIPORT_IBUS] = PIOS_BOARD_IO_UART_IBUS,
83 [HWSETTINGS_RM_FLEXIPORT_DEBUGCONSOLE] = PIOS_BOARD_IO_UART_DEBUGCONSOLE,
84 [HWSETTINGS_RM_FLEXIPORT_COMBRIDGE] = PIOS_BOARD_IO_UART_COMBRIDGE,
85 [HWSETTINGS_RM_FLEXIPORT_OSDHK] = PIOS_BOARD_IO_UART_OSDHK,
86 [HWSETTINGS_RM_FLEXIPORT_MSP] = PIOS_BOARD_IO_UART_MSP,
87 [HWSETTINGS_RM_FLEXIPORT_MAVLINK] = PIOS_BOARD_IO_UART_MAVLINK,
88 [HWSETTINGS_RM_FLEXIPORT_FRSKYSENSORHUB] = PIOS_BOARD_IO_UART_FRSKY_SENSORHUB,
89 [HWSETTINGS_RM_FLEXIPORT_HOTTTELEMETRY] = PIOS_BOARD_IO_UART_HOTT_BRIDGE,
92 static const PIOS_BOARD_IO_UART_Function main_function_map[] = {
93 [HWSETTINGS_RM_MAINPORT_TELEMETRY] = PIOS_BOARD_IO_UART_TELEMETRY,
94 [HWSETTINGS_RM_MAINPORT_GPS] = PIOS_BOARD_IO_UART_GPS,
95 [HWSETTINGS_RM_MAINPORT_SBUS] = PIOS_BOARD_IO_UART_SBUS,
96 [HWSETTINGS_RM_MAINPORT_DSM] = PIOS_BOARD_IO_UART_DSM_MAIN,
97 [HWSETTINGS_RM_MAINPORT_DEBUGCONSOLE] = PIOS_BOARD_IO_UART_DEBUGCONSOLE,
98 [HWSETTINGS_RM_MAINPORT_COMBRIDGE] = PIOS_BOARD_IO_UART_COMBRIDGE,
99 [HWSETTINGS_RM_MAINPORT_OSDHK] = PIOS_BOARD_IO_UART_OSDHK,
100 [HWSETTINGS_RM_MAINPORT_MSP] = PIOS_BOARD_IO_UART_MSP,
101 [HWSETTINGS_RM_MAINPORT_FRSKYSENSORHUB] = PIOS_BOARD_IO_UART_FRSKY_SENSORHUB,
102 [HWSETTINGS_RM_MAINPORT_HOTTTELEMETRY] = PIOS_BOARD_IO_UART_HOTT_BRIDGE,
105 void PIOS_Board_Init(void)
107 const struct pios_board_info *bdinfo = &pios_board_info_blob;
109 #if defined(PIOS_INCLUDE_LED)
110 const struct pios_gpio_cfg *led_cfg = PIOS_BOARD_HW_DEFS_GetLedCfg(bdinfo->board_rev);
111 PIOS_Assert(led_cfg);
112 PIOS_LED_Init(led_cfg);
113 #endif /* PIOS_INCLUDE_LED */
116 #ifdef PIOS_INCLUDE_INSTRUMENTATION
117 PIOS_Instrumentation_Init(PIOS_INSTRUMENTATION_MAX_COUNTERS);
118 #endif
120 /* Set up the SPI interface to the gyro/acelerometer */
121 if (PIOS_SPI_Init(&pios_spi_gyro_adapter_id, &pios_spi_gyro_cfg)) {
122 PIOS_DEBUG_Assert(0);
125 #ifdef PIOS_INCLUDE_I2C
126 if (PIOS_I2C_Init(&pios_i2c_eeprom_pressure_adapter_id, &pios_i2c_eeprom_pressure_adapter_cfg)) {
127 PIOS_DEBUG_Assert(0);
129 #endif
130 #if defined(PIOS_INCLUDE_FLASH)
131 /* Connect flash to the appropriate interface and configure it */
133 uintptr_t flash_id = 0;
135 // Initialize the external USER flash
136 if (PIOS_Flash_EEPROM_Init(&flash_id, &flash_main_chip_cfg, pios_i2c_eeprom_pressure_adapter_id, 0x50)) {
137 PIOS_DEBUG_Assert(0);
140 if (PIOS_FLASHFS_Init(&pios_uavo_settings_fs_id, &flash_main_fs_cfg, &pios_EEPROM_flash_driver, flash_id)) {
141 PIOS_DEBUG_Assert(0);
144 #endif /* if defined(PIOS_INCLUDE_FLASH) */
146 #if defined(PIOS_INCLUDE_RTC)
147 PIOS_RTC_Init(&pios_rtc_main_cfg);
148 #endif
149 /* IAP System Setup */
150 PIOS_IAP_Init();
151 // check for safe mode commands from gcs
152 if (PIOS_IAP_ReadBootCmd(0) == PIOS_IAP_CLEAR_FLASH_CMD_0 &&
153 PIOS_IAP_ReadBootCmd(1) == PIOS_IAP_CLEAR_FLASH_CMD_1 &&
154 PIOS_IAP_ReadBootCmd(2) == PIOS_IAP_CLEAR_FLASH_CMD_2) {
155 PIOS_FLASHFS_Format(pios_uavo_settings_fs_id);
156 PIOS_IAP_WriteBootCmd(0, 0);
157 PIOS_IAP_WriteBootCmd(1, 0);
158 PIOS_IAP_WriteBootCmd(2, 0);
161 #ifdef PIOS_INCLUDE_WDG
162 PIOS_WDG_Init();
163 #endif
165 /* Initialize the task monitor */
166 if (PIOS_TASK_MONITOR_Initialize(TASKINFO_RUNNING_NUMELEM)) {
167 PIOS_Assert(0);
170 /* Initialize the delayed callback library */
171 PIOS_CALLBACKSCHEDULER_Initialize();
173 /* Initialize UAVObject libraries */
174 EventDispatcherInitialize();
175 UAVObjInitialize();
176 SETTINGS_INITIALISE_ALL;
178 /* Initialize the alarms library */
179 AlarmsInitialize();
181 /* Set up pulse timers */
182 PIOS_TIM_InitClock(&tim_1_cfg);
183 PIOS_TIM_InitClock(&tim_2_cfg);
184 PIOS_TIM_InitClock(&tim_3_cfg);
185 PIOS_TIM_InitClock(&tim_5_cfg);
186 PIOS_TIM_InitClock(&tim_9_cfg);
187 PIOS_TIM_InitClock(&tim_10_cfg);
188 PIOS_TIM_InitClock(&tim_11_cfg);
190 uint16_t boot_count = PIOS_IAP_ReadBootCount();
191 if (boot_count < 3) {
192 PIOS_IAP_WriteBootCount(++boot_count);
193 AlarmsClear(SYSTEMALARMS_ALARM_BOOTFAULT);
194 } else {
195 /* Too many failed boot attempts, force hwsettings to defaults */
196 HwSettingsSetDefaults(HwSettingsHandle(), 0);
197 AlarmsSet(SYSTEMALARMS_ALARM_BOOTFAULT, SYSTEMALARMS_ALARM_CRITICAL);
200 #if defined(PIOS_INCLUDE_USB)
201 PIOS_BOARD_IO_Configure_USB();
202 #endif
204 /* Configure FlexiPort */
205 uint8_t hwsettings_flexiport;
206 HwSettingsRM_FlexiPortGet(&hwsettings_flexiport);
208 if (hwsettings_flexiport < NELEMENTS(flexi_function_map)) {
209 PIOS_BOARD_IO_Configure_UART(&pios_usart_flexi_cfg, flexi_function_map[hwsettings_flexiport]);
212 #if defined(PIOS_INCLUDE_I2C)
213 if (hwsettings_flexiport == HWSETTINGS_RM_FLEXIPORT_I2C) {
214 if (PIOS_I2C_Init(&pios_i2c_flexiport_adapter_id, &pios_i2c_flexiport_adapter_cfg)) {
215 PIOS_Assert(0);
218 #endif
220 /* Configure MainPort */
221 uint8_t hwsettings_mainport;
222 HwSettingsRM_MainPortGet(&hwsettings_mainport);
224 if (hwsettings_mainport < NELEMENTS(main_function_map)) {
225 PIOS_BOARD_IO_Configure_UART(&pios_usart_main_cfg, main_function_map[hwsettings_mainport]);
228 #if defined(PIOS_INCLUDE_PWM) || defined(PIOS_INCLUDE_PPM)
230 const struct pios_servo_cfg *pios_servo_cfg;
231 // default to servo outputs only
232 pios_servo_cfg = &pios_servo_cfg_out;
233 #endif
235 /* Configure the receiver port*/
236 uint8_t hwsettings_rcvrport;
237 HwSettingsRM_RcvrPortGet(&hwsettings_rcvrport);
239 switch (hwsettings_rcvrport) {
240 case HWSETTINGS_RM_RCVRPORT_DISABLED:
241 break;
242 case HWSETTINGS_RM_RCVRPORT_PWM:
243 #if defined(PIOS_INCLUDE_PWM)
244 /* Set up the receiver port. Later this should be optional */
245 PIOS_BOARD_IO_Configure_PWM_RCVR(&pios_pwm_cfg);
246 #endif /* PIOS_INCLUDE_PWM */
247 break;
248 case HWSETTINGS_RM_RCVRPORT_PPM:
249 case HWSETTINGS_RM_RCVRPORT_PPMOUTPUTS:
250 case HWSETTINGS_RM_RCVRPORT_PPMPWM:
251 #if defined(PIOS_INCLUDE_PPM)
252 if (hwsettings_rcvrport == HWSETTINGS_RM_RCVRPORT_PPMOUTPUTS) {
253 // configure servo outputs and the remaining 5 inputs as outputs
254 pios_servo_cfg = &pios_servo_cfg_out_in_ppm;
257 PIOS_BOARD_IO_Configure_PPM_RCVR(&pios_ppm_cfg);
259 break;
260 #endif /* PIOS_INCLUDE_PPM */
261 case HWSETTINGS_RM_RCVRPORT_OUTPUTS:
262 // configure only the servo outputs
263 pios_servo_cfg = &pios_servo_cfg_out_in;
264 break;
268 #ifdef PIOS_INCLUDE_GCSRCVR
269 PIOS_BOARD_IO_Configure_GCS_RCVR();
270 #endif
272 #ifdef PIOS_INCLUDE_OPLINKRCVR
273 PIOS_BOARD_IO_Configure_OPLink_RCVR();
274 #endif
276 #ifdef PIOS_INCLUDE_WS2811
277 #include <pios_ws2811.h>
278 HwSettingsWS2811LED_OutOptions ws2811_pin_settings;
279 HwSettingsWS2811LED_OutGet(&ws2811_pin_settings);
281 // No other choices but servo pin 1 on nano
282 if (ws2811_pin_settings != HWSETTINGS_WS2811LED_OUT_DISABLED) {
283 pios_tim_servoport_all_pins[0] = dummmy_timer; // free timer 1
284 PIOS_WS2811_Init(&pios_ws2811_id, &pios_ws2811_cfg, &pios_ws2811_pin_cfg[0]);
286 #endif // PIOS_INCLUDE_WS2811
289 #ifndef PIOS_ENABLE_DEBUG_PINS
290 // pios_servo_cfg points to the correct configuration based on input port settings
291 PIOS_Servo_Init(pios_servo_cfg);
292 #else
293 PIOS_DEBUG_Init(pios_tim_servoport_all_pins, NELEMENTS(pios_tim_servoport_all_pins));
294 #endif
296 PIOS_DELAY_WaitmS(50);
298 PIOS_BOARD_Sensors_Configure();
300 // Attach the board config check hook
301 SANITYCHECK_AttachHook(&RevoNanoConfigHook);
302 // trigger a config check if actuatorsettings are updated
303 ActuatorSettingsConnectCallback(ActuatorSettingsUpdatedCb);
306 SystemAlarmsExtendedAlarmStatusOptions RevoNanoConfigHook()
308 // inhibit usage of oneshot for non supported RECEIVER port modes
309 uint8_t recmode;
311 HwSettingsRM_RcvrPortGet(&recmode);
312 uint8_t modes[ACTUATORSETTINGS_BANKMODE_NUMELEM];
313 ActuatorSettingsBankModeGet(modes);
315 switch ((HwSettingsRM_RcvrPortOptions)recmode) {
316 // Those modes allows oneshot usage
317 case HWSETTINGS_RM_RCVRPORT_DISABLED:
318 case HWSETTINGS_RM_RCVRPORT_PPM:
319 case HWSETTINGS_RM_RCVRPORT_PPMOUTPUTS:
320 case HWSETTINGS_RM_RCVRPORT_OUTPUTS:
321 return SYSTEMALARMS_EXTENDEDALARMSTATUS_NONE;
323 // inhibit oneshot for the following modes
324 case HWSETTINGS_RM_RCVRPORT_PWM:
325 for (uint8_t i = 0; i < ACTUATORSETTINGS_BANKMODE_NUMELEM; i++) {
326 if (modes[i] == ACTUATORSETTINGS_BANKMODE_PWMSYNC ||
327 modes[i] == ACTUATORSETTINGS_BANKMODE_ONESHOT125 ||
328 modes[i] == ACTUATORSETTINGS_BANKMODE_ONESHOT42 ||
329 modes[i] == ACTUATORSETTINGS_BANKMODE_MULTISHOT) {
330 return SYSTEMALARMS_EXTENDEDALARMSTATUS_UNSUPPORTEDCONFIG_ONESHOT;;
334 return SYSTEMALARMS_EXTENDEDALARMSTATUS_NONE;
336 default:
337 break;
340 return SYSTEMALARMS_EXTENDEDALARMSTATUS_UNSUPPORTEDCONFIG_ONESHOT;;
343 // trigger a configuration check if ActuatorSettings are changed.
344 void ActuatorSettingsUpdatedCb(__attribute__((unused)) UAVObjEvent *ev)
346 configuration_check();
349 * @}
350 * @}