update credits
[librepilot.git] / flight / targets / boards / ccf3d / firmware / pios_board.c
blobcdb980073c7e210c8c7d43e051ccf7626184f9ec
1 /**
2 *****************************************************************************
3 * @file pios_board.c
4 * @author The LibrePilot Project, http://www.librepilot.org Copyright (C) 2015-2017.
5 * The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
6 * PhoenixPilot, http://github.com/PhoenixPilot, Copyright (C) 2012
7 * @addtogroup LibrePilotSystem LibrePilot System
8 * @{
9 * @addtogroup LibrePilotCore LibrePilot Core
10 * @{
11 * @brief Defines board specific static initializers for hardware for the CCF3D 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 <manualcontrolsettings.h>
34 #include <gcsreceiver.h>
35 #include <taskinfo.h>
36 #include <sanitycheck.h>
37 #include <actuatorsettings.h>
38 #include <auxmagsettings.h>
40 #ifdef PIOS_INCLUDE_INSTRUMENTATION
41 #include <pios_instrumentation.h>
42 #endif
44 #include <pios_board_io.h>
45 #include <pios_board_sensors.h>
48 * Pull in the board-specific static HW definitions.
49 * Including .c files is a bit ugly but this allows all of
50 * the HW definitions to be const and static to limit their
51 * scope.
53 * NOTE: THIS IS THE ONLY PLACE THAT SHOULD EVER INCLUDE THIS FILE
55 #include "../board_hw_defs.c"
58 static SystemAlarmsExtendedAlarmStatusOptions CopterControlConfigHook();
59 static void ActuatorSettingsUpdatedCb(__attribute__((unused)) UAVObjEvent *ev);
61 uintptr_t pios_uavo_settings_fs_id;
62 uintptr_t pios_user_fs_id = 0;
64 /**
65 * PIOS_Board_Init()
66 * initializes all the core subsystems on this specific hardware
67 * called from System/openpilot.c
69 int32_t init_test;
70 void PIOS_Board_Init(void)
72 const struct pios_board_info *bdinfo = &pios_board_info_blob;
74 #if defined(PIOS_INCLUDE_LED)
75 const struct pios_gpio_cfg *led_cfg = PIOS_BOARD_HW_DEFS_GetLedCfg(bdinfo->board_rev);
76 PIOS_Assert(led_cfg);
77 PIOS_LED_Init(led_cfg);
78 #endif /* PIOS_INCLUDE_LED */
80 #ifdef PIOS_INCLUDE_INSTRUMENTATION
81 PIOS_Instrumentation_Init(PIOS_INSTRUMENTATION_MAX_COUNTERS);
82 #endif
84 #if defined(PIOS_INCLUDE_SPI)
85 /* Set up the SPI interface to the serial flash */
86 if (PIOS_SPI_Init(&pios_spi_flash_accel_id, &pios_spi_flash_accel_cfg_cc3d)) {
87 PIOS_Assert(0);
89 #endif
91 uintptr_t flash_id;
92 if (PIOS_Flash_Jedec_Init(&flash_id, pios_spi_flash_accel_id, 0)) {
93 PIOS_DEBUG_Assert(0);
95 if (PIOS_FLASHFS_Logfs_Init(&pios_uavo_settings_fs_id, &flashfs_m25p_cfg, &pios_jedec_flash_driver, flash_id)) {
96 PIOS_DEBUG_Assert(0);
99 /* Initialize the task monitor */
100 if (PIOS_TASK_MONITOR_Initialize(TASKINFO_RUNNING_NUMELEM)) {
101 PIOS_Assert(0);
104 /* Initialize the delayed callback library */
105 PIOS_CALLBACKSCHEDULER_Initialize();
107 /* Initialize UAVObject libraries */
108 EventDispatcherInitialize();
109 UAVObjInitialize();
110 SETTINGS_INITIALISE_ALL;
112 #if defined(PIOS_INCLUDE_RTC)
113 /* Initialize the real-time clock and its associated tick */
114 PIOS_RTC_Init(&pios_rtc_main_cfg);
115 #endif
116 PIOS_IAP_Init();
117 // check for safe mode commands from gcs
118 if (PIOS_IAP_ReadBootCmd(0) == PIOS_IAP_CLEAR_FLASH_CMD_0 &&
119 PIOS_IAP_ReadBootCmd(1) == PIOS_IAP_CLEAR_FLASH_CMD_1 &&
120 PIOS_IAP_ReadBootCmd(2) == PIOS_IAP_CLEAR_FLASH_CMD_2) {
121 PIOS_FLASHFS_Format(pios_uavo_settings_fs_id);
122 PIOS_IAP_WriteBootCmd(0, 0);
123 PIOS_IAP_WriteBootCmd(1, 0);
124 PIOS_IAP_WriteBootCmd(2, 0);
127 #ifndef ERASE_FLASH
128 #ifdef PIOS_INCLUDE_WDG
129 /* Initialize watchdog as early as possible to catch faults during init */
130 PIOS_WDG_Init();
131 #endif
132 #endif
134 /* Initialize the alarms library */
135 AlarmsInitialize();
137 /* Check for repeated boot failures */
138 uint16_t boot_count = PIOS_IAP_ReadBootCount();
139 if (boot_count < 3) {
140 PIOS_IAP_WriteBootCount(++boot_count);
141 AlarmsClear(SYSTEMALARMS_ALARM_BOOTFAULT);
142 } else {
143 /* Too many failed boot attempts, force hwsettings to defaults */
144 HwSettingsSetDefaults(HwSettingsHandle(), 0);
145 AlarmsSet(SYSTEMALARMS_ALARM_BOOTFAULT, SYSTEMALARMS_ALARM_CRITICAL);
148 /* Set up pulse timers */
149 PIOS_TIM_InitClock(&tim_1_cfg);
150 PIOS_TIM_InitClock(&tim_2_cfg);
151 PIOS_TIM_InitClock(&tim_3_cfg);
152 PIOS_TIM_InitClock(&tim_4_cfg);
154 #if defined(PIOS_INCLUDE_USB)
155 PIOS_BOARD_IO_Configure_USB();
156 #endif
158 /* Configure the main IO port */
159 static const PIOS_BOARD_IO_UART_Function usart_main_function_map[] = {
160 [HWSETTINGS_CC_MAINPORT_TELEMETRY] = PIOS_BOARD_IO_UART_TELEMETRY,
161 [HWSETTINGS_CC_MAINPORT_GPS] = PIOS_BOARD_IO_UART_GPS,
162 [HWSETTINGS_CC_MAINPORT_SBUS] = PIOS_BOARD_IO_UART_SBUS,
163 [HWSETTINGS_CC_MAINPORT_DSM] = PIOS_BOARD_IO_UART_DSM_MAIN,
164 [HWSETTINGS_CC_MAINPORT_DEBUGCONSOLE] = PIOS_BOARD_IO_UART_DEBUGCONSOLE,
165 [HWSETTINGS_CC_MAINPORT_COMBRIDGE] = PIOS_BOARD_IO_UART_COMBRIDGE,
166 [HWSETTINGS_CC_MAINPORT_OSDHK] = PIOS_BOARD_IO_UART_OSDHK,
167 [HWSETTINGS_CC_MAINPORT_MSP] = PIOS_BOARD_IO_UART_MSP,
168 [HWSETTINGS_CC_MAINPORT_MAVLINK] = PIOS_BOARD_IO_UART_MAVLINK,
171 uint8_t hwsettings_cc_mainport;
172 HwSettingsCC_MainPortGet(&hwsettings_cc_mainport);
174 if (hwsettings_cc_mainport < NELEMENTS(usart_main_function_map)) {
175 PIOS_BOARD_IO_Configure_UART(&pios_usart_main_cfg, usart_main_function_map[hwsettings_cc_mainport]);
179 /* Configure the flexi port */
180 static const PIOS_BOARD_IO_UART_Function usart_flexi_function_map[] = {
181 [HWSETTINGS_CC_FLEXIPORT_TELEMETRY] = PIOS_BOARD_IO_UART_TELEMETRY,
182 [HWSETTINGS_CC_FLEXIPORT_GPS] = PIOS_BOARD_IO_UART_GPS,
183 [HWSETTINGS_CC_FLEXIPORT_DSM] = PIOS_BOARD_IO_UART_DSM_FLEXI,
184 [HWSETTINGS_CC_FLEXIPORT_EXBUS] = PIOS_BOARD_IO_UART_EXBUS,
185 [HWSETTINGS_CC_FLEXIPORT_HOTTSUMD] = PIOS_BOARD_IO_UART_HOTT_SUMD,
186 [HWSETTINGS_CC_FLEXIPORT_HOTTSUMH] = PIOS_BOARD_IO_UART_HOTT_SUMH,
187 [HWSETTINGS_CC_FLEXIPORT_SRXL] = PIOS_BOARD_IO_UART_SRXL,
188 [HWSETTINGS_CC_FLEXIPORT_IBUS] = PIOS_BOARD_IO_UART_IBUS,
189 [HWSETTINGS_CC_FLEXIPORT_DEBUGCONSOLE] = PIOS_BOARD_IO_UART_DEBUGCONSOLE,
190 [HWSETTINGS_CC_FLEXIPORT_COMBRIDGE] = PIOS_BOARD_IO_UART_COMBRIDGE,
191 [HWSETTINGS_CC_FLEXIPORT_OSDHK] = PIOS_BOARD_IO_UART_OSDHK,
192 [HWSETTINGS_CC_FLEXIPORT_MSP] = PIOS_BOARD_IO_UART_MSP,
193 [HWSETTINGS_CC_FLEXIPORT_MAVLINK] = PIOS_BOARD_IO_UART_MAVLINK,
196 uint8_t hwsettings_cc_flexiport;
197 HwSettingsCC_FlexiPortGet(&hwsettings_cc_flexiport);
199 if (hwsettings_cc_flexiport < NELEMENTS(usart_flexi_function_map)) {
200 PIOS_BOARD_IO_Configure_UART(&pios_usart_flexi_cfg, usart_flexi_function_map[hwsettings_cc_flexiport]);
203 if (hwsettings_cc_flexiport == HWSETTINGS_CC_FLEXIPORT_PPM) {
204 #if defined(PIOS_INCLUDE_PPM_FLEXI)
205 PIOS_BOARD_IO_Configure_PPM_RCVR(&pios_ppm_flexi_cfg);
206 #endif /* PIOS_INCLUDE_PPM_FLEXI */
207 } else if (hwsettings_cc_flexiport == HWSETTINGS_CC_FLEXIPORT_I2C) {
208 // F303CC has no I2C on CC3D flexi.
211 /* Configure the rcvr port */
212 uint8_t hwsettings_rcvrport;
213 HwSettingsCC_RcvrPortGet(&hwsettings_rcvrport);
215 switch ((HwSettingsCC_RcvrPortOptions)hwsettings_rcvrport) {
216 case HWSETTINGS_CC_RCVRPORT_DISABLEDONESHOT:
217 #if defined(PIOS_INCLUDE_HCSR04)
219 uint32_t pios_hcsr04_id;
220 PIOS_HCSR04_Init(&pios_hcsr04_id, &pios_hcsr04_cfg);
222 #endif
223 break;
224 case HWSETTINGS_CC_RCVRPORT_PWMNOONESHOT:
225 #if defined(PIOS_INCLUDE_PWM)
226 PIOS_BOARD_IO_Configure_PWM_RCVR(&pios_pwm_cfg);
227 #endif /* PIOS_INCLUDE_PWM */
228 break;
229 case HWSETTINGS_CC_RCVRPORT_PPM_PIN8ONESHOT:
230 #if defined(PIOS_INCLUDE_PPM)
231 PIOS_BOARD_IO_Configure_PPM_RCVR(&pios_ppm_pin8_cfg);
232 #endif /* PIOS_INCLUDE_PPM */
233 break;
234 case HWSETTINGS_CC_RCVRPORT_PPMNOONESHOT:
235 case HWSETTINGS_CC_RCVRPORT_PPMOUTPUTSNOONESHOT:
236 #if defined(PIOS_INCLUDE_PPM)
237 PIOS_BOARD_IO_Configure_PPM_RCVR(&pios_ppm_cfg);
238 #endif /* PIOS_INCLUDE_PPM */
239 break;
240 case HWSETTINGS_CC_RCVRPORT_PPMPWMNOONESHOT:
241 /* This is a combination of PPM and PWM inputs */
242 #if defined(PIOS_INCLUDE_PPM)
243 PIOS_BOARD_IO_Configure_PPM_RCVR(&pios_ppm_cfg);
244 #endif /* PIOS_INCLUDE_PPM */
245 #if defined(PIOS_INCLUDE_PWM)
246 PIOS_BOARD_IO_Configure_PWM_RCVR(&pios_pwm_with_ppm_cfg);
247 #endif /* PIOS_INCLUDE_PWM */
248 break;
249 case HWSETTINGS_CC_RCVRPORT_OUTPUTSONESHOT:
250 break;
253 #ifdef PIOS_INCLUDE_GCSRCVR
254 PIOS_BOARD_IO_Configure_GCS_RCVR();
255 #endif
257 #ifdef PIOS_INCLUDE_OPLINKRCVR
258 PIOS_BOARD_IO_Configure_OPLink_RCVR();
259 #endif
261 #ifndef PIOS_ENABLE_DEBUG_PINS
262 switch ((HwSettingsCC_RcvrPortOptions)hwsettings_rcvrport) {
263 case HWSETTINGS_CC_RCVRPORT_DISABLEDONESHOT:
264 case HWSETTINGS_CC_RCVRPORT_PWMNOONESHOT:
265 case HWSETTINGS_CC_RCVRPORT_PPMNOONESHOT:
266 case HWSETTINGS_CC_RCVRPORT_PPMPWMNOONESHOT:
267 case HWSETTINGS_CC_RCVRPORT_PPM_PIN8ONESHOT:
268 PIOS_Servo_Init(&pios_servo_cfg);
269 break;
270 case HWSETTINGS_CC_RCVRPORT_PPMOUTPUTSNOONESHOT:
271 case HWSETTINGS_CC_RCVRPORT_OUTPUTSONESHOT:
272 PIOS_Servo_Init(&pios_servo_rcvr_cfg);
273 break;
275 #else
276 PIOS_DEBUG_Init(pios_tim_servoport_all_pins, NELEMENTS(pios_tim_servoport_all_pins));
277 #endif /* PIOS_ENABLE_DEBUG_PINS */
279 PIOS_BOARD_Sensors_Configure();
281 /* Make sure we have at least one telemetry link configured or else fail initialization */
282 PIOS_Assert(pios_com_telem_rf_id || pios_com_telem_usb_id);
284 // Attach the board config check hook
285 SANITYCHECK_AttachHook(&CopterControlConfigHook);
286 // trigger a config check if actuatorsettings are updated
287 ActuatorSettingsConnectCallback(ActuatorSettingsUpdatedCb);
290 SystemAlarmsExtendedAlarmStatusOptions CopterControlConfigHook()
292 // inhibit usage of oneshot for non supported RECEIVER port modes
293 uint8_t recmode;
295 HwSettingsCC_RcvrPortGet(&recmode);
296 uint8_t flexiMode;
297 uint8_t modes[ACTUATORSETTINGS_BANKMODE_NUMELEM];
298 ActuatorSettingsBankModeGet(modes);
299 HwSettingsCC_FlexiPortGet(&flexiMode);
301 switch ((HwSettingsCC_RcvrPortOptions)recmode) {
302 // Those modes allows oneshot usage
303 case HWSETTINGS_CC_RCVRPORT_DISABLEDONESHOT:
304 case HWSETTINGS_CC_RCVRPORT_OUTPUTSONESHOT:
305 case HWSETTINGS_CC_RCVRPORT_PPM_PIN8ONESHOT:
306 if ((recmode == HWSETTINGS_CC_RCVRPORT_PPM_PIN8ONESHOT ||
307 flexiMode == HWSETTINGS_CC_FLEXIPORT_PPM) &&
308 (modes[3] == ACTUATORSETTINGS_BANKMODE_PWMSYNC ||
309 modes[3] == ACTUATORSETTINGS_BANKMODE_ONESHOT125 ||
310 modes[3] == ACTUATORSETTINGS_BANKMODE_ONESHOT42 ||
311 modes[3] == ACTUATORSETTINGS_BANKMODE_MULTISHOT)) {
312 return SYSTEMALARMS_EXTENDEDALARMSTATUS_UNSUPPORTEDCONFIG_ONESHOT;
313 } else {
314 return SYSTEMALARMS_EXTENDEDALARMSTATUS_NONE;
317 // inhibit oneshot for the following modes
318 case HWSETTINGS_CC_RCVRPORT_PPMNOONESHOT:
319 case HWSETTINGS_CC_RCVRPORT_PPMOUTPUTSNOONESHOT:
320 case HWSETTINGS_CC_RCVRPORT_PPMPWMNOONESHOT:
321 case HWSETTINGS_CC_RCVRPORT_PWMNOONESHOT:
322 for (uint8_t i = 0; i < ACTUATORSETTINGS_BANKMODE_NUMELEM; i++) {
323 if (modes[i] == ACTUATORSETTINGS_BANKMODE_PWMSYNC ||
324 modes[i] == ACTUATORSETTINGS_BANKMODE_ONESHOT125 ||
325 modes[i] == ACTUATORSETTINGS_BANKMODE_ONESHOT42 ||
326 modes[i] == ACTUATORSETTINGS_BANKMODE_MULTISHOT) {
327 return SYSTEMALARMS_EXTENDEDALARMSTATUS_UNSUPPORTEDCONFIG_ONESHOT;;
330 return SYSTEMALARMS_EXTENDEDALARMSTATUS_NONE;
333 return SYSTEMALARMS_EXTENDEDALARMSTATUS_UNSUPPORTEDCONFIG_ONESHOT;;
335 // trigger a configuration check if ActuatorSettings are changed.
336 void ActuatorSettingsUpdatedCb(__attribute__((unused)) UAVObjEvent *ev)
338 configuration_check();
342 * @}